xref: /netbsd-src/external/gpl3/binutils.old/dist/gas/config/tc-i386.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
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:8;		/* arch string length */
130   bool skip:1;			/* show_arch should skip this. */
131   enum processor_type type;	/* arch type */
132   i386_cpu_flags enable;		/* cpu feature enable flags */
133   i386_cpu_flags disable;	/* cpu feature disable flags */
134 }
135 arch_entry;
136 
137 static void update_code_flag (int, int);
138 static void set_code_flag (int);
139 static void set_16bit_gcc_code_flag (int);
140 static void set_intel_syntax (int);
141 static void set_intel_mnemonic (int);
142 static void set_allow_index_reg (int);
143 static void set_check (int);
144 static void set_cpu_arch (int);
145 #ifdef TE_PE
146 static void pe_directive_secrel (int);
147 static void pe_directive_secidx (int);
148 #endif
149 static void signed_cons (int);
150 static char *output_invalid (int c);
151 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
152 				    const char *);
153 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
154 				       const char *);
155 static int i386_att_operand (char *);
156 static int i386_intel_operand (char *, int);
157 static int i386_intel_simplify (expressionS *);
158 static int i386_intel_parse_name (const char *, expressionS *);
159 static const reg_entry *parse_register (char *, char **);
160 static char *parse_insn (char *, char *);
161 static char *parse_operands (char *, const char *);
162 static void swap_operands (void);
163 static void swap_2_operands (unsigned int, unsigned int);
164 static enum flag_code i386_addressing_mode (void);
165 static void optimize_imm (void);
166 static void optimize_disp (void);
167 static const insn_template *match_template (char);
168 static int check_string (void);
169 static int process_suffix (void);
170 static int check_byte_reg (void);
171 static int check_long_reg (void);
172 static int check_qword_reg (void);
173 static int check_word_reg (void);
174 static int finalize_imm (void);
175 static int process_operands (void);
176 static const reg_entry *build_modrm_byte (void);
177 static void output_insn (void);
178 static void output_imm (fragS *, offsetT);
179 static void output_disp (fragS *, offsetT);
180 #ifndef I386COFF
181 static void s_bss (int);
182 #endif
183 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
184 static void handle_large_common (int small ATTRIBUTE_UNUSED);
185 
186 /* GNU_PROPERTY_X86_ISA_1_USED.  */
187 static unsigned int x86_isa_1_used;
188 /* GNU_PROPERTY_X86_FEATURE_2_USED.  */
189 static unsigned int x86_feature_2_used;
190 /* Generate x86 used ISA and feature properties.  */
191 static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
192 #endif
193 
194 static const char *default_arch = DEFAULT_ARCH;
195 
196 /* parse_register() returns this when a register alias cannot be used.  */
197 static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
198 				   { Dw2Inval, Dw2Inval } };
199 
200 static const reg_entry *reg_eax;
201 static const reg_entry *reg_ds;
202 static const reg_entry *reg_es;
203 static const reg_entry *reg_ss;
204 static const reg_entry *reg_st0;
205 static const reg_entry *reg_k0;
206 
207 /* VEX prefix.  */
208 typedef struct
209 {
210   /* VEX prefix is either 2 byte or 3 byte.  EVEX is 4 byte.  */
211   unsigned char bytes[4];
212   unsigned int length;
213   /* Destination or source register specifier.  */
214   const reg_entry *register_specifier;
215 } vex_prefix;
216 
217 /* 'md_assemble ()' gathers together information and puts it into a
218    i386_insn.  */
219 
220 union i386_op
221   {
222     expressionS *disps;
223     expressionS *imms;
224     const reg_entry *regs;
225   };
226 
227 enum i386_error
228   {
229     operand_size_mismatch,
230     operand_type_mismatch,
231     register_type_mismatch,
232     number_of_operands_mismatch,
233     invalid_instruction_suffix,
234     bad_imm4,
235     unsupported_with_intel_mnemonic,
236     unsupported_syntax,
237     unsupported,
238     invalid_sib_address,
239     invalid_vsib_address,
240     invalid_vector_register_set,
241     invalid_tmm_register_set,
242     invalid_dest_and_src_register_set,
243     unsupported_vector_index_register,
244     unsupported_broadcast,
245     broadcast_needed,
246     unsupported_masking,
247     mask_not_on_destination,
248     no_default_mask,
249     unsupported_rc_sae,
250     invalid_register_operand,
251   };
252 
253 struct _i386_insn
254   {
255     /* TM holds the template for the insn were currently assembling.  */
256     insn_template tm;
257 
258     /* SUFFIX holds the instruction size suffix for byte, word, dword
259        or qword, if given.  */
260     char suffix;
261 
262     /* OPCODE_LENGTH holds the number of base opcode bytes.  */
263     unsigned char opcode_length;
264 
265     /* OPERANDS gives the number of given operands.  */
266     unsigned int operands;
267 
268     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
269        of given register, displacement, memory operands and immediate
270        operands.  */
271     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
272 
273     /* TYPES [i] is the type (see above #defines) which tells us how to
274        use OP[i] for the corresponding operand.  */
275     i386_operand_type types[MAX_OPERANDS];
276 
277     /* Displacement expression, immediate expression, or register for each
278        operand.  */
279     union i386_op op[MAX_OPERANDS];
280 
281     /* Flags for operands.  */
282     unsigned int flags[MAX_OPERANDS];
283 #define Operand_PCrel 1
284 #define Operand_Mem   2
285 
286     /* Relocation type for operand */
287     enum bfd_reloc_code_real reloc[MAX_OPERANDS];
288 
289     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
290        the base index byte below.  */
291     const reg_entry *base_reg;
292     const reg_entry *index_reg;
293     unsigned int log2_scale_factor;
294 
295     /* SEG gives the seg_entries of this insn.  They are zero unless
296        explicit segment overrides are given.  */
297     const reg_entry *seg[2];
298 
299     /* Copied first memory operand string, for re-checking.  */
300     char *memop1_string;
301 
302     /* PREFIX holds all the given prefix opcodes (usually null).
303        PREFIXES is the number of prefix opcodes.  */
304     unsigned int prefixes;
305     unsigned char prefix[MAX_PREFIXES];
306 
307     /* Register is in low 3 bits of opcode.  */
308     bool short_form;
309 
310     /* The operand to a branch insn indicates an absolute branch.  */
311     bool jumpabsolute;
312 
313     /* Extended states.  */
314     enum
315       {
316 	/* Use MMX state.  */
317 	xstate_mmx = 1 << 0,
318 	/* Use XMM state.  */
319 	xstate_xmm = 1 << 1,
320 	/* Use YMM state.  */
321 	xstate_ymm = 1 << 2 | xstate_xmm,
322 	/* Use ZMM state.  */
323 	xstate_zmm = 1 << 3 | xstate_ymm,
324 	/* Use TMM state.  */
325 	xstate_tmm = 1 << 4,
326 	/* Use MASK state.  */
327 	xstate_mask = 1 << 5
328       } xstate;
329 
330     /* Has GOTPC or TLS relocation.  */
331     bool has_gotpc_tls_reloc;
332 
333     /* RM and SIB are the modrm byte and the sib byte where the
334        addressing modes of this insn are encoded.  */
335     modrm_byte rm;
336     rex_byte rex;
337     rex_byte vrex;
338     sib_byte sib;
339     vex_prefix vex;
340 
341     /* Masking attributes.
342 
343        The struct describes masking, applied to OPERAND in the instruction.
344        REG is a pointer to the corresponding mask register.  ZEROING tells
345        whether merging or zeroing mask is used.  */
346     struct Mask_Operation
347     {
348       const reg_entry *reg;
349       unsigned int zeroing;
350       /* The operand where this operation is associated.  */
351       unsigned int operand;
352     } mask;
353 
354     /* Rounding control and SAE attributes.  */
355     struct RC_Operation
356     {
357       enum rc_type
358 	{
359 	  rc_none = -1,
360 	  rne,
361 	  rd,
362 	  ru,
363 	  rz,
364 	  saeonly
365 	} type;
366       /* In Intel syntax the operand modifier form is supposed to be used, but
367 	 we continue to accept the immediate forms as well.  */
368       bool modifier;
369     } rounding;
370 
371     /* Broadcasting attributes.
372 
373        The struct describes broadcasting, applied to OPERAND.  TYPE is
374        expresses the broadcast factor.  */
375     struct Broadcast_Operation
376     {
377       /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}.  */
378       unsigned int type;
379 
380       /* Index of broadcasted operand.  */
381       unsigned int operand;
382 
383       /* Number of bytes to broadcast.  */
384       unsigned int bytes;
385     } broadcast;
386 
387     /* Compressed disp8*N attribute.  */
388     unsigned int memshift;
389 
390     /* Prefer load or store in encoding.  */
391     enum
392       {
393 	dir_encoding_default = 0,
394 	dir_encoding_load,
395 	dir_encoding_store,
396 	dir_encoding_swap
397       } dir_encoding;
398 
399     /* Prefer 8bit, 16bit, 32bit displacement in encoding.  */
400     enum
401       {
402 	disp_encoding_default = 0,
403 	disp_encoding_8bit,
404 	disp_encoding_16bit,
405 	disp_encoding_32bit
406       } disp_encoding;
407 
408     /* Prefer the REX byte in encoding.  */
409     bool rex_encoding;
410 
411     /* Disable instruction size optimization.  */
412     bool no_optimize;
413 
414     /* How to encode vector instructions.  */
415     enum
416       {
417 	vex_encoding_default = 0,
418 	vex_encoding_vex,
419 	vex_encoding_vex3,
420 	vex_encoding_evex,
421 	vex_encoding_error
422       } vec_encoding;
423 
424     /* REP prefix.  */
425     const char *rep_prefix;
426 
427     /* HLE prefix.  */
428     const char *hle_prefix;
429 
430     /* Have BND prefix.  */
431     const char *bnd_prefix;
432 
433     /* Have NOTRACK prefix.  */
434     const char *notrack_prefix;
435 
436     /* Error message.  */
437     enum i386_error error;
438   };
439 
440 typedef struct _i386_insn i386_insn;
441 
442 /* Link RC type with corresponding string, that'll be looked for in
443    asm.  */
444 struct RC_name
445 {
446   enum rc_type type;
447   const char *name;
448   unsigned int len;
449 };
450 
451 static const struct RC_name RC_NamesTable[] =
452 {
453   {  rne, STRING_COMMA_LEN ("rn-sae") },
454   {  rd,  STRING_COMMA_LEN ("rd-sae") },
455   {  ru,  STRING_COMMA_LEN ("ru-sae") },
456   {  rz,  STRING_COMMA_LEN ("rz-sae") },
457   {  saeonly,  STRING_COMMA_LEN ("sae") },
458 };
459 
460 /* List of chars besides those in app.c:symbol_chars that can start an
461    operand.  Used to prevent the scrubber eating vital white-space.  */
462 const char extra_symbol_chars[] = "*%-([{}"
463 #ifdef LEX_AT
464 	"@"
465 #endif
466 #ifdef LEX_QM
467 	"?"
468 #endif
469 	;
470 
471 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))	\
472      && !defined (TE_GNU)				\
473      && !defined (TE_LINUX)				\
474      && !defined (TE_Haiku)				\
475      && !defined (TE_FreeBSD)				\
476      && !defined (TE_DragonFly)				\
477      && !defined (TE_NetBSD))
478 /* This array holds the chars that always start a comment.  If the
479    pre-processor is disabled, these aren't very useful.  The option
480    --divide will remove '/' from this list.  */
481 const char *i386_comment_chars = "#/";
482 #define SVR4_COMMENT_CHARS 1
483 #define PREFIX_SEPARATOR '\\'
484 
485 #else
486 const char *i386_comment_chars = "#";
487 #define PREFIX_SEPARATOR '/'
488 #endif
489 
490 /* This array holds the chars that only start a comment at the beginning of
491    a line.  If the line seems to have the form '# 123 filename'
492    .line and .file directives will appear in the pre-processed output.
493    Note that input_file.c hand checks for '#' at the beginning of the
494    first line of the input file.  This is because the compiler outputs
495    #NO_APP at the beginning of its output.
496    Also note that comments started like this one will always work if
497    '/' isn't otherwise defined.  */
498 const char line_comment_chars[] = "#/";
499 
500 const char line_separator_chars[] = ";";
501 
502 /* Chars that can be used to separate mant from exp in floating point
503    nums.  */
504 const char EXP_CHARS[] = "eE";
505 
506 /* Chars that mean this number is a floating point constant
507    As in 0f12.456
508    or    0d1.2345e12.  */
509 const char FLT_CHARS[] = "fFdDxXhHbB";
510 
511 /* Tables for lexical analysis.  */
512 static char mnemonic_chars[256];
513 static char register_chars[256];
514 static char operand_chars[256];
515 static char identifier_chars[256];
516 
517 /* Lexical macros.  */
518 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
519 #define is_operand_char(x) (operand_chars[(unsigned char) x])
520 #define is_register_char(x) (register_chars[(unsigned char) x])
521 #define is_space_char(x) ((x) == ' ')
522 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
523 
524 /* All non-digit non-letter characters that may occur in an operand.  */
525 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
526 
527 /* md_assemble() always leaves the strings it's passed unaltered.  To
528    effect this we maintain a stack of saved characters that we've smashed
529    with '\0's (indicating end of strings for various sub-fields of the
530    assembler instruction).  */
531 static char save_stack[32];
532 static char *save_stack_p;
533 #define END_STRING_AND_SAVE(s) \
534 	do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
535 #define RESTORE_END_STRING(s) \
536 	do { *(s) = *--save_stack_p; } while (0)
537 
538 /* The instruction we're assembling.  */
539 static i386_insn i;
540 
541 /* Possible templates for current insn.  */
542 static const templates *current_templates;
543 
544 /* Per instruction expressionS buffers: max displacements & immediates.  */
545 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
546 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
547 
548 /* Current operand we are working on.  */
549 static int this_operand = -1;
550 
551 /* We support four different modes.  FLAG_CODE variable is used to distinguish
552    these.  */
553 
554 enum flag_code {
555 	CODE_32BIT,
556 	CODE_16BIT,
557 	CODE_64BIT };
558 
559 static enum flag_code flag_code;
560 static unsigned int object_64bit;
561 static unsigned int disallow_64bit_reloc;
562 static int use_rela_relocations = 0;
563 /* __tls_get_addr/___tls_get_addr symbol for TLS.  */
564 static const char *tls_get_addr;
565 
566 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
567      || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
568      || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
569 
570 /* The ELF ABI to use.  */
571 enum x86_elf_abi
572 {
573   I386_ABI,
574   X86_64_ABI,
575   X86_64_X32_ABI
576 };
577 
578 static enum x86_elf_abi x86_elf_abi = I386_ABI;
579 #endif
580 
581 #if defined (TE_PE) || defined (TE_PEP)
582 /* Use big object file format.  */
583 static int use_big_obj = 0;
584 #endif
585 
586 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
587 /* 1 if generating code for a shared library.  */
588 static int shared = 0;
589 #endif
590 
591 /* 1 for intel syntax,
592    0 if att syntax.  */
593 static int intel_syntax = 0;
594 
595 static enum x86_64_isa
596 {
597   amd64 = 1,	/* AMD64 ISA.  */
598   intel64	/* Intel64 ISA.  */
599 } isa64;
600 
601 /* 1 for intel mnemonic,
602    0 if att mnemonic.  */
603 static int intel_mnemonic = !SYSV386_COMPAT;
604 
605 /* 1 if pseudo registers are permitted.  */
606 static int allow_pseudo_reg = 0;
607 
608 /* 1 if register prefix % not required.  */
609 static int allow_naked_reg = 0;
610 
611 /* 1 if the assembler should add BND prefix for all control-transferring
612    instructions supporting it, even if this prefix wasn't specified
613    explicitly.  */
614 static int add_bnd_prefix = 0;
615 
616 /* 1 if pseudo index register, eiz/riz, is allowed .  */
617 static int allow_index_reg = 0;
618 
619 /* 1 if the assembler should ignore LOCK prefix, even if it was
620    specified explicitly.  */
621 static int omit_lock_prefix = 0;
622 
623 /* 1 if the assembler should encode lfence, mfence, and sfence as
624    "lock addl $0, (%{re}sp)".  */
625 static int avoid_fence = 0;
626 
627 /* 1 if lfence should be inserted after every load.  */
628 static int lfence_after_load = 0;
629 
630 /* Non-zero if lfence should be inserted before indirect branch.  */
631 static enum lfence_before_indirect_branch_kind
632   {
633     lfence_branch_none = 0,
634     lfence_branch_register,
635     lfence_branch_memory,
636     lfence_branch_all
637   }
638 lfence_before_indirect_branch;
639 
640 /* Non-zero if lfence should be inserted before ret.  */
641 static enum lfence_before_ret_kind
642   {
643     lfence_before_ret_none = 0,
644     lfence_before_ret_not,
645     lfence_before_ret_or,
646     lfence_before_ret_shl
647   }
648 lfence_before_ret;
649 
650 /* Types of previous instruction is .byte or prefix.  */
651 static struct
652   {
653     segT seg;
654     const char *file;
655     const char *name;
656     unsigned int line;
657     enum last_insn_kind
658       {
659 	last_insn_other = 0,
660 	last_insn_directive,
661 	last_insn_prefix
662       } kind;
663   } last_insn;
664 
665 /* 1 if the assembler should generate relax relocations.  */
666 
667 static int generate_relax_relocations
668   = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
669 
670 static enum check_kind
671   {
672     check_none = 0,
673     check_warning,
674     check_error
675   }
676 sse_check, operand_check = check_warning;
677 
678 /* Non-zero if branches should be aligned within power of 2 boundary.  */
679 static int align_branch_power = 0;
680 
681 /* Types of branches to align.  */
682 enum align_branch_kind
683   {
684     align_branch_none = 0,
685     align_branch_jcc = 1,
686     align_branch_fused = 2,
687     align_branch_jmp = 3,
688     align_branch_call = 4,
689     align_branch_indirect = 5,
690     align_branch_ret = 6
691   };
692 
693 /* Type bits of branches to align.  */
694 enum align_branch_bit
695   {
696     align_branch_jcc_bit = 1 << align_branch_jcc,
697     align_branch_fused_bit = 1 << align_branch_fused,
698     align_branch_jmp_bit = 1 << align_branch_jmp,
699     align_branch_call_bit = 1 << align_branch_call,
700     align_branch_indirect_bit = 1 << align_branch_indirect,
701     align_branch_ret_bit = 1 << align_branch_ret
702   };
703 
704 static unsigned int align_branch = (align_branch_jcc_bit
705 				    | align_branch_fused_bit
706 				    | align_branch_jmp_bit);
707 
708 /* Types of condition jump used by macro-fusion.  */
709 enum mf_jcc_kind
710   {
711     mf_jcc_jo = 0,  /* base opcode 0x70  */
712     mf_jcc_jc,      /* base opcode 0x72  */
713     mf_jcc_je,      /* base opcode 0x74  */
714     mf_jcc_jna,     /* base opcode 0x76  */
715     mf_jcc_js,      /* base opcode 0x78  */
716     mf_jcc_jp,      /* base opcode 0x7a  */
717     mf_jcc_jl,      /* base opcode 0x7c  */
718     mf_jcc_jle,     /* base opcode 0x7e  */
719   };
720 
721 /* Types of compare flag-modifying insntructions used by macro-fusion.  */
722 enum mf_cmp_kind
723   {
724     mf_cmp_test_and,  /* test/cmp */
725     mf_cmp_alu_cmp,  /* add/sub/cmp */
726     mf_cmp_incdec  /* inc/dec */
727   };
728 
729 /* The maximum padding size for fused jcc.  CMP like instruction can
730    be 9 bytes and jcc can be 6 bytes.  Leave room just in case for
731    prefixes.   */
732 #define MAX_FUSED_JCC_PADDING_SIZE 20
733 
734 /* The maximum number of prefixes added for an instruction.  */
735 static unsigned int align_branch_prefix_size = 5;
736 
737 /* Optimization:
738    1. Clear the REX_W bit with register operand if possible.
739    2. Above plus use 128bit vector instruction to clear the full vector
740       register.
741  */
742 static int optimize = 0;
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    3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
749       "testb $imm7,%r8".
750  */
751 static int optimize_for_space = 0;
752 
753 /* Register prefix used for error message.  */
754 static const char *register_prefix = "%";
755 
756 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
757    leave, push, and pop instructions so that gcc has the same stack
758    frame as in 32 bit mode.  */
759 static char stackop_size = '\0';
760 
761 /* Non-zero to optimize code alignment.  */
762 int optimize_align_code = 1;
763 
764 /* Non-zero to quieten some warnings.  */
765 static int quiet_warnings = 0;
766 
767 /* CPU name.  */
768 static const char *cpu_arch_name = NULL;
769 static char *cpu_sub_arch_name = NULL;
770 
771 /* CPU feature flags.  */
772 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
773 
774 /* If we have selected a cpu we are generating instructions for.  */
775 static int cpu_arch_tune_set = 0;
776 
777 /* Cpu we are generating instructions for.  */
778 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
779 
780 /* CPU feature flags of cpu we are generating instructions for.  */
781 static i386_cpu_flags cpu_arch_tune_flags;
782 
783 /* CPU instruction set architecture used.  */
784 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
785 
786 /* CPU feature flags of instruction set architecture used.  */
787 i386_cpu_flags cpu_arch_isa_flags;
788 
789 /* If set, conditional jumps are not automatically promoted to handle
790    larger than a byte offset.  */
791 static bool no_cond_jump_promotion = false;
792 
793 /* Encode SSE instructions with VEX prefix.  */
794 static unsigned int sse2avx;
795 
796 /* Encode aligned vector move as unaligned vector move.  */
797 static unsigned int use_unaligned_vector_move;
798 
799 /* Encode scalar AVX instructions with specific vector length.  */
800 static enum
801   {
802     vex128 = 0,
803     vex256
804   } avxscalar;
805 
806 /* Encode VEX WIG instructions with specific vex.w.  */
807 static enum
808   {
809     vexw0 = 0,
810     vexw1
811   } vexwig;
812 
813 /* Encode scalar EVEX LIG instructions with specific vector length.  */
814 static enum
815   {
816     evexl128 = 0,
817     evexl256,
818     evexl512
819   } evexlig;
820 
821 /* Encode EVEX WIG instructions with specific evex.w.  */
822 static enum
823   {
824     evexw0 = 0,
825     evexw1
826   } evexwig;
827 
828 /* Value to encode in EVEX RC bits, for SAE-only instructions.  */
829 static enum rc_type evexrcig = rne;
830 
831 /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
832 static symbolS *GOT_symbol;
833 
834 /* The dwarf2 return column, adjusted for 32 or 64 bit.  */
835 unsigned int x86_dwarf2_return_column;
836 
837 /* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
838 int x86_cie_data_alignment;
839 
840 /* Interface to relax_segment.
841    There are 3 major relax states for 386 jump insns because the
842    different types of jumps add different sizes to frags when we're
843    figuring out what sort of jump to choose to reach a given label.
844 
845    BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
846    branches which are handled by md_estimate_size_before_relax() and
847    i386_generic_table_relax_frag().  */
848 
849 /* Types.  */
850 #define UNCOND_JUMP 0
851 #define COND_JUMP 1
852 #define COND_JUMP86 2
853 #define BRANCH_PADDING 3
854 #define BRANCH_PREFIX 4
855 #define FUSED_JCC_PADDING 5
856 
857 /* Sizes.  */
858 #define CODE16	1
859 #define SMALL	0
860 #define SMALL16 (SMALL | CODE16)
861 #define BIG	2
862 #define BIG16	(BIG | CODE16)
863 
864 #ifndef INLINE
865 #ifdef __GNUC__
866 #define INLINE __inline__
867 #else
868 #define INLINE
869 #endif
870 #endif
871 
872 #define ENCODE_RELAX_STATE(type, size) \
873   ((relax_substateT) (((type) << 2) | (size)))
874 #define TYPE_FROM_RELAX_STATE(s) \
875   ((s) >> 2)
876 #define DISP_SIZE_FROM_RELAX_STATE(s) \
877     ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
878 
879 /* This table is used by relax_frag to promote short jumps to long
880    ones where necessary.  SMALL (short) jumps may be promoted to BIG
881    (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
882    don't allow a short jump in a 32 bit code segment to be promoted to
883    a 16 bit offset jump because it's slower (requires data size
884    prefix), and doesn't work, unless the destination is in the bottom
885    64k of the code segment (The top 16 bits of eip are zeroed).  */
886 
887 const relax_typeS md_relax_table[] =
888 {
889   /* The fields are:
890      1) most positive reach of this state,
891      2) most negative reach of this state,
892      3) how many bytes this mode will have in the variable part of the frag
893      4) which index into the table to try if we can't fit into this one.  */
894 
895   /* UNCOND_JUMP states.  */
896   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
897   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
898   /* dword jmp adds 4 bytes to frag:
899      0 extra opcode bytes, 4 displacement bytes.  */
900   {0, 0, 4, 0},
901   /* word jmp adds 2 byte2 to frag:
902      0 extra opcode bytes, 2 displacement bytes.  */
903   {0, 0, 2, 0},
904 
905   /* COND_JUMP states.  */
906   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
907   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
908   /* dword conditionals adds 5 bytes to frag:
909      1 extra opcode byte, 4 displacement bytes.  */
910   {0, 0, 5, 0},
911   /* word conditionals add 3 bytes to frag:
912      1 extra opcode byte, 2 displacement bytes.  */
913   {0, 0, 3, 0},
914 
915   /* COND_JUMP86 states.  */
916   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
917   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
918   /* dword conditionals adds 5 bytes to frag:
919      1 extra opcode byte, 4 displacement bytes.  */
920   {0, 0, 5, 0},
921   /* word conditionals add 4 bytes to frag:
922      1 displacement byte and a 3 byte long branch insn.  */
923   {0, 0, 4, 0}
924 };
925 
926 #define ARCH(n, t, f, s) \
927   { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
928     CPU_NONE_FLAGS }
929 #define SUBARCH(n, e, d, s) \
930   { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
931     CPU_ ## d ## _FLAGS }
932 
933 static const arch_entry cpu_arch[] =
934 {
935   /* Do not replace the first two entries - i386_target_format() and
936      set_cpu_arch() rely on them being there in this order.  */
937   ARCH (generic32, GENERIC32, GENERIC32, false),
938   ARCH (generic64, GENERIC64, GENERIC64, false),
939   ARCH (i8086, UNKNOWN, NONE, false),
940   ARCH (i186, UNKNOWN, I186, false),
941   ARCH (i286, UNKNOWN, I286, false),
942   ARCH (i386, I386, I386, false),
943   ARCH (i486, I486, I486, false),
944   ARCH (i586, PENTIUM, I586, false),
945   ARCH (i686, PENTIUMPRO, I686, false),
946   ARCH (pentium, PENTIUM, I586, false),
947   ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
948   ARCH (pentiumii, PENTIUMPRO, P2, false),
949   ARCH (pentiumiii, PENTIUMPRO, P3, false),
950   ARCH (pentium4, PENTIUM4, P4, false),
951   ARCH (prescott, NOCONA, CORE, false),
952   ARCH (nocona, NOCONA, NOCONA, false),
953   ARCH (yonah, CORE, CORE, true),
954   ARCH (core, CORE, CORE, false),
955   ARCH (merom, CORE2, CORE2, true),
956   ARCH (core2, CORE2, CORE2, false),
957   ARCH (corei7, COREI7, COREI7, false),
958   ARCH (iamcu, IAMCU, IAMCU, false),
959   ARCH (k6, K6, K6, false),
960   ARCH (k6_2, K6, K6_2, false),
961   ARCH (athlon, ATHLON, ATHLON, false),
962   ARCH (sledgehammer, K8, K8, true),
963   ARCH (opteron, K8, K8, false),
964   ARCH (k8, K8, K8, false),
965   ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
966   ARCH (bdver1, BD, BDVER1, false),
967   ARCH (bdver2, BD, BDVER2, false),
968   ARCH (bdver3, BD, BDVER3, false),
969   ARCH (bdver4, BD, BDVER4, false),
970   ARCH (znver1, ZNVER, ZNVER1, false),
971   ARCH (znver2, ZNVER, ZNVER2, false),
972   ARCH (znver3, ZNVER, ZNVER3, false),
973   ARCH (btver1, BT, BTVER1, false),
974   ARCH (btver2, BT, BTVER2, false),
975 
976   SUBARCH (8087, 8087, ANY_X87, false),
977   SUBARCH (87, NONE, ANY_X87, false), /* Disable only!  */
978   SUBARCH (287, 287, ANY_287, false),
979   SUBARCH (387, 387, ANY_387, false),
980   SUBARCH (687, 687, ANY_687, false),
981   SUBARCH (cmov, CMOV, ANY_CMOV, false),
982   SUBARCH (fxsr, FXSR, ANY_FXSR, false),
983   SUBARCH (mmx, MMX, ANY_MMX, false),
984   SUBARCH (sse, SSE, ANY_SSE, false),
985   SUBARCH (sse2, SSE2, ANY_SSE2, false),
986   SUBARCH (sse3, SSE3, ANY_SSE3, false),
987   SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
988   SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
989   SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
990   SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
991   SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
992   SUBARCH (avx, AVX, ANY_AVX, false),
993   SUBARCH (avx2, AVX2, ANY_AVX2, false),
994   SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
995   SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
996   SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
997   SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
998   SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
999   SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1000   SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1001   SUBARCH (vmx, VMX, VMX, false),
1002   SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1003   SUBARCH (smx, SMX, SMX, false),
1004   SUBARCH (xsave, XSAVE, XSAVE, false),
1005   SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1006   SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1007   SUBARCH (xsaves, XSAVES, XSAVES, false),
1008   SUBARCH (aes, AES, AES, false),
1009   SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1010   SUBARCH (clmul, PCLMUL, PCLMUL, true),
1011   SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1012   SUBARCH (rdrnd, RDRND, RDRND, false),
1013   SUBARCH (f16c, F16C, F16C, false),
1014   SUBARCH (bmi2, BMI2, BMI2, false),
1015   SUBARCH (fma, FMA, FMA, false),
1016   SUBARCH (fma4, FMA4, FMA4, false),
1017   SUBARCH (xop, XOP, XOP, false),
1018   SUBARCH (lwp, LWP, LWP, false),
1019   SUBARCH (movbe, MOVBE, MOVBE, false),
1020   SUBARCH (cx16, CX16, CX16, false),
1021   SUBARCH (ept, EPT, EPT, false),
1022   SUBARCH (lzcnt, LZCNT, LZCNT, false),
1023   SUBARCH (popcnt, POPCNT, POPCNT, false),
1024   SUBARCH (hle, HLE, HLE, false),
1025   SUBARCH (rtm, RTM, RTM, false),
1026   SUBARCH (invpcid, INVPCID, INVPCID, false),
1027   SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1028   SUBARCH (nop, NOP, NOP, false),
1029   SUBARCH (syscall, SYSCALL, SYSCALL, false),
1030   SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1031   SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1032   SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1033   SUBARCH (padlock, PADLOCK, PADLOCK, false),
1034   SUBARCH (pacifica, SVME, SVME, true),
1035   SUBARCH (svme, SVME, SVME, false),
1036   SUBARCH (sse4a, SSE4A, SSE4A, false),
1037   SUBARCH (abm, ABM, ABM, false),
1038   SUBARCH (bmi, BMI, BMI, false),
1039   SUBARCH (tbm, TBM, TBM, false),
1040   SUBARCH (adx, ADX, ADX, false),
1041   SUBARCH (rdseed, RDSEED, RDSEED, false),
1042   SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1043   SUBARCH (smap, SMAP, SMAP, false),
1044   SUBARCH (mpx, MPX, MPX, false),
1045   SUBARCH (sha, SHA, SHA, false),
1046   SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1047   SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1048   SUBARCH (se1, SE1, SE1, false),
1049   SUBARCH (clwb, CLWB, CLWB, false),
1050   SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1051   SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1052   SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1053   SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1054   SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1055   SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1056   SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1057   SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1058   SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1059   SUBARCH (clzero, CLZERO, CLZERO, false),
1060   SUBARCH (mwaitx, MWAITX, MWAITX, false),
1061   SUBARCH (ospke, OSPKE, OSPKE, false),
1062   SUBARCH (rdpid, RDPID, RDPID, false),
1063   SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1064   SUBARCH (ibt, IBT, ANY_IBT, false),
1065   SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1066   SUBARCH (gfni, GFNI, GFNI, false),
1067   SUBARCH (vaes, VAES, VAES, false),
1068   SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1069   SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1070   SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1071   SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1072   SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1073   SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1074   SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1075   SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1076   SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1077   SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1078   SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1079   SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1080 	   ANY_AVX512_VP2INTERSECT, false),
1081   SUBARCH (tdx, TDX, ANY_TDX, false),
1082   SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1083   SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1084   SUBARCH (rdpru, RDPRU, RDPRU, false),
1085   SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1086   SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1087   SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1088   SUBARCH (kl, KL, ANY_KL, false),
1089   SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1090   SUBARCH (uintr, UINTR, ANY_UINTR, false),
1091   SUBARCH (hreset, HRESET, ANY_HRESET, false),
1092   SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
1093 };
1094 
1095 #undef SUBARCH
1096 #undef ARCH
1097 
1098 #ifdef I386COFF
1099 /* Like s_lcomm_internal in gas/read.c but the alignment string
1100    is allowed to be optional.  */
1101 
1102 static symbolS *
pe_lcomm_internal(int needs_align,symbolS * symbolP,addressT size)1103 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1104 {
1105   addressT align = 0;
1106 
1107   SKIP_WHITESPACE ();
1108 
1109   if (needs_align
1110       && *input_line_pointer == ',')
1111     {
1112       align = parse_align (needs_align - 1);
1113 
1114       if (align == (addressT) -1)
1115 	return NULL;
1116     }
1117   else
1118     {
1119       if (size >= 8)
1120 	align = 3;
1121       else if (size >= 4)
1122 	align = 2;
1123       else if (size >= 2)
1124 	align = 1;
1125       else
1126 	align = 0;
1127     }
1128 
1129   bss_alloc (symbolP, size, align);
1130   return symbolP;
1131 }
1132 
1133 static void
pe_lcomm(int needs_align)1134 pe_lcomm (int needs_align)
1135 {
1136   s_comm_internal (needs_align * 2, pe_lcomm_internal);
1137 }
1138 #endif
1139 
1140 const pseudo_typeS md_pseudo_table[] =
1141 {
1142 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1143   {"align", s_align_bytes, 0},
1144 #else
1145   {"align", s_align_ptwo, 0},
1146 #endif
1147   {"arch", set_cpu_arch, 0},
1148 #ifndef I386COFF
1149   {"bss", s_bss, 0},
1150 #else
1151   {"lcomm", pe_lcomm, 1},
1152 #endif
1153   {"ffloat", float_cons, 'f'},
1154   {"dfloat", float_cons, 'd'},
1155   {"tfloat", float_cons, 'x'},
1156   {"hfloat", float_cons, 'h'},
1157   {"bfloat16", float_cons, 'b'},
1158   {"value", cons, 2},
1159   {"slong", signed_cons, 4},
1160   {"noopt", s_ignore, 0},
1161   {"optim", s_ignore, 0},
1162   {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1163   {"code16", set_code_flag, CODE_16BIT},
1164   {"code32", set_code_flag, CODE_32BIT},
1165 #ifdef BFD64
1166   {"code64", set_code_flag, CODE_64BIT},
1167 #endif
1168   {"intel_syntax", set_intel_syntax, 1},
1169   {"att_syntax", set_intel_syntax, 0},
1170   {"intel_mnemonic", set_intel_mnemonic, 1},
1171   {"att_mnemonic", set_intel_mnemonic, 0},
1172   {"allow_index_reg", set_allow_index_reg, 1},
1173   {"disallow_index_reg", set_allow_index_reg, 0},
1174   {"sse_check", set_check, 0},
1175   {"operand_check", set_check, 1},
1176 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1177   {"largecomm", handle_large_common, 0},
1178 #else
1179   {"file", dwarf2_directive_file, 0},
1180   {"loc", dwarf2_directive_loc, 0},
1181   {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1182 #endif
1183 #ifdef TE_PE
1184   {"secrel32", pe_directive_secrel, 0},
1185   {"secidx", pe_directive_secidx, 0},
1186 #endif
1187   {0, 0, 0}
1188 };
1189 
1190 /* For interface with expression ().  */
1191 extern char *input_line_pointer;
1192 
1193 /* Hash table for instruction mnemonic lookup.  */
1194 static htab_t op_hash;
1195 
1196 /* Hash table for register lookup.  */
1197 static htab_t reg_hash;
1198 
1199   /* Various efficient no-op patterns for aligning code labels.
1200      Note: Don't try to assemble the instructions in the comments.
1201      0L and 0w are not legal.  */
1202 static const unsigned char f32_1[] =
1203   {0x90};				/* nop			*/
1204 static const unsigned char f32_2[] =
1205   {0x66,0x90};				/* xchg %ax,%ax		*/
1206 static const unsigned char f32_3[] =
1207   {0x8d,0x76,0x00};			/* leal 0(%esi),%esi	*/
1208 static const unsigned char f32_4[] =
1209   {0x8d,0x74,0x26,0x00};		/* leal 0(%esi,1),%esi	*/
1210 static const unsigned char f32_6[] =
1211   {0x8d,0xb6,0x00,0x00,0x00,0x00};	/* leal 0L(%esi),%esi	*/
1212 static const unsigned char f32_7[] =
1213   {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};	/* leal 0L(%esi,1),%esi */
1214 static const unsigned char f16_3[] =
1215   {0x8d,0x74,0x00};			/* lea 0(%si),%si	*/
1216 static const unsigned char f16_4[] =
1217   {0x8d,0xb4,0x00,0x00};		/* lea 0W(%si),%si	*/
1218 static const unsigned char jump_disp8[] =
1219   {0xeb};				/* jmp disp8	       */
1220 static const unsigned char jump32_disp32[] =
1221   {0xe9};				/* jmp disp32	       */
1222 static const unsigned char jump16_disp32[] =
1223   {0x66,0xe9};				/* jmp disp32	       */
1224 /* 32-bit NOPs patterns.  */
1225 static const unsigned char *const f32_patt[] = {
1226   f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1227 };
1228 /* 16-bit NOPs patterns.  */
1229 static const unsigned char *const f16_patt[] = {
1230   f32_1, f32_2, f16_3, f16_4
1231 };
1232 /* nopl (%[re]ax) */
1233 static const unsigned char alt_3[] =
1234   {0x0f,0x1f,0x00};
1235 /* nopl 0(%[re]ax) */
1236 static const unsigned char alt_4[] =
1237   {0x0f,0x1f,0x40,0x00};
1238 /* nopl 0(%[re]ax,%[re]ax,1) */
1239 static const unsigned char alt_5[] =
1240   {0x0f,0x1f,0x44,0x00,0x00};
1241 /* nopw 0(%[re]ax,%[re]ax,1) */
1242 static const unsigned char alt_6[] =
1243   {0x66,0x0f,0x1f,0x44,0x00,0x00};
1244 /* nopl 0L(%[re]ax) */
1245 static const unsigned char alt_7[] =
1246   {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1247 /* nopl 0L(%[re]ax,%[re]ax,1) */
1248 static const unsigned char alt_8[] =
1249   {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1250 /* nopw 0L(%[re]ax,%[re]ax,1) */
1251 static const unsigned char alt_9[] =
1252   {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1253 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1254 static const unsigned char alt_10[] =
1255   {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1256 /* data16 nopw %cs:0L(%eax,%eax,1) */
1257 static const unsigned char alt_11[] =
1258   {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1259 /* 32-bit and 64-bit NOPs patterns.  */
1260 static const unsigned char *const alt_patt[] = {
1261   f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1262   alt_9, alt_10, alt_11
1263 };
1264 
1265 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1266    size of a single NOP instruction MAX_SINGLE_NOP_SIZE.  */
1267 
1268 static void
i386_output_nops(char * where,const unsigned char * const * patt,int count,int max_single_nop_size)1269 i386_output_nops (char *where, const unsigned char *const *patt,
1270 		  int count, int max_single_nop_size)
1271 
1272 {
1273   /* Place the longer NOP first.  */
1274   int last;
1275   int offset;
1276   const unsigned char *nops;
1277 
1278   if (max_single_nop_size < 1)
1279     {
1280       as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1281 		max_single_nop_size);
1282       return;
1283     }
1284 
1285   nops = patt[max_single_nop_size - 1];
1286 
1287   /* Use the smaller one if the requsted one isn't available.  */
1288   if (nops == NULL)
1289     {
1290       max_single_nop_size--;
1291       nops = patt[max_single_nop_size - 1];
1292     }
1293 
1294   last = count % max_single_nop_size;
1295 
1296   count -= last;
1297   for (offset = 0; offset < count; offset += max_single_nop_size)
1298     memcpy (where + offset, nops, max_single_nop_size);
1299 
1300   if (last)
1301     {
1302       nops = patt[last - 1];
1303       if (nops == NULL)
1304 	{
1305 	  /* Use the smaller one plus one-byte NOP if the needed one
1306 	     isn't available.  */
1307 	  last--;
1308 	  nops = patt[last - 1];
1309 	  memcpy (where + offset, nops, last);
1310 	  where[offset + last] = *patt[0];
1311 	}
1312       else
1313 	memcpy (where + offset, nops, last);
1314     }
1315 }
1316 
1317 static INLINE int
fits_in_imm7(offsetT num)1318 fits_in_imm7 (offsetT num)
1319 {
1320   return (num & 0x7f) == num;
1321 }
1322 
1323 static INLINE int
fits_in_imm31(offsetT num)1324 fits_in_imm31 (offsetT num)
1325 {
1326   return (num & 0x7fffffff) == num;
1327 }
1328 
1329 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1330    single NOP instruction LIMIT.  */
1331 
1332 void
i386_generate_nops(fragS * fragP,char * where,offsetT count,int limit)1333 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1334 {
1335   const unsigned char *const *patt = NULL;
1336   int max_single_nop_size;
1337   /* Maximum number of NOPs before switching to jump over NOPs.  */
1338   int max_number_of_nops;
1339 
1340   switch (fragP->fr_type)
1341     {
1342     case rs_fill_nop:
1343     case rs_align_code:
1344       break;
1345     case rs_machine_dependent:
1346       /* Allow NOP padding for jumps and calls.  */
1347       if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1348 	  || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1349 	break;
1350       /* Fall through.  */
1351     default:
1352       return;
1353     }
1354 
1355   /* We need to decide which NOP sequence to use for 32bit and
1356      64bit. When -mtune= is used:
1357 
1358      1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1359      PROCESSOR_GENERIC32, f32_patt will be used.
1360      2. For the rest, alt_patt will be used.
1361 
1362      When -mtune= isn't used, alt_patt will be used if
1363      cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt will
1364      be used.
1365 
1366      When -march= or .arch is used, we can't use anything beyond
1367      cpu_arch_isa_flags.   */
1368 
1369   if (flag_code == CODE_16BIT)
1370     {
1371       patt = f16_patt;
1372       max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1373       /* Limit number of NOPs to 2 in 16-bit mode.  */
1374       max_number_of_nops = 2;
1375     }
1376   else
1377     {
1378       if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1379 	{
1380 	  /* PROCESSOR_UNKNOWN means that all ISAs may be used.  */
1381 	  switch (cpu_arch_tune)
1382 	    {
1383 	    case PROCESSOR_UNKNOWN:
1384 	      /* We use cpu_arch_isa_flags to check if we SHOULD
1385 		 optimize with nops.  */
1386 	      if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1387 		patt = alt_patt;
1388 	      else
1389 		patt = f32_patt;
1390 	      break;
1391 	    case PROCESSOR_PENTIUM4:
1392 	    case PROCESSOR_NOCONA:
1393 	    case PROCESSOR_CORE:
1394 	    case PROCESSOR_CORE2:
1395 	    case PROCESSOR_COREI7:
1396 	    case PROCESSOR_GENERIC64:
1397 	    case PROCESSOR_K6:
1398 	    case PROCESSOR_ATHLON:
1399 	    case PROCESSOR_K8:
1400 	    case PROCESSOR_AMDFAM10:
1401 	    case PROCESSOR_BD:
1402 	    case PROCESSOR_ZNVER:
1403 	    case PROCESSOR_BT:
1404 	      patt = alt_patt;
1405 	      break;
1406 	    case PROCESSOR_I386:
1407 	    case PROCESSOR_I486:
1408 	    case PROCESSOR_PENTIUM:
1409 	    case PROCESSOR_PENTIUMPRO:
1410 	    case PROCESSOR_IAMCU:
1411 	    case PROCESSOR_GENERIC32:
1412 	      patt = f32_patt;
1413 	      break;
1414 	    case PROCESSOR_NONE:
1415 	      abort ();
1416 	    }
1417 	}
1418       else
1419 	{
1420 	  switch (fragP->tc_frag_data.tune)
1421 	    {
1422 	    case PROCESSOR_UNKNOWN:
1423 	      /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1424 		 PROCESSOR_UNKNOWN.  */
1425 	      abort ();
1426 	      break;
1427 
1428 	    case PROCESSOR_I386:
1429 	    case PROCESSOR_I486:
1430 	    case PROCESSOR_PENTIUM:
1431 	    case PROCESSOR_IAMCU:
1432 	    case PROCESSOR_K6:
1433 	    case PROCESSOR_ATHLON:
1434 	    case PROCESSOR_K8:
1435 	    case PROCESSOR_AMDFAM10:
1436 	    case PROCESSOR_BD:
1437 	    case PROCESSOR_ZNVER:
1438 	    case PROCESSOR_BT:
1439 	    case PROCESSOR_GENERIC32:
1440 	      /* We use cpu_arch_isa_flags to check if we CAN optimize
1441 		 with nops.  */
1442 	      if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1443 		patt = alt_patt;
1444 	      else
1445 		patt = f32_patt;
1446 	      break;
1447 	    case PROCESSOR_PENTIUMPRO:
1448 	    case PROCESSOR_PENTIUM4:
1449 	    case PROCESSOR_NOCONA:
1450 	    case PROCESSOR_CORE:
1451 	    case PROCESSOR_CORE2:
1452 	    case PROCESSOR_COREI7:
1453 	      if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1454 		patt = alt_patt;
1455 	      else
1456 		patt = f32_patt;
1457 	      break;
1458 	    case PROCESSOR_GENERIC64:
1459 	      patt = alt_patt;
1460 	      break;
1461 	    case PROCESSOR_NONE:
1462 	      abort ();
1463 	    }
1464 	}
1465 
1466       if (patt == f32_patt)
1467 	{
1468 	  max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1469 	  /* Limit number of NOPs to 2 for older processors.  */
1470 	  max_number_of_nops = 2;
1471 	}
1472       else
1473 	{
1474 	  max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1475 	  /* Limit number of NOPs to 7 for newer processors.  */
1476 	  max_number_of_nops = 7;
1477 	}
1478     }
1479 
1480   if (limit == 0)
1481     limit = max_single_nop_size;
1482 
1483   if (fragP->fr_type == rs_fill_nop)
1484     {
1485       /* Output NOPs for .nop directive.  */
1486       if (limit > max_single_nop_size)
1487 	{
1488 	  as_bad_where (fragP->fr_file, fragP->fr_line,
1489 			_("invalid single nop size: %d "
1490 			  "(expect within [0, %d])"),
1491 			limit, max_single_nop_size);
1492 	  return;
1493 	}
1494     }
1495   else if (fragP->fr_type != rs_machine_dependent)
1496     fragP->fr_var = count;
1497 
1498   if ((count / max_single_nop_size) > max_number_of_nops)
1499     {
1500       /* Generate jump over NOPs.  */
1501       offsetT disp = count - 2;
1502       if (fits_in_imm7 (disp))
1503 	{
1504 	  /* Use "jmp disp8" if possible.  */
1505 	  count = disp;
1506 	  where[0] = jump_disp8[0];
1507 	  where[1] = count;
1508 	  where += 2;
1509 	}
1510       else
1511 	{
1512 	  unsigned int size_of_jump;
1513 
1514 	  if (flag_code == CODE_16BIT)
1515 	    {
1516 	      where[0] = jump16_disp32[0];
1517 	      where[1] = jump16_disp32[1];
1518 	      size_of_jump = 2;
1519 	    }
1520 	  else
1521 	    {
1522 	      where[0] = jump32_disp32[0];
1523 	      size_of_jump = 1;
1524 	    }
1525 
1526 	  count -= size_of_jump + 4;
1527 	  if (!fits_in_imm31 (count))
1528 	    {
1529 	      as_bad_where (fragP->fr_file, fragP->fr_line,
1530 			    _("jump over nop padding out of range"));
1531 	      return;
1532 	    }
1533 
1534 	  md_number_to_chars (where + size_of_jump, count, 4);
1535 	  where += size_of_jump + 4;
1536 	}
1537     }
1538 
1539   /* Generate multiple NOPs.  */
1540   i386_output_nops (where, patt, count, limit);
1541 }
1542 
1543 static INLINE int
operand_type_all_zero(const union i386_operand_type * x)1544 operand_type_all_zero (const union i386_operand_type *x)
1545 {
1546   switch (ARRAY_SIZE(x->array))
1547     {
1548     case 3:
1549       if (x->array[2])
1550 	return 0;
1551       /* Fall through.  */
1552     case 2:
1553       if (x->array[1])
1554 	return 0;
1555       /* Fall through.  */
1556     case 1:
1557       return !x->array[0];
1558     default:
1559       abort ();
1560     }
1561 }
1562 
1563 static INLINE void
operand_type_set(union i386_operand_type * x,unsigned int v)1564 operand_type_set (union i386_operand_type *x, unsigned int v)
1565 {
1566   switch (ARRAY_SIZE(x->array))
1567     {
1568     case 3:
1569       x->array[2] = v;
1570       /* Fall through.  */
1571     case 2:
1572       x->array[1] = v;
1573       /* Fall through.  */
1574     case 1:
1575       x->array[0] = v;
1576       /* Fall through.  */
1577       break;
1578     default:
1579       abort ();
1580     }
1581 
1582   x->bitfield.class = ClassNone;
1583   x->bitfield.instance = InstanceNone;
1584 }
1585 
1586 static INLINE int
operand_type_equal(const union i386_operand_type * x,const union i386_operand_type * y)1587 operand_type_equal (const union i386_operand_type *x,
1588 		    const union i386_operand_type *y)
1589 {
1590   switch (ARRAY_SIZE(x->array))
1591     {
1592     case 3:
1593       if (x->array[2] != y->array[2])
1594 	return 0;
1595       /* Fall through.  */
1596     case 2:
1597       if (x->array[1] != y->array[1])
1598 	return 0;
1599       /* Fall through.  */
1600     case 1:
1601       return x->array[0] == y->array[0];
1602       break;
1603     default:
1604       abort ();
1605     }
1606 }
1607 
1608 static INLINE int
cpu_flags_all_zero(const union i386_cpu_flags * x)1609 cpu_flags_all_zero (const union i386_cpu_flags *x)
1610 {
1611   switch (ARRAY_SIZE(x->array))
1612     {
1613     case 4:
1614       if (x->array[3])
1615 	return 0;
1616       /* Fall through.  */
1617     case 3:
1618       if (x->array[2])
1619 	return 0;
1620       /* Fall through.  */
1621     case 2:
1622       if (x->array[1])
1623 	return 0;
1624       /* Fall through.  */
1625     case 1:
1626       return !x->array[0];
1627     default:
1628       abort ();
1629     }
1630 }
1631 
1632 static INLINE int
cpu_flags_equal(const union i386_cpu_flags * x,const union i386_cpu_flags * y)1633 cpu_flags_equal (const union i386_cpu_flags *x,
1634 		 const union i386_cpu_flags *y)
1635 {
1636   switch (ARRAY_SIZE(x->array))
1637     {
1638     case 4:
1639       if (x->array[3] != y->array[3])
1640 	return 0;
1641       /* Fall through.  */
1642     case 3:
1643       if (x->array[2] != y->array[2])
1644 	return 0;
1645       /* Fall through.  */
1646     case 2:
1647       if (x->array[1] != y->array[1])
1648 	return 0;
1649       /* Fall through.  */
1650     case 1:
1651       return x->array[0] == y->array[0];
1652       break;
1653     default:
1654       abort ();
1655     }
1656 }
1657 
1658 static INLINE int
cpu_flags_check_cpu64(i386_cpu_flags f)1659 cpu_flags_check_cpu64 (i386_cpu_flags f)
1660 {
1661   return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1662 	   || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1663 }
1664 
1665 static INLINE i386_cpu_flags
cpu_flags_and(i386_cpu_flags x,i386_cpu_flags y)1666 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1667 {
1668   switch (ARRAY_SIZE (x.array))
1669     {
1670     case 4:
1671       x.array [3] &= y.array [3];
1672       /* Fall through.  */
1673     case 3:
1674       x.array [2] &= y.array [2];
1675       /* Fall through.  */
1676     case 2:
1677       x.array [1] &= y.array [1];
1678       /* Fall through.  */
1679     case 1:
1680       x.array [0] &= y.array [0];
1681       break;
1682     default:
1683       abort ();
1684     }
1685   return x;
1686 }
1687 
1688 static INLINE i386_cpu_flags
cpu_flags_or(i386_cpu_flags x,i386_cpu_flags y)1689 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1690 {
1691   switch (ARRAY_SIZE (x.array))
1692     {
1693     case 4:
1694       x.array [3] |= y.array [3];
1695       /* Fall through.  */
1696     case 3:
1697       x.array [2] |= y.array [2];
1698       /* Fall through.  */
1699     case 2:
1700       x.array [1] |= y.array [1];
1701       /* Fall through.  */
1702     case 1:
1703       x.array [0] |= y.array [0];
1704       break;
1705     default:
1706       abort ();
1707     }
1708   return x;
1709 }
1710 
1711 static INLINE i386_cpu_flags
cpu_flags_and_not(i386_cpu_flags x,i386_cpu_flags y)1712 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1713 {
1714   switch (ARRAY_SIZE (x.array))
1715     {
1716     case 4:
1717       x.array [3] &= ~y.array [3];
1718       /* Fall through.  */
1719     case 3:
1720       x.array [2] &= ~y.array [2];
1721       /* Fall through.  */
1722     case 2:
1723       x.array [1] &= ~y.array [1];
1724       /* Fall through.  */
1725     case 1:
1726       x.array [0] &= ~y.array [0];
1727       break;
1728     default:
1729       abort ();
1730     }
1731   return x;
1732 }
1733 
1734 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1735 
1736 #define CPU_FLAGS_ARCH_MATCH		0x1
1737 #define CPU_FLAGS_64BIT_MATCH		0x2
1738 
1739 #define CPU_FLAGS_PERFECT_MATCH \
1740   (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1741 
1742 /* Return CPU flags match bits. */
1743 
1744 static int
cpu_flags_match(const insn_template * t)1745 cpu_flags_match (const insn_template *t)
1746 {
1747   i386_cpu_flags x = t->cpu_flags;
1748   int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1749 
1750   x.bitfield.cpu64 = 0;
1751   x.bitfield.cpuno64 = 0;
1752 
1753   if (cpu_flags_all_zero (&x))
1754     {
1755       /* This instruction is available on all archs.  */
1756       match |= CPU_FLAGS_ARCH_MATCH;
1757     }
1758   else
1759     {
1760       /* This instruction is available only on some archs.  */
1761       i386_cpu_flags cpu = cpu_arch_flags;
1762 
1763       /* AVX512VL is no standalone feature - match it and then strip it.  */
1764       if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1765 	return match;
1766       x.bitfield.cpuavx512vl = 0;
1767 
1768       /* AVX and AVX2 present at the same time express an operand size
1769 	 dependency - strip AVX2 for the purposes here.  The operand size
1770 	 dependent check occurs in check_vecOperands().  */
1771       if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1772 	x.bitfield.cpuavx2 = 0;
1773 
1774       cpu = cpu_flags_and (x, cpu);
1775       if (!cpu_flags_all_zero (&cpu))
1776 	{
1777 	  if (x.bitfield.cpuavx)
1778 	    {
1779 	      /* We need to check a few extra flags with AVX.  */
1780 	      if (cpu.bitfield.cpuavx
1781 		  && (!t->opcode_modifier.sse2avx
1782 		      || (sse2avx && !i.prefix[DATA_PREFIX]))
1783 		  && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1784 		  && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1785 		  && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1786 		match |= CPU_FLAGS_ARCH_MATCH;
1787 	    }
1788 	  else if (x.bitfield.cpuavx512f)
1789 	    {
1790 	      /* We need to check a few extra flags with AVX512F.  */
1791 	      if (cpu.bitfield.cpuavx512f
1792 		  && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1793 		  && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1794 		  && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1795 		match |= CPU_FLAGS_ARCH_MATCH;
1796 	    }
1797 	  else
1798 	    match |= CPU_FLAGS_ARCH_MATCH;
1799 	}
1800     }
1801   return match;
1802 }
1803 
1804 static INLINE i386_operand_type
operand_type_and(i386_operand_type x,i386_operand_type y)1805 operand_type_and (i386_operand_type x, i386_operand_type y)
1806 {
1807   if (x.bitfield.class != y.bitfield.class)
1808     x.bitfield.class = ClassNone;
1809   if (x.bitfield.instance != y.bitfield.instance)
1810     x.bitfield.instance = InstanceNone;
1811 
1812   switch (ARRAY_SIZE (x.array))
1813     {
1814     case 3:
1815       x.array [2] &= y.array [2];
1816       /* Fall through.  */
1817     case 2:
1818       x.array [1] &= y.array [1];
1819       /* Fall through.  */
1820     case 1:
1821       x.array [0] &= y.array [0];
1822       break;
1823     default:
1824       abort ();
1825     }
1826   return x;
1827 }
1828 
1829 static INLINE i386_operand_type
operand_type_and_not(i386_operand_type x,i386_operand_type y)1830 operand_type_and_not (i386_operand_type x, i386_operand_type y)
1831 {
1832   gas_assert (y.bitfield.class == ClassNone);
1833   gas_assert (y.bitfield.instance == InstanceNone);
1834 
1835   switch (ARRAY_SIZE (x.array))
1836     {
1837     case 3:
1838       x.array [2] &= ~y.array [2];
1839       /* Fall through.  */
1840     case 2:
1841       x.array [1] &= ~y.array [1];
1842       /* Fall through.  */
1843     case 1:
1844       x.array [0] &= ~y.array [0];
1845       break;
1846     default:
1847       abort ();
1848     }
1849   return x;
1850 }
1851 
1852 static INLINE i386_operand_type
operand_type_or(i386_operand_type x,i386_operand_type y)1853 operand_type_or (i386_operand_type x, i386_operand_type y)
1854 {
1855   gas_assert (x.bitfield.class == ClassNone ||
1856               y.bitfield.class == ClassNone ||
1857               x.bitfield.class == y.bitfield.class);
1858   gas_assert (x.bitfield.instance == InstanceNone ||
1859               y.bitfield.instance == InstanceNone ||
1860               x.bitfield.instance == y.bitfield.instance);
1861 
1862   switch (ARRAY_SIZE (x.array))
1863     {
1864     case 3:
1865       x.array [2] |= y.array [2];
1866       /* Fall through.  */
1867     case 2:
1868       x.array [1] |= y.array [1];
1869       /* Fall through.  */
1870     case 1:
1871       x.array [0] |= y.array [0];
1872       break;
1873     default:
1874       abort ();
1875     }
1876   return x;
1877 }
1878 
1879 static INLINE i386_operand_type
operand_type_xor(i386_operand_type x,i386_operand_type y)1880 operand_type_xor (i386_operand_type x, i386_operand_type y)
1881 {
1882   gas_assert (y.bitfield.class == ClassNone);
1883   gas_assert (y.bitfield.instance == InstanceNone);
1884 
1885   switch (ARRAY_SIZE (x.array))
1886     {
1887     case 3:
1888       x.array [2] ^= y.array [2];
1889       /* Fall through.  */
1890     case 2:
1891       x.array [1] ^= y.array [1];
1892       /* Fall through.  */
1893     case 1:
1894       x.array [0] ^= y.array [0];
1895       break;
1896     default:
1897       abort ();
1898     }
1899   return x;
1900 }
1901 
1902 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
1903 static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
1904 static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
1905 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1906 static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
1907 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1908 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1909 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1910 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1911 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1912 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1913 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1914 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1915 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1916 
1917 enum operand_type
1918 {
1919   reg,
1920   imm,
1921   disp,
1922   anymem
1923 };
1924 
1925 static INLINE int
operand_type_check(i386_operand_type t,enum operand_type c)1926 operand_type_check (i386_operand_type t, enum operand_type c)
1927 {
1928   switch (c)
1929     {
1930     case reg:
1931       return t.bitfield.class == Reg;
1932 
1933     case imm:
1934       return (t.bitfield.imm8
1935 	      || t.bitfield.imm8s
1936 	      || t.bitfield.imm16
1937 	      || t.bitfield.imm32
1938 	      || t.bitfield.imm32s
1939 	      || t.bitfield.imm64);
1940 
1941     case disp:
1942       return (t.bitfield.disp8
1943 	      || t.bitfield.disp16
1944 	      || t.bitfield.disp32
1945 	      || t.bitfield.disp64);
1946 
1947     case anymem:
1948       return (t.bitfield.disp8
1949 	      || t.bitfield.disp16
1950 	      || t.bitfield.disp32
1951 	      || t.bitfield.disp64
1952 	      || t.bitfield.baseindex);
1953 
1954     default:
1955       abort ();
1956     }
1957 
1958   return 0;
1959 }
1960 
1961 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1962    between operand GIVEN and opeand WANTED for instruction template T.  */
1963 
1964 static INLINE int
match_operand_size(const insn_template * t,unsigned int wanted,unsigned int given)1965 match_operand_size (const insn_template *t, unsigned int wanted,
1966 		    unsigned int given)
1967 {
1968   return !((i.types[given].bitfield.byte
1969 	    && !t->operand_types[wanted].bitfield.byte)
1970 	   || (i.types[given].bitfield.word
1971 	       && !t->operand_types[wanted].bitfield.word)
1972 	   || (i.types[given].bitfield.dword
1973 	       && !t->operand_types[wanted].bitfield.dword)
1974 	   || (i.types[given].bitfield.qword
1975 	       && !t->operand_types[wanted].bitfield.qword)
1976 	   || (i.types[given].bitfield.tbyte
1977 	       && !t->operand_types[wanted].bitfield.tbyte));
1978 }
1979 
1980 /* Return 1 if there is no conflict in SIMD register between operand
1981    GIVEN and opeand WANTED for instruction template T.  */
1982 
1983 static INLINE int
match_simd_size(const insn_template * t,unsigned int wanted,unsigned int given)1984 match_simd_size (const insn_template *t, unsigned int wanted,
1985 		 unsigned int given)
1986 {
1987   return !((i.types[given].bitfield.xmmword
1988 	    && !t->operand_types[wanted].bitfield.xmmword)
1989 	   || (i.types[given].bitfield.ymmword
1990 	       && !t->operand_types[wanted].bitfield.ymmword)
1991 	   || (i.types[given].bitfield.zmmword
1992 	       && !t->operand_types[wanted].bitfield.zmmword)
1993 	   || (i.types[given].bitfield.tmmword
1994 	       && !t->operand_types[wanted].bitfield.tmmword));
1995 }
1996 
1997 /* Return 1 if there is no conflict in any size between operand GIVEN
1998    and opeand WANTED for instruction template T.  */
1999 
2000 static INLINE int
match_mem_size(const insn_template * t,unsigned int wanted,unsigned int given)2001 match_mem_size (const insn_template *t, unsigned int wanted,
2002 		unsigned int given)
2003 {
2004   return (match_operand_size (t, wanted, given)
2005 	  && !((i.types[given].bitfield.unspecified
2006 		&& !i.broadcast.type
2007 		&& !i.broadcast.bytes
2008 		&& !t->operand_types[wanted].bitfield.unspecified)
2009 	       || (i.types[given].bitfield.fword
2010 		   && !t->operand_types[wanted].bitfield.fword)
2011 	       /* For scalar opcode templates to allow register and memory
2012 		  operands at the same time, some special casing is needed
2013 		  here.  Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2014 		  down-conversion vpmov*.  */
2015 	       || ((t->operand_types[wanted].bitfield.class == RegSIMD
2016 		    && t->operand_types[wanted].bitfield.byte
2017 		       + t->operand_types[wanted].bitfield.word
2018 		       + t->operand_types[wanted].bitfield.dword
2019 		       + t->operand_types[wanted].bitfield.qword
2020 		       > !!t->opcode_modifier.broadcast)
2021 		   ? (i.types[given].bitfield.xmmword
2022 		      || i.types[given].bitfield.ymmword
2023 		      || i.types[given].bitfield.zmmword)
2024 		   : !match_simd_size(t, wanted, given))));
2025 }
2026 
2027 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2028    operands for instruction template T, and it has MATCH_REVERSE set if there
2029    is no size conflict on any operands for the template with operands reversed
2030    (and the template allows for reversing in the first place).  */
2031 
2032 #define MATCH_STRAIGHT 1
2033 #define MATCH_REVERSE  2
2034 
2035 static INLINE unsigned int
operand_size_match(const insn_template * t)2036 operand_size_match (const insn_template *t)
2037 {
2038   unsigned int j, match = MATCH_STRAIGHT;
2039 
2040   /* Don't check non-absolute jump instructions.  */
2041   if (t->opcode_modifier.jump
2042       && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2043     return match;
2044 
2045   /* Check memory and accumulator operand size.  */
2046   for (j = 0; j < i.operands; j++)
2047     {
2048       if (i.types[j].bitfield.class != Reg
2049 	  && i.types[j].bitfield.class != RegSIMD
2050 	  && t->opcode_modifier.anysize)
2051 	continue;
2052 
2053       if (t->operand_types[j].bitfield.class == Reg
2054 	  && !match_operand_size (t, j, j))
2055 	{
2056 	  match = 0;
2057 	  break;
2058 	}
2059 
2060       if (t->operand_types[j].bitfield.class == RegSIMD
2061 	  && !match_simd_size (t, j, j))
2062 	{
2063 	  match = 0;
2064 	  break;
2065 	}
2066 
2067       if (t->operand_types[j].bitfield.instance == Accum
2068 	  && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2069 	{
2070 	  match = 0;
2071 	  break;
2072 	}
2073 
2074       if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2075 	{
2076 	  match = 0;
2077 	  break;
2078 	}
2079     }
2080 
2081   if (!t->opcode_modifier.d)
2082     {
2083     mismatch:
2084       if (!match)
2085 	i.error = operand_size_mismatch;
2086       return match;
2087     }
2088 
2089   /* Check reverse.  */
2090   gas_assert ((i.operands >= 2 && i.operands <= 3)
2091 	      || t->opcode_modifier.vexsources);
2092 
2093   for (j = 0; j < i.operands; j++)
2094     {
2095       unsigned int given = i.operands - j - 1;
2096 
2097       /* For 4- and 5-operand insns VEX.W controls just the first two
2098 	 register operands.  */
2099       if (t->opcode_modifier.vexsources)
2100 	given = j < 2 ? 1 - j : j;
2101 
2102       if (t->operand_types[j].bitfield.class == Reg
2103 	  && !match_operand_size (t, j, given))
2104 	goto mismatch;
2105 
2106       if (t->operand_types[j].bitfield.class == RegSIMD
2107 	  && !match_simd_size (t, j, given))
2108 	goto mismatch;
2109 
2110       if (t->operand_types[j].bitfield.instance == Accum
2111 	  && (!match_operand_size (t, j, given)
2112 	      || !match_simd_size (t, j, given)))
2113 	goto mismatch;
2114 
2115       if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2116 	goto mismatch;
2117     }
2118 
2119   return match | MATCH_REVERSE;
2120 }
2121 
2122 static INLINE int
operand_type_match(i386_operand_type overlap,i386_operand_type given)2123 operand_type_match (i386_operand_type overlap,
2124 		    i386_operand_type given)
2125 {
2126   i386_operand_type temp = overlap;
2127 
2128   temp.bitfield.unspecified = 0;
2129   temp.bitfield.byte = 0;
2130   temp.bitfield.word = 0;
2131   temp.bitfield.dword = 0;
2132   temp.bitfield.fword = 0;
2133   temp.bitfield.qword = 0;
2134   temp.bitfield.tbyte = 0;
2135   temp.bitfield.xmmword = 0;
2136   temp.bitfield.ymmword = 0;
2137   temp.bitfield.zmmword = 0;
2138   temp.bitfield.tmmword = 0;
2139   if (operand_type_all_zero (&temp))
2140     goto mismatch;
2141 
2142   if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2143     return 1;
2144 
2145  mismatch:
2146   i.error = operand_type_mismatch;
2147   return 0;
2148 }
2149 
2150 /* If given types g0 and g1 are registers they must be of the same type
2151    unless the expected operand type register overlap is null.
2152    Some Intel syntax memory operand size checking also happens here.  */
2153 
2154 static INLINE int
operand_type_register_match(i386_operand_type g0,i386_operand_type t0,i386_operand_type g1,i386_operand_type t1)2155 operand_type_register_match (i386_operand_type g0,
2156 			     i386_operand_type t0,
2157 			     i386_operand_type g1,
2158 			     i386_operand_type t1)
2159 {
2160   if (g0.bitfield.class != Reg
2161       && g0.bitfield.class != RegSIMD
2162       && (!operand_type_check (g0, anymem)
2163 	  || g0.bitfield.unspecified
2164 	  || (t0.bitfield.class != Reg
2165 	      && t0.bitfield.class != RegSIMD)))
2166     return 1;
2167 
2168   if (g1.bitfield.class != Reg
2169       && g1.bitfield.class != RegSIMD
2170       && (!operand_type_check (g1, anymem)
2171 	  || g1.bitfield.unspecified
2172 	  || (t1.bitfield.class != Reg
2173 	      && t1.bitfield.class != RegSIMD)))
2174     return 1;
2175 
2176   if (g0.bitfield.byte == g1.bitfield.byte
2177       && g0.bitfield.word == g1.bitfield.word
2178       && g0.bitfield.dword == g1.bitfield.dword
2179       && g0.bitfield.qword == g1.bitfield.qword
2180       && g0.bitfield.xmmword == g1.bitfield.xmmword
2181       && g0.bitfield.ymmword == g1.bitfield.ymmword
2182       && g0.bitfield.zmmword == g1.bitfield.zmmword)
2183     return 1;
2184 
2185   /* If expectations overlap in no more than a single size, all is fine. */
2186   g0 = operand_type_and (t0, t1);
2187   if (g0.bitfield.byte
2188       + g0.bitfield.word
2189       + g0.bitfield.dword
2190       + g0.bitfield.qword
2191       + g0.bitfield.xmmword
2192       + g0.bitfield.ymmword
2193       + g0.bitfield.zmmword <= 1)
2194     return 1;
2195 
2196   i.error = register_type_mismatch;
2197 
2198   return 0;
2199 }
2200 
2201 static INLINE unsigned int
register_number(const reg_entry * r)2202 register_number (const reg_entry *r)
2203 {
2204   unsigned int nr = r->reg_num;
2205 
2206   if (r->reg_flags & RegRex)
2207     nr += 8;
2208 
2209   if (r->reg_flags & RegVRex)
2210     nr += 16;
2211 
2212   return nr;
2213 }
2214 
2215 static INLINE unsigned int
mode_from_disp_size(i386_operand_type t)2216 mode_from_disp_size (i386_operand_type t)
2217 {
2218   if (t.bitfield.disp8)
2219     return 1;
2220   else if (t.bitfield.disp16
2221 	   || t.bitfield.disp32)
2222     return 2;
2223   else
2224     return 0;
2225 }
2226 
2227 static INLINE int
fits_in_signed_byte(addressT num)2228 fits_in_signed_byte (addressT num)
2229 {
2230   return num + 0x80 <= 0xff;
2231 }
2232 
2233 static INLINE int
fits_in_unsigned_byte(addressT num)2234 fits_in_unsigned_byte (addressT num)
2235 {
2236   return num <= 0xff;
2237 }
2238 
2239 static INLINE int
fits_in_unsigned_word(addressT num)2240 fits_in_unsigned_word (addressT num)
2241 {
2242   return num <= 0xffff;
2243 }
2244 
2245 static INLINE int
fits_in_signed_word(addressT num)2246 fits_in_signed_word (addressT num)
2247 {
2248   return num + 0x8000 <= 0xffff;
2249 }
2250 
2251 static INLINE int
fits_in_signed_long(addressT num ATTRIBUTE_UNUSED)2252 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2253 {
2254 #ifndef BFD64
2255   return 1;
2256 #else
2257   return num + 0x80000000 <= 0xffffffff;
2258 #endif
2259 }				/* fits_in_signed_long() */
2260 
2261 static INLINE int
fits_in_unsigned_long(addressT num ATTRIBUTE_UNUSED)2262 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2263 {
2264 #ifndef BFD64
2265   return 1;
2266 #else
2267   return num <= 0xffffffff;
2268 #endif
2269 }				/* fits_in_unsigned_long() */
2270 
extend_to_32bit_address(addressT num)2271 static INLINE valueT extend_to_32bit_address (addressT num)
2272 {
2273 #ifdef BFD64
2274   if (fits_in_unsigned_long(num))
2275     return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2276 
2277   if (!fits_in_signed_long (num))
2278     return num & 0xffffffff;
2279 #endif
2280 
2281   return num;
2282 }
2283 
2284 static INLINE int
fits_in_disp8(offsetT num)2285 fits_in_disp8 (offsetT num)
2286 {
2287   int shift = i.memshift;
2288   unsigned int mask;
2289 
2290   if (shift == -1)
2291     abort ();
2292 
2293   mask = (1 << shift) - 1;
2294 
2295   /* Return 0 if NUM isn't properly aligned.  */
2296   if ((num & mask))
2297     return 0;
2298 
2299   /* Check if NUM will fit in 8bit after shift.  */
2300   return fits_in_signed_byte (num >> shift);
2301 }
2302 
2303 static INLINE int
fits_in_imm4(offsetT num)2304 fits_in_imm4 (offsetT num)
2305 {
2306   return (num & 0xf) == num;
2307 }
2308 
2309 static i386_operand_type
smallest_imm_type(offsetT num)2310 smallest_imm_type (offsetT num)
2311 {
2312   i386_operand_type t;
2313 
2314   operand_type_set (&t, 0);
2315   t.bitfield.imm64 = 1;
2316 
2317   if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2318     {
2319       /* This code is disabled on the 486 because all the Imm1 forms
2320 	 in the opcode table are slower on the i486.  They're the
2321 	 versions with the implicitly specified single-position
2322 	 displacement, which has another syntax if you really want to
2323 	 use that form.  */
2324       t.bitfield.imm1 = 1;
2325       t.bitfield.imm8 = 1;
2326       t.bitfield.imm8s = 1;
2327       t.bitfield.imm16 = 1;
2328       t.bitfield.imm32 = 1;
2329       t.bitfield.imm32s = 1;
2330     }
2331   else if (fits_in_signed_byte (num))
2332     {
2333       t.bitfield.imm8 = 1;
2334       t.bitfield.imm8s = 1;
2335       t.bitfield.imm16 = 1;
2336       t.bitfield.imm32 = 1;
2337       t.bitfield.imm32s = 1;
2338     }
2339   else if (fits_in_unsigned_byte (num))
2340     {
2341       t.bitfield.imm8 = 1;
2342       t.bitfield.imm16 = 1;
2343       t.bitfield.imm32 = 1;
2344       t.bitfield.imm32s = 1;
2345     }
2346   else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2347     {
2348       t.bitfield.imm16 = 1;
2349       t.bitfield.imm32 = 1;
2350       t.bitfield.imm32s = 1;
2351     }
2352   else if (fits_in_signed_long (num))
2353     {
2354       t.bitfield.imm32 = 1;
2355       t.bitfield.imm32s = 1;
2356     }
2357   else if (fits_in_unsigned_long (num))
2358     t.bitfield.imm32 = 1;
2359 
2360   return t;
2361 }
2362 
2363 static offsetT
offset_in_range(offsetT val,int size)2364 offset_in_range (offsetT val, int size)
2365 {
2366   addressT mask;
2367 
2368   switch (size)
2369     {
2370     case 1: mask = ((addressT) 1 <<  8) - 1; break;
2371     case 2: mask = ((addressT) 1 << 16) - 1; break;
2372 #ifdef BFD64
2373     case 4: mask = ((addressT) 1 << 32) - 1; break;
2374 #endif
2375     case sizeof (val): return val;
2376     default: abort ();
2377     }
2378 
2379   if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2380     {
2381       char val_buf[128];
2382       char masked_buf[128];
2383 
2384       /* Coded this way in order to ease translation.  */
2385       sprintf_vma (val_buf, val);
2386       sprintf_vma (masked_buf, val & mask);
2387       as_warn (_("0x%s shortened to 0x%s"), val_buf, masked_buf);
2388     }
2389 
2390   return val & mask;
2391 }
2392 
2393 enum PREFIX_GROUP
2394 {
2395   PREFIX_EXIST = 0,
2396   PREFIX_LOCK,
2397   PREFIX_REP,
2398   PREFIX_DS,
2399   PREFIX_OTHER
2400 };
2401 
2402 /* Returns
2403    a. PREFIX_EXIST if attempting to add a prefix where one from the
2404    same class already exists.
2405    b. PREFIX_LOCK if lock prefix is added.
2406    c. PREFIX_REP if rep/repne prefix is added.
2407    d. PREFIX_DS if ds prefix is added.
2408    e. PREFIX_OTHER if other prefix is added.
2409  */
2410 
2411 static enum PREFIX_GROUP
add_prefix(unsigned int prefix)2412 add_prefix (unsigned int prefix)
2413 {
2414   enum PREFIX_GROUP ret = PREFIX_OTHER;
2415   unsigned int q;
2416 
2417   if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2418       && flag_code == CODE_64BIT)
2419     {
2420       if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2421 	  || (i.prefix[REX_PREFIX] & prefix & REX_R)
2422 	  || (i.prefix[REX_PREFIX] & prefix & REX_X)
2423 	  || (i.prefix[REX_PREFIX] & prefix & REX_B))
2424 	ret = PREFIX_EXIST;
2425       q = REX_PREFIX;
2426     }
2427   else
2428     {
2429       switch (prefix)
2430 	{
2431 	default:
2432 	  abort ();
2433 
2434 	case DS_PREFIX_OPCODE:
2435 	  ret = PREFIX_DS;
2436 	  /* Fall through.  */
2437 	case CS_PREFIX_OPCODE:
2438 	case ES_PREFIX_OPCODE:
2439 	case FS_PREFIX_OPCODE:
2440 	case GS_PREFIX_OPCODE:
2441 	case SS_PREFIX_OPCODE:
2442 	  q = SEG_PREFIX;
2443 	  break;
2444 
2445 	case REPNE_PREFIX_OPCODE:
2446 	case REPE_PREFIX_OPCODE:
2447 	  q = REP_PREFIX;
2448 	  ret = PREFIX_REP;
2449 	  break;
2450 
2451 	case LOCK_PREFIX_OPCODE:
2452 	  q = LOCK_PREFIX;
2453 	  ret = PREFIX_LOCK;
2454 	  break;
2455 
2456 	case FWAIT_OPCODE:
2457 	  q = WAIT_PREFIX;
2458 	  break;
2459 
2460 	case ADDR_PREFIX_OPCODE:
2461 	  q = ADDR_PREFIX;
2462 	  break;
2463 
2464 	case DATA_PREFIX_OPCODE:
2465 	  q = DATA_PREFIX;
2466 	  break;
2467 	}
2468       if (i.prefix[q] != 0)
2469 	ret = PREFIX_EXIST;
2470     }
2471 
2472   if (ret)
2473     {
2474       if (!i.prefix[q])
2475 	++i.prefixes;
2476       i.prefix[q] |= prefix;
2477     }
2478   else
2479     as_bad (_("same type of prefix used twice"));
2480 
2481   return ret;
2482 }
2483 
2484 static void
update_code_flag(int value,int check)2485 update_code_flag (int value, int check)
2486 {
2487   PRINTF_LIKE ((*as_error));
2488 
2489   flag_code = (enum flag_code) value;
2490   if (flag_code == CODE_64BIT)
2491     {
2492       cpu_arch_flags.bitfield.cpu64 = 1;
2493       cpu_arch_flags.bitfield.cpuno64 = 0;
2494     }
2495   else
2496     {
2497       cpu_arch_flags.bitfield.cpu64 = 0;
2498       cpu_arch_flags.bitfield.cpuno64 = 1;
2499     }
2500   if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
2501     {
2502       if (check)
2503 	as_error = as_fatal;
2504       else
2505 	as_error = as_bad;
2506       (*as_error) (_("64bit mode not supported on `%s'."),
2507 		   cpu_arch_name ? cpu_arch_name : default_arch);
2508     }
2509   if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2510     {
2511       if (check)
2512 	as_error = as_fatal;
2513       else
2514 	as_error = as_bad;
2515       (*as_error) (_("32bit mode not supported on `%s'."),
2516 		   cpu_arch_name ? cpu_arch_name : default_arch);
2517     }
2518   stackop_size = '\0';
2519 }
2520 
2521 static void
set_code_flag(int value)2522 set_code_flag (int value)
2523 {
2524   update_code_flag (value, 0);
2525 }
2526 
2527 static void
set_16bit_gcc_code_flag(int new_code_flag)2528 set_16bit_gcc_code_flag (int new_code_flag)
2529 {
2530   flag_code = (enum flag_code) new_code_flag;
2531   if (flag_code != CODE_16BIT)
2532     abort ();
2533   cpu_arch_flags.bitfield.cpu64 = 0;
2534   cpu_arch_flags.bitfield.cpuno64 = 1;
2535   stackop_size = LONG_MNEM_SUFFIX;
2536 }
2537 
2538 static void
set_intel_syntax(int syntax_flag)2539 set_intel_syntax (int syntax_flag)
2540 {
2541   /* Find out if register prefixing is specified.  */
2542   int ask_naked_reg = 0;
2543 
2544   SKIP_WHITESPACE ();
2545   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2546     {
2547       char *string;
2548       int e = get_symbol_name (&string);
2549 
2550       if (strcmp (string, "prefix") == 0)
2551 	ask_naked_reg = 1;
2552       else if (strcmp (string, "noprefix") == 0)
2553 	ask_naked_reg = -1;
2554       else
2555 	as_bad (_("bad argument to syntax directive."));
2556       (void) restore_line_pointer (e);
2557     }
2558   demand_empty_rest_of_line ();
2559 
2560   intel_syntax = syntax_flag;
2561 
2562   if (ask_naked_reg == 0)
2563     allow_naked_reg = (intel_syntax
2564 		       && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2565   else
2566     allow_naked_reg = (ask_naked_reg < 0);
2567 
2568   expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2569 
2570   identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
2571   identifier_chars['$'] = intel_syntax ? '$' : 0;
2572   register_prefix = allow_naked_reg ? "" : "%";
2573 }
2574 
2575 static void
set_intel_mnemonic(int mnemonic_flag)2576 set_intel_mnemonic (int mnemonic_flag)
2577 {
2578   intel_mnemonic = mnemonic_flag;
2579 }
2580 
2581 static void
set_allow_index_reg(int flag)2582 set_allow_index_reg (int flag)
2583 {
2584   allow_index_reg = flag;
2585 }
2586 
2587 static void
set_check(int what)2588 set_check (int what)
2589 {
2590   enum check_kind *kind;
2591   const char *str;
2592 
2593   if (what)
2594     {
2595       kind = &operand_check;
2596       str = "operand";
2597     }
2598   else
2599     {
2600       kind = &sse_check;
2601       str = "sse";
2602     }
2603 
2604   SKIP_WHITESPACE ();
2605 
2606   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2607     {
2608       char *string;
2609       int e = get_symbol_name (&string);
2610 
2611       if (strcmp (string, "none") == 0)
2612 	*kind = check_none;
2613       else if (strcmp (string, "warning") == 0)
2614 	*kind = check_warning;
2615       else if (strcmp (string, "error") == 0)
2616 	*kind = check_error;
2617       else
2618 	as_bad (_("bad argument to %s_check directive."), str);
2619       (void) restore_line_pointer (e);
2620     }
2621   else
2622     as_bad (_("missing argument for %s_check directive"), str);
2623 
2624   demand_empty_rest_of_line ();
2625 }
2626 
2627 static void
check_cpu_arch_compatible(const char * name ATTRIBUTE_UNUSED,i386_cpu_flags new_flag ATTRIBUTE_UNUSED)2628 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2629 			   i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2630 {
2631 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2632   static const char *arch;
2633 
2634   /* Intel MCU is only supported on ELF.  */
2635   if (!IS_ELF)
2636     return;
2637 
2638   if (!arch)
2639     {
2640       /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
2641 	 use default_arch.  */
2642       arch = cpu_arch_name;
2643       if (!arch)
2644 	arch = default_arch;
2645     }
2646 
2647   /* If we are targeting Intel MCU, we must enable it.  */
2648   if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2649       == new_flag.bitfield.cpuiamcu)
2650     return;
2651 
2652   as_bad (_("`%s' is not supported on `%s'"), name, arch);
2653 #endif
2654 }
2655 
2656 static void
extend_cpu_sub_arch_name(const char * name)2657 extend_cpu_sub_arch_name (const char *name)
2658 {
2659   if (cpu_sub_arch_name)
2660     cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2661 				  ".", name, (const char *) NULL);
2662   else
2663     cpu_sub_arch_name = concat (".", name, (const char *) NULL);
2664 }
2665 
2666 static void
set_cpu_arch(int dummy ATTRIBUTE_UNUSED)2667 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2668 {
2669   typedef struct arch_stack_entry
2670   {
2671     const struct arch_stack_entry *prev;
2672     const char *name;
2673     char *sub_name;
2674     i386_cpu_flags flags;
2675     i386_cpu_flags isa_flags;
2676     enum processor_type isa;
2677     enum flag_code flag_code;
2678     char stackop_size;
2679     bool no_cond_jump_promotion;
2680   } arch_stack_entry;
2681   static const arch_stack_entry *arch_stack_top;
2682 
2683   SKIP_WHITESPACE ();
2684 
2685   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2686     {
2687       char *s;
2688       int e = get_symbol_name (&s);
2689       const char *string = s;
2690       unsigned int j = 0;
2691       i386_cpu_flags flags;
2692 
2693       if (strcmp (string, "default") == 0)
2694 	{
2695 	  if (strcmp (default_arch, "iamcu") == 0)
2696 	    string = default_arch;
2697 	  else
2698 	    {
2699 	      static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2700 
2701 	      cpu_arch_name = NULL;
2702 	      free (cpu_sub_arch_name);
2703 	      cpu_sub_arch_name = NULL;
2704 	      cpu_arch_flags = cpu_unknown_flags;
2705 	      if (flag_code == CODE_64BIT)
2706 		{
2707 		  cpu_arch_flags.bitfield.cpu64 = 1;
2708 		  cpu_arch_flags.bitfield.cpuno64 = 0;
2709 		}
2710 	      else
2711 		{
2712 		  cpu_arch_flags.bitfield.cpu64 = 0;
2713 		  cpu_arch_flags.bitfield.cpuno64 = 1;
2714 		}
2715 	      cpu_arch_isa = PROCESSOR_UNKNOWN;
2716 	      cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2717 	      if (!cpu_arch_tune_set)
2718 		{
2719 		  cpu_arch_tune = cpu_arch_isa;
2720 		  cpu_arch_tune_flags = cpu_arch_isa_flags;
2721 		}
2722 
2723 	      j = ARRAY_SIZE (cpu_arch) + 1;
2724 	    }
2725 	}
2726       else if (strcmp (string, "push") == 0)
2727 	{
2728 	  arch_stack_entry *top = XNEW (arch_stack_entry);
2729 
2730 	  top->name = cpu_arch_name;
2731 	  if (cpu_sub_arch_name)
2732 	    top->sub_name = xstrdup (cpu_sub_arch_name);
2733 	  else
2734 	    top->sub_name = NULL;
2735 	  top->flags = cpu_arch_flags;
2736 	  top->isa = cpu_arch_isa;
2737 	  top->isa_flags = cpu_arch_isa_flags;
2738 	  top->flag_code = flag_code;
2739 	  top->stackop_size = stackop_size;
2740 	  top->no_cond_jump_promotion = no_cond_jump_promotion;
2741 
2742 	  top->prev = arch_stack_top;
2743 	  arch_stack_top = top;
2744 
2745 	  (void) restore_line_pointer (e);
2746 	  demand_empty_rest_of_line ();
2747 	  return;
2748 	}
2749       else if (strcmp (string, "pop") == 0)
2750 	{
2751 	  const arch_stack_entry *top = arch_stack_top;
2752 
2753 	  if (!top)
2754 	    as_bad (_(".arch stack is empty"));
2755 	  else if (top->flag_code != flag_code
2756 		   || top->stackop_size != stackop_size)
2757 	    {
2758 	      static const unsigned int bits[] = {
2759 	        [CODE_16BIT] = 16,
2760 	        [CODE_32BIT] = 32,
2761 	        [CODE_64BIT] = 64,
2762 	      };
2763 
2764 	      as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2765 		      bits[top->flag_code],
2766 		      top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2767 	    }
2768 	  else
2769 	    {
2770 	      arch_stack_top = top->prev;
2771 
2772 	      cpu_arch_name = top->name;
2773 	      free (cpu_sub_arch_name);
2774 	      cpu_sub_arch_name = top->sub_name;
2775 	      cpu_arch_flags = top->flags;
2776 	      cpu_arch_isa = top->isa;
2777 	      cpu_arch_isa_flags = top->isa_flags;
2778 	      no_cond_jump_promotion = top->no_cond_jump_promotion;
2779 
2780 	      XDELETE (top);
2781 	    }
2782 
2783 	  (void) restore_line_pointer (e);
2784 	  demand_empty_rest_of_line ();
2785 	  return;
2786 	}
2787 
2788       for (; j < ARRAY_SIZE (cpu_arch); j++)
2789 	{
2790 	  if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2791 	     && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2792 	    {
2793 	      if (*string != '.')
2794 		{
2795 		  check_cpu_arch_compatible (string, cpu_arch[j].enable);
2796 
2797 		  cpu_arch_name = cpu_arch[j].name;
2798 		  free (cpu_sub_arch_name);
2799 		  cpu_sub_arch_name = NULL;
2800 		  cpu_arch_flags = cpu_arch[j].enable;
2801 		  if (flag_code == CODE_64BIT)
2802 		    {
2803 		      cpu_arch_flags.bitfield.cpu64 = 1;
2804 		      cpu_arch_flags.bitfield.cpuno64 = 0;
2805 		    }
2806 		  else
2807 		    {
2808 		      cpu_arch_flags.bitfield.cpu64 = 0;
2809 		      cpu_arch_flags.bitfield.cpuno64 = 1;
2810 		    }
2811 		  cpu_arch_isa = cpu_arch[j].type;
2812 		  cpu_arch_isa_flags = cpu_arch[j].enable;
2813 		  if (!cpu_arch_tune_set)
2814 		    {
2815 		      cpu_arch_tune = cpu_arch_isa;
2816 		      cpu_arch_tune_flags = cpu_arch_isa_flags;
2817 		    }
2818 		  break;
2819 		}
2820 
2821 	      if (cpu_flags_all_zero (&cpu_arch[j].enable))
2822 	        continue;
2823 
2824 	      flags = cpu_flags_or (cpu_arch_flags,
2825 				    cpu_arch[j].enable);
2826 
2827 	      if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2828 		{
2829 		  extend_cpu_sub_arch_name (string + 1);
2830 		  cpu_arch_flags = flags;
2831 		  cpu_arch_isa_flags = flags;
2832 		}
2833 	      else
2834 		cpu_arch_isa_flags
2835 		  = cpu_flags_or (cpu_arch_isa_flags,
2836 				  cpu_arch[j].enable);
2837 	      (void) restore_line_pointer (e);
2838 	      demand_empty_rest_of_line ();
2839 	      return;
2840 	    }
2841 	}
2842 
2843       if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
2844 	{
2845 	  /* Disable an ISA extension.  */
2846 	  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2847 	    if (cpu_arch[j].type == PROCESSOR_NONE
2848 	        && strcmp (string + 3, cpu_arch[j].name) == 0)
2849 	      {
2850 		flags = cpu_flags_and_not (cpu_arch_flags,
2851 					   cpu_arch[j].disable);
2852 		if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2853 		  {
2854 		    extend_cpu_sub_arch_name (string + 1);
2855 		    cpu_arch_flags = flags;
2856 		    cpu_arch_isa_flags = flags;
2857 		  }
2858 		(void) restore_line_pointer (e);
2859 		demand_empty_rest_of_line ();
2860 		return;
2861 	      }
2862 	}
2863 
2864       if (j == ARRAY_SIZE (cpu_arch))
2865 	as_bad (_("no such architecture: `%s'"), string);
2866 
2867       *input_line_pointer = e;
2868     }
2869   else
2870     as_bad (_("missing cpu architecture"));
2871 
2872   no_cond_jump_promotion = 0;
2873   if (*input_line_pointer == ','
2874       && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2875     {
2876       char *string;
2877       char e;
2878 
2879       ++input_line_pointer;
2880       e = get_symbol_name (&string);
2881 
2882       if (strcmp (string, "nojumps") == 0)
2883 	no_cond_jump_promotion = 1;
2884       else if (strcmp (string, "jumps") == 0)
2885 	;
2886       else
2887 	as_bad (_("no such architecture modifier: `%s'"), string);
2888 
2889       (void) restore_line_pointer (e);
2890     }
2891 
2892   demand_empty_rest_of_line ();
2893 }
2894 
2895 enum bfd_architecture
i386_arch(void)2896 i386_arch (void)
2897 {
2898   if (cpu_arch_isa == PROCESSOR_IAMCU)
2899     {
2900       if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2901 	  || flag_code == CODE_64BIT)
2902 	as_fatal (_("Intel MCU is 32bit ELF only"));
2903       return bfd_arch_iamcu;
2904     }
2905   else
2906     return bfd_arch_i386;
2907 }
2908 
2909 unsigned long
i386_mach(void)2910 i386_mach (void)
2911 {
2912   if (startswith (default_arch, "x86_64"))
2913     {
2914       if (default_arch[6] == '\0')
2915 	return bfd_mach_x86_64;
2916       else
2917 	return bfd_mach_x64_32;
2918     }
2919   else if (!strcmp (default_arch, "i386")
2920 	   || !strcmp (default_arch, "iamcu"))
2921     {
2922       if (cpu_arch_isa == PROCESSOR_IAMCU)
2923 	{
2924 	  if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2925 	    as_fatal (_("Intel MCU is 32bit ELF only"));
2926 	  return bfd_mach_i386_iamcu;
2927 	}
2928       else
2929 	return bfd_mach_i386_i386;
2930     }
2931   else
2932     as_fatal (_("unknown architecture"));
2933 }
2934 
2935 void
md_begin(void)2936 md_begin (void)
2937 {
2938   /* Support pseudo prefixes like {disp32}.  */
2939   lex_type ['{'] = LEX_BEGIN_NAME;
2940 
2941   /* Initialize op_hash hash table.  */
2942   op_hash = str_htab_create ();
2943 
2944   {
2945     const insn_template *optab;
2946     templates *core_optab;
2947 
2948     /* Setup for loop.  */
2949     optab = i386_optab;
2950     core_optab = XNEW (templates);
2951     core_optab->start = optab;
2952 
2953     while (1)
2954       {
2955 	++optab;
2956 	if (optab->name == NULL
2957 	    || strcmp (optab->name, (optab - 1)->name) != 0)
2958 	  {
2959 	    /* different name --> ship out current template list;
2960 	       add to hash table; & begin anew.  */
2961 	    core_optab->end = optab;
2962 	    if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2963 	      as_fatal (_("duplicate %s"), (optab - 1)->name);
2964 
2965 	    if (optab->name == NULL)
2966 	      break;
2967 	    core_optab = XNEW (templates);
2968 	    core_optab->start = optab;
2969 	  }
2970       }
2971   }
2972 
2973   /* Initialize reg_hash hash table.  */
2974   reg_hash = str_htab_create ();
2975   {
2976     const reg_entry *regtab;
2977     unsigned int regtab_size = i386_regtab_size;
2978 
2979     for (regtab = i386_regtab; regtab_size--; regtab++)
2980       {
2981 	switch (regtab->reg_type.bitfield.class)
2982 	  {
2983 	  case Reg:
2984 	    if (regtab->reg_type.bitfield.dword)
2985 	      {
2986 		if (regtab->reg_type.bitfield.instance == Accum)
2987 		  reg_eax = regtab;
2988 	      }
2989 	    else if (regtab->reg_type.bitfield.tbyte)
2990 	      {
2991 		/* There's no point inserting st(<N>) in the hash table, as
2992 		   parentheses aren't included in register_chars[] anyway.  */
2993 		if (regtab->reg_type.bitfield.instance != Accum)
2994 		  continue;
2995 		reg_st0 = regtab;
2996 	      }
2997 	    break;
2998 
2999 	  case SReg:
3000 	    switch (regtab->reg_num)
3001 	      {
3002 	      case 0: reg_es = regtab; break;
3003 	      case 2: reg_ss = regtab; break;
3004 	      case 3: reg_ds = regtab; break;
3005 	      }
3006 	    break;
3007 
3008 	  case RegMask:
3009 	    if (!regtab->reg_num)
3010 	      reg_k0 = regtab;
3011 	    break;
3012 	  }
3013 
3014 	if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3015 	  as_fatal (_("duplicate %s"), regtab->reg_name);
3016       }
3017   }
3018 
3019   /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3020   {
3021     int c;
3022     char *p;
3023 
3024     for (c = 0; c < 256; c++)
3025       {
3026 	if (ISDIGIT (c) || ISLOWER (c))
3027 	  {
3028 	    mnemonic_chars[c] = c;
3029 	    register_chars[c] = c;
3030 	    operand_chars[c] = c;
3031 	  }
3032 	else if (ISUPPER (c))
3033 	  {
3034 	    mnemonic_chars[c] = TOLOWER (c);
3035 	    register_chars[c] = mnemonic_chars[c];
3036 	    operand_chars[c] = c;
3037 	  }
3038 	else if (c == '{' || c == '}')
3039 	  {
3040 	    mnemonic_chars[c] = c;
3041 	    operand_chars[c] = c;
3042 	  }
3043 #ifdef SVR4_COMMENT_CHARS
3044 	else if (c == '\\' && strchr (i386_comment_chars, '/'))
3045 	  operand_chars[c] = c;
3046 #endif
3047 
3048 	if (ISALPHA (c) || ISDIGIT (c))
3049 	  identifier_chars[c] = c;
3050 	else if (c >= 128)
3051 	  {
3052 	    identifier_chars[c] = c;
3053 	    operand_chars[c] = c;
3054 	  }
3055       }
3056 
3057 #ifdef LEX_AT
3058     identifier_chars['@'] = '@';
3059 #endif
3060 #ifdef LEX_QM
3061     identifier_chars['?'] = '?';
3062     operand_chars['?'] = '?';
3063 #endif
3064     mnemonic_chars['_'] = '_';
3065     mnemonic_chars['-'] = '-';
3066     mnemonic_chars['.'] = '.';
3067     identifier_chars['_'] = '_';
3068     identifier_chars['.'] = '.';
3069 
3070     for (p = operand_special_chars; *p != '\0'; p++)
3071       operand_chars[(unsigned char) *p] = *p;
3072   }
3073 
3074   if (flag_code == CODE_64BIT)
3075     {
3076 #if defined (OBJ_COFF) && defined (TE_PE)
3077       x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3078 				  ? 32 : 16);
3079 #else
3080       x86_dwarf2_return_column = 16;
3081 #endif
3082       x86_cie_data_alignment = -8;
3083     }
3084   else
3085     {
3086       x86_dwarf2_return_column = 8;
3087       x86_cie_data_alignment = -4;
3088     }
3089 
3090   /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3091      can be turned into BRANCH_PREFIX frag.  */
3092   if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3093     abort ();
3094 }
3095 
3096 void
i386_print_statistics(FILE * file)3097 i386_print_statistics (FILE *file)
3098 {
3099   htab_print_statistics (file, "i386 opcode", op_hash);
3100   htab_print_statistics (file, "i386 register", reg_hash);
3101 }
3102 
3103 #ifdef DEBUG386
3104 
3105 /* Debugging routines for md_assemble.  */
3106 static void pte (insn_template *);
3107 static void pt (i386_operand_type);
3108 static void pe (expressionS *);
3109 static void ps (symbolS *);
3110 
3111 static void
pi(const char * line,i386_insn * x)3112 pi (const char *line, i386_insn *x)
3113 {
3114   unsigned int j;
3115 
3116   fprintf (stdout, "%s: template ", line);
3117   pte (&x->tm);
3118   fprintf (stdout, "  address: base %s  index %s  scale %x\n",
3119 	   x->base_reg ? x->base_reg->reg_name : "none",
3120 	   x->index_reg ? x->index_reg->reg_name : "none",
3121 	   x->log2_scale_factor);
3122   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
3123 	   x->rm.mode, x->rm.reg, x->rm.regmem);
3124   fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
3125 	   x->sib.base, x->sib.index, x->sib.scale);
3126   fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
3127 	   (x->rex & REX_W) != 0,
3128 	   (x->rex & REX_R) != 0,
3129 	   (x->rex & REX_X) != 0,
3130 	   (x->rex & REX_B) != 0);
3131   for (j = 0; j < x->operands; j++)
3132     {
3133       fprintf (stdout, "    #%d:  ", j + 1);
3134       pt (x->types[j]);
3135       fprintf (stdout, "\n");
3136       if (x->types[j].bitfield.class == Reg
3137 	  || x->types[j].bitfield.class == RegMMX
3138 	  || x->types[j].bitfield.class == RegSIMD
3139 	  || x->types[j].bitfield.class == RegMask
3140 	  || x->types[j].bitfield.class == SReg
3141 	  || x->types[j].bitfield.class == RegCR
3142 	  || x->types[j].bitfield.class == RegDR
3143 	  || x->types[j].bitfield.class == RegTR
3144 	  || x->types[j].bitfield.class == RegBND)
3145 	fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3146       if (operand_type_check (x->types[j], imm))
3147 	pe (x->op[j].imms);
3148       if (operand_type_check (x->types[j], disp))
3149 	pe (x->op[j].disps);
3150     }
3151 }
3152 
3153 static void
pte(insn_template * t)3154 pte (insn_template *t)
3155 {
3156   static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3157   static const char *const opc_spc[] = {
3158     NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3159     "XOP08", "XOP09", "XOP0A",
3160   };
3161   unsigned int j;
3162 
3163   fprintf (stdout, " %d operands ", t->operands);
3164   if (opc_pfx[t->opcode_modifier.opcodeprefix])
3165     fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3166   if (opc_spc[t->opcode_modifier.opcodespace])
3167     fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
3168   fprintf (stdout, "opcode %x ", t->base_opcode);
3169   if (t->extension_opcode != None)
3170     fprintf (stdout, "ext %x ", t->extension_opcode);
3171   if (t->opcode_modifier.d)
3172     fprintf (stdout, "D");
3173   if (t->opcode_modifier.w)
3174     fprintf (stdout, "W");
3175   fprintf (stdout, "\n");
3176   for (j = 0; j < t->operands; j++)
3177     {
3178       fprintf (stdout, "    #%d type ", j + 1);
3179       pt (t->operand_types[j]);
3180       fprintf (stdout, "\n");
3181     }
3182 }
3183 
3184 static void
pe(expressionS * e)3185 pe (expressionS *e)
3186 {
3187   fprintf (stdout, "    operation     %d\n", e->X_op);
3188   fprintf (stdout, "    add_number    %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3189 	   e->X_add_number, e->X_add_number);
3190   if (e->X_add_symbol)
3191     {
3192       fprintf (stdout, "    add_symbol    ");
3193       ps (e->X_add_symbol);
3194       fprintf (stdout, "\n");
3195     }
3196   if (e->X_op_symbol)
3197     {
3198       fprintf (stdout, "    op_symbol    ");
3199       ps (e->X_op_symbol);
3200       fprintf (stdout, "\n");
3201     }
3202 }
3203 
3204 static void
ps(symbolS * s)3205 ps (symbolS *s)
3206 {
3207   fprintf (stdout, "%s type %s%s",
3208 	   S_GET_NAME (s),
3209 	   S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3210 	   segment_name (S_GET_SEGMENT (s)));
3211 }
3212 
3213 static struct type_name
3214   {
3215     i386_operand_type mask;
3216     const char *name;
3217   }
3218 const type_names[] =
3219 {
3220   { OPERAND_TYPE_REG8, "r8" },
3221   { OPERAND_TYPE_REG16, "r16" },
3222   { OPERAND_TYPE_REG32, "r32" },
3223   { OPERAND_TYPE_REG64, "r64" },
3224   { OPERAND_TYPE_ACC8, "acc8" },
3225   { OPERAND_TYPE_ACC16, "acc16" },
3226   { OPERAND_TYPE_ACC32, "acc32" },
3227   { OPERAND_TYPE_ACC64, "acc64" },
3228   { OPERAND_TYPE_IMM8, "i8" },
3229   { OPERAND_TYPE_IMM8, "i8s" },
3230   { OPERAND_TYPE_IMM16, "i16" },
3231   { OPERAND_TYPE_IMM32, "i32" },
3232   { OPERAND_TYPE_IMM32S, "i32s" },
3233   { OPERAND_TYPE_IMM64, "i64" },
3234   { OPERAND_TYPE_IMM1, "i1" },
3235   { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3236   { OPERAND_TYPE_DISP8, "d8" },
3237   { OPERAND_TYPE_DISP16, "d16" },
3238   { OPERAND_TYPE_DISP32, "d32" },
3239   { OPERAND_TYPE_DISP64, "d64" },
3240   { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3241   { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3242   { OPERAND_TYPE_CONTROL, "control reg" },
3243   { OPERAND_TYPE_TEST, "test reg" },
3244   { OPERAND_TYPE_DEBUG, "debug reg" },
3245   { OPERAND_TYPE_FLOATREG, "FReg" },
3246   { OPERAND_TYPE_FLOATACC, "FAcc" },
3247   { OPERAND_TYPE_SREG, "SReg" },
3248   { OPERAND_TYPE_REGMMX, "rMMX" },
3249   { OPERAND_TYPE_REGXMM, "rXMM" },
3250   { OPERAND_TYPE_REGYMM, "rYMM" },
3251   { OPERAND_TYPE_REGZMM, "rZMM" },
3252   { OPERAND_TYPE_REGTMM, "rTMM" },
3253   { OPERAND_TYPE_REGMASK, "Mask reg" },
3254 };
3255 
3256 static void
pt(i386_operand_type t)3257 pt (i386_operand_type t)
3258 {
3259   unsigned int j;
3260   i386_operand_type a;
3261 
3262   for (j = 0; j < ARRAY_SIZE (type_names); j++)
3263     {
3264       a = operand_type_and (t, type_names[j].mask);
3265       if (operand_type_equal (&a, &type_names[j].mask))
3266 	fprintf (stdout, "%s, ",  type_names[j].name);
3267     }
3268   fflush (stdout);
3269 }
3270 
3271 #endif /* DEBUG386 */
3272 
3273 static bfd_reloc_code_real_type
reloc(unsigned int size,int pcrel,int sign,bfd_reloc_code_real_type other)3274 reloc (unsigned int size,
3275        int pcrel,
3276        int sign,
3277        bfd_reloc_code_real_type other)
3278 {
3279   if (other != NO_RELOC)
3280     {
3281       reloc_howto_type *rel;
3282 
3283       if (size == 8)
3284 	switch (other)
3285 	  {
3286 	  case BFD_RELOC_X86_64_GOT32:
3287 	    return BFD_RELOC_X86_64_GOT64;
3288 	    break;
3289 	  case BFD_RELOC_X86_64_GOTPLT64:
3290 	    return BFD_RELOC_X86_64_GOTPLT64;
3291 	    break;
3292 	  case BFD_RELOC_X86_64_PLTOFF64:
3293 	    return BFD_RELOC_X86_64_PLTOFF64;
3294 	    break;
3295 	  case BFD_RELOC_X86_64_GOTPC32:
3296 	    other = BFD_RELOC_X86_64_GOTPC64;
3297 	    break;
3298 	  case BFD_RELOC_X86_64_GOTPCREL:
3299 	    other = BFD_RELOC_X86_64_GOTPCREL64;
3300 	    break;
3301 	  case BFD_RELOC_X86_64_TPOFF32:
3302 	    other = BFD_RELOC_X86_64_TPOFF64;
3303 	    break;
3304 	  case BFD_RELOC_X86_64_DTPOFF32:
3305 	    other = BFD_RELOC_X86_64_DTPOFF64;
3306 	    break;
3307 	  default:
3308 	    break;
3309 	  }
3310 
3311 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3312       if (other == BFD_RELOC_SIZE32)
3313 	{
3314 	  if (size == 8)
3315 	    other = BFD_RELOC_SIZE64;
3316 	  if (pcrel)
3317 	    {
3318 	      as_bad (_("there are no pc-relative size relocations"));
3319 	      return NO_RELOC;
3320 	    }
3321 	}
3322 #endif
3323 
3324       /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
3325       if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3326 	sign = -1;
3327 
3328       rel = bfd_reloc_type_lookup (stdoutput, other);
3329       if (!rel)
3330 	as_bad (_("unknown relocation (%u)"), other);
3331       else if (size != bfd_get_reloc_size (rel))
3332 	as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3333 		bfd_get_reloc_size (rel),
3334 		size);
3335       else if (pcrel && !rel->pc_relative)
3336 	as_bad (_("non-pc-relative relocation for pc-relative field"));
3337       else if ((rel->complain_on_overflow == complain_overflow_signed
3338 		&& !sign)
3339 	       || (rel->complain_on_overflow == complain_overflow_unsigned
3340 		   && sign > 0))
3341 	as_bad (_("relocated field and relocation type differ in signedness"));
3342       else
3343 	return other;
3344       return NO_RELOC;
3345     }
3346 
3347   if (pcrel)
3348     {
3349       if (!sign)
3350 	as_bad (_("there are no unsigned pc-relative relocations"));
3351       switch (size)
3352 	{
3353 	case 1: return BFD_RELOC_8_PCREL;
3354 	case 2: return BFD_RELOC_16_PCREL;
3355 	case 4: return BFD_RELOC_32_PCREL;
3356 	case 8: return BFD_RELOC_64_PCREL;
3357 	}
3358       as_bad (_("cannot do %u byte pc-relative relocation"), size);
3359     }
3360   else
3361     {
3362       if (sign > 0)
3363 	switch (size)
3364 	  {
3365 	  case 4: return BFD_RELOC_X86_64_32S;
3366 	  }
3367       else
3368 	switch (size)
3369 	  {
3370 	  case 1: return BFD_RELOC_8;
3371 	  case 2: return BFD_RELOC_16;
3372 	  case 4: return BFD_RELOC_32;
3373 	  case 8: return BFD_RELOC_64;
3374 	  }
3375       as_bad (_("cannot do %s %u byte relocation"),
3376 	      sign > 0 ? "signed" : "unsigned", size);
3377     }
3378 
3379   return NO_RELOC;
3380 }
3381 
3382 /* Here we decide which fixups can be adjusted to make them relative to
3383    the beginning of the section instead of the symbol.  Basically we need
3384    to make sure that the dynamic relocations are done correctly, so in
3385    some cases we force the original symbol to be used.  */
3386 
3387 int
tc_i386_fix_adjustable(fixS * fixP ATTRIBUTE_UNUSED)3388 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3389 {
3390 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3391   if (!IS_ELF)
3392     return 1;
3393 
3394   /* Don't adjust pc-relative references to merge sections in 64-bit
3395      mode.  */
3396   if (use_rela_relocations
3397       && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3398       && fixP->fx_pcrel)
3399     return 0;
3400 
3401   /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3402      and changed later by validate_fix.  */
3403   if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3404       && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3405     return 0;
3406 
3407   /* Adjust_reloc_syms doesn't know about the GOT.  Need to keep symbol
3408      for size relocations.  */
3409   if (fixP->fx_r_type == BFD_RELOC_SIZE32
3410       || fixP->fx_r_type == BFD_RELOC_SIZE64
3411       || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3412       || fixP->fx_r_type == BFD_RELOC_386_GOT32
3413       || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3414       || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3415       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3416       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3417       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3418       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3419       || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3420       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3421       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3422       || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3423       || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3424       || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3425       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3426       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3427       || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3428       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3429       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3430       || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3431       || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3432       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3433       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3434       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3435       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3436       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3437       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3438       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3439       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3440     return 0;
3441 #endif
3442   return 1;
3443 }
3444 
3445 static INLINE bool
want_disp32(const insn_template * t)3446 want_disp32 (const insn_template *t)
3447 {
3448   return flag_code != CODE_64BIT
3449 	 || i.prefix[ADDR_PREFIX]
3450 	 || (t->base_opcode == 0x8d
3451 	     && t->opcode_modifier.opcodespace == SPACE_BASE
3452 	     && (!i.types[1].bitfield.qword
3453 		|| t->opcode_modifier.size == SIZE32));
3454 }
3455 
3456 static int
intel_float_operand(const char * mnemonic)3457 intel_float_operand (const char *mnemonic)
3458 {
3459   /* Note that the value returned is meaningful only for opcodes with (memory)
3460      operands, hence the code here is free to improperly handle opcodes that
3461      have no operands (for better performance and smaller code). */
3462 
3463   if (mnemonic[0] != 'f')
3464     return 0; /* non-math */
3465 
3466   switch (mnemonic[1])
3467     {
3468     /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3469        the fs segment override prefix not currently handled because no
3470        call path can make opcodes without operands get here */
3471     case 'i':
3472       return 2 /* integer op */;
3473     case 'l':
3474       if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3475 	return 3; /* fldcw/fldenv */
3476       break;
3477     case 'n':
3478       if (mnemonic[2] != 'o' /* fnop */)
3479 	return 3; /* non-waiting control op */
3480       break;
3481     case 'r':
3482       if (mnemonic[2] == 's')
3483 	return 3; /* frstor/frstpm */
3484       break;
3485     case 's':
3486       if (mnemonic[2] == 'a')
3487 	return 3; /* fsave */
3488       if (mnemonic[2] == 't')
3489 	{
3490 	  switch (mnemonic[3])
3491 	    {
3492 	    case 'c': /* fstcw */
3493 	    case 'd': /* fstdw */
3494 	    case 'e': /* fstenv */
3495 	    case 's': /* fsts[gw] */
3496 	      return 3;
3497 	    }
3498 	}
3499       break;
3500     case 'x':
3501       if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3502 	return 0; /* fxsave/fxrstor are not really math ops */
3503       break;
3504     }
3505 
3506   return 1;
3507 }
3508 
3509 static INLINE void
install_template(const insn_template * t)3510 install_template (const insn_template *t)
3511 {
3512   unsigned int l;
3513 
3514   i.tm = *t;
3515 
3516   /* Note that for pseudo prefixes this produces a length of 1. But for them
3517      the length isn't interesting at all.  */
3518   for (l = 1; l < 4; ++l)
3519     if (!(t->base_opcode >> (8 * l)))
3520       break;
3521 
3522   i.opcode_length = l;
3523 }
3524 
3525 /* Build the VEX prefix.  */
3526 
3527 static void
build_vex_prefix(const insn_template * t)3528 build_vex_prefix (const insn_template *t)
3529 {
3530   unsigned int register_specifier;
3531   unsigned int vector_length;
3532   unsigned int w;
3533 
3534   /* Check register specifier.  */
3535   if (i.vex.register_specifier)
3536     {
3537       register_specifier =
3538 	~register_number (i.vex.register_specifier) & 0xf;
3539       gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3540     }
3541   else
3542     register_specifier = 0xf;
3543 
3544   /* Use 2-byte VEX prefix by swapping destination and source operand
3545      if there are more than 1 register operand.  */
3546   if (i.reg_operands > 1
3547       && i.vec_encoding != vex_encoding_vex3
3548       && i.dir_encoding == dir_encoding_default
3549       && i.operands == i.reg_operands
3550       && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3551       && i.tm.opcode_modifier.opcodespace == SPACE_0F
3552       && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3553       && i.rex == REX_B)
3554     {
3555       unsigned int xchg = i.operands - 1;
3556       union i386_op temp_op;
3557       i386_operand_type temp_type;
3558 
3559       temp_type = i.types[xchg];
3560       i.types[xchg] = i.types[0];
3561       i.types[0] = temp_type;
3562       temp_op = i.op[xchg];
3563       i.op[xchg] = i.op[0];
3564       i.op[0] = temp_op;
3565 
3566       gas_assert (i.rm.mode == 3);
3567 
3568       i.rex = REX_R;
3569       xchg = i.rm.regmem;
3570       i.rm.regmem = i.rm.reg;
3571       i.rm.reg = xchg;
3572 
3573       if (i.tm.opcode_modifier.d)
3574 	i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3575 			    ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3576       else /* Use the next insn.  */
3577 	install_template (&t[1]);
3578     }
3579 
3580   /* Use 2-byte VEX prefix by swapping commutative source operands if there
3581      are no memory operands and at least 3 register ones.  */
3582   if (i.reg_operands >= 3
3583       && i.vec_encoding != vex_encoding_vex3
3584       && i.reg_operands == i.operands - i.imm_operands
3585       && i.tm.opcode_modifier.vex
3586       && i.tm.opcode_modifier.commutative
3587       && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3588       && i.rex == REX_B
3589       && i.vex.register_specifier
3590       && !(i.vex.register_specifier->reg_flags & RegRex))
3591     {
3592       unsigned int xchg = i.operands - i.reg_operands;
3593       union i386_op temp_op;
3594       i386_operand_type temp_type;
3595 
3596       gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
3597       gas_assert (!i.tm.opcode_modifier.sae);
3598       gas_assert (operand_type_equal (&i.types[i.operands - 2],
3599                                       &i.types[i.operands - 3]));
3600       gas_assert (i.rm.mode == 3);
3601 
3602       temp_type = i.types[xchg];
3603       i.types[xchg] = i.types[xchg + 1];
3604       i.types[xchg + 1] = temp_type;
3605       temp_op = i.op[xchg];
3606       i.op[xchg] = i.op[xchg + 1];
3607       i.op[xchg + 1] = temp_op;
3608 
3609       i.rex = 0;
3610       xchg = i.rm.regmem | 8;
3611       i.rm.regmem = ~register_specifier & 0xf;
3612       gas_assert (!(i.rm.regmem & 8));
3613       i.vex.register_specifier += xchg - i.rm.regmem;
3614       register_specifier = ~xchg & 0xf;
3615     }
3616 
3617   if (i.tm.opcode_modifier.vex == VEXScalar)
3618     vector_length = avxscalar;
3619   else if (i.tm.opcode_modifier.vex == VEX256)
3620     vector_length = 1;
3621   else
3622     {
3623       unsigned int op;
3624 
3625       /* Determine vector length from the last multi-length vector
3626 	 operand.  */
3627       vector_length = 0;
3628       for (op = t->operands; op--;)
3629 	if (t->operand_types[op].bitfield.xmmword
3630 	    && t->operand_types[op].bitfield.ymmword
3631 	    && i.types[op].bitfield.ymmword)
3632 	  {
3633 	    vector_length = 1;
3634 	    break;
3635 	  }
3636     }
3637 
3638   /* Check the REX.W bit and VEXW.  */
3639   if (i.tm.opcode_modifier.vexw == VEXWIG)
3640     w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3641   else if (i.tm.opcode_modifier.vexw)
3642     w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3643   else
3644     w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3645 
3646   /* Use 2-byte VEX prefix if possible.  */
3647   if (w == 0
3648       && i.vec_encoding != vex_encoding_vex3
3649       && i.tm.opcode_modifier.opcodespace == SPACE_0F
3650       && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3651     {
3652       /* 2-byte VEX prefix.  */
3653       unsigned int r;
3654 
3655       i.vex.length = 2;
3656       i.vex.bytes[0] = 0xc5;
3657 
3658       /* Check the REX.R bit.  */
3659       r = (i.rex & REX_R) ? 0 : 1;
3660       i.vex.bytes[1] = (r << 7
3661 			| register_specifier << 3
3662 			| vector_length << 2
3663 			| i.tm.opcode_modifier.opcodeprefix);
3664     }
3665   else
3666     {
3667       /* 3-byte VEX prefix.  */
3668       i.vex.length = 3;
3669 
3670       switch (i.tm.opcode_modifier.opcodespace)
3671 	{
3672 	case SPACE_0F:
3673 	case SPACE_0F38:
3674 	case SPACE_0F3A:
3675 	  i.vex.bytes[0] = 0xc4;
3676 	  break;
3677 	case SPACE_XOP08:
3678 	case SPACE_XOP09:
3679 	case SPACE_XOP0A:
3680 	  i.vex.bytes[0] = 0x8f;
3681 	  break;
3682 	default:
3683 	  abort ();
3684 	}
3685 
3686       /* The high 3 bits of the second VEX byte are 1's compliment
3687 	 of RXB bits from REX.  */
3688       i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3689 
3690       i.vex.bytes[2] = (w << 7
3691 			| register_specifier << 3
3692 			| vector_length << 2
3693 			| i.tm.opcode_modifier.opcodeprefix);
3694     }
3695 }
3696 
3697 static INLINE bool
is_evex_encoding(const insn_template * t)3698 is_evex_encoding (const insn_template *t)
3699 {
3700   return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3701 	 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3702 	 || t->opcode_modifier.sae;
3703 }
3704 
3705 static INLINE bool
is_any_vex_encoding(const insn_template * t)3706 is_any_vex_encoding (const insn_template *t)
3707 {
3708   return t->opcode_modifier.vex || is_evex_encoding (t);
3709 }
3710 
3711 static unsigned int
get_broadcast_bytes(const insn_template * t,bool diag)3712 get_broadcast_bytes (const insn_template *t, bool diag)
3713 {
3714   unsigned int op, bytes;
3715   const i386_operand_type *types;
3716 
3717   if (i.broadcast.type)
3718     return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3719 				* i.broadcast.type);
3720 
3721   gas_assert (intel_syntax);
3722 
3723   for (op = 0; op < t->operands; ++op)
3724     if (t->operand_types[op].bitfield.baseindex)
3725       break;
3726 
3727   gas_assert (op < t->operands);
3728 
3729   if (t->opcode_modifier.evex
3730       && t->opcode_modifier.evex != EVEXDYN)
3731     switch (i.broadcast.bytes)
3732       {
3733       case 1:
3734 	if (t->operand_types[op].bitfield.word)
3735 	  return 2;
3736       /* Fall through.  */
3737       case 2:
3738 	if (t->operand_types[op].bitfield.dword)
3739 	  return 4;
3740       /* Fall through.  */
3741       case 4:
3742 	if (t->operand_types[op].bitfield.qword)
3743 	  return 8;
3744       /* Fall through.  */
3745       case 8:
3746 	if (t->operand_types[op].bitfield.xmmword)
3747 	  return 16;
3748 	if (t->operand_types[op].bitfield.ymmword)
3749 	  return 32;
3750 	if (t->operand_types[op].bitfield.zmmword)
3751 	  return 64;
3752       /* Fall through.  */
3753       default:
3754         abort ();
3755       }
3756 
3757   gas_assert (op + 1 < t->operands);
3758 
3759   if (t->operand_types[op + 1].bitfield.xmmword
3760       + t->operand_types[op + 1].bitfield.ymmword
3761       + t->operand_types[op + 1].bitfield.zmmword > 1)
3762     {
3763       types = &i.types[op + 1];
3764       diag = false;
3765     }
3766   else /* Ambiguous - guess with a preference to non-AVX512VL forms.  */
3767     types = &t->operand_types[op];
3768 
3769   if (types->bitfield.zmmword)
3770     bytes = 64;
3771   else if (types->bitfield.ymmword)
3772     bytes = 32;
3773   else
3774     bytes = 16;
3775 
3776   if (diag)
3777     as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3778 	     t->name, bytes * 8);
3779 
3780   return bytes;
3781 }
3782 
3783 /* Build the EVEX prefix.  */
3784 
3785 static void
build_evex_prefix(void)3786 build_evex_prefix (void)
3787 {
3788   unsigned int register_specifier, w;
3789   rex_byte vrex_used = 0;
3790 
3791   /* Check register specifier.  */
3792   if (i.vex.register_specifier)
3793     {
3794       gas_assert ((i.vrex & REX_X) == 0);
3795 
3796       register_specifier = i.vex.register_specifier->reg_num;
3797       if ((i.vex.register_specifier->reg_flags & RegRex))
3798 	register_specifier += 8;
3799       /* The upper 16 registers are encoded in the fourth byte of the
3800 	 EVEX prefix.  */
3801       if (!(i.vex.register_specifier->reg_flags & RegVRex))
3802 	i.vex.bytes[3] = 0x8;
3803       register_specifier = ~register_specifier & 0xf;
3804     }
3805   else
3806     {
3807       register_specifier = 0xf;
3808 
3809       /* Encode upper 16 vector index register in the fourth byte of
3810 	 the EVEX prefix.  */
3811       if (!(i.vrex & REX_X))
3812 	i.vex.bytes[3] = 0x8;
3813       else
3814 	vrex_used |= REX_X;
3815     }
3816 
3817   /* 4 byte EVEX prefix.  */
3818   i.vex.length = 4;
3819   i.vex.bytes[0] = 0x62;
3820 
3821   /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3822      bits from REX.  */
3823   gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3824   gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
3825   i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3826 
3827   /* The fifth bit of the second EVEX byte is 1's compliment of the
3828      REX_R bit in VREX.  */
3829   if (!(i.vrex & REX_R))
3830     i.vex.bytes[1] |= 0x10;
3831   else
3832     vrex_used |= REX_R;
3833 
3834   if ((i.reg_operands + i.imm_operands) == i.operands)
3835     {
3836       /* When all operands are registers, the REX_X bit in REX is not
3837 	 used.  We reuse it to encode the upper 16 registers, which is
3838 	 indicated by the REX_B bit in VREX.  The REX_X bit is encoded
3839 	 as 1's compliment.  */
3840       if ((i.vrex & REX_B))
3841 	{
3842 	  vrex_used |= REX_B;
3843 	  i.vex.bytes[1] &= ~0x40;
3844 	}
3845     }
3846 
3847   /* EVEX instructions shouldn't need the REX prefix.  */
3848   i.vrex &= ~vrex_used;
3849   gas_assert (i.vrex == 0);
3850 
3851   /* Check the REX.W bit and VEXW.  */
3852   if (i.tm.opcode_modifier.vexw == VEXWIG)
3853     w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3854   else if (i.tm.opcode_modifier.vexw)
3855     w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3856   else
3857     w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
3858 
3859   /* The third byte of the EVEX prefix.  */
3860   i.vex.bytes[2] = ((w << 7)
3861 		    | (register_specifier << 3)
3862 		    | 4 /* Encode the U bit.  */
3863 		    | i.tm.opcode_modifier.opcodeprefix);
3864 
3865   /* The fourth byte of the EVEX prefix.  */
3866   /* The zeroing-masking bit.  */
3867   if (i.mask.reg && i.mask.zeroing)
3868     i.vex.bytes[3] |= 0x80;
3869 
3870   /* Don't always set the broadcast bit if there is no RC.  */
3871   if (i.rounding.type == rc_none)
3872     {
3873       /* Encode the vector length.  */
3874       unsigned int vec_length;
3875 
3876       if (!i.tm.opcode_modifier.evex
3877 	  || i.tm.opcode_modifier.evex == EVEXDYN)
3878 	{
3879 	  unsigned int op;
3880 
3881 	  /* Determine vector length from the last multi-length vector
3882 	     operand.  */
3883 	  for (op = i.operands; op--;)
3884 	    if (i.tm.operand_types[op].bitfield.xmmword
3885 		+ i.tm.operand_types[op].bitfield.ymmword
3886 		+ i.tm.operand_types[op].bitfield.zmmword > 1)
3887 	      {
3888 		if (i.types[op].bitfield.zmmword)
3889 		  {
3890 		    i.tm.opcode_modifier.evex = EVEX512;
3891 		    break;
3892 		  }
3893 		else if (i.types[op].bitfield.ymmword)
3894 		  {
3895 		    i.tm.opcode_modifier.evex = EVEX256;
3896 		    break;
3897 		  }
3898 		else if (i.types[op].bitfield.xmmword)
3899 		  {
3900 		    i.tm.opcode_modifier.evex = EVEX128;
3901 		    break;
3902 		  }
3903 		else if (i.broadcast.bytes && op == i.broadcast.operand)
3904 		  {
3905 		    switch (get_broadcast_bytes (&i.tm, true))
3906 		      {
3907 			case 64:
3908 			  i.tm.opcode_modifier.evex = EVEX512;
3909 			  break;
3910 			case 32:
3911 			  i.tm.opcode_modifier.evex = EVEX256;
3912 			  break;
3913 			case 16:
3914 			  i.tm.opcode_modifier.evex = EVEX128;
3915 			  break;
3916 			default:
3917 			  abort ();
3918 		      }
3919 		    break;
3920 		  }
3921 	      }
3922 
3923 	  if (op >= MAX_OPERANDS)
3924 	    abort ();
3925 	}
3926 
3927       switch (i.tm.opcode_modifier.evex)
3928 	{
3929 	case EVEXLIG: /* LL' is ignored */
3930 	  vec_length = evexlig << 5;
3931 	  break;
3932 	case EVEX128:
3933 	  vec_length = 0 << 5;
3934 	  break;
3935 	case EVEX256:
3936 	  vec_length = 1 << 5;
3937 	  break;
3938 	case EVEX512:
3939 	  vec_length = 2 << 5;
3940 	  break;
3941 	default:
3942 	  abort ();
3943 	  break;
3944 	}
3945       i.vex.bytes[3] |= vec_length;
3946       /* Encode the broadcast bit.  */
3947       if (i.broadcast.bytes)
3948 	i.vex.bytes[3] |= 0x10;
3949     }
3950   else if (i.rounding.type != saeonly)
3951     i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
3952   else
3953     i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
3954 
3955   if (i.mask.reg)
3956     i.vex.bytes[3] |= i.mask.reg->reg_num;
3957 }
3958 
3959 static void
process_immext(void)3960 process_immext (void)
3961 {
3962   expressionS *exp;
3963 
3964   /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
3965      which is coded in the same place as an 8-bit immediate field
3966      would be.  Here we fake an 8-bit immediate operand from the
3967      opcode suffix stored in tm.extension_opcode.
3968 
3969      AVX instructions also use this encoding, for some of
3970      3 argument instructions.  */
3971 
3972   gas_assert (i.imm_operands <= 1
3973 	      && (i.operands <= 2
3974 		  || (is_any_vex_encoding (&i.tm)
3975 		      && i.operands <= 4)));
3976 
3977   exp = &im_expressions[i.imm_operands++];
3978   i.op[i.operands].imms = exp;
3979   i.types[i.operands] = imm8;
3980   i.operands++;
3981   exp->X_op = O_constant;
3982   exp->X_add_number = i.tm.extension_opcode;
3983   i.tm.extension_opcode = None;
3984 }
3985 
3986 
3987 static int
check_hle(void)3988 check_hle (void)
3989 {
3990   switch (i.tm.opcode_modifier.prefixok)
3991     {
3992     default:
3993       abort ();
3994     case PrefixLock:
3995     case PrefixNone:
3996     case PrefixNoTrack:
3997     case PrefixRep:
3998       as_bad (_("invalid instruction `%s' after `%s'"),
3999 	      i.tm.name, i.hle_prefix);
4000       return 0;
4001     case PrefixHLELock:
4002       if (i.prefix[LOCK_PREFIX])
4003 	return 1;
4004       as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4005       return 0;
4006     case PrefixHLEAny:
4007       return 1;
4008     case PrefixHLERelease:
4009       if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4010 	{
4011 	  as_bad (_("instruction `%s' after `xacquire' not allowed"),
4012 		  i.tm.name);
4013 	  return 0;
4014 	}
4015       if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4016 	{
4017 	  as_bad (_("memory destination needed for instruction `%s'"
4018 		    " after `xrelease'"), i.tm.name);
4019 	  return 0;
4020 	}
4021       return 1;
4022     }
4023 }
4024 
4025 /* Encode aligned vector move as unaligned vector move.  */
4026 
4027 static void
encode_with_unaligned_vector_move(void)4028 encode_with_unaligned_vector_move (void)
4029 {
4030   switch (i.tm.base_opcode)
4031     {
4032     case 0x28:	/* Load instructions.  */
4033     case 0x29:	/* Store instructions.  */
4034       /* movaps/movapd/vmovaps/vmovapd.  */
4035       if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4036 	  && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4037 	i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4038       break;
4039     case 0x6f:	/* Load instructions.  */
4040     case 0x7f:	/* Store instructions.  */
4041       /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4042       if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4043 	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4044 	i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4045       break;
4046     default:
4047       break;
4048     }
4049 }
4050 
4051 /* Try the shortest encoding by shortening operand size.  */
4052 
4053 static void
optimize_encoding(void)4054 optimize_encoding (void)
4055 {
4056   unsigned int j;
4057 
4058   if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4059       && i.tm.base_opcode == 0x8d)
4060     {
4061       /* Optimize: -O:
4062 	   lea symbol, %rN    -> mov $symbol, %rN
4063 	   lea (%rM), %rN     -> mov %rM, %rN
4064 	   lea (,%rM,1), %rN  -> mov %rM, %rN
4065 
4066 	   and in 32-bit mode for 16-bit addressing
4067 
4068 	   lea (%rM), %rN     -> movzx %rM, %rN
4069 
4070 	   and in 64-bit mode zap 32-bit addressing in favor of using a
4071 	   32-bit (or less) destination.
4072        */
4073       if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4074 	{
4075 	  if (!i.op[1].regs->reg_type.bitfield.word)
4076 	    i.tm.opcode_modifier.size = SIZE32;
4077 	  i.prefix[ADDR_PREFIX] = 0;
4078 	}
4079 
4080       if (!i.index_reg && !i.base_reg)
4081 	{
4082 	  /* Handle:
4083 	       lea symbol, %rN    -> mov $symbol, %rN
4084 	   */
4085 	  if (flag_code == CODE_64BIT)
4086 	    {
4087 	      /* Don't transform a relocation to a 16-bit one.  */
4088 	      if (i.op[0].disps
4089 		  && i.op[0].disps->X_op != O_constant
4090 		  && i.op[1].regs->reg_type.bitfield.word)
4091 		return;
4092 
4093 	      if (!i.op[1].regs->reg_type.bitfield.qword
4094 		  || i.tm.opcode_modifier.size == SIZE32)
4095 		{
4096 		  i.tm.base_opcode = 0xb8;
4097 		  i.tm.opcode_modifier.modrm = 0;
4098 		  if (!i.op[1].regs->reg_type.bitfield.word)
4099 		    i.types[0].bitfield.imm32 = 1;
4100 		  else
4101 		    {
4102 		      i.tm.opcode_modifier.size = SIZE16;
4103 		      i.types[0].bitfield.imm16 = 1;
4104 		    }
4105 		}
4106 	      else
4107 		{
4108 		  /* Subject to further optimization below.  */
4109 		  i.tm.base_opcode = 0xc7;
4110 		  i.tm.extension_opcode = 0;
4111 		  i.types[0].bitfield.imm32s = 1;
4112 		  i.types[0].bitfield.baseindex = 0;
4113 		}
4114 	    }
4115 	  /* Outside of 64-bit mode address and operand sizes have to match if
4116 	     a relocation is involved, as otherwise we wouldn't (currently) or
4117 	     even couldn't express the relocation correctly.  */
4118 	  else if (i.op[0].disps
4119 		   && i.op[0].disps->X_op != O_constant
4120 		   && ((!i.prefix[ADDR_PREFIX])
4121 		       != (flag_code == CODE_32BIT
4122 			   ? i.op[1].regs->reg_type.bitfield.dword
4123 			   : i.op[1].regs->reg_type.bitfield.word)))
4124 	    return;
4125 	  /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4126 	     destination is going to grow encoding size.  */
4127 	  else if (flag_code == CODE_16BIT
4128 		   && (optimize <= 1 || optimize_for_space)
4129 		   && !i.prefix[ADDR_PREFIX]
4130 		   && i.op[1].regs->reg_type.bitfield.dword)
4131 	    return;
4132 	  else
4133 	    {
4134 	      i.tm.base_opcode = 0xb8;
4135 	      i.tm.opcode_modifier.modrm = 0;
4136 	      if (i.op[1].regs->reg_type.bitfield.dword)
4137 		i.types[0].bitfield.imm32 = 1;
4138 	      else
4139 		i.types[0].bitfield.imm16 = 1;
4140 
4141 	      if (i.op[0].disps
4142 		  && i.op[0].disps->X_op == O_constant
4143 		  && i.op[1].regs->reg_type.bitfield.dword
4144 		  /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4145 		     GCC 5. */
4146 		  && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4147 		i.op[0].disps->X_add_number &= 0xffff;
4148 	    }
4149 
4150 	  i.tm.operand_types[0] = i.types[0];
4151 	  i.imm_operands = 1;
4152 	  if (!i.op[0].imms)
4153 	    {
4154 	      i.op[0].imms = &im_expressions[0];
4155 	      i.op[0].imms->X_op = O_absent;
4156 	    }
4157 	}
4158       else if (i.op[0].disps
4159 		  && (i.op[0].disps->X_op != O_constant
4160 		      || i.op[0].disps->X_add_number))
4161 	return;
4162       else
4163 	{
4164 	  /* Handle:
4165 	       lea (%rM), %rN     -> mov %rM, %rN
4166 	       lea (,%rM,1), %rN  -> mov %rM, %rN
4167 	       lea (%rM), %rN     -> movzx %rM, %rN
4168 	   */
4169 	  const reg_entry *addr_reg;
4170 
4171 	  if (!i.index_reg && i.base_reg->reg_num != RegIP)
4172 	    addr_reg = i.base_reg;
4173 	  else if (!i.base_reg
4174 		   && i.index_reg->reg_num != RegIZ
4175 		   && !i.log2_scale_factor)
4176 	    addr_reg = i.index_reg;
4177 	  else
4178 	    return;
4179 
4180 	  if (addr_reg->reg_type.bitfield.word
4181 	      && i.op[1].regs->reg_type.bitfield.dword)
4182 	    {
4183 	      if (flag_code != CODE_32BIT)
4184 		return;
4185 	      i.tm.opcode_modifier.opcodespace = SPACE_0F;
4186 	      i.tm.base_opcode = 0xb7;
4187 	    }
4188 	  else
4189 	    i.tm.base_opcode = 0x8b;
4190 
4191 	  if (addr_reg->reg_type.bitfield.dword
4192 	      && i.op[1].regs->reg_type.bitfield.qword)
4193 	    i.tm.opcode_modifier.size = SIZE32;
4194 
4195 	  i.op[0].regs = addr_reg;
4196 	  i.reg_operands = 2;
4197 	}
4198 
4199       i.mem_operands = 0;
4200       i.disp_operands = 0;
4201       i.prefix[ADDR_PREFIX] = 0;
4202       i.prefix[SEG_PREFIX] = 0;
4203       i.seg[0] = NULL;
4204     }
4205 
4206   if (optimize_for_space
4207       && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4208       && i.reg_operands == 1
4209       && i.imm_operands == 1
4210       && !i.types[1].bitfield.byte
4211       && i.op[0].imms->X_op == O_constant
4212       && fits_in_imm7 (i.op[0].imms->X_add_number)
4213       && (i.tm.base_opcode == 0xa8
4214 	  || (i.tm.base_opcode == 0xf6
4215 	      && i.tm.extension_opcode == 0x0)))
4216     {
4217       /* Optimize: -Os:
4218 	   test $imm7, %r64/%r32/%r16  -> test $imm7, %r8
4219        */
4220       unsigned int base_regnum = i.op[1].regs->reg_num;
4221       if (flag_code == CODE_64BIT || base_regnum < 4)
4222 	{
4223 	  i.types[1].bitfield.byte = 1;
4224 	  /* Ignore the suffix.  */
4225 	  i.suffix = 0;
4226 	  /* Convert to byte registers.  */
4227 	  if (i.types[1].bitfield.word)
4228 	    j = 16;
4229 	  else if (i.types[1].bitfield.dword)
4230 	    j = 32;
4231 	  else
4232 	    j = 48;
4233 	  if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4234 	    j += 8;
4235 	  i.op[1].regs -= j;
4236 	}
4237     }
4238   else if (flag_code == CODE_64BIT
4239 	   && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4240 	   && ((i.types[1].bitfield.qword
4241 		&& i.reg_operands == 1
4242 		&& i.imm_operands == 1
4243 		&& i.op[0].imms->X_op == O_constant
4244 		&& ((i.tm.base_opcode == 0xb8
4245 		     && i.tm.extension_opcode == None
4246 		     && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4247 		    || (fits_in_imm31 (i.op[0].imms->X_add_number)
4248 			&& ((i.tm.base_opcode == 0x24
4249 			     || i.tm.base_opcode == 0xa8)
4250 			    || (i.tm.base_opcode == 0x80
4251 				&& i.tm.extension_opcode == 0x4)
4252 			    || ((i.tm.base_opcode == 0xf6
4253 				 || (i.tm.base_opcode | 1) == 0xc7)
4254 				&& i.tm.extension_opcode == 0x0)))
4255 		    || (fits_in_imm7 (i.op[0].imms->X_add_number)
4256 			&& i.tm.base_opcode == 0x83
4257 			&& i.tm.extension_opcode == 0x4)))
4258 	       || (i.types[0].bitfield.qword
4259 		   && ((i.reg_operands == 2
4260 			&& i.op[0].regs == i.op[1].regs
4261 			&& (i.tm.base_opcode == 0x30
4262 			    || i.tm.base_opcode == 0x28))
4263 		       || (i.reg_operands == 1
4264 			   && i.operands == 1
4265 			   && i.tm.base_opcode == 0x30)))))
4266     {
4267       /* Optimize: -O:
4268 	   andq $imm31, %r64   -> andl $imm31, %r32
4269 	   andq $imm7, %r64    -> andl $imm7, %r32
4270 	   testq $imm31, %r64  -> testl $imm31, %r32
4271 	   xorq %r64, %r64     -> xorl %r32, %r32
4272 	   subq %r64, %r64     -> subl %r32, %r32
4273 	   movq $imm31, %r64   -> movl $imm31, %r32
4274 	   movq $imm32, %r64   -> movl $imm32, %r32
4275         */
4276       i.tm.opcode_modifier.norex64 = 1;
4277       if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
4278 	{
4279 	  /* Handle
4280 	       movq $imm31, %r64   -> movl $imm31, %r32
4281 	       movq $imm32, %r64   -> movl $imm32, %r32
4282 	   */
4283 	  i.tm.operand_types[0].bitfield.imm32 = 1;
4284 	  i.tm.operand_types[0].bitfield.imm32s = 0;
4285 	  i.tm.operand_types[0].bitfield.imm64 = 0;
4286 	  i.types[0].bitfield.imm32 = 1;
4287 	  i.types[0].bitfield.imm32s = 0;
4288 	  i.types[0].bitfield.imm64 = 0;
4289 	  i.types[1].bitfield.dword = 1;
4290 	  i.types[1].bitfield.qword = 0;
4291 	  if ((i.tm.base_opcode | 1) == 0xc7)
4292 	    {
4293 	      /* Handle
4294 		   movq $imm31, %r64   -> movl $imm31, %r32
4295 	       */
4296 	      i.tm.base_opcode = 0xb8;
4297 	      i.tm.extension_opcode = None;
4298 	      i.tm.opcode_modifier.w = 0;
4299 	      i.tm.opcode_modifier.modrm = 0;
4300 	    }
4301 	}
4302     }
4303   else if (optimize > 1
4304 	   && !optimize_for_space
4305 	   && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4306 	   && i.reg_operands == 2
4307 	   && i.op[0].regs == i.op[1].regs
4308 	   && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4309 	       || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4310 	   && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4311     {
4312       /* Optimize: -O2:
4313 	   andb %rN, %rN  -> testb %rN, %rN
4314 	   andw %rN, %rN  -> testw %rN, %rN
4315 	   andq %rN, %rN  -> testq %rN, %rN
4316 	   orb %rN, %rN   -> testb %rN, %rN
4317 	   orw %rN, %rN   -> testw %rN, %rN
4318 	   orq %rN, %rN   -> testq %rN, %rN
4319 
4320 	   and outside of 64-bit mode
4321 
4322 	   andl %rN, %rN  -> testl %rN, %rN
4323 	   orl %rN, %rN   -> testl %rN, %rN
4324        */
4325       i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4326     }
4327   else if (i.reg_operands == 3
4328 	   && i.op[0].regs == i.op[1].regs
4329 	   && !i.types[2].bitfield.xmmword
4330 	   && (i.tm.opcode_modifier.vex
4331 	       || ((!i.mask.reg || i.mask.zeroing)
4332 		   && i.rounding.type == rc_none
4333 		   && is_evex_encoding (&i.tm)
4334 		   && (i.vec_encoding != vex_encoding_evex
4335 		       || cpu_arch_isa_flags.bitfield.cpuavx512vl
4336 		       || i.tm.cpu_flags.bitfield.cpuavx512vl
4337 		       || (i.tm.operand_types[2].bitfield.zmmword
4338 			   && i.types[2].bitfield.ymmword))))
4339 	   && ((i.tm.base_opcode == 0x55
4340 		|| i.tm.base_opcode == 0x57
4341 		|| i.tm.base_opcode == 0xdf
4342 		|| i.tm.base_opcode == 0xef
4343 		|| i.tm.base_opcode == 0xf8
4344 		|| i.tm.base_opcode == 0xf9
4345 		|| i.tm.base_opcode == 0xfa
4346 		|| i.tm.base_opcode == 0xfb
4347 		|| i.tm.base_opcode == 0x42
4348 		|| i.tm.base_opcode == 0x47)
4349 	       && i.tm.extension_opcode == None))
4350     {
4351       /* Optimize: -O1:
4352 	   VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4353 	   vpsubq and vpsubw:
4354 	     EVEX VOP %zmmM, %zmmM, %zmmN
4355 	       -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4356 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4357 	     EVEX VOP %ymmM, %ymmM, %ymmN
4358 	       -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4359 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4360 	     VEX VOP %ymmM, %ymmM, %ymmN
4361 	       -> VEX VOP %xmmM, %xmmM, %xmmN
4362 	   VOP, one of vpandn and vpxor:
4363 	     VEX VOP %ymmM, %ymmM, %ymmN
4364 	       -> VEX VOP %xmmM, %xmmM, %xmmN
4365 	   VOP, one of vpandnd and vpandnq:
4366 	     EVEX VOP %zmmM, %zmmM, %zmmN
4367 	       -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4368 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4369 	     EVEX VOP %ymmM, %ymmM, %ymmN
4370 	       -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4371 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4372 	   VOP, one of vpxord and vpxorq:
4373 	     EVEX VOP %zmmM, %zmmM, %zmmN
4374 	       -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4375 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4376 	     EVEX VOP %ymmM, %ymmM, %ymmN
4377 	       -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4378 	       -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4379 	   VOP, one of kxord and kxorq:
4380 	     VEX VOP %kM, %kM, %kN
4381 	       -> VEX kxorw %kM, %kM, %kN
4382 	   VOP, one of kandnd and kandnq:
4383 	     VEX VOP %kM, %kM, %kN
4384 	       -> VEX kandnw %kM, %kM, %kN
4385        */
4386       if (is_evex_encoding (&i.tm))
4387 	{
4388 	  if (i.vec_encoding != vex_encoding_evex)
4389 	    {
4390 	      i.tm.opcode_modifier.vex = VEX128;
4391 	      i.tm.opcode_modifier.vexw = VEXW0;
4392 	      i.tm.opcode_modifier.evex = 0;
4393 	    }
4394 	  else if (optimize > 1)
4395 	    i.tm.opcode_modifier.evex = EVEX128;
4396 	  else
4397 	    return;
4398 	}
4399       else if (i.tm.operand_types[0].bitfield.class == RegMask)
4400 	{
4401 	  i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4402 	  i.tm.opcode_modifier.vexw = VEXW0;
4403 	}
4404       else
4405 	i.tm.opcode_modifier.vex = VEX128;
4406 
4407       if (i.tm.opcode_modifier.vex)
4408 	for (j = 0; j < 3; j++)
4409 	  {
4410 	    i.types[j].bitfield.xmmword = 1;
4411 	    i.types[j].bitfield.ymmword = 0;
4412 	  }
4413     }
4414   else if (i.vec_encoding != vex_encoding_evex
4415 	   && !i.types[0].bitfield.zmmword
4416 	   && !i.types[1].bitfield.zmmword
4417 	   && !i.mask.reg
4418 	   && !i.broadcast.bytes
4419 	   && is_evex_encoding (&i.tm)
4420 	   && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4421 	       || (i.tm.base_opcode & ~4) == 0xdb
4422 	       || (i.tm.base_opcode & ~4) == 0xeb)
4423 	   && i.tm.extension_opcode == None)
4424     {
4425       /* Optimize: -O1:
4426 	   VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4427 	   vmovdqu32 and vmovdqu64:
4428 	     EVEX VOP %xmmM, %xmmN
4429 	       -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4430 	     EVEX VOP %ymmM, %ymmN
4431 	       -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4432 	     EVEX VOP %xmmM, mem
4433 	       -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4434 	     EVEX VOP %ymmM, mem
4435 	       -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4436 	     EVEX VOP mem, %xmmN
4437 	       -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4438 	     EVEX VOP mem, %ymmN
4439 	       -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4440 	   VOP, one of vpand, vpandn, vpor, vpxor:
4441 	     EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4442 	       -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4443 	     EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4444 	       -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4445 	     EVEX VOP{d,q} mem, %xmmM, %xmmN
4446 	       -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4447 	     EVEX VOP{d,q} mem, %ymmM, %ymmN
4448 	       -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4449        */
4450       for (j = 0; j < i.operands; j++)
4451 	if (operand_type_check (i.types[j], disp)
4452 	    && i.op[j].disps->X_op == O_constant)
4453 	  {
4454 	    /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4455 	       has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4456 	       bytes, we choose EVEX Disp8 over VEX Disp32.  */
4457 	    int evex_disp8, vex_disp8;
4458 	    unsigned int memshift = i.memshift;
4459 	    offsetT n = i.op[j].disps->X_add_number;
4460 
4461 	    evex_disp8 = fits_in_disp8 (n);
4462 	    i.memshift = 0;
4463 	    vex_disp8 = fits_in_disp8 (n);
4464 	    if (evex_disp8 != vex_disp8)
4465 	      {
4466 		i.memshift = memshift;
4467 		return;
4468 	      }
4469 
4470 	    i.types[j].bitfield.disp8 = vex_disp8;
4471 	    break;
4472 	  }
4473       if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4474 	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4475 	i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4476       i.tm.opcode_modifier.vex
4477 	= i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4478       i.tm.opcode_modifier.vexw = VEXW0;
4479       /* VPAND, VPOR, and VPXOR are commutative.  */
4480       if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4481 	i.tm.opcode_modifier.commutative = 1;
4482       i.tm.opcode_modifier.evex = 0;
4483       i.tm.opcode_modifier.masking = 0;
4484       i.tm.opcode_modifier.broadcast = 0;
4485       i.tm.opcode_modifier.disp8memshift = 0;
4486       i.memshift = 0;
4487       if (j < i.operands)
4488 	i.types[j].bitfield.disp8
4489 	  = fits_in_disp8 (i.op[j].disps->X_add_number);
4490     }
4491 }
4492 
4493 /* Return non-zero for load instruction.  */
4494 
4495 static int
load_insn_p(void)4496 load_insn_p (void)
4497 {
4498   unsigned int dest;
4499   int any_vex_p = is_any_vex_encoding (&i.tm);
4500   unsigned int base_opcode = i.tm.base_opcode | 1;
4501 
4502   if (!any_vex_p)
4503     {
4504       /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4505 	 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4506 	 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote.  */
4507       if (i.tm.opcode_modifier.anysize)
4508 	return 0;
4509 
4510       /* pop.   */
4511       if (strcmp (i.tm.name, "pop") == 0)
4512 	return 1;
4513     }
4514 
4515   if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4516     {
4517       /* popf, popa.   */
4518       if (i.tm.base_opcode == 0x9d
4519 	  || i.tm.base_opcode == 0x61)
4520 	return 1;
4521 
4522       /* movs, cmps, lods, scas.  */
4523       if ((i.tm.base_opcode | 0xb) == 0xaf)
4524 	return 1;
4525 
4526       /* outs, xlatb.  */
4527       if (base_opcode == 0x6f
4528 	  || i.tm.base_opcode == 0xd7)
4529 	return 1;
4530       /* NB: For AMD-specific insns with implicit memory operands,
4531 	 they're intentionally not covered.  */
4532     }
4533 
4534   /* No memory operand.  */
4535   if (!i.mem_operands)
4536     return 0;
4537 
4538   if (any_vex_p)
4539     {
4540       /* vldmxcsr.  */
4541       if (i.tm.base_opcode == 0xae
4542 	  && i.tm.opcode_modifier.vex
4543 	  && i.tm.opcode_modifier.opcodespace == SPACE_0F
4544 	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4545 	  && i.tm.extension_opcode == 2)
4546 	return 1;
4547     }
4548   else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4549     {
4550       /* test, not, neg, mul, imul, div, idiv.  */
4551       if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4552 	  && i.tm.extension_opcode != 1)
4553 	return 1;
4554 
4555       /* inc, dec.  */
4556       if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4557 	return 1;
4558 
4559       /* add, or, adc, sbb, and, sub, xor, cmp.  */
4560       if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4561 	return 1;
4562 
4563       /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4564       if ((base_opcode == 0xc1
4565 	   || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4566 	  && i.tm.extension_opcode != 6)
4567 	return 1;
4568 
4569       /* Check for x87 instructions.  */
4570       if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
4571 	{
4572 	  /* Skip fst, fstp, fstenv, fstcw.  */
4573 	  if (i.tm.base_opcode == 0xd9
4574 	      && (i.tm.extension_opcode == 2
4575 		  || i.tm.extension_opcode == 3
4576 		  || i.tm.extension_opcode == 6
4577 		  || i.tm.extension_opcode == 7))
4578 	    return 0;
4579 
4580 	  /* Skip fisttp, fist, fistp, fstp.  */
4581 	  if (i.tm.base_opcode == 0xdb
4582 	      && (i.tm.extension_opcode == 1
4583 		  || i.tm.extension_opcode == 2
4584 		  || i.tm.extension_opcode == 3
4585 		  || i.tm.extension_opcode == 7))
4586 	    return 0;
4587 
4588 	  /* Skip fisttp, fst, fstp, fsave, fstsw.  */
4589 	  if (i.tm.base_opcode == 0xdd
4590 	      && (i.tm.extension_opcode == 1
4591 		  || i.tm.extension_opcode == 2
4592 		  || i.tm.extension_opcode == 3
4593 		  || i.tm.extension_opcode == 6
4594 		  || i.tm.extension_opcode == 7))
4595 	    return 0;
4596 
4597 	  /* Skip fisttp, fist, fistp, fbstp, fistp.  */
4598 	  if (i.tm.base_opcode == 0xdf
4599 	      && (i.tm.extension_opcode == 1
4600 		  || i.tm.extension_opcode == 2
4601 		  || i.tm.extension_opcode == 3
4602 		  || i.tm.extension_opcode == 6
4603 		  || i.tm.extension_opcode == 7))
4604 	    return 0;
4605 
4606 	  return 1;
4607 	}
4608     }
4609   else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4610     {
4611       /* bt, bts, btr, btc.  */
4612       if (i.tm.base_opcode == 0xba
4613 	  && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4614 	return 1;
4615 
4616       /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld.  */
4617       if (i.tm.base_opcode == 0xc7
4618 	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4619 	  && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4620 	      || i.tm.extension_opcode == 6))
4621 	return 1;
4622 
4623       /* fxrstor, ldmxcsr, xrstor.  */
4624       if (i.tm.base_opcode == 0xae
4625 	  && (i.tm.extension_opcode == 1
4626 	      || i.tm.extension_opcode == 2
4627 	      || i.tm.extension_opcode == 5))
4628 	return 1;
4629 
4630       /* lgdt, lidt, lmsw.  */
4631       if (i.tm.base_opcode == 0x01
4632 	  && (i.tm.extension_opcode == 2
4633 	      || i.tm.extension_opcode == 3
4634 	      || i.tm.extension_opcode == 6))
4635 	return 1;
4636     }
4637 
4638   dest = i.operands - 1;
4639 
4640   /* Check fake imm8 operand and 3 source operands.  */
4641   if ((i.tm.opcode_modifier.immext
4642        || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4643       && i.types[dest].bitfield.imm8)
4644     dest--;
4645 
4646   /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg.  */
4647   if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4648       && (base_opcode == 0x1
4649 	  || base_opcode == 0x9
4650 	  || base_opcode == 0x11
4651 	  || base_opcode == 0x19
4652 	  || base_opcode == 0x21
4653 	  || base_opcode == 0x29
4654 	  || base_opcode == 0x31
4655 	  || base_opcode == 0x39
4656 	  || (base_opcode | 2) == 0x87))
4657     return 1;
4658 
4659   /* xadd.  */
4660   if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4661       && base_opcode == 0xc1)
4662     return 1;
4663 
4664   /* Check for load instruction.  */
4665   return (i.types[dest].bitfield.class != ClassNone
4666 	  || i.types[dest].bitfield.instance == Accum);
4667 }
4668 
4669 /* Output lfence, 0xfaee8, after instruction.  */
4670 
4671 static void
insert_lfence_after(void)4672 insert_lfence_after (void)
4673 {
4674   if (lfence_after_load && load_insn_p ())
4675     {
4676       /* There are also two REP string instructions that require
4677 	 special treatment. Specifically, the compare string (CMPS)
4678 	 and scan string (SCAS) instructions set EFLAGS in a manner
4679 	 that depends on the data being compared/scanned. When used
4680 	 with a REP prefix, the number of iterations may therefore
4681 	 vary depending on this data. If the data is a program secret
4682 	 chosen by the adversary using an LVI method,
4683 	 then this data-dependent behavior may leak some aspect
4684 	 of the secret.  */
4685       if (((i.tm.base_opcode | 0x1) == 0xa7
4686 	   || (i.tm.base_opcode | 0x1) == 0xaf)
4687 	  && i.prefix[REP_PREFIX])
4688 	{
4689 	    as_warn (_("`%s` changes flags which would affect control flow behavior"),
4690 		     i.tm.name);
4691 	}
4692       char *p = frag_more (3);
4693       *p++ = 0xf;
4694       *p++ = 0xae;
4695       *p = 0xe8;
4696     }
4697 }
4698 
4699 /* Output lfence, 0xfaee8, before instruction.  */
4700 
4701 static void
insert_lfence_before(void)4702 insert_lfence_before (void)
4703 {
4704   char *p;
4705 
4706   if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
4707     return;
4708 
4709   if (i.tm.base_opcode == 0xff
4710       && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4711     {
4712       /* Insert lfence before indirect branch if needed.  */
4713 
4714       if (lfence_before_indirect_branch == lfence_branch_none)
4715 	return;
4716 
4717       if (i.operands != 1)
4718 	abort ();
4719 
4720       if (i.reg_operands == 1)
4721 	{
4722 	  /* Indirect branch via register.  Don't insert lfence with
4723 	     -mlfence-after-load=yes.  */
4724 	  if (lfence_after_load
4725 	      || lfence_before_indirect_branch == lfence_branch_memory)
4726 	    return;
4727 	}
4728       else if (i.mem_operands == 1
4729 	       && lfence_before_indirect_branch != lfence_branch_register)
4730 	{
4731 	  as_warn (_("indirect `%s` with memory operand should be avoided"),
4732 		   i.tm.name);
4733 	  return;
4734 	}
4735       else
4736 	return;
4737 
4738       if (last_insn.kind != last_insn_other
4739 	  && last_insn.seg == now_seg)
4740 	{
4741 	  as_warn_where (last_insn.file, last_insn.line,
4742 			 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4743 			 last_insn.name, i.tm.name);
4744 	  return;
4745 	}
4746 
4747       p = frag_more (3);
4748       *p++ = 0xf;
4749       *p++ = 0xae;
4750       *p = 0xe8;
4751       return;
4752     }
4753 
4754   /* Output or/not/shl and lfence before near ret.  */
4755   if (lfence_before_ret != lfence_before_ret_none
4756       && (i.tm.base_opcode == 0xc2
4757 	  || i.tm.base_opcode == 0xc3))
4758     {
4759       if (last_insn.kind != last_insn_other
4760 	  && last_insn.seg == now_seg)
4761 	{
4762 	  as_warn_where (last_insn.file, last_insn.line,
4763 			 _("`%s` skips -mlfence-before-ret on `%s`"),
4764 			 last_insn.name, i.tm.name);
4765 	  return;
4766 	}
4767 
4768       /* Near ret ingore operand size override under CPU64.  */
4769       char prefix = flag_code == CODE_64BIT
4770 		    ? 0x48
4771 		    : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
4772 
4773       if (lfence_before_ret == lfence_before_ret_not)
4774 	{
4775 	  /* not: 0xf71424, may add prefix
4776 	     for operand size override or 64-bit code.  */
4777 	  p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4778 	  if (prefix)
4779 	    *p++ = prefix;
4780 	  *p++ = 0xf7;
4781 	  *p++ = 0x14;
4782 	  *p++ = 0x24;
4783 	  if (prefix)
4784 	    *p++ = prefix;
4785 	  *p++ = 0xf7;
4786 	  *p++ = 0x14;
4787 	  *p++ = 0x24;
4788 	}
4789       else
4790 	{
4791 	  p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4792 	  if (prefix)
4793 	    *p++ = prefix;
4794 	  if (lfence_before_ret == lfence_before_ret_or)
4795 	    {
4796 	      /* or: 0x830c2400, may add prefix
4797 		 for operand size override or 64-bit code.  */
4798 	      *p++ = 0x83;
4799 	      *p++ = 0x0c;
4800 	    }
4801 	  else
4802 	    {
4803 	      /* shl: 0xc1242400, may add prefix
4804 		 for operand size override or 64-bit code.  */
4805 	      *p++ = 0xc1;
4806 	      *p++ = 0x24;
4807 	    }
4808 
4809 	  *p++ = 0x24;
4810 	  *p++ = 0x0;
4811 	}
4812 
4813       *p++ = 0xf;
4814       *p++ = 0xae;
4815       *p = 0xe8;
4816     }
4817 }
4818 
4819 /* This is the guts of the machine-dependent assembler.  LINE points to a
4820    machine dependent instruction.  This function is supposed to emit
4821    the frags/bytes it assembles to.  */
4822 
4823 void
md_assemble(char * line)4824 md_assemble (char *line)
4825 {
4826   unsigned int j;
4827   char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
4828   const insn_template *t;
4829 
4830   /* Initialize globals.  */
4831   memset (&i, '\0', sizeof (i));
4832   i.rounding.type = rc_none;
4833   for (j = 0; j < MAX_OPERANDS; j++)
4834     i.reloc[j] = NO_RELOC;
4835   memset (disp_expressions, '\0', sizeof (disp_expressions));
4836   memset (im_expressions, '\0', sizeof (im_expressions));
4837   save_stack_p = save_stack;
4838 
4839   /* First parse an instruction mnemonic & call i386_operand for the operands.
4840      We assume that the scrubber has arranged it so that line[0] is the valid
4841      start of a (possibly prefixed) mnemonic.  */
4842 
4843   line = parse_insn (line, mnemonic);
4844   if (line == NULL)
4845     return;
4846   mnem_suffix = i.suffix;
4847 
4848   line = parse_operands (line, mnemonic);
4849   this_operand = -1;
4850   xfree (i.memop1_string);
4851   i.memop1_string = NULL;
4852   if (line == NULL)
4853     return;
4854 
4855   /* Now we've parsed the mnemonic into a set of templates, and have the
4856      operands at hand.  */
4857 
4858   /* All Intel opcodes have reversed operands except for "bound", "enter",
4859      "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4860      "rmpadjust", and "rmpupdate".  We also don't reverse intersegment "jmp"
4861      and "call" instructions with 2 immediate operands so that the immediate
4862      segment precedes the offset consistently in Intel and AT&T modes.  */
4863   if (intel_syntax
4864       && i.operands > 1
4865       && (strcmp (mnemonic, "bound") != 0)
4866       && (strncmp (mnemonic, "invlpg", 6) != 0)
4867       && !startswith (mnemonic, "monitor")
4868       && !startswith (mnemonic, "mwait")
4869       && (strcmp (mnemonic, "pvalidate") != 0)
4870       && !startswith (mnemonic, "rmp")
4871       && (strcmp (mnemonic, "tpause") != 0)
4872       && (strcmp (mnemonic, "umwait") != 0)
4873       && !(operand_type_check (i.types[0], imm)
4874 	   && operand_type_check (i.types[1], imm)))
4875     swap_operands ();
4876 
4877   /* The order of the immediates should be reversed
4878      for 2 immediates extrq and insertq instructions */
4879   if (i.imm_operands == 2
4880       && (strcmp (mnemonic, "extrq") == 0
4881 	  || strcmp (mnemonic, "insertq") == 0))
4882       swap_2_operands (0, 1);
4883 
4884   if (i.imm_operands)
4885     optimize_imm ();
4886 
4887   if (i.disp_operands && !want_disp32 (current_templates->start)
4888       && (!current_templates->start->opcode_modifier.jump
4889 	  || i.jumpabsolute || i.types[0].bitfield.baseindex))
4890     {
4891       for (j = 0; j < i.operands; ++j)
4892 	{
4893 	  const expressionS *exp = i.op[j].disps;
4894 
4895 	  if (!operand_type_check (i.types[j], disp))
4896 	    continue;
4897 
4898 	  if (exp->X_op != O_constant)
4899 	    continue;
4900 
4901 	  /* Since displacement is signed extended to 64bit, don't allow
4902 	     disp32 if it is out of range.  */
4903 	  if (fits_in_signed_long (exp->X_add_number))
4904 	    continue;
4905 
4906 	  i.types[j].bitfield.disp32 = 0;
4907 	  if (i.types[j].bitfield.baseindex)
4908 	    {
4909 	      char number_buf[128];
4910 
4911 	      /* Coded this way in order to allow for ease of translation.  */
4912 	      sprintf_vma (number_buf, exp->X_add_number);
4913 	      as_bad (_("0x%s out of range of signed 32bit displacement"),
4914 		      number_buf);
4915 	      return;
4916 	    }
4917 	}
4918     }
4919 
4920   /* Don't optimize displacement for movabs since it only takes 64bit
4921      displacement.  */
4922   if (i.disp_operands
4923       && i.disp_encoding <= disp_encoding_8bit
4924       && (flag_code != CODE_64BIT
4925 	  || strcmp (mnemonic, "movabs") != 0))
4926     optimize_disp ();
4927 
4928   /* Next, we find a template that matches the given insn,
4929      making sure the overlap of the given operands types is consistent
4930      with the template operand types.  */
4931 
4932   if (!(t = match_template (mnem_suffix)))
4933     return;
4934 
4935   if (sse_check != check_none
4936       /* The opcode space check isn't strictly needed; it's there only to
4937 	 bypass the logic below when easily possible.  */
4938       && t->opcode_modifier.opcodespace >= SPACE_0F
4939       && t->opcode_modifier.opcodespace <= SPACE_0F3A
4940       && !i.tm.cpu_flags.bitfield.cpusse4a
4941       && !is_any_vex_encoding (t))
4942     {
4943       bool simd = false;
4944 
4945       for (j = 0; j < t->operands; ++j)
4946 	{
4947 	  if (t->operand_types[j].bitfield.class == RegMMX)
4948 	    break;
4949 	  if (t->operand_types[j].bitfield.class == RegSIMD)
4950 	    simd = true;
4951 	}
4952 
4953       if (j >= t->operands && simd)
4954 	(sse_check == check_warning
4955 	 ? as_warn
4956 	 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4957     }
4958 
4959   if (i.tm.opcode_modifier.fwait)
4960     if (!add_prefix (FWAIT_OPCODE))
4961       return;
4962 
4963   /* Check if REP prefix is OK.  */
4964   if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
4965     {
4966       as_bad (_("invalid instruction `%s' after `%s'"),
4967 		i.tm.name, i.rep_prefix);
4968       return;
4969     }
4970 
4971   /* Check for lock without a lockable instruction.  Destination operand
4972      must be memory unless it is xchg (0x86).  */
4973   if (i.prefix[LOCK_PREFIX]
4974       && (i.tm.opcode_modifier.prefixok < PrefixLock
4975 	  || i.mem_operands == 0
4976 	  || (i.tm.base_opcode != 0x86
4977 	      && !(i.flags[i.operands - 1] & Operand_Mem))))
4978     {
4979       as_bad (_("expecting lockable instruction after `lock'"));
4980       return;
4981     }
4982 
4983   /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns.  */
4984   if (i.prefix[DATA_PREFIX]
4985       && (is_any_vex_encoding (&i.tm)
4986 	  || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4987 	  || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
4988     {
4989       as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4990       return;
4991     }
4992 
4993   /* Check if HLE prefix is OK.  */
4994   if (i.hle_prefix && !check_hle ())
4995     return;
4996 
4997   /* Check BND prefix.  */
4998   if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4999     as_bad (_("expecting valid branch instruction after `bnd'"));
5000 
5001   /* Check NOTRACK prefix.  */
5002   if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5003     as_bad (_("expecting indirect branch instruction after `notrack'"));
5004 
5005   if (i.tm.cpu_flags.bitfield.cpumpx)
5006     {
5007       if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5008 	as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5009       else if (flag_code != CODE_16BIT
5010 	       ? i.prefix[ADDR_PREFIX]
5011 	       : i.mem_operands && !i.prefix[ADDR_PREFIX])
5012 	as_bad (_("16-bit address isn't allowed in MPX instructions"));
5013     }
5014 
5015   /* Insert BND prefix.  */
5016   if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5017     {
5018       if (!i.prefix[BND_PREFIX])
5019 	add_prefix (BND_PREFIX_OPCODE);
5020       else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5021 	{
5022 	  as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5023 	  i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5024 	}
5025     }
5026 
5027   /* Check string instruction segment overrides.  */
5028   if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5029     {
5030       gas_assert (i.mem_operands);
5031       if (!check_string ())
5032 	return;
5033       i.disp_operands = 0;
5034     }
5035 
5036   if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5037     optimize_encoding ();
5038 
5039   if (use_unaligned_vector_move)
5040     encode_with_unaligned_vector_move ();
5041 
5042   if (!process_suffix ())
5043     return;
5044 
5045   /* Update operand types and check extended states.  */
5046   for (j = 0; j < i.operands; j++)
5047     {
5048       i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5049       switch (i.tm.operand_types[j].bitfield.class)
5050 	{
5051 	default:
5052 	  break;
5053 	case RegMMX:
5054 	  i.xstate |= xstate_mmx;
5055 	  break;
5056 	case RegMask:
5057 	  i.xstate |= xstate_mask;
5058 	  break;
5059 	case RegSIMD:
5060 	  if (i.tm.operand_types[j].bitfield.tmmword)
5061 	    i.xstate |= xstate_tmm;
5062 	  else if (i.tm.operand_types[j].bitfield.zmmword)
5063 	    i.xstate |= xstate_zmm;
5064 	  else if (i.tm.operand_types[j].bitfield.ymmword)
5065 	    i.xstate |= xstate_ymm;
5066 	  else if (i.tm.operand_types[j].bitfield.xmmword)
5067 	    i.xstate |= xstate_xmm;
5068 	  break;
5069 	}
5070     }
5071 
5072   /* Make still unresolved immediate matches conform to size of immediate
5073      given in i.suffix.  */
5074   if (!finalize_imm ())
5075     return;
5076 
5077   if (i.types[0].bitfield.imm1)
5078     i.imm_operands = 0;	/* kludge for shift insns.  */
5079 
5080   /* We only need to check those implicit registers for instructions
5081      with 3 operands or less.  */
5082   if (i.operands <= 3)
5083     for (j = 0; j < i.operands; j++)
5084       if (i.types[j].bitfield.instance != InstanceNone
5085 	  && !i.types[j].bitfield.xmmword)
5086 	i.reg_operands--;
5087 
5088   /* For insns with operands there are more diddles to do to the opcode.  */
5089   if (i.operands)
5090     {
5091       if (!process_operands ())
5092 	return;
5093     }
5094   else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
5095     {
5096       /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
5097       as_warn (_("translating to `%sp'"), i.tm.name);
5098     }
5099 
5100   if (is_any_vex_encoding (&i.tm))
5101     {
5102       if (!cpu_arch_flags.bitfield.cpui286)
5103 	{
5104 	  as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5105 		  i.tm.name);
5106 	  return;
5107 	}
5108 
5109       /* Check for explicit REX prefix.  */
5110       if (i.prefix[REX_PREFIX] || i.rex_encoding)
5111 	{
5112 	  as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5113 	  return;
5114 	}
5115 
5116       if (i.tm.opcode_modifier.vex)
5117 	build_vex_prefix (t);
5118       else
5119 	build_evex_prefix ();
5120 
5121       /* The individual REX.RXBW bits got consumed.  */
5122       i.rex &= REX_OPCODE;
5123     }
5124 
5125   /* Handle conversion of 'int $3' --> special int3 insn.  XOP or FMA4
5126      instructions may define INT_OPCODE as well, so avoid this corner
5127      case for those instructions that use MODRM.  */
5128   if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5129       && i.tm.base_opcode == INT_OPCODE
5130       && !i.tm.opcode_modifier.modrm
5131       && i.op[0].imms->X_add_number == 3)
5132     {
5133       i.tm.base_opcode = INT3_OPCODE;
5134       i.imm_operands = 0;
5135     }
5136 
5137   if ((i.tm.opcode_modifier.jump == JUMP
5138        || i.tm.opcode_modifier.jump == JUMP_BYTE
5139        || i.tm.opcode_modifier.jump == JUMP_DWORD)
5140       && i.op[0].disps->X_op == O_constant)
5141     {
5142       /* Convert "jmp constant" (and "call constant") to a jump (call) to
5143 	 the absolute address given by the constant.  Since ix86 jumps and
5144 	 calls are pc relative, we need to generate a reloc.  */
5145       i.op[0].disps->X_add_symbol = &abs_symbol;
5146       i.op[0].disps->X_op = O_symbol;
5147     }
5148 
5149   /* For 8 bit registers we need an empty rex prefix.  Also if the
5150      instruction already has a prefix, we need to convert old
5151      registers to new ones.  */
5152 
5153   if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5154        && (i.op[0].regs->reg_flags & RegRex64) != 0)
5155       || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5156 	  && (i.op[1].regs->reg_flags & RegRex64) != 0)
5157       || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5158 	   || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5159 	  && i.rex != 0))
5160     {
5161       int x;
5162 
5163       i.rex |= REX_OPCODE;
5164       for (x = 0; x < 2; x++)
5165 	{
5166 	  /* Look for 8 bit operand that uses old registers.  */
5167 	  if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5168 	      && (i.op[x].regs->reg_flags & RegRex64) == 0)
5169 	    {
5170 	      gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5171 	      /* In case it is "hi" register, give up.  */
5172 	      if (i.op[x].regs->reg_num > 3)
5173 		as_bad (_("can't encode register '%s%s' in an "
5174 			  "instruction requiring REX prefix."),
5175 			register_prefix, i.op[x].regs->reg_name);
5176 
5177 	      /* Otherwise it is equivalent to the extended register.
5178 		 Since the encoding doesn't change this is merely
5179 		 cosmetic cleanup for debug output.  */
5180 
5181 	      i.op[x].regs = i.op[x].regs + 8;
5182 	    }
5183 	}
5184     }
5185 
5186   if (i.rex == 0 && i.rex_encoding)
5187     {
5188       /* Check if we can add a REX_OPCODE byte.  Look for 8 bit operand
5189 	 that uses legacy register.  If it is "hi" register, don't add
5190 	 the REX_OPCODE byte.  */
5191       int x;
5192       for (x = 0; x < 2; x++)
5193 	if (i.types[x].bitfield.class == Reg
5194 	    && i.types[x].bitfield.byte
5195 	    && (i.op[x].regs->reg_flags & RegRex64) == 0
5196 	    && i.op[x].regs->reg_num > 3)
5197 	  {
5198 	    gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5199 	    i.rex_encoding = false;
5200 	    break;
5201 	  }
5202 
5203       if (i.rex_encoding)
5204 	i.rex = REX_OPCODE;
5205     }
5206 
5207   if (i.rex != 0)
5208     add_prefix (REX_OPCODE | i.rex);
5209 
5210   insert_lfence_before ();
5211 
5212   /* We are ready to output the insn.  */
5213   output_insn ();
5214 
5215   insert_lfence_after ();
5216 
5217   last_insn.seg = now_seg;
5218 
5219   if (i.tm.opcode_modifier.isprefix)
5220     {
5221       last_insn.kind = last_insn_prefix;
5222       last_insn.name = i.tm.name;
5223       last_insn.file = as_where (&last_insn.line);
5224     }
5225   else
5226     last_insn.kind = last_insn_other;
5227 }
5228 
5229 static char *
parse_insn(char * line,char * mnemonic)5230 parse_insn (char *line, char *mnemonic)
5231 {
5232   char *l = line;
5233   char *token_start = l;
5234   char *mnem_p;
5235   int supported;
5236   const insn_template *t;
5237   char *dot_p = NULL;
5238 
5239   while (1)
5240     {
5241       mnem_p = mnemonic;
5242       while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5243 	{
5244 	  if (*mnem_p == '.')
5245 	    dot_p = mnem_p;
5246 	  mnem_p++;
5247 	  if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5248 	    {
5249 	      as_bad (_("no such instruction: `%s'"), token_start);
5250 	      return NULL;
5251 	    }
5252 	  l++;
5253 	}
5254       if (!is_space_char (*l)
5255 	  && *l != END_OF_INSN
5256 	  && (intel_syntax
5257 	      || (*l != PREFIX_SEPARATOR
5258 		  && *l != ',')))
5259 	{
5260 	  as_bad (_("invalid character %s in mnemonic"),
5261 		  output_invalid (*l));
5262 	  return NULL;
5263 	}
5264       if (token_start == l)
5265 	{
5266 	  if (!intel_syntax && *l == PREFIX_SEPARATOR)
5267 	    as_bad (_("expecting prefix; got nothing"));
5268 	  else
5269 	    as_bad (_("expecting mnemonic; got nothing"));
5270 	  return NULL;
5271 	}
5272 
5273       /* Look up instruction (or prefix) via hash table.  */
5274       current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5275 
5276       if (*l != END_OF_INSN
5277 	  && (!is_space_char (*l) || l[1] != END_OF_INSN)
5278 	  && current_templates
5279 	  && current_templates->start->opcode_modifier.isprefix)
5280 	{
5281 	  if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
5282 	    {
5283 	      as_bad ((flag_code != CODE_64BIT
5284 		       ? _("`%s' is only supported in 64-bit mode")
5285 		       : _("`%s' is not supported in 64-bit mode")),
5286 		      current_templates->start->name);
5287 	      return NULL;
5288 	    }
5289 	  /* If we are in 16-bit mode, do not allow addr16 or data16.
5290 	     Similarly, in 32-bit mode, do not allow addr32 or data32.  */
5291 	  if ((current_templates->start->opcode_modifier.size == SIZE16
5292 	       || current_templates->start->opcode_modifier.size == SIZE32)
5293 	      && flag_code != CODE_64BIT
5294 	      && ((current_templates->start->opcode_modifier.size == SIZE32)
5295 		  ^ (flag_code == CODE_16BIT)))
5296 	    {
5297 	      as_bad (_("redundant %s prefix"),
5298 		      current_templates->start->name);
5299 	      return NULL;
5300 	    }
5301 
5302 	  if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5303 	    {
5304 	      /* Handle pseudo prefixes.  */
5305 	      switch (current_templates->start->extension_opcode)
5306 		{
5307 		case Prefix_Disp8:
5308 		  /* {disp8} */
5309 		  i.disp_encoding = disp_encoding_8bit;
5310 		  break;
5311 		case Prefix_Disp16:
5312 		  /* {disp16} */
5313 		  i.disp_encoding = disp_encoding_16bit;
5314 		  break;
5315 		case Prefix_Disp32:
5316 		  /* {disp32} */
5317 		  i.disp_encoding = disp_encoding_32bit;
5318 		  break;
5319 		case Prefix_Load:
5320 		  /* {load} */
5321 		  i.dir_encoding = dir_encoding_load;
5322 		  break;
5323 		case Prefix_Store:
5324 		  /* {store} */
5325 		  i.dir_encoding = dir_encoding_store;
5326 		  break;
5327 		case Prefix_VEX:
5328 		  /* {vex} */
5329 		  i.vec_encoding = vex_encoding_vex;
5330 		  break;
5331 		case Prefix_VEX3:
5332 		  /* {vex3} */
5333 		  i.vec_encoding = vex_encoding_vex3;
5334 		  break;
5335 		case Prefix_EVEX:
5336 		  /* {evex} */
5337 		  i.vec_encoding = vex_encoding_evex;
5338 		  break;
5339 		case Prefix_REX:
5340 		  /* {rex} */
5341 		  i.rex_encoding = true;
5342 		  break;
5343 		case Prefix_NoOptimize:
5344 		  /* {nooptimize} */
5345 		  i.no_optimize = true;
5346 		  break;
5347 		default:
5348 		  abort ();
5349 		}
5350 	    }
5351 	  else
5352 	    {
5353 	      /* Add prefix, checking for repeated prefixes.  */
5354 	      switch (add_prefix (current_templates->start->base_opcode))
5355 		{
5356 		case PREFIX_EXIST:
5357 		  return NULL;
5358 		case PREFIX_DS:
5359 		  if (current_templates->start->cpu_flags.bitfield.cpuibt)
5360 		    i.notrack_prefix = current_templates->start->name;
5361 		  break;
5362 		case PREFIX_REP:
5363 		  if (current_templates->start->cpu_flags.bitfield.cpuhle)
5364 		    i.hle_prefix = current_templates->start->name;
5365 		  else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5366 		    i.bnd_prefix = current_templates->start->name;
5367 		  else
5368 		    i.rep_prefix = current_templates->start->name;
5369 		  break;
5370 		default:
5371 		  break;
5372 		}
5373 	    }
5374 	  /* Skip past PREFIX_SEPARATOR and reset token_start.  */
5375 	  token_start = ++l;
5376 	}
5377       else
5378 	break;
5379     }
5380 
5381   if (!current_templates)
5382     {
5383       /* Deprecated functionality (new code should use pseudo-prefixes instead):
5384 	 Check if we should swap operand or force 32bit displacement in
5385 	 encoding.  */
5386       if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5387 	i.dir_encoding = dir_encoding_swap;
5388       else if (mnem_p - 3 == dot_p
5389 	       && dot_p[1] == 'd'
5390 	       && dot_p[2] == '8')
5391 	i.disp_encoding = disp_encoding_8bit;
5392       else if (mnem_p - 4 == dot_p
5393 	       && dot_p[1] == 'd'
5394 	       && dot_p[2] == '3'
5395 	       && dot_p[3] == '2')
5396 	i.disp_encoding = disp_encoding_32bit;
5397       else
5398 	goto check_suffix;
5399       mnem_p = dot_p;
5400       *dot_p = '\0';
5401       current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5402     }
5403 
5404   if (!current_templates)
5405     {
5406     check_suffix:
5407       if (mnem_p > mnemonic)
5408 	{
5409 	  /* See if we can get a match by trimming off a suffix.  */
5410 	  switch (mnem_p[-1])
5411 	    {
5412 	    case WORD_MNEM_SUFFIX:
5413 	      if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5414 		i.suffix = SHORT_MNEM_SUFFIX;
5415 	      else
5416 		/* Fall through.  */
5417 	      case BYTE_MNEM_SUFFIX:
5418 	      case QWORD_MNEM_SUFFIX:
5419 		i.suffix = mnem_p[-1];
5420 	      mnem_p[-1] = '\0';
5421 	      current_templates
5422 		= (const templates *) str_hash_find (op_hash, mnemonic);
5423 	      break;
5424 	    case SHORT_MNEM_SUFFIX:
5425 	    case LONG_MNEM_SUFFIX:
5426 	      if (!intel_syntax)
5427 		{
5428 		  i.suffix = mnem_p[-1];
5429 		  mnem_p[-1] = '\0';
5430 		  current_templates
5431 		    = (const templates *) str_hash_find (op_hash, mnemonic);
5432 		}
5433 	      break;
5434 
5435 	      /* Intel Syntax.  */
5436 	    case 'd':
5437 	      if (intel_syntax)
5438 		{
5439 		  if (intel_float_operand (mnemonic) == 1)
5440 		    i.suffix = SHORT_MNEM_SUFFIX;
5441 		  else
5442 		    i.suffix = LONG_MNEM_SUFFIX;
5443 		  mnem_p[-1] = '\0';
5444 		  current_templates
5445 		    = (const templates *) str_hash_find (op_hash, mnemonic);
5446 		}
5447 	      break;
5448 	    }
5449 	}
5450 
5451       if (!current_templates)
5452 	{
5453 	  as_bad (_("no such instruction: `%s'"), token_start);
5454 	  return NULL;
5455 	}
5456     }
5457 
5458   if (current_templates->start->opcode_modifier.jump == JUMP
5459       || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5460     {
5461       /* Check for a branch hint.  We allow ",pt" and ",pn" for
5462 	 predict taken and predict not taken respectively.
5463 	 I'm not sure that branch hints actually do anything on loop
5464 	 and jcxz insns (JumpByte) for current Pentium4 chips.  They
5465 	 may work in the future and it doesn't hurt to accept them
5466 	 now.  */
5467       if (l[0] == ',' && l[1] == 'p')
5468 	{
5469 	  if (l[2] == 't')
5470 	    {
5471 	      if (!add_prefix (DS_PREFIX_OPCODE))
5472 		return NULL;
5473 	      l += 3;
5474 	    }
5475 	  else if (l[2] == 'n')
5476 	    {
5477 	      if (!add_prefix (CS_PREFIX_OPCODE))
5478 		return NULL;
5479 	      l += 3;
5480 	    }
5481 	}
5482     }
5483   /* Any other comma loses.  */
5484   if (*l == ',')
5485     {
5486       as_bad (_("invalid character %s in mnemonic"),
5487 	      output_invalid (*l));
5488       return NULL;
5489     }
5490 
5491   /* Check if instruction is supported on specified architecture.  */
5492   supported = 0;
5493   for (t = current_templates->start; t < current_templates->end; ++t)
5494     {
5495       supported |= cpu_flags_match (t);
5496       if (supported == CPU_FLAGS_PERFECT_MATCH)
5497 	{
5498 	  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5499 	    as_warn (_("use .code16 to ensure correct addressing mode"));
5500 
5501 	  return l;
5502 	}
5503     }
5504 
5505   if (!(supported & CPU_FLAGS_64BIT_MATCH))
5506     as_bad (flag_code == CODE_64BIT
5507 	    ? _("`%s' is not supported in 64-bit mode")
5508 	    : _("`%s' is only supported in 64-bit mode"),
5509 	    current_templates->start->name);
5510   else
5511     as_bad (_("`%s' is not supported on `%s%s'"),
5512 	    current_templates->start->name,
5513 	    cpu_arch_name ? cpu_arch_name : default_arch,
5514 	    cpu_sub_arch_name ? cpu_sub_arch_name : "");
5515 
5516   return NULL;
5517 }
5518 
5519 static char *
parse_operands(char * l,const char * mnemonic)5520 parse_operands (char *l, const char *mnemonic)
5521 {
5522   char *token_start;
5523 
5524   /* 1 if operand is pending after ','.  */
5525   unsigned int expecting_operand = 0;
5526 
5527   while (*l != END_OF_INSN)
5528     {
5529       /* Non-zero if operand parens not balanced.  */
5530       unsigned int paren_not_balanced = 0;
5531       /* True if inside double quotes.  */
5532       bool in_quotes = false;
5533 
5534       /* Skip optional white space before operand.  */
5535       if (is_space_char (*l))
5536 	++l;
5537       if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
5538 	{
5539 	  as_bad (_("invalid character %s before operand %d"),
5540 		  output_invalid (*l),
5541 		  i.operands + 1);
5542 	  return NULL;
5543 	}
5544       token_start = l;	/* After white space.  */
5545       while (in_quotes || paren_not_balanced || *l != ',')
5546 	{
5547 	  if (*l == END_OF_INSN)
5548 	    {
5549 	      if (in_quotes)
5550 		{
5551 		  as_bad (_("unbalanced double quotes in operand %d."),
5552 			  i.operands + 1);
5553 		  return NULL;
5554 		}
5555 	      if (paren_not_balanced)
5556 		{
5557 		  know (!intel_syntax);
5558 		  as_bad (_("unbalanced parenthesis in operand %d."),
5559 			  i.operands + 1);
5560 		  return NULL;
5561 		}
5562 	      else
5563 		break;	/* we are done */
5564 	    }
5565 	  else if (*l == '\\' && l[1] == '"')
5566 	    ++l;
5567 	  else if (*l == '"')
5568 	    in_quotes = !in_quotes;
5569 	  else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
5570 	    {
5571 	      as_bad (_("invalid character %s in operand %d"),
5572 		      output_invalid (*l),
5573 		      i.operands + 1);
5574 	      return NULL;
5575 	    }
5576 	  if (!intel_syntax && !in_quotes)
5577 	    {
5578 	      if (*l == '(')
5579 		++paren_not_balanced;
5580 	      if (*l == ')')
5581 		--paren_not_balanced;
5582 	    }
5583 	  l++;
5584 	}
5585       if (l != token_start)
5586 	{			/* Yes, we've read in another operand.  */
5587 	  unsigned int operand_ok;
5588 	  this_operand = i.operands++;
5589 	  if (i.operands > MAX_OPERANDS)
5590 	    {
5591 	      as_bad (_("spurious operands; (%d operands/instruction max)"),
5592 		      MAX_OPERANDS);
5593 	      return NULL;
5594 	    }
5595 	  i.types[this_operand].bitfield.unspecified = 1;
5596 	  /* Now parse operand adding info to 'i' as we go along.  */
5597 	  END_STRING_AND_SAVE (l);
5598 
5599 	  if (i.mem_operands > 1)
5600 	    {
5601 	      as_bad (_("too many memory references for `%s'"),
5602 		      mnemonic);
5603 	      return 0;
5604 	    }
5605 
5606 	  if (intel_syntax)
5607 	    operand_ok =
5608 	      i386_intel_operand (token_start,
5609 				  intel_float_operand (mnemonic));
5610 	  else
5611 	    operand_ok = i386_att_operand (token_start);
5612 
5613 	  RESTORE_END_STRING (l);
5614 	  if (!operand_ok)
5615 	    return NULL;
5616 	}
5617       else
5618 	{
5619 	  if (expecting_operand)
5620 	    {
5621 	    expecting_operand_after_comma:
5622 	      as_bad (_("expecting operand after ','; got nothing"));
5623 	      return NULL;
5624 	    }
5625 	  if (*l == ',')
5626 	    {
5627 	      as_bad (_("expecting operand before ','; got nothing"));
5628 	      return NULL;
5629 	    }
5630 	}
5631 
5632       /* Now *l must be either ',' or END_OF_INSN.  */
5633       if (*l == ',')
5634 	{
5635 	  if (*++l == END_OF_INSN)
5636 	    {
5637 	      /* Just skip it, if it's \n complain.  */
5638 	      goto expecting_operand_after_comma;
5639 	    }
5640 	  expecting_operand = 1;
5641 	}
5642     }
5643   return l;
5644 }
5645 
5646 static void
swap_2_operands(unsigned int xchg1,unsigned int xchg2)5647 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
5648 {
5649   union i386_op temp_op;
5650   i386_operand_type temp_type;
5651   unsigned int temp_flags;
5652   enum bfd_reloc_code_real temp_reloc;
5653 
5654   temp_type = i.types[xchg2];
5655   i.types[xchg2] = i.types[xchg1];
5656   i.types[xchg1] = temp_type;
5657 
5658   temp_flags = i.flags[xchg2];
5659   i.flags[xchg2] = i.flags[xchg1];
5660   i.flags[xchg1] = temp_flags;
5661 
5662   temp_op = i.op[xchg2];
5663   i.op[xchg2] = i.op[xchg1];
5664   i.op[xchg1] = temp_op;
5665 
5666   temp_reloc = i.reloc[xchg2];
5667   i.reloc[xchg2] = i.reloc[xchg1];
5668   i.reloc[xchg1] = temp_reloc;
5669 
5670   if (i.mask.reg)
5671     {
5672       if (i.mask.operand == xchg1)
5673 	i.mask.operand = xchg2;
5674       else if (i.mask.operand == xchg2)
5675 	i.mask.operand = xchg1;
5676     }
5677   if (i.broadcast.type || i.broadcast.bytes)
5678     {
5679       if (i.broadcast.operand == xchg1)
5680 	i.broadcast.operand = xchg2;
5681       else if (i.broadcast.operand == xchg2)
5682 	i.broadcast.operand = xchg1;
5683     }
5684 }
5685 
5686 static void
swap_operands(void)5687 swap_operands (void)
5688 {
5689   switch (i.operands)
5690     {
5691     case 5:
5692     case 4:
5693       swap_2_operands (1, i.operands - 2);
5694       /* Fall through.  */
5695     case 3:
5696     case 2:
5697       swap_2_operands (0, i.operands - 1);
5698       break;
5699     default:
5700       abort ();
5701     }
5702 
5703   if (i.mem_operands == 2)
5704     {
5705       const reg_entry *temp_seg;
5706       temp_seg = i.seg[0];
5707       i.seg[0] = i.seg[1];
5708       i.seg[1] = temp_seg;
5709     }
5710 }
5711 
5712 /* Try to ensure constant immediates are represented in the smallest
5713    opcode possible.  */
5714 static void
optimize_imm(void)5715 optimize_imm (void)
5716 {
5717   char guess_suffix = 0;
5718   int op;
5719 
5720   if (i.suffix)
5721     guess_suffix = i.suffix;
5722   else if (i.reg_operands)
5723     {
5724       /* Figure out a suffix from the last register operand specified.
5725 	 We can't do this properly yet, i.e. excluding special register
5726 	 instances, but the following works for instructions with
5727 	 immediates.  In any case, we can't set i.suffix yet.  */
5728       for (op = i.operands; --op >= 0;)
5729 	if (i.types[op].bitfield.class != Reg)
5730 	  continue;
5731 	else if (i.types[op].bitfield.byte)
5732 	  {
5733 	    guess_suffix = BYTE_MNEM_SUFFIX;
5734 	    break;
5735 	  }
5736 	else if (i.types[op].bitfield.word)
5737 	  {
5738 	    guess_suffix = WORD_MNEM_SUFFIX;
5739 	    break;
5740 	  }
5741 	else if (i.types[op].bitfield.dword)
5742 	  {
5743 	    guess_suffix = LONG_MNEM_SUFFIX;
5744 	    break;
5745 	  }
5746 	else if (i.types[op].bitfield.qword)
5747 	  {
5748 	    guess_suffix = QWORD_MNEM_SUFFIX;
5749 	    break;
5750 	  }
5751     }
5752   else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5753     guess_suffix = WORD_MNEM_SUFFIX;
5754 
5755   for (op = i.operands; --op >= 0;)
5756     if (operand_type_check (i.types[op], imm))
5757       {
5758 	switch (i.op[op].imms->X_op)
5759 	  {
5760 	  case O_constant:
5761 	    /* If a suffix is given, this operand may be shortened.  */
5762 	    switch (guess_suffix)
5763 	      {
5764 	      case LONG_MNEM_SUFFIX:
5765 		i.types[op].bitfield.imm32 = 1;
5766 		i.types[op].bitfield.imm64 = 1;
5767 		break;
5768 	      case WORD_MNEM_SUFFIX:
5769 		i.types[op].bitfield.imm16 = 1;
5770 		i.types[op].bitfield.imm32 = 1;
5771 		i.types[op].bitfield.imm32s = 1;
5772 		i.types[op].bitfield.imm64 = 1;
5773 		break;
5774 	      case BYTE_MNEM_SUFFIX:
5775 		i.types[op].bitfield.imm8 = 1;
5776 		i.types[op].bitfield.imm8s = 1;
5777 		i.types[op].bitfield.imm16 = 1;
5778 		i.types[op].bitfield.imm32 = 1;
5779 		i.types[op].bitfield.imm32s = 1;
5780 		i.types[op].bitfield.imm64 = 1;
5781 		break;
5782 	      }
5783 
5784 	    /* If this operand is at most 16 bits, convert it
5785 	       to a signed 16 bit number before trying to see
5786 	       whether it will fit in an even smaller size.
5787 	       This allows a 16-bit operand such as $0xffe0 to
5788 	       be recognised as within Imm8S range.  */
5789 	    if ((i.types[op].bitfield.imm16)
5790 		&& fits_in_unsigned_word (i.op[op].imms->X_add_number))
5791 	      {
5792 		i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5793 						^ 0x8000) - 0x8000);
5794 	      }
5795 #ifdef BFD64
5796 	    /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
5797 	    if ((i.types[op].bitfield.imm32)
5798 		&& fits_in_unsigned_long (i.op[op].imms->X_add_number))
5799 	      {
5800 		i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5801 						^ ((offsetT) 1 << 31))
5802 					       - ((offsetT) 1 << 31));
5803 	      }
5804 #endif
5805 	    i.types[op]
5806 	      = operand_type_or (i.types[op],
5807 				 smallest_imm_type (i.op[op].imms->X_add_number));
5808 
5809 	    /* We must avoid matching of Imm32 templates when 64bit
5810 	       only immediate is available.  */
5811 	    if (guess_suffix == QWORD_MNEM_SUFFIX)
5812 	      i.types[op].bitfield.imm32 = 0;
5813 	    break;
5814 
5815 	  case O_absent:
5816 	  case O_register:
5817 	    abort ();
5818 
5819 	    /* Symbols and expressions.  */
5820 	  default:
5821 	    /* Convert symbolic operand to proper sizes for matching, but don't
5822 	       prevent matching a set of insns that only supports sizes other
5823 	       than those matching the insn suffix.  */
5824 	    {
5825 	      i386_operand_type mask, allowed;
5826 	      const insn_template *t = current_templates->start;
5827 
5828 	      operand_type_set (&mask, 0);
5829 	      allowed = t->operand_types[op];
5830 
5831 	      while (++t < current_templates->end)
5832 		{
5833 		  allowed = operand_type_and (allowed, anyimm);
5834 		  allowed = operand_type_or (allowed, t->operand_types[op]);
5835 		}
5836 	      switch (guess_suffix)
5837 		{
5838 		case QWORD_MNEM_SUFFIX:
5839 		  mask.bitfield.imm64 = 1;
5840 		  mask.bitfield.imm32s = 1;
5841 		  break;
5842 		case LONG_MNEM_SUFFIX:
5843 		  mask.bitfield.imm32 = 1;
5844 		  break;
5845 		case WORD_MNEM_SUFFIX:
5846 		  mask.bitfield.imm16 = 1;
5847 		  break;
5848 		case BYTE_MNEM_SUFFIX:
5849 		  mask.bitfield.imm8 = 1;
5850 		  break;
5851 		default:
5852 		  break;
5853 		}
5854 	      allowed = operand_type_and (mask, allowed);
5855 	      if (!operand_type_all_zero (&allowed))
5856 		i.types[op] = operand_type_and (i.types[op], mask);
5857 	    }
5858 	    break;
5859 	  }
5860       }
5861 }
5862 
5863 /* Try to use the smallest displacement type too.  */
5864 static void
optimize_disp(void)5865 optimize_disp (void)
5866 {
5867   int op;
5868 
5869   for (op = i.operands; --op >= 0;)
5870     if (operand_type_check (i.types[op], disp))
5871       {
5872 	if (i.op[op].disps->X_op == O_constant)
5873 	  {
5874 	    offsetT op_disp = i.op[op].disps->X_add_number;
5875 
5876 	    if (!op_disp && i.types[op].bitfield.baseindex)
5877 	      {
5878 		i.types[op] = operand_type_and_not (i.types[op], anydisp);
5879 		i.op[op].disps = NULL;
5880 		i.disp_operands--;
5881 		continue;
5882 	      }
5883 
5884 	    if (i.types[op].bitfield.disp16
5885 		&& fits_in_unsigned_word (op_disp))
5886 	      {
5887 		/* If this operand is at most 16 bits, convert
5888 		   to a signed 16 bit number and don't use 64bit
5889 		   displacement.  */
5890 		op_disp = ((op_disp ^ 0x8000) - 0x8000);
5891 		i.types[op].bitfield.disp64 = 0;
5892 	      }
5893 
5894 #ifdef BFD64
5895 	    /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
5896 	    if ((flag_code != CODE_64BIT
5897 		 ? i.types[op].bitfield.disp32
5898 		 : want_disp32 (current_templates->start)
5899 		   && (!current_templates->start->opcode_modifier.jump
5900 		       || i.jumpabsolute || i.types[op].bitfield.baseindex))
5901 		&& fits_in_unsigned_long (op_disp))
5902 	      {
5903 		/* If this operand is at most 32 bits, convert
5904 		   to a signed 32 bit number and don't use 64bit
5905 		   displacement.  */
5906 		op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5907 		i.types[op].bitfield.disp64 = 0;
5908 		i.types[op].bitfield.disp32 = 1;
5909 	      }
5910 
5911 	    if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5912 	      {
5913 		i.types[op].bitfield.disp64 = 0;
5914 		i.types[op].bitfield.disp32 = 1;
5915 	      }
5916 #endif
5917 	    if ((i.types[op].bitfield.disp32
5918 		 || i.types[op].bitfield.disp16)
5919 		&& fits_in_disp8 (op_disp))
5920 	      i.types[op].bitfield.disp8 = 1;
5921 
5922 	    i.op[op].disps->X_add_number = op_disp;
5923 	  }
5924 	else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5925 		 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5926 	  {
5927 	    fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5928 			 i.op[op].disps, 0, i.reloc[op]);
5929 	    i.types[op] = operand_type_and_not (i.types[op], anydisp);
5930 	  }
5931  	else
5932 	  /* We only support 64bit displacement on constants.  */
5933 	  i.types[op].bitfield.disp64 = 0;
5934       }
5935 }
5936 
5937 /* Return 1 if there is a match in broadcast bytes between operand
5938    GIVEN and instruction template T.   */
5939 
5940 static INLINE int
match_broadcast_size(const insn_template * t,unsigned int given)5941 match_broadcast_size (const insn_template *t, unsigned int given)
5942 {
5943   return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5944 	   && i.types[given].bitfield.byte)
5945 	  || (t->opcode_modifier.broadcast == WORD_BROADCAST
5946 	      && i.types[given].bitfield.word)
5947 	  || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5948 	      && i.types[given].bitfield.dword)
5949 	  || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5950 	      && i.types[given].bitfield.qword));
5951 }
5952 
5953 /* Check if operands are valid for the instruction.  */
5954 
5955 static int
check_VecOperands(const insn_template * t)5956 check_VecOperands (const insn_template *t)
5957 {
5958   unsigned int op;
5959   i386_cpu_flags cpu;
5960 
5961   /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5962      any one operand are implicity requiring AVX512VL support if the actual
5963      operand size is YMMword or XMMword.  Since this function runs after
5964      template matching, there's no need to check for YMMword/XMMword in
5965      the template.  */
5966   cpu = cpu_flags_and (t->cpu_flags, avx512);
5967   if (!cpu_flags_all_zero (&cpu)
5968       && !t->cpu_flags.bitfield.cpuavx512vl
5969       && !cpu_arch_flags.bitfield.cpuavx512vl)
5970     {
5971       for (op = 0; op < t->operands; ++op)
5972 	{
5973 	  if (t->operand_types[op].bitfield.zmmword
5974 	      && (i.types[op].bitfield.ymmword
5975 		  || i.types[op].bitfield.xmmword))
5976 	    {
5977 	      i.error = unsupported;
5978 	      return 1;
5979 	    }
5980 	}
5981     }
5982 
5983   /* Somewhat similarly, templates specifying both AVX and AVX2 are
5984      requiring AVX2 support if the actual operand size is YMMword.  */
5985   if (t->cpu_flags.bitfield.cpuavx
5986       && t->cpu_flags.bitfield.cpuavx2
5987       && !cpu_arch_flags.bitfield.cpuavx2)
5988     {
5989       for (op = 0; op < t->operands; ++op)
5990 	{
5991 	  if (t->operand_types[op].bitfield.xmmword
5992 	      && i.types[op].bitfield.ymmword)
5993 	    {
5994 	      i.error = unsupported;
5995 	      return 1;
5996 	    }
5997 	}
5998     }
5999 
6000   /* Without VSIB byte, we can't have a vector register for index.  */
6001   if (!t->opcode_modifier.sib
6002       && i.index_reg
6003       && (i.index_reg->reg_type.bitfield.xmmword
6004 	  || i.index_reg->reg_type.bitfield.ymmword
6005 	  || i.index_reg->reg_type.bitfield.zmmword))
6006     {
6007       i.error = unsupported_vector_index_register;
6008       return 1;
6009     }
6010 
6011   /* Check if default mask is allowed.  */
6012   if (t->opcode_modifier.nodefmask
6013       && (!i.mask.reg || i.mask.reg->reg_num == 0))
6014     {
6015       i.error = no_default_mask;
6016       return 1;
6017     }
6018 
6019   /* For VSIB byte, we need a vector register for index, and all vector
6020      registers must be distinct.  */
6021   if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6022     {
6023       if (!i.index_reg
6024 	  || !((t->opcode_modifier.sib == VECSIB128
6025 		&& i.index_reg->reg_type.bitfield.xmmword)
6026 	       || (t->opcode_modifier.sib == VECSIB256
6027 		   && i.index_reg->reg_type.bitfield.ymmword)
6028 	       || (t->opcode_modifier.sib == VECSIB512
6029 		   && i.index_reg->reg_type.bitfield.zmmword)))
6030       {
6031 	i.error = invalid_vsib_address;
6032 	return 1;
6033       }
6034 
6035       gas_assert (i.reg_operands == 2 || i.mask.reg);
6036       if (i.reg_operands == 2 && !i.mask.reg)
6037 	{
6038 	  gas_assert (i.types[0].bitfield.class == RegSIMD);
6039 	  gas_assert (i.types[0].bitfield.xmmword
6040 		      || i.types[0].bitfield.ymmword);
6041 	  gas_assert (i.types[2].bitfield.class == RegSIMD);
6042 	  gas_assert (i.types[2].bitfield.xmmword
6043 		      || i.types[2].bitfield.ymmword);
6044 	  if (operand_check == check_none)
6045 	    return 0;
6046 	  if (register_number (i.op[0].regs)
6047 	      != register_number (i.index_reg)
6048 	      && register_number (i.op[2].regs)
6049 		 != register_number (i.index_reg)
6050 	      && register_number (i.op[0].regs)
6051 		 != register_number (i.op[2].regs))
6052 	    return 0;
6053 	  if (operand_check == check_error)
6054 	    {
6055 	      i.error = invalid_vector_register_set;
6056 	      return 1;
6057 	    }
6058 	  as_warn (_("mask, index, and destination registers should be distinct"));
6059 	}
6060       else if (i.reg_operands == 1 && i.mask.reg)
6061 	{
6062 	  if (i.types[1].bitfield.class == RegSIMD
6063 	      && (i.types[1].bitfield.xmmword
6064 	          || i.types[1].bitfield.ymmword
6065 	          || i.types[1].bitfield.zmmword)
6066 	      && (register_number (i.op[1].regs)
6067 		  == register_number (i.index_reg)))
6068 	    {
6069 	      if (operand_check == check_error)
6070 		{
6071 		  i.error = invalid_vector_register_set;
6072 		  return 1;
6073 		}
6074 	      if (operand_check != check_none)
6075 		as_warn (_("index and destination registers should be distinct"));
6076 	    }
6077 	}
6078     }
6079 
6080   /* For AMX instructions with 3 TMM register operands, all operands
6081       must be distinct.  */
6082   if (i.reg_operands == 3
6083       && t->operand_types[0].bitfield.tmmword
6084       && (i.op[0].regs == i.op[1].regs
6085           || i.op[0].regs == i.op[2].regs
6086           || i.op[1].regs == i.op[2].regs))
6087     {
6088       i.error = invalid_tmm_register_set;
6089       return 1;
6090     }
6091 
6092   /* For some special instructions require that destination must be distinct
6093      from source registers.  */
6094   if (t->opcode_modifier.distinctdest)
6095     {
6096       unsigned int dest_reg = i.operands - 1;
6097 
6098       know (i.operands >= 3);
6099 
6100       /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
6101       if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6102 	  || (i.reg_operands > 2
6103 	      && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6104 	{
6105 	  i.error = invalid_dest_and_src_register_set;
6106 	  return 1;
6107 	}
6108     }
6109 
6110   /* Check if broadcast is supported by the instruction and is applied
6111      to the memory operand.  */
6112   if (i.broadcast.type || i.broadcast.bytes)
6113     {
6114       i386_operand_type type, overlap;
6115 
6116       /* Check if specified broadcast is supported in this instruction,
6117 	 and its broadcast bytes match the memory operand.  */
6118       op = i.broadcast.operand;
6119       if (!t->opcode_modifier.broadcast
6120 	  || !(i.flags[op] & Operand_Mem)
6121 	  || (!i.types[op].bitfield.unspecified
6122 	      && !match_broadcast_size (t, op)))
6123 	{
6124 	bad_broadcast:
6125 	  i.error = unsupported_broadcast;
6126 	  return 1;
6127 	}
6128 
6129       if (i.broadcast.type)
6130 	i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6131 			     * i.broadcast.type);
6132       operand_type_set (&type, 0);
6133       switch (get_broadcast_bytes (t, false))
6134 	{
6135 	case 2:
6136 	  type.bitfield.word = 1;
6137 	  break;
6138 	case 4:
6139 	  type.bitfield.dword = 1;
6140 	  break;
6141 	case 8:
6142 	  type.bitfield.qword = 1;
6143 	  break;
6144 	case 16:
6145 	  type.bitfield.xmmword = 1;
6146 	  break;
6147 	case 32:
6148 	  type.bitfield.ymmword = 1;
6149 	  break;
6150 	case 64:
6151 	  type.bitfield.zmmword = 1;
6152 	  break;
6153 	default:
6154 	  goto bad_broadcast;
6155 	}
6156 
6157       overlap = operand_type_and (type, t->operand_types[op]);
6158       if (t->operand_types[op].bitfield.class == RegSIMD
6159 	  && t->operand_types[op].bitfield.byte
6160 	     + t->operand_types[op].bitfield.word
6161 	     + t->operand_types[op].bitfield.dword
6162 	     + t->operand_types[op].bitfield.qword > 1)
6163 	{
6164 	  overlap.bitfield.xmmword = 0;
6165 	  overlap.bitfield.ymmword = 0;
6166 	  overlap.bitfield.zmmword = 0;
6167 	}
6168       if (operand_type_all_zero (&overlap))
6169 	  goto bad_broadcast;
6170 
6171       if (t->opcode_modifier.checkregsize)
6172 	{
6173 	  unsigned int j;
6174 
6175 	  type.bitfield.baseindex = 1;
6176 	  for (j = 0; j < i.operands; ++j)
6177 	    {
6178 	      if (j != op
6179 		  && !operand_type_register_match(i.types[j],
6180 						  t->operand_types[j],
6181 						  type,
6182 						  t->operand_types[op]))
6183 		goto bad_broadcast;
6184 	    }
6185 	}
6186     }
6187   /* If broadcast is supported in this instruction, we need to check if
6188      operand of one-element size isn't specified without broadcast.  */
6189   else if (t->opcode_modifier.broadcast && i.mem_operands)
6190     {
6191       /* Find memory operand.  */
6192       for (op = 0; op < i.operands; op++)
6193 	if (i.flags[op] & Operand_Mem)
6194 	  break;
6195       gas_assert (op < i.operands);
6196       /* Check size of the memory operand.  */
6197       if (match_broadcast_size (t, op))
6198 	{
6199 	  i.error = broadcast_needed;
6200 	  return 1;
6201 	}
6202     }
6203   else
6204     op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
6205 
6206   /* Check if requested masking is supported.  */
6207   if (i.mask.reg)
6208     {
6209       switch (t->opcode_modifier.masking)
6210 	{
6211 	case BOTH_MASKING:
6212 	  break;
6213 	case MERGING_MASKING:
6214 	  if (i.mask.zeroing)
6215 	    {
6216 	case 0:
6217 	      i.error = unsupported_masking;
6218 	      return 1;
6219 	    }
6220 	  break;
6221 	case DYNAMIC_MASKING:
6222 	  /* Memory destinations allow only merging masking.  */
6223 	  if (i.mask.zeroing && i.mem_operands)
6224 	    {
6225 	      /* Find memory operand.  */
6226 	      for (op = 0; op < i.operands; op++)
6227 		if (i.flags[op] & Operand_Mem)
6228 		  break;
6229 	      gas_assert (op < i.operands);
6230 	      if (op == i.operands - 1)
6231 		{
6232 		  i.error = unsupported_masking;
6233 		  return 1;
6234 		}
6235 	    }
6236 	  break;
6237 	default:
6238 	  abort ();
6239 	}
6240     }
6241 
6242   /* Check if masking is applied to dest operand.  */
6243   if (i.mask.reg && (i.mask.operand != i.operands - 1))
6244     {
6245       i.error = mask_not_on_destination;
6246       return 1;
6247     }
6248 
6249   /* Check RC/SAE.  */
6250   if (i.rounding.type != rc_none)
6251     {
6252       if (!t->opcode_modifier.sae
6253 	  || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6254 	  || i.mem_operands)
6255 	{
6256 	  i.error = unsupported_rc_sae;
6257 	  return 1;
6258 	}
6259 
6260       /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6261 	 operand.  */
6262       if (t->opcode_modifier.evex != EVEXLIG)
6263 	{
6264 	  for (op = 0; op < t->operands; ++op)
6265 	    if (i.types[op].bitfield.zmmword)
6266 	      break;
6267 	  if (op >= t->operands)
6268 	    {
6269 	      i.error = operand_size_mismatch;
6270 	      return 1;
6271 	    }
6272 	}
6273     }
6274 
6275   /* Check the special Imm4 cases; must be the first operand.  */
6276   if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6277     {
6278       if (i.op[0].imms->X_op != O_constant
6279 	  || !fits_in_imm4 (i.op[0].imms->X_add_number))
6280 	{
6281 	  i.error = bad_imm4;
6282 	  return 1;
6283 	}
6284 
6285       /* Turn off Imm<N> so that update_imm won't complain.  */
6286       operand_type_set (&i.types[0], 0);
6287     }
6288 
6289   /* Check vector Disp8 operand.  */
6290   if (t->opcode_modifier.disp8memshift
6291       && i.disp_encoding <= disp_encoding_8bit)
6292     {
6293       if (i.broadcast.bytes)
6294 	i.memshift = t->opcode_modifier.broadcast - 1;
6295       else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6296 	i.memshift = t->opcode_modifier.disp8memshift;
6297       else
6298 	{
6299 	  const i386_operand_type *type = NULL, *fallback = NULL;
6300 
6301 	  i.memshift = 0;
6302 	  for (op = 0; op < i.operands; op++)
6303 	    if (i.flags[op] & Operand_Mem)
6304 	      {
6305 		if (t->opcode_modifier.evex == EVEXLIG)
6306 		  i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6307 		else if (t->operand_types[op].bitfield.xmmword
6308 			 + t->operand_types[op].bitfield.ymmword
6309 			 + t->operand_types[op].bitfield.zmmword <= 1)
6310 		  type = &t->operand_types[op];
6311 		else if (!i.types[op].bitfield.unspecified)
6312 		  type = &i.types[op];
6313 		else /* Ambiguities get resolved elsewhere.  */
6314 		  fallback = &t->operand_types[op];
6315 	      }
6316 	    else if (i.types[op].bitfield.class == RegSIMD
6317 		     && t->opcode_modifier.evex != EVEXLIG)
6318 	      {
6319 		if (i.types[op].bitfield.zmmword)
6320 		  i.memshift = 6;
6321 		else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6322 		  i.memshift = 5;
6323 		else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6324 		  i.memshift = 4;
6325 	      }
6326 
6327 	  if (!type && !i.memshift)
6328 	    type = fallback;
6329 	  if (type)
6330 	    {
6331 	      if (type->bitfield.zmmword)
6332 		i.memshift = 6;
6333 	      else if (type->bitfield.ymmword)
6334 		i.memshift = 5;
6335 	      else if (type->bitfield.xmmword)
6336 		i.memshift = 4;
6337 	    }
6338 
6339 	  /* For the check in fits_in_disp8().  */
6340 	  if (i.memshift == 0)
6341 	    i.memshift = -1;
6342 	}
6343 
6344       for (op = 0; op < i.operands; op++)
6345 	if (operand_type_check (i.types[op], disp)
6346 	    && i.op[op].disps->X_op == O_constant)
6347 	  {
6348 	    if (fits_in_disp8 (i.op[op].disps->X_add_number))
6349 	      {
6350 		i.types[op].bitfield.disp8 = 1;
6351 		return 0;
6352 	      }
6353 	    i.types[op].bitfield.disp8 = 0;
6354 	  }
6355     }
6356 
6357   i.memshift = 0;
6358 
6359   return 0;
6360 }
6361 
6362 /* Check if encoding requirements are met by the instruction.  */
6363 
6364 static int
VEX_check_encoding(const insn_template * t)6365 VEX_check_encoding (const insn_template *t)
6366 {
6367   if (i.vec_encoding == vex_encoding_error)
6368     {
6369       i.error = unsupported;
6370       return 1;
6371     }
6372 
6373   if (i.vec_encoding == vex_encoding_evex)
6374     {
6375       /* This instruction must be encoded with EVEX prefix.  */
6376       if (!is_evex_encoding (t))
6377 	{
6378 	  i.error = unsupported;
6379 	  return 1;
6380 	}
6381       return 0;
6382     }
6383 
6384   if (!t->opcode_modifier.vex)
6385     {
6386       /* This instruction template doesn't have VEX prefix.  */
6387       if (i.vec_encoding != vex_encoding_default)
6388 	{
6389 	  i.error = unsupported;
6390 	  return 1;
6391 	}
6392       return 0;
6393     }
6394 
6395   return 0;
6396 }
6397 
6398 static const insn_template *
match_template(char mnem_suffix)6399 match_template (char mnem_suffix)
6400 {
6401   /* Points to template once we've found it.  */
6402   const insn_template *t;
6403   i386_operand_type overlap0, overlap1, overlap2, overlap3;
6404   i386_operand_type overlap4;
6405   unsigned int found_reverse_match;
6406   i386_opcode_modifier suffix_check;
6407   i386_operand_type operand_types [MAX_OPERANDS];
6408   int addr_prefix_disp;
6409   unsigned int j, size_match, check_register;
6410   enum i386_error specific_error = 0;
6411 
6412 #if MAX_OPERANDS != 5
6413 # error "MAX_OPERANDS must be 5."
6414 #endif
6415 
6416   found_reverse_match = 0;
6417   addr_prefix_disp = -1;
6418 
6419   /* Prepare for mnemonic suffix check.  */
6420   memset (&suffix_check, 0, sizeof (suffix_check));
6421   switch (mnem_suffix)
6422     {
6423     case BYTE_MNEM_SUFFIX:
6424       suffix_check.no_bsuf = 1;
6425       break;
6426     case WORD_MNEM_SUFFIX:
6427       suffix_check.no_wsuf = 1;
6428       break;
6429     case SHORT_MNEM_SUFFIX:
6430       suffix_check.no_ssuf = 1;
6431       break;
6432     case LONG_MNEM_SUFFIX:
6433       suffix_check.no_lsuf = 1;
6434       break;
6435     case QWORD_MNEM_SUFFIX:
6436       suffix_check.no_qsuf = 1;
6437       break;
6438     default:
6439       /* NB: In Intel syntax, normally we can check for memory operand
6440 	 size when there is no mnemonic suffix.  But jmp and call have
6441 	 2 different encodings with Dword memory operand size, one with
6442 	 No_ldSuf and the other without.  i.suffix is set to
6443 	 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf.  */
6444       if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6445 	suffix_check.no_ldsuf = 1;
6446     }
6447 
6448   /* Must have right number of operands.  */
6449   i.error = number_of_operands_mismatch;
6450 
6451   for (t = current_templates->start; t < current_templates->end; t++)
6452     {
6453       addr_prefix_disp = -1;
6454       found_reverse_match = 0;
6455 
6456       if (i.operands != t->operands)
6457 	continue;
6458 
6459       /* Check processor support.  */
6460       i.error = unsupported;
6461       if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
6462 	continue;
6463 
6464       /* Check Pseudo Prefix.  */
6465       i.error = unsupported;
6466       if (t->opcode_modifier.pseudovexprefix
6467 	  && !(i.vec_encoding == vex_encoding_vex
6468 	      || i.vec_encoding == vex_encoding_vex3))
6469 	continue;
6470 
6471       /* Check AT&T mnemonic.   */
6472       i.error = unsupported_with_intel_mnemonic;
6473       if (intel_mnemonic && t->opcode_modifier.attmnemonic)
6474 	continue;
6475 
6476       /* Check AT&T/Intel syntax.  */
6477       i.error = unsupported_syntax;
6478       if ((intel_syntax && t->opcode_modifier.attsyntax)
6479 	  || (!intel_syntax && t->opcode_modifier.intelsyntax))
6480 	continue;
6481 
6482       /* Check Intel64/AMD64 ISA.   */
6483       switch (isa64)
6484 	{
6485 	default:
6486 	  /* Default: Don't accept Intel64.  */
6487 	  if (t->opcode_modifier.isa64 == INTEL64)
6488 	    continue;
6489 	  break;
6490 	case amd64:
6491 	  /* -mamd64: Don't accept Intel64 and Intel64 only.  */
6492 	  if (t->opcode_modifier.isa64 >= INTEL64)
6493 	    continue;
6494 	  break;
6495 	case intel64:
6496 	  /* -mintel64: Don't accept AMD64.  */
6497 	  if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
6498 	    continue;
6499 	  break;
6500 	}
6501 
6502       /* Check the suffix.  */
6503       i.error = invalid_instruction_suffix;
6504       if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6505 	  || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6506 	  || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6507 	  || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6508 	  || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6509 	  || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
6510 	continue;
6511 
6512       size_match = operand_size_match (t);
6513       if (!size_match)
6514 	continue;
6515 
6516       /* This is intentionally not
6517 
6518 	 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6519 
6520 	 as the case of a missing * on the operand is accepted (perhaps with
6521 	 a warning, issued further down).  */
6522       if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6523 	{
6524 	  i.error = operand_type_mismatch;
6525 	  continue;
6526 	}
6527 
6528       for (j = 0; j < MAX_OPERANDS; j++)
6529 	operand_types[j] = t->operand_types[j];
6530 
6531       /* In general, don't allow
6532 	 - 64-bit operands outside of 64-bit mode,
6533 	 - 32-bit operands on pre-386.  */
6534       j = i.imm_operands + (t->operands > i.imm_operands + 1);
6535       if (((i.suffix == QWORD_MNEM_SUFFIX
6536 	    && flag_code != CODE_64BIT
6537 	    && !(t->opcode_modifier.opcodespace == SPACE_0F
6538 		 && t->base_opcode == 0xc7
6539 		 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
6540 		 && t->extension_opcode == 1) /* cmpxchg8b */)
6541 	   || (i.suffix == LONG_MNEM_SUFFIX
6542 	       && !cpu_arch_flags.bitfield.cpui386))
6543 	  && (intel_syntax
6544 	      ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
6545 		 && !intel_float_operand (t->name))
6546 	      : intel_float_operand (t->name) != 2)
6547 	  && (t->operands == i.imm_operands
6548 	      || (operand_types[i.imm_operands].bitfield.class != RegMMX
6549 	       && operand_types[i.imm_operands].bitfield.class != RegSIMD
6550 	       && operand_types[i.imm_operands].bitfield.class != RegMask)
6551 	      || (operand_types[j].bitfield.class != RegMMX
6552 		  && operand_types[j].bitfield.class != RegSIMD
6553 		  && operand_types[j].bitfield.class != RegMask))
6554 	  && !t->opcode_modifier.sib)
6555 	continue;
6556 
6557       /* Do not verify operands when there are none.  */
6558       if (!t->operands)
6559 	{
6560 	  if (VEX_check_encoding (t))
6561 	    {
6562 	      specific_error = i.error;
6563 	      continue;
6564 	    }
6565 
6566 	  /* We've found a match; break out of loop.  */
6567 	  break;
6568 	}
6569 
6570       if (!t->opcode_modifier.jump
6571 	  || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6572 	{
6573 	  /* There should be only one Disp operand.  */
6574 	  for (j = 0; j < MAX_OPERANDS; j++)
6575 	    if (operand_type_check (operand_types[j], disp))
6576 	      break;
6577 	  if (j < MAX_OPERANDS)
6578 	    {
6579 	      bool override = (i.prefix[ADDR_PREFIX] != 0);
6580 
6581 	      addr_prefix_disp = j;
6582 
6583 	      /* Address size prefix will turn Disp64 operand into Disp32 and
6584 		 Disp32/Disp16 one into Disp16/Disp32 respectively.  */
6585 	      switch (flag_code)
6586 		{
6587 		case CODE_16BIT:
6588 		  override = !override;
6589 		  /* Fall through.  */
6590 		case CODE_32BIT:
6591 		  if (operand_types[j].bitfield.disp32
6592 		      && operand_types[j].bitfield.disp16)
6593 		    {
6594 		      operand_types[j].bitfield.disp16 = override;
6595 		      operand_types[j].bitfield.disp32 = !override;
6596 		    }
6597 		  gas_assert (!operand_types[j].bitfield.disp64);
6598 		  break;
6599 
6600 		case CODE_64BIT:
6601 		  if (operand_types[j].bitfield.disp64)
6602 		    {
6603 		      gas_assert (!operand_types[j].bitfield.disp32);
6604 		      operand_types[j].bitfield.disp32 = override;
6605 		      operand_types[j].bitfield.disp64 = !override;
6606 		    }
6607 		  operand_types[j].bitfield.disp16 = 0;
6608 		  break;
6609 		}
6610 	    }
6611 	}
6612 
6613       switch (i.reloc[0])
6614 	{
6615 	case BFD_RELOC_386_GOT32:
6616 	  /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
6617 	  if (t->base_opcode == 0xa0
6618 	      && t->opcode_modifier.opcodespace == SPACE_BASE)
6619 	    continue;
6620 	  break;
6621 	case BFD_RELOC_386_TLS_GOTIE:
6622 	case BFD_RELOC_386_TLS_LE_32:
6623 	case BFD_RELOC_X86_64_GOTTPOFF:
6624 	case BFD_RELOC_X86_64_TLSLD:
6625 	  /* Don't allow KMOV in TLS code sequences.  */
6626 	  if (t->opcode_modifier.vex)
6627 	    continue;
6628 	  break;
6629 	default:
6630 	  break;
6631 	}
6632 
6633       /* We check register size if needed.  */
6634       if (t->opcode_modifier.checkregsize)
6635 	{
6636 	  check_register = (1 << t->operands) - 1;
6637 	  if (i.broadcast.type || i.broadcast.bytes)
6638 	    check_register &= ~(1 << i.broadcast.operand);
6639 	}
6640       else
6641 	check_register = 0;
6642 
6643       overlap0 = operand_type_and (i.types[0], operand_types[0]);
6644       switch (t->operands)
6645 	{
6646 	case 1:
6647 	  if (!operand_type_match (overlap0, i.types[0]))
6648 	    continue;
6649 	  break;
6650 	case 2:
6651 	  /* xchg %eax, %eax is a special case. It is an alias for nop
6652 	     only in 32bit mode and we can use opcode 0x90.  In 64bit
6653 	     mode, we can't use 0x90 for xchg %eax, %eax since it should
6654 	     zero-extend %eax to %rax.  */
6655 	  if (flag_code == CODE_64BIT
6656 	      && t->base_opcode == 0x90
6657 	      && t->opcode_modifier.opcodespace == SPACE_BASE
6658 	      && i.types[0].bitfield.instance == Accum
6659 	      && i.types[0].bitfield.dword
6660 	      && i.types[1].bitfield.instance == Accum
6661 	      && i.types[1].bitfield.dword)
6662 	    continue;
6663 	  /* xrelease mov %eax, <disp> is another special case. It must not
6664 	     match the accumulator-only encoding of mov.  */
6665 	  if (flag_code != CODE_64BIT
6666 	      && i.hle_prefix
6667 	      && t->base_opcode == 0xa0
6668 	      && t->opcode_modifier.opcodespace == SPACE_BASE
6669 	      && i.types[0].bitfield.instance == Accum
6670 	      && (i.flags[1] & Operand_Mem))
6671 	    continue;
6672 	  /* Fall through.  */
6673 
6674 	case 3:
6675 	  if (!(size_match & MATCH_STRAIGHT))
6676 	    goto check_reverse;
6677 	  /* Reverse direction of operands if swapping is possible in the first
6678 	     place (operands need to be symmetric) and
6679 	     - the load form is requested, and the template is a store form,
6680 	     - the store form is requested, and the template is a load form,
6681 	     - the non-default (swapped) form is requested.  */
6682 	  overlap1 = operand_type_and (operand_types[0], operand_types[1]);
6683 	  if (t->opcode_modifier.d && i.reg_operands == i.operands
6684 	      && !operand_type_all_zero (&overlap1))
6685 	    switch (i.dir_encoding)
6686 	      {
6687 	      case dir_encoding_load:
6688 		if (operand_type_check (operand_types[i.operands - 1], anymem)
6689 		    || t->opcode_modifier.regmem)
6690 		  goto check_reverse;
6691 		break;
6692 
6693 	      case dir_encoding_store:
6694 		if (!operand_type_check (operand_types[i.operands - 1], anymem)
6695 		    && !t->opcode_modifier.regmem)
6696 		  goto check_reverse;
6697 		break;
6698 
6699 	      case dir_encoding_swap:
6700 		goto check_reverse;
6701 
6702 	      case dir_encoding_default:
6703 		break;
6704 	      }
6705 	  /* If we want store form, we skip the current load.  */
6706 	  if ((i.dir_encoding == dir_encoding_store
6707 	       || i.dir_encoding == dir_encoding_swap)
6708 	      && i.mem_operands == 0
6709 	      && t->opcode_modifier.load)
6710 	    continue;
6711 	  /* Fall through.  */
6712 	case 4:
6713 	case 5:
6714 	  overlap1 = operand_type_and (i.types[1], operand_types[1]);
6715 	  if (!operand_type_match (overlap0, i.types[0])
6716 	      || !operand_type_match (overlap1, i.types[1])
6717 	      || ((check_register & 3) == 3
6718 		  && !operand_type_register_match (i.types[0],
6719 						   operand_types[0],
6720 						   i.types[1],
6721 						   operand_types[1])))
6722 	    {
6723 	      /* Check if other direction is valid ...  */
6724 	      if (!t->opcode_modifier.d)
6725 		continue;
6726 
6727 	    check_reverse:
6728 	      if (!(size_match & MATCH_REVERSE))
6729 		continue;
6730 	      /* Try reversing direction of operands.  */
6731 	      j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6732 	      overlap0 = operand_type_and (i.types[0], operand_types[j]);
6733 	      overlap1 = operand_type_and (i.types[j], operand_types[0]);
6734 	      overlap2 = operand_type_and (i.types[1], operand_types[1]);
6735 	      gas_assert (t->operands != 3 || !check_register);
6736 	      if (!operand_type_match (overlap0, i.types[0])
6737 		  || !operand_type_match (overlap1, i.types[j])
6738 		  || (t->operands == 3
6739 		      && !operand_type_match (overlap2, i.types[1]))
6740 		  || (check_register
6741 		      && !operand_type_register_match (i.types[0],
6742 						       operand_types[j],
6743 						       i.types[j],
6744 						       operand_types[0])))
6745 		{
6746 		  /* Does not match either direction.  */
6747 		  continue;
6748 		}
6749 	      /* found_reverse_match holds which of D or FloatR
6750 		 we've found.  */
6751 	      if (!t->opcode_modifier.d)
6752 		found_reverse_match = 0;
6753 	      else if (operand_types[0].bitfield.tbyte)
6754 		found_reverse_match = Opcode_FloatD;
6755 	      else if (t->opcode_modifier.vexsources)
6756 		{
6757 		  found_reverse_match = Opcode_VexW;
6758 		  goto check_operands_345;
6759 		}
6760 	      else if (operand_types[0].bitfield.xmmword
6761 		       || operand_types[i.operands - 1].bitfield.xmmword
6762 		       || operand_types[0].bitfield.class == RegMMX
6763 		       || operand_types[i.operands - 1].bitfield.class == RegMMX
6764 		       || is_any_vex_encoding(t))
6765 		found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6766 				      ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
6767 	      else
6768 		found_reverse_match = Opcode_D;
6769 	      if (t->opcode_modifier.floatr)
6770 		found_reverse_match |= Opcode_FloatR;
6771 	    }
6772 	  else
6773 	    {
6774 	      /* Found a forward 2 operand match here.  */
6775 	    check_operands_345:
6776 	      switch (t->operands)
6777 		{
6778 		case 5:
6779 		  overlap4 = operand_type_and (i.types[4], operand_types[4]);
6780 		  if (!operand_type_match (overlap4, i.types[4])
6781 		      || !operand_type_register_match (i.types[3],
6782 						       operand_types[3],
6783 						       i.types[4],
6784 						       operand_types[4]))
6785 		    continue;
6786 		  /* Fall through.  */
6787 		case 4:
6788 		  overlap3 = operand_type_and (i.types[3], operand_types[3]);
6789 		  if (!operand_type_match (overlap3, i.types[3])
6790 		      || ((check_register & 0xa) == 0xa
6791 			  && !operand_type_register_match (i.types[1],
6792 							    operand_types[1],
6793 							    i.types[3],
6794 							    operand_types[3]))
6795 		      || ((check_register & 0xc) == 0xc
6796 			  && !operand_type_register_match (i.types[2],
6797 							    operand_types[2],
6798 							    i.types[3],
6799 							    operand_types[3])))
6800 		    continue;
6801 		  /* Fall through.  */
6802 		case 3:
6803 		  overlap2 = operand_type_and (i.types[2], operand_types[2]);
6804 		  if (!operand_type_match (overlap2, i.types[2])
6805 		      || ((check_register & 5) == 5
6806 			  && !operand_type_register_match (i.types[0],
6807 							    operand_types[0],
6808 							    i.types[2],
6809 							    operand_types[2]))
6810 		      || ((check_register & 6) == 6
6811 			  && !operand_type_register_match (i.types[1],
6812 							    operand_types[1],
6813 							    i.types[2],
6814 							    operand_types[2])))
6815 		    continue;
6816 		  break;
6817 		}
6818 	    }
6819 	  /* Found either forward/reverse 2, 3 or 4 operand match here:
6820 	     slip through to break.  */
6821 	}
6822 
6823       /* Check if vector operands are valid.  */
6824       if (check_VecOperands (t))
6825 	{
6826 	  specific_error = i.error;
6827 	  continue;
6828 	}
6829 
6830       /* Check if VEX/EVEX encoding requirements can be satisfied.  */
6831       if (VEX_check_encoding (t))
6832 	{
6833 	  specific_error = i.error;
6834 	  continue;
6835 	}
6836 
6837       /* We've found a match; break out of loop.  */
6838       break;
6839     }
6840 
6841   if (t == current_templates->end)
6842     {
6843       /* We found no match.  */
6844       const char *err_msg;
6845       switch (specific_error ? specific_error : i.error)
6846 	{
6847 	default:
6848 	  abort ();
6849 	case operand_size_mismatch:
6850 	  err_msg = _("operand size mismatch");
6851 	  break;
6852 	case operand_type_mismatch:
6853 	  err_msg = _("operand type mismatch");
6854 	  break;
6855 	case register_type_mismatch:
6856 	  err_msg = _("register type mismatch");
6857 	  break;
6858 	case number_of_operands_mismatch:
6859 	  err_msg = _("number of operands mismatch");
6860 	  break;
6861 	case invalid_instruction_suffix:
6862 	  err_msg = _("invalid instruction suffix");
6863 	  break;
6864 	case bad_imm4:
6865 	  err_msg = _("constant doesn't fit in 4 bits");
6866 	  break;
6867 	case unsupported_with_intel_mnemonic:
6868 	  err_msg = _("unsupported with Intel mnemonic");
6869 	  break;
6870 	case unsupported_syntax:
6871 	  err_msg = _("unsupported syntax");
6872 	  break;
6873 	case unsupported:
6874 	  as_bad (_("unsupported instruction `%s'"),
6875 		  current_templates->start->name);
6876 	  return NULL;
6877 	case invalid_sib_address:
6878 	  err_msg = _("invalid SIB address");
6879 	  break;
6880 	case invalid_vsib_address:
6881 	  err_msg = _("invalid VSIB address");
6882 	  break;
6883 	case invalid_vector_register_set:
6884 	  err_msg = _("mask, index, and destination registers must be distinct");
6885 	  break;
6886 	case invalid_tmm_register_set:
6887 	  err_msg = _("all tmm registers must be distinct");
6888 	  break;
6889 	case invalid_dest_and_src_register_set:
6890 	  err_msg = _("destination and source registers must be distinct");
6891 	  break;
6892 	case unsupported_vector_index_register:
6893 	  err_msg = _("unsupported vector index register");
6894 	  break;
6895 	case unsupported_broadcast:
6896 	  err_msg = _("unsupported broadcast");
6897 	  break;
6898 	case broadcast_needed:
6899 	  err_msg = _("broadcast is needed for operand of such type");
6900 	  break;
6901 	case unsupported_masking:
6902 	  err_msg = _("unsupported masking");
6903 	  break;
6904 	case mask_not_on_destination:
6905 	  err_msg = _("mask not on destination operand");
6906 	  break;
6907 	case no_default_mask:
6908 	  err_msg = _("default mask isn't allowed");
6909 	  break;
6910 	case unsupported_rc_sae:
6911 	  err_msg = _("unsupported static rounding/sae");
6912 	  break;
6913 	case invalid_register_operand:
6914 	  err_msg = _("invalid register operand");
6915 	  break;
6916 	}
6917       as_bad (_("%s for `%s'"), err_msg,
6918 	      current_templates->start->name);
6919       return NULL;
6920     }
6921 
6922   if (!quiet_warnings)
6923     {
6924       if (!intel_syntax
6925 	  && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6926 	as_warn (_("indirect %s without `*'"), t->name);
6927 
6928       if (t->opcode_modifier.isprefix
6929 	  && t->opcode_modifier.mnemonicsize == IGNORESIZE)
6930 	{
6931 	  /* Warn them that a data or address size prefix doesn't
6932 	     affect assembly of the next line of code.  */
6933 	  as_warn (_("stand-alone `%s' prefix"), t->name);
6934 	}
6935     }
6936 
6937   /* Copy the template we found.  */
6938   install_template (t);
6939 
6940   if (addr_prefix_disp != -1)
6941     i.tm.operand_types[addr_prefix_disp]
6942       = operand_types[addr_prefix_disp];
6943 
6944   switch (found_reverse_match)
6945     {
6946     case 0:
6947       break;
6948 
6949     default:
6950       /* If we found a reverse match we must alter the opcode direction
6951 	 bit and clear/flip the regmem modifier one.  found_reverse_match
6952 	 holds bits to change (different for int & float insns).  */
6953 
6954       i.tm.base_opcode ^= found_reverse_match;
6955 
6956       i.tm.operand_types[0] = operand_types[i.operands - 1];
6957       i.tm.operand_types[i.operands - 1] = operand_types[0];
6958 
6959       /* Certain SIMD insns have their load forms specified in the opcode
6960 	 table, and hence we need to _set_ RegMem instead of clearing it.
6961 	 We need to avoid setting the bit though on insns like KMOVW.  */
6962       i.tm.opcode_modifier.regmem
6963 	= i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6964 	  && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6965 	  && !i.tm.opcode_modifier.regmem;
6966       break;
6967 
6968     case Opcode_VexW:
6969       /* Only the first two register operands need reversing, alongside
6970 	 flipping VEX.W.  */
6971       i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
6972 
6973       j = i.tm.operand_types[0].bitfield.imm8;
6974       i.tm.operand_types[j] = operand_types[j + 1];
6975       i.tm.operand_types[j + 1] = operand_types[j];
6976       break;
6977     }
6978 
6979   return t;
6980 }
6981 
6982 static int
check_string(void)6983 check_string (void)
6984 {
6985   unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6986   unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
6987 
6988   if (i.seg[op] != NULL && i.seg[op] != reg_es)
6989     {
6990       as_bad (_("`%s' operand %u must use `%ses' segment"),
6991 	      i.tm.name,
6992 	      intel_syntax ? i.tm.operands - es_op : es_op + 1,
6993 	      register_prefix);
6994       return 0;
6995     }
6996 
6997   /* There's only ever one segment override allowed per instruction.
6998      This instruction possibly has a legal segment override on the
6999      second operand, so copy the segment to where non-string
7000      instructions store it, allowing common code.  */
7001   i.seg[op] = i.seg[1];
7002 
7003   return 1;
7004 }
7005 
7006 static int
process_suffix(void)7007 process_suffix (void)
7008 {
7009   bool is_crc32 = false, is_movx = false;
7010 
7011   /* If matched instruction specifies an explicit instruction mnemonic
7012      suffix, use it.  */
7013   if (i.tm.opcode_modifier.size == SIZE16)
7014     i.suffix = WORD_MNEM_SUFFIX;
7015   else if (i.tm.opcode_modifier.size == SIZE32)
7016     i.suffix = LONG_MNEM_SUFFIX;
7017   else if (i.tm.opcode_modifier.size == SIZE64)
7018     i.suffix = QWORD_MNEM_SUFFIX;
7019   else if (i.reg_operands
7020 	   && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7021 	   && !i.tm.opcode_modifier.addrprefixopreg)
7022     {
7023       unsigned int numop = i.operands;
7024 
7025       /* MOVSX/MOVZX */
7026       is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7027 		 && (i.tm.base_opcode | 8) == 0xbe)
7028 		|| (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7029 		    && i.tm.base_opcode == 0x63
7030 		    && i.tm.cpu_flags.bitfield.cpu64);
7031 
7032       /* CRC32 */
7033       is_crc32 = (i.tm.base_opcode == 0xf0
7034 		  && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7035 		  && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
7036 
7037       /* movsx/movzx want only their source operand considered here, for the
7038 	 ambiguity checking below.  The suffix will be replaced afterwards
7039 	 to represent the destination (register).  */
7040       if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7041 	--i.operands;
7042 
7043       /* crc32 needs REX.W set regardless of suffix / source operand size.  */
7044       if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
7045         i.rex |= REX_W;
7046 
7047       /* If there's no instruction mnemonic suffix we try to invent one
7048 	 based on GPR operands.  */
7049       if (!i.suffix)
7050 	{
7051 	  /* We take i.suffix from the last register operand specified,
7052 	     Destination register type is more significant than source
7053 	     register type.  crc32 in SSE4.2 prefers source register
7054 	     type. */
7055 	  unsigned int op = is_crc32 ? 1 : i.operands;
7056 
7057 	  while (op--)
7058 	    if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7059 		|| i.tm.operand_types[op].bitfield.instance == Accum)
7060 	      {
7061 		if (i.types[op].bitfield.class != Reg)
7062 		  continue;
7063 		if (i.types[op].bitfield.byte)
7064 		  i.suffix = BYTE_MNEM_SUFFIX;
7065 		else if (i.types[op].bitfield.word)
7066 		  i.suffix = WORD_MNEM_SUFFIX;
7067 		else if (i.types[op].bitfield.dword)
7068 		  i.suffix = LONG_MNEM_SUFFIX;
7069 		else if (i.types[op].bitfield.qword)
7070 		  i.suffix = QWORD_MNEM_SUFFIX;
7071 		else
7072 		  continue;
7073 		break;
7074 	      }
7075 
7076 	  /* As an exception, movsx/movzx silently default to a byte source
7077 	     in AT&T mode.  */
7078 	  if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7079 	    i.suffix = BYTE_MNEM_SUFFIX;
7080 	}
7081       else if (i.suffix == BYTE_MNEM_SUFFIX)
7082 	{
7083 	  if (intel_syntax
7084 	      && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7085 	      && i.tm.opcode_modifier.no_bsuf)
7086 	    i.suffix = 0;
7087 	  else if (!check_byte_reg ())
7088 	    return 0;
7089 	}
7090       else if (i.suffix == LONG_MNEM_SUFFIX)
7091 	{
7092 	  if (intel_syntax
7093 	      && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7094 	      && i.tm.opcode_modifier.no_lsuf
7095 	      && !i.tm.opcode_modifier.todword
7096 	      && !i.tm.opcode_modifier.toqword)
7097 	    i.suffix = 0;
7098 	  else if (!check_long_reg ())
7099 	    return 0;
7100 	}
7101       else if (i.suffix == QWORD_MNEM_SUFFIX)
7102 	{
7103 	  if (intel_syntax
7104 	      && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7105 	      && i.tm.opcode_modifier.no_qsuf
7106 	      && !i.tm.opcode_modifier.todword
7107 	      && !i.tm.opcode_modifier.toqword)
7108 	    i.suffix = 0;
7109 	  else if (!check_qword_reg ())
7110 	    return 0;
7111 	}
7112       else if (i.suffix == WORD_MNEM_SUFFIX)
7113 	{
7114 	  if (intel_syntax
7115 	      && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7116 	      && i.tm.opcode_modifier.no_wsuf)
7117 	    i.suffix = 0;
7118 	  else if (!check_word_reg ())
7119 	    return 0;
7120 	}
7121       else if (intel_syntax
7122 	       && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7123 	/* Do nothing if the instruction is going to ignore the prefix.  */
7124 	;
7125       else
7126 	abort ();
7127 
7128       /* Undo the movsx/movzx change done above.  */
7129       i.operands = numop;
7130     }
7131   else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7132 	   && !i.suffix)
7133     {
7134       i.suffix = stackop_size;
7135       if (stackop_size == LONG_MNEM_SUFFIX)
7136 	{
7137 	  /* stackop_size is set to LONG_MNEM_SUFFIX for the
7138 	     .code16gcc directive to support 16-bit mode with
7139 	     32-bit address.  For IRET without a suffix, generate
7140 	     16-bit IRET (opcode 0xcf) to return from an interrupt
7141 	     handler.  */
7142 	  if (i.tm.base_opcode == 0xcf)
7143 	    {
7144 	      i.suffix = WORD_MNEM_SUFFIX;
7145 	      as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7146 	    }
7147 	  /* Warn about changed behavior for segment register push/pop.  */
7148 	  else if ((i.tm.base_opcode | 1) == 0x07)
7149 	    as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7150 		     i.tm.name);
7151 	}
7152     }
7153   else if (!i.suffix
7154 	   && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7155 	       || i.tm.opcode_modifier.jump == JUMP_BYTE
7156 	       || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7157 	       || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7158 		   && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7159 		   && i.tm.extension_opcode <= 3)))
7160     {
7161       switch (flag_code)
7162 	{
7163 	case CODE_64BIT:
7164 	  if (!i.tm.opcode_modifier.no_qsuf)
7165 	    {
7166 	      if (i.tm.opcode_modifier.jump == JUMP_BYTE
7167 		  || i.tm.opcode_modifier.no_lsuf)
7168 		i.suffix = QWORD_MNEM_SUFFIX;
7169 	      break;
7170 	    }
7171 	  /* Fall through.  */
7172 	case CODE_32BIT:
7173 	  if (!i.tm.opcode_modifier.no_lsuf)
7174 	    i.suffix = LONG_MNEM_SUFFIX;
7175 	  break;
7176 	case CODE_16BIT:
7177 	  if (!i.tm.opcode_modifier.no_wsuf)
7178 	    i.suffix = WORD_MNEM_SUFFIX;
7179 	  break;
7180 	}
7181     }
7182 
7183   if (!i.suffix
7184       && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7185 	  /* Also cover lret/retf/iret in 64-bit mode.  */
7186 	  || (flag_code == CODE_64BIT
7187 	      && !i.tm.opcode_modifier.no_lsuf
7188 	      && !i.tm.opcode_modifier.no_qsuf))
7189       && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7190       /* Explicit sizing prefixes are assumed to disambiguate insns.  */
7191       && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7192       /* Accept FLDENV et al without suffix.  */
7193       && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7194     {
7195       unsigned int suffixes, evex = 0;
7196 
7197       suffixes = !i.tm.opcode_modifier.no_bsuf;
7198       if (!i.tm.opcode_modifier.no_wsuf)
7199 	suffixes |= 1 << 1;
7200       if (!i.tm.opcode_modifier.no_lsuf)
7201 	suffixes |= 1 << 2;
7202       if (!i.tm.opcode_modifier.no_ldsuf)
7203 	suffixes |= 1 << 3;
7204       if (!i.tm.opcode_modifier.no_ssuf)
7205 	suffixes |= 1 << 4;
7206       if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7207 	suffixes |= 1 << 5;
7208 
7209       /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
7210 	 also suitable for AT&T syntax mode, it was requested that this be
7211 	 restricted to just Intel syntax.  */
7212       if (intel_syntax && is_any_vex_encoding (&i.tm)
7213 	  && !i.broadcast.type && !i.broadcast.bytes)
7214 	{
7215 	  unsigned int op;
7216 
7217 	  for (op = 0; op < i.tm.operands; ++op)
7218 	    {
7219 	      if (is_evex_encoding (&i.tm)
7220 		  && !cpu_arch_flags.bitfield.cpuavx512vl)
7221 		{
7222 		  if (i.tm.operand_types[op].bitfield.ymmword)
7223 		    i.tm.operand_types[op].bitfield.xmmword = 0;
7224 		  if (i.tm.operand_types[op].bitfield.zmmword)
7225 		    i.tm.operand_types[op].bitfield.ymmword = 0;
7226 		  if (!i.tm.opcode_modifier.evex
7227 		      || i.tm.opcode_modifier.evex == EVEXDYN)
7228 		    i.tm.opcode_modifier.evex = EVEX512;
7229 		}
7230 
7231 	      if (i.tm.operand_types[op].bitfield.xmmword
7232 		  + i.tm.operand_types[op].bitfield.ymmword
7233 		  + i.tm.operand_types[op].bitfield.zmmword < 2)
7234 		continue;
7235 
7236 	      /* Any properly sized operand disambiguates the insn.  */
7237 	      if (i.types[op].bitfield.xmmword
7238 		  || i.types[op].bitfield.ymmword
7239 		  || i.types[op].bitfield.zmmword)
7240 		{
7241 		  suffixes &= ~(7 << 6);
7242 		  evex = 0;
7243 		  break;
7244 		}
7245 
7246 	      if ((i.flags[op] & Operand_Mem)
7247 		  && i.tm.operand_types[op].bitfield.unspecified)
7248 		{
7249 		  if (i.tm.operand_types[op].bitfield.xmmword)
7250 		    suffixes |= 1 << 6;
7251 		  if (i.tm.operand_types[op].bitfield.ymmword)
7252 		    suffixes |= 1 << 7;
7253 		  if (i.tm.operand_types[op].bitfield.zmmword)
7254 		    suffixes |= 1 << 8;
7255 		  if (is_evex_encoding (&i.tm))
7256 		    evex = EVEX512;
7257 		}
7258 	    }
7259 	}
7260 
7261       /* Are multiple suffixes / operand sizes allowed?  */
7262       if (suffixes & (suffixes - 1))
7263 	{
7264 	  if (intel_syntax
7265 	      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7266 		  || operand_check == check_error))
7267 	    {
7268 	      as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
7269 	      return 0;
7270 	    }
7271 	  if (operand_check == check_error)
7272 	    {
7273 	      as_bad (_("no instruction mnemonic suffix given and "
7274 			"no register operands; can't size `%s'"), i.tm.name);
7275 	      return 0;
7276 	    }
7277 	  if (operand_check == check_warning)
7278 	    as_warn (_("%s; using default for `%s'"),
7279 		       intel_syntax
7280 		       ? _("ambiguous operand size")
7281 		       : _("no instruction mnemonic suffix given and "
7282 			   "no register operands"),
7283 		       i.tm.name);
7284 
7285 	  if (i.tm.opcode_modifier.floatmf)
7286 	    i.suffix = SHORT_MNEM_SUFFIX;
7287 	  else if (is_movx)
7288 	    /* handled below */;
7289 	  else if (evex)
7290 	    i.tm.opcode_modifier.evex = evex;
7291 	  else if (flag_code == CODE_16BIT)
7292 	    i.suffix = WORD_MNEM_SUFFIX;
7293 	  else if (!i.tm.opcode_modifier.no_lsuf)
7294 	    i.suffix = LONG_MNEM_SUFFIX;
7295 	  else
7296 	    i.suffix = QWORD_MNEM_SUFFIX;
7297 	}
7298     }
7299 
7300   if (is_movx)
7301     {
7302       /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7303 	 In AT&T syntax, if there is no suffix (warned about above), the default
7304 	 will be byte extension.  */
7305       if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7306 	i.tm.base_opcode |= 1;
7307 
7308       /* For further processing, the suffix should represent the destination
7309 	 (register).  This is already the case when one was used with
7310 	 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7311 	 no suffix to begin with.  */
7312       if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7313 	{
7314 	  if (i.types[1].bitfield.word)
7315 	    i.suffix = WORD_MNEM_SUFFIX;
7316 	  else if (i.types[1].bitfield.qword)
7317 	    i.suffix = QWORD_MNEM_SUFFIX;
7318 	  else
7319 	    i.suffix = LONG_MNEM_SUFFIX;
7320 
7321 	  i.tm.opcode_modifier.w = 0;
7322 	}
7323     }
7324 
7325   if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7326     i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7327 		   != (i.tm.operand_types[1].bitfield.class == Reg);
7328 
7329   /* Change the opcode based on the operand size given by i.suffix.  */
7330   switch (i.suffix)
7331     {
7332     /* Size floating point instruction.  */
7333     case LONG_MNEM_SUFFIX:
7334       if (i.tm.opcode_modifier.floatmf)
7335 	{
7336 	  i.tm.base_opcode ^= 4;
7337 	  break;
7338 	}
7339     /* fall through */
7340     case WORD_MNEM_SUFFIX:
7341     case QWORD_MNEM_SUFFIX:
7342       /* It's not a byte, select word/dword operation.  */
7343       if (i.tm.opcode_modifier.w)
7344 	{
7345 	  if (i.short_form)
7346 	    i.tm.base_opcode |= 8;
7347 	  else
7348 	    i.tm.base_opcode |= 1;
7349 	}
7350     /* fall through */
7351     case SHORT_MNEM_SUFFIX:
7352       /* Now select between word & dword operations via the operand
7353 	 size prefix, except for instructions that will ignore this
7354 	 prefix anyway.  */
7355       if (i.suffix != QWORD_MNEM_SUFFIX
7356 	  && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7357 	  && !i.tm.opcode_modifier.floatmf
7358 	  && !is_any_vex_encoding (&i.tm)
7359 	  && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7360 	      || (flag_code == CODE_64BIT
7361 		  && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7362 	{
7363 	  unsigned int prefix = DATA_PREFIX_OPCODE;
7364 
7365 	  if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7366 	    prefix = ADDR_PREFIX_OPCODE;
7367 
7368 	  if (!add_prefix (prefix))
7369 	    return 0;
7370 	}
7371 
7372       /* Set mode64 for an operand.  */
7373       if (i.suffix == QWORD_MNEM_SUFFIX
7374 	  && flag_code == CODE_64BIT
7375 	  && !i.tm.opcode_modifier.norex64
7376 	  && !i.tm.opcode_modifier.vexw
7377 	  /* Special case for xchg %rax,%rax.  It is NOP and doesn't
7378 	     need rex64. */
7379 	  && ! (i.operands == 2
7380 		&& i.tm.base_opcode == 0x90
7381 		&& i.tm.extension_opcode == None
7382 		&& i.types[0].bitfield.instance == Accum
7383 		&& i.types[0].bitfield.qword
7384 		&& i.types[1].bitfield.instance == Accum
7385 		&& i.types[1].bitfield.qword))
7386 	i.rex |= REX_W;
7387 
7388       break;
7389 
7390     case 0:
7391       /* Select word/dword/qword operation with explicit data sizing prefix
7392 	 when there are no suitable register operands.  */
7393       if (i.tm.opcode_modifier.w
7394 	  && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7395 	  && (!i.reg_operands
7396 	      || (i.reg_operands == 1
7397 		      /* ShiftCount */
7398 		  && (i.tm.operand_types[0].bitfield.instance == RegC
7399 		      /* InOutPortReg */
7400 		      || i.tm.operand_types[0].bitfield.instance == RegD
7401 		      || i.tm.operand_types[1].bitfield.instance == RegD
7402 		      /* CRC32 */
7403 		      || is_crc32))))
7404 	i.tm.base_opcode |= 1;
7405       break;
7406     }
7407 
7408   if (i.tm.opcode_modifier.addrprefixopreg)
7409     {
7410       gas_assert (!i.suffix);
7411       gas_assert (i.reg_operands);
7412 
7413       if (i.tm.operand_types[0].bitfield.instance == Accum
7414 	  || i.operands == 1)
7415 	{
7416 	  /* The address size override prefix changes the size of the
7417 	     first operand.  */
7418 	  if (flag_code == CODE_64BIT
7419 	      && i.op[0].regs->reg_type.bitfield.word)
7420 	    {
7421 	      as_bad (_("16-bit addressing unavailable for `%s'"),
7422 		      i.tm.name);
7423 	      return 0;
7424 	    }
7425 
7426 	  if ((flag_code == CODE_32BIT
7427 	       ? i.op[0].regs->reg_type.bitfield.word
7428 	       : i.op[0].regs->reg_type.bitfield.dword)
7429 	      && !add_prefix (ADDR_PREFIX_OPCODE))
7430 	    return 0;
7431 	}
7432       else
7433 	{
7434 	  /* Check invalid register operand when the address size override
7435 	     prefix changes the size of register operands.  */
7436 	  unsigned int op;
7437 	  enum { need_word, need_dword, need_qword } need;
7438 
7439 	  /* Check the register operand for the address size prefix if
7440 	     the memory operand has no real registers, like symbol, DISP
7441 	     or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
7442 	  if (i.mem_operands == 1
7443 	      && i.reg_operands == 1
7444 	      && i.operands == 2
7445 	      && i.types[1].bitfield.class == Reg
7446 	      && (flag_code == CODE_32BIT
7447 		  ? i.op[1].regs->reg_type.bitfield.word
7448 		  : i.op[1].regs->reg_type.bitfield.dword)
7449 	      && ((i.base_reg == NULL && i.index_reg == NULL)
7450 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7451 		  || (x86_elf_abi == X86_64_X32_ABI
7452 		      && i.base_reg
7453 		      && i.base_reg->reg_num == RegIP
7454 		      && i.base_reg->reg_type.bitfield.qword))
7455 #else
7456 		  || 0)
7457 #endif
7458 	      && !add_prefix (ADDR_PREFIX_OPCODE))
7459 	    return 0;
7460 
7461 	  if (flag_code == CODE_32BIT)
7462 	    need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7463 	  else if (i.prefix[ADDR_PREFIX])
7464 	    need = need_dword;
7465 	  else
7466 	    need = flag_code == CODE_64BIT ? need_qword : need_word;
7467 
7468 	  for (op = 0; op < i.operands; op++)
7469 	    {
7470 	      if (i.types[op].bitfield.class != Reg)
7471 		continue;
7472 
7473 	      switch (need)
7474 		{
7475 		case need_word:
7476 		  if (i.op[op].regs->reg_type.bitfield.word)
7477 		    continue;
7478 		  break;
7479 		case need_dword:
7480 		  if (i.op[op].regs->reg_type.bitfield.dword)
7481 		    continue;
7482 		  break;
7483 		case need_qword:
7484 		  if (i.op[op].regs->reg_type.bitfield.qword)
7485 		    continue;
7486 		  break;
7487 		}
7488 
7489 	      as_bad (_("invalid register operand size for `%s'"),
7490 		      i.tm.name);
7491 	      return 0;
7492 	    }
7493 	}
7494     }
7495 
7496   return 1;
7497 }
7498 
7499 static int
check_byte_reg(void)7500 check_byte_reg (void)
7501 {
7502   int op;
7503 
7504   for (op = i.operands; --op >= 0;)
7505     {
7506       /* Skip non-register operands. */
7507       if (i.types[op].bitfield.class != Reg)
7508 	continue;
7509 
7510       /* If this is an eight bit register, it's OK.  If it's the 16 or
7511 	 32 bit version of an eight bit register, we will just use the
7512 	 low portion, and that's OK too.  */
7513       if (i.types[op].bitfield.byte)
7514 	continue;
7515 
7516       /* I/O port address operands are OK too.  */
7517       if (i.tm.operand_types[op].bitfield.instance == RegD
7518 	  && i.tm.operand_types[op].bitfield.word)
7519 	continue;
7520 
7521       /* crc32 only wants its source operand checked here.  */
7522       if (i.tm.base_opcode == 0xf0
7523 	  && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7524 	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7525 	  && op != 0)
7526 	continue;
7527 
7528       /* Any other register is bad.  */
7529       as_bad (_("`%s%s' not allowed with `%s%c'"),
7530 	      register_prefix, i.op[op].regs->reg_name,
7531 	      i.tm.name, i.suffix);
7532       return 0;
7533     }
7534   return 1;
7535 }
7536 
7537 static int
check_long_reg(void)7538 check_long_reg (void)
7539 {
7540   int op;
7541 
7542   for (op = i.operands; --op >= 0;)
7543     /* Skip non-register operands. */
7544     if (i.types[op].bitfield.class != Reg)
7545       continue;
7546     /* Reject eight bit registers, except where the template requires
7547        them. (eg. movzb)  */
7548     else if (i.types[op].bitfield.byte
7549 	     && (i.tm.operand_types[op].bitfield.class == Reg
7550 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7551 	     && (i.tm.operand_types[op].bitfield.word
7552 		 || i.tm.operand_types[op].bitfield.dword))
7553       {
7554 	as_bad (_("`%s%s' not allowed with `%s%c'"),
7555 		register_prefix,
7556 		i.op[op].regs->reg_name,
7557 		i.tm.name,
7558 		i.suffix);
7559 	return 0;
7560       }
7561     /* Error if the e prefix on a general reg is missing.  */
7562     else if (i.types[op].bitfield.word
7563 	     && (i.tm.operand_types[op].bitfield.class == Reg
7564 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7565 	     && i.tm.operand_types[op].bitfield.dword)
7566       {
7567 	as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7568 		register_prefix, i.op[op].regs->reg_name,
7569 		i.suffix);
7570 	return 0;
7571       }
7572     /* Warn if the r prefix on a general reg is present.  */
7573     else if (i.types[op].bitfield.qword
7574 	     && (i.tm.operand_types[op].bitfield.class == Reg
7575 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7576 	     && i.tm.operand_types[op].bitfield.dword)
7577       {
7578 	if (intel_syntax
7579 	    && i.tm.opcode_modifier.toqword
7580 	    && i.types[0].bitfield.class != RegSIMD)
7581 	  {
7582 	    /* Convert to QWORD.  We want REX byte. */
7583 	    i.suffix = QWORD_MNEM_SUFFIX;
7584 	  }
7585 	else
7586 	  {
7587 	    as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7588 		    register_prefix, i.op[op].regs->reg_name,
7589 		    i.suffix);
7590 	    return 0;
7591 	  }
7592       }
7593   return 1;
7594 }
7595 
7596 static int
check_qword_reg(void)7597 check_qword_reg (void)
7598 {
7599   int op;
7600 
7601   for (op = i.operands; --op >= 0; )
7602     /* Skip non-register operands. */
7603     if (i.types[op].bitfield.class != Reg)
7604       continue;
7605     /* Reject eight bit registers, except where the template requires
7606        them. (eg. movzb)  */
7607     else if (i.types[op].bitfield.byte
7608 	     && (i.tm.operand_types[op].bitfield.class == Reg
7609 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7610 	     && (i.tm.operand_types[op].bitfield.word
7611 		 || i.tm.operand_types[op].bitfield.dword))
7612       {
7613 	as_bad (_("`%s%s' not allowed with `%s%c'"),
7614 		register_prefix,
7615 		i.op[op].regs->reg_name,
7616 		i.tm.name,
7617 		i.suffix);
7618 	return 0;
7619       }
7620     /* Warn if the r prefix on a general reg is missing.  */
7621     else if ((i.types[op].bitfield.word
7622 	      || i.types[op].bitfield.dword)
7623 	     && (i.tm.operand_types[op].bitfield.class == Reg
7624 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7625 	     && i.tm.operand_types[op].bitfield.qword)
7626       {
7627 	/* Prohibit these changes in the 64bit mode, since the
7628 	   lowering is more complicated.  */
7629 	if (intel_syntax
7630 	    && i.tm.opcode_modifier.todword
7631 	    && i.types[0].bitfield.class != RegSIMD)
7632 	  {
7633 	    /* Convert to DWORD.  We don't want REX byte. */
7634 	    i.suffix = LONG_MNEM_SUFFIX;
7635 	  }
7636 	else
7637 	  {
7638 	    as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7639 		    register_prefix, i.op[op].regs->reg_name,
7640 		    i.suffix);
7641 	    return 0;
7642 	  }
7643       }
7644   return 1;
7645 }
7646 
7647 static int
check_word_reg(void)7648 check_word_reg (void)
7649 {
7650   int op;
7651   for (op = i.operands; --op >= 0;)
7652     /* Skip non-register operands. */
7653     if (i.types[op].bitfield.class != Reg)
7654       continue;
7655     /* Reject eight bit registers, except where the template requires
7656        them. (eg. movzb)  */
7657     else if (i.types[op].bitfield.byte
7658 	     && (i.tm.operand_types[op].bitfield.class == Reg
7659 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7660 	     && (i.tm.operand_types[op].bitfield.word
7661 		 || i.tm.operand_types[op].bitfield.dword))
7662       {
7663 	as_bad (_("`%s%s' not allowed with `%s%c'"),
7664 		register_prefix,
7665 		i.op[op].regs->reg_name,
7666 		i.tm.name,
7667 		i.suffix);
7668 	return 0;
7669       }
7670     /* Error if the e or r prefix on a general reg is present.  */
7671     else if ((i.types[op].bitfield.dword
7672 		 || i.types[op].bitfield.qword)
7673 	     && (i.tm.operand_types[op].bitfield.class == Reg
7674 		 || i.tm.operand_types[op].bitfield.instance == Accum)
7675 	     && i.tm.operand_types[op].bitfield.word)
7676       {
7677 	as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7678 		register_prefix, i.op[op].regs->reg_name,
7679 		i.suffix);
7680 	return 0;
7681       }
7682     /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7683     else if (i.types[op].bitfield.qword
7684 	     && intel_syntax
7685 	     && i.tm.opcode_modifier.toqword)
7686       {
7687 	  /* Convert to QWORD.  We want EVEX.W byte. */
7688 	  i.suffix = QWORD_MNEM_SUFFIX;
7689       }
7690   return 1;
7691 }
7692 
7693 static int
update_imm(unsigned int j)7694 update_imm (unsigned int j)
7695 {
7696   i386_operand_type overlap = i.types[j];
7697   if ((overlap.bitfield.imm8
7698        || overlap.bitfield.imm8s
7699        || overlap.bitfield.imm16
7700        || overlap.bitfield.imm32
7701        || overlap.bitfield.imm32s
7702        || overlap.bitfield.imm64)
7703       && !operand_type_equal (&overlap, &imm8)
7704       && !operand_type_equal (&overlap, &imm8s)
7705       && !operand_type_equal (&overlap, &imm16)
7706       && !operand_type_equal (&overlap, &imm32)
7707       && !operand_type_equal (&overlap, &imm32s)
7708       && !operand_type_equal (&overlap, &imm64))
7709     {
7710       if (i.suffix)
7711 	{
7712 	  i386_operand_type temp;
7713 
7714 	  operand_type_set (&temp, 0);
7715 	  if (i.suffix == BYTE_MNEM_SUFFIX)
7716 	    {
7717 	      temp.bitfield.imm8 = overlap.bitfield.imm8;
7718 	      temp.bitfield.imm8s = overlap.bitfield.imm8s;
7719 	    }
7720 	  else if (i.suffix == WORD_MNEM_SUFFIX)
7721 	    temp.bitfield.imm16 = overlap.bitfield.imm16;
7722 	  else if (i.suffix == QWORD_MNEM_SUFFIX)
7723 	    {
7724 	      temp.bitfield.imm64 = overlap.bitfield.imm64;
7725 	      temp.bitfield.imm32s = overlap.bitfield.imm32s;
7726 	    }
7727 	  else
7728 	    temp.bitfield.imm32 = overlap.bitfield.imm32;
7729 	  overlap = temp;
7730 	}
7731       else if (operand_type_equal (&overlap, &imm16_32_32s)
7732 	       || operand_type_equal (&overlap, &imm16_32)
7733 	       || operand_type_equal (&overlap, &imm16_32s))
7734 	{
7735 	  if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
7736 	    overlap = imm16;
7737 	  else
7738 	    overlap = imm32s;
7739 	}
7740       else if (i.prefix[REX_PREFIX] & REX_W)
7741 	overlap = operand_type_and (overlap, imm32s);
7742       else if (i.prefix[DATA_PREFIX])
7743 	overlap = operand_type_and (overlap,
7744 				    flag_code != CODE_16BIT ? imm16 : imm32);
7745       if (!operand_type_equal (&overlap, &imm8)
7746 	  && !operand_type_equal (&overlap, &imm8s)
7747 	  && !operand_type_equal (&overlap, &imm16)
7748 	  && !operand_type_equal (&overlap, &imm32)
7749 	  && !operand_type_equal (&overlap, &imm32s)
7750 	  && !operand_type_equal (&overlap, &imm64))
7751 	{
7752 	  as_bad (_("no instruction mnemonic suffix given; "
7753 		    "can't determine immediate size"));
7754 	  return 0;
7755 	}
7756     }
7757   i.types[j] = overlap;
7758 
7759   return 1;
7760 }
7761 
7762 static int
finalize_imm(void)7763 finalize_imm (void)
7764 {
7765   unsigned int j, n;
7766 
7767   /* Update the first 2 immediate operands.  */
7768   n = i.operands > 2 ? 2 : i.operands;
7769   if (n)
7770     {
7771       for (j = 0; j < n; j++)
7772 	if (update_imm (j) == 0)
7773 	  return 0;
7774 
7775       /* The 3rd operand can't be immediate operand.  */
7776       gas_assert (operand_type_check (i.types[2], imm) == 0);
7777     }
7778 
7779   return 1;
7780 }
7781 
7782 static int
process_operands(void)7783 process_operands (void)
7784 {
7785   /* Default segment register this instruction will use for memory
7786      accesses.  0 means unknown.  This is only for optimizing out
7787      unnecessary segment overrides.  */
7788   const reg_entry *default_seg = NULL;
7789 
7790   if (i.tm.opcode_modifier.sse2avx)
7791     {
7792       /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7793 	 need converting.  */
7794       i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7795       i.prefix[REX_PREFIX] = 0;
7796       i.rex_encoding = 0;
7797     }
7798   /* ImmExt should be processed after SSE2AVX.  */
7799   else if (i.tm.opcode_modifier.immext)
7800     process_immext ();
7801 
7802   if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
7803     {
7804       unsigned int dupl = i.operands;
7805       unsigned int dest = dupl - 1;
7806       unsigned int j;
7807 
7808       /* The destination must be an xmm register.  */
7809       gas_assert (i.reg_operands
7810 		  && MAX_OPERANDS > dupl
7811 		  && operand_type_equal (&i.types[dest], &regxmm));
7812 
7813       if (i.tm.operand_types[0].bitfield.instance == Accum
7814 	  && i.tm.operand_types[0].bitfield.xmmword)
7815 	{
7816 	  if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
7817 	    {
7818 	      /* Keep xmm0 for instructions with VEX prefix and 3
7819 		 sources.  */
7820 	      i.tm.operand_types[0].bitfield.instance = InstanceNone;
7821 	      i.tm.operand_types[0].bitfield.class = RegSIMD;
7822 	      goto duplicate;
7823 	    }
7824 	  else
7825 	    {
7826 	      /* We remove the first xmm0 and keep the number of
7827 		 operands unchanged, which in fact duplicates the
7828 		 destination.  */
7829 	      for (j = 1; j < i.operands; j++)
7830 		{
7831 		  i.op[j - 1] = i.op[j];
7832 		  i.types[j - 1] = i.types[j];
7833 		  i.tm.operand_types[j - 1] = i.tm.operand_types[j];
7834 		  i.flags[j - 1] = i.flags[j];
7835 		}
7836 	    }
7837 	}
7838       else if (i.tm.opcode_modifier.implicit1stxmm0)
7839 	{
7840 	  gas_assert ((MAX_OPERANDS - 1) > dupl
7841 		      && (i.tm.opcode_modifier.vexsources
7842 			  == VEX3SOURCES));
7843 
7844 	  /* Add the implicit xmm0 for instructions with VEX prefix
7845 	     and 3 sources.  */
7846 	  for (j = i.operands; j > 0; j--)
7847 	    {
7848 	      i.op[j] = i.op[j - 1];
7849 	      i.types[j] = i.types[j - 1];
7850 	      i.tm.operand_types[j] = i.tm.operand_types[j - 1];
7851 	      i.flags[j] = i.flags[j - 1];
7852 	    }
7853 	  i.op[0].regs
7854 	    = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7855 	  i.types[0] = regxmm;
7856 	  i.tm.operand_types[0] = regxmm;
7857 
7858 	  i.operands += 2;
7859 	  i.reg_operands += 2;
7860 	  i.tm.operands += 2;
7861 
7862 	  dupl++;
7863 	  dest++;
7864 	  i.op[dupl] = i.op[dest];
7865 	  i.types[dupl] = i.types[dest];
7866 	  i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7867 	  i.flags[dupl] = i.flags[dest];
7868 	}
7869       else
7870 	{
7871 	duplicate:
7872 	  i.operands++;
7873 	  i.reg_operands++;
7874 	  i.tm.operands++;
7875 
7876 	  i.op[dupl] = i.op[dest];
7877 	  i.types[dupl] = i.types[dest];
7878 	  i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7879 	  i.flags[dupl] = i.flags[dest];
7880 	}
7881 
7882        if (i.tm.opcode_modifier.immext)
7883 	 process_immext ();
7884     }
7885   else if (i.tm.operand_types[0].bitfield.instance == Accum
7886 	   && i.tm.operand_types[0].bitfield.xmmword)
7887     {
7888       unsigned int j;
7889 
7890       for (j = 1; j < i.operands; j++)
7891 	{
7892 	  i.op[j - 1] = i.op[j];
7893 	  i.types[j - 1] = i.types[j];
7894 
7895 	  /* We need to adjust fields in i.tm since they are used by
7896 	     build_modrm_byte.  */
7897 	  i.tm.operand_types [j - 1] = i.tm.operand_types [j];
7898 
7899 	  i.flags[j - 1] = i.flags[j];
7900 	}
7901 
7902       i.operands--;
7903       i.reg_operands--;
7904       i.tm.operands--;
7905     }
7906   else if (i.tm.opcode_modifier.implicitquadgroup)
7907     {
7908       unsigned int regnum, first_reg_in_group, last_reg_in_group;
7909 
7910       /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
7911       gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
7912       regnum = register_number (i.op[1].regs);
7913       first_reg_in_group = regnum & ~3;
7914       last_reg_in_group = first_reg_in_group + 3;
7915       if (regnum != first_reg_in_group)
7916 	as_warn (_("source register `%s%s' implicitly denotes"
7917 		   " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7918 		 register_prefix, i.op[1].regs->reg_name,
7919 		 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7920 		 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7921 		 i.tm.name);
7922     }
7923   else if (i.tm.opcode_modifier.regkludge)
7924     {
7925       /* The imul $imm, %reg instruction is converted into
7926 	 imul $imm, %reg, %reg, and the clr %reg instruction
7927 	 is converted into xor %reg, %reg.  */
7928 
7929       unsigned int first_reg_op;
7930 
7931       if (operand_type_check (i.types[0], reg))
7932 	first_reg_op = 0;
7933       else
7934 	first_reg_op = 1;
7935       /* Pretend we saw the extra register operand.  */
7936       gas_assert (i.reg_operands == 1
7937 		  && i.op[first_reg_op + 1].regs == 0);
7938       i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7939       i.types[first_reg_op + 1] = i.types[first_reg_op];
7940       i.operands++;
7941       i.reg_operands++;
7942     }
7943 
7944   if (i.tm.opcode_modifier.modrm)
7945     {
7946       /* The opcode is completed (modulo i.tm.extension_opcode which
7947 	 must be put into the modrm byte).  Now, we make the modrm and
7948 	 index base bytes based on all the info we've collected.  */
7949 
7950       default_seg = build_modrm_byte ();
7951     }
7952   else if (i.types[0].bitfield.class == SReg)
7953     {
7954       if (flag_code != CODE_64BIT
7955 	  ? i.tm.base_opcode == POP_SEG_SHORT
7956 	    && i.op[0].regs->reg_num == 1
7957 	  : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
7958 	    && i.op[0].regs->reg_num < 4)
7959 	{
7960 	  as_bad (_("you can't `%s %s%s'"),
7961 		  i.tm.name, register_prefix, i.op[0].regs->reg_name);
7962 	  return 0;
7963 	}
7964       if (i.op[0].regs->reg_num > 3
7965 	  && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
7966 	{
7967 	  i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7968 	  i.tm.opcode_modifier.opcodespace = SPACE_0F;
7969 	}
7970       i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7971     }
7972   else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7973 	   && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
7974     {
7975       default_seg = reg_ds;
7976     }
7977   else if (i.tm.opcode_modifier.isstring)
7978     {
7979       /* For the string instructions that allow a segment override
7980 	 on one of their operands, the default segment is ds.  */
7981       default_seg = reg_ds;
7982     }
7983   else if (i.short_form)
7984     {
7985       /* The register or float register operand is in operand
7986 	 0 or 1.  */
7987       unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
7988 
7989       /* Register goes in low 3 bits of opcode.  */
7990       i.tm.base_opcode |= i.op[op].regs->reg_num;
7991       if ((i.op[op].regs->reg_flags & RegRex) != 0)
7992 	i.rex |= REX_B;
7993       if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7994 	{
7995 	  /* Warn about some common errors, but press on regardless.
7996 	     The first case can be generated by gcc (<= 2.8.1).  */
7997 	  if (i.operands == 2)
7998 	    {
7999 	      /* Reversed arguments on faddp, fsubp, etc.  */
8000 	      as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8001 		       register_prefix, i.op[!intel_syntax].regs->reg_name,
8002 		       register_prefix, i.op[intel_syntax].regs->reg_name);
8003 	    }
8004 	  else
8005 	    {
8006 	      /* Extraneous `l' suffix on fp insn.  */
8007 	      as_warn (_("translating to `%s %s%s'"), i.tm.name,
8008 		       register_prefix, i.op[0].regs->reg_name);
8009 	    }
8010 	}
8011     }
8012 
8013   if ((i.seg[0] || i.prefix[SEG_PREFIX])
8014       && i.tm.base_opcode == 0x8d /* lea */
8015       && i.tm.opcode_modifier.opcodespace == SPACE_BASE
8016       && !is_any_vex_encoding(&i.tm))
8017     {
8018       if (!quiet_warnings)
8019 	as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8020       if (optimize)
8021 	{
8022 	  i.seg[0] = NULL;
8023 	  i.prefix[SEG_PREFIX] = 0;
8024 	}
8025     }
8026 
8027   /* If a segment was explicitly specified, and the specified segment
8028      is neither the default nor the one already recorded from a prefix,
8029      use an opcode prefix to select it.  If we never figured out what
8030      the default segment is, then default_seg will be zero at this
8031      point, and the specified segment prefix will always be used.  */
8032   if (i.seg[0]
8033       && i.seg[0] != default_seg
8034       && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8035     {
8036       if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8037 	return 0;
8038     }
8039   return 1;
8040 }
8041 
set_rex_vrex(const reg_entry * r,unsigned int rex_bit,bool do_sse2avx)8042 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8043 				 bool do_sse2avx)
8044 {
8045   if (r->reg_flags & RegRex)
8046     {
8047       if (i.rex & rex_bit)
8048 	as_bad (_("same type of prefix used twice"));
8049       i.rex |= rex_bit;
8050     }
8051   else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8052     {
8053       gas_assert (i.vex.register_specifier == r);
8054       i.vex.register_specifier += 8;
8055     }
8056 
8057   if (r->reg_flags & RegVRex)
8058     i.vrex |= rex_bit;
8059 }
8060 
8061 static const reg_entry *
build_modrm_byte(void)8062 build_modrm_byte (void)
8063 {
8064   const reg_entry *default_seg = NULL;
8065   unsigned int source, dest;
8066   int vex_3_sources;
8067 
8068   vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
8069   if (vex_3_sources)
8070     {
8071       unsigned int nds, reg_slot;
8072       expressionS *exp;
8073 
8074       dest = i.operands - 1;
8075       nds = dest - 1;
8076 
8077       /* There are 2 kinds of instructions:
8078 	 1. 5 operands: 4 register operands or 3 register operands
8079 	 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8080 	 VexW0 or VexW1.  The destination must be either XMM, YMM or
8081 	 ZMM register.
8082 	 2. 4 operands: 4 register operands or 3 register operands
8083 	 plus 1 memory operand, with VexXDS.  */
8084       gas_assert ((i.reg_operands == 4
8085 		   || (i.reg_operands == 3 && i.mem_operands == 1))
8086 		  && i.tm.opcode_modifier.vexvvvv == VEXXDS
8087 		  && i.tm.opcode_modifier.vexw
8088 		  && i.tm.operand_types[dest].bitfield.class == RegSIMD);
8089 
8090       /* If VexW1 is set, the first non-immediate operand is the source and
8091 	 the second non-immediate one is encoded in the immediate operand.  */
8092       if (i.tm.opcode_modifier.vexw == VEXW1)
8093 	{
8094 	  source = i.imm_operands;
8095 	  reg_slot = i.imm_operands + 1;
8096 	}
8097       else
8098 	{
8099 	  source = i.imm_operands + 1;
8100 	  reg_slot = i.imm_operands;
8101 	}
8102 
8103       if (i.imm_operands == 0)
8104 	{
8105 	  /* When there is no immediate operand, generate an 8bit
8106 	     immediate operand to encode the first operand.  */
8107 	  exp = &im_expressions[i.imm_operands++];
8108 	  i.op[i.operands].imms = exp;
8109 	  i.types[i.operands] = imm8;
8110 	  i.operands++;
8111 
8112 	  gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8113 	  exp->X_op = O_constant;
8114 	  exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
8115 	  gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8116 	}
8117       else
8118 	{
8119 	  gas_assert (i.imm_operands == 1);
8120 	  gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8121 	  gas_assert (!i.tm.opcode_modifier.immext);
8122 
8123 	  /* Turn on Imm8 again so that output_imm will generate it.  */
8124 	  i.types[0].bitfield.imm8 = 1;
8125 
8126 	  gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8127 	  i.op[0].imms->X_add_number
8128 	      |= register_number (i.op[reg_slot].regs) << 4;
8129 	  gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8130 	}
8131 
8132       gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
8133       i.vex.register_specifier = i.op[nds].regs;
8134     }
8135   else
8136     source = dest = 0;
8137 
8138   /* i.reg_operands MUST be the number of real register operands;
8139      implicit registers do not count.  If there are 3 register
8140      operands, it must be a instruction with VexNDS.  For a
8141      instruction with VexNDD, the destination register is encoded
8142      in VEX prefix.  If there are 4 register operands, it must be
8143      a instruction with VEX prefix and 3 sources.  */
8144   if (i.mem_operands == 0
8145       && ((i.reg_operands == 2
8146 	   && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
8147 	  || (i.reg_operands == 3
8148 	      && i.tm.opcode_modifier.vexvvvv == VEXXDS)
8149 	  || (i.reg_operands == 4 && vex_3_sources)))
8150     {
8151       switch (i.operands)
8152 	{
8153 	case 2:
8154 	  source = 0;
8155 	  break;
8156 	case 3:
8157 	  /* When there are 3 operands, one of them may be immediate,
8158 	     which may be the first or the last operand.  Otherwise,
8159 	     the first operand must be shift count register (cl) or it
8160 	     is an instruction with VexNDS. */
8161 	  gas_assert (i.imm_operands == 1
8162 		      || (i.imm_operands == 0
8163 			  && (i.tm.opcode_modifier.vexvvvv == VEXXDS
8164 			      || (i.types[0].bitfield.instance == RegC
8165 				  && i.types[0].bitfield.byte))));
8166 	  if (operand_type_check (i.types[0], imm)
8167 	      || (i.types[0].bitfield.instance == RegC
8168 		  && i.types[0].bitfield.byte))
8169 	    source = 1;
8170 	  else
8171 	    source = 0;
8172 	  break;
8173 	case 4:
8174 	  /* When there are 4 operands, the first two must be 8bit
8175 	     immediate operands. The source operand will be the 3rd
8176 	     one.
8177 
8178 	     For instructions with VexNDS, if the first operand
8179 	     an imm8, the source operand is the 2nd one.  If the last
8180 	     operand is imm8, the source operand is the first one.  */
8181 	  gas_assert ((i.imm_operands == 2
8182 		       && i.types[0].bitfield.imm8
8183 		       && i.types[1].bitfield.imm8)
8184 		      || (i.tm.opcode_modifier.vexvvvv == VEXXDS
8185 			  && i.imm_operands == 1
8186 			  && (i.types[0].bitfield.imm8
8187 			      || i.types[i.operands - 1].bitfield.imm8)));
8188 	  if (i.imm_operands == 2)
8189 	    source = 2;
8190 	  else
8191 	    {
8192 	      if (i.types[0].bitfield.imm8)
8193 		source = 1;
8194 	      else
8195 		source = 0;
8196 	    }
8197 	  break;
8198 	case 5:
8199 	  gas_assert (!is_evex_encoding (&i.tm));
8200 	  gas_assert (i.imm_operands == 1 && vex_3_sources);
8201 	  break;
8202 	default:
8203 	  abort ();
8204 	}
8205 
8206       if (!vex_3_sources)
8207 	{
8208 	  dest = source + 1;
8209 
8210 	  if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8211 	    {
8212 	      /* For instructions with VexNDS, the register-only source
8213 		 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
8214 		 register.  It is encoded in VEX prefix.  */
8215 
8216 	      i386_operand_type op;
8217 	      unsigned int vvvv;
8218 
8219 	      /* Swap two source operands if needed.  */
8220 	      if (i.tm.opcode_modifier.swapsources)
8221 		{
8222 		  vvvv = source;
8223 		  source = dest;
8224 		}
8225 	      else
8226 		vvvv = dest;
8227 
8228 	      op = i.tm.operand_types[vvvv];
8229 	      if ((dest + 1) >= i.operands
8230 		  || ((op.bitfield.class != Reg
8231 		       || (!op.bitfield.dword && !op.bitfield.qword))
8232 		      && op.bitfield.class != RegSIMD
8233 		      && !operand_type_equal (&op, &regmask)))
8234 		abort ();
8235 	      i.vex.register_specifier = i.op[vvvv].regs;
8236 	      dest++;
8237 	    }
8238 	}
8239 
8240       i.rm.mode = 3;
8241       /* One of the register operands will be encoded in the i.rm.reg
8242 	 field, the other in the combined i.rm.mode and i.rm.regmem
8243 	 fields.  If no form of this instruction supports a memory
8244 	 destination operand, then we assume the source operand may
8245 	 sometimes be a memory operand and so we need to store the
8246 	 destination in the i.rm.reg field.  */
8247       if (!i.tm.opcode_modifier.regmem
8248 	  && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
8249 	{
8250 	  i.rm.reg = i.op[dest].regs->reg_num;
8251 	  i.rm.regmem = i.op[source].regs->reg_num;
8252 	  set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8253 	  set_rex_vrex (i.op[source].regs, REX_B, false);
8254 	}
8255       else
8256 	{
8257 	  i.rm.reg = i.op[source].regs->reg_num;
8258 	  i.rm.regmem = i.op[dest].regs->reg_num;
8259 	  set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8260 	  set_rex_vrex (i.op[source].regs, REX_R, false);
8261 	}
8262       if (flag_code != CODE_64BIT && (i.rex & REX_R))
8263 	{
8264 	  if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
8265 	    abort ();
8266 	  i.rex &= ~REX_R;
8267 	  add_prefix (LOCK_PREFIX_OPCODE);
8268 	}
8269     }
8270   else
8271     {			/* If it's not 2 reg operands...  */
8272       unsigned int mem;
8273 
8274       if (i.mem_operands)
8275 	{
8276 	  unsigned int fake_zero_displacement = 0;
8277 	  unsigned int op;
8278 
8279 	  for (op = 0; op < i.operands; op++)
8280 	    if (i.flags[op] & Operand_Mem)
8281 	      break;
8282 	  gas_assert (op < i.operands);
8283 
8284 	  if (i.tm.opcode_modifier.sib)
8285 	    {
8286 	      /* The index register of VSIB shouldn't be RegIZ.  */
8287 	      if (i.tm.opcode_modifier.sib != SIBMEM
8288 		  && i.index_reg->reg_num == RegIZ)
8289 		abort ();
8290 
8291 	      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8292 	      if (!i.base_reg)
8293 		{
8294 		  i.sib.base = NO_BASE_REGISTER;
8295 		  i.sib.scale = i.log2_scale_factor;
8296 		  i.types[op] = operand_type_and_not (i.types[op], anydisp);
8297 		  i.types[op].bitfield.disp32 = 1;
8298 		}
8299 
8300 	      /* Since the mandatory SIB always has index register, so
8301 		 the code logic remains unchanged. The non-mandatory SIB
8302 		 without index register is allowed and will be handled
8303 		 later.  */
8304 	      if (i.index_reg)
8305 		{
8306 		  if (i.index_reg->reg_num == RegIZ)
8307 		    i.sib.index = NO_INDEX_REGISTER;
8308 		  else
8309 		    i.sib.index = i.index_reg->reg_num;
8310 		  set_rex_vrex (i.index_reg, REX_X, false);
8311 		}
8312 	    }
8313 
8314 	  default_seg = reg_ds;
8315 
8316 	  if (i.base_reg == 0)
8317 	    {
8318 	      i.rm.mode = 0;
8319 	      if (!i.disp_operands)
8320 		fake_zero_displacement = 1;
8321 	      if (i.index_reg == 0)
8322 		{
8323 		  /* Both check for VSIB and mandatory non-vector SIB. */
8324 		  gas_assert (!i.tm.opcode_modifier.sib
8325 			      || i.tm.opcode_modifier.sib == SIBMEM);
8326 		  /* Operand is just <disp>  */
8327 		  i.types[op] = operand_type_and_not (i.types[op], anydisp);
8328 		  if (flag_code == CODE_64BIT)
8329 		    {
8330 		      /* 64bit mode overwrites the 32bit absolute
8331 			 addressing by RIP relative addressing and
8332 			 absolute addressing is encoded by one of the
8333 			 redundant SIB forms.  */
8334 		      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8335 		      i.sib.base = NO_BASE_REGISTER;
8336 		      i.sib.index = NO_INDEX_REGISTER;
8337 		      i.types[op].bitfield.disp32 = 1;
8338 		    }
8339 		  else if ((flag_code == CODE_16BIT)
8340 			   ^ (i.prefix[ADDR_PREFIX] != 0))
8341 		    {
8342 		      i.rm.regmem = NO_BASE_REGISTER_16;
8343 		      i.types[op].bitfield.disp16 = 1;
8344 		    }
8345 		  else
8346 		    {
8347 		      i.rm.regmem = NO_BASE_REGISTER;
8348 		      i.types[op].bitfield.disp32 = 1;
8349 		    }
8350 		}
8351 	      else if (!i.tm.opcode_modifier.sib)
8352 		{
8353 		  /* !i.base_reg && i.index_reg  */
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 		  i.sib.base = NO_BASE_REGISTER;
8359 		  i.sib.scale = i.log2_scale_factor;
8360 		  i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8361 		  i.types[op] = operand_type_and_not (i.types[op], anydisp);
8362 		  i.types[op].bitfield.disp32 = 1;
8363 		  if ((i.index_reg->reg_flags & RegRex) != 0)
8364 		    i.rex |= REX_X;
8365 		}
8366 	    }
8367 	  /* RIP addressing for 64bit mode.  */
8368 	  else if (i.base_reg->reg_num == RegIP)
8369 	    {
8370 	      gas_assert (!i.tm.opcode_modifier.sib);
8371 	      i.rm.regmem = NO_BASE_REGISTER;
8372 	      i.types[op].bitfield.disp8 = 0;
8373 	      i.types[op].bitfield.disp16 = 0;
8374 	      i.types[op].bitfield.disp32 = 1;
8375 	      i.types[op].bitfield.disp64 = 0;
8376 	      i.flags[op] |= Operand_PCrel;
8377 	      if (! i.disp_operands)
8378 		fake_zero_displacement = 1;
8379 	    }
8380 	  else if (i.base_reg->reg_type.bitfield.word)
8381 	    {
8382 	      gas_assert (!i.tm.opcode_modifier.sib);
8383 	      switch (i.base_reg->reg_num)
8384 		{
8385 		case 3: /* (%bx)  */
8386 		  if (i.index_reg == 0)
8387 		    i.rm.regmem = 7;
8388 		  else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
8389 		    i.rm.regmem = i.index_reg->reg_num - 6;
8390 		  break;
8391 		case 5: /* (%bp)  */
8392 		  default_seg = reg_ss;
8393 		  if (i.index_reg == 0)
8394 		    {
8395 		      i.rm.regmem = 6;
8396 		      if (operand_type_check (i.types[op], disp) == 0)
8397 			{
8398 			  /* fake (%bp) into 0(%bp)  */
8399 			  if (i.disp_encoding == disp_encoding_16bit)
8400 			    i.types[op].bitfield.disp16 = 1;
8401 			  else
8402 			    i.types[op].bitfield.disp8 = 1;
8403 			  fake_zero_displacement = 1;
8404 			}
8405 		    }
8406 		  else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
8407 		    i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8408 		  break;
8409 		default: /* (%si) -> 4 or (%di) -> 5  */
8410 		  i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8411 		}
8412 	      if (!fake_zero_displacement
8413 		  && !i.disp_operands
8414 		  && i.disp_encoding)
8415 		{
8416 		  fake_zero_displacement = 1;
8417 		  if (i.disp_encoding == disp_encoding_8bit)
8418 		    i.types[op].bitfield.disp8 = 1;
8419 		  else
8420 		    i.types[op].bitfield.disp16 = 1;
8421 		}
8422 	      i.rm.mode = mode_from_disp_size (i.types[op]);
8423 	    }
8424 	  else /* i.base_reg and 32/64 bit mode  */
8425 	    {
8426 	      if (operand_type_check (i.types[op], disp))
8427 		{
8428 		  i.types[op].bitfield.disp16 = 0;
8429 		  i.types[op].bitfield.disp64 = 0;
8430 		  i.types[op].bitfield.disp32 = 1;
8431 		}
8432 
8433 	      if (!i.tm.opcode_modifier.sib)
8434 		i.rm.regmem = i.base_reg->reg_num;
8435 	      if ((i.base_reg->reg_flags & RegRex) != 0)
8436 		i.rex |= REX_B;
8437 	      i.sib.base = i.base_reg->reg_num;
8438 	      /* x86-64 ignores REX prefix bit here to avoid decoder
8439 		 complications.  */
8440 	      if (!(i.base_reg->reg_flags & RegRex)
8441 		  && (i.base_reg->reg_num == EBP_REG_NUM
8442 		   || i.base_reg->reg_num == ESP_REG_NUM))
8443 		  default_seg = reg_ss;
8444 	      if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8445 		{
8446 		  fake_zero_displacement = 1;
8447 		  if (i.disp_encoding == disp_encoding_32bit)
8448 		    i.types[op].bitfield.disp32 = 1;
8449 		  else
8450 		    i.types[op].bitfield.disp8 = 1;
8451 		}
8452 	      i.sib.scale = i.log2_scale_factor;
8453 	      if (i.index_reg == 0)
8454 		{
8455 		  /* Only check for VSIB. */
8456 		  gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8457 			      && i.tm.opcode_modifier.sib != VECSIB256
8458 			      && i.tm.opcode_modifier.sib != VECSIB512);
8459 
8460 		  /* <disp>(%esp) becomes two byte modrm with no index
8461 		     register.  We've already stored the code for esp
8462 		     in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8463 		     Any base register besides %esp will not use the
8464 		     extra modrm byte.  */
8465 		  i.sib.index = NO_INDEX_REGISTER;
8466 		}
8467 	      else if (!i.tm.opcode_modifier.sib)
8468 		{
8469 		  if (i.index_reg->reg_num == RegIZ)
8470 		    i.sib.index = NO_INDEX_REGISTER;
8471 		  else
8472 		    i.sib.index = i.index_reg->reg_num;
8473 		  i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8474 		  if ((i.index_reg->reg_flags & RegRex) != 0)
8475 		    i.rex |= REX_X;
8476 		}
8477 
8478 	      if (i.disp_operands
8479 		  && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8480 		      || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8481 		i.rm.mode = 0;
8482 	      else
8483 		{
8484 		  if (!fake_zero_displacement
8485 		      && !i.disp_operands
8486 		      && i.disp_encoding)
8487 		    {
8488 		      fake_zero_displacement = 1;
8489 		      if (i.disp_encoding == disp_encoding_8bit)
8490 			i.types[op].bitfield.disp8 = 1;
8491 		      else
8492 			i.types[op].bitfield.disp32 = 1;
8493 		    }
8494 		  i.rm.mode = mode_from_disp_size (i.types[op]);
8495 		}
8496 	    }
8497 
8498 	  if (fake_zero_displacement)
8499 	    {
8500 	      /* Fakes a zero displacement assuming that i.types[op]
8501 		 holds the correct displacement size.  */
8502 	      expressionS *exp;
8503 
8504 	      gas_assert (i.op[op].disps == 0);
8505 	      exp = &disp_expressions[i.disp_operands++];
8506 	      i.op[op].disps = exp;
8507 	      exp->X_op = O_constant;
8508 	      exp->X_add_number = 0;
8509 	      exp->X_add_symbol = (symbolS *) 0;
8510 	      exp->X_op_symbol = (symbolS *) 0;
8511 	    }
8512 
8513 	  mem = op;
8514 	}
8515       else
8516 	mem = ~0;
8517 
8518       if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
8519 	{
8520 	  if (operand_type_check (i.types[0], imm))
8521 	    i.vex.register_specifier = NULL;
8522 	  else
8523 	    {
8524 	      /* VEX.vvvv encodes one of the sources when the first
8525 		 operand is not an immediate.  */
8526 	      if (i.tm.opcode_modifier.vexw == VEXW0)
8527 		i.vex.register_specifier = i.op[0].regs;
8528 	      else
8529 		i.vex.register_specifier = i.op[1].regs;
8530 	    }
8531 
8532 	  /* Destination is a XMM register encoded in the ModRM.reg
8533 	     and VEX.R bit.  */
8534 	  i.rm.reg = i.op[2].regs->reg_num;
8535 	  if ((i.op[2].regs->reg_flags & RegRex) != 0)
8536 	    i.rex |= REX_R;
8537 
8538 	  /* ModRM.rm and VEX.B encodes the other source.  */
8539 	  if (!i.mem_operands)
8540 	    {
8541 	      i.rm.mode = 3;
8542 
8543 	      if (i.tm.opcode_modifier.vexw == VEXW0)
8544 		i.rm.regmem = i.op[1].regs->reg_num;
8545 	      else
8546 		i.rm.regmem = i.op[0].regs->reg_num;
8547 
8548 	      if ((i.op[1].regs->reg_flags & RegRex) != 0)
8549 		i.rex |= REX_B;
8550 	    }
8551 	}
8552       else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
8553 	{
8554 	  i.vex.register_specifier = i.op[2].regs;
8555 	  if (!i.mem_operands)
8556 	    {
8557 	      i.rm.mode = 3;
8558 	      i.rm.regmem = i.op[1].regs->reg_num;
8559 	      if ((i.op[1].regs->reg_flags & RegRex) != 0)
8560 		i.rex |= REX_B;
8561 	    }
8562 	}
8563       /* Fill in i.rm.reg or i.rm.regmem field with register operand
8564 	 (if any) based on i.tm.extension_opcode.  Again, we must be
8565 	 careful to make sure that segment/control/debug/test/MMX
8566 	 registers are coded into the i.rm.reg field.  */
8567       else if (i.reg_operands)
8568 	{
8569 	  unsigned int op;
8570 	  unsigned int vex_reg = ~0;
8571 
8572 	  for (op = 0; op < i.operands; op++)
8573 	    if (i.types[op].bitfield.class == Reg
8574 		|| i.types[op].bitfield.class == RegBND
8575 		|| i.types[op].bitfield.class == RegMask
8576 		|| i.types[op].bitfield.class == SReg
8577 		|| i.types[op].bitfield.class == RegCR
8578 		|| i.types[op].bitfield.class == RegDR
8579 		|| i.types[op].bitfield.class == RegTR
8580 		|| i.types[op].bitfield.class == RegSIMD
8581 		|| i.types[op].bitfield.class == RegMMX)
8582 	      break;
8583 
8584 	  if (vex_3_sources)
8585 	    op = dest;
8586 	  else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8587 	    {
8588 	      /* For instructions with VexNDS, the register-only
8589 		 source operand is encoded in VEX prefix. */
8590 	      gas_assert (mem != (unsigned int) ~0);
8591 
8592 	      if (op > mem)
8593 		{
8594 		  vex_reg = op++;
8595 		  gas_assert (op < i.operands);
8596 		}
8597 	      else
8598 		{
8599 		  /* Check register-only source operand when two source
8600 		     operands are swapped.  */
8601 		  if (!i.tm.operand_types[op].bitfield.baseindex
8602 		      && i.tm.operand_types[op + 1].bitfield.baseindex)
8603 		    {
8604 		      vex_reg = op;
8605 		      op += 2;
8606 		      gas_assert (mem == (vex_reg + 1)
8607 				  && op < i.operands);
8608 		    }
8609 		  else
8610 		    {
8611 		      vex_reg = op + 1;
8612 		      gas_assert (vex_reg < i.operands);
8613 		    }
8614 		}
8615 	    }
8616 	  else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
8617 	    {
8618 	      /* For instructions with VexNDD, the register destination
8619 		 is encoded in VEX prefix.  */
8620 	      if (i.mem_operands == 0)
8621 		{
8622 		  /* There is no memory operand.  */
8623 		  gas_assert ((op + 2) == i.operands);
8624 		  vex_reg = op + 1;
8625 		}
8626 	      else
8627 		{
8628 		  /* There are only 2 non-immediate operands.  */
8629 		  gas_assert (op < i.imm_operands + 2
8630 			      && i.operands == i.imm_operands + 2);
8631 		  vex_reg = i.imm_operands + 1;
8632 		}
8633 	    }
8634 	  else
8635 	    gas_assert (op < i.operands);
8636 
8637 	  if (vex_reg != (unsigned int) ~0)
8638 	    {
8639 	      i386_operand_type *type = &i.tm.operand_types[vex_reg];
8640 
8641 	      if ((type->bitfield.class != Reg
8642 		   || (!type->bitfield.dword && !type->bitfield.qword))
8643 		  && type->bitfield.class != RegSIMD
8644 		  && !operand_type_equal (type, &regmask))
8645 		abort ();
8646 
8647 	      i.vex.register_specifier = i.op[vex_reg].regs;
8648 	    }
8649 
8650 	  /* Don't set OP operand twice.  */
8651 	  if (vex_reg != op)
8652 	    {
8653 	      /* If there is an extension opcode to put here, the
8654 		 register number must be put into the regmem field.  */
8655 	      if (i.tm.extension_opcode != None)
8656 		{
8657 		  i.rm.regmem = i.op[op].regs->reg_num;
8658 		  set_rex_vrex (i.op[op].regs, REX_B,
8659 				i.tm.opcode_modifier.sse2avx);
8660 		}
8661 	      else
8662 		{
8663 		  i.rm.reg = i.op[op].regs->reg_num;
8664 		  set_rex_vrex (i.op[op].regs, REX_R,
8665 				i.tm.opcode_modifier.sse2avx);
8666 		}
8667 	    }
8668 
8669 	  /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8670 	     must set it to 3 to indicate this is a register operand
8671 	     in the regmem field.  */
8672 	  if (!i.mem_operands)
8673 	    i.rm.mode = 3;
8674 	}
8675 
8676       /* Fill in i.rm.reg field with extension opcode (if any).  */
8677       if (i.tm.extension_opcode != None)
8678 	i.rm.reg = i.tm.extension_opcode;
8679     }
8680   return default_seg;
8681 }
8682 
8683 static INLINE void
frag_opcode_byte(unsigned char byte)8684 frag_opcode_byte (unsigned char byte)
8685 {
8686   if (now_seg != absolute_section)
8687     FRAG_APPEND_1_CHAR (byte);
8688   else
8689     ++abs_section_offset;
8690 }
8691 
8692 static unsigned int
flip_code16(unsigned int code16)8693 flip_code16 (unsigned int code16)
8694 {
8695   gas_assert (i.tm.operands == 1);
8696 
8697   return !(i.prefix[REX_PREFIX] & REX_W)
8698 	 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8699 		    : i.tm.operand_types[0].bitfield.disp16)
8700 	 ? CODE16 : 0;
8701 }
8702 
8703 static void
output_branch(void)8704 output_branch (void)
8705 {
8706   char *p;
8707   int size;
8708   int code16;
8709   int prefix;
8710   relax_substateT subtype;
8711   symbolS *sym;
8712   offsetT off;
8713 
8714   if (now_seg == absolute_section)
8715     {
8716       as_bad (_("relaxable branches not supported in absolute section"));
8717       return;
8718     }
8719 
8720   code16 = flag_code == CODE_16BIT ? CODE16 : 0;
8721   size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
8722 
8723   prefix = 0;
8724   if (i.prefix[DATA_PREFIX] != 0)
8725     {
8726       prefix = 1;
8727       i.prefixes -= 1;
8728       code16 ^= flip_code16(code16);
8729     }
8730   /* Pentium4 branch hints.  */
8731   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8732       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8733     {
8734       prefix++;
8735       i.prefixes--;
8736     }
8737   if (i.prefix[REX_PREFIX] != 0)
8738     {
8739       prefix++;
8740       i.prefixes--;
8741     }
8742 
8743   /* BND prefixed jump.  */
8744   if (i.prefix[BND_PREFIX] != 0)
8745     {
8746       prefix++;
8747       i.prefixes--;
8748     }
8749 
8750   if (i.prefixes != 0)
8751     as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8752 
8753   /* It's always a symbol;  End frag & setup for relax.
8754      Make sure there is enough room in this frag for the largest
8755      instruction we may generate in md_convert_frag.  This is 2
8756      bytes for the opcode and room for the prefix and largest
8757      displacement.  */
8758   frag_grow (prefix + 2 + 4);
8759   /* Prefix and 1 opcode byte go in fr_fix.  */
8760   p = frag_more (prefix + 1);
8761   if (i.prefix[DATA_PREFIX] != 0)
8762     *p++ = DATA_PREFIX_OPCODE;
8763   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8764       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8765     *p++ = i.prefix[SEG_PREFIX];
8766   if (i.prefix[BND_PREFIX] != 0)
8767     *p++ = BND_PREFIX_OPCODE;
8768   if (i.prefix[REX_PREFIX] != 0)
8769     *p++ = i.prefix[REX_PREFIX];
8770   *p = i.tm.base_opcode;
8771 
8772   if ((unsigned char) *p == JUMP_PC_RELATIVE)
8773     subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
8774   else if (cpu_arch_flags.bitfield.cpui386)
8775     subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
8776   else
8777     subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
8778   subtype |= code16;
8779 
8780   sym = i.op[0].disps->X_add_symbol;
8781   off = i.op[0].disps->X_add_number;
8782 
8783   if (i.op[0].disps->X_op != O_constant
8784       && i.op[0].disps->X_op != O_symbol)
8785     {
8786       /* Handle complex expressions.  */
8787       sym = make_expr_symbol (i.op[0].disps);
8788       off = 0;
8789     }
8790 
8791   frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8792 
8793   /* 1 possible extra opcode + 4 byte displacement go in var part.
8794      Pass reloc in fr_var.  */
8795   frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
8796 }
8797 
8798 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8799 /* Return TRUE iff PLT32 relocation should be used for branching to
8800    symbol S.  */
8801 
8802 static bool
need_plt32_p(symbolS * s)8803 need_plt32_p (symbolS *s)
8804 {
8805   /* PLT32 relocation is ELF only.  */
8806   if (!IS_ELF)
8807     return false;
8808 
8809 #ifdef TE_SOLARIS
8810   /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8811      krtld support it.  */
8812   return false;
8813 #endif
8814 
8815   /* Since there is no need to prepare for PLT branch on x86-64, we
8816      can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8817      be used as a marker for 32-bit PC-relative branches.  */
8818   if (!object_64bit)
8819     return false;
8820 
8821   if (s == NULL)
8822     return false;
8823 
8824   /* Weak or undefined symbol need PLT32 relocation.  */
8825   if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8826     return true;
8827 
8828   /* Non-global symbol doesn't need PLT32 relocation.  */
8829   if (! S_IS_EXTERNAL (s))
8830     return false;
8831 
8832   /* Other global symbols need PLT32 relocation.  NB: Symbol with
8833      non-default visibilities are treated as normal global symbol
8834      so that PLT32 relocation can be used as a marker for 32-bit
8835      PC-relative branches.  It is useful for linker relaxation.  */
8836   return true;
8837 }
8838 #endif
8839 
8840 static void
output_jump(void)8841 output_jump (void)
8842 {
8843   char *p;
8844   int size;
8845   fixS *fixP;
8846   bfd_reloc_code_real_type jump_reloc = i.reloc[0];
8847 
8848   if (i.tm.opcode_modifier.jump == JUMP_BYTE)
8849     {
8850       /* This is a loop or jecxz type instruction.  */
8851       size = 1;
8852       if (i.prefix[ADDR_PREFIX] != 0)
8853 	{
8854 	  frag_opcode_byte (ADDR_PREFIX_OPCODE);
8855 	  i.prefixes -= 1;
8856 	}
8857       /* Pentium4 branch hints.  */
8858       if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8859 	  || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8860 	{
8861 	  frag_opcode_byte (i.prefix[SEG_PREFIX]);
8862 	  i.prefixes--;
8863 	}
8864     }
8865   else
8866     {
8867       int code16;
8868 
8869       code16 = 0;
8870       if (flag_code == CODE_16BIT)
8871 	code16 = CODE16;
8872 
8873       if (i.prefix[DATA_PREFIX] != 0)
8874 	{
8875 	  frag_opcode_byte (DATA_PREFIX_OPCODE);
8876 	  i.prefixes -= 1;
8877 	  code16 ^= flip_code16(code16);
8878 	}
8879 
8880       size = 4;
8881       if (code16)
8882 	size = 2;
8883     }
8884 
8885   /* BND prefixed jump.  */
8886   if (i.prefix[BND_PREFIX] != 0)
8887     {
8888       frag_opcode_byte (i.prefix[BND_PREFIX]);
8889       i.prefixes -= 1;
8890     }
8891 
8892   if (i.prefix[REX_PREFIX] != 0)
8893     {
8894       frag_opcode_byte (i.prefix[REX_PREFIX]);
8895       i.prefixes -= 1;
8896     }
8897 
8898   if (i.prefixes != 0)
8899     as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8900 
8901   if (now_seg == absolute_section)
8902     {
8903       abs_section_offset += i.opcode_length + size;
8904       return;
8905     }
8906 
8907   p = frag_more (i.opcode_length + size);
8908   switch (i.opcode_length)
8909     {
8910     case 2:
8911       *p++ = i.tm.base_opcode >> 8;
8912       /* Fall through.  */
8913     case 1:
8914       *p++ = i.tm.base_opcode;
8915       break;
8916     default:
8917       abort ();
8918     }
8919 
8920 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8921   if (flag_code == CODE_64BIT && size == 4
8922       && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
8923       && need_plt32_p (i.op[0].disps->X_add_symbol))
8924     jump_reloc = BFD_RELOC_X86_64_PLT32;
8925 #endif
8926 
8927   jump_reloc = reloc (size, 1, 1, jump_reloc);
8928 
8929   fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
8930 		      i.op[0].disps, 1, jump_reloc);
8931 
8932   /* All jumps handled here are signed, but don't unconditionally use a
8933      signed limit check for 32 and 16 bit jumps as we want to allow wrap
8934      around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8935      respectively.  */
8936   switch (size)
8937     {
8938     case 1:
8939       fixP->fx_signed = 1;
8940       break;
8941 
8942     case 2:
8943       if (i.tm.base_opcode == 0xc7f8)
8944 	fixP->fx_signed = 1;
8945       break;
8946 
8947     case 4:
8948       if (flag_code == CODE_64BIT)
8949 	fixP->fx_signed = 1;
8950       break;
8951     }
8952 }
8953 
8954 static void
output_interseg_jump(void)8955 output_interseg_jump (void)
8956 {
8957   char *p;
8958   int size;
8959   int prefix;
8960   int code16;
8961 
8962   code16 = 0;
8963   if (flag_code == CODE_16BIT)
8964     code16 = CODE16;
8965 
8966   prefix = 0;
8967   if (i.prefix[DATA_PREFIX] != 0)
8968     {
8969       prefix = 1;
8970       i.prefixes -= 1;
8971       code16 ^= CODE16;
8972     }
8973 
8974   gas_assert (!i.prefix[REX_PREFIX]);
8975 
8976   size = 4;
8977   if (code16)
8978     size = 2;
8979 
8980   if (i.prefixes != 0)
8981     as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8982 
8983   if (now_seg == absolute_section)
8984     {
8985       abs_section_offset += prefix + 1 + 2 + size;
8986       return;
8987     }
8988 
8989   /* 1 opcode; 2 segment; offset  */
8990   p = frag_more (prefix + 1 + 2 + size);
8991 
8992   if (i.prefix[DATA_PREFIX] != 0)
8993     *p++ = DATA_PREFIX_OPCODE;
8994 
8995   if (i.prefix[REX_PREFIX] != 0)
8996     *p++ = i.prefix[REX_PREFIX];
8997 
8998   *p++ = i.tm.base_opcode;
8999   if (i.op[1].imms->X_op == O_constant)
9000     {
9001       offsetT n = i.op[1].imms->X_add_number;
9002 
9003       if (size == 2
9004 	  && !fits_in_unsigned_word (n)
9005 	  && !fits_in_signed_word (n))
9006 	{
9007 	  as_bad (_("16-bit jump out of range"));
9008 	  return;
9009 	}
9010       md_number_to_chars (p, n, size);
9011     }
9012   else
9013     fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9014 		 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9015 
9016   p += size;
9017   if (i.op[0].imms->X_op == O_constant)
9018     md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9019   else
9020     fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9021 		 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9022 }
9023 
9024 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9025 void
x86_cleanup(void)9026 x86_cleanup (void)
9027 {
9028   char *p;
9029   asection *seg = now_seg;
9030   subsegT subseg = now_subseg;
9031   asection *sec;
9032   unsigned int alignment, align_size_1;
9033   unsigned int isa_1_descsz, feature_2_descsz, descsz;
9034   unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9035   unsigned int padding;
9036 
9037   if (!IS_ELF || !x86_used_note)
9038     return;
9039 
9040   x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9041 
9042   /* The .note.gnu.property section layout:
9043 
9044      Field	Length		Contents
9045      ----	----		----
9046      n_namsz	4		4
9047      n_descsz	4		The note descriptor size
9048      n_type	4		NT_GNU_PROPERTY_TYPE_0
9049      n_name	4		"GNU"
9050      n_desc	n_descsz	The program property array
9051      ....	....		....
9052    */
9053 
9054   /* Create the .note.gnu.property section.  */
9055   sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9056   bfd_set_section_flags (sec,
9057 			 (SEC_ALLOC
9058 			  | SEC_LOAD
9059 			  | SEC_DATA
9060 			  | SEC_HAS_CONTENTS
9061 			  | SEC_READONLY));
9062 
9063   if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9064     {
9065       align_size_1 = 7;
9066       alignment = 3;
9067     }
9068   else
9069     {
9070       align_size_1 = 3;
9071       alignment = 2;
9072     }
9073 
9074   bfd_set_section_alignment (sec, alignment);
9075   elf_section_type (sec) = SHT_NOTE;
9076 
9077   /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9078 				  + 4-byte data  */
9079   isa_1_descsz_raw = 4 + 4 + 4;
9080   /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
9081   isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9082 
9083   feature_2_descsz_raw = isa_1_descsz;
9084   /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9085 				      + 4-byte data  */
9086   feature_2_descsz_raw += 4 + 4 + 4;
9087   /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
9088   feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9089 		      & ~align_size_1);
9090 
9091   descsz = feature_2_descsz;
9092   /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
9093   p = frag_more (4 + 4 + 4 + 4 + descsz);
9094 
9095   /* Write n_namsz.  */
9096   md_number_to_chars (p, (valueT) 4, 4);
9097 
9098   /* Write n_descsz.  */
9099   md_number_to_chars (p + 4, (valueT) descsz, 4);
9100 
9101   /* Write n_type.  */
9102   md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9103 
9104   /* Write n_name.  */
9105   memcpy (p + 4 * 3, "GNU", 4);
9106 
9107   /* Write 4-byte type.  */
9108   md_number_to_chars (p + 4 * 4,
9109 		      (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9110 
9111   /* Write 4-byte data size.  */
9112   md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9113 
9114   /* Write 4-byte data.  */
9115   md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9116 
9117   /* Zero out paddings.  */
9118   padding = isa_1_descsz - isa_1_descsz_raw;
9119   if (padding)
9120     memset (p + 4 * 7, 0, padding);
9121 
9122   /* Write 4-byte type.  */
9123   md_number_to_chars (p + isa_1_descsz + 4 * 4,
9124 		      (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9125 
9126   /* Write 4-byte data size.  */
9127   md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9128 
9129   /* Write 4-byte data.  */
9130   md_number_to_chars (p + isa_1_descsz + 4 * 6,
9131 		      (valueT) x86_feature_2_used, 4);
9132 
9133   /* Zero out paddings.  */
9134   padding = feature_2_descsz - feature_2_descsz_raw;
9135   if (padding)
9136     memset (p + isa_1_descsz + 4 * 7, 0, padding);
9137 
9138   /* We probably can't restore the current segment, for there likely
9139      isn't one yet...  */
9140   if (seg && subseg)
9141     subseg_set (seg, subseg);
9142 }
9143 #endif
9144 
9145 static unsigned int
encoding_length(const fragS * start_frag,offsetT start_off,const char * frag_now_ptr)9146 encoding_length (const fragS *start_frag, offsetT start_off,
9147 		 const char *frag_now_ptr)
9148 {
9149   unsigned int len = 0;
9150 
9151   if (start_frag != frag_now)
9152     {
9153       const fragS *fr = start_frag;
9154 
9155       do {
9156 	len += fr->fr_fix;
9157 	fr = fr->fr_next;
9158       } while (fr && fr != frag_now);
9159     }
9160 
9161   return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9162 }
9163 
9164 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9165    be macro-fused with conditional jumps.
9166    NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9167    or is one of the following format:
9168 
9169     cmp m, imm
9170     add m, imm
9171     sub m, imm
9172    test m, imm
9173     and m, imm
9174     inc m
9175     dec m
9176 
9177    it is unfusible.  */
9178 
9179 static int
maybe_fused_with_jcc_p(enum mf_cmp_kind * mf_cmp_p)9180 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9181 {
9182   /* No RIP address.  */
9183   if (i.base_reg && i.base_reg->reg_num == RegIP)
9184     return 0;
9185 
9186   /* No opcodes outside of base encoding space.  */
9187   if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9188     return 0;
9189 
9190   /* add, sub without add/sub m, imm.  */
9191   if (i.tm.base_opcode <= 5
9192       || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9193       || ((i.tm.base_opcode | 3) == 0x83
9194 	  && (i.tm.extension_opcode == 0x5
9195 	      || i.tm.extension_opcode == 0x0)))
9196     {
9197       *mf_cmp_p = mf_cmp_alu_cmp;
9198       return !(i.mem_operands && i.imm_operands);
9199     }
9200 
9201   /* and without and m, imm.  */
9202   if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9203       || ((i.tm.base_opcode | 3) == 0x83
9204 	  && i.tm.extension_opcode == 0x4))
9205     {
9206       *mf_cmp_p = mf_cmp_test_and;
9207       return !(i.mem_operands && i.imm_operands);
9208     }
9209 
9210   /* test without test m imm.  */
9211   if ((i.tm.base_opcode | 1) == 0x85
9212       || (i.tm.base_opcode | 1) == 0xa9
9213       || ((i.tm.base_opcode | 1) == 0xf7
9214 	  && i.tm.extension_opcode == 0))
9215     {
9216       *mf_cmp_p = mf_cmp_test_and;
9217       return !(i.mem_operands && i.imm_operands);
9218     }
9219 
9220   /* cmp without cmp m, imm.  */
9221   if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9222       || ((i.tm.base_opcode | 3) == 0x83
9223 	  && (i.tm.extension_opcode == 0x7)))
9224     {
9225       *mf_cmp_p = mf_cmp_alu_cmp;
9226       return !(i.mem_operands && i.imm_operands);
9227     }
9228 
9229   /* inc, dec without inc/dec m.   */
9230   if ((i.tm.cpu_flags.bitfield.cpuno64
9231        && (i.tm.base_opcode | 0xf) == 0x4f)
9232       || ((i.tm.base_opcode | 1) == 0xff
9233 	  && i.tm.extension_opcode <= 0x1))
9234     {
9235       *mf_cmp_p = mf_cmp_incdec;
9236       return !i.mem_operands;
9237     }
9238 
9239   return 0;
9240 }
9241 
9242 /* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
9243 
9244 static int
add_fused_jcc_padding_frag_p(enum mf_cmp_kind * mf_cmp_p)9245 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9246 {
9247   /* NB: Don't work with COND_JUMP86 without i386.  */
9248   if (!align_branch_power
9249       || now_seg == absolute_section
9250       || !cpu_arch_flags.bitfield.cpui386
9251       || !(align_branch & align_branch_fused_bit))
9252     return 0;
9253 
9254   if (maybe_fused_with_jcc_p (mf_cmp_p))
9255     {
9256       if (last_insn.kind == last_insn_other
9257 	  || last_insn.seg != now_seg)
9258 	return 1;
9259       if (flag_debug)
9260 	as_warn_where (last_insn.file, last_insn.line,
9261 		       _("`%s` skips -malign-branch-boundary on `%s`"),
9262 		       last_insn.name, i.tm.name);
9263     }
9264 
9265   return 0;
9266 }
9267 
9268 /* Return 1 if a BRANCH_PREFIX frag should be generated.  */
9269 
9270 static int
add_branch_prefix_frag_p(void)9271 add_branch_prefix_frag_p (void)
9272 {
9273   /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
9274      to PadLock instructions since they include prefixes in opcode.  */
9275   if (!align_branch_power
9276       || !align_branch_prefix_size
9277       || now_seg == absolute_section
9278       || i.tm.cpu_flags.bitfield.cpupadlock
9279       || !cpu_arch_flags.bitfield.cpui386)
9280     return 0;
9281 
9282   /* Don't add prefix if it is a prefix or there is no operand in case
9283      that segment prefix is special.  */
9284   if (!i.operands || i.tm.opcode_modifier.isprefix)
9285     return 0;
9286 
9287   if (last_insn.kind == last_insn_other
9288       || last_insn.seg != now_seg)
9289     return 1;
9290 
9291   if (flag_debug)
9292     as_warn_where (last_insn.file, last_insn.line,
9293 		   _("`%s` skips -malign-branch-boundary on `%s`"),
9294 		   last_insn.name, i.tm.name);
9295 
9296   return 0;
9297 }
9298 
9299 /* Return 1 if a BRANCH_PADDING frag should be generated.  */
9300 
9301 static int
add_branch_padding_frag_p(enum align_branch_kind * branch_p,enum mf_jcc_kind * mf_jcc_p)9302 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9303 			   enum mf_jcc_kind *mf_jcc_p)
9304 {
9305   int add_padding;
9306 
9307   /* NB: Don't work with COND_JUMP86 without i386.  */
9308   if (!align_branch_power
9309       || now_seg == absolute_section
9310       || !cpu_arch_flags.bitfield.cpui386
9311       || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9312     return 0;
9313 
9314   add_padding = 0;
9315 
9316   /* Check for jcc and direct jmp.  */
9317   if (i.tm.opcode_modifier.jump == JUMP)
9318     {
9319       if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9320 	{
9321 	  *branch_p = align_branch_jmp;
9322 	  add_padding = align_branch & align_branch_jmp_bit;
9323 	}
9324       else
9325 	{
9326 	  /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9327 	     igore the lowest bit.  */
9328 	  *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9329 	  *branch_p = align_branch_jcc;
9330 	  if ((align_branch & align_branch_jcc_bit))
9331 	    add_padding = 1;
9332 	}
9333     }
9334   else if ((i.tm.base_opcode | 1) == 0xc3)
9335     {
9336       /* Near ret.  */
9337       *branch_p = align_branch_ret;
9338       if ((align_branch & align_branch_ret_bit))
9339 	add_padding = 1;
9340     }
9341   else
9342     {
9343       /* Check for indirect jmp, direct and indirect calls.  */
9344       if (i.tm.base_opcode == 0xe8)
9345 	{
9346 	  /* Direct call.  */
9347 	  *branch_p = align_branch_call;
9348 	  if ((align_branch & align_branch_call_bit))
9349 	    add_padding = 1;
9350 	}
9351       else if (i.tm.base_opcode == 0xff
9352 	       && (i.tm.extension_opcode == 2
9353 		   || i.tm.extension_opcode == 4))
9354 	{
9355 	  /* Indirect call and jmp.  */
9356 	  *branch_p = align_branch_indirect;
9357 	  if ((align_branch & align_branch_indirect_bit))
9358 	    add_padding = 1;
9359 	}
9360 
9361       if (add_padding
9362 	  && i.disp_operands
9363 	  && tls_get_addr
9364 	  && (i.op[0].disps->X_op == O_symbol
9365 	      || (i.op[0].disps->X_op == O_subtract
9366 		  && i.op[0].disps->X_op_symbol == GOT_symbol)))
9367 	{
9368 	  symbolS *s = i.op[0].disps->X_add_symbol;
9369 	  /* No padding to call to global or undefined tls_get_addr.  */
9370 	  if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9371 	      && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9372 	    return 0;
9373 	}
9374     }
9375 
9376   if (add_padding
9377       && last_insn.kind != last_insn_other
9378       && last_insn.seg == now_seg)
9379     {
9380       if (flag_debug)
9381 	as_warn_where (last_insn.file, last_insn.line,
9382 		       _("`%s` skips -malign-branch-boundary on `%s`"),
9383 		       last_insn.name, i.tm.name);
9384       return 0;
9385     }
9386 
9387   return add_padding;
9388 }
9389 
9390 static void
output_insn(void)9391 output_insn (void)
9392 {
9393   fragS *insn_start_frag;
9394   offsetT insn_start_off;
9395   fragS *fragP = NULL;
9396   enum align_branch_kind branch = align_branch_none;
9397   /* The initializer is arbitrary just to avoid uninitialized error.
9398      it's actually either assigned in add_branch_padding_frag_p
9399      or never be used.  */
9400   enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9401 
9402 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9403   if (IS_ELF && x86_used_note && now_seg != absolute_section)
9404     {
9405       if ((i.xstate & xstate_tmm) == xstate_tmm
9406 	  || i.tm.cpu_flags.bitfield.cpuamx_tile)
9407 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9408 
9409       if (i.tm.cpu_flags.bitfield.cpu8087
9410 	  || i.tm.cpu_flags.bitfield.cpu287
9411 	  || i.tm.cpu_flags.bitfield.cpu387
9412 	  || i.tm.cpu_flags.bitfield.cpu687
9413 	  || i.tm.cpu_flags.bitfield.cpufisttp)
9414 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9415 
9416       if ((i.xstate & xstate_mmx)
9417 	  || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9418 	      && !is_any_vex_encoding (&i.tm)
9419 	      && (i.tm.base_opcode == 0x77 /* emms */
9420 		  || i.tm.base_opcode == 0x0e /* femms */)))
9421 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9422 
9423       if (i.index_reg)
9424 	{
9425 	  if (i.index_reg->reg_type.bitfield.zmmword)
9426 	    i.xstate |= xstate_zmm;
9427 	  else if (i.index_reg->reg_type.bitfield.ymmword)
9428 	    i.xstate |= xstate_ymm;
9429 	  else if (i.index_reg->reg_type.bitfield.xmmword)
9430 	    i.xstate |= xstate_xmm;
9431 	}
9432 
9433       /* vzeroall / vzeroupper */
9434       if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9435 	i.xstate |= xstate_ymm;
9436 
9437       if ((i.xstate & xstate_xmm)
9438 	  /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9439 	  || (i.tm.base_opcode == 0xae
9440 	      && (i.tm.cpu_flags.bitfield.cpusse
9441 		  || i.tm.cpu_flags.bitfield.cpuavx))
9442 	  || i.tm.cpu_flags.bitfield.cpuwidekl
9443 	  || i.tm.cpu_flags.bitfield.cpukl)
9444 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9445 
9446       if ((i.xstate & xstate_ymm) == xstate_ymm)
9447 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9448       if ((i.xstate & xstate_zmm) == xstate_zmm)
9449 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9450       if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9451 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9452       if (i.tm.cpu_flags.bitfield.cpufxsr)
9453 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9454       if (i.tm.cpu_flags.bitfield.cpuxsave)
9455 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9456       if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9457 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9458       if (i.tm.cpu_flags.bitfield.cpuxsavec)
9459 	x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9460 
9461       if (x86_feature_2_used
9462 	  || i.tm.cpu_flags.bitfield.cpucmov
9463 	  || i.tm.cpu_flags.bitfield.cpusyscall
9464 	  || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9465 	      && i.tm.base_opcode == 0xc7
9466 	      && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
9467 	      && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9468 	x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9469       if (i.tm.cpu_flags.bitfield.cpusse3
9470 	  || i.tm.cpu_flags.bitfield.cpussse3
9471 	  || i.tm.cpu_flags.bitfield.cpusse4_1
9472 	  || i.tm.cpu_flags.bitfield.cpusse4_2
9473 	  || i.tm.cpu_flags.bitfield.cpucx16
9474 	  || i.tm.cpu_flags.bitfield.cpupopcnt
9475 	  /* LAHF-SAHF insns in 64-bit mode.  */
9476 	  || (flag_code == CODE_64BIT
9477 	      && (i.tm.base_opcode | 1) == 0x9f
9478 	      && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
9479 	x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9480       if (i.tm.cpu_flags.bitfield.cpuavx
9481 	  || i.tm.cpu_flags.bitfield.cpuavx2
9482 	  /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9483 	     XOP, FMA4, LPW, TBM, and AMX.  */
9484 	  || (i.tm.opcode_modifier.vex
9485 	      && !i.tm.cpu_flags.bitfield.cpuavx512f
9486 	      && !i.tm.cpu_flags.bitfield.cpuavx512bw
9487 	      && !i.tm.cpu_flags.bitfield.cpuavx512dq
9488 	      && !i.tm.cpu_flags.bitfield.cpuxop
9489 	      && !i.tm.cpu_flags.bitfield.cpufma4
9490 	      && !i.tm.cpu_flags.bitfield.cpulwp
9491 	      && !i.tm.cpu_flags.bitfield.cputbm
9492 	      && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9493 	  || i.tm.cpu_flags.bitfield.cpuf16c
9494 	  || i.tm.cpu_flags.bitfield.cpufma
9495 	  || i.tm.cpu_flags.bitfield.cpulzcnt
9496 	  || i.tm.cpu_flags.bitfield.cpumovbe
9497 	  || i.tm.cpu_flags.bitfield.cpuxsaves
9498 	  || (x86_feature_2_used
9499 	      & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9500 		 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9501 		 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9502 	x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9503       if (i.tm.cpu_flags.bitfield.cpuavx512f
9504 	  || i.tm.cpu_flags.bitfield.cpuavx512bw
9505 	  || i.tm.cpu_flags.bitfield.cpuavx512dq
9506 	  || i.tm.cpu_flags.bitfield.cpuavx512vl
9507 	  /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9508 	     AVX512-4FMAPS, and AVX512-4VNNIW.  */
9509 	  || (i.tm.opcode_modifier.evex
9510 	      && !i.tm.cpu_flags.bitfield.cpuavx512er
9511 	      && !i.tm.cpu_flags.bitfield.cpuavx512pf
9512 	      && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
9513 	      && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9514 	x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9515     }
9516 #endif
9517 
9518   /* Tie dwarf2 debug info to the address at the start of the insn.
9519      We can't do this after the insn has been output as the current
9520      frag may have been closed off.  eg. by frag_var.  */
9521   dwarf2_emit_insn (0);
9522 
9523   insn_start_frag = frag_now;
9524   insn_start_off = frag_now_fix ();
9525 
9526   if (add_branch_padding_frag_p (&branch, &mf_jcc))
9527     {
9528       char *p;
9529       /* Branch can be 8 bytes.  Leave some room for prefixes.  */
9530       unsigned int max_branch_padding_size = 14;
9531 
9532       /* Align section to boundary.  */
9533       record_alignment (now_seg, align_branch_power);
9534 
9535       /* Make room for padding.  */
9536       frag_grow (max_branch_padding_size);
9537 
9538       /* Start of the padding.  */
9539       p = frag_more (0);
9540 
9541       fragP = frag_now;
9542 
9543       frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9544 		ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9545 		NULL, 0, p);
9546 
9547       fragP->tc_frag_data.mf_type = mf_jcc;
9548       fragP->tc_frag_data.branch_type = branch;
9549       fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9550     }
9551 
9552   /* Output jumps.  */
9553   if (i.tm.opcode_modifier.jump == JUMP)
9554     output_branch ();
9555   else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9556 	   || i.tm.opcode_modifier.jump == JUMP_DWORD)
9557     output_jump ();
9558   else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9559     output_interseg_jump ();
9560   else
9561     {
9562       /* Output normal instructions here.  */
9563       char *p;
9564       unsigned char *q;
9565       unsigned int j;
9566       enum mf_cmp_kind mf_cmp;
9567 
9568       if (avoid_fence
9569 	  && (i.tm.base_opcode == 0xaee8
9570 	      || i.tm.base_opcode == 0xaef0
9571 	      || i.tm.base_opcode == 0xaef8))
9572 	{
9573 	  /* Encode lfence, mfence, and sfence as
9574 	     f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
9575 	  if (flag_code == CODE_16BIT)
9576 	    as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9577 	  else if (omit_lock_prefix)
9578 	    as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9579 		    i.tm.name);
9580 	  else if (now_seg != absolute_section)
9581 	    {
9582 	      offsetT val = 0x240483f0ULL;
9583 
9584 	      p = frag_more (5);
9585 	      md_number_to_chars (p, val, 5);
9586 	    }
9587 	  else
9588 	    abs_section_offset += 5;
9589 	  return;
9590 	}
9591 
9592       /* Some processors fail on LOCK prefix. This options makes
9593 	 assembler ignore LOCK prefix and serves as a workaround.  */
9594       if (omit_lock_prefix)
9595 	{
9596 	  if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9597 	      && i.tm.opcode_modifier.isprefix)
9598 	    return;
9599 	  i.prefix[LOCK_PREFIX] = 0;
9600 	}
9601 
9602       if (branch)
9603 	/* Skip if this is a branch.  */
9604 	;
9605       else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9606 	{
9607 	  /* Make room for padding.  */
9608 	  frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9609 	  p = frag_more (0);
9610 
9611 	  fragP = frag_now;
9612 
9613 	  frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9614 		    ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9615 		    NULL, 0, p);
9616 
9617 	  fragP->tc_frag_data.mf_type = mf_cmp;
9618 	  fragP->tc_frag_data.branch_type = align_branch_fused;
9619 	  fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9620 	}
9621       else if (add_branch_prefix_frag_p ())
9622 	{
9623 	  unsigned int max_prefix_size = align_branch_prefix_size;
9624 
9625 	  /* Make room for padding.  */
9626 	  frag_grow (max_prefix_size);
9627 	  p = frag_more (0);
9628 
9629 	  fragP = frag_now;
9630 
9631 	  frag_var (rs_machine_dependent, max_prefix_size, 0,
9632 		    ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9633 		    NULL, 0, p);
9634 
9635 	  fragP->tc_frag_data.max_bytes = max_prefix_size;
9636 	}
9637 
9638       /* Since the VEX/EVEX prefix contains the implicit prefix, we
9639 	 don't need the explicit prefix.  */
9640       if (!is_any_vex_encoding (&i.tm))
9641 	{
9642 	  switch (i.tm.opcode_modifier.opcodeprefix)
9643 	    {
9644 	    case PREFIX_0X66:
9645 	      add_prefix (0x66);
9646 	      break;
9647 	    case PREFIX_0XF2:
9648 	      add_prefix (0xf2);
9649 	      break;
9650 	    case PREFIX_0XF3:
9651 	      if (!i.tm.cpu_flags.bitfield.cpupadlock
9652 		  || (i.prefix[REP_PREFIX] != 0xf3))
9653 		add_prefix (0xf3);
9654 	      break;
9655 	    case PREFIX_NONE:
9656 	      switch (i.opcode_length)
9657 		{
9658 		case 2:
9659 		  break;
9660 		case 1:
9661 		  /* Check for pseudo prefixes.  */
9662 		  if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9663 		    break;
9664 		  as_bad_where (insn_start_frag->fr_file,
9665 				insn_start_frag->fr_line,
9666 				_("pseudo prefix without instruction"));
9667 		  return;
9668 		default:
9669 		  abort ();
9670 		}
9671 	      break;
9672 	    default:
9673 	      abort ();
9674 	    }
9675 
9676 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9677 	  /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9678 	     R_X86_64_GOTTPOFF relocation so that linker can safely
9679 	     perform IE->LE optimization.  A dummy REX_OPCODE prefix
9680 	     is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9681 	     relocation for GDesc -> IE/LE optimization.  */
9682 	  if (x86_elf_abi == X86_64_X32_ABI
9683 	      && i.operands == 2
9684 	      && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9685 		  || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
9686 	      && i.prefix[REX_PREFIX] == 0)
9687 	    add_prefix (REX_OPCODE);
9688 #endif
9689 
9690 	  /* The prefix bytes.  */
9691 	  for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9692 	    if (*q)
9693 	      frag_opcode_byte (*q);
9694 	}
9695       else
9696 	{
9697 	  for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9698 	    if (*q)
9699 	      switch (j)
9700 		{
9701 		case SEG_PREFIX:
9702 		case ADDR_PREFIX:
9703 		  frag_opcode_byte (*q);
9704 		  break;
9705 		default:
9706 		  /* There should be no other prefixes for instructions
9707 		     with VEX prefix.  */
9708 		  abort ();
9709 		}
9710 
9711 	  /* For EVEX instructions i.vrex should become 0 after
9712 	     build_evex_prefix.  For VEX instructions upper 16 registers
9713 	     aren't available, so VREX should be 0.  */
9714 	  if (i.vrex)
9715 	    abort ();
9716 	  /* Now the VEX prefix.  */
9717 	  if (now_seg != absolute_section)
9718 	    {
9719 	      p = frag_more (i.vex.length);
9720 	      for (j = 0; j < i.vex.length; j++)
9721 		p[j] = i.vex.bytes[j];
9722 	    }
9723 	  else
9724 	    abs_section_offset += i.vex.length;
9725 	}
9726 
9727       /* Now the opcode; be careful about word order here!  */
9728       j = i.opcode_length;
9729       if (!i.vex.length)
9730 	switch (i.tm.opcode_modifier.opcodespace)
9731 	  {
9732 	  case SPACE_BASE:
9733 	    break;
9734 	  case SPACE_0F:
9735 	    ++j;
9736 	    break;
9737 	  case SPACE_0F38:
9738 	  case SPACE_0F3A:
9739 	    j += 2;
9740 	    break;
9741 	  default:
9742 	    abort ();
9743 	  }
9744 
9745       if (now_seg == absolute_section)
9746 	abs_section_offset += j;
9747       else if (j == 1)
9748 	{
9749 	  FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9750 	}
9751       else
9752 	{
9753 	  p = frag_more (j);
9754 	  if (!i.vex.length
9755 	      && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9756 	    {
9757 	      *p++ = 0x0f;
9758 	      if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9759 		*p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9760 		       ? 0x38 : 0x3a;
9761 	    }
9762 
9763 	  switch (i.opcode_length)
9764 	    {
9765 	    case 2:
9766 	      /* Put out high byte first: can't use md_number_to_chars!  */
9767 	      *p++ = (i.tm.base_opcode >> 8) & 0xff;
9768 	      /* Fall through.  */
9769 	    case 1:
9770 	      *p = i.tm.base_opcode & 0xff;
9771 	      break;
9772 	    default:
9773 	      abort ();
9774 	      break;
9775 	    }
9776 
9777 	}
9778 
9779       /* Now the modrm byte and sib byte (if present).  */
9780       if (i.tm.opcode_modifier.modrm)
9781 	{
9782 	  frag_opcode_byte ((i.rm.regmem << 0)
9783 			     | (i.rm.reg << 3)
9784 			     | (i.rm.mode << 6));
9785 	  /* If i.rm.regmem == ESP (4)
9786 	     && i.rm.mode != (Register mode)
9787 	     && not 16 bit
9788 	     ==> need second modrm byte.  */
9789 	  if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9790 	      && i.rm.mode != 3
9791 	      && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
9792 	    frag_opcode_byte ((i.sib.base << 0)
9793 			      | (i.sib.index << 3)
9794 			      | (i.sib.scale << 6));
9795 	}
9796 
9797       if (i.disp_operands)
9798 	output_disp (insn_start_frag, insn_start_off);
9799 
9800       if (i.imm_operands)
9801 	output_imm (insn_start_frag, insn_start_off);
9802 
9803       /*
9804        * frag_now_fix () returning plain abs_section_offset when we're in the
9805        * absolute section, and abs_section_offset not getting updated as data
9806        * gets added to the frag breaks the logic below.
9807        */
9808       if (now_seg != absolute_section)
9809 	{
9810 	  j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9811 	  if (j > 15)
9812 	    as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9813 		     j);
9814 	  else if (fragP)
9815 	    {
9816 	      /* NB: Don't add prefix with GOTPC relocation since
9817 		 output_disp() above depends on the fixed encoding
9818 		 length.  Can't add prefix with TLS relocation since
9819 		 it breaks TLS linker optimization.  */
9820 	      unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9821 	      /* Prefix count on the current instruction.  */
9822 	      unsigned int count = i.vex.length;
9823 	      unsigned int k;
9824 	      for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9825 		/* REX byte is encoded in VEX/EVEX prefix.  */
9826 		if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9827 		  count++;
9828 
9829 	      /* Count prefixes for extended opcode maps.  */
9830 	      if (!i.vex.length)
9831 		switch (i.tm.opcode_modifier.opcodespace)
9832 		  {
9833 		  case SPACE_BASE:
9834 		    break;
9835 		  case SPACE_0F:
9836 		    count++;
9837 		    break;
9838 		  case SPACE_0F38:
9839 		  case SPACE_0F3A:
9840 		    count += 2;
9841 		    break;
9842 		  default:
9843 		    abort ();
9844 		  }
9845 
9846 	      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9847 		  == BRANCH_PREFIX)
9848 		{
9849 		  /* Set the maximum prefix size in BRANCH_PREFIX
9850 		     frag.  */
9851 		  if (fragP->tc_frag_data.max_bytes > max)
9852 		    fragP->tc_frag_data.max_bytes = max;
9853 		  if (fragP->tc_frag_data.max_bytes > count)
9854 		    fragP->tc_frag_data.max_bytes -= count;
9855 		  else
9856 		    fragP->tc_frag_data.max_bytes = 0;
9857 		}
9858 	      else
9859 		{
9860 		  /* Remember the maximum prefix size in FUSED_JCC_PADDING
9861 		     frag.  */
9862 		  unsigned int max_prefix_size;
9863 		  if (align_branch_prefix_size > max)
9864 		    max_prefix_size = max;
9865 		  else
9866 		    max_prefix_size = align_branch_prefix_size;
9867 		  if (max_prefix_size > count)
9868 		    fragP->tc_frag_data.max_prefix_length
9869 		      = max_prefix_size - count;
9870 		}
9871 
9872 	      /* Use existing segment prefix if possible.  Use CS
9873 		 segment prefix in 64-bit mode.  In 32-bit mode, use SS
9874 		 segment prefix with ESP/EBP base register and use DS
9875 		 segment prefix without ESP/EBP base register.  */
9876 	      if (i.prefix[SEG_PREFIX])
9877 		fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9878 	      else if (flag_code == CODE_64BIT)
9879 		fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9880 	      else if (i.base_reg
9881 		       && (i.base_reg->reg_num == 4
9882 			   || i.base_reg->reg_num == 5))
9883 		fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9884 	      else
9885 		fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9886 	    }
9887 	}
9888     }
9889 
9890   /* NB: Don't work with COND_JUMP86 without i386.  */
9891   if (align_branch_power
9892       && now_seg != absolute_section
9893       && cpu_arch_flags.bitfield.cpui386)
9894     {
9895       /* Terminate each frag so that we can add prefix and check for
9896          fused jcc.  */
9897       frag_wane (frag_now);
9898       frag_new (0);
9899     }
9900 
9901 #ifdef DEBUG386
9902   if (flag_debug)
9903     {
9904       pi ("" /*line*/, &i);
9905     }
9906 #endif /* DEBUG386  */
9907 }
9908 
9909 /* Return the size of the displacement operand N.  */
9910 
9911 static int
disp_size(unsigned int n)9912 disp_size (unsigned int n)
9913 {
9914   int size = 4;
9915 
9916   if (i.types[n].bitfield.disp64)
9917     size = 8;
9918   else if (i.types[n].bitfield.disp8)
9919     size = 1;
9920   else if (i.types[n].bitfield.disp16)
9921     size = 2;
9922   return size;
9923 }
9924 
9925 /* Return the size of the immediate operand N.  */
9926 
9927 static int
imm_size(unsigned int n)9928 imm_size (unsigned int n)
9929 {
9930   int size = 4;
9931   if (i.types[n].bitfield.imm64)
9932     size = 8;
9933   else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9934     size = 1;
9935   else if (i.types[n].bitfield.imm16)
9936     size = 2;
9937   return size;
9938 }
9939 
9940 static void
output_disp(fragS * insn_start_frag,offsetT insn_start_off)9941 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
9942 {
9943   char *p;
9944   unsigned int n;
9945 
9946   for (n = 0; n < i.operands; n++)
9947     {
9948       if (operand_type_check (i.types[n], disp))
9949 	{
9950 	  int size = disp_size (n);
9951 
9952 	  if (now_seg == absolute_section)
9953 	    abs_section_offset += size;
9954 	  else if (i.op[n].disps->X_op == O_constant)
9955 	    {
9956 	      offsetT val = i.op[n].disps->X_add_number;
9957 
9958 	      val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9959 				     size);
9960 	      p = frag_more (size);
9961 	      md_number_to_chars (p, val, size);
9962 	    }
9963 	  else
9964 	    {
9965 	      enum bfd_reloc_code_real reloc_type;
9966 	      bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9967 	      bool sign = (flag_code == CODE_64BIT && size == 4
9968 			   && (!want_disp32 (&i.tm)
9969 			       || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9970 				   && !i.types[n].bitfield.baseindex)))
9971 			  || pcrel;
9972 	      fixS *fixP;
9973 
9974 	      /* We can't have 8 bit displacement here.  */
9975 	      gas_assert (!i.types[n].bitfield.disp8);
9976 
9977 	      /* The PC relative address is computed relative
9978 		 to the instruction boundary, so in case immediate
9979 		 fields follows, we need to adjust the value.  */
9980 	      if (pcrel && i.imm_operands)
9981 		{
9982 		  unsigned int n1;
9983 		  int sz = 0;
9984 
9985 		  for (n1 = 0; n1 < i.operands; n1++)
9986 		    if (operand_type_check (i.types[n1], imm))
9987 		      {
9988 			/* Only one immediate is allowed for PC
9989 			   relative address.  */
9990 			gas_assert (sz == 0);
9991 			sz = imm_size (n1);
9992 			i.op[n].disps->X_add_number -= sz;
9993 		      }
9994 		  /* We should find the immediate.  */
9995 		  gas_assert (sz != 0);
9996 		}
9997 
9998 	      p = frag_more (size);
9999 	      reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10000 	      if (GOT_symbol
10001 		  && GOT_symbol == i.op[n].disps->X_add_symbol
10002 		  && (((reloc_type == BFD_RELOC_32
10003 			|| reloc_type == BFD_RELOC_X86_64_32S
10004 			|| (reloc_type == BFD_RELOC_64
10005 			    && object_64bit))
10006 		       && (i.op[n].disps->X_op == O_symbol
10007 			   || (i.op[n].disps->X_op == O_add
10008 			       && ((symbol_get_value_expression
10009 				    (i.op[n].disps->X_op_symbol)->X_op)
10010 				   == O_subtract))))
10011 		      || reloc_type == BFD_RELOC_32_PCREL))
10012 		{
10013 		  if (!object_64bit)
10014 		    {
10015 		      reloc_type = BFD_RELOC_386_GOTPC;
10016 		      i.has_gotpc_tls_reloc = true;
10017 		      i.op[n].disps->X_add_number +=
10018 			encoding_length (insn_start_frag, insn_start_off, p);
10019 		    }
10020 		  else if (reloc_type == BFD_RELOC_64)
10021 		    reloc_type = BFD_RELOC_X86_64_GOTPC64;
10022 		  else
10023 		    /* Don't do the adjustment for x86-64, as there
10024 		       the pcrel addressing is relative to the _next_
10025 		       insn, and that is taken care of in other code.  */
10026 		    reloc_type = BFD_RELOC_X86_64_GOTPC32;
10027 		}
10028 	      else if (align_branch_power)
10029 		{
10030 		  switch (reloc_type)
10031 		    {
10032 		    case BFD_RELOC_386_TLS_GD:
10033 		    case BFD_RELOC_386_TLS_LDM:
10034 		    case BFD_RELOC_386_TLS_IE:
10035 		    case BFD_RELOC_386_TLS_IE_32:
10036 		    case BFD_RELOC_386_TLS_GOTIE:
10037 		    case BFD_RELOC_386_TLS_GOTDESC:
10038 		    case BFD_RELOC_386_TLS_DESC_CALL:
10039 		    case BFD_RELOC_X86_64_TLSGD:
10040 		    case BFD_RELOC_X86_64_TLSLD:
10041 		    case BFD_RELOC_X86_64_GOTTPOFF:
10042 		    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10043 		    case BFD_RELOC_X86_64_TLSDESC_CALL:
10044 		      i.has_gotpc_tls_reloc = true;
10045 		    default:
10046 		      break;
10047 		    }
10048 		}
10049 	      fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10050 				  size, i.op[n].disps, pcrel,
10051 				  reloc_type);
10052 
10053 	      if (flag_code == CODE_64BIT && size == 4 && pcrel
10054 		  && !i.prefix[ADDR_PREFIX])
10055 		fixP->fx_signed = 1;
10056 
10057 	      /* Check for "call/jmp *mem", "mov mem, %reg",
10058 		 "test %reg, mem" and "binop mem, %reg" where binop
10059 		 is one of adc, add, and, cmp, or, sbb, sub, xor
10060 		 instructions without data prefix.  Always generate
10061 		 R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
10062 	      if (i.prefix[DATA_PREFIX] == 0
10063 		  && (generate_relax_relocations
10064 		      || (!object_64bit
10065 			  && i.rm.mode == 0
10066 			  && i.rm.regmem == 5))
10067 		  && (i.rm.mode == 2
10068 		      || (i.rm.mode == 0 && i.rm.regmem == 5))
10069 		  && i.tm.opcode_modifier.opcodespace == SPACE_BASE
10070 		  && ((i.operands == 1
10071 		       && i.tm.base_opcode == 0xff
10072 		       && (i.rm.reg == 2 || i.rm.reg == 4))
10073 		      || (i.operands == 2
10074 			  && (i.tm.base_opcode == 0x8b
10075 			      || i.tm.base_opcode == 0x85
10076 			      || (i.tm.base_opcode & ~0x38) == 0x03))))
10077 		{
10078 		  if (object_64bit)
10079 		    {
10080 		      fixP->fx_tcbit = i.rex != 0;
10081 		      if (i.base_reg
10082 			  && (i.base_reg->reg_num == RegIP))
10083 		      fixP->fx_tcbit2 = 1;
10084 		    }
10085 		  else
10086 		    fixP->fx_tcbit2 = 1;
10087 		}
10088 	    }
10089 	}
10090     }
10091 }
10092 
10093 static void
output_imm(fragS * insn_start_frag,offsetT insn_start_off)10094 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10095 {
10096   char *p;
10097   unsigned int n;
10098 
10099   for (n = 0; n < i.operands; n++)
10100     {
10101       if (operand_type_check (i.types[n], imm))
10102 	{
10103 	  int size = imm_size (n);
10104 
10105 	  if (now_seg == absolute_section)
10106 	    abs_section_offset += size;
10107 	  else if (i.op[n].imms->X_op == O_constant)
10108 	    {
10109 	      offsetT val;
10110 
10111 	      val = offset_in_range (i.op[n].imms->X_add_number,
10112 				     size);
10113 	      p = frag_more (size);
10114 	      md_number_to_chars (p, val, size);
10115 	    }
10116 	  else
10117 	    {
10118 	      /* Not absolute_section.
10119 		 Need a 32-bit fixup (don't support 8bit
10120 		 non-absolute imms).  Try to support other
10121 		 sizes ...  */
10122 	      enum bfd_reloc_code_real reloc_type;
10123 	      int sign;
10124 
10125 	      if (i.types[n].bitfield.imm32s
10126 		  && (i.suffix == QWORD_MNEM_SUFFIX
10127 		      || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
10128 		sign = 1;
10129 	      else
10130 		sign = 0;
10131 
10132 	      p = frag_more (size);
10133 	      reloc_type = reloc (size, 0, sign, i.reloc[n]);
10134 
10135 	      /*   This is tough to explain.  We end up with this one if we
10136 	       * have operands that look like
10137 	       * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
10138 	       * obtain the absolute address of the GOT, and it is strongly
10139 	       * preferable from a performance point of view to avoid using
10140 	       * a runtime relocation for this.  The actual sequence of
10141 	       * instructions often look something like:
10142 	       *
10143 	       *	call	.L66
10144 	       * .L66:
10145 	       *	popl	%ebx
10146 	       *	addl	$_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10147 	       *
10148 	       *   The call and pop essentially return the absolute address
10149 	       * of the label .L66 and store it in %ebx.  The linker itself
10150 	       * will ultimately change the first operand of the addl so
10151 	       * that %ebx points to the GOT, but to keep things simple, the
10152 	       * .o file must have this operand set so that it generates not
10153 	       * the absolute address of .L66, but the absolute address of
10154 	       * itself.  This allows the linker itself simply treat a GOTPC
10155 	       * relocation as asking for a pcrel offset to the GOT to be
10156 	       * added in, and the addend of the relocation is stored in the
10157 	       * operand field for the instruction itself.
10158 	       *
10159 	       *   Our job here is to fix the operand so that it would add
10160 	       * the correct offset so that %ebx would point to itself.  The
10161 	       * thing that is tricky is that .-.L66 will point to the
10162 	       * beginning of the instruction, so we need to further modify
10163 	       * the operand so that it will point to itself.  There are
10164 	       * other cases where you have something like:
10165 	       *
10166 	       *	.long	$_GLOBAL_OFFSET_TABLE_+[.-.L66]
10167 	       *
10168 	       * and here no correction would be required.  Internally in
10169 	       * the assembler we treat operands of this form as not being
10170 	       * pcrel since the '.' is explicitly mentioned, and I wonder
10171 	       * whether it would simplify matters to do it this way.  Who
10172 	       * knows.  In earlier versions of the PIC patches, the
10173 	       * pcrel_adjust field was used to store the correction, but
10174 	       * since the expression is not pcrel, I felt it would be
10175 	       * confusing to do it this way.  */
10176 
10177 	      if ((reloc_type == BFD_RELOC_32
10178 		   || reloc_type == BFD_RELOC_X86_64_32S
10179 		   || reloc_type == BFD_RELOC_64)
10180 		  && GOT_symbol
10181 		  && GOT_symbol == i.op[n].imms->X_add_symbol
10182 		  && (i.op[n].imms->X_op == O_symbol
10183 		      || (i.op[n].imms->X_op == O_add
10184 			  && ((symbol_get_value_expression
10185 			       (i.op[n].imms->X_op_symbol)->X_op)
10186 			      == O_subtract))))
10187 		{
10188 		  if (!object_64bit)
10189 		    reloc_type = BFD_RELOC_386_GOTPC;
10190 		  else if (size == 4)
10191 		    reloc_type = BFD_RELOC_X86_64_GOTPC32;
10192 		  else if (size == 8)
10193 		    reloc_type = BFD_RELOC_X86_64_GOTPC64;
10194 		  i.has_gotpc_tls_reloc = true;
10195 		  i.op[n].imms->X_add_number +=
10196 		    encoding_length (insn_start_frag, insn_start_off, p);
10197 		}
10198 	      fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10199 			   i.op[n].imms, 0, reloc_type);
10200 	    }
10201 	}
10202     }
10203 }
10204 
10205 /* x86_cons_fix_new is called via the expression parsing code when a
10206    reloc is needed.  We use this hook to get the correct .got reloc.  */
10207 static int cons_sign = -1;
10208 
10209 void
x86_cons_fix_new(fragS * frag,unsigned int off,unsigned int len,expressionS * exp,bfd_reloc_code_real_type r)10210 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10211 		  expressionS *exp, bfd_reloc_code_real_type r)
10212 {
10213   r = reloc (len, 0, cons_sign, r);
10214 
10215 #ifdef TE_PE
10216   if (exp->X_op == O_secrel)
10217     {
10218       exp->X_op = O_symbol;
10219       r = BFD_RELOC_32_SECREL;
10220     }
10221   else if (exp->X_op == O_secidx)
10222     r = BFD_RELOC_16_SECIDX;
10223 #endif
10224 
10225   fix_new_exp (frag, off, len, exp, 0, r);
10226 }
10227 
10228 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10229    purpose of the `.dc.a' internal pseudo-op.  */
10230 
10231 int
x86_address_bytes(void)10232 x86_address_bytes (void)
10233 {
10234   if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10235     return 4;
10236   return stdoutput->arch_info->bits_per_address / 8;
10237 }
10238 
10239 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10240      || defined (LEX_AT)) && !defined (TE_PE)
10241 # define lex_got(reloc, adjust, types) NULL
10242 #else
10243 /* Parse operands of the form
10244    <symbol>@GOTOFF+<nnn>
10245    and similar .plt or .got references.
10246 
10247    If we find one, set up the correct relocation in RELOC and copy the
10248    input string, minus the `@GOTOFF' into a malloc'd buffer for
10249    parsing by the calling routine.  Return this buffer, and if ADJUST
10250    is non-null set it to the length of the string we removed from the
10251    input line.  Otherwise return NULL.  */
10252 static char *
lex_got(enum bfd_reloc_code_real * rel,int * adjust,i386_operand_type * types)10253 lex_got (enum bfd_reloc_code_real *rel,
10254 	 int *adjust,
10255 	 i386_operand_type *types)
10256 {
10257   /* Some of the relocations depend on the size of what field is to
10258      be relocated.  But in our callers i386_immediate and i386_displacement
10259      we don't yet know the operand size (this will be set by insn
10260      matching).  Hence we record the word32 relocation here,
10261      and adjust the reloc according to the real size in reloc().  */
10262   static const struct
10263   {
10264     const char *str;
10265     int len;
10266     const enum bfd_reloc_code_real rel[2];
10267     const i386_operand_type types64;
10268     bool need_GOT_symbol;
10269   }
10270     gotrel[] =
10271   {
10272 #ifndef TE_PE
10273 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10274     { STRING_COMMA_LEN ("SIZE"),      { BFD_RELOC_SIZE32,
10275 					BFD_RELOC_SIZE32 },
10276       OPERAND_TYPE_IMM32_64, false },
10277 #endif
10278     { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
10279 				       BFD_RELOC_X86_64_PLTOFF64 },
10280       OPERAND_TYPE_IMM64, true },
10281     { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
10282 				       BFD_RELOC_X86_64_PLT32    },
10283       OPERAND_TYPE_IMM32_32S_DISP32, false },
10284     { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
10285 				       BFD_RELOC_X86_64_GOTPLT64 },
10286       OPERAND_TYPE_IMM64_DISP64, true },
10287     { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_386_GOTOFF,
10288 				       BFD_RELOC_X86_64_GOTOFF64 },
10289       OPERAND_TYPE_IMM64_DISP64, true },
10290     { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10291 				       BFD_RELOC_X86_64_GOTPCREL },
10292       OPERAND_TYPE_IMM32_32S_DISP32, true },
10293     { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
10294 				       BFD_RELOC_X86_64_TLSGD    },
10295       OPERAND_TYPE_IMM32_32S_DISP32, true },
10296     { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
10297 				       _dummy_first_bfd_reloc_code_real },
10298       OPERAND_TYPE_NONE, true },
10299     { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
10300 				       BFD_RELOC_X86_64_TLSLD    },
10301       OPERAND_TYPE_IMM32_32S_DISP32, true },
10302     { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10303 				       BFD_RELOC_X86_64_GOTTPOFF },
10304       OPERAND_TYPE_IMM32_32S_DISP32, true },
10305     { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
10306 				       BFD_RELOC_X86_64_TPOFF32  },
10307       OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10308     { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
10309 				       _dummy_first_bfd_reloc_code_real },
10310       OPERAND_TYPE_NONE, true },
10311     { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
10312 				       BFD_RELOC_X86_64_DTPOFF32 },
10313       OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10314     { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10315 				       _dummy_first_bfd_reloc_code_real },
10316       OPERAND_TYPE_NONE, true },
10317     { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10318 				       _dummy_first_bfd_reloc_code_real },
10319       OPERAND_TYPE_NONE, true },
10320     { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
10321 				       BFD_RELOC_X86_64_GOT32    },
10322       OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10323     { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
10324 				       BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10325       OPERAND_TYPE_IMM32_32S_DISP32, true },
10326     { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
10327 				       BFD_RELOC_X86_64_TLSDESC_CALL },
10328       OPERAND_TYPE_IMM32_32S_DISP32, true },
10329 #else /* TE_PE */
10330     { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10331 				       BFD_RELOC_32_SECREL },
10332       OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10333 #endif
10334   };
10335   char *cp;
10336   unsigned int j;
10337 
10338 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10339   if (!IS_ELF)
10340     return NULL;
10341 #endif
10342 
10343   for (cp = input_line_pointer; *cp != '@'; cp++)
10344     if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10345       return NULL;
10346 
10347   for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10348     {
10349       int len = gotrel[j].len;
10350       if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10351 	{
10352 	  if (gotrel[j].rel[object_64bit] != 0)
10353 	    {
10354 	      int first, second;
10355 	      char *tmpbuf, *past_reloc;
10356 
10357 	      *rel = gotrel[j].rel[object_64bit];
10358 
10359 	      if (types)
10360 		{
10361 		  if (flag_code != CODE_64BIT)
10362 		    {
10363 		      types->bitfield.imm32 = 1;
10364 		      types->bitfield.disp32 = 1;
10365 		    }
10366 		  else
10367 		    *types = gotrel[j].types64;
10368 		}
10369 
10370 	      if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10371 		GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10372 
10373 	      /* The length of the first part of our input line.  */
10374 	      first = cp - input_line_pointer;
10375 
10376 	      /* The second part goes from after the reloc token until
10377 		 (and including) an end_of_line char or comma.  */
10378 	      past_reloc = cp + 1 + len;
10379 	      cp = past_reloc;
10380 	      while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10381 		++cp;
10382 	      second = cp + 1 - past_reloc;
10383 
10384 	      /* Allocate and copy string.  The trailing NUL shouldn't
10385 		 be necessary, but be safe.  */
10386 	      tmpbuf = XNEWVEC (char, first + second + 2);
10387 	      memcpy (tmpbuf, input_line_pointer, first);
10388 	      if (second != 0 && *past_reloc != ' ')
10389 		/* Replace the relocation token with ' ', so that
10390 		   errors like foo@GOTOFF1 will be detected.  */
10391 		tmpbuf[first++] = ' ';
10392 	      else
10393 		/* Increment length by 1 if the relocation token is
10394 		   removed.  */
10395 		len++;
10396 	      if (adjust)
10397 		*adjust = len;
10398 	      memcpy (tmpbuf + first, past_reloc, second);
10399 	      tmpbuf[first + second] = '\0';
10400 	      return tmpbuf;
10401 	    }
10402 
10403 	  as_bad (_("@%s reloc is not supported with %d-bit output format"),
10404 		  gotrel[j].str, 1 << (5 + object_64bit));
10405 	  return NULL;
10406 	}
10407     }
10408 
10409   /* Might be a symbol version string.  Don't as_bad here.  */
10410   return NULL;
10411 }
10412 #endif
10413 
10414 bfd_reloc_code_real_type
x86_cons(expressionS * exp,int size)10415 x86_cons (expressionS *exp, int size)
10416 {
10417   bfd_reloc_code_real_type got_reloc = NO_RELOC;
10418 
10419 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10420       && !defined (LEX_AT)) \
10421     || defined (TE_PE)
10422   intel_syntax = -intel_syntax;
10423 
10424   exp->X_md = 0;
10425   if (size == 4 || (object_64bit && size == 8))
10426     {
10427       /* Handle @GOTOFF and the like in an expression.  */
10428       char *save;
10429       char *gotfree_input_line;
10430       int adjust = 0;
10431 
10432       save = input_line_pointer;
10433       gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10434       if (gotfree_input_line)
10435 	input_line_pointer = gotfree_input_line;
10436 
10437       expression (exp);
10438 
10439       if (gotfree_input_line)
10440 	{
10441 	  /* expression () has merrily parsed up to the end of line,
10442 	     or a comma - in the wrong buffer.  Transfer how far
10443 	     input_line_pointer has moved to the right buffer.  */
10444 	  input_line_pointer = (save
10445 				+ (input_line_pointer - gotfree_input_line)
10446 				+ adjust);
10447 	  free (gotfree_input_line);
10448 	  if (exp->X_op == O_constant
10449 	      || exp->X_op == O_absent
10450 	      || exp->X_op == O_illegal
10451 	      || exp->X_op == O_register
10452 	      || exp->X_op == O_big)
10453 	    {
10454 	      char c = *input_line_pointer;
10455 	      *input_line_pointer = 0;
10456 	      as_bad (_("missing or invalid expression `%s'"), save);
10457 	      *input_line_pointer = c;
10458 	    }
10459 	  else if ((got_reloc == BFD_RELOC_386_PLT32
10460 		    || got_reloc == BFD_RELOC_X86_64_PLT32)
10461 		   && exp->X_op != O_symbol)
10462 	    {
10463 	      char c = *input_line_pointer;
10464 	      *input_line_pointer = 0;
10465 	      as_bad (_("invalid PLT expression `%s'"), save);
10466 	      *input_line_pointer = c;
10467 	    }
10468 	}
10469     }
10470   else
10471     expression (exp);
10472 
10473   intel_syntax = -intel_syntax;
10474 
10475   if (intel_syntax)
10476     i386_intel_simplify (exp);
10477 #else
10478   expression (exp);
10479 #endif
10480 
10481   /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
10482   if (size == 4 && exp->X_op == O_constant && !object_64bit)
10483     exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10484 
10485   return got_reloc;
10486 }
10487 
10488 static void
signed_cons(int size)10489 signed_cons (int size)
10490 {
10491   if (object_64bit)
10492     cons_sign = 1;
10493   cons (size);
10494   cons_sign = -1;
10495 }
10496 
10497 #ifdef TE_PE
10498 static void
pe_directive_secrel(int dummy ATTRIBUTE_UNUSED)10499 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
10500 {
10501   expressionS exp;
10502 
10503   do
10504     {
10505       expression (&exp);
10506       if (exp.X_op == O_symbol)
10507 	exp.X_op = O_secrel;
10508 
10509       emit_expr (&exp, 4);
10510     }
10511   while (*input_line_pointer++ == ',');
10512 
10513   input_line_pointer--;
10514   demand_empty_rest_of_line ();
10515 }
10516 
10517 static void
pe_directive_secidx(int dummy ATTRIBUTE_UNUSED)10518 pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10519 {
10520   expressionS exp;
10521 
10522   do
10523     {
10524       expression (&exp);
10525       if (exp.X_op == O_symbol)
10526 	exp.X_op = O_secidx;
10527 
10528       emit_expr (&exp, 2);
10529     }
10530   while (*input_line_pointer++ == ',');
10531 
10532   input_line_pointer--;
10533   demand_empty_rest_of_line ();
10534 }
10535 #endif
10536 
10537 /* Handle Rounding Control / SAE specifiers.  */
10538 
10539 static char *
RC_SAE_specifier(const char * pstr)10540 RC_SAE_specifier (const char *pstr)
10541 {
10542   unsigned int j;
10543 
10544   for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10545     {
10546       if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10547 	{
10548 	  if (i.rounding.type != rc_none)
10549 	    {
10550 	      as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10551 	      return NULL;
10552 	    }
10553 
10554 	  i.rounding.type = RC_NamesTable[j].type;
10555 
10556 	  return (char *)(pstr + RC_NamesTable[j].len);
10557 	}
10558     }
10559 
10560   return NULL;
10561 }
10562 
10563 /* Handle Vector operations.  */
10564 
10565 static char *
check_VecOperations(char * op_string)10566 check_VecOperations (char *op_string)
10567 {
10568   const reg_entry *mask;
10569   const char *saved;
10570   char *end_op;
10571 
10572   while (*op_string)
10573     {
10574       saved = op_string;
10575       if (*op_string == '{')
10576 	{
10577 	  op_string++;
10578 
10579 	  /* Check broadcasts.  */
10580 	  if (startswith (op_string, "1to"))
10581 	    {
10582 	      unsigned int bcst_type;
10583 
10584 	      if (i.broadcast.type)
10585 		goto duplicated_vec_op;
10586 
10587 	      op_string += 3;
10588 	      if (*op_string == '8')
10589 		bcst_type = 8;
10590 	      else if (*op_string == '4')
10591 		bcst_type = 4;
10592 	      else if (*op_string == '2')
10593 		bcst_type = 2;
10594 	      else if (*op_string == '1'
10595 		       && *(op_string+1) == '6')
10596 		{
10597 		  bcst_type = 16;
10598 		  op_string++;
10599 		}
10600 	      else if (*op_string == '3'
10601 		       && *(op_string+1) == '2')
10602 		{
10603 		  bcst_type = 32;
10604 		  op_string++;
10605 		}
10606 	      else
10607 		{
10608 		  as_bad (_("Unsupported broadcast: `%s'"), saved);
10609 		  return NULL;
10610 		}
10611 	      op_string++;
10612 
10613 	      i.broadcast.type = bcst_type;
10614 	      i.broadcast.operand = this_operand;
10615 	    }
10616 	  /* Check masking operation.  */
10617 	  else if ((mask = parse_register (op_string, &end_op)) != NULL)
10618 	    {
10619 	      if (mask == &bad_reg)
10620 		return NULL;
10621 
10622 	      /* k0 can't be used for write mask.  */
10623 	      if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
10624 		{
10625 		  as_bad (_("`%s%s' can't be used for write mask"),
10626 			  register_prefix, mask->reg_name);
10627 		  return NULL;
10628 		}
10629 
10630 	      if (!i.mask.reg)
10631 		{
10632 		  i.mask.reg = mask;
10633 		  i.mask.operand = this_operand;
10634 		}
10635 	      else if (i.mask.reg->reg_num)
10636 		goto duplicated_vec_op;
10637 	      else
10638 		{
10639 		  i.mask.reg = mask;
10640 
10641 		  /* Only "{z}" is allowed here.  No need to check
10642 		     zeroing mask explicitly.  */
10643 		  if (i.mask.operand != (unsigned int) this_operand)
10644 		    {
10645 		      as_bad (_("invalid write mask `%s'"), saved);
10646 		      return NULL;
10647 		    }
10648 		}
10649 
10650 	      op_string = end_op;
10651 	    }
10652 	  /* Check zeroing-flag for masking operation.  */
10653 	  else if (*op_string == 'z')
10654 	    {
10655 	      if (!i.mask.reg)
10656 		{
10657 		  i.mask.reg = reg_k0;
10658 		  i.mask.zeroing = 1;
10659 		  i.mask.operand = this_operand;
10660 		}
10661 	      else
10662 		{
10663 		  if (i.mask.zeroing)
10664 		    {
10665 		    duplicated_vec_op:
10666 		      as_bad (_("duplicated `%s'"), saved);
10667 		      return NULL;
10668 		    }
10669 
10670 		  i.mask.zeroing = 1;
10671 
10672 		  /* Only "{%k}" is allowed here.  No need to check mask
10673 		     register explicitly.  */
10674 		  if (i.mask.operand != (unsigned int) this_operand)
10675 		    {
10676 		      as_bad (_("invalid zeroing-masking `%s'"),
10677 			      saved);
10678 		      return NULL;
10679 		    }
10680 		}
10681 
10682 	      op_string++;
10683 	    }
10684 	  else if (intel_syntax
10685 		   && (op_string = RC_SAE_specifier (op_string)) != NULL)
10686 	    i.rounding.modifier = true;
10687 	  else
10688 	    goto unknown_vec_op;
10689 
10690 	  if (*op_string != '}')
10691 	    {
10692 	      as_bad (_("missing `}' in `%s'"), saved);
10693 	      return NULL;
10694 	    }
10695 	  op_string++;
10696 
10697 	  /* Strip whitespace since the addition of pseudo prefixes
10698 	     changed how the scrubber treats '{'.  */
10699 	  if (is_space_char (*op_string))
10700 	    ++op_string;
10701 
10702 	  continue;
10703 	}
10704     unknown_vec_op:
10705       /* We don't know this one.  */
10706       as_bad (_("unknown vector operation: `%s'"), saved);
10707       return NULL;
10708     }
10709 
10710   if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
10711     {
10712       as_bad (_("zeroing-masking only allowed with write mask"));
10713       return NULL;
10714     }
10715 
10716   return op_string;
10717 }
10718 
10719 static int
i386_immediate(char * imm_start)10720 i386_immediate (char *imm_start)
10721 {
10722   char *save_input_line_pointer;
10723   char *gotfree_input_line;
10724   segT exp_seg = 0;
10725   expressionS *exp;
10726   i386_operand_type types;
10727 
10728   operand_type_set (&types, ~0);
10729 
10730   if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10731     {
10732       as_bad (_("at most %d immediate operands are allowed"),
10733 	      MAX_IMMEDIATE_OPERANDS);
10734       return 0;
10735     }
10736 
10737   exp = &im_expressions[i.imm_operands++];
10738   i.op[this_operand].imms = exp;
10739 
10740   if (is_space_char (*imm_start))
10741     ++imm_start;
10742 
10743   save_input_line_pointer = input_line_pointer;
10744   input_line_pointer = imm_start;
10745 
10746   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
10747   if (gotfree_input_line)
10748     input_line_pointer = gotfree_input_line;
10749 
10750   exp_seg = expression (exp);
10751 
10752   SKIP_WHITESPACE ();
10753   if (*input_line_pointer)
10754     as_bad (_("junk `%s' after expression"), input_line_pointer);
10755 
10756   input_line_pointer = save_input_line_pointer;
10757   if (gotfree_input_line)
10758     {
10759       free (gotfree_input_line);
10760 
10761       if (exp->X_op == O_constant)
10762 	exp->X_op = O_illegal;
10763     }
10764 
10765   if (exp_seg == reg_section)
10766     {
10767       as_bad (_("illegal immediate register operand %s"), imm_start);
10768       return 0;
10769     }
10770 
10771   return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10772 }
10773 
10774 static int
i386_finalize_immediate(segT exp_seg ATTRIBUTE_UNUSED,expressionS * exp,i386_operand_type types,const char * imm_start)10775 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10776 			 i386_operand_type types, const char *imm_start)
10777 {
10778   if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
10779     {
10780       if (imm_start)
10781 	as_bad (_("missing or invalid immediate expression `%s'"),
10782 		imm_start);
10783       return 0;
10784     }
10785   else if (exp->X_op == O_constant)
10786     {
10787       /* Size it properly later.  */
10788       i.types[this_operand].bitfield.imm64 = 1;
10789 
10790       /* If not 64bit, sign/zero extend val, to account for wraparound
10791 	 when !BFD64.  */
10792       if (flag_code != CODE_64BIT)
10793 	exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10794     }
10795 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
10796   else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
10797 	   && exp_seg != absolute_section
10798 	   && exp_seg != text_section
10799 	   && exp_seg != data_section
10800 	   && exp_seg != bss_section
10801 	   && exp_seg != undefined_section
10802 	   && !bfd_is_com_section (exp_seg))
10803     {
10804       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
10805       return 0;
10806     }
10807 #endif
10808   else
10809     {
10810       /* This is an address.  The size of the address will be
10811 	 determined later, depending on destination register,
10812 	 suffix, or the default for the section.  */
10813       i.types[this_operand].bitfield.imm8 = 1;
10814       i.types[this_operand].bitfield.imm16 = 1;
10815       i.types[this_operand].bitfield.imm32 = 1;
10816       i.types[this_operand].bitfield.imm32s = 1;
10817       i.types[this_operand].bitfield.imm64 = 1;
10818       i.types[this_operand] = operand_type_and (i.types[this_operand],
10819 						types);
10820     }
10821 
10822   return 1;
10823 }
10824 
10825 static char *
i386_scale(char * scale)10826 i386_scale (char *scale)
10827 {
10828   offsetT val;
10829   char *save = input_line_pointer;
10830 
10831   input_line_pointer = scale;
10832   val = get_absolute_expression ();
10833 
10834   switch (val)
10835     {
10836     case 1:
10837       i.log2_scale_factor = 0;
10838       break;
10839     case 2:
10840       i.log2_scale_factor = 1;
10841       break;
10842     case 4:
10843       i.log2_scale_factor = 2;
10844       break;
10845     case 8:
10846       i.log2_scale_factor = 3;
10847       break;
10848     default:
10849       {
10850 	char sep = *input_line_pointer;
10851 
10852 	*input_line_pointer = '\0';
10853 	as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10854 		scale);
10855 	*input_line_pointer = sep;
10856 	input_line_pointer = save;
10857 	return NULL;
10858       }
10859     }
10860   if (i.log2_scale_factor != 0 && i.index_reg == 0)
10861     {
10862       as_warn (_("scale factor of %d without an index register"),
10863 	       1 << i.log2_scale_factor);
10864       i.log2_scale_factor = 0;
10865     }
10866   scale = input_line_pointer;
10867   input_line_pointer = save;
10868   return scale;
10869 }
10870 
10871 static int
i386_displacement(char * disp_start,char * disp_end)10872 i386_displacement (char *disp_start, char *disp_end)
10873 {
10874   expressionS *exp;
10875   segT exp_seg = 0;
10876   char *save_input_line_pointer;
10877   char *gotfree_input_line;
10878   int override;
10879   i386_operand_type bigdisp, types = anydisp;
10880   int ret;
10881 
10882   if (i.disp_operands == MAX_MEMORY_OPERANDS)
10883     {
10884       as_bad (_("at most %d displacement operands are allowed"),
10885 	      MAX_MEMORY_OPERANDS);
10886       return 0;
10887     }
10888 
10889   operand_type_set (&bigdisp, 0);
10890   if (i.jumpabsolute
10891       || i.types[this_operand].bitfield.baseindex
10892       || (current_templates->start->opcode_modifier.jump != JUMP
10893 	  && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
10894     {
10895       i386_addressing_mode ();
10896       override = (i.prefix[ADDR_PREFIX] != 0);
10897       if (flag_code == CODE_64BIT)
10898 	{
10899 	  bigdisp.bitfield.disp32 = 1;
10900 	  if (!override)
10901 	    bigdisp.bitfield.disp64 = 1;
10902 	}
10903       else if ((flag_code == CODE_16BIT) ^ override)
10904 	  bigdisp.bitfield.disp16 = 1;
10905       else
10906 	  bigdisp.bitfield.disp32 = 1;
10907     }
10908   else
10909     {
10910       /* For PC-relative branches, the width of the displacement may be
10911 	 dependent upon data size, but is never dependent upon address size.
10912 	 Also make sure to not unintentionally match against a non-PC-relative
10913 	 branch template.  */
10914       static templates aux_templates;
10915       const insn_template *t = current_templates->start;
10916       bool has_intel64 = false;
10917 
10918       aux_templates.start = t;
10919       while (++t < current_templates->end)
10920 	{
10921 	  if (t->opcode_modifier.jump
10922 	      != current_templates->start->opcode_modifier.jump)
10923 	    break;
10924 	  if ((t->opcode_modifier.isa64 >= INTEL64))
10925 	    has_intel64 = true;
10926 	}
10927       if (t < current_templates->end)
10928 	{
10929 	  aux_templates.end = t;
10930 	  current_templates = &aux_templates;
10931 	}
10932 
10933       override = (i.prefix[DATA_PREFIX] != 0);
10934       if (flag_code == CODE_64BIT)
10935 	{
10936 	  if ((override || i.suffix == WORD_MNEM_SUFFIX)
10937 	      && (!intel64 || !has_intel64))
10938 	    bigdisp.bitfield.disp16 = 1;
10939 	  else
10940 	    bigdisp.bitfield.disp32 = 1;
10941 	}
10942       else
10943 	{
10944 	  if (!override)
10945 	    override = (i.suffix == (flag_code != CODE_16BIT
10946 				     ? WORD_MNEM_SUFFIX
10947 				     : LONG_MNEM_SUFFIX));
10948 	  bigdisp.bitfield.disp32 = 1;
10949 	  if ((flag_code == CODE_16BIT) ^ override)
10950 	    {
10951 	      bigdisp.bitfield.disp32 = 0;
10952 	      bigdisp.bitfield.disp16 = 1;
10953 	    }
10954 	}
10955     }
10956   i.types[this_operand] = operand_type_or (i.types[this_operand],
10957 					   bigdisp);
10958 
10959   exp = &disp_expressions[i.disp_operands];
10960   i.op[this_operand].disps = exp;
10961   i.disp_operands++;
10962   save_input_line_pointer = input_line_pointer;
10963   input_line_pointer = disp_start;
10964   END_STRING_AND_SAVE (disp_end);
10965 
10966 #ifndef GCC_ASM_O_HACK
10967 #define GCC_ASM_O_HACK 0
10968 #endif
10969 #if GCC_ASM_O_HACK
10970   END_STRING_AND_SAVE (disp_end + 1);
10971   if (i.types[this_operand].bitfield.baseIndex
10972       && displacement_string_end[-1] == '+')
10973     {
10974       /* This hack is to avoid a warning when using the "o"
10975 	 constraint within gcc asm statements.
10976 	 For instance:
10977 
10978 	 #define _set_tssldt_desc(n,addr,limit,type) \
10979 	 __asm__ __volatile__ ( \
10980 	 "movw %w2,%0\n\t" \
10981 	 "movw %w1,2+%0\n\t" \
10982 	 "rorl $16,%1\n\t" \
10983 	 "movb %b1,4+%0\n\t" \
10984 	 "movb %4,5+%0\n\t" \
10985 	 "movb $0,6+%0\n\t" \
10986 	 "movb %h1,7+%0\n\t" \
10987 	 "rorl $16,%1" \
10988 	 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10989 
10990 	 This works great except that the output assembler ends
10991 	 up looking a bit weird if it turns out that there is
10992 	 no offset.  You end up producing code that looks like:
10993 
10994 	 #APP
10995 	 movw $235,(%eax)
10996 	 movw %dx,2+(%eax)
10997 	 rorl $16,%edx
10998 	 movb %dl,4+(%eax)
10999 	 movb $137,5+(%eax)
11000 	 movb $0,6+(%eax)
11001 	 movb %dh,7+(%eax)
11002 	 rorl $16,%edx
11003 	 #NO_APP
11004 
11005 	 So here we provide the missing zero.  */
11006 
11007       *displacement_string_end = '0';
11008     }
11009 #endif
11010   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11011   if (gotfree_input_line)
11012     input_line_pointer = gotfree_input_line;
11013 
11014   exp_seg = expression (exp);
11015 
11016   SKIP_WHITESPACE ();
11017   if (*input_line_pointer)
11018     as_bad (_("junk `%s' after expression"), input_line_pointer);
11019 #if GCC_ASM_O_HACK
11020   RESTORE_END_STRING (disp_end + 1);
11021 #endif
11022   input_line_pointer = save_input_line_pointer;
11023   if (gotfree_input_line)
11024     {
11025       free (gotfree_input_line);
11026 
11027       if (exp->X_op == O_constant || exp->X_op == O_register)
11028 	exp->X_op = O_illegal;
11029     }
11030 
11031   ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11032 
11033   RESTORE_END_STRING (disp_end);
11034 
11035   return ret;
11036 }
11037 
11038 static int
i386_finalize_displacement(segT exp_seg ATTRIBUTE_UNUSED,expressionS * exp,i386_operand_type types,const char * disp_start)11039 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11040 			    i386_operand_type types, const char *disp_start)
11041 {
11042   int ret = 1;
11043 
11044   /* We do this to make sure that the section symbol is in
11045      the symbol table.  We will ultimately change the relocation
11046      to be relative to the beginning of the section.  */
11047   if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
11048       || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11049       || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11050     {
11051       if (exp->X_op != O_symbol)
11052 	goto inv_disp;
11053 
11054       if (S_IS_LOCAL (exp->X_add_symbol)
11055 	  && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11056 	  && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
11057 	section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
11058       exp->X_op = O_subtract;
11059       exp->X_op_symbol = GOT_symbol;
11060       if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
11061 	i.reloc[this_operand] = BFD_RELOC_32_PCREL;
11062       else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11063 	i.reloc[this_operand] = BFD_RELOC_64;
11064       else
11065 	i.reloc[this_operand] = BFD_RELOC_32;
11066     }
11067 
11068   else if (exp->X_op == O_absent
11069 	   || exp->X_op == O_illegal
11070 	   || exp->X_op == O_big)
11071     {
11072     inv_disp:
11073       as_bad (_("missing or invalid displacement expression `%s'"),
11074 	      disp_start);
11075       ret = 0;
11076     }
11077 
11078   else if (exp->X_op == O_constant)
11079     {
11080       /* Sizing gets taken care of by optimize_disp().
11081 
11082 	 If not 64bit, sign/zero extend val, to account for wraparound
11083 	 when !BFD64.  */
11084       if (flag_code != CODE_64BIT)
11085 	exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11086     }
11087 
11088 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11089   else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
11090 	   && exp_seg != absolute_section
11091 	   && exp_seg != text_section
11092 	   && exp_seg != data_section
11093 	   && exp_seg != bss_section
11094 	   && exp_seg != undefined_section
11095 	   && !bfd_is_com_section (exp_seg))
11096     {
11097       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
11098       ret = 0;
11099     }
11100 #endif
11101 
11102   else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
11103     i.types[this_operand].bitfield.disp8 = 1;
11104 
11105   /* Check if this is a displacement only operand.  */
11106   if (!i.types[this_operand].bitfield.baseindex)
11107     i.types[this_operand] =
11108       operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11109 		       operand_type_and (i.types[this_operand], types));
11110 
11111   return ret;
11112 }
11113 
11114 /* Return the active addressing mode, taking address override and
11115    registers forming the address into consideration.  Update the
11116    address override prefix if necessary.  */
11117 
11118 static enum flag_code
i386_addressing_mode(void)11119 i386_addressing_mode (void)
11120 {
11121   enum flag_code addr_mode;
11122 
11123   if (i.prefix[ADDR_PREFIX])
11124     addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
11125   else if (flag_code == CODE_16BIT
11126 	   && current_templates->start->cpu_flags.bitfield.cpumpx
11127 	   /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11128 	      from md_assemble() by "is not a valid base/index expression"
11129 	      when there is a base and/or index.  */
11130 	   && !i.types[this_operand].bitfield.baseindex)
11131     {
11132       /* MPX insn memory operands with neither base nor index must be forced
11133 	 to use 32-bit addressing in 16-bit mode.  */
11134       addr_mode = CODE_32BIT;
11135       i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11136       ++i.prefixes;
11137       gas_assert (!i.types[this_operand].bitfield.disp16);
11138       gas_assert (!i.types[this_operand].bitfield.disp32);
11139     }
11140   else
11141     {
11142       addr_mode = flag_code;
11143 
11144 #if INFER_ADDR_PREFIX
11145       if (i.mem_operands == 0)
11146 	{
11147 	  /* Infer address prefix from the first memory operand.  */
11148 	  const reg_entry *addr_reg = i.base_reg;
11149 
11150 	  if (addr_reg == NULL)
11151 	    addr_reg = i.index_reg;
11152 
11153 	  if (addr_reg)
11154 	    {
11155 	      if (addr_reg->reg_type.bitfield.dword)
11156 		addr_mode = CODE_32BIT;
11157 	      else if (flag_code != CODE_64BIT
11158 		       && addr_reg->reg_type.bitfield.word)
11159 		addr_mode = CODE_16BIT;
11160 
11161 	      if (addr_mode != flag_code)
11162 		{
11163 		  i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11164 		  i.prefixes += 1;
11165 		  /* Change the size of any displacement too.  At most one
11166 		     of Disp16 or Disp32 is set.
11167 		     FIXME.  There doesn't seem to be any real need for
11168 		     separate Disp16 and Disp32 flags.  The same goes for
11169 		     Imm16 and Imm32.  Removing them would probably clean
11170 		     up the code quite a lot.  */
11171 		  if (flag_code != CODE_64BIT
11172 		      && (i.types[this_operand].bitfield.disp16
11173 			  || i.types[this_operand].bitfield.disp32))
11174 		    i.types[this_operand]
11175 		      = operand_type_xor (i.types[this_operand], disp16_32);
11176 		}
11177 	    }
11178 	}
11179 #endif
11180     }
11181 
11182   return addr_mode;
11183 }
11184 
11185 /* Make sure the memory operand we've been dealt is valid.
11186    Return 1 on success, 0 on a failure.  */
11187 
11188 static int
i386_index_check(const char * operand_string)11189 i386_index_check (const char *operand_string)
11190 {
11191   const char *kind = "base/index";
11192   enum flag_code addr_mode = i386_addressing_mode ();
11193   const insn_template *t = current_templates->start;
11194 
11195   if (t->opcode_modifier.isstring
11196       && !t->cpu_flags.bitfield.cpupadlock
11197       && (current_templates->end[-1].opcode_modifier.isstring
11198 	  || i.mem_operands))
11199     {
11200       /* Memory operands of string insns are special in that they only allow
11201 	 a single register (rDI, rSI, or rBX) as their memory address.  */
11202       const reg_entry *expected_reg;
11203       static const char *di_si[][2] =
11204 	{
11205 	  { "esi", "edi" },
11206 	  { "si", "di" },
11207 	  { "rsi", "rdi" }
11208 	};
11209       static const char *bx[] = { "ebx", "bx", "rbx" };
11210 
11211       kind = "string address";
11212 
11213       if (t->opcode_modifier.prefixok == PrefixRep)
11214 	{
11215 	  int es_op = current_templates->end[-1].opcode_modifier.isstring
11216 		      - IS_STRING_ES_OP0;
11217 	  int op = 0;
11218 
11219 	  if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
11220 	      || ((!i.mem_operands != !intel_syntax)
11221 		  && current_templates->end[-1].operand_types[1]
11222 		     .bitfield.baseindex))
11223 	    op = 1;
11224 	  expected_reg
11225 	    = (const reg_entry *) str_hash_find (reg_hash,
11226 						 di_si[addr_mode][op == es_op]);
11227 	}
11228       else
11229 	expected_reg
11230 	  = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
11231 
11232       if (i.base_reg != expected_reg
11233 	  || i.index_reg
11234 	  || operand_type_check (i.types[this_operand], disp))
11235 	{
11236 	  /* The second memory operand must have the same size as
11237 	     the first one.  */
11238 	  if (i.mem_operands
11239 	      && i.base_reg
11240 	      && !((addr_mode == CODE_64BIT
11241 		    && i.base_reg->reg_type.bitfield.qword)
11242 		   || (addr_mode == CODE_32BIT
11243 		       ? i.base_reg->reg_type.bitfield.dword
11244 		       : i.base_reg->reg_type.bitfield.word)))
11245 	    goto bad_address;
11246 
11247 	  as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11248 		   operand_string,
11249 		   intel_syntax ? '[' : '(',
11250 		   register_prefix,
11251 		   expected_reg->reg_name,
11252 		   intel_syntax ? ']' : ')');
11253 	  return 1;
11254 	}
11255       else
11256 	return 1;
11257 
11258     bad_address:
11259       as_bad (_("`%s' is not a valid %s expression"),
11260 	      operand_string, kind);
11261       return 0;
11262     }
11263   else
11264     {
11265       if (addr_mode != CODE_16BIT)
11266 	{
11267 	  /* 32-bit/64-bit checks.  */
11268 	  if (i.disp_encoding == disp_encoding_16bit)
11269 	    {
11270 	    bad_disp:
11271 	      as_bad (_("invalid `%s' prefix"),
11272 		      addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11273 	      return 0;
11274 	    }
11275 
11276 	  if ((i.base_reg
11277 	       && ((addr_mode == CODE_64BIT
11278 		    ? !i.base_reg->reg_type.bitfield.qword
11279 		    : !i.base_reg->reg_type.bitfield.dword)
11280 		   || (i.index_reg && i.base_reg->reg_num == RegIP)
11281 		   || i.base_reg->reg_num == RegIZ))
11282 	      || (i.index_reg
11283 		  && !i.index_reg->reg_type.bitfield.xmmword
11284 		  && !i.index_reg->reg_type.bitfield.ymmword
11285 		  && !i.index_reg->reg_type.bitfield.zmmword
11286 		  && ((addr_mode == CODE_64BIT
11287 		       ? !i.index_reg->reg_type.bitfield.qword
11288 		       : !i.index_reg->reg_type.bitfield.dword)
11289 		      || !i.index_reg->reg_type.bitfield.baseindex)))
11290 	    goto bad_address;
11291 
11292 	  /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
11293 	  if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
11294 	       && t->opcode_modifier.opcodespace == SPACE_0F
11295 	       && t->base_opcode == 0x1b)
11296 	      || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11297 		  && t->opcode_modifier.opcodespace == SPACE_0F
11298 		  && (t->base_opcode & ~1) == 0x1a)
11299 	      || t->opcode_modifier.sib == SIBMEM)
11300 	    {
11301 	      /* They cannot use RIP-relative addressing. */
11302 	      if (i.base_reg && i.base_reg->reg_num == RegIP)
11303 		{
11304 		  as_bad (_("`%s' cannot be used here"), operand_string);
11305 		  return 0;
11306 		}
11307 
11308 	      /* bndldx and bndstx ignore their scale factor. */
11309 	      if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11310 		  && t->opcode_modifier.opcodespace == SPACE_0F
11311 		  && (t->base_opcode & ~1) == 0x1a
11312 		  && i.log2_scale_factor)
11313 		as_warn (_("register scaling is being ignored here"));
11314 	    }
11315 	}
11316       else
11317 	{
11318 	  /* 16-bit checks.  */
11319 	  if (i.disp_encoding == disp_encoding_32bit)
11320 	    goto bad_disp;
11321 
11322 	  if ((i.base_reg
11323 	       && (!i.base_reg->reg_type.bitfield.word
11324 		   || !i.base_reg->reg_type.bitfield.baseindex))
11325 	      || (i.index_reg
11326 		  && (!i.index_reg->reg_type.bitfield.word
11327 		      || !i.index_reg->reg_type.bitfield.baseindex
11328 		      || !(i.base_reg
11329 			   && i.base_reg->reg_num < 6
11330 			   && i.index_reg->reg_num >= 6
11331 			   && i.log2_scale_factor == 0))))
11332 	    goto bad_address;
11333 	}
11334     }
11335   return 1;
11336 }
11337 
11338 /* Handle vector immediates.  */
11339 
11340 static int
RC_SAE_immediate(const char * imm_start)11341 RC_SAE_immediate (const char *imm_start)
11342 {
11343   const char *pstr = imm_start;
11344 
11345   if (*pstr != '{')
11346     return 0;
11347 
11348   pstr = RC_SAE_specifier (pstr + 1);
11349   if (pstr == NULL)
11350     return 0;
11351 
11352   if (*pstr++ != '}')
11353     {
11354       as_bad (_("Missing '}': '%s'"), imm_start);
11355       return 0;
11356     }
11357   /* RC/SAE immediate string should contain nothing more.  */;
11358   if (*pstr != 0)
11359     {
11360       as_bad (_("Junk after '}': '%s'"), imm_start);
11361       return 0;
11362     }
11363 
11364   /* Internally this doesn't count as an operand.  */
11365   --i.operands;
11366 
11367   return 1;
11368 }
11369 
11370 /* Only string instructions can have a second memory operand, so
11371    reduce current_templates to just those if it contains any.  */
11372 static int
maybe_adjust_templates(void)11373 maybe_adjust_templates (void)
11374 {
11375   const insn_template *t;
11376 
11377   gas_assert (i.mem_operands == 1);
11378 
11379   for (t = current_templates->start; t < current_templates->end; ++t)
11380     if (t->opcode_modifier.isstring)
11381       break;
11382 
11383   if (t < current_templates->end)
11384     {
11385       static templates aux_templates;
11386       bool recheck;
11387 
11388       aux_templates.start = t;
11389       for (; t < current_templates->end; ++t)
11390 	if (!t->opcode_modifier.isstring)
11391 	  break;
11392       aux_templates.end = t;
11393 
11394       /* Determine whether to re-check the first memory operand.  */
11395       recheck = (aux_templates.start != current_templates->start
11396 		 || t != current_templates->end);
11397 
11398       current_templates = &aux_templates;
11399 
11400       if (recheck)
11401 	{
11402 	  i.mem_operands = 0;
11403 	  if (i.memop1_string != NULL
11404 	      && i386_index_check (i.memop1_string) == 0)
11405 	    return 0;
11406 	  i.mem_operands = 1;
11407 	}
11408     }
11409 
11410   return 1;
11411 }
11412 
starts_memory_operand(char c)11413 static INLINE bool starts_memory_operand (char c)
11414 {
11415   return ISDIGIT (c)
11416 	 || is_identifier_char (c)
11417 	 || strchr ("([\"+-!~", c);
11418 }
11419 
11420 /* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
11421    on error.  */
11422 
11423 static int
i386_att_operand(char * operand_string)11424 i386_att_operand (char *operand_string)
11425 {
11426   const reg_entry *r;
11427   char *end_op;
11428   char *op_string = operand_string;
11429 
11430   if (is_space_char (*op_string))
11431     ++op_string;
11432 
11433   /* We check for an absolute prefix (differentiating,
11434      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
11435   if (*op_string == ABSOLUTE_PREFIX)
11436     {
11437       ++op_string;
11438       if (is_space_char (*op_string))
11439 	++op_string;
11440       i.jumpabsolute = true;
11441     }
11442 
11443   /* Check if operand is a register.  */
11444   if ((r = parse_register (op_string, &end_op)) != NULL)
11445     {
11446       i386_operand_type temp;
11447 
11448       if (r == &bad_reg)
11449 	return 0;
11450 
11451       /* Check for a segment override by searching for ':' after a
11452 	 segment register.  */
11453       op_string = end_op;
11454       if (is_space_char (*op_string))
11455 	++op_string;
11456       if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
11457 	{
11458 	  i.seg[i.mem_operands] = r;
11459 
11460 	  /* Skip the ':' and whitespace.  */
11461 	  ++op_string;
11462 	  if (is_space_char (*op_string))
11463 	    ++op_string;
11464 
11465 	  /* Handle case of %es:*foo.  */
11466 	  if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
11467 	    {
11468 	      ++op_string;
11469 	      if (is_space_char (*op_string))
11470 		++op_string;
11471 	      i.jumpabsolute = true;
11472 	    }
11473 
11474 	  if (!starts_memory_operand (*op_string))
11475 	    {
11476 	      as_bad (_("bad memory operand `%s'"), op_string);
11477 	      return 0;
11478 	    }
11479 	  goto do_memory_reference;
11480 	}
11481 
11482       /* Handle vector operations.  */
11483       if (*op_string == '{')
11484 	{
11485 	  op_string = check_VecOperations (op_string);
11486 	  if (op_string == NULL)
11487 	    return 0;
11488 	}
11489 
11490       if (*op_string)
11491 	{
11492 	  as_bad (_("junk `%s' after register"), op_string);
11493 	  return 0;
11494 	}
11495       temp = r->reg_type;
11496       temp.bitfield.baseindex = 0;
11497       i.types[this_operand] = operand_type_or (i.types[this_operand],
11498 					       temp);
11499       i.types[this_operand].bitfield.unspecified = 0;
11500       i.op[this_operand].regs = r;
11501       i.reg_operands++;
11502 
11503       /* A GPR may follow an RC or SAE immediate only if a (vector) register
11504          operand was also present earlier on.  */
11505       if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11506           && i.reg_operands == 1)
11507 	{
11508 	  unsigned int j;
11509 
11510 	  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11511 	    if (i.rounding.type == RC_NamesTable[j].type)
11512 	      break;
11513 	  as_bad (_("`%s': misplaced `{%s}'"),
11514 		  current_templates->start->name, RC_NamesTable[j].name);
11515 	  return 0;
11516 	}
11517     }
11518   else if (*op_string == REGISTER_PREFIX)
11519     {
11520       as_bad (_("bad register name `%s'"), op_string);
11521       return 0;
11522     }
11523   else if (*op_string == IMMEDIATE_PREFIX)
11524     {
11525       ++op_string;
11526       if (i.jumpabsolute)
11527 	{
11528 	  as_bad (_("immediate operand illegal with absolute jump"));
11529 	  return 0;
11530 	}
11531       if (!i386_immediate (op_string))
11532 	return 0;
11533       if (i.rounding.type != rc_none)
11534 	{
11535 	  as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11536 		  current_templates->start->name);
11537 	  return 0;
11538 	}
11539     }
11540   else if (RC_SAE_immediate (operand_string))
11541     {
11542       /* If it is a RC or SAE immediate, do the necessary placement check:
11543 	 Only another immediate or a GPR may precede it.  */
11544       if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11545 	  || (i.reg_operands == 1
11546 	      && i.op[0].regs->reg_type.bitfield.class != Reg))
11547 	{
11548 	  as_bad (_("`%s': misplaced `%s'"),
11549 		  current_templates->start->name, operand_string);
11550 	  return 0;
11551 	}
11552     }
11553   else if (starts_memory_operand (*op_string))
11554     {
11555       /* This is a memory reference of some sort.  */
11556       char *base_string;
11557 
11558       /* Start and end of displacement string expression (if found).  */
11559       char *displacement_string_start;
11560       char *displacement_string_end;
11561 
11562     do_memory_reference:
11563       if (i.mem_operands == 1 && !maybe_adjust_templates ())
11564 	return 0;
11565       if ((i.mem_operands == 1
11566 	   && !current_templates->start->opcode_modifier.isstring)
11567 	  || i.mem_operands == 2)
11568 	{
11569 	  as_bad (_("too many memory references for `%s'"),
11570 		  current_templates->start->name);
11571 	  return 0;
11572 	}
11573 
11574       /* Check for base index form.  We detect the base index form by
11575 	 looking for an ')' at the end of the operand, searching
11576 	 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11577 	 after the '('.  */
11578       base_string = op_string + strlen (op_string);
11579 
11580       /* Handle vector operations.  */
11581       --base_string;
11582       if (is_space_char (*base_string))
11583 	--base_string;
11584 
11585       if (*base_string == '}')
11586 	{
11587 	  char *vop_start = NULL;
11588 
11589 	  while (base_string-- > op_string)
11590 	    {
11591 	      if (*base_string == '"')
11592 		break;
11593 	      if (*base_string != '{')
11594 		continue;
11595 
11596 	      vop_start = base_string;
11597 
11598 	      --base_string;
11599 	      if (is_space_char (*base_string))
11600 		--base_string;
11601 
11602 	      if (*base_string != '}')
11603 		break;
11604 
11605 	      vop_start = NULL;
11606 	    }
11607 
11608 	  if (!vop_start)
11609 	    {
11610 	      as_bad (_("unbalanced figure braces"));
11611 	      return 0;
11612 	    }
11613 
11614 	  if (check_VecOperations (vop_start) == NULL)
11615 	    return 0;
11616 	}
11617 
11618       /* If we only have a displacement, set-up for it to be parsed later.  */
11619       displacement_string_start = op_string;
11620       displacement_string_end = base_string + 1;
11621 
11622       if (*base_string == ')')
11623 	{
11624 	  char *temp_string;
11625 	  unsigned int parens_not_balanced = 1;
11626 
11627 	  /* We've already checked that the number of left & right ()'s are
11628 	     equal, so this loop will not be infinite.  */
11629 	  do
11630 	    {
11631 	      base_string--;
11632 	      if (*base_string == ')')
11633 		parens_not_balanced++;
11634 	      if (*base_string == '(')
11635 		parens_not_balanced--;
11636 	    }
11637 	  while (parens_not_balanced && *base_string != '"');
11638 
11639 	  temp_string = base_string;
11640 
11641 	  /* Skip past '(' and whitespace.  */
11642 	  if (*base_string == '(')
11643 	    ++base_string;
11644 	  if (is_space_char (*base_string))
11645 	    ++base_string;
11646 
11647 	  if (*base_string == ','
11648 	      || ((i.base_reg = parse_register (base_string, &end_op))
11649 		  != NULL))
11650 	    {
11651 	      displacement_string_end = temp_string;
11652 
11653 	      i.types[this_operand].bitfield.baseindex = 1;
11654 
11655 	      if (i.base_reg)
11656 		{
11657 		  if (i.base_reg == &bad_reg)
11658 		    return 0;
11659 		  base_string = end_op;
11660 		  if (is_space_char (*base_string))
11661 		    ++base_string;
11662 		}
11663 
11664 	      /* There may be an index reg or scale factor here.  */
11665 	      if (*base_string == ',')
11666 		{
11667 		  ++base_string;
11668 		  if (is_space_char (*base_string))
11669 		    ++base_string;
11670 
11671 		  if ((i.index_reg = parse_register (base_string, &end_op))
11672 		      != NULL)
11673 		    {
11674 		      if (i.index_reg == &bad_reg)
11675 			return 0;
11676 		      base_string = end_op;
11677 		      if (is_space_char (*base_string))
11678 			++base_string;
11679 		      if (*base_string == ',')
11680 			{
11681 			  ++base_string;
11682 			  if (is_space_char (*base_string))
11683 			    ++base_string;
11684 			}
11685 		      else if (*base_string != ')')
11686 			{
11687 			  as_bad (_("expecting `,' or `)' "
11688 				    "after index register in `%s'"),
11689 				  operand_string);
11690 			  return 0;
11691 			}
11692 		    }
11693 		  else if (*base_string == REGISTER_PREFIX)
11694 		    {
11695 		      end_op = strchr (base_string, ',');
11696 		      if (end_op)
11697 			*end_op = '\0';
11698 		      as_bad (_("bad register name `%s'"), base_string);
11699 		      return 0;
11700 		    }
11701 
11702 		  /* Check for scale factor.  */
11703 		  if (*base_string != ')')
11704 		    {
11705 		      char *end_scale = i386_scale (base_string);
11706 
11707 		      if (!end_scale)
11708 			return 0;
11709 
11710 		      base_string = end_scale;
11711 		      if (is_space_char (*base_string))
11712 			++base_string;
11713 		      if (*base_string != ')')
11714 			{
11715 			  as_bad (_("expecting `)' "
11716 				    "after scale factor in `%s'"),
11717 				  operand_string);
11718 			  return 0;
11719 			}
11720 		    }
11721 		  else if (!i.index_reg)
11722 		    {
11723 		      as_bad (_("expecting index register or scale factor "
11724 				"after `,'; got '%c'"),
11725 			      *base_string);
11726 		      return 0;
11727 		    }
11728 		}
11729 	      else if (*base_string != ')')
11730 		{
11731 		  as_bad (_("expecting `,' or `)' "
11732 			    "after base register in `%s'"),
11733 			  operand_string);
11734 		  return 0;
11735 		}
11736 	    }
11737 	  else if (*base_string == REGISTER_PREFIX)
11738 	    {
11739 	      end_op = strchr (base_string, ',');
11740 	      if (end_op)
11741 		*end_op = '\0';
11742 	      as_bad (_("bad register name `%s'"), base_string);
11743 	      return 0;
11744 	    }
11745 	}
11746 
11747       /* If there's an expression beginning the operand, parse it,
11748 	 assuming displacement_string_start and
11749 	 displacement_string_end are meaningful.  */
11750       if (displacement_string_start != displacement_string_end)
11751 	{
11752 	  if (!i386_displacement (displacement_string_start,
11753 				  displacement_string_end))
11754 	    return 0;
11755 	}
11756 
11757       /* Special case for (%dx) while doing input/output op.  */
11758       if (i.base_reg
11759 	  && i.base_reg->reg_type.bitfield.instance == RegD
11760 	  && i.base_reg->reg_type.bitfield.word
11761 	  && i.index_reg == 0
11762 	  && i.log2_scale_factor == 0
11763 	  && i.seg[i.mem_operands] == 0
11764 	  && !operand_type_check (i.types[this_operand], disp))
11765 	{
11766 	  i.types[this_operand] = i.base_reg->reg_type;
11767 	  return 1;
11768 	}
11769 
11770       if (i386_index_check (operand_string) == 0)
11771 	return 0;
11772       i.flags[this_operand] |= Operand_Mem;
11773       if (i.mem_operands == 0)
11774 	i.memop1_string = xstrdup (operand_string);
11775       i.mem_operands++;
11776     }
11777   else
11778     {
11779       /* It's not a memory operand; argh!  */
11780       as_bad (_("invalid char %s beginning operand %d `%s'"),
11781 	      output_invalid (*op_string),
11782 	      this_operand + 1,
11783 	      op_string);
11784       return 0;
11785     }
11786   return 1;			/* Normal return.  */
11787 }
11788 
11789 /* Calculate the maximum variable size (i.e., excluding fr_fix)
11790    that an rs_machine_dependent frag may reach.  */
11791 
11792 unsigned int
i386_frag_max_var(fragS * frag)11793 i386_frag_max_var (fragS *frag)
11794 {
11795   /* The only relaxable frags are for jumps.
11796      Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
11797   gas_assert (frag->fr_type == rs_machine_dependent);
11798   return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11799 }
11800 
11801 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11802 static int
elf_symbol_resolved_in_segment_p(symbolS * fr_symbol,offsetT fr_var)11803 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
11804 {
11805   /* STT_GNU_IFUNC symbol must go through PLT.  */
11806   if ((symbol_get_bfdsym (fr_symbol)->flags
11807        & BSF_GNU_INDIRECT_FUNCTION) != 0)
11808     return 0;
11809 
11810   if (!S_IS_EXTERNAL (fr_symbol))
11811     /* Symbol may be weak or local.  */
11812     return !S_IS_WEAK (fr_symbol);
11813 
11814   /* Global symbols with non-default visibility can't be preempted. */
11815   if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11816     return 1;
11817 
11818   if (fr_var != NO_RELOC)
11819     switch ((enum bfd_reloc_code_real) fr_var)
11820       {
11821       case BFD_RELOC_386_PLT32:
11822       case BFD_RELOC_X86_64_PLT32:
11823 	/* Symbol with PLT relocation may be preempted. */
11824 	return 0;
11825       default:
11826 	abort ();
11827       }
11828 
11829   /* Global symbols with default visibility in a shared library may be
11830      preempted by another definition.  */
11831   return !shared;
11832 }
11833 #endif
11834 
11835 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11836    Note also work for Skylake and Cascadelake.
11837 ---------------------------------------------------------------------
11838 |   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
11839 | ------  | ----------- | ------- | -------- |
11840 |   Jo    |      N      |    N    |     Y    |
11841 |   Jno   |      N      |    N    |     Y    |
11842 |  Jc/Jb  |      Y      |    N    |     Y    |
11843 | Jae/Jnb |      Y      |    N    |     Y    |
11844 |  Je/Jz  |      Y      |    Y    |     Y    |
11845 | Jne/Jnz |      Y      |    Y    |     Y    |
11846 | Jna/Jbe |      Y      |    N    |     Y    |
11847 | Ja/Jnbe |      Y      |    N    |     Y    |
11848 |   Js    |      N      |    N    |     Y    |
11849 |   Jns   |      N      |    N    |     Y    |
11850 |  Jp/Jpe |      N      |    N    |     Y    |
11851 | Jnp/Jpo |      N      |    N    |     Y    |
11852 | Jl/Jnge |      Y      |    Y    |     Y    |
11853 | Jge/Jnl |      Y      |    Y    |     Y    |
11854 | Jle/Jng |      Y      |    Y    |     Y    |
11855 | Jg/Jnle |      Y      |    Y    |     Y    |
11856 ---------------------------------------------------------------------  */
11857 static int
i386_macro_fusible_p(enum mf_cmp_kind mf_cmp,enum mf_jcc_kind mf_jcc)11858 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11859 {
11860   if (mf_cmp == mf_cmp_alu_cmp)
11861     return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11862 	    || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11863   if (mf_cmp == mf_cmp_incdec)
11864     return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11865 	    || mf_jcc == mf_jcc_jle);
11866   if (mf_cmp == mf_cmp_test_and)
11867     return 1;
11868   return 0;
11869 }
11870 
11871 /* Return the next non-empty frag.  */
11872 
11873 static fragS *
i386_next_non_empty_frag(fragS * fragP)11874 i386_next_non_empty_frag (fragS *fragP)
11875 {
11876   /* There may be a frag with a ".fill 0" when there is no room in
11877      the current frag for frag_grow in output_insn.  */
11878   for (fragP = fragP->fr_next;
11879        (fragP != NULL
11880 	&& fragP->fr_type == rs_fill
11881 	&& fragP->fr_fix == 0);
11882        fragP = fragP->fr_next)
11883     ;
11884   return fragP;
11885 }
11886 
11887 /* Return the next jcc frag after BRANCH_PADDING.  */
11888 
11889 static fragS *
i386_next_fusible_jcc_frag(fragS * maybe_cmp_fragP,fragS * pad_fragP)11890 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
11891 {
11892   fragS *branch_fragP;
11893   if (!pad_fragP)
11894     return NULL;
11895 
11896   if (pad_fragP->fr_type == rs_machine_dependent
11897       && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
11898 	  == BRANCH_PADDING))
11899     {
11900       branch_fragP = i386_next_non_empty_frag (pad_fragP);
11901       if (branch_fragP->fr_type != rs_machine_dependent)
11902 	return NULL;
11903       if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11904 	  && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11905 				   pad_fragP->tc_frag_data.mf_type))
11906 	return branch_fragP;
11907     }
11908 
11909   return NULL;
11910 }
11911 
11912 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
11913 
11914 static void
i386_classify_machine_dependent_frag(fragS * fragP)11915 i386_classify_machine_dependent_frag (fragS *fragP)
11916 {
11917   fragS *cmp_fragP;
11918   fragS *pad_fragP;
11919   fragS *branch_fragP;
11920   fragS *next_fragP;
11921   unsigned int max_prefix_length;
11922 
11923   if (fragP->tc_frag_data.classified)
11924     return;
11925 
11926   /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
11927      FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
11928   for (next_fragP = fragP;
11929        next_fragP != NULL;
11930        next_fragP = next_fragP->fr_next)
11931     {
11932       next_fragP->tc_frag_data.classified = 1;
11933       if (next_fragP->fr_type == rs_machine_dependent)
11934 	switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11935 	  {
11936 	  case BRANCH_PADDING:
11937 	    /* The BRANCH_PADDING frag must be followed by a branch
11938 	       frag.  */
11939 	    branch_fragP = i386_next_non_empty_frag (next_fragP);
11940 	    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11941 	    break;
11942 	  case FUSED_JCC_PADDING:
11943 	    /* Check if this is a fused jcc:
11944 	       FUSED_JCC_PADDING
11945 	       CMP like instruction
11946 	       BRANCH_PADDING
11947 	       COND_JUMP
11948 	       */
11949 	    cmp_fragP = i386_next_non_empty_frag (next_fragP);
11950 	    pad_fragP = i386_next_non_empty_frag (cmp_fragP);
11951 	    branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
11952 	    if (branch_fragP)
11953 	      {
11954 		/* The BRANCH_PADDING frag is merged with the
11955 		   FUSED_JCC_PADDING frag.  */
11956 		next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11957 		/* CMP like instruction size.  */
11958 		next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11959 		frag_wane (pad_fragP);
11960 		/* Skip to branch_fragP.  */
11961 		next_fragP = branch_fragP;
11962 	      }
11963 	    else if (next_fragP->tc_frag_data.max_prefix_length)
11964 	      {
11965 		/* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11966 		   a fused jcc.  */
11967 		next_fragP->fr_subtype
11968 		  = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11969 		next_fragP->tc_frag_data.max_bytes
11970 		  = next_fragP->tc_frag_data.max_prefix_length;
11971 		/* This will be updated in the BRANCH_PREFIX scan.  */
11972 		next_fragP->tc_frag_data.max_prefix_length = 0;
11973 	      }
11974 	    else
11975 	      frag_wane (next_fragP);
11976 	    break;
11977 	  }
11978     }
11979 
11980   /* Stop if there is no BRANCH_PREFIX.  */
11981   if (!align_branch_prefix_size)
11982     return;
11983 
11984   /* Scan for BRANCH_PREFIX.  */
11985   for (; fragP != NULL; fragP = fragP->fr_next)
11986     {
11987       if (fragP->fr_type != rs_machine_dependent
11988 	  || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11989 	      != BRANCH_PREFIX))
11990 	continue;
11991 
11992       /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11993 	 COND_JUMP_PREFIX.  */
11994       max_prefix_length = 0;
11995       for (next_fragP = fragP;
11996 	   next_fragP != NULL;
11997 	   next_fragP = next_fragP->fr_next)
11998 	{
11999 	  if (next_fragP->fr_type == rs_fill)
12000 	    /* Skip rs_fill frags.  */
12001 	    continue;
12002 	  else if (next_fragP->fr_type != rs_machine_dependent)
12003 	    /* Stop for all other frags.  */
12004 	    break;
12005 
12006 	  /* rs_machine_dependent frags.  */
12007 	  if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12008 	      == BRANCH_PREFIX)
12009 	    {
12010 	      /* Count BRANCH_PREFIX frags.  */
12011 	      if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12012 		{
12013 		  max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12014 		  frag_wane (next_fragP);
12015 		}
12016 	      else
12017 		max_prefix_length
12018 		  += next_fragP->tc_frag_data.max_bytes;
12019 	    }
12020 	  else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12021 		    == BRANCH_PADDING)
12022 		   || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12023 		       == FUSED_JCC_PADDING))
12024 	    {
12025 	      /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
12026 	      fragP->tc_frag_data.u.padding_fragP = next_fragP;
12027 	      break;
12028 	    }
12029 	  else
12030 	    /* Stop for other rs_machine_dependent frags.  */
12031 	    break;
12032 	}
12033 
12034       fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12035 
12036       /* Skip to the next frag.  */
12037       fragP = next_fragP;
12038     }
12039 }
12040 
12041 /* Compute padding size for
12042 
12043 	FUSED_JCC_PADDING
12044 	CMP like instruction
12045 	BRANCH_PADDING
12046 	COND_JUMP/UNCOND_JUMP
12047 
12048    or
12049 
12050 	BRANCH_PADDING
12051 	COND_JUMP/UNCOND_JUMP
12052  */
12053 
12054 static int
i386_branch_padding_size(fragS * fragP,offsetT address)12055 i386_branch_padding_size (fragS *fragP, offsetT address)
12056 {
12057   unsigned int offset, size, padding_size;
12058   fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12059 
12060   /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
12061   if (!address)
12062     address = fragP->fr_address;
12063   address += fragP->fr_fix;
12064 
12065   /* CMP like instrunction size.  */
12066   size = fragP->tc_frag_data.cmp_size;
12067 
12068   /* The base size of the branch frag.  */
12069   size += branch_fragP->fr_fix;
12070 
12071   /* Add opcode and displacement bytes for the rs_machine_dependent
12072      branch frag.  */
12073   if (branch_fragP->fr_type == rs_machine_dependent)
12074     size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12075 
12076   /* Check if branch is within boundary and doesn't end at the last
12077      byte.  */
12078   offset = address & ((1U << align_branch_power) - 1);
12079   if ((offset + size) >= (1U << align_branch_power))
12080     /* Padding needed to avoid crossing boundary.  */
12081     padding_size = (1U << align_branch_power) - offset;
12082   else
12083     /* No padding needed.  */
12084     padding_size = 0;
12085 
12086   /* The return value may be saved in tc_frag_data.length which is
12087      unsigned byte.  */
12088   if (!fits_in_unsigned_byte (padding_size))
12089     abort ();
12090 
12091   return padding_size;
12092 }
12093 
12094 /* i386_generic_table_relax_frag()
12095 
12096    Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12097    grow/shrink padding to align branch frags.  Hand others to
12098    relax_frag().  */
12099 
12100 long
i386_generic_table_relax_frag(segT segment,fragS * fragP,long stretch)12101 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12102 {
12103   if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12104       || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12105     {
12106       long padding_size = i386_branch_padding_size (fragP, 0);
12107       long grow = padding_size - fragP->tc_frag_data.length;
12108 
12109       /* When the BRANCH_PREFIX frag is used, the computed address
12110          must match the actual address and there should be no padding.  */
12111       if (fragP->tc_frag_data.padding_address
12112 	  && (fragP->tc_frag_data.padding_address != fragP->fr_address
12113 	      || padding_size))
12114 	abort ();
12115 
12116       /* Update the padding size.  */
12117       if (grow)
12118 	fragP->tc_frag_data.length = padding_size;
12119 
12120       return grow;
12121     }
12122   else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12123     {
12124       fragS *padding_fragP, *next_fragP;
12125       long padding_size, left_size, last_size;
12126 
12127       padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12128       if (!padding_fragP)
12129 	/* Use the padding set by the leading BRANCH_PREFIX frag.  */
12130 	return (fragP->tc_frag_data.length
12131 		- fragP->tc_frag_data.last_length);
12132 
12133       /* Compute the relative address of the padding frag in the very
12134         first time where the BRANCH_PREFIX frag sizes are zero.  */
12135       if (!fragP->tc_frag_data.padding_address)
12136 	fragP->tc_frag_data.padding_address
12137 	  = padding_fragP->fr_address - (fragP->fr_address - stretch);
12138 
12139       /* First update the last length from the previous interation.  */
12140       left_size = fragP->tc_frag_data.prefix_length;
12141       for (next_fragP = fragP;
12142 	   next_fragP != padding_fragP;
12143 	   next_fragP = next_fragP->fr_next)
12144 	if (next_fragP->fr_type == rs_machine_dependent
12145 	    && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12146 		== BRANCH_PREFIX))
12147 	  {
12148 	    if (left_size)
12149 	      {
12150 		int max = next_fragP->tc_frag_data.max_bytes;
12151 		if (max)
12152 		  {
12153 		    int size;
12154 		    if (max > left_size)
12155 		      size = left_size;
12156 		    else
12157 		      size = max;
12158 		    left_size -= size;
12159 		    next_fragP->tc_frag_data.last_length = size;
12160 		  }
12161 	      }
12162 	    else
12163 	      next_fragP->tc_frag_data.last_length = 0;
12164 	  }
12165 
12166       /* Check the padding size for the padding frag.  */
12167       padding_size = i386_branch_padding_size
12168 	(padding_fragP, (fragP->fr_address
12169 			 + fragP->tc_frag_data.padding_address));
12170 
12171       last_size = fragP->tc_frag_data.prefix_length;
12172       /* Check if there is change from the last interation.  */
12173       if (padding_size == last_size)
12174 	{
12175 	  /* Update the expected address of the padding frag.  */
12176 	  padding_fragP->tc_frag_data.padding_address
12177 	    = (fragP->fr_address + padding_size
12178 	       + fragP->tc_frag_data.padding_address);
12179 	  return 0;
12180 	}
12181 
12182       if (padding_size > fragP->tc_frag_data.max_prefix_length)
12183 	{
12184 	  /* No padding if there is no sufficient room.  Clear the
12185 	     expected address of the padding frag.  */
12186 	  padding_fragP->tc_frag_data.padding_address = 0;
12187 	  padding_size = 0;
12188 	}
12189       else
12190 	/* Store the expected address of the padding frag.  */
12191 	padding_fragP->tc_frag_data.padding_address
12192 	  = (fragP->fr_address + padding_size
12193 	     + fragP->tc_frag_data.padding_address);
12194 
12195       fragP->tc_frag_data.prefix_length = padding_size;
12196 
12197       /* Update the length for the current interation.  */
12198       left_size = padding_size;
12199       for (next_fragP = fragP;
12200 	   next_fragP != padding_fragP;
12201 	   next_fragP = next_fragP->fr_next)
12202 	if (next_fragP->fr_type == rs_machine_dependent
12203 	    && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12204 		== BRANCH_PREFIX))
12205 	  {
12206 	    if (left_size)
12207 	      {
12208 		int max = next_fragP->tc_frag_data.max_bytes;
12209 		if (max)
12210 		  {
12211 		    int size;
12212 		    if (max > left_size)
12213 		      size = left_size;
12214 		    else
12215 		      size = max;
12216 		    left_size -= size;
12217 		    next_fragP->tc_frag_data.length = size;
12218 		  }
12219 	      }
12220 	    else
12221 	      next_fragP->tc_frag_data.length = 0;
12222 	  }
12223 
12224       return (fragP->tc_frag_data.length
12225 	      - fragP->tc_frag_data.last_length);
12226     }
12227   return relax_frag (segment, fragP, stretch);
12228 }
12229 
12230 /* md_estimate_size_before_relax()
12231 
12232    Called just before relax() for rs_machine_dependent frags.  The x86
12233    assembler uses these frags to handle variable size jump
12234    instructions.
12235 
12236    Any symbol that is now undefined will not become defined.
12237    Return the correct fr_subtype in the frag.
12238    Return the initial "guess for variable size of frag" to caller.
12239    The guess is actually the growth beyond the fixed part.  Whatever
12240    we do to grow the fixed or variable part contributes to our
12241    returned value.  */
12242 
12243 int
md_estimate_size_before_relax(fragS * fragP,segT segment)12244 md_estimate_size_before_relax (fragS *fragP, segT segment)
12245 {
12246   if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12247       || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12248       || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12249     {
12250       i386_classify_machine_dependent_frag (fragP);
12251       return fragP->tc_frag_data.length;
12252     }
12253 
12254   /* We've already got fragP->fr_subtype right;  all we have to do is
12255      check for un-relaxable symbols.  On an ELF system, we can't relax
12256      an externally visible symbol, because it may be overridden by a
12257      shared library.  */
12258   if (S_GET_SEGMENT (fragP->fr_symbol) != segment
12259 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12260       || (IS_ELF
12261 	  && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12262 						fragP->fr_var))
12263 #endif
12264 #if defined (OBJ_COFF) && defined (TE_PE)
12265       || (OUTPUT_FLAVOR == bfd_target_coff_flavour
12266 	  && S_IS_WEAK (fragP->fr_symbol))
12267 #endif
12268       )
12269     {
12270       /* Symbol is undefined in this segment, or we need to keep a
12271 	 reloc so that weak symbols can be overridden.  */
12272       int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
12273       enum bfd_reloc_code_real reloc_type;
12274       unsigned char *opcode;
12275       int old_fr_fix;
12276       fixS *fixP = NULL;
12277 
12278       if (fragP->fr_var != NO_RELOC)
12279 	reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
12280       else if (size == 2)
12281 	reloc_type = BFD_RELOC_16_PCREL;
12282 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12283       else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12284 	       && need_plt32_p (fragP->fr_symbol))
12285 	reloc_type = BFD_RELOC_X86_64_PLT32;
12286 #endif
12287       else
12288 	reloc_type = BFD_RELOC_32_PCREL;
12289 
12290       old_fr_fix = fragP->fr_fix;
12291       opcode = (unsigned char *) fragP->fr_opcode;
12292 
12293       switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
12294 	{
12295 	case UNCOND_JUMP:
12296 	  /* Make jmp (0xeb) a (d)word displacement jump.  */
12297 	  opcode[0] = 0xe9;
12298 	  fragP->fr_fix += size;
12299 	  fixP = fix_new (fragP, old_fr_fix, size,
12300 			  fragP->fr_symbol,
12301 			  fragP->fr_offset, 1,
12302 			  reloc_type);
12303 	  break;
12304 
12305 	case COND_JUMP86:
12306 	  if (size == 2
12307 	      && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
12308 	    {
12309 	      /* Negate the condition, and branch past an
12310 		 unconditional jump.  */
12311 	      opcode[0] ^= 1;
12312 	      opcode[1] = 3;
12313 	      /* Insert an unconditional jump.  */
12314 	      opcode[2] = 0xe9;
12315 	      /* We added two extra opcode bytes, and have a two byte
12316 		 offset.  */
12317 	      fragP->fr_fix += 2 + 2;
12318 	      fix_new (fragP, old_fr_fix + 2, 2,
12319 		       fragP->fr_symbol,
12320 		       fragP->fr_offset, 1,
12321 		       reloc_type);
12322 	      break;
12323 	    }
12324 	  /* Fall through.  */
12325 
12326 	case COND_JUMP:
12327 	  if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12328 	    {
12329 	      fragP->fr_fix += 1;
12330 	      fixP = fix_new (fragP, old_fr_fix, 1,
12331 			      fragP->fr_symbol,
12332 			      fragP->fr_offset, 1,
12333 			      BFD_RELOC_8_PCREL);
12334 	      fixP->fx_signed = 1;
12335 	      break;
12336 	    }
12337 
12338 	  /* This changes the byte-displacement jump 0x7N
12339 	     to the (d)word-displacement jump 0x0f,0x8N.  */
12340 	  opcode[1] = opcode[0] + 0x10;
12341 	  opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12342 	  /* We've added an opcode byte.  */
12343 	  fragP->fr_fix += 1 + size;
12344 	  fixP = fix_new (fragP, old_fr_fix + 1, size,
12345 			  fragP->fr_symbol,
12346 			  fragP->fr_offset, 1,
12347 			  reloc_type);
12348 	  break;
12349 
12350 	default:
12351 	  BAD_CASE (fragP->fr_subtype);
12352 	  break;
12353 	}
12354 
12355       /* All jumps handled here are signed, but don't unconditionally use a
12356 	 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12357 	 around at 4G (outside of 64-bit mode) and 64k.  */
12358       if (size == 4 && flag_code == CODE_64BIT)
12359 	fixP->fx_signed = 1;
12360 
12361       frag_wane (fragP);
12362       return fragP->fr_fix - old_fr_fix;
12363     }
12364 
12365   /* Guess size depending on current relax state.  Initially the relax
12366      state will correspond to a short jump and we return 1, because
12367      the variable part of the frag (the branch offset) is one byte
12368      long.  However, we can relax a section more than once and in that
12369      case we must either set fr_subtype back to the unrelaxed state,
12370      or return the value for the appropriate branch.  */
12371   return md_relax_table[fragP->fr_subtype].rlx_length;
12372 }
12373 
12374 /* Called after relax() is finished.
12375 
12376    In:	Address of frag.
12377 	fr_type == rs_machine_dependent.
12378 	fr_subtype is what the address relaxed to.
12379 
12380    Out:	Any fixSs and constants are set up.
12381 	Caller will turn frag into a ".space 0".  */
12382 
12383 void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,segT sec ATTRIBUTE_UNUSED,fragS * fragP)12384 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12385                  fragS *fragP)
12386 {
12387   unsigned char *opcode;
12388   unsigned char *where_to_put_displacement = NULL;
12389   offsetT target_address;
12390   offsetT opcode_address;
12391   unsigned int extension = 0;
12392   offsetT displacement_from_opcode_start;
12393 
12394   if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12395       || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12396       || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12397     {
12398       /* Generate nop padding.  */
12399       unsigned int size = fragP->tc_frag_data.length;
12400       if (size)
12401 	{
12402 	  if (size > fragP->tc_frag_data.max_bytes)
12403 	    abort ();
12404 
12405 	  if (flag_debug)
12406 	    {
12407 	      const char *msg;
12408 	      const char *branch = "branch";
12409 	      const char *prefix = "";
12410 	      fragS *padding_fragP;
12411 	      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12412 		  == BRANCH_PREFIX)
12413 		{
12414 		  padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12415 		  switch (fragP->tc_frag_data.default_prefix)
12416 		    {
12417 		    default:
12418 		      abort ();
12419 		      break;
12420 		    case CS_PREFIX_OPCODE:
12421 		      prefix = " cs";
12422 		      break;
12423 		    case DS_PREFIX_OPCODE:
12424 		      prefix = " ds";
12425 		      break;
12426 		    case ES_PREFIX_OPCODE:
12427 		      prefix = " es";
12428 		      break;
12429 		    case FS_PREFIX_OPCODE:
12430 		      prefix = " fs";
12431 		      break;
12432 		    case GS_PREFIX_OPCODE:
12433 		      prefix = " gs";
12434 		      break;
12435 		    case SS_PREFIX_OPCODE:
12436 		      prefix = " ss";
12437 		      break;
12438 		    }
12439 		  if (padding_fragP)
12440 		    msg = _("%s:%u: add %d%s at 0x%llx to align "
12441 			    "%s within %d-byte boundary\n");
12442 		  else
12443 		    msg = _("%s:%u: add additional %d%s at 0x%llx to "
12444 			    "align %s within %d-byte boundary\n");
12445 		}
12446 	      else
12447 		{
12448 		  padding_fragP = fragP;
12449 		  msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12450 			  "%s within %d-byte boundary\n");
12451 		}
12452 
12453 	      if (padding_fragP)
12454 		switch (padding_fragP->tc_frag_data.branch_type)
12455 		  {
12456 		  case align_branch_jcc:
12457 		    branch = "jcc";
12458 		    break;
12459 		  case align_branch_fused:
12460 		    branch = "fused jcc";
12461 		    break;
12462 		  case align_branch_jmp:
12463 		    branch = "jmp";
12464 		    break;
12465 		  case align_branch_call:
12466 		    branch = "call";
12467 		    break;
12468 		  case align_branch_indirect:
12469 		    branch = "indiret branch";
12470 		    break;
12471 		  case align_branch_ret:
12472 		    branch = "ret";
12473 		    break;
12474 		  default:
12475 		    break;
12476 		  }
12477 
12478 	      fprintf (stdout, msg,
12479 		       fragP->fr_file, fragP->fr_line, size, prefix,
12480 		       (long long) fragP->fr_address, branch,
12481 		       1 << align_branch_power);
12482 	    }
12483 	  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12484 	    memset (fragP->fr_opcode,
12485 		    fragP->tc_frag_data.default_prefix, size);
12486 	  else
12487 	    i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12488 				size, 0);
12489 	  fragP->fr_fix += size;
12490 	}
12491       return;
12492     }
12493 
12494   opcode = (unsigned char *) fragP->fr_opcode;
12495 
12496   /* Address we want to reach in file space.  */
12497   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
12498 
12499   /* Address opcode resides at in file space.  */
12500   opcode_address = fragP->fr_address + fragP->fr_fix;
12501 
12502   /* Displacement from opcode start to fill into instruction.  */
12503   displacement_from_opcode_start = target_address - opcode_address;
12504 
12505   if ((fragP->fr_subtype & BIG) == 0)
12506     {
12507       /* Don't have to change opcode.  */
12508       extension = 1;		/* 1 opcode + 1 displacement  */
12509       where_to_put_displacement = &opcode[1];
12510     }
12511   else
12512     {
12513       if (no_cond_jump_promotion
12514 	  && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
12515 	as_warn_where (fragP->fr_file, fragP->fr_line,
12516 		       _("long jump required"));
12517 
12518       switch (fragP->fr_subtype)
12519 	{
12520 	case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12521 	  extension = 4;		/* 1 opcode + 4 displacement  */
12522 	  opcode[0] = 0xe9;
12523 	  where_to_put_displacement = &opcode[1];
12524 	  break;
12525 
12526 	case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12527 	  extension = 2;		/* 1 opcode + 2 displacement  */
12528 	  opcode[0] = 0xe9;
12529 	  where_to_put_displacement = &opcode[1];
12530 	  break;
12531 
12532 	case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12533 	case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12534 	  extension = 5;		/* 2 opcode + 4 displacement  */
12535 	  opcode[1] = opcode[0] + 0x10;
12536 	  opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12537 	  where_to_put_displacement = &opcode[2];
12538 	  break;
12539 
12540 	case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12541 	  extension = 3;		/* 2 opcode + 2 displacement  */
12542 	  opcode[1] = opcode[0] + 0x10;
12543 	  opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12544 	  where_to_put_displacement = &opcode[2];
12545 	  break;
12546 
12547 	case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12548 	  extension = 4;
12549 	  opcode[0] ^= 1;
12550 	  opcode[1] = 3;
12551 	  opcode[2] = 0xe9;
12552 	  where_to_put_displacement = &opcode[3];
12553 	  break;
12554 
12555 	default:
12556 	  BAD_CASE (fragP->fr_subtype);
12557 	  break;
12558 	}
12559     }
12560 
12561   /* If size if less then four we are sure that the operand fits,
12562      but if it's 4, then it could be that the displacement is larger
12563      then -/+ 2GB.  */
12564   if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12565       && object_64bit
12566       && ((addressT) (displacement_from_opcode_start - extension
12567 		      + ((addressT) 1 << 31))
12568 	  > (((addressT) 2 << 31) - 1)))
12569     {
12570       as_bad_where (fragP->fr_file, fragP->fr_line,
12571 		    _("jump target out of range"));
12572       /* Make us emit 0.  */
12573       displacement_from_opcode_start = extension;
12574     }
12575   /* Now put displacement after opcode.  */
12576   md_number_to_chars ((char *) where_to_put_displacement,
12577 		      (valueT) (displacement_from_opcode_start - extension),
12578 		      DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
12579   fragP->fr_fix += extension;
12580 }
12581 
12582 /* Apply a fixup (fixP) to segment data, once it has been determined
12583    by our caller that we have all the info we need to fix it up.
12584 
12585    Parameter valP is the pointer to the value of the bits.
12586 
12587    On the 386, immediates, displacements, and data pointers are all in
12588    the same (little-endian) format, so we don't need to care about which
12589    we are handling.  */
12590 
12591 void
md_apply_fix(fixS * fixP,valueT * valP,segT seg ATTRIBUTE_UNUSED)12592 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
12593 {
12594   char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
12595   valueT value = *valP;
12596 
12597 #if !defined (TE_Mach)
12598   if (fixP->fx_pcrel)
12599     {
12600       switch (fixP->fx_r_type)
12601 	{
12602 	default:
12603 	  break;
12604 
12605 	case BFD_RELOC_64:
12606 	  fixP->fx_r_type = BFD_RELOC_64_PCREL;
12607 	  break;
12608 	case BFD_RELOC_32:
12609 	case BFD_RELOC_X86_64_32S:
12610 	  fixP->fx_r_type = BFD_RELOC_32_PCREL;
12611 	  break;
12612 	case BFD_RELOC_16:
12613 	  fixP->fx_r_type = BFD_RELOC_16_PCREL;
12614 	  break;
12615 	case BFD_RELOC_8:
12616 	  fixP->fx_r_type = BFD_RELOC_8_PCREL;
12617 	  break;
12618 	}
12619     }
12620 
12621   if (fixP->fx_addsy != NULL
12622       && (fixP->fx_r_type == BFD_RELOC_32_PCREL
12623 	  || fixP->fx_r_type == BFD_RELOC_64_PCREL
12624 	  || fixP->fx_r_type == BFD_RELOC_16_PCREL
12625 	  || fixP->fx_r_type == BFD_RELOC_8_PCREL)
12626       && !use_rela_relocations)
12627     {
12628       /* This is a hack.  There should be a better way to handle this.
12629 	 This covers for the fact that bfd_install_relocation will
12630 	 subtract the current location (for partial_inplace, PC relative
12631 	 relocations); see more below.  */
12632 #ifndef OBJ_AOUT
12633       if (IS_ELF
12634 #ifdef TE_PE
12635 	  || OUTPUT_FLAVOR == bfd_target_coff_flavour
12636 #endif
12637 	  )
12638 	value += fixP->fx_where + fixP->fx_frag->fr_address;
12639 #endif
12640 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12641       if (IS_ELF)
12642 	{
12643 	  segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
12644 
12645 	  if ((sym_seg == seg
12646 	       || (symbol_section_p (fixP->fx_addsy)
12647 		   && sym_seg != absolute_section))
12648 	      && !generic_force_reloc (fixP))
12649 	    {
12650 	      /* Yes, we add the values in twice.  This is because
12651 		 bfd_install_relocation subtracts them out again.  I think
12652 		 bfd_install_relocation is broken, but I don't dare change
12653 		 it.  FIXME.  */
12654 	      value += fixP->fx_where + fixP->fx_frag->fr_address;
12655 	    }
12656 	}
12657 #endif
12658 #if defined (OBJ_COFF) && defined (TE_PE)
12659       /* For some reason, the PE format does not store a
12660 	 section address offset for a PC relative symbol.  */
12661       if (S_GET_SEGMENT (fixP->fx_addsy) != seg
12662 	  || S_IS_WEAK (fixP->fx_addsy))
12663 	value += md_pcrel_from (fixP);
12664 #endif
12665     }
12666 #if defined (OBJ_COFF) && defined (TE_PE)
12667   if (fixP->fx_addsy != NULL
12668       && S_IS_WEAK (fixP->fx_addsy)
12669       /* PR 16858: Do not modify weak function references.  */
12670       && ! fixP->fx_pcrel)
12671     {
12672 #if !defined (TE_PEP)
12673       /* For x86 PE weak function symbols are neither PC-relative
12674 	 nor do they set S_IS_FUNCTION.  So the only reliable way
12675 	 to detect them is to check the flags of their containing
12676 	 section.  */
12677       if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12678 	  && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12679 	;
12680       else
12681 #endif
12682       value -= S_GET_VALUE (fixP->fx_addsy);
12683     }
12684 #endif
12685 
12686   /* Fix a few things - the dynamic linker expects certain values here,
12687      and we must not disappoint it.  */
12688 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12689   if (IS_ELF && fixP->fx_addsy)
12690     switch (fixP->fx_r_type)
12691       {
12692       case BFD_RELOC_386_PLT32:
12693       case BFD_RELOC_X86_64_PLT32:
12694 	/* Make the jump instruction point to the address of the operand.
12695 	   At runtime we merely add the offset to the actual PLT entry.
12696 	   NB: Subtract the offset size only for jump instructions.  */
12697 	if (fixP->fx_pcrel)
12698 	  value = -4;
12699 	break;
12700 
12701       case BFD_RELOC_386_TLS_GD:
12702       case BFD_RELOC_386_TLS_LDM:
12703       case BFD_RELOC_386_TLS_IE_32:
12704       case BFD_RELOC_386_TLS_IE:
12705       case BFD_RELOC_386_TLS_GOTIE:
12706       case BFD_RELOC_386_TLS_GOTDESC:
12707       case BFD_RELOC_X86_64_TLSGD:
12708       case BFD_RELOC_X86_64_TLSLD:
12709       case BFD_RELOC_X86_64_GOTTPOFF:
12710       case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
12711 	value = 0; /* Fully resolved at runtime.  No addend.  */
12712 	/* Fallthrough */
12713       case BFD_RELOC_386_TLS_LE:
12714       case BFD_RELOC_386_TLS_LDO_32:
12715       case BFD_RELOC_386_TLS_LE_32:
12716       case BFD_RELOC_X86_64_DTPOFF32:
12717       case BFD_RELOC_X86_64_DTPOFF64:
12718       case BFD_RELOC_X86_64_TPOFF32:
12719       case BFD_RELOC_X86_64_TPOFF64:
12720 	S_SET_THREAD_LOCAL (fixP->fx_addsy);
12721 	break;
12722 
12723       case BFD_RELOC_386_TLS_DESC_CALL:
12724       case BFD_RELOC_X86_64_TLSDESC_CALL:
12725 	value = 0; /* Fully resolved at runtime.  No addend.  */
12726 	S_SET_THREAD_LOCAL (fixP->fx_addsy);
12727 	fixP->fx_done = 0;
12728 	return;
12729 
12730       case BFD_RELOC_VTABLE_INHERIT:
12731       case BFD_RELOC_VTABLE_ENTRY:
12732 	fixP->fx_done = 0;
12733 	return;
12734 
12735       default:
12736 	break;
12737       }
12738 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)  */
12739 
12740   /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
12741   if (!object_64bit)
12742     value = extend_to_32bit_address (value);
12743 
12744   *valP = value;
12745 #endif /* !defined (TE_Mach)  */
12746 
12747   /* Are we finished with this relocation now?  */
12748   if (fixP->fx_addsy == NULL)
12749     {
12750       fixP->fx_done = 1;
12751       switch (fixP->fx_r_type)
12752 	{
12753 	case BFD_RELOC_X86_64_32S:
12754 	  fixP->fx_signed = 1;
12755 	  break;
12756 
12757 	default:
12758 	  break;
12759 	}
12760     }
12761 #if defined (OBJ_COFF) && defined (TE_PE)
12762   else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12763     {
12764       fixP->fx_done = 0;
12765       /* Remember value for tc_gen_reloc.  */
12766       fixP->fx_addnumber = value;
12767       /* Clear out the frag for now.  */
12768       value = 0;
12769     }
12770 #endif
12771   else if (use_rela_relocations)
12772     {
12773       if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12774 	fixP->fx_no_overflow = 1;
12775       /* Remember value for tc_gen_reloc.  */
12776       fixP->fx_addnumber = value;
12777       value = 0;
12778     }
12779 
12780   md_number_to_chars (p, value, fixP->fx_size);
12781 }
12782 
12783 const char *
md_atof(int type,char * litP,int * sizeP)12784 md_atof (int type, char *litP, int *sizeP)
12785 {
12786   /* This outputs the LITTLENUMs in REVERSE order;
12787      in accord with the bigendian 386.  */
12788   return ieee_md_atof (type, litP, sizeP, false);
12789 }
12790 
12791 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
12792 
12793 static char *
output_invalid(int c)12794 output_invalid (int c)
12795 {
12796   if (ISPRINT (c))
12797     snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12798 	      "'%c'", c);
12799   else
12800     snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12801 	      "(0x%x)", (unsigned char) c);
12802   return output_invalid_buf;
12803 }
12804 
12805 /* Verify that @r can be used in the current context.  */
12806 
check_register(const reg_entry * r)12807 static bool check_register (const reg_entry *r)
12808 {
12809   if (allow_pseudo_reg)
12810     return true;
12811 
12812   if (operand_type_all_zero (&r->reg_type))
12813     return false;
12814 
12815   if ((r->reg_type.bitfield.dword
12816        || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12817        || r->reg_type.bitfield.class == RegCR
12818        || r->reg_type.bitfield.class == RegDR)
12819       && !cpu_arch_flags.bitfield.cpui386)
12820     return false;
12821 
12822   if (r->reg_type.bitfield.class == RegTR
12823       && (flag_code == CODE_64BIT
12824 	  || !cpu_arch_flags.bitfield.cpui386
12825 	  || cpu_arch_isa_flags.bitfield.cpui586
12826 	  || cpu_arch_isa_flags.bitfield.cpui686))
12827     return false;
12828 
12829   if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12830     return false;
12831 
12832   if (!cpu_arch_flags.bitfield.cpuavx512f)
12833     {
12834       if (r->reg_type.bitfield.zmmword
12835 	  || r->reg_type.bitfield.class == RegMask)
12836 	return false;
12837 
12838       if (!cpu_arch_flags.bitfield.cpuavx)
12839 	{
12840 	  if (r->reg_type.bitfield.ymmword)
12841 	    return false;
12842 
12843 	  if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12844 	    return false;
12845 	}
12846     }
12847 
12848   if (r->reg_type.bitfield.tmmword
12849       && (!cpu_arch_flags.bitfield.cpuamx_tile
12850           || flag_code != CODE_64BIT))
12851     return false;
12852 
12853   if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12854     return false;
12855 
12856   /* Don't allow fake index register unless allow_index_reg isn't 0. */
12857   if (!allow_index_reg && r->reg_num == RegIZ)
12858     return false;
12859 
12860   /* Upper 16 vector registers are only available with VREX in 64bit
12861      mode, and require EVEX encoding.  */
12862   if (r->reg_flags & RegVRex)
12863     {
12864       if (!cpu_arch_flags.bitfield.cpuavx512f
12865 	  || flag_code != CODE_64BIT)
12866 	return false;
12867 
12868       if (i.vec_encoding == vex_encoding_default)
12869 	i.vec_encoding = vex_encoding_evex;
12870       else if (i.vec_encoding != vex_encoding_evex)
12871 	i.vec_encoding = vex_encoding_error;
12872     }
12873 
12874   if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12875       && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12876       && flag_code != CODE_64BIT)
12877     return false;
12878 
12879   if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12880       && !intel_syntax)
12881     return false;
12882 
12883   return true;
12884 }
12885 
12886 /* REG_STRING starts *before* REGISTER_PREFIX.  */
12887 
12888 static const reg_entry *
parse_real_register(char * reg_string,char ** end_op)12889 parse_real_register (char *reg_string, char **end_op)
12890 {
12891   char *s = reg_string;
12892   char *p;
12893   char reg_name_given[MAX_REG_NAME_SIZE + 1];
12894   const reg_entry *r;
12895 
12896   /* Skip possible REGISTER_PREFIX and possible whitespace.  */
12897   if (*s == REGISTER_PREFIX)
12898     ++s;
12899 
12900   if (is_space_char (*s))
12901     ++s;
12902 
12903   p = reg_name_given;
12904   while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
12905     {
12906       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
12907 	return (const reg_entry *) NULL;
12908       s++;
12909     }
12910 
12911   /* For naked regs, make sure that we are not dealing with an identifier.
12912      This prevents confusing an identifier like `eax_var' with register
12913      `eax'.  */
12914   if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12915     return (const reg_entry *) NULL;
12916 
12917   *end_op = s;
12918 
12919   r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
12920 
12921   /* Handle floating point regs, allowing spaces in the (i) part.  */
12922   if (r == reg_st0)
12923     {
12924       if (!cpu_arch_flags.bitfield.cpu8087
12925 	  && !cpu_arch_flags.bitfield.cpu287
12926 	  && !cpu_arch_flags.bitfield.cpu387
12927 	  && !allow_pseudo_reg)
12928 	return (const reg_entry *) NULL;
12929 
12930       if (is_space_char (*s))
12931 	++s;
12932       if (*s == '(')
12933 	{
12934 	  ++s;
12935 	  if (is_space_char (*s))
12936 	    ++s;
12937 	  if (*s >= '0' && *s <= '7')
12938 	    {
12939 	      int fpr = *s - '0';
12940 	      ++s;
12941 	      if (is_space_char (*s))
12942 		++s;
12943 	      if (*s == ')')
12944 		{
12945 		  *end_op = s + 1;
12946 		  know (r[fpr].reg_num == fpr);
12947 		  return r + fpr;
12948 		}
12949 	    }
12950 	  /* We have "%st(" then garbage.  */
12951 	  return (const reg_entry *) NULL;
12952 	}
12953     }
12954 
12955   return r && check_register (r) ? r : NULL;
12956 }
12957 
12958 /* REG_STRING starts *before* REGISTER_PREFIX.  */
12959 
12960 static const reg_entry *
parse_register(char * reg_string,char ** end_op)12961 parse_register (char *reg_string, char **end_op)
12962 {
12963   const reg_entry *r;
12964 
12965   if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12966     r = parse_real_register (reg_string, end_op);
12967   else
12968     r = NULL;
12969   if (!r)
12970     {
12971       char *save = input_line_pointer;
12972       char c;
12973       symbolS *symbolP;
12974 
12975       input_line_pointer = reg_string;
12976       c = get_symbol_name (&reg_string);
12977       symbolP = symbol_find (reg_string);
12978       while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12979 	{
12980 	  const expressionS *e = symbol_get_value_expression(symbolP);
12981 
12982 	  if (e->X_op != O_symbol || e->X_add_number)
12983 	    break;
12984 	  symbolP = e->X_add_symbol;
12985 	}
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
i386_parse_name(char * name,expressionS * e,char * nextcharP)13009 i386_parse_name (char *name, expressionS *e, char *nextcharP)
13010 {
13011   const reg_entry *r = NULL;
13012   char *end = input_line_pointer;
13013 
13014   *end = *nextcharP;
13015   if (*name == REGISTER_PREFIX || allow_naked_reg)
13016     r = parse_real_register (name, &input_line_pointer);
13017   if (r && end <= input_line_pointer)
13018     {
13019       *nextcharP = *input_line_pointer;
13020       *input_line_pointer = 0;
13021       if (r != &bad_reg)
13022 	{
13023 	  e->X_op = O_register;
13024 	  e->X_add_number = r - i386_regtab;
13025 	}
13026       else
13027 	  e->X_op = O_illegal;
13028       return 1;
13029     }
13030   input_line_pointer = end;
13031   *end = 0;
13032   return intel_syntax ? i386_intel_parse_name (name, e) : 0;
13033 }
13034 
13035 void
md_operand(expressionS * e)13036 md_operand (expressionS *e)
13037 {
13038   char *end;
13039   const reg_entry *r;
13040 
13041   switch (*input_line_pointer)
13042     {
13043     case REGISTER_PREFIX:
13044       r = parse_real_register (input_line_pointer, &end);
13045       if (r)
13046 	{
13047 	  e->X_op = O_register;
13048 	  e->X_add_number = r - i386_regtab;
13049 	  input_line_pointer = end;
13050 	}
13051       break;
13052 
13053     case '[':
13054       gas_assert (intel_syntax);
13055       end = input_line_pointer++;
13056       expression (e);
13057       if (*input_line_pointer == ']')
13058 	{
13059 	  ++input_line_pointer;
13060 	  e->X_op_symbol = make_expr_symbol (e);
13061 	  e->X_add_symbol = NULL;
13062 	  e->X_add_number = 0;
13063 	  e->X_op = O_index;
13064 	}
13065       else
13066 	{
13067 	  e->X_op = O_absent;
13068 	  input_line_pointer = end;
13069 	}
13070       break;
13071     }
13072 }
13073 
13074 
13075 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13076 const char *md_shortopts = "kVQ:sqnO::";
13077 #else
13078 const char *md_shortopts = "qnO::";
13079 #endif
13080 
13081 #define OPTION_32 (OPTION_MD_BASE + 0)
13082 #define OPTION_64 (OPTION_MD_BASE + 1)
13083 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
13084 #define OPTION_MARCH (OPTION_MD_BASE + 3)
13085 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
13086 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13087 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13088 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13089 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
13090 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
13091 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
13092 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
13093 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13094 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13095 #define OPTION_X32 (OPTION_MD_BASE + 14)
13096 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
13097 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13098 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
13099 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
13100 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
13101 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
13102 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
13103 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13104 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
13105 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
13106 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
13107 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
13108 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13109 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13110 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
13111 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
13112 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13113 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13114 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
13115 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
13116 
13117 struct option md_longopts[] =
13118 {
13119   {"32", no_argument, NULL, OPTION_32},
13120 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13121      || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13122   {"64", no_argument, NULL, OPTION_64},
13123 #endif
13124 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13125   {"x32", no_argument, NULL, OPTION_X32},
13126   {"mshared", no_argument, NULL, OPTION_MSHARED},
13127   {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
13128 #endif
13129   {"divide", no_argument, NULL, OPTION_DIVIDE},
13130   {"march", required_argument, NULL, OPTION_MARCH},
13131   {"mtune", required_argument, NULL, OPTION_MTUNE},
13132   {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13133   {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13134   {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13135   {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
13136   {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
13137   {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
13138   {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
13139   {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
13140   {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
13141   {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
13142   {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
13143   {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13144   {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
13145 # if defined (TE_PE) || defined (TE_PEP)
13146   {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13147 #endif
13148   {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
13149   {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
13150   {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
13151   {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
13152   {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13153   {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13154   {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
13155   {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
13156   {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13157   {"mlfence-before-indirect-branch", required_argument, NULL,
13158    OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13159   {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
13160   {"mamd64", no_argument, NULL, OPTION_MAMD64},
13161   {"mintel64", no_argument, NULL, OPTION_MINTEL64},
13162   {NULL, no_argument, NULL, 0}
13163 };
13164 size_t md_longopts_size = sizeof (md_longopts);
13165 
13166 int
md_parse_option(int c,const char * arg)13167 md_parse_option (int c, const char *arg)
13168 {
13169   unsigned int j;
13170   char *arch, *next, *saved, *type;
13171 
13172   switch (c)
13173     {
13174     case 'n':
13175       optimize_align_code = 0;
13176       break;
13177 
13178     case 'q':
13179       quiet_warnings = 1;
13180       break;
13181 
13182 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13183       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13184 	 should be emitted or not.  FIXME: Not implemented.  */
13185     case 'Q':
13186       if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13187 	return 0;
13188       break;
13189 
13190       /* -V: SVR4 argument to print version ID.  */
13191     case 'V':
13192       print_version_id ();
13193       break;
13194 
13195       /* -k: Ignore for FreeBSD compatibility.  */
13196     case 'k':
13197       break;
13198 
13199     case 's':
13200       /* -s: On i386 Solaris, this tells the native assembler to use
13201 	 .stab instead of .stab.excl.  We always use .stab anyhow.  */
13202       break;
13203 
13204     case OPTION_MSHARED:
13205       shared = 1;
13206       break;
13207 
13208     case OPTION_X86_USED_NOTE:
13209       if (strcasecmp (arg, "yes") == 0)
13210         x86_used_note = 1;
13211       else if (strcasecmp (arg, "no") == 0)
13212         x86_used_note = 0;
13213       else
13214         as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13215       break;
13216 
13217 
13218 #endif
13219 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13220      || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13221     case OPTION_64:
13222       {
13223 	const char **list, **l;
13224 
13225 	list = bfd_target_list ();
13226 	for (l = list; *l != NULL; l++)
13227 	  if (startswith (*l, "elf64-x86-64")
13228 	      || strcmp (*l, "coff-x86-64") == 0
13229 	      || strcmp (*l, "pe-x86-64") == 0
13230 	      || strcmp (*l, "pei-x86-64") == 0
13231 	      || strcmp (*l, "mach-o-x86-64") == 0)
13232 	    {
13233 	      default_arch = "x86_64";
13234 	      break;
13235 	    }
13236 	if (*l == NULL)
13237 	  as_fatal (_("no compiled in support for x86_64"));
13238 	free (list);
13239       }
13240       break;
13241 #endif
13242 
13243 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13244     case OPTION_X32:
13245       if (IS_ELF)
13246 	{
13247 	  const char **list, **l;
13248 
13249 	  list = bfd_target_list ();
13250 	  for (l = list; *l != NULL; l++)
13251 	    if (startswith (*l, "elf32-x86-64"))
13252 	      {
13253 		default_arch = "x86_64:32";
13254 		break;
13255 	      }
13256 	  if (*l == NULL)
13257 	    as_fatal (_("no compiled in support for 32bit x86_64"));
13258 	  free (list);
13259 	}
13260       else
13261 	as_fatal (_("32bit x86_64 is only supported for ELF"));
13262       break;
13263 #endif
13264 
13265     case OPTION_32:
13266       default_arch = "i386";
13267       break;
13268 
13269     case OPTION_DIVIDE:
13270 #ifdef SVR4_COMMENT_CHARS
13271       {
13272 	char *n, *t;
13273 	const char *s;
13274 
13275 	n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
13276 	t = n;
13277 	for (s = i386_comment_chars; *s != '\0'; s++)
13278 	  if (*s != '/')
13279 	    *t++ = *s;
13280 	*t = '\0';
13281 	i386_comment_chars = n;
13282       }
13283 #endif
13284       break;
13285 
13286     case OPTION_MARCH:
13287       saved = xstrdup (arg);
13288       arch = saved;
13289       /* Allow -march=+nosse.  */
13290       if (*arch == '+')
13291 	arch++;
13292       do
13293 	{
13294 	  if (*arch == '.')
13295 	    as_fatal (_("invalid -march= option: `%s'"), arg);
13296 	  next = strchr (arch, '+');
13297 	  if (next)
13298 	    *next++ = '\0';
13299 	  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13300 	    {
13301 	      if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13302 	          && strcmp (arch, cpu_arch[j].name) == 0)
13303 		{
13304 		  /* Processor.  */
13305 		  if (! cpu_arch[j].enable.bitfield.cpui386)
13306 		    continue;
13307 
13308 		  cpu_arch_name = cpu_arch[j].name;
13309 		  free (cpu_sub_arch_name);
13310 		  cpu_sub_arch_name = NULL;
13311 		  cpu_arch_flags = cpu_arch[j].enable;
13312 		  cpu_arch_isa = cpu_arch[j].type;
13313 		  cpu_arch_isa_flags = cpu_arch[j].enable;
13314 		  if (!cpu_arch_tune_set)
13315 		    {
13316 		      cpu_arch_tune = cpu_arch_isa;
13317 		      cpu_arch_tune_flags = cpu_arch_isa_flags;
13318 		    }
13319 		  break;
13320 		}
13321 	      else if (cpu_arch[j].type == PROCESSOR_NONE
13322 		       && strcmp (arch, cpu_arch[j].name) == 0
13323 		       && !cpu_flags_all_zero (&cpu_arch[j].enable))
13324 		{
13325 		  /* ISA extension.  */
13326 		  i386_cpu_flags flags;
13327 
13328 		  flags = cpu_flags_or (cpu_arch_flags,
13329 					cpu_arch[j].enable);
13330 
13331 		  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13332 		    {
13333 		      extend_cpu_sub_arch_name (arch);
13334 		      cpu_arch_flags = flags;
13335 		      cpu_arch_isa_flags = flags;
13336 		    }
13337 		  else
13338 		    cpu_arch_isa_flags
13339 		      = cpu_flags_or (cpu_arch_isa_flags,
13340 				      cpu_arch[j].enable);
13341 		  break;
13342 		}
13343 	    }
13344 
13345 	  if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
13346 	    {
13347 	      /* Disable an ISA extension.  */
13348 	      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13349 		if (cpu_arch[j].type == PROCESSOR_NONE
13350 		    && strcmp (arch + 2, cpu_arch[j].name) == 0)
13351 		  {
13352 		    i386_cpu_flags flags;
13353 
13354 		    flags = cpu_flags_and_not (cpu_arch_flags,
13355 					       cpu_arch[j].disable);
13356 		    if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13357 		      {
13358 			extend_cpu_sub_arch_name (arch);
13359 			cpu_arch_flags = flags;
13360 			cpu_arch_isa_flags = flags;
13361 		      }
13362 		    break;
13363 		  }
13364 	    }
13365 
13366 	  if (j >= ARRAY_SIZE (cpu_arch))
13367 	    as_fatal (_("invalid -march= option: `%s'"), arg);
13368 
13369 	  arch = next;
13370 	}
13371       while (next != NULL);
13372       free (saved);
13373       break;
13374 
13375     case OPTION_MTUNE:
13376       if (*arg == '.')
13377 	as_fatal (_("invalid -mtune= option: `%s'"), arg);
13378       for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13379 	{
13380 	  if (cpu_arch[j].type != PROCESSOR_NONE
13381 	      && strcmp (arg, cpu_arch[j].name) == 0)
13382 	    {
13383 	      cpu_arch_tune_set = 1;
13384 	      cpu_arch_tune = cpu_arch [j].type;
13385 	      cpu_arch_tune_flags = cpu_arch[j].enable;
13386 	      break;
13387 	    }
13388 	}
13389       if (j >= ARRAY_SIZE (cpu_arch))
13390 	as_fatal (_("invalid -mtune= option: `%s'"), arg);
13391       break;
13392 
13393     case OPTION_MMNEMONIC:
13394       if (strcasecmp (arg, "att") == 0)
13395 	intel_mnemonic = 0;
13396       else if (strcasecmp (arg, "intel") == 0)
13397 	intel_mnemonic = 1;
13398       else
13399 	as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
13400       break;
13401 
13402     case OPTION_MSYNTAX:
13403       if (strcasecmp (arg, "att") == 0)
13404 	intel_syntax = 0;
13405       else if (strcasecmp (arg, "intel") == 0)
13406 	intel_syntax = 1;
13407       else
13408 	as_fatal (_("invalid -msyntax= option: `%s'"), arg);
13409       break;
13410 
13411     case OPTION_MINDEX_REG:
13412       allow_index_reg = 1;
13413       break;
13414 
13415     case OPTION_MNAKED_REG:
13416       allow_naked_reg = 1;
13417       break;
13418 
13419     case OPTION_MSSE2AVX:
13420       sse2avx = 1;
13421       break;
13422 
13423     case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13424       use_unaligned_vector_move = 1;
13425       break;
13426 
13427     case OPTION_MSSE_CHECK:
13428       if (strcasecmp (arg, "error") == 0)
13429 	sse_check = check_error;
13430       else if (strcasecmp (arg, "warning") == 0)
13431 	sse_check = check_warning;
13432       else if (strcasecmp (arg, "none") == 0)
13433 	sse_check = check_none;
13434       else
13435 	as_fatal (_("invalid -msse-check= option: `%s'"), arg);
13436       break;
13437 
13438     case OPTION_MOPERAND_CHECK:
13439       if (strcasecmp (arg, "error") == 0)
13440 	operand_check = check_error;
13441       else if (strcasecmp (arg, "warning") == 0)
13442 	operand_check = check_warning;
13443       else if (strcasecmp (arg, "none") == 0)
13444 	operand_check = check_none;
13445       else
13446 	as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13447       break;
13448 
13449     case OPTION_MAVXSCALAR:
13450       if (strcasecmp (arg, "128") == 0)
13451 	avxscalar = vex128;
13452       else if (strcasecmp (arg, "256") == 0)
13453 	avxscalar = vex256;
13454       else
13455 	as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
13456       break;
13457 
13458     case OPTION_MVEXWIG:
13459       if (strcmp (arg, "0") == 0)
13460 	vexwig = vexw0;
13461       else if (strcmp (arg, "1") == 0)
13462 	vexwig = vexw1;
13463       else
13464 	as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13465       break;
13466 
13467     case OPTION_MADD_BND_PREFIX:
13468       add_bnd_prefix = 1;
13469       break;
13470 
13471     case OPTION_MEVEXLIG:
13472       if (strcmp (arg, "128") == 0)
13473 	evexlig = evexl128;
13474       else if (strcmp (arg, "256") == 0)
13475 	evexlig = evexl256;
13476       else  if (strcmp (arg, "512") == 0)
13477 	evexlig = evexl512;
13478       else
13479 	as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13480       break;
13481 
13482     case OPTION_MEVEXRCIG:
13483       if (strcmp (arg, "rne") == 0)
13484 	evexrcig = rne;
13485       else if (strcmp (arg, "rd") == 0)
13486 	evexrcig = rd;
13487       else if (strcmp (arg, "ru") == 0)
13488 	evexrcig = ru;
13489       else if (strcmp (arg, "rz") == 0)
13490 	evexrcig = rz;
13491       else
13492 	as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13493       break;
13494 
13495     case OPTION_MEVEXWIG:
13496       if (strcmp (arg, "0") == 0)
13497 	evexwig = evexw0;
13498       else if (strcmp (arg, "1") == 0)
13499 	evexwig = evexw1;
13500       else
13501 	as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13502       break;
13503 
13504 # if defined (TE_PE) || defined (TE_PEP)
13505     case OPTION_MBIG_OBJ:
13506       use_big_obj = 1;
13507       break;
13508 #endif
13509 
13510     case OPTION_MOMIT_LOCK_PREFIX:
13511       if (strcasecmp (arg, "yes") == 0)
13512         omit_lock_prefix = 1;
13513       else if (strcasecmp (arg, "no") == 0)
13514         omit_lock_prefix = 0;
13515       else
13516         as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13517       break;
13518 
13519     case OPTION_MFENCE_AS_LOCK_ADD:
13520       if (strcasecmp (arg, "yes") == 0)
13521         avoid_fence = 1;
13522       else if (strcasecmp (arg, "no") == 0)
13523         avoid_fence = 0;
13524       else
13525         as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13526       break;
13527 
13528     case OPTION_MLFENCE_AFTER_LOAD:
13529       if (strcasecmp (arg, "yes") == 0)
13530 	lfence_after_load = 1;
13531       else if (strcasecmp (arg, "no") == 0)
13532 	lfence_after_load = 0;
13533       else
13534         as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13535       break;
13536 
13537     case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13538       if (strcasecmp (arg, "all") == 0)
13539 	{
13540 	  lfence_before_indirect_branch = lfence_branch_all;
13541 	  if (lfence_before_ret == lfence_before_ret_none)
13542 	    lfence_before_ret = lfence_before_ret_shl;
13543 	}
13544       else if (strcasecmp (arg, "memory") == 0)
13545 	lfence_before_indirect_branch = lfence_branch_memory;
13546       else if (strcasecmp (arg, "register") == 0)
13547 	lfence_before_indirect_branch = lfence_branch_register;
13548       else if (strcasecmp (arg, "none") == 0)
13549 	lfence_before_indirect_branch = lfence_branch_none;
13550       else
13551         as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13552 		  arg);
13553       break;
13554 
13555     case OPTION_MLFENCE_BEFORE_RET:
13556       if (strcasecmp (arg, "or") == 0)
13557 	lfence_before_ret = lfence_before_ret_or;
13558       else if (strcasecmp (arg, "not") == 0)
13559 	lfence_before_ret = lfence_before_ret_not;
13560       else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13561 	lfence_before_ret = lfence_before_ret_shl;
13562       else if (strcasecmp (arg, "none") == 0)
13563 	lfence_before_ret = lfence_before_ret_none;
13564       else
13565         as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13566 		  arg);
13567       break;
13568 
13569     case OPTION_MRELAX_RELOCATIONS:
13570       if (strcasecmp (arg, "yes") == 0)
13571         generate_relax_relocations = 1;
13572       else if (strcasecmp (arg, "no") == 0)
13573         generate_relax_relocations = 0;
13574       else
13575         as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13576       break;
13577 
13578     case OPTION_MALIGN_BRANCH_BOUNDARY:
13579       {
13580 	char *end;
13581 	long int align = strtoul (arg, &end, 0);
13582 	if (*end == '\0')
13583 	  {
13584 	    if (align == 0)
13585 	      {
13586 		align_branch_power = 0;
13587 		break;
13588 	      }
13589 	    else if (align >= 16)
13590 	      {
13591 		int align_power;
13592 		for (align_power = 0;
13593 		     (align & 1) == 0;
13594 		     align >>= 1, align_power++)
13595 		  continue;
13596 		/* Limit alignment power to 31.  */
13597 		if (align == 1 && align_power < 32)
13598 		  {
13599 		    align_branch_power = align_power;
13600 		    break;
13601 		  }
13602 	      }
13603 	  }
13604 	as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13605       }
13606       break;
13607 
13608     case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13609       {
13610 	char *end;
13611 	int align = strtoul (arg, &end, 0);
13612 	/* Some processors only support 5 prefixes.  */
13613 	if (*end == '\0' && align >= 0 && align < 6)
13614 	  {
13615 	    align_branch_prefix_size = align;
13616 	    break;
13617 	  }
13618 	as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13619 		  arg);
13620       }
13621       break;
13622 
13623     case OPTION_MALIGN_BRANCH:
13624       align_branch = 0;
13625       saved = xstrdup (arg);
13626       type = saved;
13627       do
13628 	{
13629 	  next = strchr (type, '+');
13630 	  if (next)
13631 	    *next++ = '\0';
13632 	  if (strcasecmp (type, "jcc") == 0)
13633 	    align_branch |= align_branch_jcc_bit;
13634 	  else if (strcasecmp (type, "fused") == 0)
13635 	    align_branch |= align_branch_fused_bit;
13636 	  else if (strcasecmp (type, "jmp") == 0)
13637 	    align_branch |= align_branch_jmp_bit;
13638 	  else if (strcasecmp (type, "call") == 0)
13639 	    align_branch |= align_branch_call_bit;
13640 	  else if (strcasecmp (type, "ret") == 0)
13641 	    align_branch |= align_branch_ret_bit;
13642 	  else if (strcasecmp (type, "indirect") == 0)
13643 	    align_branch |= align_branch_indirect_bit;
13644 	  else
13645 	    as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13646 	  type = next;
13647 	}
13648       while (next != NULL);
13649       free (saved);
13650       break;
13651 
13652     case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13653       align_branch_power = 5;
13654       align_branch_prefix_size = 5;
13655       align_branch = (align_branch_jcc_bit
13656 		      | align_branch_fused_bit
13657 		      | align_branch_jmp_bit);
13658       break;
13659 
13660     case OPTION_MAMD64:
13661       isa64 = amd64;
13662       break;
13663 
13664     case OPTION_MINTEL64:
13665       isa64 = intel64;
13666       break;
13667 
13668     case 'O':
13669       if (arg == NULL)
13670 	{
13671 	  optimize = 1;
13672 	  /* Turn off -Os.  */
13673 	  optimize_for_space = 0;
13674 	}
13675       else if (*arg == 's')
13676 	{
13677 	  optimize_for_space = 1;
13678 	  /* Turn on all encoding optimizations.  */
13679 	  optimize = INT_MAX;
13680 	}
13681       else
13682 	{
13683 	  optimize = atoi (arg);
13684 	  /* Turn off -Os.  */
13685 	  optimize_for_space = 0;
13686 	}
13687       break;
13688 
13689     default:
13690       return 0;
13691     }
13692   return 1;
13693 }
13694 
13695 #define MESSAGE_TEMPLATE \
13696 "                                                                                "
13697 
13698 static char *
output_message(FILE * stream,char * p,char * message,char * start,int * left_p,const char * name,int len)13699 output_message (FILE *stream, char *p, char *message, char *start,
13700 		int *left_p, const char *name, int len)
13701 {
13702   int size = sizeof (MESSAGE_TEMPLATE);
13703   int left = *left_p;
13704 
13705   /* Reserve 2 spaces for ", " or ",\0" */
13706   left -= len + 2;
13707 
13708   /* Check if there is any room.  */
13709   if (left >= 0)
13710     {
13711       if (p != start)
13712 	{
13713 	  *p++ = ',';
13714 	  *p++ = ' ';
13715 	}
13716       p = mempcpy (p, name, len);
13717     }
13718   else
13719     {
13720       /* Output the current message now and start a new one.  */
13721       *p++ = ',';
13722       *p = '\0';
13723       fprintf (stream, "%s\n", message);
13724       p = start;
13725       left = size - (start - message) - len - 2;
13726 
13727       gas_assert (left >= 0);
13728 
13729       p = mempcpy (p, name, len);
13730     }
13731 
13732   *left_p = left;
13733   return p;
13734 }
13735 
13736 static void
show_arch(FILE * stream,int ext,int check)13737 show_arch (FILE *stream, int ext, int check)
13738 {
13739   static char message[] = MESSAGE_TEMPLATE;
13740   char *start = message + 27;
13741   char *p;
13742   int size = sizeof (MESSAGE_TEMPLATE);
13743   int left;
13744   const char *name;
13745   int len;
13746   unsigned int j;
13747 
13748   p = start;
13749   left = size - (start - message);
13750 
13751   if (!ext && check)
13752     {
13753       p = output_message (stream, p, message, start, &left,
13754 			  STRING_COMMA_LEN ("default"));
13755       p = output_message (stream, p, message, start, &left,
13756 			  STRING_COMMA_LEN ("push"));
13757       p = output_message (stream, p, message, start, &left,
13758 			  STRING_COMMA_LEN ("pop"));
13759     }
13760 
13761   for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13762     {
13763       /* Should it be skipped?  */
13764       if (cpu_arch [j].skip)
13765 	continue;
13766 
13767       name = cpu_arch [j].name;
13768       len = cpu_arch [j].len;
13769       if (cpu_arch[j].type == PROCESSOR_NONE)
13770 	{
13771 	  /* It is an extension.  Skip if we aren't asked to show it.  */
13772 	  if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
13773 	    continue;
13774 	}
13775       else if (ext)
13776 	{
13777 	  /* It is an processor.  Skip if we show only extension.  */
13778 	  continue;
13779 	}
13780       else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
13781 	{
13782 	  /* It is an impossible processor - skip.  */
13783 	  continue;
13784 	}
13785 
13786       p = output_message (stream, p, message, start, &left, name, len);
13787     }
13788 
13789   /* Display disabled extensions.  */
13790   if (ext)
13791     for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13792       {
13793 	char *str;
13794 
13795 	if (cpu_arch[j].type != PROCESSOR_NONE
13796 	    || !cpu_flags_all_zero (&cpu_arch[j].enable))
13797 	  continue;
13798 	str = xasprintf ("no%s", cpu_arch[j].name);
13799 	p = output_message (stream, p, message, start, &left, str,
13800 			    strlen (str));
13801 	free (str);
13802       }
13803 
13804   *p = '\0';
13805   fprintf (stream, "%s\n", message);
13806 }
13807 
13808 void
md_show_usage(FILE * stream)13809 md_show_usage (FILE *stream)
13810 {
13811 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13812   fprintf (stream, _("\
13813   -Qy, -Qn                ignored\n\
13814   -V                      print assembler version number\n\
13815   -k                      ignored\n"));
13816 #endif
13817   fprintf (stream, _("\
13818   -n                      Do not optimize code alignment\n\
13819   -q                      quieten some warnings\n"));
13820 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13821   fprintf (stream, _("\
13822   -s                      ignored\n"));
13823 #endif
13824 #ifdef BFD64
13825 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13826   fprintf (stream, _("\
13827   --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
13828 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
13829   fprintf (stream, _("\
13830   --32/--64               generate 32bit/64bit object\n"));
13831 # endif
13832 #endif
13833 #ifdef SVR4_COMMENT_CHARS
13834   fprintf (stream, _("\
13835   --divide                do not treat `/' as a comment character\n"));
13836 #else
13837   fprintf (stream, _("\
13838   --divide                ignored\n"));
13839 #endif
13840   fprintf (stream, _("\
13841   -march=CPU[,+EXTENSION...]\n\
13842                           generate code for CPU and EXTENSION, CPU is one of:\n"));
13843   show_arch (stream, 0, 1);
13844   fprintf (stream, _("\
13845                           EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
13846   show_arch (stream, 1, 0);
13847   fprintf (stream, _("\
13848   -mtune=CPU              optimize for CPU, CPU is one of:\n"));
13849   show_arch (stream, 0, 0);
13850   fprintf (stream, _("\
13851   -msse2avx               encode SSE instructions with VEX prefix\n"));
13852   fprintf (stream, _("\
13853   -muse-unaligned-vector-move\n\
13854                           encode aligned vector move as unaligned vector move\n"));
13855   fprintf (stream, _("\
13856   -msse-check=[none|error|warning] (default: warning)\n\
13857                           check SSE instructions\n"));
13858   fprintf (stream, _("\
13859   -moperand-check=[none|error|warning] (default: warning)\n\
13860                           check operand combinations for validity\n"));
13861   fprintf (stream, _("\
13862   -mavxscalar=[128|256] (default: 128)\n\
13863                           encode scalar AVX instructions with specific vector\n\
13864                            length\n"));
13865   fprintf (stream, _("\
13866   -mvexwig=[0|1] (default: 0)\n\
13867                           encode VEX instructions with specific VEX.W value\n\
13868                            for VEX.W bit ignored instructions\n"));
13869   fprintf (stream, _("\
13870   -mevexlig=[128|256|512] (default: 128)\n\
13871                           encode scalar EVEX instructions with specific vector\n\
13872                            length\n"));
13873   fprintf (stream, _("\
13874   -mevexwig=[0|1] (default: 0)\n\
13875                           encode EVEX instructions with specific EVEX.W value\n\
13876                            for EVEX.W bit ignored instructions\n"));
13877   fprintf (stream, _("\
13878   -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
13879                           encode EVEX instructions with specific EVEX.RC value\n\
13880                            for SAE-only ignored instructions\n"));
13881   fprintf (stream, _("\
13882   -mmnemonic=[att|intel] "));
13883   if (SYSV386_COMPAT)
13884     fprintf (stream, _("(default: att)\n"));
13885   else
13886     fprintf (stream, _("(default: intel)\n"));
13887   fprintf (stream, _("\
13888                           use AT&T/Intel mnemonic\n"));
13889   fprintf (stream, _("\
13890   -msyntax=[att|intel] (default: att)\n\
13891                           use AT&T/Intel syntax\n"));
13892   fprintf (stream, _("\
13893   -mindex-reg             support pseudo index registers\n"));
13894   fprintf (stream, _("\
13895   -mnaked-reg             don't require `%%' prefix for registers\n"));
13896   fprintf (stream, _("\
13897   -madd-bnd-prefix        add BND prefix for all valid branches\n"));
13898 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13899   fprintf (stream, _("\
13900   -mshared                disable branch optimization for shared code\n"));
13901   fprintf (stream, _("\
13902   -mx86-used-note=[no|yes] "));
13903   if (DEFAULT_X86_USED_NOTE)
13904     fprintf (stream, _("(default: yes)\n"));
13905   else
13906     fprintf (stream, _("(default: no)\n"));
13907   fprintf (stream, _("\
13908                           generate x86 used ISA and feature properties\n"));
13909 #endif
13910 #if defined (TE_PE) || defined (TE_PEP)
13911   fprintf (stream, _("\
13912   -mbig-obj               generate big object files\n"));
13913 #endif
13914   fprintf (stream, _("\
13915   -momit-lock-prefix=[no|yes] (default: no)\n\
13916                           strip all lock prefixes\n"));
13917   fprintf (stream, _("\
13918   -mfence-as-lock-add=[no|yes] (default: no)\n\
13919                           encode lfence, mfence and sfence as\n\
13920                            lock addl $0x0, (%%{re}sp)\n"));
13921   fprintf (stream, _("\
13922   -mrelax-relocations=[no|yes] "));
13923   if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13924     fprintf (stream, _("(default: yes)\n"));
13925   else
13926     fprintf (stream, _("(default: no)\n"));
13927   fprintf (stream, _("\
13928                           generate relax relocations\n"));
13929   fprintf (stream, _("\
13930   -malign-branch-boundary=NUM (default: 0)\n\
13931                           align branches within NUM byte boundary\n"));
13932   fprintf (stream, _("\
13933   -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13934                           TYPE is combination of jcc, fused, jmp, call, ret,\n\
13935                            indirect\n\
13936                           specify types of branches to align\n"));
13937   fprintf (stream, _("\
13938   -malign-branch-prefix-size=NUM (default: 5)\n\
13939                           align branches with NUM prefixes per instruction\n"));
13940   fprintf (stream, _("\
13941   -mbranches-within-32B-boundaries\n\
13942                           align branches within 32 byte boundary\n"));
13943   fprintf (stream, _("\
13944   -mlfence-after-load=[no|yes] (default: no)\n\
13945                           generate lfence after load\n"));
13946   fprintf (stream, _("\
13947   -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13948                           generate lfence before indirect near branch\n"));
13949   fprintf (stream, _("\
13950   -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
13951                           generate lfence before ret\n"));
13952   fprintf (stream, _("\
13953   -mamd64                 accept only AMD64 ISA [default]\n"));
13954   fprintf (stream, _("\
13955   -mintel64               accept only Intel64 ISA\n"));
13956 }
13957 
13958 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
13959      || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13960      || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13961 
13962 /* Pick the target format to use.  */
13963 
13964 const char *
i386_target_format(void)13965 i386_target_format (void)
13966 {
13967   if (startswith (default_arch, "x86_64"))
13968     {
13969       update_code_flag (CODE_64BIT, 1);
13970       if (default_arch[6] == '\0')
13971 	x86_elf_abi = X86_64_ABI;
13972       else
13973 	x86_elf_abi = X86_64_X32_ABI;
13974     }
13975   else if (!strcmp (default_arch, "i386"))
13976     update_code_flag (CODE_32BIT, 1);
13977   else if (!strcmp (default_arch, "iamcu"))
13978     {
13979       update_code_flag (CODE_32BIT, 1);
13980       if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13981 	{
13982 	  static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13983 	  cpu_arch_name = "iamcu";
13984 	  free (cpu_sub_arch_name);
13985 	  cpu_sub_arch_name = NULL;
13986 	  cpu_arch_flags = iamcu_flags;
13987 	  cpu_arch_isa = PROCESSOR_IAMCU;
13988 	  cpu_arch_isa_flags = iamcu_flags;
13989 	  if (!cpu_arch_tune_set)
13990 	    {
13991 	      cpu_arch_tune = cpu_arch_isa;
13992 	      cpu_arch_tune_flags = cpu_arch_isa_flags;
13993 	    }
13994 	}
13995       else if (cpu_arch_isa != PROCESSOR_IAMCU)
13996 	as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13997 		  cpu_arch_name);
13998     }
13999   else
14000     as_fatal (_("unknown architecture"));
14001 
14002   if (cpu_flags_all_zero (&cpu_arch_isa_flags))
14003     cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
14004   if (cpu_flags_all_zero (&cpu_arch_tune_flags))
14005     cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
14006 
14007   switch (OUTPUT_FLAVOR)
14008     {
14009 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
14010     case bfd_target_aout_flavour:
14011       return AOUT_TARGET_FORMAT;
14012 #endif
14013 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14014 # if defined (TE_PE) || defined (TE_PEP)
14015     case bfd_target_coff_flavour:
14016       if (flag_code == CODE_64BIT)
14017 	{
14018 	  object_64bit = 1;
14019 	  return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14020 	}
14021       return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
14022 # elif defined (TE_GO32)
14023     case bfd_target_coff_flavour:
14024       return "coff-go32";
14025 # else
14026     case bfd_target_coff_flavour:
14027       return "coff-i386";
14028 # endif
14029 #endif
14030 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14031     case bfd_target_elf_flavour:
14032       {
14033 	const char *format;
14034 
14035 	switch (x86_elf_abi)
14036 	  {
14037 	  default:
14038 	    format = ELF_TARGET_FORMAT;
14039 #ifndef TE_SOLARIS
14040 	    tls_get_addr = "___tls_get_addr";
14041 #endif
14042 	    break;
14043 	  case X86_64_ABI:
14044 	    use_rela_relocations = 1;
14045 	    object_64bit = 1;
14046 #ifndef TE_SOLARIS
14047 	    tls_get_addr = "__tls_get_addr";
14048 #endif
14049 	    format = ELF_TARGET_FORMAT64;
14050 	    break;
14051 	  case X86_64_X32_ABI:
14052 	    use_rela_relocations = 1;
14053 	    object_64bit = 1;
14054 #ifndef TE_SOLARIS
14055 	    tls_get_addr = "__tls_get_addr";
14056 #endif
14057 	    disallow_64bit_reloc = 1;
14058 	    format = ELF_TARGET_FORMAT32;
14059 	    break;
14060 	  }
14061 	if (cpu_arch_isa == PROCESSOR_IAMCU)
14062 	  {
14063 	    if (x86_elf_abi != I386_ABI)
14064 	      as_fatal (_("Intel MCU is 32bit only"));
14065 	    return ELF_TARGET_IAMCU_FORMAT;
14066 	  }
14067 	else
14068 	  return format;
14069       }
14070 #endif
14071 #if defined (OBJ_MACH_O)
14072     case bfd_target_mach_o_flavour:
14073       if (flag_code == CODE_64BIT)
14074 	{
14075 	  use_rela_relocations = 1;
14076 	  object_64bit = 1;
14077 	  return "mach-o-x86-64";
14078 	}
14079       else
14080 	return "mach-o-i386";
14081 #endif
14082     default:
14083       abort ();
14084       return NULL;
14085     }
14086 }
14087 
14088 #endif /* OBJ_MAYBE_ more than one  */
14089 
14090 symbolS *
md_undefined_symbol(char * name)14091 md_undefined_symbol (char *name)
14092 {
14093   if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14094       && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14095       && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14096       && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
14097     {
14098       if (!GOT_symbol)
14099 	{
14100 	  if (symbol_find (name))
14101 	    as_bad (_("GOT already in symbol table"));
14102 	  GOT_symbol = symbol_new (name, undefined_section,
14103 				   &zero_address_frag, 0);
14104 	};
14105       return GOT_symbol;
14106     }
14107   return 0;
14108 }
14109 
14110 /* Round up a section size to the appropriate boundary.  */
14111 
14112 valueT
md_section_align(segT segment ATTRIBUTE_UNUSED,valueT size)14113 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
14114 {
14115 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14116   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14117     {
14118       /* For a.out, force the section size to be aligned.  If we don't do
14119 	 this, BFD will align it for us, but it will not write out the
14120 	 final bytes of the section.  This may be a bug in BFD, but it is
14121 	 easier to fix it here since that is how the other a.out targets
14122 	 work.  */
14123       int align;
14124 
14125       align = bfd_section_alignment (segment);
14126       size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
14127     }
14128 #endif
14129 
14130   return size;
14131 }
14132 
14133 /* On the i386, PC-relative offsets are relative to the start of the
14134    next instruction.  That is, the address of the offset, plus its
14135    size, since the offset is always the last part of the insn.  */
14136 
14137 long
md_pcrel_from(fixS * fixP)14138 md_pcrel_from (fixS *fixP)
14139 {
14140   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14141 }
14142 
14143 #ifndef I386COFF
14144 
14145 static void
s_bss(int ignore ATTRIBUTE_UNUSED)14146 s_bss (int ignore ATTRIBUTE_UNUSED)
14147 {
14148   int temp;
14149 
14150 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14151   if (IS_ELF)
14152     obj_elf_section_change_hook ();
14153 #endif
14154   temp = get_absolute_expression ();
14155   subseg_set (bss_section, (subsegT) temp);
14156   demand_empty_rest_of_line ();
14157 }
14158 
14159 #endif
14160 
14161 /* Remember constant directive.  */
14162 
14163 void
i386_cons_align(int ignore ATTRIBUTE_UNUSED)14164 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14165 {
14166   if (last_insn.kind != last_insn_directive
14167       && (bfd_section_flags (now_seg) & SEC_CODE))
14168     {
14169       last_insn.seg = now_seg;
14170       last_insn.kind = last_insn_directive;
14171       last_insn.name = "constant directive";
14172       last_insn.file = as_where (&last_insn.line);
14173       if (lfence_before_ret != lfence_before_ret_none)
14174 	{
14175 	  if (lfence_before_indirect_branch != lfence_branch_none)
14176 	    as_warn (_("constant directive skips -mlfence-before-ret "
14177 		       "and -mlfence-before-indirect-branch"));
14178 	  else
14179 	    as_warn (_("constant directive skips -mlfence-before-ret"));
14180 	}
14181       else if (lfence_before_indirect_branch != lfence_branch_none)
14182 	as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
14183     }
14184 }
14185 
14186 int
i386_validate_fix(fixS * fixp)14187 i386_validate_fix (fixS *fixp)
14188 {
14189   if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14190     {
14191       reloc_howto_type *howto;
14192 
14193       howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14194       as_bad_where (fixp->fx_file, fixp->fx_line,
14195 		    _("invalid %s relocation against register"),
14196 		    howto ? howto->name : "<unknown>");
14197       return 0;
14198     }
14199 
14200 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14201   if (fixp->fx_r_type == BFD_RELOC_SIZE32
14202       || fixp->fx_r_type == BFD_RELOC_SIZE64)
14203     return IS_ELF && fixp->fx_addsy
14204 	   && (!S_IS_DEFINED (fixp->fx_addsy)
14205 	       || S_IS_EXTERNAL (fixp->fx_addsy));
14206 #endif
14207 
14208   if (fixp->fx_subsy)
14209     {
14210       if (fixp->fx_subsy == GOT_symbol)
14211 	{
14212 	  if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14213 	    {
14214 	      if (!object_64bit)
14215 		abort ();
14216 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14217 	      if (fixp->fx_tcbit2)
14218 		fixp->fx_r_type = (fixp->fx_tcbit
14219 				   ? BFD_RELOC_X86_64_REX_GOTPCRELX
14220 				   : BFD_RELOC_X86_64_GOTPCRELX);
14221 	      else
14222 #endif
14223 		fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14224 	    }
14225 	  else
14226 	    {
14227 	      if (!object_64bit)
14228 		fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14229 	      else
14230 		fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14231 	    }
14232 	  fixp->fx_subsy = 0;
14233 	}
14234     }
14235 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14236   else
14237     {
14238       /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14239 	 to section.  Since PLT32 relocation must be against symbols,
14240 	 turn such PLT32 relocation into PC32 relocation.  */
14241       if (fixp->fx_addsy
14242 	  && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14243 	      || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14244 	  && symbol_section_p (fixp->fx_addsy))
14245 	fixp->fx_r_type = BFD_RELOC_32_PCREL;
14246       if (!object_64bit)
14247 	{
14248 	  if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14249 	      && fixp->fx_tcbit2)
14250 	    fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14251 	}
14252     }
14253 #endif
14254 
14255   return 1;
14256 }
14257 
14258 arelent *
tc_gen_reloc(asection * section ATTRIBUTE_UNUSED,fixS * fixp)14259 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
14260 {
14261   arelent *rel;
14262   bfd_reloc_code_real_type code;
14263 
14264   switch (fixp->fx_r_type)
14265     {
14266 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14267       symbolS *sym;
14268 
14269     case BFD_RELOC_SIZE32:
14270     case BFD_RELOC_SIZE64:
14271       if (fixp->fx_addsy
14272 	  && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14273 	  && (!fixp->fx_subsy
14274 	      || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14275 	sym = fixp->fx_addsy;
14276       else if (fixp->fx_subsy
14277 	       && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14278 	       && (!fixp->fx_addsy
14279 		   || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14280 	sym = fixp->fx_subsy;
14281       else
14282 	sym = NULL;
14283       if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
14284 	{
14285 	  /* Resolve size relocation against local symbol to size of
14286 	     the symbol plus addend.  */
14287 	  valueT value = S_GET_SIZE (sym);
14288 
14289 	  if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14290 	    value = bfd_section_size (S_GET_SEGMENT (sym));
14291 	  if (sym == fixp->fx_subsy)
14292 	    {
14293 	      value = -value;
14294 	      if (fixp->fx_addsy)
14295 	        value += S_GET_VALUE (fixp->fx_addsy);
14296 	    }
14297 	  else if (fixp->fx_subsy)
14298 	    value -= S_GET_VALUE (fixp->fx_subsy);
14299 	  value += fixp->fx_offset;
14300 	  if (fixp->fx_r_type == BFD_RELOC_SIZE32
14301 	      && object_64bit
14302 	      && !fits_in_unsigned_long (value))
14303 	    as_bad_where (fixp->fx_file, fixp->fx_line,
14304 			  _("symbol size computation overflow"));
14305 	  fixp->fx_addsy = NULL;
14306 	  fixp->fx_subsy = NULL;
14307 	  md_apply_fix (fixp, (valueT *) &value, NULL);
14308 	  return NULL;
14309 	}
14310       if (!fixp->fx_addsy || fixp->fx_subsy)
14311 	{
14312 	  as_bad_where (fixp->fx_file, fixp->fx_line,
14313 			"unsupported expression involving @size");
14314 	  return NULL;
14315 	}
14316 #endif
14317       /* Fall through.  */
14318 
14319     case BFD_RELOC_X86_64_PLT32:
14320     case BFD_RELOC_X86_64_GOT32:
14321     case BFD_RELOC_X86_64_GOTPCREL:
14322     case BFD_RELOC_X86_64_GOTPCRELX:
14323     case BFD_RELOC_X86_64_REX_GOTPCRELX:
14324     case BFD_RELOC_386_PLT32:
14325     case BFD_RELOC_386_GOT32:
14326     case BFD_RELOC_386_GOT32X:
14327     case BFD_RELOC_386_GOTOFF:
14328     case BFD_RELOC_386_GOTPC:
14329     case BFD_RELOC_386_TLS_GD:
14330     case BFD_RELOC_386_TLS_LDM:
14331     case BFD_RELOC_386_TLS_LDO_32:
14332     case BFD_RELOC_386_TLS_IE_32:
14333     case BFD_RELOC_386_TLS_IE:
14334     case BFD_RELOC_386_TLS_GOTIE:
14335     case BFD_RELOC_386_TLS_LE_32:
14336     case BFD_RELOC_386_TLS_LE:
14337     case BFD_RELOC_386_TLS_GOTDESC:
14338     case BFD_RELOC_386_TLS_DESC_CALL:
14339     case BFD_RELOC_X86_64_TLSGD:
14340     case BFD_RELOC_X86_64_TLSLD:
14341     case BFD_RELOC_X86_64_DTPOFF32:
14342     case BFD_RELOC_X86_64_DTPOFF64:
14343     case BFD_RELOC_X86_64_GOTTPOFF:
14344     case BFD_RELOC_X86_64_TPOFF32:
14345     case BFD_RELOC_X86_64_TPOFF64:
14346     case BFD_RELOC_X86_64_GOTOFF64:
14347     case BFD_RELOC_X86_64_GOTPC32:
14348     case BFD_RELOC_X86_64_GOT64:
14349     case BFD_RELOC_X86_64_GOTPCREL64:
14350     case BFD_RELOC_X86_64_GOTPC64:
14351     case BFD_RELOC_X86_64_GOTPLT64:
14352     case BFD_RELOC_X86_64_PLTOFF64:
14353     case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14354     case BFD_RELOC_X86_64_TLSDESC_CALL:
14355     case BFD_RELOC_RVA:
14356     case BFD_RELOC_VTABLE_ENTRY:
14357     case BFD_RELOC_VTABLE_INHERIT:
14358 #ifdef TE_PE
14359     case BFD_RELOC_32_SECREL:
14360     case BFD_RELOC_16_SECIDX:
14361 #endif
14362       code = fixp->fx_r_type;
14363       break;
14364     case BFD_RELOC_X86_64_32S:
14365       if (!fixp->fx_pcrel)
14366 	{
14367 	  /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
14368 	  code = fixp->fx_r_type;
14369 	  break;
14370 	}
14371       /* Fall through.  */
14372     default:
14373       if (fixp->fx_pcrel)
14374 	{
14375 	  switch (fixp->fx_size)
14376 	    {
14377 	    default:
14378 	      as_bad_where (fixp->fx_file, fixp->fx_line,
14379 			    _("can not do %d byte pc-relative relocation"),
14380 			    fixp->fx_size);
14381 	      code = BFD_RELOC_32_PCREL;
14382 	      break;
14383 	    case 1: code = BFD_RELOC_8_PCREL;  break;
14384 	    case 2: code = BFD_RELOC_16_PCREL; break;
14385 	    case 4: code = BFD_RELOC_32_PCREL; break;
14386 #ifdef BFD64
14387 	    case 8: code = BFD_RELOC_64_PCREL; break;
14388 #endif
14389 	    }
14390 	}
14391       else
14392 	{
14393 	  switch (fixp->fx_size)
14394 	    {
14395 	    default:
14396 	      as_bad_where (fixp->fx_file, fixp->fx_line,
14397 			    _("can not do %d byte relocation"),
14398 			    fixp->fx_size);
14399 	      code = BFD_RELOC_32;
14400 	      break;
14401 	    case 1: code = BFD_RELOC_8;  break;
14402 	    case 2: code = BFD_RELOC_16; break;
14403 	    case 4: code = BFD_RELOC_32; break;
14404 #ifdef BFD64
14405 	    case 8: code = BFD_RELOC_64; break;
14406 #endif
14407 	    }
14408 	}
14409       break;
14410     }
14411 
14412   if ((code == BFD_RELOC_32
14413        || code == BFD_RELOC_32_PCREL
14414        || code == BFD_RELOC_X86_64_32S)
14415       && GOT_symbol
14416       && fixp->fx_addsy == GOT_symbol)
14417     {
14418       if (!object_64bit)
14419 	code = BFD_RELOC_386_GOTPC;
14420       else
14421 	code = BFD_RELOC_X86_64_GOTPC32;
14422     }
14423   if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14424       && GOT_symbol
14425       && fixp->fx_addsy == GOT_symbol)
14426     {
14427       code = BFD_RELOC_X86_64_GOTPC64;
14428     }
14429 
14430   rel = XNEW (arelent);
14431   rel->sym_ptr_ptr = XNEW (asymbol *);
14432   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
14433 
14434   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
14435 
14436   if (!use_rela_relocations)
14437     {
14438       /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14439 	 vtable entry to be used in the relocation's section offset.  */
14440       if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14441 	rel->address = fixp->fx_offset;
14442 #if defined (OBJ_COFF) && defined (TE_PE)
14443       else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14444 	rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14445       else
14446 #endif
14447       rel->addend = 0;
14448     }
14449   /* Use the rela in 64bit mode.  */
14450   else
14451     {
14452       if (disallow_64bit_reloc)
14453 	switch (code)
14454 	  {
14455 	  case BFD_RELOC_X86_64_DTPOFF64:
14456 	  case BFD_RELOC_X86_64_TPOFF64:
14457 	  case BFD_RELOC_64_PCREL:
14458 	  case BFD_RELOC_X86_64_GOTOFF64:
14459 	  case BFD_RELOC_X86_64_GOT64:
14460 	  case BFD_RELOC_X86_64_GOTPCREL64:
14461 	  case BFD_RELOC_X86_64_GOTPC64:
14462 	  case BFD_RELOC_X86_64_GOTPLT64:
14463 	  case BFD_RELOC_X86_64_PLTOFF64:
14464 	    as_bad_where (fixp->fx_file, fixp->fx_line,
14465 			  _("cannot represent relocation type %s in x32 mode"),
14466 			  bfd_get_reloc_code_name (code));
14467 	    break;
14468 	  default:
14469 	    break;
14470 	  }
14471 
14472       if (!fixp->fx_pcrel)
14473 	rel->addend = fixp->fx_offset;
14474       else
14475 	switch (code)
14476 	  {
14477 	  case BFD_RELOC_X86_64_PLT32:
14478 	  case BFD_RELOC_X86_64_GOT32:
14479 	  case BFD_RELOC_X86_64_GOTPCREL:
14480 	  case BFD_RELOC_X86_64_GOTPCRELX:
14481 	  case BFD_RELOC_X86_64_REX_GOTPCRELX:
14482 	  case BFD_RELOC_X86_64_TLSGD:
14483 	  case BFD_RELOC_X86_64_TLSLD:
14484 	  case BFD_RELOC_X86_64_GOTTPOFF:
14485 	  case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14486 	  case BFD_RELOC_X86_64_TLSDESC_CALL:
14487 	    rel->addend = fixp->fx_offset - fixp->fx_size;
14488 	    break;
14489 	  default:
14490 	    rel->addend = (section->vma
14491 			   - fixp->fx_size
14492 			   + fixp->fx_addnumber
14493 			   + md_pcrel_from (fixp));
14494 	    break;
14495 	  }
14496     }
14497 
14498   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14499   if (rel->howto == NULL)
14500     {
14501       as_bad_where (fixp->fx_file, fixp->fx_line,
14502 		    _("cannot represent relocation type %s"),
14503 		    bfd_get_reloc_code_name (code));
14504       /* Set howto to a garbage value so that we can keep going.  */
14505       rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
14506       gas_assert (rel->howto != NULL);
14507     }
14508 
14509   return rel;
14510 }
14511 
14512 #include "tc-i386-intel.c"
14513 
14514 void
tc_x86_parse_to_dw2regnum(expressionS * exp)14515 tc_x86_parse_to_dw2regnum (expressionS *exp)
14516 {
14517   int saved_naked_reg;
14518   char saved_register_dot;
14519 
14520   saved_naked_reg = allow_naked_reg;
14521   allow_naked_reg = 1;
14522   saved_register_dot = register_chars['.'];
14523   register_chars['.'] = '.';
14524   allow_pseudo_reg = 1;
14525   expression_and_evaluate (exp);
14526   allow_pseudo_reg = 0;
14527   register_chars['.'] = saved_register_dot;
14528   allow_naked_reg = saved_naked_reg;
14529 
14530   if (exp->X_op == O_register && exp->X_add_number >= 0)
14531     {
14532       if ((addressT) exp->X_add_number < i386_regtab_size)
14533 	{
14534 	  exp->X_op = O_constant;
14535 	  exp->X_add_number = i386_regtab[exp->X_add_number]
14536 			      .dw2_regnum[flag_code >> 1];
14537 	}
14538       else
14539 	exp->X_op = O_illegal;
14540     }
14541 }
14542 
14543 void
tc_x86_frame_initial_instructions(void)14544 tc_x86_frame_initial_instructions (void)
14545 {
14546   static unsigned int sp_regno[2];
14547 
14548   if (!sp_regno[flag_code >> 1])
14549     {
14550       char *saved_input = input_line_pointer;
14551       char sp[][4] = {"esp", "rsp"};
14552       expressionS exp;
14553 
14554       input_line_pointer = sp[flag_code >> 1];
14555       tc_x86_parse_to_dw2regnum (&exp);
14556       gas_assert (exp.X_op == O_constant);
14557       sp_regno[flag_code >> 1] = exp.X_add_number;
14558       input_line_pointer = saved_input;
14559     }
14560 
14561   cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14562   cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
14563 }
14564 
14565 int
x86_dwarf2_addr_size(void)14566 x86_dwarf2_addr_size (void)
14567 {
14568 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14569   if (x86_elf_abi == X86_64_X32_ABI)
14570     return 4;
14571 #endif
14572   return bfd_arch_bits_per_address (stdoutput) / 8;
14573 }
14574 
14575 int
i386_elf_section_type(const char * str,size_t len)14576 i386_elf_section_type (const char *str, size_t len)
14577 {
14578   if (flag_code == CODE_64BIT
14579       && len == sizeof ("unwind") - 1
14580       && startswith (str, "unwind"))
14581     return SHT_X86_64_UNWIND;
14582 
14583   return -1;
14584 }
14585 
14586 #ifdef TE_SOLARIS
14587 void
i386_solaris_fix_up_eh_frame(segT sec)14588 i386_solaris_fix_up_eh_frame (segT sec)
14589 {
14590   if (flag_code == CODE_64BIT)
14591     elf_section_type (sec) = SHT_X86_64_UNWIND;
14592 }
14593 #endif
14594 
14595 #ifdef TE_PE
14596 void
tc_pe_dwarf2_emit_offset(symbolS * symbol,unsigned int size)14597 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14598 {
14599   expressionS exp;
14600 
14601   exp.X_op = O_secrel;
14602   exp.X_add_symbol = symbol;
14603   exp.X_add_number = 0;
14604   emit_expr (&exp, size);
14605 }
14606 #endif
14607 
14608 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14609 /* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
14610 
14611 bfd_vma
x86_64_section_letter(int letter,const char ** ptr_msg)14612 x86_64_section_letter (int letter, const char **ptr_msg)
14613 {
14614   if (flag_code == CODE_64BIT)
14615     {
14616       if (letter == 'l')
14617 	return SHF_X86_64_LARGE;
14618 
14619       *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
14620     }
14621   else
14622     *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
14623   return -1;
14624 }
14625 
14626 bfd_vma
x86_64_section_word(char * str,size_t len)14627 x86_64_section_word (char *str, size_t len)
14628 {
14629   if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
14630     return SHF_X86_64_LARGE;
14631 
14632   return -1;
14633 }
14634 
14635 static void
handle_large_common(int small ATTRIBUTE_UNUSED)14636 handle_large_common (int small ATTRIBUTE_UNUSED)
14637 {
14638   if (flag_code != CODE_64BIT)
14639     {
14640       s_comm_internal (0, elf_common_parse);
14641       as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14642     }
14643   else
14644     {
14645       static segT lbss_section;
14646       asection *saved_com_section_ptr = elf_com_section_ptr;
14647       asection *saved_bss_section = bss_section;
14648 
14649       if (lbss_section == NULL)
14650 	{
14651 	  flagword applicable;
14652 	  segT seg = now_seg;
14653 	  subsegT subseg = now_subseg;
14654 
14655 	  /* The .lbss section is for local .largecomm symbols.  */
14656 	  lbss_section = subseg_new (".lbss", 0);
14657 	  applicable = bfd_applicable_section_flags (stdoutput);
14658 	  bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
14659 	  seg_info (lbss_section)->bss = 1;
14660 
14661 	  subseg_set (seg, subseg);
14662 	}
14663 
14664       elf_com_section_ptr = &_bfd_elf_large_com_section;
14665       bss_section = lbss_section;
14666 
14667       s_comm_internal (0, elf_common_parse);
14668 
14669       elf_com_section_ptr = saved_com_section_ptr;
14670       bss_section = saved_bss_section;
14671     }
14672 }
14673 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14674