Limit multi-byte nop instructions to 10 bytes
[deliverable/binutils-gdb.git] / gas / config / tc-i386.c
1 /* tc-i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989-2015 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
36 #ifdef TE_LINUX
37 /* Default to compress debug sections for Linux. */
38 int flag_compress_debug = 1;
39 #endif
40
41 #ifndef REGISTER_WARNINGS
42 #define REGISTER_WARNINGS 1
43 #endif
44
45 #ifndef INFER_ADDR_PREFIX
46 #define INFER_ADDR_PREFIX 1
47 #endif
48
49 #ifndef DEFAULT_ARCH
50 #define DEFAULT_ARCH "i386"
51 #endif
52
53 #ifndef INLINE
54 #if __GNUC__ >= 2
55 #define INLINE __inline__
56 #else
57 #define INLINE
58 #endif
59 #endif
60
61 /* Prefixes will be emitted in the order defined below.
62 WAIT_PREFIX must be the first prefix since FWAIT is really is an
63 instruction, and so must come before any prefixes.
64 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
65 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
66 #define WAIT_PREFIX 0
67 #define SEG_PREFIX 1
68 #define ADDR_PREFIX 2
69 #define DATA_PREFIX 3
70 #define REP_PREFIX 4
71 #define HLE_PREFIX REP_PREFIX
72 #define BND_PREFIX REP_PREFIX
73 #define LOCK_PREFIX 5
74 #define REX_PREFIX 6 /* must come last. */
75 #define MAX_PREFIXES 7 /* max prefixes per opcode */
76
77 /* we define the syntax here (modulo base,index,scale syntax) */
78 #define REGISTER_PREFIX '%'
79 #define IMMEDIATE_PREFIX '$'
80 #define ABSOLUTE_PREFIX '*'
81
82 /* these are the instruction mnemonic suffixes in AT&T syntax or
83 memory operand size in Intel syntax. */
84 #define WORD_MNEM_SUFFIX 'w'
85 #define BYTE_MNEM_SUFFIX 'b'
86 #define SHORT_MNEM_SUFFIX 's'
87 #define LONG_MNEM_SUFFIX 'l'
88 #define QWORD_MNEM_SUFFIX 'q'
89 #define XMMWORD_MNEM_SUFFIX 'x'
90 #define YMMWORD_MNEM_SUFFIX 'y'
91 #define ZMMWORD_MNEM_SUFFIX 'z'
92 /* Intel Syntax. Use a non-ascii letter since since it never appears
93 in instructions. */
94 #define LONG_DOUBLE_MNEM_SUFFIX '\1'
95
96 #define END_OF_INSN '\0'
97
98 /*
99 'templates' is for grouping together 'template' structures for opcodes
100 of the same name. This is only used for storing the insns in the grand
101 ole hash table of insns.
102 The templates themselves start at START and range up to (but not including)
103 END.
104 */
105 typedef struct
106 {
107 const insn_template *start;
108 const insn_template *end;
109 }
110 templates;
111
112 /* 386 operand encoding bytes: see 386 book for details of this. */
113 typedef struct
114 {
115 unsigned int regmem; /* codes register or memory operand */
116 unsigned int reg; /* codes register operand (or extended opcode) */
117 unsigned int mode; /* how to interpret regmem & reg */
118 }
119 modrm_byte;
120
121 /* x86-64 extension prefix. */
122 typedef int rex_byte;
123
124 /* 386 opcode byte to code indirect addressing. */
125 typedef struct
126 {
127 unsigned base;
128 unsigned index;
129 unsigned scale;
130 }
131 sib_byte;
132
133 /* x86 arch names, types and features */
134 typedef struct
135 {
136 const char *name; /* arch name */
137 unsigned int len; /* arch string length */
138 enum processor_type type; /* arch type */
139 i386_cpu_flags flags; /* cpu feature flags */
140 unsigned int skip; /* show_arch should skip this. */
141 unsigned int negated; /* turn off indicated flags. */
142 }
143 arch_entry;
144
145 static void update_code_flag (int, int);
146 static void set_code_flag (int);
147 static void set_16bit_gcc_code_flag (int);
148 static void set_intel_syntax (int);
149 static void set_intel_mnemonic (int);
150 static void set_allow_index_reg (int);
151 static void set_check (int);
152 static void set_cpu_arch (int);
153 #ifdef TE_PE
154 static void pe_directive_secrel (int);
155 #endif
156 static void signed_cons (int);
157 static char *output_invalid (int c);
158 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
159 const char *);
160 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
161 const char *);
162 static int i386_att_operand (char *);
163 static int i386_intel_operand (char *, int);
164 static int i386_intel_simplify (expressionS *);
165 static int i386_intel_parse_name (const char *, expressionS *);
166 static const reg_entry *parse_register (char *, char **);
167 static char *parse_insn (char *, char *);
168 static char *parse_operands (char *, const char *);
169 static void swap_operands (void);
170 static void swap_2_operands (int, int);
171 static void optimize_imm (void);
172 static void optimize_disp (void);
173 static const insn_template *match_template (void);
174 static int check_string (void);
175 static int process_suffix (void);
176 static int check_byte_reg (void);
177 static int check_long_reg (void);
178 static int check_qword_reg (void);
179 static int check_word_reg (void);
180 static int finalize_imm (void);
181 static int process_operands (void);
182 static const seg_entry *build_modrm_byte (void);
183 static void output_insn (void);
184 static void output_imm (fragS *, offsetT);
185 static void output_disp (fragS *, offsetT);
186 #ifndef I386COFF
187 static void s_bss (int);
188 #endif
189 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
190 static void handle_large_common (int small ATTRIBUTE_UNUSED);
191 #endif
192
193 static const char *default_arch = DEFAULT_ARCH;
194
195 /* This struct describes rounding control and SAE in the instruction. */
196 struct RC_Operation
197 {
198 enum rc_type
199 {
200 rne = 0,
201 rd,
202 ru,
203 rz,
204 saeonly
205 } type;
206 int operand;
207 };
208
209 static struct RC_Operation rc_op;
210
211 /* The struct describes masking, applied to OPERAND in the instruction.
212 MASK is a pointer to the corresponding mask register. ZEROING tells
213 whether merging or zeroing mask is used. */
214 struct Mask_Operation
215 {
216 const reg_entry *mask;
217 unsigned int zeroing;
218 /* The operand where this operation is associated. */
219 int operand;
220 };
221
222 static struct Mask_Operation mask_op;
223
224 /* The struct describes broadcasting, applied to OPERAND. FACTOR is
225 broadcast factor. */
226 struct Broadcast_Operation
227 {
228 /* Type of broadcast: no broadcast, {1to8}, or {1to16}. */
229 int type;
230
231 /* Index of broadcasted operand. */
232 int operand;
233 };
234
235 static struct Broadcast_Operation broadcast_op;
236
237 /* VEX prefix. */
238 typedef struct
239 {
240 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
241 unsigned char bytes[4];
242 unsigned int length;
243 /* Destination or source register specifier. */
244 const reg_entry *register_specifier;
245 } vex_prefix;
246
247 /* 'md_assemble ()' gathers together information and puts it into a
248 i386_insn. */
249
250 union i386_op
251 {
252 expressionS *disps;
253 expressionS *imms;
254 const reg_entry *regs;
255 };
256
257 enum i386_error
258 {
259 operand_size_mismatch,
260 operand_type_mismatch,
261 register_type_mismatch,
262 number_of_operands_mismatch,
263 invalid_instruction_suffix,
264 bad_imm4,
265 old_gcc_only,
266 unsupported_with_intel_mnemonic,
267 unsupported_syntax,
268 unsupported,
269 invalid_vsib_address,
270 invalid_vector_register_set,
271 unsupported_vector_index_register,
272 unsupported_broadcast,
273 broadcast_not_on_src_operand,
274 broadcast_needed,
275 unsupported_masking,
276 mask_not_on_destination,
277 no_default_mask,
278 unsupported_rc_sae,
279 rc_sae_operand_not_last_imm,
280 invalid_register_operand,
281 try_vector_disp8
282 };
283
284 struct _i386_insn
285 {
286 /* TM holds the template for the insn were currently assembling. */
287 insn_template tm;
288
289 /* SUFFIX holds the instruction size suffix for byte, word, dword
290 or qword, if given. */
291 char suffix;
292
293 /* OPERANDS gives the number of given operands. */
294 unsigned int operands;
295
296 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
297 of given register, displacement, memory operands and immediate
298 operands. */
299 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
300
301 /* TYPES [i] is the type (see above #defines) which tells us how to
302 use OP[i] for the corresponding operand. */
303 i386_operand_type types[MAX_OPERANDS];
304
305 /* Displacement expression, immediate expression, or register for each
306 operand. */
307 union i386_op op[MAX_OPERANDS];
308
309 /* Flags for operands. */
310 unsigned int flags[MAX_OPERANDS];
311 #define Operand_PCrel 1
312
313 /* Relocation type for operand */
314 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
315
316 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
317 the base index byte below. */
318 const reg_entry *base_reg;
319 const reg_entry *index_reg;
320 unsigned int log2_scale_factor;
321
322 /* SEG gives the seg_entries of this insn. They are zero unless
323 explicit segment overrides are given. */
324 const seg_entry *seg[2];
325
326 /* PREFIX holds all the given prefix opcodes (usually null).
327 PREFIXES is the number of prefix opcodes. */
328 unsigned int prefixes;
329 unsigned char prefix[MAX_PREFIXES];
330
331 /* RM and SIB are the modrm byte and the sib byte where the
332 addressing modes of this insn are encoded. */
333 modrm_byte rm;
334 rex_byte rex;
335 rex_byte vrex;
336 sib_byte sib;
337 vex_prefix vex;
338
339 /* Masking attributes. */
340 struct Mask_Operation *mask;
341
342 /* Rounding control and SAE attributes. */
343 struct RC_Operation *rounding;
344
345 /* Broadcasting attributes. */
346 struct Broadcast_Operation *broadcast;
347
348 /* Compressed disp8*N attribute. */
349 unsigned int memshift;
350
351 /* Swap operand in encoding. */
352 unsigned int swap_operand;
353
354 /* Prefer 8bit or 32bit displacement in encoding. */
355 enum
356 {
357 disp_encoding_default = 0,
358 disp_encoding_8bit,
359 disp_encoding_32bit
360 } disp_encoding;
361
362 /* REP prefix. */
363 const char *rep_prefix;
364
365 /* HLE prefix. */
366 const char *hle_prefix;
367
368 /* Have BND prefix. */
369 const char *bnd_prefix;
370
371 /* Need VREX to support upper 16 registers. */
372 int need_vrex;
373
374 /* Error message. */
375 enum i386_error error;
376 };
377
378 typedef struct _i386_insn i386_insn;
379
380 /* Link RC type with corresponding string, that'll be looked for in
381 asm. */
382 struct RC_name
383 {
384 enum rc_type type;
385 const char *name;
386 unsigned int len;
387 };
388
389 static const struct RC_name RC_NamesTable[] =
390 {
391 { rne, STRING_COMMA_LEN ("rn-sae") },
392 { rd, STRING_COMMA_LEN ("rd-sae") },
393 { ru, STRING_COMMA_LEN ("ru-sae") },
394 { rz, STRING_COMMA_LEN ("rz-sae") },
395 { saeonly, STRING_COMMA_LEN ("sae") },
396 };
397
398 /* List of chars besides those in app.c:symbol_chars that can start an
399 operand. Used to prevent the scrubber eating vital white-space. */
400 const char extra_symbol_chars[] = "*%-([{"
401 #ifdef LEX_AT
402 "@"
403 #endif
404 #ifdef LEX_QM
405 "?"
406 #endif
407 ;
408
409 #if (defined (TE_I386AIX) \
410 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
411 && !defined (TE_GNU) \
412 && !defined (TE_LINUX) \
413 && !defined (TE_NACL) \
414 && !defined (TE_NETWARE) \
415 && !defined (TE_FreeBSD) \
416 && !defined (TE_DragonFly) \
417 && !defined (TE_NetBSD)))
418 /* This array holds the chars that always start a comment. If the
419 pre-processor is disabled, these aren't very useful. The option
420 --divide will remove '/' from this list. */
421 const char *i386_comment_chars = "#/";
422 #define SVR4_COMMENT_CHARS 1
423 #define PREFIX_SEPARATOR '\\'
424
425 #else
426 const char *i386_comment_chars = "#";
427 #define PREFIX_SEPARATOR '/'
428 #endif
429
430 /* This array holds the chars that only start a comment at the beginning of
431 a line. If the line seems to have the form '# 123 filename'
432 .line and .file directives will appear in the pre-processed output.
433 Note that input_file.c hand checks for '#' at the beginning of the
434 first line of the input file. This is because the compiler outputs
435 #NO_APP at the beginning of its output.
436 Also note that comments started like this one will always work if
437 '/' isn't otherwise defined. */
438 const char line_comment_chars[] = "#/";
439
440 const char line_separator_chars[] = ";";
441
442 /* Chars that can be used to separate mant from exp in floating point
443 nums. */
444 const char EXP_CHARS[] = "eE";
445
446 /* Chars that mean this number is a floating point constant
447 As in 0f12.456
448 or 0d1.2345e12. */
449 const char FLT_CHARS[] = "fFdDxX";
450
451 /* Tables for lexical analysis. */
452 static char mnemonic_chars[256];
453 static char register_chars[256];
454 static char operand_chars[256];
455 static char identifier_chars[256];
456 static char digit_chars[256];
457
458 /* Lexical macros. */
459 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
460 #define is_operand_char(x) (operand_chars[(unsigned char) x])
461 #define is_register_char(x) (register_chars[(unsigned char) x])
462 #define is_space_char(x) ((x) == ' ')
463 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
464 #define is_digit_char(x) (digit_chars[(unsigned char) x])
465
466 /* All non-digit non-letter characters that may occur in an operand. */
467 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
468
469 /* md_assemble() always leaves the strings it's passed unaltered. To
470 effect this we maintain a stack of saved characters that we've smashed
471 with '\0's (indicating end of strings for various sub-fields of the
472 assembler instruction). */
473 static char save_stack[32];
474 static char *save_stack_p;
475 #define END_STRING_AND_SAVE(s) \
476 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
477 #define RESTORE_END_STRING(s) \
478 do { *(s) = *--save_stack_p; } while (0)
479
480 /* The instruction we're assembling. */
481 static i386_insn i;
482
483 /* Possible templates for current insn. */
484 static const templates *current_templates;
485
486 /* Per instruction expressionS buffers: max displacements & immediates. */
487 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
488 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
489
490 /* Current operand we are working on. */
491 static int this_operand = -1;
492
493 /* We support four different modes. FLAG_CODE variable is used to distinguish
494 these. */
495
496 enum flag_code {
497 CODE_32BIT,
498 CODE_16BIT,
499 CODE_64BIT };
500
501 static enum flag_code flag_code;
502 static unsigned int object_64bit;
503 static unsigned int disallow_64bit_reloc;
504 static int use_rela_relocations = 0;
505
506 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
507 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
508 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
509
510 /* The ELF ABI to use. */
511 enum x86_elf_abi
512 {
513 I386_ABI,
514 X86_64_ABI,
515 X86_64_X32_ABI
516 };
517
518 static enum x86_elf_abi x86_elf_abi = I386_ABI;
519 #endif
520
521 #if defined (TE_PE) || defined (TE_PEP)
522 /* Use big object file format. */
523 static int use_big_obj = 0;
524 #endif
525
526 /* 1 for intel syntax,
527 0 if att syntax. */
528 static int intel_syntax = 0;
529
530 /* 1 for intel mnemonic,
531 0 if att mnemonic. */
532 static int intel_mnemonic = !SYSV386_COMPAT;
533
534 /* 1 if support old (<= 2.8.1) versions of gcc. */
535 static int old_gcc = OLDGCC_COMPAT;
536
537 /* 1 if pseudo registers are permitted. */
538 static int allow_pseudo_reg = 0;
539
540 /* 1 if register prefix % not required. */
541 static int allow_naked_reg = 0;
542
543 /* 1 if the assembler should add BND prefix for all control-tranferring
544 instructions supporting it, even if this prefix wasn't specified
545 explicitly. */
546 static int add_bnd_prefix = 0;
547
548 /* 1 if pseudo index register, eiz/riz, is allowed . */
549 static int allow_index_reg = 0;
550
551 /* 1 if the assembler should ignore LOCK prefix, even if it was
552 specified explicitly. */
553 static int omit_lock_prefix = 0;
554
555 static enum check_kind
556 {
557 check_none = 0,
558 check_warning,
559 check_error
560 }
561 sse_check, operand_check = check_warning;
562
563 /* Register prefix used for error message. */
564 static const char *register_prefix = "%";
565
566 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
567 leave, push, and pop instructions so that gcc has the same stack
568 frame as in 32 bit mode. */
569 static char stackop_size = '\0';
570
571 /* Non-zero to optimize code alignment. */
572 int optimize_align_code = 1;
573
574 /* Non-zero to quieten some warnings. */
575 static int quiet_warnings = 0;
576
577 /* CPU name. */
578 static const char *cpu_arch_name = NULL;
579 static char *cpu_sub_arch_name = NULL;
580
581 /* CPU feature flags. */
582 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
583
584 /* If we have selected a cpu we are generating instructions for. */
585 static int cpu_arch_tune_set = 0;
586
587 /* Cpu we are generating instructions for. */
588 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
589
590 /* CPU feature flags of cpu we are generating instructions for. */
591 static i386_cpu_flags cpu_arch_tune_flags;
592
593 /* CPU instruction set architecture used. */
594 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
595
596 /* CPU feature flags of instruction set architecture used. */
597 i386_cpu_flags cpu_arch_isa_flags;
598
599 /* If set, conditional jumps are not automatically promoted to handle
600 larger than a byte offset. */
601 static unsigned int no_cond_jump_promotion = 0;
602
603 /* Encode SSE instructions with VEX prefix. */
604 static unsigned int sse2avx;
605
606 /* Encode scalar AVX instructions with specific vector length. */
607 static enum
608 {
609 vex128 = 0,
610 vex256
611 } avxscalar;
612
613 /* Encode scalar EVEX LIG instructions with specific vector length. */
614 static enum
615 {
616 evexl128 = 0,
617 evexl256,
618 evexl512
619 } evexlig;
620
621 /* Encode EVEX WIG instructions with specific evex.w. */
622 static enum
623 {
624 evexw0 = 0,
625 evexw1
626 } evexwig;
627
628 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
629 static enum rc_type evexrcig = rne;
630
631 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
632 static symbolS *GOT_symbol;
633
634 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
635 unsigned int x86_dwarf2_return_column;
636
637 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
638 int x86_cie_data_alignment;
639
640 /* Interface to relax_segment.
641 There are 3 major relax states for 386 jump insns because the
642 different types of jumps add different sizes to frags when we're
643 figuring out what sort of jump to choose to reach a given label. */
644
645 /* Types. */
646 #define UNCOND_JUMP 0
647 #define COND_JUMP 1
648 #define COND_JUMP86 2
649
650 /* Sizes. */
651 #define CODE16 1
652 #define SMALL 0
653 #define SMALL16 (SMALL | CODE16)
654 #define BIG 2
655 #define BIG16 (BIG | CODE16)
656
657 #ifndef INLINE
658 #ifdef __GNUC__
659 #define INLINE __inline__
660 #else
661 #define INLINE
662 #endif
663 #endif
664
665 #define ENCODE_RELAX_STATE(type, size) \
666 ((relax_substateT) (((type) << 2) | (size)))
667 #define TYPE_FROM_RELAX_STATE(s) \
668 ((s) >> 2)
669 #define DISP_SIZE_FROM_RELAX_STATE(s) \
670 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
671
672 /* This table is used by relax_frag to promote short jumps to long
673 ones where necessary. SMALL (short) jumps may be promoted to BIG
674 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
675 don't allow a short jump in a 32 bit code segment to be promoted to
676 a 16 bit offset jump because it's slower (requires data size
677 prefix), and doesn't work, unless the destination is in the bottom
678 64k of the code segment (The top 16 bits of eip are zeroed). */
679
680 const relax_typeS md_relax_table[] =
681 {
682 /* The fields are:
683 1) most positive reach of this state,
684 2) most negative reach of this state,
685 3) how many bytes this mode will have in the variable part of the frag
686 4) which index into the table to try if we can't fit into this one. */
687
688 /* UNCOND_JUMP states. */
689 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
690 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
691 /* dword jmp adds 4 bytes to frag:
692 0 extra opcode bytes, 4 displacement bytes. */
693 {0, 0, 4, 0},
694 /* word jmp adds 2 byte2 to frag:
695 0 extra opcode bytes, 2 displacement bytes. */
696 {0, 0, 2, 0},
697
698 /* COND_JUMP states. */
699 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
700 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
701 /* dword conditionals adds 5 bytes to frag:
702 1 extra opcode byte, 4 displacement bytes. */
703 {0, 0, 5, 0},
704 /* word conditionals add 3 bytes to frag:
705 1 extra opcode byte, 2 displacement bytes. */
706 {0, 0, 3, 0},
707
708 /* COND_JUMP86 states. */
709 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
710 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
711 /* dword conditionals adds 5 bytes to frag:
712 1 extra opcode byte, 4 displacement bytes. */
713 {0, 0, 5, 0},
714 /* word conditionals add 4 bytes to frag:
715 1 displacement byte and a 3 byte long branch insn. */
716 {0, 0, 4, 0}
717 };
718
719 static const arch_entry cpu_arch[] =
720 {
721 /* Do not replace the first two entries - i386_target_format()
722 relies on them being there in this order. */
723 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
724 CPU_GENERIC32_FLAGS, 0, 0 },
725 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
726 CPU_GENERIC64_FLAGS, 0, 0 },
727 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
728 CPU_NONE_FLAGS, 0, 0 },
729 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
730 CPU_I186_FLAGS, 0, 0 },
731 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
732 CPU_I286_FLAGS, 0, 0 },
733 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
734 CPU_I386_FLAGS, 0, 0 },
735 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
736 CPU_I486_FLAGS, 0, 0 },
737 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
738 CPU_I586_FLAGS, 0, 0 },
739 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
740 CPU_I686_FLAGS, 0, 0 },
741 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
742 CPU_I586_FLAGS, 0, 0 },
743 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
744 CPU_PENTIUMPRO_FLAGS, 0, 0 },
745 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
746 CPU_P2_FLAGS, 0, 0 },
747 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
748 CPU_P3_FLAGS, 0, 0 },
749 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
750 CPU_P4_FLAGS, 0, 0 },
751 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
752 CPU_CORE_FLAGS, 0, 0 },
753 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
754 CPU_NOCONA_FLAGS, 0, 0 },
755 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
756 CPU_CORE_FLAGS, 1, 0 },
757 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
758 CPU_CORE_FLAGS, 0, 0 },
759 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
760 CPU_CORE2_FLAGS, 1, 0 },
761 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
762 CPU_CORE2_FLAGS, 0, 0 },
763 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
764 CPU_COREI7_FLAGS, 0, 0 },
765 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
766 CPU_L1OM_FLAGS, 0, 0 },
767 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
768 CPU_K1OM_FLAGS, 0, 0 },
769 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
770 CPU_K6_FLAGS, 0, 0 },
771 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
772 CPU_K6_2_FLAGS, 0, 0 },
773 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
774 CPU_ATHLON_FLAGS, 0, 0 },
775 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
776 CPU_K8_FLAGS, 1, 0 },
777 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
778 CPU_K8_FLAGS, 0, 0 },
779 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
780 CPU_K8_FLAGS, 0, 0 },
781 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
782 CPU_AMDFAM10_FLAGS, 0, 0 },
783 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
784 CPU_BDVER1_FLAGS, 0, 0 },
785 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
786 CPU_BDVER2_FLAGS, 0, 0 },
787 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
788 CPU_BDVER3_FLAGS, 0, 0 },
789 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
790 CPU_BDVER4_FLAGS, 0, 0 },
791 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
792 CPU_ZNVER1_FLAGS, 0, 0 },
793 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
794 CPU_BTVER1_FLAGS, 0, 0 },
795 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
796 CPU_BTVER2_FLAGS, 0, 0 },
797 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
798 CPU_8087_FLAGS, 0, 0 },
799 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
800 CPU_287_FLAGS, 0, 0 },
801 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
802 CPU_387_FLAGS, 0, 0 },
803 { STRING_COMMA_LEN (".no87"), PROCESSOR_UNKNOWN,
804 CPU_ANY87_FLAGS, 0, 1 },
805 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
806 CPU_MMX_FLAGS, 0, 0 },
807 { STRING_COMMA_LEN (".nommx"), PROCESSOR_UNKNOWN,
808 CPU_3DNOWA_FLAGS, 0, 1 },
809 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
810 CPU_SSE_FLAGS, 0, 0 },
811 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
812 CPU_SSE2_FLAGS, 0, 0 },
813 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
814 CPU_SSE3_FLAGS, 0, 0 },
815 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
816 CPU_SSSE3_FLAGS, 0, 0 },
817 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
818 CPU_SSE4_1_FLAGS, 0, 0 },
819 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
820 CPU_SSE4_2_FLAGS, 0, 0 },
821 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
822 CPU_SSE4_2_FLAGS, 0, 0 },
823 { STRING_COMMA_LEN (".nosse"), PROCESSOR_UNKNOWN,
824 CPU_ANY_SSE_FLAGS, 0, 1 },
825 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
826 CPU_AVX_FLAGS, 0, 0 },
827 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
828 CPU_AVX2_FLAGS, 0, 0 },
829 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
830 CPU_AVX512F_FLAGS, 0, 0 },
831 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
832 CPU_AVX512CD_FLAGS, 0, 0 },
833 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
834 CPU_AVX512ER_FLAGS, 0, 0 },
835 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
836 CPU_AVX512PF_FLAGS, 0, 0 },
837 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
838 CPU_AVX512DQ_FLAGS, 0, 0 },
839 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
840 CPU_AVX512BW_FLAGS, 0, 0 },
841 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
842 CPU_AVX512VL_FLAGS, 0, 0 },
843 { STRING_COMMA_LEN (".noavx"), PROCESSOR_UNKNOWN,
844 CPU_ANY_AVX_FLAGS, 0, 1 },
845 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
846 CPU_VMX_FLAGS, 0, 0 },
847 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
848 CPU_VMFUNC_FLAGS, 0, 0 },
849 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
850 CPU_SMX_FLAGS, 0, 0 },
851 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
852 CPU_XSAVE_FLAGS, 0, 0 },
853 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
854 CPU_XSAVEOPT_FLAGS, 0, 0 },
855 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
856 CPU_XSAVEC_FLAGS, 0, 0 },
857 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
858 CPU_XSAVES_FLAGS, 0, 0 },
859 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
860 CPU_AES_FLAGS, 0, 0 },
861 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
862 CPU_PCLMUL_FLAGS, 0, 0 },
863 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
864 CPU_PCLMUL_FLAGS, 1, 0 },
865 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
866 CPU_FSGSBASE_FLAGS, 0, 0 },
867 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
868 CPU_RDRND_FLAGS, 0, 0 },
869 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
870 CPU_F16C_FLAGS, 0, 0 },
871 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
872 CPU_BMI2_FLAGS, 0, 0 },
873 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
874 CPU_FMA_FLAGS, 0, 0 },
875 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
876 CPU_FMA4_FLAGS, 0, 0 },
877 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
878 CPU_XOP_FLAGS, 0, 0 },
879 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
880 CPU_LWP_FLAGS, 0, 0 },
881 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
882 CPU_MOVBE_FLAGS, 0, 0 },
883 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
884 CPU_CX16_FLAGS, 0, 0 },
885 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
886 CPU_EPT_FLAGS, 0, 0 },
887 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
888 CPU_LZCNT_FLAGS, 0, 0 },
889 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
890 CPU_HLE_FLAGS, 0, 0 },
891 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
892 CPU_RTM_FLAGS, 0, 0 },
893 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
894 CPU_INVPCID_FLAGS, 0, 0 },
895 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
896 CPU_CLFLUSH_FLAGS, 0, 0 },
897 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
898 CPU_NOP_FLAGS, 0, 0 },
899 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
900 CPU_SYSCALL_FLAGS, 0, 0 },
901 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
902 CPU_RDTSCP_FLAGS, 0, 0 },
903 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
904 CPU_3DNOW_FLAGS, 0, 0 },
905 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
906 CPU_3DNOWA_FLAGS, 0, 0 },
907 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
908 CPU_PADLOCK_FLAGS, 0, 0 },
909 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
910 CPU_SVME_FLAGS, 1, 0 },
911 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
912 CPU_SVME_FLAGS, 0, 0 },
913 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
914 CPU_SSE4A_FLAGS, 0, 0 },
915 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
916 CPU_ABM_FLAGS, 0, 0 },
917 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
918 CPU_BMI_FLAGS, 0, 0 },
919 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
920 CPU_TBM_FLAGS, 0, 0 },
921 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
922 CPU_ADX_FLAGS, 0, 0 },
923 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
924 CPU_RDSEED_FLAGS, 0, 0 },
925 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
926 CPU_PRFCHW_FLAGS, 0, 0 },
927 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
928 CPU_SMAP_FLAGS, 0, 0 },
929 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
930 CPU_MPX_FLAGS, 0, 0 },
931 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
932 CPU_SHA_FLAGS, 0, 0 },
933 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
934 CPU_CLFLUSHOPT_FLAGS, 0, 0 },
935 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
936 CPU_PREFETCHWT1_FLAGS, 0, 0 },
937 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
938 CPU_SE1_FLAGS, 0, 0 },
939 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
940 CPU_CLWB_FLAGS, 0, 0 },
941 { STRING_COMMA_LEN (".pcommit"), PROCESSOR_UNKNOWN,
942 CPU_PCOMMIT_FLAGS, 0, 0 },
943 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
944 CPU_AVX512IFMA_FLAGS, 0, 0 },
945 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
946 CPU_AVX512VBMI_FLAGS, 0, 0 },
947 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
948 CPU_CLZERO_FLAGS, 0, 0 },
949 };
950
951 #ifdef I386COFF
952 /* Like s_lcomm_internal in gas/read.c but the alignment string
953 is allowed to be optional. */
954
955 static symbolS *
956 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
957 {
958 addressT align = 0;
959
960 SKIP_WHITESPACE ();
961
962 if (needs_align
963 && *input_line_pointer == ',')
964 {
965 align = parse_align (needs_align - 1);
966
967 if (align == (addressT) -1)
968 return NULL;
969 }
970 else
971 {
972 if (size >= 8)
973 align = 3;
974 else if (size >= 4)
975 align = 2;
976 else if (size >= 2)
977 align = 1;
978 else
979 align = 0;
980 }
981
982 bss_alloc (symbolP, size, align);
983 return symbolP;
984 }
985
986 static void
987 pe_lcomm (int needs_align)
988 {
989 s_comm_internal (needs_align * 2, pe_lcomm_internal);
990 }
991 #endif
992
993 const pseudo_typeS md_pseudo_table[] =
994 {
995 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
996 {"align", s_align_bytes, 0},
997 #else
998 {"align", s_align_ptwo, 0},
999 #endif
1000 {"arch", set_cpu_arch, 0},
1001 #ifndef I386COFF
1002 {"bss", s_bss, 0},
1003 #else
1004 {"lcomm", pe_lcomm, 1},
1005 #endif
1006 {"ffloat", float_cons, 'f'},
1007 {"dfloat", float_cons, 'd'},
1008 {"tfloat", float_cons, 'x'},
1009 {"value", cons, 2},
1010 {"slong", signed_cons, 4},
1011 {"noopt", s_ignore, 0},
1012 {"optim", s_ignore, 0},
1013 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1014 {"code16", set_code_flag, CODE_16BIT},
1015 {"code32", set_code_flag, CODE_32BIT},
1016 {"code64", set_code_flag, CODE_64BIT},
1017 {"intel_syntax", set_intel_syntax, 1},
1018 {"att_syntax", set_intel_syntax, 0},
1019 {"intel_mnemonic", set_intel_mnemonic, 1},
1020 {"att_mnemonic", set_intel_mnemonic, 0},
1021 {"allow_index_reg", set_allow_index_reg, 1},
1022 {"disallow_index_reg", set_allow_index_reg, 0},
1023 {"sse_check", set_check, 0},
1024 {"operand_check", set_check, 1},
1025 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1026 {"largecomm", handle_large_common, 0},
1027 #else
1028 {"file", (void (*) (int)) dwarf2_directive_file, 0},
1029 {"loc", dwarf2_directive_loc, 0},
1030 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1031 #endif
1032 #ifdef TE_PE
1033 {"secrel32", pe_directive_secrel, 0},
1034 #endif
1035 {0, 0, 0}
1036 };
1037
1038 /* For interface with expression (). */
1039 extern char *input_line_pointer;
1040
1041 /* Hash table for instruction mnemonic lookup. */
1042 static struct hash_control *op_hash;
1043
1044 /* Hash table for register lookup. */
1045 static struct hash_control *reg_hash;
1046 \f
1047 void
1048 i386_align_code (fragS *fragP, int count)
1049 {
1050 /* Various efficient no-op patterns for aligning code labels.
1051 Note: Don't try to assemble the instructions in the comments.
1052 0L and 0w are not legal. */
1053 static const char f32_1[] =
1054 {0x90}; /* nop */
1055 static const char f32_2[] =
1056 {0x66,0x90}; /* xchg %ax,%ax */
1057 static const char f32_3[] =
1058 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1059 static const char f32_4[] =
1060 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1061 static const char f32_5[] =
1062 {0x90, /* nop */
1063 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1064 static const char f32_6[] =
1065 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1066 static const char f32_7[] =
1067 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1068 static const char f32_8[] =
1069 {0x90, /* nop */
1070 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1071 static const char f32_9[] =
1072 {0x89,0xf6, /* movl %esi,%esi */
1073 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
1074 static const char f32_10[] =
1075 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
1076 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
1077 static const char f32_11[] =
1078 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
1079 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
1080 static const char f32_12[] =
1081 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
1082 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
1083 static const char f32_13[] =
1084 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
1085 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
1086 static const char f32_14[] =
1087 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
1088 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
1089 static const char f16_3[] =
1090 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
1091 static const char f16_4[] =
1092 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
1093 static const char f16_5[] =
1094 {0x90, /* nop */
1095 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
1096 static const char f16_6[] =
1097 {0x89,0xf6, /* mov %si,%si */
1098 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
1099 static const char f16_7[] =
1100 {0x8d,0x74,0x00, /* lea 0(%si),%si */
1101 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
1102 static const char f16_8[] =
1103 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
1104 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
1105 static const char jump_31[] =
1106 {0xeb,0x1d,0x90,0x90,0x90,0x90,0x90, /* jmp .+31; lotsa nops */
1107 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
1108 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
1109 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
1110 static const char *const f32_patt[] = {
1111 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
1112 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14
1113 };
1114 static const char *const f16_patt[] = {
1115 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8
1116 };
1117 /* nopl (%[re]ax) */
1118 static const char alt_3[] =
1119 {0x0f,0x1f,0x00};
1120 /* nopl 0(%[re]ax) */
1121 static const char alt_4[] =
1122 {0x0f,0x1f,0x40,0x00};
1123 /* nopl 0(%[re]ax,%[re]ax,1) */
1124 static const char alt_5[] =
1125 {0x0f,0x1f,0x44,0x00,0x00};
1126 /* nopw 0(%[re]ax,%[re]ax,1) */
1127 static const char alt_6[] =
1128 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1129 /* nopl 0L(%[re]ax) */
1130 static const char alt_7[] =
1131 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1132 /* nopl 0L(%[re]ax,%[re]ax,1) */
1133 static const char alt_8[] =
1134 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1135 /* nopw 0L(%[re]ax,%[re]ax,1) */
1136 static const char alt_9[] =
1137 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1138 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1139 static const char alt_10[] =
1140 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1141 static const char *const alt_patt[] = {
1142 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1143 alt_9, alt_10
1144 };
1145
1146 /* Only align for at least a positive non-zero boundary. */
1147 if (count <= 0 || count > MAX_MEM_FOR_RS_ALIGN_CODE)
1148 return;
1149
1150 /* We need to decide which NOP sequence to use for 32bit and
1151 64bit. When -mtune= is used:
1152
1153 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1154 PROCESSOR_GENERIC32, f32_patt will be used.
1155 2. For the rest, alt_patt will be used.
1156
1157 When -mtune= isn't used, alt_patt will be used if
1158 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
1159 be used.
1160
1161 When -march= or .arch is used, we can't use anything beyond
1162 cpu_arch_isa_flags. */
1163
1164 if (flag_code == CODE_16BIT)
1165 {
1166 if (count > 8)
1167 {
1168 memcpy (fragP->fr_literal + fragP->fr_fix,
1169 jump_31, count);
1170 /* Adjust jump offset. */
1171 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
1172 }
1173 else
1174 memcpy (fragP->fr_literal + fragP->fr_fix,
1175 f16_patt[count - 1], count);
1176 }
1177 else
1178 {
1179 const char *const *patt = NULL;
1180
1181 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1182 {
1183 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1184 switch (cpu_arch_tune)
1185 {
1186 case PROCESSOR_UNKNOWN:
1187 /* We use cpu_arch_isa_flags to check if we SHOULD
1188 optimize with nops. */
1189 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1190 patt = alt_patt;
1191 else
1192 patt = f32_patt;
1193 break;
1194 case PROCESSOR_PENTIUM4:
1195 case PROCESSOR_NOCONA:
1196 case PROCESSOR_CORE:
1197 case PROCESSOR_CORE2:
1198 case PROCESSOR_COREI7:
1199 case PROCESSOR_L1OM:
1200 case PROCESSOR_K1OM:
1201 case PROCESSOR_GENERIC64:
1202 case PROCESSOR_K6:
1203 case PROCESSOR_ATHLON:
1204 case PROCESSOR_K8:
1205 case PROCESSOR_AMDFAM10:
1206 case PROCESSOR_BD:
1207 case PROCESSOR_ZNVER:
1208 case PROCESSOR_BT:
1209 patt = alt_patt;
1210 break;
1211 case PROCESSOR_I386:
1212 case PROCESSOR_I486:
1213 case PROCESSOR_PENTIUM:
1214 case PROCESSOR_PENTIUMPRO:
1215 case PROCESSOR_GENERIC32:
1216 patt = f32_patt;
1217 break;
1218 }
1219 }
1220 else
1221 {
1222 switch (fragP->tc_frag_data.tune)
1223 {
1224 case PROCESSOR_UNKNOWN:
1225 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1226 PROCESSOR_UNKNOWN. */
1227 abort ();
1228 break;
1229
1230 case PROCESSOR_I386:
1231 case PROCESSOR_I486:
1232 case PROCESSOR_PENTIUM:
1233 case PROCESSOR_K6:
1234 case PROCESSOR_ATHLON:
1235 case PROCESSOR_K8:
1236 case PROCESSOR_AMDFAM10:
1237 case PROCESSOR_BD:
1238 case PROCESSOR_ZNVER:
1239 case PROCESSOR_BT:
1240 case PROCESSOR_GENERIC32:
1241 /* We use cpu_arch_isa_flags to check if we CAN optimize
1242 with nops. */
1243 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1244 patt = alt_patt;
1245 else
1246 patt = f32_patt;
1247 break;
1248 case PROCESSOR_PENTIUMPRO:
1249 case PROCESSOR_PENTIUM4:
1250 case PROCESSOR_NOCONA:
1251 case PROCESSOR_CORE:
1252 case PROCESSOR_CORE2:
1253 case PROCESSOR_COREI7:
1254 case PROCESSOR_L1OM:
1255 case PROCESSOR_K1OM:
1256 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1257 patt = alt_patt;
1258 else
1259 patt = f32_patt;
1260 break;
1261 case PROCESSOR_GENERIC64:
1262 patt = alt_patt;
1263 break;
1264 }
1265 }
1266
1267 if (patt == f32_patt)
1268 {
1269 /* If the padding is less than 15 bytes, we use the normal
1270 ones. Otherwise, we use a jump instruction and adjust
1271 its offset. */
1272 int limit;
1273
1274 /* For 64bit, the limit is 3 bytes. */
1275 if (flag_code == CODE_64BIT
1276 && fragP->tc_frag_data.isa_flags.bitfield.cpulm)
1277 limit = 3;
1278 else
1279 limit = 15;
1280 if (count < limit)
1281 memcpy (fragP->fr_literal + fragP->fr_fix,
1282 patt[count - 1], count);
1283 else
1284 {
1285 memcpy (fragP->fr_literal + fragP->fr_fix,
1286 jump_31, count);
1287 /* Adjust jump offset. */
1288 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
1289 }
1290 }
1291 else
1292 {
1293 /* Maximum length of an instruction is 10 byte. If the
1294 padding is greater than 10 bytes and we don't use jump,
1295 we have to break it into smaller pieces. */
1296 int padding = count;
1297 while (padding > 10)
1298 {
1299 padding -= 10;
1300 memcpy (fragP->fr_literal + fragP->fr_fix + padding,
1301 patt [9], 10);
1302 }
1303
1304 if (padding)
1305 memcpy (fragP->fr_literal + fragP->fr_fix,
1306 patt [padding - 1], padding);
1307 }
1308 }
1309 fragP->fr_var = count;
1310 }
1311
1312 static INLINE int
1313 operand_type_all_zero (const union i386_operand_type *x)
1314 {
1315 switch (ARRAY_SIZE(x->array))
1316 {
1317 case 3:
1318 if (x->array[2])
1319 return 0;
1320 case 2:
1321 if (x->array[1])
1322 return 0;
1323 case 1:
1324 return !x->array[0];
1325 default:
1326 abort ();
1327 }
1328 }
1329
1330 static INLINE void
1331 operand_type_set (union i386_operand_type *x, unsigned int v)
1332 {
1333 switch (ARRAY_SIZE(x->array))
1334 {
1335 case 3:
1336 x->array[2] = v;
1337 case 2:
1338 x->array[1] = v;
1339 case 1:
1340 x->array[0] = v;
1341 break;
1342 default:
1343 abort ();
1344 }
1345 }
1346
1347 static INLINE int
1348 operand_type_equal (const union i386_operand_type *x,
1349 const union i386_operand_type *y)
1350 {
1351 switch (ARRAY_SIZE(x->array))
1352 {
1353 case 3:
1354 if (x->array[2] != y->array[2])
1355 return 0;
1356 case 2:
1357 if (x->array[1] != y->array[1])
1358 return 0;
1359 case 1:
1360 return x->array[0] == y->array[0];
1361 break;
1362 default:
1363 abort ();
1364 }
1365 }
1366
1367 static INLINE int
1368 cpu_flags_all_zero (const union i386_cpu_flags *x)
1369 {
1370 switch (ARRAY_SIZE(x->array))
1371 {
1372 case 3:
1373 if (x->array[2])
1374 return 0;
1375 case 2:
1376 if (x->array[1])
1377 return 0;
1378 case 1:
1379 return !x->array[0];
1380 default:
1381 abort ();
1382 }
1383 }
1384
1385 static INLINE void
1386 cpu_flags_set (union i386_cpu_flags *x, unsigned int v)
1387 {
1388 switch (ARRAY_SIZE(x->array))
1389 {
1390 case 3:
1391 x->array[2] = v;
1392 case 2:
1393 x->array[1] = v;
1394 case 1:
1395 x->array[0] = v;
1396 break;
1397 default:
1398 abort ();
1399 }
1400 }
1401
1402 static INLINE int
1403 cpu_flags_equal (const union i386_cpu_flags *x,
1404 const union i386_cpu_flags *y)
1405 {
1406 switch (ARRAY_SIZE(x->array))
1407 {
1408 case 3:
1409 if (x->array[2] != y->array[2])
1410 return 0;
1411 case 2:
1412 if (x->array[1] != y->array[1])
1413 return 0;
1414 case 1:
1415 return x->array[0] == y->array[0];
1416 break;
1417 default:
1418 abort ();
1419 }
1420 }
1421
1422 static INLINE int
1423 cpu_flags_check_cpu64 (i386_cpu_flags f)
1424 {
1425 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1426 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1427 }
1428
1429 static INLINE i386_cpu_flags
1430 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1431 {
1432 switch (ARRAY_SIZE (x.array))
1433 {
1434 case 3:
1435 x.array [2] &= y.array [2];
1436 case 2:
1437 x.array [1] &= y.array [1];
1438 case 1:
1439 x.array [0] &= y.array [0];
1440 break;
1441 default:
1442 abort ();
1443 }
1444 return x;
1445 }
1446
1447 static INLINE i386_cpu_flags
1448 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1449 {
1450 switch (ARRAY_SIZE (x.array))
1451 {
1452 case 3:
1453 x.array [2] |= y.array [2];
1454 case 2:
1455 x.array [1] |= y.array [1];
1456 case 1:
1457 x.array [0] |= y.array [0];
1458 break;
1459 default:
1460 abort ();
1461 }
1462 return x;
1463 }
1464
1465 static INLINE i386_cpu_flags
1466 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1467 {
1468 switch (ARRAY_SIZE (x.array))
1469 {
1470 case 3:
1471 x.array [2] &= ~y.array [2];
1472 case 2:
1473 x.array [1] &= ~y.array [1];
1474 case 1:
1475 x.array [0] &= ~y.array [0];
1476 break;
1477 default:
1478 abort ();
1479 }
1480 return x;
1481 }
1482
1483 #define CPU_FLAGS_ARCH_MATCH 0x1
1484 #define CPU_FLAGS_64BIT_MATCH 0x2
1485 #define CPU_FLAGS_AES_MATCH 0x4
1486 #define CPU_FLAGS_PCLMUL_MATCH 0x8
1487 #define CPU_FLAGS_AVX_MATCH 0x10
1488
1489 #define CPU_FLAGS_32BIT_MATCH \
1490 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_AES_MATCH \
1491 | CPU_FLAGS_PCLMUL_MATCH | CPU_FLAGS_AVX_MATCH)
1492 #define CPU_FLAGS_PERFECT_MATCH \
1493 (CPU_FLAGS_32BIT_MATCH | CPU_FLAGS_64BIT_MATCH)
1494
1495 /* Return CPU flags match bits. */
1496
1497 static int
1498 cpu_flags_match (const insn_template *t)
1499 {
1500 i386_cpu_flags x = t->cpu_flags;
1501 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1502
1503 x.bitfield.cpu64 = 0;
1504 x.bitfield.cpuno64 = 0;
1505
1506 if (cpu_flags_all_zero (&x))
1507 {
1508 /* This instruction is available on all archs. */
1509 match |= CPU_FLAGS_32BIT_MATCH;
1510 }
1511 else
1512 {
1513 /* This instruction is available only on some archs. */
1514 i386_cpu_flags cpu = cpu_arch_flags;
1515
1516 cpu.bitfield.cpu64 = 0;
1517 cpu.bitfield.cpuno64 = 0;
1518 cpu = cpu_flags_and (x, cpu);
1519 if (!cpu_flags_all_zero (&cpu))
1520 {
1521 if (x.bitfield.cpuavx)
1522 {
1523 /* We only need to check AES/PCLMUL/SSE2AVX with AVX. */
1524 if (cpu.bitfield.cpuavx)
1525 {
1526 /* Check SSE2AVX. */
1527 if (!t->opcode_modifier.sse2avx|| sse2avx)
1528 {
1529 match |= (CPU_FLAGS_ARCH_MATCH
1530 | CPU_FLAGS_AVX_MATCH);
1531 /* Check AES. */
1532 if (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1533 match |= CPU_FLAGS_AES_MATCH;
1534 /* Check PCLMUL. */
1535 if (!x.bitfield.cpupclmul
1536 || cpu.bitfield.cpupclmul)
1537 match |= CPU_FLAGS_PCLMUL_MATCH;
1538 }
1539 }
1540 else
1541 match |= CPU_FLAGS_ARCH_MATCH;
1542 }
1543 else
1544 match |= CPU_FLAGS_32BIT_MATCH;
1545 }
1546 }
1547 return match;
1548 }
1549
1550 static INLINE i386_operand_type
1551 operand_type_and (i386_operand_type x, i386_operand_type y)
1552 {
1553 switch (ARRAY_SIZE (x.array))
1554 {
1555 case 3:
1556 x.array [2] &= y.array [2];
1557 case 2:
1558 x.array [1] &= y.array [1];
1559 case 1:
1560 x.array [0] &= y.array [0];
1561 break;
1562 default:
1563 abort ();
1564 }
1565 return x;
1566 }
1567
1568 static INLINE i386_operand_type
1569 operand_type_or (i386_operand_type x, i386_operand_type y)
1570 {
1571 switch (ARRAY_SIZE (x.array))
1572 {
1573 case 3:
1574 x.array [2] |= y.array [2];
1575 case 2:
1576 x.array [1] |= y.array [1];
1577 case 1:
1578 x.array [0] |= y.array [0];
1579 break;
1580 default:
1581 abort ();
1582 }
1583 return x;
1584 }
1585
1586 static INLINE i386_operand_type
1587 operand_type_xor (i386_operand_type x, i386_operand_type y)
1588 {
1589 switch (ARRAY_SIZE (x.array))
1590 {
1591 case 3:
1592 x.array [2] ^= y.array [2];
1593 case 2:
1594 x.array [1] ^= y.array [1];
1595 case 1:
1596 x.array [0] ^= y.array [0];
1597 break;
1598 default:
1599 abort ();
1600 }
1601 return x;
1602 }
1603
1604 static const i386_operand_type acc32 = OPERAND_TYPE_ACC32;
1605 static const i386_operand_type acc64 = OPERAND_TYPE_ACC64;
1606 static const i386_operand_type control = OPERAND_TYPE_CONTROL;
1607 static const i386_operand_type inoutportreg
1608 = OPERAND_TYPE_INOUTPORTREG;
1609 static const i386_operand_type reg16_inoutportreg
1610 = OPERAND_TYPE_REG16_INOUTPORTREG;
1611 static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
1612 static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
1613 static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
1614 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
1615 static const i386_operand_type anydisp
1616 = OPERAND_TYPE_ANYDISP;
1617 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1618 static const i386_operand_type regymm = OPERAND_TYPE_REGYMM;
1619 static const i386_operand_type regzmm = OPERAND_TYPE_REGZMM;
1620 static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
1621 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1622 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1623 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1624 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1625 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1626 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1627 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1628 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1629 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1630 static const i386_operand_type vec_imm4 = OPERAND_TYPE_VEC_IMM4;
1631
1632 enum operand_type
1633 {
1634 reg,
1635 imm,
1636 disp,
1637 anymem
1638 };
1639
1640 static INLINE int
1641 operand_type_check (i386_operand_type t, enum operand_type c)
1642 {
1643 switch (c)
1644 {
1645 case reg:
1646 return (t.bitfield.reg8
1647 || t.bitfield.reg16
1648 || t.bitfield.reg32
1649 || t.bitfield.reg64);
1650
1651 case imm:
1652 return (t.bitfield.imm8
1653 || t.bitfield.imm8s
1654 || t.bitfield.imm16
1655 || t.bitfield.imm32
1656 || t.bitfield.imm32s
1657 || t.bitfield.imm64);
1658
1659 case disp:
1660 return (t.bitfield.disp8
1661 || t.bitfield.disp16
1662 || t.bitfield.disp32
1663 || t.bitfield.disp32s
1664 || t.bitfield.disp64);
1665
1666 case anymem:
1667 return (t.bitfield.disp8
1668 || t.bitfield.disp16
1669 || t.bitfield.disp32
1670 || t.bitfield.disp32s
1671 || t.bitfield.disp64
1672 || t.bitfield.baseindex);
1673
1674 default:
1675 abort ();
1676 }
1677
1678 return 0;
1679 }
1680
1681 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit on
1682 operand J for instruction template T. */
1683
1684 static INLINE int
1685 match_reg_size (const insn_template *t, unsigned int j)
1686 {
1687 return !((i.types[j].bitfield.byte
1688 && !t->operand_types[j].bitfield.byte)
1689 || (i.types[j].bitfield.word
1690 && !t->operand_types[j].bitfield.word)
1691 || (i.types[j].bitfield.dword
1692 && !t->operand_types[j].bitfield.dword)
1693 || (i.types[j].bitfield.qword
1694 && !t->operand_types[j].bitfield.qword));
1695 }
1696
1697 /* Return 1 if there is no conflict in any size on operand J for
1698 instruction template T. */
1699
1700 static INLINE int
1701 match_mem_size (const insn_template *t, unsigned int j)
1702 {
1703 return (match_reg_size (t, j)
1704 && !((i.types[j].bitfield.unspecified
1705 && !t->operand_types[j].bitfield.unspecified)
1706 || (i.types[j].bitfield.fword
1707 && !t->operand_types[j].bitfield.fword)
1708 || (i.types[j].bitfield.tbyte
1709 && !t->operand_types[j].bitfield.tbyte)
1710 || (i.types[j].bitfield.xmmword
1711 && !t->operand_types[j].bitfield.xmmword)
1712 || (i.types[j].bitfield.ymmword
1713 && !t->operand_types[j].bitfield.ymmword)
1714 || (i.types[j].bitfield.zmmword
1715 && !t->operand_types[j].bitfield.zmmword)));
1716 }
1717
1718 /* Return 1 if there is no size conflict on any operands for
1719 instruction template T. */
1720
1721 static INLINE int
1722 operand_size_match (const insn_template *t)
1723 {
1724 unsigned int j;
1725 int match = 1;
1726
1727 /* Don't check jump instructions. */
1728 if (t->opcode_modifier.jump
1729 || t->opcode_modifier.jumpbyte
1730 || t->opcode_modifier.jumpdword
1731 || t->opcode_modifier.jumpintersegment)
1732 return match;
1733
1734 /* Check memory and accumulator operand size. */
1735 for (j = 0; j < i.operands; j++)
1736 {
1737 if (t->operand_types[j].bitfield.anysize)
1738 continue;
1739
1740 if (t->operand_types[j].bitfield.acc && !match_reg_size (t, j))
1741 {
1742 match = 0;
1743 break;
1744 }
1745
1746 if (i.types[j].bitfield.mem && !match_mem_size (t, j))
1747 {
1748 match = 0;
1749 break;
1750 }
1751 }
1752
1753 if (match)
1754 return match;
1755 else if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
1756 {
1757 mismatch:
1758 i.error = operand_size_mismatch;
1759 return 0;
1760 }
1761
1762 /* Check reverse. */
1763 gas_assert (i.operands == 2);
1764
1765 match = 1;
1766 for (j = 0; j < 2; j++)
1767 {
1768 if (t->operand_types[j].bitfield.acc
1769 && !match_reg_size (t, j ? 0 : 1))
1770 goto mismatch;
1771
1772 if (i.types[j].bitfield.mem
1773 && !match_mem_size (t, j ? 0 : 1))
1774 goto mismatch;
1775 }
1776
1777 return match;
1778 }
1779
1780 static INLINE int
1781 operand_type_match (i386_operand_type overlap,
1782 i386_operand_type given)
1783 {
1784 i386_operand_type temp = overlap;
1785
1786 temp.bitfield.jumpabsolute = 0;
1787 temp.bitfield.unspecified = 0;
1788 temp.bitfield.byte = 0;
1789 temp.bitfield.word = 0;
1790 temp.bitfield.dword = 0;
1791 temp.bitfield.fword = 0;
1792 temp.bitfield.qword = 0;
1793 temp.bitfield.tbyte = 0;
1794 temp.bitfield.xmmword = 0;
1795 temp.bitfield.ymmword = 0;
1796 temp.bitfield.zmmword = 0;
1797 if (operand_type_all_zero (&temp))
1798 goto mismatch;
1799
1800 if (given.bitfield.baseindex == overlap.bitfield.baseindex
1801 && given.bitfield.jumpabsolute == overlap.bitfield.jumpabsolute)
1802 return 1;
1803
1804 mismatch:
1805 i.error = operand_type_mismatch;
1806 return 0;
1807 }
1808
1809 /* If given types g0 and g1 are registers they must be of the same type
1810 unless the expected operand type register overlap is null.
1811 Note that Acc in a template matches every size of reg. */
1812
1813 static INLINE int
1814 operand_type_register_match (i386_operand_type m0,
1815 i386_operand_type g0,
1816 i386_operand_type t0,
1817 i386_operand_type m1,
1818 i386_operand_type g1,
1819 i386_operand_type t1)
1820 {
1821 if (!operand_type_check (g0, reg))
1822 return 1;
1823
1824 if (!operand_type_check (g1, reg))
1825 return 1;
1826
1827 if (g0.bitfield.reg8 == g1.bitfield.reg8
1828 && g0.bitfield.reg16 == g1.bitfield.reg16
1829 && g0.bitfield.reg32 == g1.bitfield.reg32
1830 && g0.bitfield.reg64 == g1.bitfield.reg64)
1831 return 1;
1832
1833 if (m0.bitfield.acc)
1834 {
1835 t0.bitfield.reg8 = 1;
1836 t0.bitfield.reg16 = 1;
1837 t0.bitfield.reg32 = 1;
1838 t0.bitfield.reg64 = 1;
1839 }
1840
1841 if (m1.bitfield.acc)
1842 {
1843 t1.bitfield.reg8 = 1;
1844 t1.bitfield.reg16 = 1;
1845 t1.bitfield.reg32 = 1;
1846 t1.bitfield.reg64 = 1;
1847 }
1848
1849 if (!(t0.bitfield.reg8 & t1.bitfield.reg8)
1850 && !(t0.bitfield.reg16 & t1.bitfield.reg16)
1851 && !(t0.bitfield.reg32 & t1.bitfield.reg32)
1852 && !(t0.bitfield.reg64 & t1.bitfield.reg64))
1853 return 1;
1854
1855 i.error = register_type_mismatch;
1856
1857 return 0;
1858 }
1859
1860 static INLINE unsigned int
1861 register_number (const reg_entry *r)
1862 {
1863 unsigned int nr = r->reg_num;
1864
1865 if (r->reg_flags & RegRex)
1866 nr += 8;
1867
1868 return nr;
1869 }
1870
1871 static INLINE unsigned int
1872 mode_from_disp_size (i386_operand_type t)
1873 {
1874 if (t.bitfield.disp8 || t.bitfield.vec_disp8)
1875 return 1;
1876 else if (t.bitfield.disp16
1877 || t.bitfield.disp32
1878 || t.bitfield.disp32s)
1879 return 2;
1880 else
1881 return 0;
1882 }
1883
1884 static INLINE int
1885 fits_in_signed_byte (addressT num)
1886 {
1887 return num + 0x80 <= 0xff;
1888 }
1889
1890 static INLINE int
1891 fits_in_unsigned_byte (addressT num)
1892 {
1893 return num <= 0xff;
1894 }
1895
1896 static INLINE int
1897 fits_in_unsigned_word (addressT num)
1898 {
1899 return num <= 0xffff;
1900 }
1901
1902 static INLINE int
1903 fits_in_signed_word (addressT num)
1904 {
1905 return num + 0x8000 <= 0xffff;
1906 }
1907
1908 static INLINE int
1909 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
1910 {
1911 #ifndef BFD64
1912 return 1;
1913 #else
1914 return num + 0x80000000 <= 0xffffffff;
1915 #endif
1916 } /* fits_in_signed_long() */
1917
1918 static INLINE int
1919 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
1920 {
1921 #ifndef BFD64
1922 return 1;
1923 #else
1924 return num <= 0xffffffff;
1925 #endif
1926 } /* fits_in_unsigned_long() */
1927
1928 static INLINE int
1929 fits_in_vec_disp8 (offsetT num)
1930 {
1931 int shift = i.memshift;
1932 unsigned int mask;
1933
1934 if (shift == -1)
1935 abort ();
1936
1937 mask = (1 << shift) - 1;
1938
1939 /* Return 0 if NUM isn't properly aligned. */
1940 if ((num & mask))
1941 return 0;
1942
1943 /* Check if NUM will fit in 8bit after shift. */
1944 return fits_in_signed_byte (num >> shift);
1945 }
1946
1947 static INLINE int
1948 fits_in_imm4 (offsetT num)
1949 {
1950 return (num & 0xf) == num;
1951 }
1952
1953 static i386_operand_type
1954 smallest_imm_type (offsetT num)
1955 {
1956 i386_operand_type t;
1957
1958 operand_type_set (&t, 0);
1959 t.bitfield.imm64 = 1;
1960
1961 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
1962 {
1963 /* This code is disabled on the 486 because all the Imm1 forms
1964 in the opcode table are slower on the i486. They're the
1965 versions with the implicitly specified single-position
1966 displacement, which has another syntax if you really want to
1967 use that form. */
1968 t.bitfield.imm1 = 1;
1969 t.bitfield.imm8 = 1;
1970 t.bitfield.imm8s = 1;
1971 t.bitfield.imm16 = 1;
1972 t.bitfield.imm32 = 1;
1973 t.bitfield.imm32s = 1;
1974 }
1975 else if (fits_in_signed_byte (num))
1976 {
1977 t.bitfield.imm8 = 1;
1978 t.bitfield.imm8s = 1;
1979 t.bitfield.imm16 = 1;
1980 t.bitfield.imm32 = 1;
1981 t.bitfield.imm32s = 1;
1982 }
1983 else if (fits_in_unsigned_byte (num))
1984 {
1985 t.bitfield.imm8 = 1;
1986 t.bitfield.imm16 = 1;
1987 t.bitfield.imm32 = 1;
1988 t.bitfield.imm32s = 1;
1989 }
1990 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
1991 {
1992 t.bitfield.imm16 = 1;
1993 t.bitfield.imm32 = 1;
1994 t.bitfield.imm32s = 1;
1995 }
1996 else if (fits_in_signed_long (num))
1997 {
1998 t.bitfield.imm32 = 1;
1999 t.bitfield.imm32s = 1;
2000 }
2001 else if (fits_in_unsigned_long (num))
2002 t.bitfield.imm32 = 1;
2003
2004 return t;
2005 }
2006
2007 static offsetT
2008 offset_in_range (offsetT val, int size)
2009 {
2010 addressT mask;
2011
2012 switch (size)
2013 {
2014 case 1: mask = ((addressT) 1 << 8) - 1; break;
2015 case 2: mask = ((addressT) 1 << 16) - 1; break;
2016 case 4: mask = ((addressT) 2 << 31) - 1; break;
2017 #ifdef BFD64
2018 case 8: mask = ((addressT) 2 << 63) - 1; break;
2019 #endif
2020 default: abort ();
2021 }
2022
2023 #ifdef BFD64
2024 /* If BFD64, sign extend val for 32bit address mode. */
2025 if (flag_code != CODE_64BIT
2026 || i.prefix[ADDR_PREFIX])
2027 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
2028 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2029 #endif
2030
2031 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
2032 {
2033 char buf1[40], buf2[40];
2034
2035 sprint_value (buf1, val);
2036 sprint_value (buf2, val & mask);
2037 as_warn (_("%s shortened to %s"), buf1, buf2);
2038 }
2039 return val & mask;
2040 }
2041
2042 enum PREFIX_GROUP
2043 {
2044 PREFIX_EXIST = 0,
2045 PREFIX_LOCK,
2046 PREFIX_REP,
2047 PREFIX_OTHER
2048 };
2049
2050 /* Returns
2051 a. PREFIX_EXIST if attempting to add a prefix where one from the
2052 same class already exists.
2053 b. PREFIX_LOCK if lock prefix is added.
2054 c. PREFIX_REP if rep/repne prefix is added.
2055 d. PREFIX_OTHER if other prefix is added.
2056 */
2057
2058 static enum PREFIX_GROUP
2059 add_prefix (unsigned int prefix)
2060 {
2061 enum PREFIX_GROUP ret = PREFIX_OTHER;
2062 unsigned int q;
2063
2064 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2065 && flag_code == CODE_64BIT)
2066 {
2067 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2068 || ((i.prefix[REX_PREFIX] & (REX_R | REX_X | REX_B))
2069 && (prefix & (REX_R | REX_X | REX_B))))
2070 ret = PREFIX_EXIST;
2071 q = REX_PREFIX;
2072 }
2073 else
2074 {
2075 switch (prefix)
2076 {
2077 default:
2078 abort ();
2079
2080 case CS_PREFIX_OPCODE:
2081 case DS_PREFIX_OPCODE:
2082 case ES_PREFIX_OPCODE:
2083 case FS_PREFIX_OPCODE:
2084 case GS_PREFIX_OPCODE:
2085 case SS_PREFIX_OPCODE:
2086 q = SEG_PREFIX;
2087 break;
2088
2089 case REPNE_PREFIX_OPCODE:
2090 case REPE_PREFIX_OPCODE:
2091 q = REP_PREFIX;
2092 ret = PREFIX_REP;
2093 break;
2094
2095 case LOCK_PREFIX_OPCODE:
2096 q = LOCK_PREFIX;
2097 ret = PREFIX_LOCK;
2098 break;
2099
2100 case FWAIT_OPCODE:
2101 q = WAIT_PREFIX;
2102 break;
2103
2104 case ADDR_PREFIX_OPCODE:
2105 q = ADDR_PREFIX;
2106 break;
2107
2108 case DATA_PREFIX_OPCODE:
2109 q = DATA_PREFIX;
2110 break;
2111 }
2112 if (i.prefix[q] != 0)
2113 ret = PREFIX_EXIST;
2114 }
2115
2116 if (ret)
2117 {
2118 if (!i.prefix[q])
2119 ++i.prefixes;
2120 i.prefix[q] |= prefix;
2121 }
2122 else
2123 as_bad (_("same type of prefix used twice"));
2124
2125 return ret;
2126 }
2127
2128 static void
2129 update_code_flag (int value, int check)
2130 {
2131 PRINTF_LIKE ((*as_error));
2132
2133 flag_code = (enum flag_code) value;
2134 if (flag_code == CODE_64BIT)
2135 {
2136 cpu_arch_flags.bitfield.cpu64 = 1;
2137 cpu_arch_flags.bitfield.cpuno64 = 0;
2138 }
2139 else
2140 {
2141 cpu_arch_flags.bitfield.cpu64 = 0;
2142 cpu_arch_flags.bitfield.cpuno64 = 1;
2143 }
2144 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
2145 {
2146 if (check)
2147 as_error = as_fatal;
2148 else
2149 as_error = as_bad;
2150 (*as_error) (_("64bit mode not supported on `%s'."),
2151 cpu_arch_name ? cpu_arch_name : default_arch);
2152 }
2153 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2154 {
2155 if (check)
2156 as_error = as_fatal;
2157 else
2158 as_error = as_bad;
2159 (*as_error) (_("32bit mode not supported on `%s'."),
2160 cpu_arch_name ? cpu_arch_name : default_arch);
2161 }
2162 stackop_size = '\0';
2163 }
2164
2165 static void
2166 set_code_flag (int value)
2167 {
2168 update_code_flag (value, 0);
2169 }
2170
2171 static void
2172 set_16bit_gcc_code_flag (int new_code_flag)
2173 {
2174 flag_code = (enum flag_code) new_code_flag;
2175 if (flag_code != CODE_16BIT)
2176 abort ();
2177 cpu_arch_flags.bitfield.cpu64 = 0;
2178 cpu_arch_flags.bitfield.cpuno64 = 1;
2179 stackop_size = LONG_MNEM_SUFFIX;
2180 }
2181
2182 static void
2183 set_intel_syntax (int syntax_flag)
2184 {
2185 /* Find out if register prefixing is specified. */
2186 int ask_naked_reg = 0;
2187
2188 SKIP_WHITESPACE ();
2189 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2190 {
2191 char *string = input_line_pointer;
2192 int e = get_symbol_end ();
2193
2194 if (strcmp (string, "prefix") == 0)
2195 ask_naked_reg = 1;
2196 else if (strcmp (string, "noprefix") == 0)
2197 ask_naked_reg = -1;
2198 else
2199 as_bad (_("bad argument to syntax directive."));
2200 *input_line_pointer = e;
2201 }
2202 demand_empty_rest_of_line ();
2203
2204 intel_syntax = syntax_flag;
2205
2206 if (ask_naked_reg == 0)
2207 allow_naked_reg = (intel_syntax
2208 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2209 else
2210 allow_naked_reg = (ask_naked_reg < 0);
2211
2212 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2213
2214 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
2215 identifier_chars['$'] = intel_syntax ? '$' : 0;
2216 register_prefix = allow_naked_reg ? "" : "%";
2217 }
2218
2219 static void
2220 set_intel_mnemonic (int mnemonic_flag)
2221 {
2222 intel_mnemonic = mnemonic_flag;
2223 }
2224
2225 static void
2226 set_allow_index_reg (int flag)
2227 {
2228 allow_index_reg = flag;
2229 }
2230
2231 static void
2232 set_check (int what)
2233 {
2234 enum check_kind *kind;
2235 const char *str;
2236
2237 if (what)
2238 {
2239 kind = &operand_check;
2240 str = "operand";
2241 }
2242 else
2243 {
2244 kind = &sse_check;
2245 str = "sse";
2246 }
2247
2248 SKIP_WHITESPACE ();
2249
2250 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2251 {
2252 char *string = input_line_pointer;
2253 int e = get_symbol_end ();
2254
2255 if (strcmp (string, "none") == 0)
2256 *kind = check_none;
2257 else if (strcmp (string, "warning") == 0)
2258 *kind = check_warning;
2259 else if (strcmp (string, "error") == 0)
2260 *kind = check_error;
2261 else
2262 as_bad (_("bad argument to %s_check directive."), str);
2263 *input_line_pointer = e;
2264 }
2265 else
2266 as_bad (_("missing argument for %s_check directive"), str);
2267
2268 demand_empty_rest_of_line ();
2269 }
2270
2271 static void
2272 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2273 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2274 {
2275 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2276 static const char *arch;
2277
2278 /* Intel LIOM is only supported on ELF. */
2279 if (!IS_ELF)
2280 return;
2281
2282 if (!arch)
2283 {
2284 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2285 use default_arch. */
2286 arch = cpu_arch_name;
2287 if (!arch)
2288 arch = default_arch;
2289 }
2290
2291 /* If we are targeting Intel L1OM, we must enable it. */
2292 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
2293 || new_flag.bitfield.cpul1om)
2294 return;
2295
2296 /* If we are targeting Intel K1OM, we must enable it. */
2297 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2298 || new_flag.bitfield.cpuk1om)
2299 return;
2300
2301 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2302 #endif
2303 }
2304
2305 static void
2306 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2307 {
2308 SKIP_WHITESPACE ();
2309
2310 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2311 {
2312 char *string = input_line_pointer;
2313 int e = get_symbol_end ();
2314 unsigned int j;
2315 i386_cpu_flags flags;
2316
2317 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2318 {
2319 if (strcmp (string, cpu_arch[j].name) == 0)
2320 {
2321 check_cpu_arch_compatible (string, cpu_arch[j].flags);
2322
2323 if (*string != '.')
2324 {
2325 cpu_arch_name = cpu_arch[j].name;
2326 cpu_sub_arch_name = NULL;
2327 cpu_arch_flags = cpu_arch[j].flags;
2328 if (flag_code == CODE_64BIT)
2329 {
2330 cpu_arch_flags.bitfield.cpu64 = 1;
2331 cpu_arch_flags.bitfield.cpuno64 = 0;
2332 }
2333 else
2334 {
2335 cpu_arch_flags.bitfield.cpu64 = 0;
2336 cpu_arch_flags.bitfield.cpuno64 = 1;
2337 }
2338 cpu_arch_isa = cpu_arch[j].type;
2339 cpu_arch_isa_flags = cpu_arch[j].flags;
2340 if (!cpu_arch_tune_set)
2341 {
2342 cpu_arch_tune = cpu_arch_isa;
2343 cpu_arch_tune_flags = cpu_arch_isa_flags;
2344 }
2345 break;
2346 }
2347
2348 if (!cpu_arch[j].negated)
2349 flags = cpu_flags_or (cpu_arch_flags,
2350 cpu_arch[j].flags);
2351 else
2352 flags = cpu_flags_and_not (cpu_arch_flags,
2353 cpu_arch[j].flags);
2354 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2355 {
2356 if (cpu_sub_arch_name)
2357 {
2358 char *name = cpu_sub_arch_name;
2359 cpu_sub_arch_name = concat (name,
2360 cpu_arch[j].name,
2361 (const char *) NULL);
2362 free (name);
2363 }
2364 else
2365 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
2366 cpu_arch_flags = flags;
2367 cpu_arch_isa_flags = flags;
2368 }
2369 *input_line_pointer = e;
2370 demand_empty_rest_of_line ();
2371 return;
2372 }
2373 }
2374 if (j >= ARRAY_SIZE (cpu_arch))
2375 as_bad (_("no such architecture: `%s'"), string);
2376
2377 *input_line_pointer = e;
2378 }
2379 else
2380 as_bad (_("missing cpu architecture"));
2381
2382 no_cond_jump_promotion = 0;
2383 if (*input_line_pointer == ','
2384 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2385 {
2386 char *string = ++input_line_pointer;
2387 int e = get_symbol_end ();
2388
2389 if (strcmp (string, "nojumps") == 0)
2390 no_cond_jump_promotion = 1;
2391 else if (strcmp (string, "jumps") == 0)
2392 ;
2393 else
2394 as_bad (_("no such architecture modifier: `%s'"), string);
2395
2396 *input_line_pointer = e;
2397 }
2398
2399 demand_empty_rest_of_line ();
2400 }
2401
2402 enum bfd_architecture
2403 i386_arch (void)
2404 {
2405 if (cpu_arch_isa == PROCESSOR_L1OM)
2406 {
2407 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2408 || flag_code != CODE_64BIT)
2409 as_fatal (_("Intel L1OM is 64bit ELF only"));
2410 return bfd_arch_l1om;
2411 }
2412 else if (cpu_arch_isa == PROCESSOR_K1OM)
2413 {
2414 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2415 || flag_code != CODE_64BIT)
2416 as_fatal (_("Intel K1OM is 64bit ELF only"));
2417 return bfd_arch_k1om;
2418 }
2419 else
2420 return bfd_arch_i386;
2421 }
2422
2423 unsigned long
2424 i386_mach (void)
2425 {
2426 if (!strncmp (default_arch, "x86_64", 6))
2427 {
2428 if (cpu_arch_isa == PROCESSOR_L1OM)
2429 {
2430 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2431 || default_arch[6] != '\0')
2432 as_fatal (_("Intel L1OM is 64bit ELF only"));
2433 return bfd_mach_l1om;
2434 }
2435 else if (cpu_arch_isa == PROCESSOR_K1OM)
2436 {
2437 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2438 || default_arch[6] != '\0')
2439 as_fatal (_("Intel K1OM is 64bit ELF only"));
2440 return bfd_mach_k1om;
2441 }
2442 else if (default_arch[6] == '\0')
2443 return bfd_mach_x86_64;
2444 else
2445 return bfd_mach_x64_32;
2446 }
2447 else if (!strcmp (default_arch, "i386"))
2448 return bfd_mach_i386_i386;
2449 else
2450 as_fatal (_("unknown architecture"));
2451 }
2452 \f
2453 void
2454 md_begin (void)
2455 {
2456 const char *hash_err;
2457
2458 /* Initialize op_hash hash table. */
2459 op_hash = hash_new ();
2460
2461 {
2462 const insn_template *optab;
2463 templates *core_optab;
2464
2465 /* Setup for loop. */
2466 optab = i386_optab;
2467 core_optab = (templates *) xmalloc (sizeof (templates));
2468 core_optab->start = optab;
2469
2470 while (1)
2471 {
2472 ++optab;
2473 if (optab->name == NULL
2474 || strcmp (optab->name, (optab - 1)->name) != 0)
2475 {
2476 /* different name --> ship out current template list;
2477 add to hash table; & begin anew. */
2478 core_optab->end = optab;
2479 hash_err = hash_insert (op_hash,
2480 (optab - 1)->name,
2481 (void *) core_optab);
2482 if (hash_err)
2483 {
2484 as_fatal (_("can't hash %s: %s"),
2485 (optab - 1)->name,
2486 hash_err);
2487 }
2488 if (optab->name == NULL)
2489 break;
2490 core_optab = (templates *) xmalloc (sizeof (templates));
2491 core_optab->start = optab;
2492 }
2493 }
2494 }
2495
2496 /* Initialize reg_hash hash table. */
2497 reg_hash = hash_new ();
2498 {
2499 const reg_entry *regtab;
2500 unsigned int regtab_size = i386_regtab_size;
2501
2502 for (regtab = i386_regtab; regtab_size--; regtab++)
2503 {
2504 hash_err = hash_insert (reg_hash, regtab->reg_name, (void *) regtab);
2505 if (hash_err)
2506 as_fatal (_("can't hash %s: %s"),
2507 regtab->reg_name,
2508 hash_err);
2509 }
2510 }
2511
2512 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
2513 {
2514 int c;
2515 char *p;
2516
2517 for (c = 0; c < 256; c++)
2518 {
2519 if (ISDIGIT (c))
2520 {
2521 digit_chars[c] = c;
2522 mnemonic_chars[c] = c;
2523 register_chars[c] = c;
2524 operand_chars[c] = c;
2525 }
2526 else if (ISLOWER (c))
2527 {
2528 mnemonic_chars[c] = c;
2529 register_chars[c] = c;
2530 operand_chars[c] = c;
2531 }
2532 else if (ISUPPER (c))
2533 {
2534 mnemonic_chars[c] = TOLOWER (c);
2535 register_chars[c] = mnemonic_chars[c];
2536 operand_chars[c] = c;
2537 }
2538 else if (c == '{' || c == '}')
2539 operand_chars[c] = c;
2540
2541 if (ISALPHA (c) || ISDIGIT (c))
2542 identifier_chars[c] = c;
2543 else if (c >= 128)
2544 {
2545 identifier_chars[c] = c;
2546 operand_chars[c] = c;
2547 }
2548 }
2549
2550 #ifdef LEX_AT
2551 identifier_chars['@'] = '@';
2552 #endif
2553 #ifdef LEX_QM
2554 identifier_chars['?'] = '?';
2555 operand_chars['?'] = '?';
2556 #endif
2557 digit_chars['-'] = '-';
2558 mnemonic_chars['_'] = '_';
2559 mnemonic_chars['-'] = '-';
2560 mnemonic_chars['.'] = '.';
2561 identifier_chars['_'] = '_';
2562 identifier_chars['.'] = '.';
2563
2564 for (p = operand_special_chars; *p != '\0'; p++)
2565 operand_chars[(unsigned char) *p] = *p;
2566 }
2567
2568 if (flag_code == CODE_64BIT)
2569 {
2570 #if defined (OBJ_COFF) && defined (TE_PE)
2571 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
2572 ? 32 : 16);
2573 #else
2574 x86_dwarf2_return_column = 16;
2575 #endif
2576 x86_cie_data_alignment = -8;
2577 }
2578 else
2579 {
2580 x86_dwarf2_return_column = 8;
2581 x86_cie_data_alignment = -4;
2582 }
2583 }
2584
2585 void
2586 i386_print_statistics (FILE *file)
2587 {
2588 hash_print_statistics (file, "i386 opcode", op_hash);
2589 hash_print_statistics (file, "i386 register", reg_hash);
2590 }
2591 \f
2592 #ifdef DEBUG386
2593
2594 /* Debugging routines for md_assemble. */
2595 static void pte (insn_template *);
2596 static void pt (i386_operand_type);
2597 static void pe (expressionS *);
2598 static void ps (symbolS *);
2599
2600 static void
2601 pi (char *line, i386_insn *x)
2602 {
2603 unsigned int j;
2604
2605 fprintf (stdout, "%s: template ", line);
2606 pte (&x->tm);
2607 fprintf (stdout, " address: base %s index %s scale %x\n",
2608 x->base_reg ? x->base_reg->reg_name : "none",
2609 x->index_reg ? x->index_reg->reg_name : "none",
2610 x->log2_scale_factor);
2611 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
2612 x->rm.mode, x->rm.reg, x->rm.regmem);
2613 fprintf (stdout, " sib: base %x index %x scale %x\n",
2614 x->sib.base, x->sib.index, x->sib.scale);
2615 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
2616 (x->rex & REX_W) != 0,
2617 (x->rex & REX_R) != 0,
2618 (x->rex & REX_X) != 0,
2619 (x->rex & REX_B) != 0);
2620 for (j = 0; j < x->operands; j++)
2621 {
2622 fprintf (stdout, " #%d: ", j + 1);
2623 pt (x->types[j]);
2624 fprintf (stdout, "\n");
2625 if (x->types[j].bitfield.reg8
2626 || x->types[j].bitfield.reg16
2627 || x->types[j].bitfield.reg32
2628 || x->types[j].bitfield.reg64
2629 || x->types[j].bitfield.regmmx
2630 || x->types[j].bitfield.regxmm
2631 || x->types[j].bitfield.regymm
2632 || x->types[j].bitfield.regzmm
2633 || x->types[j].bitfield.sreg2
2634 || x->types[j].bitfield.sreg3
2635 || x->types[j].bitfield.control
2636 || x->types[j].bitfield.debug
2637 || x->types[j].bitfield.test)
2638 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
2639 if (operand_type_check (x->types[j], imm))
2640 pe (x->op[j].imms);
2641 if (operand_type_check (x->types[j], disp))
2642 pe (x->op[j].disps);
2643 }
2644 }
2645
2646 static void
2647 pte (insn_template *t)
2648 {
2649 unsigned int j;
2650 fprintf (stdout, " %d operands ", t->operands);
2651 fprintf (stdout, "opcode %x ", t->base_opcode);
2652 if (t->extension_opcode != None)
2653 fprintf (stdout, "ext %x ", t->extension_opcode);
2654 if (t->opcode_modifier.d)
2655 fprintf (stdout, "D");
2656 if (t->opcode_modifier.w)
2657 fprintf (stdout, "W");
2658 fprintf (stdout, "\n");
2659 for (j = 0; j < t->operands; j++)
2660 {
2661 fprintf (stdout, " #%d type ", j + 1);
2662 pt (t->operand_types[j]);
2663 fprintf (stdout, "\n");
2664 }
2665 }
2666
2667 static void
2668 pe (expressionS *e)
2669 {
2670 fprintf (stdout, " operation %d\n", e->X_op);
2671 fprintf (stdout, " add_number %ld (%lx)\n",
2672 (long) e->X_add_number, (long) e->X_add_number);
2673 if (e->X_add_symbol)
2674 {
2675 fprintf (stdout, " add_symbol ");
2676 ps (e->X_add_symbol);
2677 fprintf (stdout, "\n");
2678 }
2679 if (e->X_op_symbol)
2680 {
2681 fprintf (stdout, " op_symbol ");
2682 ps (e->X_op_symbol);
2683 fprintf (stdout, "\n");
2684 }
2685 }
2686
2687 static void
2688 ps (symbolS *s)
2689 {
2690 fprintf (stdout, "%s type %s%s",
2691 S_GET_NAME (s),
2692 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
2693 segment_name (S_GET_SEGMENT (s)));
2694 }
2695
2696 static struct type_name
2697 {
2698 i386_operand_type mask;
2699 const char *name;
2700 }
2701 const type_names[] =
2702 {
2703 { OPERAND_TYPE_REG8, "r8" },
2704 { OPERAND_TYPE_REG16, "r16" },
2705 { OPERAND_TYPE_REG32, "r32" },
2706 { OPERAND_TYPE_REG64, "r64" },
2707 { OPERAND_TYPE_IMM8, "i8" },
2708 { OPERAND_TYPE_IMM8, "i8s" },
2709 { OPERAND_TYPE_IMM16, "i16" },
2710 { OPERAND_TYPE_IMM32, "i32" },
2711 { OPERAND_TYPE_IMM32S, "i32s" },
2712 { OPERAND_TYPE_IMM64, "i64" },
2713 { OPERAND_TYPE_IMM1, "i1" },
2714 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
2715 { OPERAND_TYPE_DISP8, "d8" },
2716 { OPERAND_TYPE_DISP16, "d16" },
2717 { OPERAND_TYPE_DISP32, "d32" },
2718 { OPERAND_TYPE_DISP32S, "d32s" },
2719 { OPERAND_TYPE_DISP64, "d64" },
2720 { OPERAND_TYPE_VEC_DISP8, "Vector d8" },
2721 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
2722 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
2723 { OPERAND_TYPE_CONTROL, "control reg" },
2724 { OPERAND_TYPE_TEST, "test reg" },
2725 { OPERAND_TYPE_DEBUG, "debug reg" },
2726 { OPERAND_TYPE_FLOATREG, "FReg" },
2727 { OPERAND_TYPE_FLOATACC, "FAcc" },
2728 { OPERAND_TYPE_SREG2, "SReg2" },
2729 { OPERAND_TYPE_SREG3, "SReg3" },
2730 { OPERAND_TYPE_ACC, "Acc" },
2731 { OPERAND_TYPE_JUMPABSOLUTE, "Jump Absolute" },
2732 { OPERAND_TYPE_REGMMX, "rMMX" },
2733 { OPERAND_TYPE_REGXMM, "rXMM" },
2734 { OPERAND_TYPE_REGYMM, "rYMM" },
2735 { OPERAND_TYPE_REGZMM, "rZMM" },
2736 { OPERAND_TYPE_REGMASK, "Mask reg" },
2737 { OPERAND_TYPE_ESSEG, "es" },
2738 };
2739
2740 static void
2741 pt (i386_operand_type t)
2742 {
2743 unsigned int j;
2744 i386_operand_type a;
2745
2746 for (j = 0; j < ARRAY_SIZE (type_names); j++)
2747 {
2748 a = operand_type_and (t, type_names[j].mask);
2749 if (!operand_type_all_zero (&a))
2750 fprintf (stdout, "%s, ", type_names[j].name);
2751 }
2752 fflush (stdout);
2753 }
2754
2755 #endif /* DEBUG386 */
2756 \f
2757 static bfd_reloc_code_real_type
2758 reloc (unsigned int size,
2759 int pcrel,
2760 int sign,
2761 bfd_reloc_code_real_type other)
2762 {
2763 if (other != NO_RELOC)
2764 {
2765 reloc_howto_type *rel;
2766
2767 if (size == 8)
2768 switch (other)
2769 {
2770 case BFD_RELOC_X86_64_GOT32:
2771 return BFD_RELOC_X86_64_GOT64;
2772 break;
2773 case BFD_RELOC_X86_64_GOTPLT64:
2774 return BFD_RELOC_X86_64_GOTPLT64;
2775 break;
2776 case BFD_RELOC_X86_64_PLTOFF64:
2777 return BFD_RELOC_X86_64_PLTOFF64;
2778 break;
2779 case BFD_RELOC_X86_64_GOTPC32:
2780 other = BFD_RELOC_X86_64_GOTPC64;
2781 break;
2782 case BFD_RELOC_X86_64_GOTPCREL:
2783 other = BFD_RELOC_X86_64_GOTPCREL64;
2784 break;
2785 case BFD_RELOC_X86_64_TPOFF32:
2786 other = BFD_RELOC_X86_64_TPOFF64;
2787 break;
2788 case BFD_RELOC_X86_64_DTPOFF32:
2789 other = BFD_RELOC_X86_64_DTPOFF64;
2790 break;
2791 default:
2792 break;
2793 }
2794
2795 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2796 if (other == BFD_RELOC_SIZE32)
2797 {
2798 if (size == 8)
2799 other = BFD_RELOC_SIZE64;
2800 if (pcrel)
2801 {
2802 as_bad (_("there are no pc-relative size relocations"));
2803 return NO_RELOC;
2804 }
2805 }
2806 #endif
2807
2808 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
2809 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
2810 sign = -1;
2811
2812 rel = bfd_reloc_type_lookup (stdoutput, other);
2813 if (!rel)
2814 as_bad (_("unknown relocation (%u)"), other);
2815 else if (size != bfd_get_reloc_size (rel))
2816 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
2817 bfd_get_reloc_size (rel),
2818 size);
2819 else if (pcrel && !rel->pc_relative)
2820 as_bad (_("non-pc-relative relocation for pc-relative field"));
2821 else if ((rel->complain_on_overflow == complain_overflow_signed
2822 && !sign)
2823 || (rel->complain_on_overflow == complain_overflow_unsigned
2824 && sign > 0))
2825 as_bad (_("relocated field and relocation type differ in signedness"));
2826 else
2827 return other;
2828 return NO_RELOC;
2829 }
2830
2831 if (pcrel)
2832 {
2833 if (!sign)
2834 as_bad (_("there are no unsigned pc-relative relocations"));
2835 switch (size)
2836 {
2837 case 1: return BFD_RELOC_8_PCREL;
2838 case 2: return BFD_RELOC_16_PCREL;
2839 case 4: return BFD_RELOC_32_PCREL;
2840 case 8: return BFD_RELOC_64_PCREL;
2841 }
2842 as_bad (_("cannot do %u byte pc-relative relocation"), size);
2843 }
2844 else
2845 {
2846 if (sign > 0)
2847 switch (size)
2848 {
2849 case 4: return BFD_RELOC_X86_64_32S;
2850 }
2851 else
2852 switch (size)
2853 {
2854 case 1: return BFD_RELOC_8;
2855 case 2: return BFD_RELOC_16;
2856 case 4: return BFD_RELOC_32;
2857 case 8: return BFD_RELOC_64;
2858 }
2859 as_bad (_("cannot do %s %u byte relocation"),
2860 sign > 0 ? "signed" : "unsigned", size);
2861 }
2862
2863 return NO_RELOC;
2864 }
2865
2866 /* Here we decide which fixups can be adjusted to make them relative to
2867 the beginning of the section instead of the symbol. Basically we need
2868 to make sure that the dynamic relocations are done correctly, so in
2869 some cases we force the original symbol to be used. */
2870
2871 int
2872 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
2873 {
2874 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2875 if (!IS_ELF)
2876 return 1;
2877
2878 /* Don't adjust pc-relative references to merge sections in 64-bit
2879 mode. */
2880 if (use_rela_relocations
2881 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
2882 && fixP->fx_pcrel)
2883 return 0;
2884
2885 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
2886 and changed later by validate_fix. */
2887 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
2888 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
2889 return 0;
2890
2891 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
2892 for size relocations. */
2893 if (fixP->fx_r_type == BFD_RELOC_SIZE32
2894 || fixP->fx_r_type == BFD_RELOC_SIZE64
2895 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
2896 || fixP->fx_r_type == BFD_RELOC_386_PLT32
2897 || fixP->fx_r_type == BFD_RELOC_386_GOT32
2898 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
2899 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
2900 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
2901 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
2902 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
2903 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
2904 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
2905 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
2906 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
2907 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
2908 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
2909 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
2910 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
2911 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
2912 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
2913 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
2914 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
2915 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
2916 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
2917 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
2918 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
2919 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
2920 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
2921 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2922 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2923 return 0;
2924 #endif
2925 return 1;
2926 }
2927
2928 static int
2929 intel_float_operand (const char *mnemonic)
2930 {
2931 /* Note that the value returned is meaningful only for opcodes with (memory)
2932 operands, hence the code here is free to improperly handle opcodes that
2933 have no operands (for better performance and smaller code). */
2934
2935 if (mnemonic[0] != 'f')
2936 return 0; /* non-math */
2937
2938 switch (mnemonic[1])
2939 {
2940 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
2941 the fs segment override prefix not currently handled because no
2942 call path can make opcodes without operands get here */
2943 case 'i':
2944 return 2 /* integer op */;
2945 case 'l':
2946 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
2947 return 3; /* fldcw/fldenv */
2948 break;
2949 case 'n':
2950 if (mnemonic[2] != 'o' /* fnop */)
2951 return 3; /* non-waiting control op */
2952 break;
2953 case 'r':
2954 if (mnemonic[2] == 's')
2955 return 3; /* frstor/frstpm */
2956 break;
2957 case 's':
2958 if (mnemonic[2] == 'a')
2959 return 3; /* fsave */
2960 if (mnemonic[2] == 't')
2961 {
2962 switch (mnemonic[3])
2963 {
2964 case 'c': /* fstcw */
2965 case 'd': /* fstdw */
2966 case 'e': /* fstenv */
2967 case 's': /* fsts[gw] */
2968 return 3;
2969 }
2970 }
2971 break;
2972 case 'x':
2973 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
2974 return 0; /* fxsave/fxrstor are not really math ops */
2975 break;
2976 }
2977
2978 return 1;
2979 }
2980
2981 /* Build the VEX prefix. */
2982
2983 static void
2984 build_vex_prefix (const insn_template *t)
2985 {
2986 unsigned int register_specifier;
2987 unsigned int implied_prefix;
2988 unsigned int vector_length;
2989
2990 /* Check register specifier. */
2991 if (i.vex.register_specifier)
2992 {
2993 register_specifier =
2994 ~register_number (i.vex.register_specifier) & 0xf;
2995 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
2996 }
2997 else
2998 register_specifier = 0xf;
2999
3000 /* Use 2-byte VEX prefix by swappping destination and source
3001 operand. */
3002 if (!i.swap_operand
3003 && i.operands == i.reg_operands
3004 && i.tm.opcode_modifier.vexopcode == VEX0F
3005 && i.tm.opcode_modifier.s
3006 && i.rex == REX_B)
3007 {
3008 unsigned int xchg = i.operands - 1;
3009 union i386_op temp_op;
3010 i386_operand_type temp_type;
3011
3012 temp_type = i.types[xchg];
3013 i.types[xchg] = i.types[0];
3014 i.types[0] = temp_type;
3015 temp_op = i.op[xchg];
3016 i.op[xchg] = i.op[0];
3017 i.op[0] = temp_op;
3018
3019 gas_assert (i.rm.mode == 3);
3020
3021 i.rex = REX_R;
3022 xchg = i.rm.regmem;
3023 i.rm.regmem = i.rm.reg;
3024 i.rm.reg = xchg;
3025
3026 /* Use the next insn. */
3027 i.tm = t[1];
3028 }
3029
3030 if (i.tm.opcode_modifier.vex == VEXScalar)
3031 vector_length = avxscalar;
3032 else
3033 vector_length = i.tm.opcode_modifier.vex == VEX256 ? 1 : 0;
3034
3035 switch ((i.tm.base_opcode >> 8) & 0xff)
3036 {
3037 case 0:
3038 implied_prefix = 0;
3039 break;
3040 case DATA_PREFIX_OPCODE:
3041 implied_prefix = 1;
3042 break;
3043 case REPE_PREFIX_OPCODE:
3044 implied_prefix = 2;
3045 break;
3046 case REPNE_PREFIX_OPCODE:
3047 implied_prefix = 3;
3048 break;
3049 default:
3050 abort ();
3051 }
3052
3053 /* Use 2-byte VEX prefix if possible. */
3054 if (i.tm.opcode_modifier.vexopcode == VEX0F
3055 && i.tm.opcode_modifier.vexw != VEXW1
3056 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3057 {
3058 /* 2-byte VEX prefix. */
3059 unsigned int r;
3060
3061 i.vex.length = 2;
3062 i.vex.bytes[0] = 0xc5;
3063
3064 /* Check the REX.R bit. */
3065 r = (i.rex & REX_R) ? 0 : 1;
3066 i.vex.bytes[1] = (r << 7
3067 | register_specifier << 3
3068 | vector_length << 2
3069 | implied_prefix);
3070 }
3071 else
3072 {
3073 /* 3-byte VEX prefix. */
3074 unsigned int m, w;
3075
3076 i.vex.length = 3;
3077
3078 switch (i.tm.opcode_modifier.vexopcode)
3079 {
3080 case VEX0F:
3081 m = 0x1;
3082 i.vex.bytes[0] = 0xc4;
3083 break;
3084 case VEX0F38:
3085 m = 0x2;
3086 i.vex.bytes[0] = 0xc4;
3087 break;
3088 case VEX0F3A:
3089 m = 0x3;
3090 i.vex.bytes[0] = 0xc4;
3091 break;
3092 case XOP08:
3093 m = 0x8;
3094 i.vex.bytes[0] = 0x8f;
3095 break;
3096 case XOP09:
3097 m = 0x9;
3098 i.vex.bytes[0] = 0x8f;
3099 break;
3100 case XOP0A:
3101 m = 0xa;
3102 i.vex.bytes[0] = 0x8f;
3103 break;
3104 default:
3105 abort ();
3106 }
3107
3108 /* The high 3 bits of the second VEX byte are 1's compliment
3109 of RXB bits from REX. */
3110 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3111
3112 /* Check the REX.W bit. */
3113 w = (i.rex & REX_W) ? 1 : 0;
3114 if (i.tm.opcode_modifier.vexw == VEXW1)
3115 w = 1;
3116
3117 i.vex.bytes[2] = (w << 7
3118 | register_specifier << 3
3119 | vector_length << 2
3120 | implied_prefix);
3121 }
3122 }
3123
3124 /* Build the EVEX prefix. */
3125
3126 static void
3127 build_evex_prefix (void)
3128 {
3129 unsigned int register_specifier;
3130 unsigned int implied_prefix;
3131 unsigned int m, w;
3132 rex_byte vrex_used = 0;
3133
3134 /* Check register specifier. */
3135 if (i.vex.register_specifier)
3136 {
3137 gas_assert ((i.vrex & REX_X) == 0);
3138
3139 register_specifier = i.vex.register_specifier->reg_num;
3140 if ((i.vex.register_specifier->reg_flags & RegRex))
3141 register_specifier += 8;
3142 /* The upper 16 registers are encoded in the fourth byte of the
3143 EVEX prefix. */
3144 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3145 i.vex.bytes[3] = 0x8;
3146 register_specifier = ~register_specifier & 0xf;
3147 }
3148 else
3149 {
3150 register_specifier = 0xf;
3151
3152 /* Encode upper 16 vector index register in the fourth byte of
3153 the EVEX prefix. */
3154 if (!(i.vrex & REX_X))
3155 i.vex.bytes[3] = 0x8;
3156 else
3157 vrex_used |= REX_X;
3158 }
3159
3160 switch ((i.tm.base_opcode >> 8) & 0xff)
3161 {
3162 case 0:
3163 implied_prefix = 0;
3164 break;
3165 case DATA_PREFIX_OPCODE:
3166 implied_prefix = 1;
3167 break;
3168 case REPE_PREFIX_OPCODE:
3169 implied_prefix = 2;
3170 break;
3171 case REPNE_PREFIX_OPCODE:
3172 implied_prefix = 3;
3173 break;
3174 default:
3175 abort ();
3176 }
3177
3178 /* 4 byte EVEX prefix. */
3179 i.vex.length = 4;
3180 i.vex.bytes[0] = 0x62;
3181
3182 /* mmmm bits. */
3183 switch (i.tm.opcode_modifier.vexopcode)
3184 {
3185 case VEX0F:
3186 m = 1;
3187 break;
3188 case VEX0F38:
3189 m = 2;
3190 break;
3191 case VEX0F3A:
3192 m = 3;
3193 break;
3194 default:
3195 abort ();
3196 break;
3197 }
3198
3199 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3200 bits from REX. */
3201 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3202
3203 /* The fifth bit of the second EVEX byte is 1's compliment of the
3204 REX_R bit in VREX. */
3205 if (!(i.vrex & REX_R))
3206 i.vex.bytes[1] |= 0x10;
3207 else
3208 vrex_used |= REX_R;
3209
3210 if ((i.reg_operands + i.imm_operands) == i.operands)
3211 {
3212 /* When all operands are registers, the REX_X bit in REX is not
3213 used. We reuse it to encode the upper 16 registers, which is
3214 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3215 as 1's compliment. */
3216 if ((i.vrex & REX_B))
3217 {
3218 vrex_used |= REX_B;
3219 i.vex.bytes[1] &= ~0x40;
3220 }
3221 }
3222
3223 /* EVEX instructions shouldn't need the REX prefix. */
3224 i.vrex &= ~vrex_used;
3225 gas_assert (i.vrex == 0);
3226
3227 /* Check the REX.W bit. */
3228 w = (i.rex & REX_W) ? 1 : 0;
3229 if (i.tm.opcode_modifier.vexw)
3230 {
3231 if (i.tm.opcode_modifier.vexw == VEXW1)
3232 w = 1;
3233 }
3234 /* If w is not set it means we are dealing with WIG instruction. */
3235 else if (!w)
3236 {
3237 if (evexwig == evexw1)
3238 w = 1;
3239 }
3240
3241 /* Encode the U bit. */
3242 implied_prefix |= 0x4;
3243
3244 /* The third byte of the EVEX prefix. */
3245 i.vex.bytes[2] = (w << 7 | register_specifier << 3 | implied_prefix);
3246
3247 /* The fourth byte of the EVEX prefix. */
3248 /* The zeroing-masking bit. */
3249 if (i.mask && i.mask->zeroing)
3250 i.vex.bytes[3] |= 0x80;
3251
3252 /* Don't always set the broadcast bit if there is no RC. */
3253 if (!i.rounding)
3254 {
3255 /* Encode the vector length. */
3256 unsigned int vec_length;
3257
3258 switch (i.tm.opcode_modifier.evex)
3259 {
3260 case EVEXLIG: /* LL' is ignored */
3261 vec_length = evexlig << 5;
3262 break;
3263 case EVEX128:
3264 vec_length = 0 << 5;
3265 break;
3266 case EVEX256:
3267 vec_length = 1 << 5;
3268 break;
3269 case EVEX512:
3270 vec_length = 2 << 5;
3271 break;
3272 default:
3273 abort ();
3274 break;
3275 }
3276 i.vex.bytes[3] |= vec_length;
3277 /* Encode the broadcast bit. */
3278 if (i.broadcast)
3279 i.vex.bytes[3] |= 0x10;
3280 }
3281 else
3282 {
3283 if (i.rounding->type != saeonly)
3284 i.vex.bytes[3] |= 0x10 | (i.rounding->type << 5);
3285 else
3286 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
3287 }
3288
3289 if (i.mask && i.mask->mask)
3290 i.vex.bytes[3] |= i.mask->mask->reg_num;
3291 }
3292
3293 static void
3294 process_immext (void)
3295 {
3296 expressionS *exp;
3297
3298 if ((i.tm.cpu_flags.bitfield.cpusse3 || i.tm.cpu_flags.bitfield.cpusvme)
3299 && i.operands > 0)
3300 {
3301 /* MONITOR/MWAIT as well as SVME instructions have fixed operands
3302 with an opcode suffix which is coded in the same place as an
3303 8-bit immediate field would be.
3304 Here we check those operands and remove them afterwards. */
3305 unsigned int x;
3306
3307 for (x = 0; x < i.operands; x++)
3308 if (register_number (i.op[x].regs) != x)
3309 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
3310 register_prefix, i.op[x].regs->reg_name, x + 1,
3311 i.tm.name);
3312
3313 i.operands = 0;
3314 }
3315
3316 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
3317 which is coded in the same place as an 8-bit immediate field
3318 would be. Here we fake an 8-bit immediate operand from the
3319 opcode suffix stored in tm.extension_opcode.
3320
3321 AVX instructions also use this encoding, for some of
3322 3 argument instructions. */
3323
3324 gas_assert (i.imm_operands <= 1
3325 && (i.operands <= 2
3326 || ((i.tm.opcode_modifier.vex
3327 || i.tm.opcode_modifier.evex)
3328 && i.operands <= 4)));
3329
3330 exp = &im_expressions[i.imm_operands++];
3331 i.op[i.operands].imms = exp;
3332 i.types[i.operands] = imm8;
3333 i.operands++;
3334 exp->X_op = O_constant;
3335 exp->X_add_number = i.tm.extension_opcode;
3336 i.tm.extension_opcode = None;
3337 }
3338
3339
3340 static int
3341 check_hle (void)
3342 {
3343 switch (i.tm.opcode_modifier.hleprefixok)
3344 {
3345 default:
3346 abort ();
3347 case HLEPrefixNone:
3348 as_bad (_("invalid instruction `%s' after `%s'"),
3349 i.tm.name, i.hle_prefix);
3350 return 0;
3351 case HLEPrefixLock:
3352 if (i.prefix[LOCK_PREFIX])
3353 return 1;
3354 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
3355 return 0;
3356 case HLEPrefixAny:
3357 return 1;
3358 case HLEPrefixRelease:
3359 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
3360 {
3361 as_bad (_("instruction `%s' after `xacquire' not allowed"),
3362 i.tm.name);
3363 return 0;
3364 }
3365 if (i.mem_operands == 0
3366 || !operand_type_check (i.types[i.operands - 1], anymem))
3367 {
3368 as_bad (_("memory destination needed for instruction `%s'"
3369 " after `xrelease'"), i.tm.name);
3370 return 0;
3371 }
3372 return 1;
3373 }
3374 }
3375
3376 /* This is the guts of the machine-dependent assembler. LINE points to a
3377 machine dependent instruction. This function is supposed to emit
3378 the frags/bytes it assembles to. */
3379
3380 void
3381 md_assemble (char *line)
3382 {
3383 unsigned int j;
3384 char mnemonic[MAX_MNEM_SIZE];
3385 const insn_template *t;
3386
3387 /* Initialize globals. */
3388 memset (&i, '\0', sizeof (i));
3389 for (j = 0; j < MAX_OPERANDS; j++)
3390 i.reloc[j] = NO_RELOC;
3391 memset (disp_expressions, '\0', sizeof (disp_expressions));
3392 memset (im_expressions, '\0', sizeof (im_expressions));
3393 save_stack_p = save_stack;
3394
3395 /* First parse an instruction mnemonic & call i386_operand for the operands.
3396 We assume that the scrubber has arranged it so that line[0] is the valid
3397 start of a (possibly prefixed) mnemonic. */
3398
3399 line = parse_insn (line, mnemonic);
3400 if (line == NULL)
3401 return;
3402
3403 line = parse_operands (line, mnemonic);
3404 this_operand = -1;
3405 if (line == NULL)
3406 return;
3407
3408 /* Now we've parsed the mnemonic into a set of templates, and have the
3409 operands at hand. */
3410
3411 /* All intel opcodes have reversed operands except for "bound" and
3412 "enter". We also don't reverse intersegment "jmp" and "call"
3413 instructions with 2 immediate operands so that the immediate segment
3414 precedes the offset, as it does when in AT&T mode. */
3415 if (intel_syntax
3416 && i.operands > 1
3417 && (strcmp (mnemonic, "bound") != 0)
3418 && (strcmp (mnemonic, "invlpga") != 0)
3419 && !(operand_type_check (i.types[0], imm)
3420 && operand_type_check (i.types[1], imm)))
3421 swap_operands ();
3422
3423 /* The order of the immediates should be reversed
3424 for 2 immediates extrq and insertq instructions */
3425 if (i.imm_operands == 2
3426 && (strcmp (mnemonic, "extrq") == 0
3427 || strcmp (mnemonic, "insertq") == 0))
3428 swap_2_operands (0, 1);
3429
3430 if (i.imm_operands)
3431 optimize_imm ();
3432
3433 /* Don't optimize displacement for movabs since it only takes 64bit
3434 displacement. */
3435 if (i.disp_operands
3436 && i.disp_encoding != disp_encoding_32bit
3437 && (flag_code != CODE_64BIT
3438 || strcmp (mnemonic, "movabs") != 0))
3439 optimize_disp ();
3440
3441 /* Next, we find a template that matches the given insn,
3442 making sure the overlap of the given operands types is consistent
3443 with the template operand types. */
3444
3445 if (!(t = match_template ()))
3446 return;
3447
3448 if (sse_check != check_none
3449 && !i.tm.opcode_modifier.noavx
3450 && (i.tm.cpu_flags.bitfield.cpusse
3451 || i.tm.cpu_flags.bitfield.cpusse2
3452 || i.tm.cpu_flags.bitfield.cpusse3
3453 || i.tm.cpu_flags.bitfield.cpussse3
3454 || i.tm.cpu_flags.bitfield.cpusse4_1
3455 || i.tm.cpu_flags.bitfield.cpusse4_2))
3456 {
3457 (sse_check == check_warning
3458 ? as_warn
3459 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
3460 }
3461
3462 /* Zap movzx and movsx suffix. The suffix has been set from
3463 "word ptr" or "byte ptr" on the source operand in Intel syntax
3464 or extracted from mnemonic in AT&T syntax. But we'll use
3465 the destination register to choose the suffix for encoding. */
3466 if ((i.tm.base_opcode & ~9) == 0x0fb6)
3467 {
3468 /* In Intel syntax, there must be a suffix. In AT&T syntax, if
3469 there is no suffix, the default will be byte extension. */
3470 if (i.reg_operands != 2
3471 && !i.suffix
3472 && intel_syntax)
3473 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
3474
3475 i.suffix = 0;
3476 }
3477
3478 if (i.tm.opcode_modifier.fwait)
3479 if (!add_prefix (FWAIT_OPCODE))
3480 return;
3481
3482 /* Check if REP prefix is OK. */
3483 if (i.rep_prefix && !i.tm.opcode_modifier.repprefixok)
3484 {
3485 as_bad (_("invalid instruction `%s' after `%s'"),
3486 i.tm.name, i.rep_prefix);
3487 return;
3488 }
3489
3490 /* Check for lock without a lockable instruction. Destination operand
3491 must be memory unless it is xchg (0x86). */
3492 if (i.prefix[LOCK_PREFIX]
3493 && (!i.tm.opcode_modifier.islockable
3494 || i.mem_operands == 0
3495 || (i.tm.base_opcode != 0x86
3496 && !operand_type_check (i.types[i.operands - 1], anymem))))
3497 {
3498 as_bad (_("expecting lockable instruction after `lock'"));
3499 return;
3500 }
3501
3502 /* Check if HLE prefix is OK. */
3503 if (i.hle_prefix && !check_hle ())
3504 return;
3505
3506 /* Check BND prefix. */
3507 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
3508 as_bad (_("expecting valid branch instruction after `bnd'"));
3509
3510 if (i.tm.cpu_flags.bitfield.cpumpx
3511 && flag_code == CODE_64BIT
3512 && i.prefix[ADDR_PREFIX])
3513 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
3514
3515 /* Insert BND prefix. */
3516 if (add_bnd_prefix
3517 && i.tm.opcode_modifier.bndprefixok
3518 && !i.prefix[BND_PREFIX])
3519 add_prefix (BND_PREFIX_OPCODE);
3520
3521 /* Check string instruction segment overrides. */
3522 if (i.tm.opcode_modifier.isstring && i.mem_operands != 0)
3523 {
3524 if (!check_string ())
3525 return;
3526 i.disp_operands = 0;
3527 }
3528
3529 if (!process_suffix ())
3530 return;
3531
3532 /* Update operand types. */
3533 for (j = 0; j < i.operands; j++)
3534 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3535
3536 /* Make still unresolved immediate matches conform to size of immediate
3537 given in i.suffix. */
3538 if (!finalize_imm ())
3539 return;
3540
3541 if (i.types[0].bitfield.imm1)
3542 i.imm_operands = 0; /* kludge for shift insns. */
3543
3544 /* We only need to check those implicit registers for instructions
3545 with 3 operands or less. */
3546 if (i.operands <= 3)
3547 for (j = 0; j < i.operands; j++)
3548 if (i.types[j].bitfield.inoutportreg
3549 || i.types[j].bitfield.shiftcount
3550 || i.types[j].bitfield.acc
3551 || i.types[j].bitfield.floatacc)
3552 i.reg_operands--;
3553
3554 /* ImmExt should be processed after SSE2AVX. */
3555 if (!i.tm.opcode_modifier.sse2avx
3556 && i.tm.opcode_modifier.immext)
3557 process_immext ();
3558
3559 /* For insns with operands there are more diddles to do to the opcode. */
3560 if (i.operands)
3561 {
3562 if (!process_operands ())
3563 return;
3564 }
3565 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
3566 {
3567 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
3568 as_warn (_("translating to `%sp'"), i.tm.name);
3569 }
3570
3571 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
3572 {
3573 if (flag_code == CODE_16BIT)
3574 {
3575 as_bad (_("instruction `%s' isn't supported in 16-bit mode."),
3576 i.tm.name);
3577 return;
3578 }
3579
3580 if (i.tm.opcode_modifier.vex)
3581 build_vex_prefix (t);
3582 else
3583 build_evex_prefix ();
3584 }
3585
3586 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
3587 instructions may define INT_OPCODE as well, so avoid this corner
3588 case for those instructions that use MODRM. */
3589 if (i.tm.base_opcode == INT_OPCODE
3590 && !i.tm.opcode_modifier.modrm
3591 && i.op[0].imms->X_add_number == 3)
3592 {
3593 i.tm.base_opcode = INT3_OPCODE;
3594 i.imm_operands = 0;
3595 }
3596
3597 if ((i.tm.opcode_modifier.jump
3598 || i.tm.opcode_modifier.jumpbyte
3599 || i.tm.opcode_modifier.jumpdword)
3600 && i.op[0].disps->X_op == O_constant)
3601 {
3602 /* Convert "jmp constant" (and "call constant") to a jump (call) to
3603 the absolute address given by the constant. Since ix86 jumps and
3604 calls are pc relative, we need to generate a reloc. */
3605 i.op[0].disps->X_add_symbol = &abs_symbol;
3606 i.op[0].disps->X_op = O_symbol;
3607 }
3608
3609 if (i.tm.opcode_modifier.rex64)
3610 i.rex |= REX_W;
3611
3612 /* For 8 bit registers we need an empty rex prefix. Also if the
3613 instruction already has a prefix, we need to convert old
3614 registers to new ones. */
3615
3616 if ((i.types[0].bitfield.reg8
3617 && (i.op[0].regs->reg_flags & RegRex64) != 0)
3618 || (i.types[1].bitfield.reg8
3619 && (i.op[1].regs->reg_flags & RegRex64) != 0)
3620 || ((i.types[0].bitfield.reg8
3621 || i.types[1].bitfield.reg8)
3622 && i.rex != 0))
3623 {
3624 int x;
3625
3626 i.rex |= REX_OPCODE;
3627 for (x = 0; x < 2; x++)
3628 {
3629 /* Look for 8 bit operand that uses old registers. */
3630 if (i.types[x].bitfield.reg8
3631 && (i.op[x].regs->reg_flags & RegRex64) == 0)
3632 {
3633 /* In case it is "hi" register, give up. */
3634 if (i.op[x].regs->reg_num > 3)
3635 as_bad (_("can't encode register '%s%s' in an "
3636 "instruction requiring REX prefix."),
3637 register_prefix, i.op[x].regs->reg_name);
3638
3639 /* Otherwise it is equivalent to the extended register.
3640 Since the encoding doesn't change this is merely
3641 cosmetic cleanup for debug output. */
3642
3643 i.op[x].regs = i.op[x].regs + 8;
3644 }
3645 }
3646 }
3647
3648 if (i.rex != 0)
3649 add_prefix (REX_OPCODE | i.rex);
3650
3651 /* We are ready to output the insn. */
3652 output_insn ();
3653 }
3654
3655 static char *
3656 parse_insn (char *line, char *mnemonic)
3657 {
3658 char *l = line;
3659 char *token_start = l;
3660 char *mnem_p;
3661 int supported;
3662 const insn_template *t;
3663 char *dot_p = NULL;
3664
3665 while (1)
3666 {
3667 mnem_p = mnemonic;
3668 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
3669 {
3670 if (*mnem_p == '.')
3671 dot_p = mnem_p;
3672 mnem_p++;
3673 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
3674 {
3675 as_bad (_("no such instruction: `%s'"), token_start);
3676 return NULL;
3677 }
3678 l++;
3679 }
3680 if (!is_space_char (*l)
3681 && *l != END_OF_INSN
3682 && (intel_syntax
3683 || (*l != PREFIX_SEPARATOR
3684 && *l != ',')))
3685 {
3686 as_bad (_("invalid character %s in mnemonic"),
3687 output_invalid (*l));
3688 return NULL;
3689 }
3690 if (token_start == l)
3691 {
3692 if (!intel_syntax && *l == PREFIX_SEPARATOR)
3693 as_bad (_("expecting prefix; got nothing"));
3694 else
3695 as_bad (_("expecting mnemonic; got nothing"));
3696 return NULL;
3697 }
3698
3699 /* Look up instruction (or prefix) via hash table. */
3700 current_templates = (const templates *) hash_find (op_hash, mnemonic);
3701
3702 if (*l != END_OF_INSN
3703 && (!is_space_char (*l) || l[1] != END_OF_INSN)
3704 && current_templates
3705 && current_templates->start->opcode_modifier.isprefix)
3706 {
3707 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
3708 {
3709 as_bad ((flag_code != CODE_64BIT
3710 ? _("`%s' is only supported in 64-bit mode")
3711 : _("`%s' is not supported in 64-bit mode")),
3712 current_templates->start->name);
3713 return NULL;
3714 }
3715 /* If we are in 16-bit mode, do not allow addr16 or data16.
3716 Similarly, in 32-bit mode, do not allow addr32 or data32. */
3717 if ((current_templates->start->opcode_modifier.size16
3718 || current_templates->start->opcode_modifier.size32)
3719 && flag_code != CODE_64BIT
3720 && (current_templates->start->opcode_modifier.size32
3721 ^ (flag_code == CODE_16BIT)))
3722 {
3723 as_bad (_("redundant %s prefix"),
3724 current_templates->start->name);
3725 return NULL;
3726 }
3727 /* Add prefix, checking for repeated prefixes. */
3728 switch (add_prefix (current_templates->start->base_opcode))
3729 {
3730 case PREFIX_EXIST:
3731 return NULL;
3732 case PREFIX_REP:
3733 if (current_templates->start->cpu_flags.bitfield.cpuhle)
3734 i.hle_prefix = current_templates->start->name;
3735 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
3736 i.bnd_prefix = current_templates->start->name;
3737 else
3738 i.rep_prefix = current_templates->start->name;
3739 break;
3740 default:
3741 break;
3742 }
3743 /* Skip past PREFIX_SEPARATOR and reset token_start. */
3744 token_start = ++l;
3745 }
3746 else
3747 break;
3748 }
3749
3750 if (!current_templates)
3751 {
3752 /* Check if we should swap operand or force 32bit displacement in
3753 encoding. */
3754 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
3755 i.swap_operand = 1;
3756 else if (mnem_p - 3 == dot_p
3757 && dot_p[1] == 'd'
3758 && dot_p[2] == '8')
3759 i.disp_encoding = disp_encoding_8bit;
3760 else if (mnem_p - 4 == dot_p
3761 && dot_p[1] == 'd'
3762 && dot_p[2] == '3'
3763 && dot_p[3] == '2')
3764 i.disp_encoding = disp_encoding_32bit;
3765 else
3766 goto check_suffix;
3767 mnem_p = dot_p;
3768 *dot_p = '\0';
3769 current_templates = (const templates *) hash_find (op_hash, mnemonic);
3770 }
3771
3772 if (!current_templates)
3773 {
3774 check_suffix:
3775 /* See if we can get a match by trimming off a suffix. */
3776 switch (mnem_p[-1])
3777 {
3778 case WORD_MNEM_SUFFIX:
3779 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
3780 i.suffix = SHORT_MNEM_SUFFIX;
3781 else
3782 case BYTE_MNEM_SUFFIX:
3783 case QWORD_MNEM_SUFFIX:
3784 i.suffix = mnem_p[-1];
3785 mnem_p[-1] = '\0';
3786 current_templates = (const templates *) hash_find (op_hash,
3787 mnemonic);
3788 break;
3789 case SHORT_MNEM_SUFFIX:
3790 case LONG_MNEM_SUFFIX:
3791 if (!intel_syntax)
3792 {
3793 i.suffix = mnem_p[-1];
3794 mnem_p[-1] = '\0';
3795 current_templates = (const templates *) hash_find (op_hash,
3796 mnemonic);
3797 }
3798 break;
3799
3800 /* Intel Syntax. */
3801 case 'd':
3802 if (intel_syntax)
3803 {
3804 if (intel_float_operand (mnemonic) == 1)
3805 i.suffix = SHORT_MNEM_SUFFIX;
3806 else
3807 i.suffix = LONG_MNEM_SUFFIX;
3808 mnem_p[-1] = '\0';
3809 current_templates = (const templates *) hash_find (op_hash,
3810 mnemonic);
3811 }
3812 break;
3813 }
3814 if (!current_templates)
3815 {
3816 as_bad (_("no such instruction: `%s'"), token_start);
3817 return NULL;
3818 }
3819 }
3820
3821 if (current_templates->start->opcode_modifier.jump
3822 || current_templates->start->opcode_modifier.jumpbyte)
3823 {
3824 /* Check for a branch hint. We allow ",pt" and ",pn" for
3825 predict taken and predict not taken respectively.
3826 I'm not sure that branch hints actually do anything on loop
3827 and jcxz insns (JumpByte) for current Pentium4 chips. They
3828 may work in the future and it doesn't hurt to accept them
3829 now. */
3830 if (l[0] == ',' && l[1] == 'p')
3831 {
3832 if (l[2] == 't')
3833 {
3834 if (!add_prefix (DS_PREFIX_OPCODE))
3835 return NULL;
3836 l += 3;
3837 }
3838 else if (l[2] == 'n')
3839 {
3840 if (!add_prefix (CS_PREFIX_OPCODE))
3841 return NULL;
3842 l += 3;
3843 }
3844 }
3845 }
3846 /* Any other comma loses. */
3847 if (*l == ',')
3848 {
3849 as_bad (_("invalid character %s in mnemonic"),
3850 output_invalid (*l));
3851 return NULL;
3852 }
3853
3854 /* Check if instruction is supported on specified architecture. */
3855 supported = 0;
3856 for (t = current_templates->start; t < current_templates->end; ++t)
3857 {
3858 supported |= cpu_flags_match (t);
3859 if (supported == CPU_FLAGS_PERFECT_MATCH)
3860 goto skip;
3861 }
3862
3863 if (!(supported & CPU_FLAGS_64BIT_MATCH))
3864 {
3865 as_bad (flag_code == CODE_64BIT
3866 ? _("`%s' is not supported in 64-bit mode")
3867 : _("`%s' is only supported in 64-bit mode"),
3868 current_templates->start->name);
3869 return NULL;
3870 }
3871 if (supported != CPU_FLAGS_PERFECT_MATCH)
3872 {
3873 as_bad (_("`%s' is not supported on `%s%s'"),
3874 current_templates->start->name,
3875 cpu_arch_name ? cpu_arch_name : default_arch,
3876 cpu_sub_arch_name ? cpu_sub_arch_name : "");
3877 return NULL;
3878 }
3879
3880 skip:
3881 if (!cpu_arch_flags.bitfield.cpui386
3882 && (flag_code != CODE_16BIT))
3883 {
3884 as_warn (_("use .code16 to ensure correct addressing mode"));
3885 }
3886
3887 return l;
3888 }
3889
3890 static char *
3891 parse_operands (char *l, const char *mnemonic)
3892 {
3893 char *token_start;
3894
3895 /* 1 if operand is pending after ','. */
3896 unsigned int expecting_operand = 0;
3897
3898 /* Non-zero if operand parens not balanced. */
3899 unsigned int paren_not_balanced;
3900
3901 while (*l != END_OF_INSN)
3902 {
3903 /* Skip optional white space before operand. */
3904 if (is_space_char (*l))
3905 ++l;
3906 if (!is_operand_char (*l) && *l != END_OF_INSN)
3907 {
3908 as_bad (_("invalid character %s before operand %d"),
3909 output_invalid (*l),
3910 i.operands + 1);
3911 return NULL;
3912 }
3913 token_start = l; /* after white space */
3914 paren_not_balanced = 0;
3915 while (paren_not_balanced || *l != ',')
3916 {
3917 if (*l == END_OF_INSN)
3918 {
3919 if (paren_not_balanced)
3920 {
3921 if (!intel_syntax)
3922 as_bad (_("unbalanced parenthesis in operand %d."),
3923 i.operands + 1);
3924 else
3925 as_bad (_("unbalanced brackets in operand %d."),
3926 i.operands + 1);
3927 return NULL;
3928 }
3929 else
3930 break; /* we are done */
3931 }
3932 else if (!is_operand_char (*l) && !is_space_char (*l))
3933 {
3934 as_bad (_("invalid character %s in operand %d"),
3935 output_invalid (*l),
3936 i.operands + 1);
3937 return NULL;
3938 }
3939 if (!intel_syntax)
3940 {
3941 if (*l == '(')
3942 ++paren_not_balanced;
3943 if (*l == ')')
3944 --paren_not_balanced;
3945 }
3946 else
3947 {
3948 if (*l == '[')
3949 ++paren_not_balanced;
3950 if (*l == ']')
3951 --paren_not_balanced;
3952 }
3953 l++;
3954 }
3955 if (l != token_start)
3956 { /* Yes, we've read in another operand. */
3957 unsigned int operand_ok;
3958 this_operand = i.operands++;
3959 i.types[this_operand].bitfield.unspecified = 1;
3960 if (i.operands > MAX_OPERANDS)
3961 {
3962 as_bad (_("spurious operands; (%d operands/instruction max)"),
3963 MAX_OPERANDS);
3964 return NULL;
3965 }
3966 /* Now parse operand adding info to 'i' as we go along. */
3967 END_STRING_AND_SAVE (l);
3968
3969 if (intel_syntax)
3970 operand_ok =
3971 i386_intel_operand (token_start,
3972 intel_float_operand (mnemonic));
3973 else
3974 operand_ok = i386_att_operand (token_start);
3975
3976 RESTORE_END_STRING (l);
3977 if (!operand_ok)
3978 return NULL;
3979 }
3980 else
3981 {
3982 if (expecting_operand)
3983 {
3984 expecting_operand_after_comma:
3985 as_bad (_("expecting operand after ','; got nothing"));
3986 return NULL;
3987 }
3988 if (*l == ',')
3989 {
3990 as_bad (_("expecting operand before ','; got nothing"));
3991 return NULL;
3992 }
3993 }
3994
3995 /* Now *l must be either ',' or END_OF_INSN. */
3996 if (*l == ',')
3997 {
3998 if (*++l == END_OF_INSN)
3999 {
4000 /* Just skip it, if it's \n complain. */
4001 goto expecting_operand_after_comma;
4002 }
4003 expecting_operand = 1;
4004 }
4005 }
4006 return l;
4007 }
4008
4009 static void
4010 swap_2_operands (int xchg1, int xchg2)
4011 {
4012 union i386_op temp_op;
4013 i386_operand_type temp_type;
4014 enum bfd_reloc_code_real temp_reloc;
4015
4016 temp_type = i.types[xchg2];
4017 i.types[xchg2] = i.types[xchg1];
4018 i.types[xchg1] = temp_type;
4019 temp_op = i.op[xchg2];
4020 i.op[xchg2] = i.op[xchg1];
4021 i.op[xchg1] = temp_op;
4022 temp_reloc = i.reloc[xchg2];
4023 i.reloc[xchg2] = i.reloc[xchg1];
4024 i.reloc[xchg1] = temp_reloc;
4025
4026 if (i.mask)
4027 {
4028 if (i.mask->operand == xchg1)
4029 i.mask->operand = xchg2;
4030 else if (i.mask->operand == xchg2)
4031 i.mask->operand = xchg1;
4032 }
4033 if (i.broadcast)
4034 {
4035 if (i.broadcast->operand == xchg1)
4036 i.broadcast->operand = xchg2;
4037 else if (i.broadcast->operand == xchg2)
4038 i.broadcast->operand = xchg1;
4039 }
4040 if (i.rounding)
4041 {
4042 if (i.rounding->operand == xchg1)
4043 i.rounding->operand = xchg2;
4044 else if (i.rounding->operand == xchg2)
4045 i.rounding->operand = xchg1;
4046 }
4047 }
4048
4049 static void
4050 swap_operands (void)
4051 {
4052 switch (i.operands)
4053 {
4054 case 5:
4055 case 4:
4056 swap_2_operands (1, i.operands - 2);
4057 case 3:
4058 case 2:
4059 swap_2_operands (0, i.operands - 1);
4060 break;
4061 default:
4062 abort ();
4063 }
4064
4065 if (i.mem_operands == 2)
4066 {
4067 const seg_entry *temp_seg;
4068 temp_seg = i.seg[0];
4069 i.seg[0] = i.seg[1];
4070 i.seg[1] = temp_seg;
4071 }
4072 }
4073
4074 /* Try to ensure constant immediates are represented in the smallest
4075 opcode possible. */
4076 static void
4077 optimize_imm (void)
4078 {
4079 char guess_suffix = 0;
4080 int op;
4081
4082 if (i.suffix)
4083 guess_suffix = i.suffix;
4084 else if (i.reg_operands)
4085 {
4086 /* Figure out a suffix from the last register operand specified.
4087 We can't do this properly yet, ie. excluding InOutPortReg,
4088 but the following works for instructions with immediates.
4089 In any case, we can't set i.suffix yet. */
4090 for (op = i.operands; --op >= 0;)
4091 if (i.types[op].bitfield.reg8)
4092 {
4093 guess_suffix = BYTE_MNEM_SUFFIX;
4094 break;
4095 }
4096 else if (i.types[op].bitfield.reg16)
4097 {
4098 guess_suffix = WORD_MNEM_SUFFIX;
4099 break;
4100 }
4101 else if (i.types[op].bitfield.reg32)
4102 {
4103 guess_suffix = LONG_MNEM_SUFFIX;
4104 break;
4105 }
4106 else if (i.types[op].bitfield.reg64)
4107 {
4108 guess_suffix = QWORD_MNEM_SUFFIX;
4109 break;
4110 }
4111 }
4112 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
4113 guess_suffix = WORD_MNEM_SUFFIX;
4114
4115 for (op = i.operands; --op >= 0;)
4116 if (operand_type_check (i.types[op], imm))
4117 {
4118 switch (i.op[op].imms->X_op)
4119 {
4120 case O_constant:
4121 /* If a suffix is given, this operand may be shortened. */
4122 switch (guess_suffix)
4123 {
4124 case LONG_MNEM_SUFFIX:
4125 i.types[op].bitfield.imm32 = 1;
4126 i.types[op].bitfield.imm64 = 1;
4127 break;
4128 case WORD_MNEM_SUFFIX:
4129 i.types[op].bitfield.imm16 = 1;
4130 i.types[op].bitfield.imm32 = 1;
4131 i.types[op].bitfield.imm32s = 1;
4132 i.types[op].bitfield.imm64 = 1;
4133 break;
4134 case BYTE_MNEM_SUFFIX:
4135 i.types[op].bitfield.imm8 = 1;
4136 i.types[op].bitfield.imm8s = 1;
4137 i.types[op].bitfield.imm16 = 1;
4138 i.types[op].bitfield.imm32 = 1;
4139 i.types[op].bitfield.imm32s = 1;
4140 i.types[op].bitfield.imm64 = 1;
4141 break;
4142 }
4143
4144 /* If this operand is at most 16 bits, convert it
4145 to a signed 16 bit number before trying to see
4146 whether it will fit in an even smaller size.
4147 This allows a 16-bit operand such as $0xffe0 to
4148 be recognised as within Imm8S range. */
4149 if ((i.types[op].bitfield.imm16)
4150 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
4151 {
4152 i.op[op].imms->X_add_number =
4153 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
4154 }
4155 if ((i.types[op].bitfield.imm32)
4156 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
4157 == 0))
4158 {
4159 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
4160 ^ ((offsetT) 1 << 31))
4161 - ((offsetT) 1 << 31));
4162 }
4163 i.types[op]
4164 = operand_type_or (i.types[op],
4165 smallest_imm_type (i.op[op].imms->X_add_number));
4166
4167 /* We must avoid matching of Imm32 templates when 64bit
4168 only immediate is available. */
4169 if (guess_suffix == QWORD_MNEM_SUFFIX)
4170 i.types[op].bitfield.imm32 = 0;
4171 break;
4172
4173 case O_absent:
4174 case O_register:
4175 abort ();
4176
4177 /* Symbols and expressions. */
4178 default:
4179 /* Convert symbolic operand to proper sizes for matching, but don't
4180 prevent matching a set of insns that only supports sizes other
4181 than those matching the insn suffix. */
4182 {
4183 i386_operand_type mask, allowed;
4184 const insn_template *t;
4185
4186 operand_type_set (&mask, 0);
4187 operand_type_set (&allowed, 0);
4188
4189 for (t = current_templates->start;
4190 t < current_templates->end;
4191 ++t)
4192 allowed = operand_type_or (allowed,
4193 t->operand_types[op]);
4194 switch (guess_suffix)
4195 {
4196 case QWORD_MNEM_SUFFIX:
4197 mask.bitfield.imm64 = 1;
4198 mask.bitfield.imm32s = 1;
4199 break;
4200 case LONG_MNEM_SUFFIX:
4201 mask.bitfield.imm32 = 1;
4202 break;
4203 case WORD_MNEM_SUFFIX:
4204 mask.bitfield.imm16 = 1;
4205 break;
4206 case BYTE_MNEM_SUFFIX:
4207 mask.bitfield.imm8 = 1;
4208 break;
4209 default:
4210 break;
4211 }
4212 allowed = operand_type_and (mask, allowed);
4213 if (!operand_type_all_zero (&allowed))
4214 i.types[op] = operand_type_and (i.types[op], mask);
4215 }
4216 break;
4217 }
4218 }
4219 }
4220
4221 /* Try to use the smallest displacement type too. */
4222 static void
4223 optimize_disp (void)
4224 {
4225 int op;
4226
4227 for (op = i.operands; --op >= 0;)
4228 if (operand_type_check (i.types[op], disp))
4229 {
4230 if (i.op[op].disps->X_op == O_constant)
4231 {
4232 offsetT op_disp = i.op[op].disps->X_add_number;
4233
4234 if (i.types[op].bitfield.disp16
4235 && (op_disp & ~(offsetT) 0xffff) == 0)
4236 {
4237 /* If this operand is at most 16 bits, convert
4238 to a signed 16 bit number and don't use 64bit
4239 displacement. */
4240 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
4241 i.types[op].bitfield.disp64 = 0;
4242 }
4243 if (i.types[op].bitfield.disp32
4244 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
4245 {
4246 /* If this operand is at most 32 bits, convert
4247 to a signed 32 bit number and don't use 64bit
4248 displacement. */
4249 op_disp &= (((offsetT) 2 << 31) - 1);
4250 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
4251 i.types[op].bitfield.disp64 = 0;
4252 }
4253 if (!op_disp && i.types[op].bitfield.baseindex)
4254 {
4255 i.types[op].bitfield.disp8 = 0;
4256 i.types[op].bitfield.disp16 = 0;
4257 i.types[op].bitfield.disp32 = 0;
4258 i.types[op].bitfield.disp32s = 0;
4259 i.types[op].bitfield.disp64 = 0;
4260 i.op[op].disps = 0;
4261 i.disp_operands--;
4262 }
4263 else if (flag_code == CODE_64BIT)
4264 {
4265 if (fits_in_signed_long (op_disp))
4266 {
4267 i.types[op].bitfield.disp64 = 0;
4268 i.types[op].bitfield.disp32s = 1;
4269 }
4270 if (i.prefix[ADDR_PREFIX]
4271 && fits_in_unsigned_long (op_disp))
4272 i.types[op].bitfield.disp32 = 1;
4273 }
4274 if ((i.types[op].bitfield.disp32
4275 || i.types[op].bitfield.disp32s
4276 || i.types[op].bitfield.disp16)
4277 && fits_in_signed_byte (op_disp))
4278 i.types[op].bitfield.disp8 = 1;
4279 }
4280 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
4281 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
4282 {
4283 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
4284 i.op[op].disps, 0, i.reloc[op]);
4285 i.types[op].bitfield.disp8 = 0;
4286 i.types[op].bitfield.disp16 = 0;
4287 i.types[op].bitfield.disp32 = 0;
4288 i.types[op].bitfield.disp32s = 0;
4289 i.types[op].bitfield.disp64 = 0;
4290 }
4291 else
4292 /* We only support 64bit displacement on constants. */
4293 i.types[op].bitfield.disp64 = 0;
4294 }
4295 }
4296
4297 /* Check if operands are valid for the instruction. */
4298
4299 static int
4300 check_VecOperands (const insn_template *t)
4301 {
4302 unsigned int op;
4303
4304 /* Without VSIB byte, we can't have a vector register for index. */
4305 if (!t->opcode_modifier.vecsib
4306 && i.index_reg
4307 && (i.index_reg->reg_type.bitfield.regxmm
4308 || i.index_reg->reg_type.bitfield.regymm
4309 || i.index_reg->reg_type.bitfield.regzmm))
4310 {
4311 i.error = unsupported_vector_index_register;
4312 return 1;
4313 }
4314
4315 /* Check if default mask is allowed. */
4316 if (t->opcode_modifier.nodefmask
4317 && (!i.mask || i.mask->mask->reg_num == 0))
4318 {
4319 i.error = no_default_mask;
4320 return 1;
4321 }
4322
4323 /* For VSIB byte, we need a vector register for index, and all vector
4324 registers must be distinct. */
4325 if (t->opcode_modifier.vecsib)
4326 {
4327 if (!i.index_reg
4328 || !((t->opcode_modifier.vecsib == VecSIB128
4329 && i.index_reg->reg_type.bitfield.regxmm)
4330 || (t->opcode_modifier.vecsib == VecSIB256
4331 && i.index_reg->reg_type.bitfield.regymm)
4332 || (t->opcode_modifier.vecsib == VecSIB512
4333 && i.index_reg->reg_type.bitfield.regzmm)))
4334 {
4335 i.error = invalid_vsib_address;
4336 return 1;
4337 }
4338
4339 gas_assert (i.reg_operands == 2 || i.mask);
4340 if (i.reg_operands == 2 && !i.mask)
4341 {
4342 gas_assert (i.types[0].bitfield.regxmm
4343 || i.types[0].bitfield.regymm);
4344 gas_assert (i.types[2].bitfield.regxmm
4345 || i.types[2].bitfield.regymm);
4346 if (operand_check == check_none)
4347 return 0;
4348 if (register_number (i.op[0].regs)
4349 != register_number (i.index_reg)
4350 && register_number (i.op[2].regs)
4351 != register_number (i.index_reg)
4352 && register_number (i.op[0].regs)
4353 != register_number (i.op[2].regs))
4354 return 0;
4355 if (operand_check == check_error)
4356 {
4357 i.error = invalid_vector_register_set;
4358 return 1;
4359 }
4360 as_warn (_("mask, index, and destination registers should be distinct"));
4361 }
4362 else if (i.reg_operands == 1 && i.mask)
4363 {
4364 if ((i.types[1].bitfield.regymm
4365 || i.types[1].bitfield.regzmm)
4366 && (register_number (i.op[1].regs)
4367 == register_number (i.index_reg)))
4368 {
4369 if (operand_check == check_error)
4370 {
4371 i.error = invalid_vector_register_set;
4372 return 1;
4373 }
4374 if (operand_check != check_none)
4375 as_warn (_("index and destination registers should be distinct"));
4376 }
4377 }
4378 }
4379
4380 /* Check if broadcast is supported by the instruction and is applied
4381 to the memory operand. */
4382 if (i.broadcast)
4383 {
4384 int broadcasted_opnd_size;
4385
4386 /* Check if specified broadcast is supported in this instruction,
4387 and it's applied to memory operand of DWORD or QWORD type,
4388 depending on VecESize. */
4389 if (i.broadcast->type != t->opcode_modifier.broadcast
4390 || !i.types[i.broadcast->operand].bitfield.mem
4391 || (t->opcode_modifier.vecesize == 0
4392 && !i.types[i.broadcast->operand].bitfield.dword
4393 && !i.types[i.broadcast->operand].bitfield.unspecified)
4394 || (t->opcode_modifier.vecesize == 1
4395 && !i.types[i.broadcast->operand].bitfield.qword
4396 && !i.types[i.broadcast->operand].bitfield.unspecified))
4397 goto bad_broadcast;
4398
4399 broadcasted_opnd_size = t->opcode_modifier.vecesize ? 64 : 32;
4400 if (i.broadcast->type == BROADCAST_1TO16)
4401 broadcasted_opnd_size <<= 4; /* Broadcast 1to16. */
4402 else if (i.broadcast->type == BROADCAST_1TO8)
4403 broadcasted_opnd_size <<= 3; /* Broadcast 1to8. */
4404 else if (i.broadcast->type == BROADCAST_1TO4)
4405 broadcasted_opnd_size <<= 2; /* Broadcast 1to4. */
4406 else if (i.broadcast->type == BROADCAST_1TO2)
4407 broadcasted_opnd_size <<= 1; /* Broadcast 1to2. */
4408 else
4409 goto bad_broadcast;
4410
4411 if ((broadcasted_opnd_size == 256
4412 && !t->operand_types[i.broadcast->operand].bitfield.ymmword)
4413 || (broadcasted_opnd_size == 512
4414 && !t->operand_types[i.broadcast->operand].bitfield.zmmword))
4415 {
4416 bad_broadcast:
4417 i.error = unsupported_broadcast;
4418 return 1;
4419 }
4420 }
4421 /* If broadcast is supported in this instruction, we need to check if
4422 operand of one-element size isn't specified without broadcast. */
4423 else if (t->opcode_modifier.broadcast && i.mem_operands)
4424 {
4425 /* Find memory operand. */
4426 for (op = 0; op < i.operands; op++)
4427 if (operand_type_check (i.types[op], anymem))
4428 break;
4429 gas_assert (op < i.operands);
4430 /* Check size of the memory operand. */
4431 if ((t->opcode_modifier.vecesize == 0
4432 && i.types[op].bitfield.dword)
4433 || (t->opcode_modifier.vecesize == 1
4434 && i.types[op].bitfield.qword))
4435 {
4436 i.error = broadcast_needed;
4437 return 1;
4438 }
4439 }
4440
4441 /* Check if requested masking is supported. */
4442 if (i.mask
4443 && (!t->opcode_modifier.masking
4444 || (i.mask->zeroing
4445 && t->opcode_modifier.masking == MERGING_MASKING)))
4446 {
4447 i.error = unsupported_masking;
4448 return 1;
4449 }
4450
4451 /* Check if masking is applied to dest operand. */
4452 if (i.mask && (i.mask->operand != (int) (i.operands - 1)))
4453 {
4454 i.error = mask_not_on_destination;
4455 return 1;
4456 }
4457
4458 /* Check RC/SAE. */
4459 if (i.rounding)
4460 {
4461 if ((i.rounding->type != saeonly
4462 && !t->opcode_modifier.staticrounding)
4463 || (i.rounding->type == saeonly
4464 && (t->opcode_modifier.staticrounding
4465 || !t->opcode_modifier.sae)))
4466 {
4467 i.error = unsupported_rc_sae;
4468 return 1;
4469 }
4470 /* If the instruction has several immediate operands and one of
4471 them is rounding, the rounding operand should be the last
4472 immediate operand. */
4473 if (i.imm_operands > 1
4474 && i.rounding->operand != (int) (i.imm_operands - 1))
4475 {
4476 i.error = rc_sae_operand_not_last_imm;
4477 return 1;
4478 }
4479 }
4480
4481 /* Check vector Disp8 operand. */
4482 if (t->opcode_modifier.disp8memshift)
4483 {
4484 if (i.broadcast)
4485 i.memshift = t->opcode_modifier.vecesize ? 3 : 2;
4486 else
4487 i.memshift = t->opcode_modifier.disp8memshift;
4488
4489 for (op = 0; op < i.operands; op++)
4490 if (operand_type_check (i.types[op], disp)
4491 && i.op[op].disps->X_op == O_constant)
4492 {
4493 offsetT value = i.op[op].disps->X_add_number;
4494 int vec_disp8_ok = fits_in_vec_disp8 (value);
4495 if (t->operand_types [op].bitfield.vec_disp8)
4496 {
4497 if (vec_disp8_ok)
4498 i.types[op].bitfield.vec_disp8 = 1;
4499 else
4500 {
4501 /* Vector insn can only have Vec_Disp8/Disp32 in
4502 32/64bit modes, and Vec_Disp8/Disp16 in 16bit
4503 mode. */
4504 i.types[op].bitfield.disp8 = 0;
4505 if (flag_code != CODE_16BIT)
4506 i.types[op].bitfield.disp16 = 0;
4507 }
4508 }
4509 else if (flag_code != CODE_16BIT)
4510 {
4511 /* One form of this instruction supports vector Disp8.
4512 Try vector Disp8 if we need to use Disp32. */
4513 if (vec_disp8_ok && !fits_in_signed_byte (value))
4514 {
4515 i.error = try_vector_disp8;
4516 return 1;
4517 }
4518 }
4519 }
4520 }
4521 else
4522 i.memshift = -1;
4523
4524 return 0;
4525 }
4526
4527 /* Check if operands are valid for the instruction. Update VEX
4528 operand types. */
4529
4530 static int
4531 VEX_check_operands (const insn_template *t)
4532 {
4533 /* VREX is only valid with EVEX prefix. */
4534 if (i.need_vrex && !t->opcode_modifier.evex)
4535 {
4536 i.error = invalid_register_operand;
4537 return 1;
4538 }
4539
4540 if (!t->opcode_modifier.vex)
4541 return 0;
4542
4543 /* Only check VEX_Imm4, which must be the first operand. */
4544 if (t->operand_types[0].bitfield.vec_imm4)
4545 {
4546 if (i.op[0].imms->X_op != O_constant
4547 || !fits_in_imm4 (i.op[0].imms->X_add_number))
4548 {
4549 i.error = bad_imm4;
4550 return 1;
4551 }
4552
4553 /* Turn off Imm8 so that update_imm won't complain. */
4554 i.types[0] = vec_imm4;
4555 }
4556
4557 return 0;
4558 }
4559
4560 static const insn_template *
4561 match_template (void)
4562 {
4563 /* Points to template once we've found it. */
4564 const insn_template *t;
4565 i386_operand_type overlap0, overlap1, overlap2, overlap3;
4566 i386_operand_type overlap4;
4567 unsigned int found_reverse_match;
4568 i386_opcode_modifier suffix_check;
4569 i386_operand_type operand_types [MAX_OPERANDS];
4570 int addr_prefix_disp;
4571 unsigned int j;
4572 unsigned int found_cpu_match;
4573 unsigned int check_register;
4574 enum i386_error specific_error = 0;
4575
4576 #if MAX_OPERANDS != 5
4577 # error "MAX_OPERANDS must be 5."
4578 #endif
4579
4580 found_reverse_match = 0;
4581 addr_prefix_disp = -1;
4582
4583 memset (&suffix_check, 0, sizeof (suffix_check));
4584 if (i.suffix == BYTE_MNEM_SUFFIX)
4585 suffix_check.no_bsuf = 1;
4586 else if (i.suffix == WORD_MNEM_SUFFIX)
4587 suffix_check.no_wsuf = 1;
4588 else if (i.suffix == SHORT_MNEM_SUFFIX)
4589 suffix_check.no_ssuf = 1;
4590 else if (i.suffix == LONG_MNEM_SUFFIX)
4591 suffix_check.no_lsuf = 1;
4592 else if (i.suffix == QWORD_MNEM_SUFFIX)
4593 suffix_check.no_qsuf = 1;
4594 else if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
4595 suffix_check.no_ldsuf = 1;
4596
4597 /* Must have right number of operands. */
4598 i.error = number_of_operands_mismatch;
4599
4600 for (t = current_templates->start; t < current_templates->end; t++)
4601 {
4602 addr_prefix_disp = -1;
4603
4604 if (i.operands != t->operands)
4605 continue;
4606
4607 /* Check processor support. */
4608 i.error = unsupported;
4609 found_cpu_match = (cpu_flags_match (t)
4610 == CPU_FLAGS_PERFECT_MATCH);
4611 if (!found_cpu_match)
4612 continue;
4613
4614 /* Check old gcc support. */
4615 i.error = old_gcc_only;
4616 if (!old_gcc && t->opcode_modifier.oldgcc)
4617 continue;
4618
4619 /* Check AT&T mnemonic. */
4620 i.error = unsupported_with_intel_mnemonic;
4621 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
4622 continue;
4623
4624 /* Check AT&T/Intel syntax. */
4625 i.error = unsupported_syntax;
4626 if ((intel_syntax && t->opcode_modifier.attsyntax)
4627 || (!intel_syntax && t->opcode_modifier.intelsyntax))
4628 continue;
4629
4630 /* Check the suffix, except for some instructions in intel mode. */
4631 i.error = invalid_instruction_suffix;
4632 if ((!intel_syntax || !t->opcode_modifier.ignoresize)
4633 && ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
4634 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
4635 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
4636 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
4637 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
4638 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf)))
4639 continue;
4640
4641 if (!operand_size_match (t))
4642 continue;
4643
4644 for (j = 0; j < MAX_OPERANDS; j++)
4645 operand_types[j] = t->operand_types[j];
4646
4647 /* In general, don't allow 64-bit operands in 32-bit mode. */
4648 if (i.suffix == QWORD_MNEM_SUFFIX
4649 && flag_code != CODE_64BIT
4650 && (intel_syntax
4651 ? (!t->opcode_modifier.ignoresize
4652 && !intel_float_operand (t->name))
4653 : intel_float_operand (t->name) != 2)
4654 && ((!operand_types[0].bitfield.regmmx
4655 && !operand_types[0].bitfield.regxmm
4656 && !operand_types[0].bitfield.regymm
4657 && !operand_types[0].bitfield.regzmm)
4658 || (!operand_types[t->operands > 1].bitfield.regmmx
4659 && operand_types[t->operands > 1].bitfield.regxmm
4660 && operand_types[t->operands > 1].bitfield.regymm
4661 && operand_types[t->operands > 1].bitfield.regzmm))
4662 && (t->base_opcode != 0x0fc7
4663 || t->extension_opcode != 1 /* cmpxchg8b */))
4664 continue;
4665
4666 /* In general, don't allow 32-bit operands on pre-386. */
4667 else if (i.suffix == LONG_MNEM_SUFFIX
4668 && !cpu_arch_flags.bitfield.cpui386
4669 && (intel_syntax
4670 ? (!t->opcode_modifier.ignoresize
4671 && !intel_float_operand (t->name))
4672 : intel_float_operand (t->name) != 2)
4673 && ((!operand_types[0].bitfield.regmmx
4674 && !operand_types[0].bitfield.regxmm)
4675 || (!operand_types[t->operands > 1].bitfield.regmmx
4676 && operand_types[t->operands > 1].bitfield.regxmm)))
4677 continue;
4678
4679 /* Do not verify operands when there are none. */
4680 else
4681 {
4682 if (!t->operands)
4683 /* We've found a match; break out of loop. */
4684 break;
4685 }
4686
4687 /* Address size prefix will turn Disp64/Disp32/Disp16 operand
4688 into Disp32/Disp16/Disp32 operand. */
4689 if (i.prefix[ADDR_PREFIX] != 0)
4690 {
4691 /* There should be only one Disp operand. */
4692 switch (flag_code)
4693 {
4694 case CODE_16BIT:
4695 for (j = 0; j < MAX_OPERANDS; j++)
4696 {
4697 if (operand_types[j].bitfield.disp16)
4698 {
4699 addr_prefix_disp = j;
4700 operand_types[j].bitfield.disp32 = 1;
4701 operand_types[j].bitfield.disp16 = 0;
4702 break;
4703 }
4704 }
4705 break;
4706 case CODE_32BIT:
4707 for (j = 0; j < MAX_OPERANDS; j++)
4708 {
4709 if (operand_types[j].bitfield.disp32)
4710 {
4711 addr_prefix_disp = j;
4712 operand_types[j].bitfield.disp32 = 0;
4713 operand_types[j].bitfield.disp16 = 1;
4714 break;
4715 }
4716 }
4717 break;
4718 case CODE_64BIT:
4719 for (j = 0; j < MAX_OPERANDS; j++)
4720 {
4721 if (operand_types[j].bitfield.disp64)
4722 {
4723 addr_prefix_disp = j;
4724 operand_types[j].bitfield.disp64 = 0;
4725 operand_types[j].bitfield.disp32 = 1;
4726 break;
4727 }
4728 }
4729 break;
4730 }
4731 }
4732
4733 /* We check register size if needed. */
4734 check_register = t->opcode_modifier.checkregsize;
4735 overlap0 = operand_type_and (i.types[0], operand_types[0]);
4736 switch (t->operands)
4737 {
4738 case 1:
4739 if (!operand_type_match (overlap0, i.types[0]))
4740 continue;
4741 break;
4742 case 2:
4743 /* xchg %eax, %eax is a special case. It is an aliase for nop
4744 only in 32bit mode and we can use opcode 0x90. In 64bit
4745 mode, we can't use 0x90 for xchg %eax, %eax since it should
4746 zero-extend %eax to %rax. */
4747 if (flag_code == CODE_64BIT
4748 && t->base_opcode == 0x90
4749 && operand_type_equal (&i.types [0], &acc32)
4750 && operand_type_equal (&i.types [1], &acc32))
4751 continue;
4752 if (i.swap_operand)
4753 {
4754 /* If we swap operand in encoding, we either match
4755 the next one or reverse direction of operands. */
4756 if (t->opcode_modifier.s)
4757 continue;
4758 else if (t->opcode_modifier.d)
4759 goto check_reverse;
4760 }
4761
4762 case 3:
4763 /* If we swap operand in encoding, we match the next one. */
4764 if (i.swap_operand && t->opcode_modifier.s)
4765 continue;
4766 case 4:
4767 case 5:
4768 overlap1 = operand_type_and (i.types[1], operand_types[1]);
4769 if (!operand_type_match (overlap0, i.types[0])
4770 || !operand_type_match (overlap1, i.types[1])
4771 || (check_register
4772 && !operand_type_register_match (overlap0, i.types[0],
4773 operand_types[0],
4774 overlap1, i.types[1],
4775 operand_types[1])))
4776 {
4777 /* Check if other direction is valid ... */
4778 if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
4779 continue;
4780
4781 check_reverse:
4782 /* Try reversing direction of operands. */
4783 overlap0 = operand_type_and (i.types[0], operand_types[1]);
4784 overlap1 = operand_type_and (i.types[1], operand_types[0]);
4785 if (!operand_type_match (overlap0, i.types[0])
4786 || !operand_type_match (overlap1, i.types[1])
4787 || (check_register
4788 && !operand_type_register_match (overlap0,
4789 i.types[0],
4790 operand_types[1],
4791 overlap1,
4792 i.types[1],
4793 operand_types[0])))
4794 {
4795 /* Does not match either direction. */
4796 continue;
4797 }
4798 /* found_reverse_match holds which of D or FloatDR
4799 we've found. */
4800 if (t->opcode_modifier.d)
4801 found_reverse_match = Opcode_D;
4802 else if (t->opcode_modifier.floatd)
4803 found_reverse_match = Opcode_FloatD;
4804 else
4805 found_reverse_match = 0;
4806 if (t->opcode_modifier.floatr)
4807 found_reverse_match |= Opcode_FloatR;
4808 }
4809 else
4810 {
4811 /* Found a forward 2 operand match here. */
4812 switch (t->operands)
4813 {
4814 case 5:
4815 overlap4 = operand_type_and (i.types[4],
4816 operand_types[4]);
4817 case 4:
4818 overlap3 = operand_type_and (i.types[3],
4819 operand_types[3]);
4820 case 3:
4821 overlap2 = operand_type_and (i.types[2],
4822 operand_types[2]);
4823 break;
4824 }
4825
4826 switch (t->operands)
4827 {
4828 case 5:
4829 if (!operand_type_match (overlap4, i.types[4])
4830 || !operand_type_register_match (overlap3,
4831 i.types[3],
4832 operand_types[3],
4833 overlap4,
4834 i.types[4],
4835 operand_types[4]))
4836 continue;
4837 case 4:
4838 if (!operand_type_match (overlap3, i.types[3])
4839 || (check_register
4840 && !operand_type_register_match (overlap2,
4841 i.types[2],
4842 operand_types[2],
4843 overlap3,
4844 i.types[3],
4845 operand_types[3])))
4846 continue;
4847 case 3:
4848 /* Here we make use of the fact that there are no
4849 reverse match 3 operand instructions, and all 3
4850 operand instructions only need to be checked for
4851 register consistency between operands 2 and 3. */
4852 if (!operand_type_match (overlap2, i.types[2])
4853 || (check_register
4854 && !operand_type_register_match (overlap1,
4855 i.types[1],
4856 operand_types[1],
4857 overlap2,
4858 i.types[2],
4859 operand_types[2])))
4860 continue;
4861 break;
4862 }
4863 }
4864 /* Found either forward/reverse 2, 3 or 4 operand match here:
4865 slip through to break. */
4866 }
4867 if (!found_cpu_match)
4868 {
4869 found_reverse_match = 0;
4870 continue;
4871 }
4872
4873 /* Check if vector and VEX operands are valid. */
4874 if (check_VecOperands (t) || VEX_check_operands (t))
4875 {
4876 specific_error = i.error;
4877 continue;
4878 }
4879
4880 /* We've found a match; break out of loop. */
4881 break;
4882 }
4883
4884 if (t == current_templates->end)
4885 {
4886 /* We found no match. */
4887 const char *err_msg;
4888 switch (specific_error ? specific_error : i.error)
4889 {
4890 default:
4891 abort ();
4892 case operand_size_mismatch:
4893 err_msg = _("operand size mismatch");
4894 break;
4895 case operand_type_mismatch:
4896 err_msg = _("operand type mismatch");
4897 break;
4898 case register_type_mismatch:
4899 err_msg = _("register type mismatch");
4900 break;
4901 case number_of_operands_mismatch:
4902 err_msg = _("number of operands mismatch");
4903 break;
4904 case invalid_instruction_suffix:
4905 err_msg = _("invalid instruction suffix");
4906 break;
4907 case bad_imm4:
4908 err_msg = _("constant doesn't fit in 4 bits");
4909 break;
4910 case old_gcc_only:
4911 err_msg = _("only supported with old gcc");
4912 break;
4913 case unsupported_with_intel_mnemonic:
4914 err_msg = _("unsupported with Intel mnemonic");
4915 break;
4916 case unsupported_syntax:
4917 err_msg = _("unsupported syntax");
4918 break;
4919 case unsupported:
4920 as_bad (_("unsupported instruction `%s'"),
4921 current_templates->start->name);
4922 return NULL;
4923 case invalid_vsib_address:
4924 err_msg = _("invalid VSIB address");
4925 break;
4926 case invalid_vector_register_set:
4927 err_msg = _("mask, index, and destination registers must be distinct");
4928 break;
4929 case unsupported_vector_index_register:
4930 err_msg = _("unsupported vector index register");
4931 break;
4932 case unsupported_broadcast:
4933 err_msg = _("unsupported broadcast");
4934 break;
4935 case broadcast_not_on_src_operand:
4936 err_msg = _("broadcast not on source memory operand");
4937 break;
4938 case broadcast_needed:
4939 err_msg = _("broadcast is needed for operand of such type");
4940 break;
4941 case unsupported_masking:
4942 err_msg = _("unsupported masking");
4943 break;
4944 case mask_not_on_destination:
4945 err_msg = _("mask not on destination operand");
4946 break;
4947 case no_default_mask:
4948 err_msg = _("default mask isn't allowed");
4949 break;
4950 case unsupported_rc_sae:
4951 err_msg = _("unsupported static rounding/sae");
4952 break;
4953 case rc_sae_operand_not_last_imm:
4954 if (intel_syntax)
4955 err_msg = _("RC/SAE operand must precede immediate operands");
4956 else
4957 err_msg = _("RC/SAE operand must follow immediate operands");
4958 break;
4959 case invalid_register_operand:
4960 err_msg = _("invalid register operand");
4961 break;
4962 }
4963 as_bad (_("%s for `%s'"), err_msg,
4964 current_templates->start->name);
4965 return NULL;
4966 }
4967
4968 if (!quiet_warnings)
4969 {
4970 if (!intel_syntax
4971 && (i.types[0].bitfield.jumpabsolute
4972 != operand_types[0].bitfield.jumpabsolute))
4973 {
4974 as_warn (_("indirect %s without `*'"), t->name);
4975 }
4976
4977 if (t->opcode_modifier.isprefix
4978 && t->opcode_modifier.ignoresize)
4979 {
4980 /* Warn them that a data or address size prefix doesn't
4981 affect assembly of the next line of code. */
4982 as_warn (_("stand-alone `%s' prefix"), t->name);
4983 }
4984 }
4985
4986 /* Copy the template we found. */
4987 i.tm = *t;
4988
4989 if (addr_prefix_disp != -1)
4990 i.tm.operand_types[addr_prefix_disp]
4991 = operand_types[addr_prefix_disp];
4992
4993 if (found_reverse_match)
4994 {
4995 /* If we found a reverse match we must alter the opcode
4996 direction bit. found_reverse_match holds bits to change
4997 (different for int & float insns). */
4998
4999 i.tm.base_opcode ^= found_reverse_match;
5000
5001 i.tm.operand_types[0] = operand_types[1];
5002 i.tm.operand_types[1] = operand_types[0];
5003 }
5004
5005 return t;
5006 }
5007
5008 static int
5009 check_string (void)
5010 {
5011 int mem_op = operand_type_check (i.types[0], anymem) ? 0 : 1;
5012 if (i.tm.operand_types[mem_op].bitfield.esseg)
5013 {
5014 if (i.seg[0] != NULL && i.seg[0] != &es)
5015 {
5016 as_bad (_("`%s' operand %d must use `%ses' segment"),
5017 i.tm.name,
5018 mem_op + 1,
5019 register_prefix);
5020 return 0;
5021 }
5022 /* There's only ever one segment override allowed per instruction.
5023 This instruction possibly has a legal segment override on the
5024 second operand, so copy the segment to where non-string
5025 instructions store it, allowing common code. */
5026 i.seg[0] = i.seg[1];
5027 }
5028 else if (i.tm.operand_types[mem_op + 1].bitfield.esseg)
5029 {
5030 if (i.seg[1] != NULL && i.seg[1] != &es)
5031 {
5032 as_bad (_("`%s' operand %d must use `%ses' segment"),
5033 i.tm.name,
5034 mem_op + 2,
5035 register_prefix);
5036 return 0;
5037 }
5038 }
5039 return 1;
5040 }
5041
5042 static int
5043 process_suffix (void)
5044 {
5045 /* If matched instruction specifies an explicit instruction mnemonic
5046 suffix, use it. */
5047 if (i.tm.opcode_modifier.size16)
5048 i.suffix = WORD_MNEM_SUFFIX;
5049 else if (i.tm.opcode_modifier.size32)
5050 i.suffix = LONG_MNEM_SUFFIX;
5051 else if (i.tm.opcode_modifier.size64)
5052 i.suffix = QWORD_MNEM_SUFFIX;
5053 else if (i.reg_operands)
5054 {
5055 /* If there's no instruction mnemonic suffix we try to invent one
5056 based on register operands. */
5057 if (!i.suffix)
5058 {
5059 /* We take i.suffix from the last register operand specified,
5060 Destination register type is more significant than source
5061 register type. crc32 in SSE4.2 prefers source register
5062 type. */
5063 if (i.tm.base_opcode == 0xf20f38f1)
5064 {
5065 if (i.types[0].bitfield.reg16)
5066 i.suffix = WORD_MNEM_SUFFIX;
5067 else if (i.types[0].bitfield.reg32)
5068 i.suffix = LONG_MNEM_SUFFIX;
5069 else if (i.types[0].bitfield.reg64)
5070 i.suffix = QWORD_MNEM_SUFFIX;
5071 }
5072 else if (i.tm.base_opcode == 0xf20f38f0)
5073 {
5074 if (i.types[0].bitfield.reg8)
5075 i.suffix = BYTE_MNEM_SUFFIX;
5076 }
5077
5078 if (!i.suffix)
5079 {
5080 int op;
5081
5082 if (i.tm.base_opcode == 0xf20f38f1
5083 || i.tm.base_opcode == 0xf20f38f0)
5084 {
5085 /* We have to know the operand size for crc32. */
5086 as_bad (_("ambiguous memory operand size for `%s`"),
5087 i.tm.name);
5088 return 0;
5089 }
5090
5091 for (op = i.operands; --op >= 0;)
5092 if (!i.tm.operand_types[op].bitfield.inoutportreg)
5093 {
5094 if (i.types[op].bitfield.reg8)
5095 {
5096 i.suffix = BYTE_MNEM_SUFFIX;
5097 break;
5098 }
5099 else if (i.types[op].bitfield.reg16)
5100 {
5101 i.suffix = WORD_MNEM_SUFFIX;
5102 break;
5103 }
5104 else if (i.types[op].bitfield.reg32)
5105 {
5106 i.suffix = LONG_MNEM_SUFFIX;
5107 break;
5108 }
5109 else if (i.types[op].bitfield.reg64)
5110 {
5111 i.suffix = QWORD_MNEM_SUFFIX;
5112 break;
5113 }
5114 }
5115 }
5116 }
5117 else if (i.suffix == BYTE_MNEM_SUFFIX)
5118 {
5119 if (intel_syntax
5120 && i.tm.opcode_modifier.ignoresize
5121 && i.tm.opcode_modifier.no_bsuf)
5122 i.suffix = 0;
5123 else if (!check_byte_reg ())
5124 return 0;
5125 }
5126 else if (i.suffix == LONG_MNEM_SUFFIX)
5127 {
5128 if (intel_syntax
5129 && i.tm.opcode_modifier.ignoresize
5130 && i.tm.opcode_modifier.no_lsuf)
5131 i.suffix = 0;
5132 else if (!check_long_reg ())
5133 return 0;
5134 }
5135 else if (i.suffix == QWORD_MNEM_SUFFIX)
5136 {
5137 if (intel_syntax
5138 && i.tm.opcode_modifier.ignoresize
5139 && i.tm.opcode_modifier.no_qsuf)
5140 i.suffix = 0;
5141 else if (!check_qword_reg ())
5142 return 0;
5143 }
5144 else if (i.suffix == WORD_MNEM_SUFFIX)
5145 {
5146 if (intel_syntax
5147 && i.tm.opcode_modifier.ignoresize
5148 && i.tm.opcode_modifier.no_wsuf)
5149 i.suffix = 0;
5150 else if (!check_word_reg ())
5151 return 0;
5152 }
5153 else if (i.suffix == XMMWORD_MNEM_SUFFIX
5154 || i.suffix == YMMWORD_MNEM_SUFFIX
5155 || i.suffix == ZMMWORD_MNEM_SUFFIX)
5156 {
5157 /* Skip if the instruction has x/y/z suffix. match_template
5158 should check if it is a valid suffix. */
5159 }
5160 else if (intel_syntax && i.tm.opcode_modifier.ignoresize)
5161 /* Do nothing if the instruction is going to ignore the prefix. */
5162 ;
5163 else
5164 abort ();
5165 }
5166 else if (i.tm.opcode_modifier.defaultsize
5167 && !i.suffix
5168 /* exclude fldenv/frstor/fsave/fstenv */
5169 && i.tm.opcode_modifier.no_ssuf)
5170 {
5171 i.suffix = stackop_size;
5172 }
5173 else if (intel_syntax
5174 && !i.suffix
5175 && (i.tm.operand_types[0].bitfield.jumpabsolute
5176 || i.tm.opcode_modifier.jumpbyte
5177 || i.tm.opcode_modifier.jumpintersegment
5178 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
5179 && i.tm.extension_opcode <= 3)))
5180 {
5181 switch (flag_code)
5182 {
5183 case CODE_64BIT:
5184 if (!i.tm.opcode_modifier.no_qsuf)
5185 {
5186 i.suffix = QWORD_MNEM_SUFFIX;
5187 break;
5188 }
5189 case CODE_32BIT:
5190 if (!i.tm.opcode_modifier.no_lsuf)
5191 i.suffix = LONG_MNEM_SUFFIX;
5192 break;
5193 case CODE_16BIT:
5194 if (!i.tm.opcode_modifier.no_wsuf)
5195 i.suffix = WORD_MNEM_SUFFIX;
5196 break;
5197 }
5198 }
5199
5200 if (!i.suffix)
5201 {
5202 if (!intel_syntax)
5203 {
5204 if (i.tm.opcode_modifier.w)
5205 {
5206 as_bad (_("no instruction mnemonic suffix given and "
5207 "no register operands; can't size instruction"));
5208 return 0;
5209 }
5210 }
5211 else
5212 {
5213 unsigned int suffixes;
5214
5215 suffixes = !i.tm.opcode_modifier.no_bsuf;
5216 if (!i.tm.opcode_modifier.no_wsuf)
5217 suffixes |= 1 << 1;
5218 if (!i.tm.opcode_modifier.no_lsuf)
5219 suffixes |= 1 << 2;
5220 if (!i.tm.opcode_modifier.no_ldsuf)
5221 suffixes |= 1 << 3;
5222 if (!i.tm.opcode_modifier.no_ssuf)
5223 suffixes |= 1 << 4;
5224 if (!i.tm.opcode_modifier.no_qsuf)
5225 suffixes |= 1 << 5;
5226
5227 /* There are more than suffix matches. */
5228 if (i.tm.opcode_modifier.w
5229 || ((suffixes & (suffixes - 1))
5230 && !i.tm.opcode_modifier.defaultsize
5231 && !i.tm.opcode_modifier.ignoresize))
5232 {
5233 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
5234 return 0;
5235 }
5236 }
5237 }
5238
5239 /* Change the opcode based on the operand size given by i.suffix;
5240 We don't need to change things for byte insns. */
5241
5242 if (i.suffix
5243 && i.suffix != BYTE_MNEM_SUFFIX
5244 && i.suffix != XMMWORD_MNEM_SUFFIX
5245 && i.suffix != YMMWORD_MNEM_SUFFIX
5246 && i.suffix != ZMMWORD_MNEM_SUFFIX)
5247 {
5248 /* It's not a byte, select word/dword operation. */
5249 if (i.tm.opcode_modifier.w)
5250 {
5251 if (i.tm.opcode_modifier.shortform)
5252 i.tm.base_opcode |= 8;
5253 else
5254 i.tm.base_opcode |= 1;
5255 }
5256
5257 /* Now select between word & dword operations via the operand
5258 size prefix, except for instructions that will ignore this
5259 prefix anyway. */
5260 if (i.tm.opcode_modifier.addrprefixop0)
5261 {
5262 /* The address size override prefix changes the size of the
5263 first operand. */
5264 if ((flag_code == CODE_32BIT
5265 && i.op->regs[0].reg_type.bitfield.reg16)
5266 || (flag_code != CODE_32BIT
5267 && i.op->regs[0].reg_type.bitfield.reg32))
5268 if (!add_prefix (ADDR_PREFIX_OPCODE))
5269 return 0;
5270 }
5271 else if (i.suffix != QWORD_MNEM_SUFFIX
5272 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
5273 && !i.tm.opcode_modifier.ignoresize
5274 && !i.tm.opcode_modifier.floatmf
5275 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
5276 || (flag_code == CODE_64BIT
5277 && i.tm.opcode_modifier.jumpbyte)))
5278 {
5279 unsigned int prefix = DATA_PREFIX_OPCODE;
5280
5281 if (i.tm.opcode_modifier.jumpbyte) /* jcxz, loop */
5282 prefix = ADDR_PREFIX_OPCODE;
5283
5284 if (!add_prefix (prefix))
5285 return 0;
5286 }
5287
5288 /* Set mode64 for an operand. */
5289 if (i.suffix == QWORD_MNEM_SUFFIX
5290 && flag_code == CODE_64BIT
5291 && !i.tm.opcode_modifier.norex64)
5292 {
5293 /* Special case for xchg %rax,%rax. It is NOP and doesn't
5294 need rex64. cmpxchg8b is also a special case. */
5295 if (! (i.operands == 2
5296 && i.tm.base_opcode == 0x90
5297 && i.tm.extension_opcode == None
5298 && operand_type_equal (&i.types [0], &acc64)
5299 && operand_type_equal (&i.types [1], &acc64))
5300 && ! (i.operands == 1
5301 && i.tm.base_opcode == 0xfc7
5302 && i.tm.extension_opcode == 1
5303 && !operand_type_check (i.types [0], reg)
5304 && operand_type_check (i.types [0], anymem)))
5305 i.rex |= REX_W;
5306 }
5307
5308 /* Size floating point instruction. */
5309 if (i.suffix == LONG_MNEM_SUFFIX)
5310 if (i.tm.opcode_modifier.floatmf)
5311 i.tm.base_opcode ^= 4;
5312 }
5313
5314 return 1;
5315 }
5316
5317 static int
5318 check_byte_reg (void)
5319 {
5320 int op;
5321
5322 for (op = i.operands; --op >= 0;)
5323 {
5324 /* If this is an eight bit register, it's OK. If it's the 16 or
5325 32 bit version of an eight bit register, we will just use the
5326 low portion, and that's OK too. */
5327 if (i.types[op].bitfield.reg8)
5328 continue;
5329
5330 /* I/O port address operands are OK too. */
5331 if (i.tm.operand_types[op].bitfield.inoutportreg)
5332 continue;
5333
5334 /* crc32 doesn't generate this warning. */
5335 if (i.tm.base_opcode == 0xf20f38f0)
5336 continue;
5337
5338 if ((i.types[op].bitfield.reg16
5339 || i.types[op].bitfield.reg32
5340 || i.types[op].bitfield.reg64)
5341 && i.op[op].regs->reg_num < 4
5342 /* Prohibit these changes in 64bit mode, since the lowering
5343 would be more complicated. */
5344 && flag_code != CODE_64BIT)
5345 {
5346 #if REGISTER_WARNINGS
5347 if (!quiet_warnings)
5348 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
5349 register_prefix,
5350 (i.op[op].regs + (i.types[op].bitfield.reg16
5351 ? REGNAM_AL - REGNAM_AX
5352 : REGNAM_AL - REGNAM_EAX))->reg_name,
5353 register_prefix,
5354 i.op[op].regs->reg_name,
5355 i.suffix);
5356 #endif
5357 continue;
5358 }
5359 /* Any other register is bad. */
5360 if (i.types[op].bitfield.reg16
5361 || i.types[op].bitfield.reg32
5362 || i.types[op].bitfield.reg64
5363 || i.types[op].bitfield.regmmx
5364 || i.types[op].bitfield.regxmm
5365 || i.types[op].bitfield.regymm
5366 || i.types[op].bitfield.regzmm
5367 || i.types[op].bitfield.sreg2
5368 || i.types[op].bitfield.sreg3
5369 || i.types[op].bitfield.control
5370 || i.types[op].bitfield.debug
5371 || i.types[op].bitfield.test
5372 || i.types[op].bitfield.floatreg
5373 || i.types[op].bitfield.floatacc)
5374 {
5375 as_bad (_("`%s%s' not allowed with `%s%c'"),
5376 register_prefix,
5377 i.op[op].regs->reg_name,
5378 i.tm.name,
5379 i.suffix);
5380 return 0;
5381 }
5382 }
5383 return 1;
5384 }
5385
5386 static int
5387 check_long_reg (void)
5388 {
5389 int op;
5390
5391 for (op = i.operands; --op >= 0;)
5392 /* Reject eight bit registers, except where the template requires
5393 them. (eg. movzb) */
5394 if (i.types[op].bitfield.reg8
5395 && (i.tm.operand_types[op].bitfield.reg16
5396 || i.tm.operand_types[op].bitfield.reg32
5397 || i.tm.operand_types[op].bitfield.acc))
5398 {
5399 as_bad (_("`%s%s' not allowed with `%s%c'"),
5400 register_prefix,
5401 i.op[op].regs->reg_name,
5402 i.tm.name,
5403 i.suffix);
5404 return 0;
5405 }
5406 /* Warn if the e prefix on a general reg is missing. */
5407 else if ((!quiet_warnings || flag_code == CODE_64BIT)
5408 && i.types[op].bitfield.reg16
5409 && (i.tm.operand_types[op].bitfield.reg32
5410 || i.tm.operand_types[op].bitfield.acc))
5411 {
5412 /* Prohibit these changes in the 64bit mode, since the
5413 lowering is more complicated. */
5414 if (flag_code == CODE_64BIT)
5415 {
5416 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
5417 register_prefix, i.op[op].regs->reg_name,
5418 i.suffix);
5419 return 0;
5420 }
5421 #if REGISTER_WARNINGS
5422 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
5423 register_prefix,
5424 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
5425 register_prefix, i.op[op].regs->reg_name, i.suffix);
5426 #endif
5427 }
5428 /* Warn if the r prefix on a general reg is present. */
5429 else if (i.types[op].bitfield.reg64
5430 && (i.tm.operand_types[op].bitfield.reg32
5431 || i.tm.operand_types[op].bitfield.acc))
5432 {
5433 if (intel_syntax
5434 && i.tm.opcode_modifier.toqword
5435 && !i.types[0].bitfield.regxmm)
5436 {
5437 /* Convert to QWORD. We want REX byte. */
5438 i.suffix = QWORD_MNEM_SUFFIX;
5439 }
5440 else
5441 {
5442 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
5443 register_prefix, i.op[op].regs->reg_name,
5444 i.suffix);
5445 return 0;
5446 }
5447 }
5448 return 1;
5449 }
5450
5451 static int
5452 check_qword_reg (void)
5453 {
5454 int op;
5455
5456 for (op = i.operands; --op >= 0; )
5457 /* Reject eight bit registers, except where the template requires
5458 them. (eg. movzb) */
5459 if (i.types[op].bitfield.reg8
5460 && (i.tm.operand_types[op].bitfield.reg16
5461 || i.tm.operand_types[op].bitfield.reg32
5462 || i.tm.operand_types[op].bitfield.acc))
5463 {
5464 as_bad (_("`%s%s' not allowed with `%s%c'"),
5465 register_prefix,
5466 i.op[op].regs->reg_name,
5467 i.tm.name,
5468 i.suffix);
5469 return 0;
5470 }
5471 /* Warn if the r prefix on a general reg is missing. */
5472 else if ((i.types[op].bitfield.reg16
5473 || i.types[op].bitfield.reg32)
5474 && (i.tm.operand_types[op].bitfield.reg32
5475 || i.tm.operand_types[op].bitfield.acc))
5476 {
5477 /* Prohibit these changes in the 64bit mode, since the
5478 lowering is more complicated. */
5479 if (intel_syntax
5480 && i.tm.opcode_modifier.todword
5481 && !i.types[0].bitfield.regxmm)
5482 {
5483 /* Convert to DWORD. We don't want REX byte. */
5484 i.suffix = LONG_MNEM_SUFFIX;
5485 }
5486 else
5487 {
5488 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
5489 register_prefix, i.op[op].regs->reg_name,
5490 i.suffix);
5491 return 0;
5492 }
5493 }
5494 return 1;
5495 }
5496
5497 static int
5498 check_word_reg (void)
5499 {
5500 int op;
5501 for (op = i.operands; --op >= 0;)
5502 /* Reject eight bit registers, except where the template requires
5503 them. (eg. movzb) */
5504 if (i.types[op].bitfield.reg8
5505 && (i.tm.operand_types[op].bitfield.reg16
5506 || i.tm.operand_types[op].bitfield.reg32
5507 || i.tm.operand_types[op].bitfield.acc))
5508 {
5509 as_bad (_("`%s%s' not allowed with `%s%c'"),
5510 register_prefix,
5511 i.op[op].regs->reg_name,
5512 i.tm.name,
5513 i.suffix);
5514 return 0;
5515 }
5516 /* Warn if the e or r prefix on a general reg is present. */
5517 else if ((!quiet_warnings || flag_code == CODE_64BIT)
5518 && (i.types[op].bitfield.reg32
5519 || i.types[op].bitfield.reg64)
5520 && (i.tm.operand_types[op].bitfield.reg16
5521 || i.tm.operand_types[op].bitfield.acc))
5522 {
5523 /* Prohibit these changes in the 64bit mode, since the
5524 lowering is more complicated. */
5525 if (flag_code == CODE_64BIT)
5526 {
5527 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
5528 register_prefix, i.op[op].regs->reg_name,
5529 i.suffix);
5530 return 0;
5531 }
5532 #if REGISTER_WARNINGS
5533 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
5534 register_prefix,
5535 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
5536 register_prefix, i.op[op].regs->reg_name, i.suffix);
5537 #endif
5538 }
5539 return 1;
5540 }
5541
5542 static int
5543 update_imm (unsigned int j)
5544 {
5545 i386_operand_type overlap = i.types[j];
5546 if ((overlap.bitfield.imm8
5547 || overlap.bitfield.imm8s
5548 || overlap.bitfield.imm16
5549 || overlap.bitfield.imm32
5550 || overlap.bitfield.imm32s
5551 || overlap.bitfield.imm64)
5552 && !operand_type_equal (&overlap, &imm8)
5553 && !operand_type_equal (&overlap, &imm8s)
5554 && !operand_type_equal (&overlap, &imm16)
5555 && !operand_type_equal (&overlap, &imm32)
5556 && !operand_type_equal (&overlap, &imm32s)
5557 && !operand_type_equal (&overlap, &imm64))
5558 {
5559 if (i.suffix)
5560 {
5561 i386_operand_type temp;
5562
5563 operand_type_set (&temp, 0);
5564 if (i.suffix == BYTE_MNEM_SUFFIX)
5565 {
5566 temp.bitfield.imm8 = overlap.bitfield.imm8;
5567 temp.bitfield.imm8s = overlap.bitfield.imm8s;
5568 }
5569 else if (i.suffix == WORD_MNEM_SUFFIX)
5570 temp.bitfield.imm16 = overlap.bitfield.imm16;
5571 else if (i.suffix == QWORD_MNEM_SUFFIX)
5572 {
5573 temp.bitfield.imm64 = overlap.bitfield.imm64;
5574 temp.bitfield.imm32s = overlap.bitfield.imm32s;
5575 }
5576 else
5577 temp.bitfield.imm32 = overlap.bitfield.imm32;
5578 overlap = temp;
5579 }
5580 else if (operand_type_equal (&overlap, &imm16_32_32s)
5581 || operand_type_equal (&overlap, &imm16_32)
5582 || operand_type_equal (&overlap, &imm16_32s))
5583 {
5584 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5585 overlap = imm16;
5586 else
5587 overlap = imm32s;
5588 }
5589 if (!operand_type_equal (&overlap, &imm8)
5590 && !operand_type_equal (&overlap, &imm8s)
5591 && !operand_type_equal (&overlap, &imm16)
5592 && !operand_type_equal (&overlap, &imm32)
5593 && !operand_type_equal (&overlap, &imm32s)
5594 && !operand_type_equal (&overlap, &imm64))
5595 {
5596 as_bad (_("no instruction mnemonic suffix given; "
5597 "can't determine immediate size"));
5598 return 0;
5599 }
5600 }
5601 i.types[j] = overlap;
5602
5603 return 1;
5604 }
5605
5606 static int
5607 finalize_imm (void)
5608 {
5609 unsigned int j, n;
5610
5611 /* Update the first 2 immediate operands. */
5612 n = i.operands > 2 ? 2 : i.operands;
5613 if (n)
5614 {
5615 for (j = 0; j < n; j++)
5616 if (update_imm (j) == 0)
5617 return 0;
5618
5619 /* The 3rd operand can't be immediate operand. */
5620 gas_assert (operand_type_check (i.types[2], imm) == 0);
5621 }
5622
5623 return 1;
5624 }
5625
5626 static int
5627 bad_implicit_operand (int xmm)
5628 {
5629 const char *ireg = xmm ? "xmm0" : "ymm0";
5630
5631 if (intel_syntax)
5632 as_bad (_("the last operand of `%s' must be `%s%s'"),
5633 i.tm.name, register_prefix, ireg);
5634 else
5635 as_bad (_("the first operand of `%s' must be `%s%s'"),
5636 i.tm.name, register_prefix, ireg);
5637 return 0;
5638 }
5639
5640 static int
5641 process_operands (void)
5642 {
5643 /* Default segment register this instruction will use for memory
5644 accesses. 0 means unknown. This is only for optimizing out
5645 unnecessary segment overrides. */
5646 const seg_entry *default_seg = 0;
5647
5648 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
5649 {
5650 unsigned int dupl = i.operands;
5651 unsigned int dest = dupl - 1;
5652 unsigned int j;
5653
5654 /* The destination must be an xmm register. */
5655 gas_assert (i.reg_operands
5656 && MAX_OPERANDS > dupl
5657 && operand_type_equal (&i.types[dest], &regxmm));
5658
5659 if (i.tm.opcode_modifier.firstxmm0)
5660 {
5661 /* The first operand is implicit and must be xmm0. */
5662 gas_assert (operand_type_equal (&i.types[0], &regxmm));
5663 if (register_number (i.op[0].regs) != 0)
5664 return bad_implicit_operand (1);
5665
5666 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
5667 {
5668 /* Keep xmm0 for instructions with VEX prefix and 3
5669 sources. */
5670 goto duplicate;
5671 }
5672 else
5673 {
5674 /* We remove the first xmm0 and keep the number of
5675 operands unchanged, which in fact duplicates the
5676 destination. */
5677 for (j = 1; j < i.operands; j++)
5678 {
5679 i.op[j - 1] = i.op[j];
5680 i.types[j - 1] = i.types[j];
5681 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
5682 }
5683 }
5684 }
5685 else if (i.tm.opcode_modifier.implicit1stxmm0)
5686 {
5687 gas_assert ((MAX_OPERANDS - 1) > dupl
5688 && (i.tm.opcode_modifier.vexsources
5689 == VEX3SOURCES));
5690
5691 /* Add the implicit xmm0 for instructions with VEX prefix
5692 and 3 sources. */
5693 for (j = i.operands; j > 0; j--)
5694 {
5695 i.op[j] = i.op[j - 1];
5696 i.types[j] = i.types[j - 1];
5697 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
5698 }
5699 i.op[0].regs
5700 = (const reg_entry *) hash_find (reg_hash, "xmm0");
5701 i.types[0] = regxmm;
5702 i.tm.operand_types[0] = regxmm;
5703
5704 i.operands += 2;
5705 i.reg_operands += 2;
5706 i.tm.operands += 2;
5707
5708 dupl++;
5709 dest++;
5710 i.op[dupl] = i.op[dest];
5711 i.types[dupl] = i.types[dest];
5712 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
5713 }
5714 else
5715 {
5716 duplicate:
5717 i.operands++;
5718 i.reg_operands++;
5719 i.tm.operands++;
5720
5721 i.op[dupl] = i.op[dest];
5722 i.types[dupl] = i.types[dest];
5723 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
5724 }
5725
5726 if (i.tm.opcode_modifier.immext)
5727 process_immext ();
5728 }
5729 else if (i.tm.opcode_modifier.firstxmm0)
5730 {
5731 unsigned int j;
5732
5733 /* The first operand is implicit and must be xmm0/ymm0/zmm0. */
5734 gas_assert (i.reg_operands
5735 && (operand_type_equal (&i.types[0], &regxmm)
5736 || operand_type_equal (&i.types[0], &regymm)
5737 || operand_type_equal (&i.types[0], &regzmm)));
5738 if (register_number (i.op[0].regs) != 0)
5739 return bad_implicit_operand (i.types[0].bitfield.regxmm);
5740
5741 for (j = 1; j < i.operands; j++)
5742 {
5743 i.op[j - 1] = i.op[j];
5744 i.types[j - 1] = i.types[j];
5745
5746 /* We need to adjust fields in i.tm since they are used by
5747 build_modrm_byte. */
5748 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
5749 }
5750
5751 i.operands--;
5752 i.reg_operands--;
5753 i.tm.operands--;
5754 }
5755 else if (i.tm.opcode_modifier.regkludge)
5756 {
5757 /* The imul $imm, %reg instruction is converted into
5758 imul $imm, %reg, %reg, and the clr %reg instruction
5759 is converted into xor %reg, %reg. */
5760
5761 unsigned int first_reg_op;
5762
5763 if (operand_type_check (i.types[0], reg))
5764 first_reg_op = 0;
5765 else
5766 first_reg_op = 1;
5767 /* Pretend we saw the extra register operand. */
5768 gas_assert (i.reg_operands == 1
5769 && i.op[first_reg_op + 1].regs == 0);
5770 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
5771 i.types[first_reg_op + 1] = i.types[first_reg_op];
5772 i.operands++;
5773 i.reg_operands++;
5774 }
5775
5776 if (i.tm.opcode_modifier.shortform)
5777 {
5778 if (i.types[0].bitfield.sreg2
5779 || i.types[0].bitfield.sreg3)
5780 {
5781 if (i.tm.base_opcode == POP_SEG_SHORT
5782 && i.op[0].regs->reg_num == 1)
5783 {
5784 as_bad (_("you can't `pop %scs'"), register_prefix);
5785 return 0;
5786 }
5787 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
5788 if ((i.op[0].regs->reg_flags & RegRex) != 0)
5789 i.rex |= REX_B;
5790 }
5791 else
5792 {
5793 /* The register or float register operand is in operand
5794 0 or 1. */
5795 unsigned int op;
5796
5797 if (i.types[0].bitfield.floatreg
5798 || operand_type_check (i.types[0], reg))
5799 op = 0;
5800 else
5801 op = 1;
5802 /* Register goes in low 3 bits of opcode. */
5803 i.tm.base_opcode |= i.op[op].regs->reg_num;
5804 if ((i.op[op].regs->reg_flags & RegRex) != 0)
5805 i.rex |= REX_B;
5806 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
5807 {
5808 /* Warn about some common errors, but press on regardless.
5809 The first case can be generated by gcc (<= 2.8.1). */
5810 if (i.operands == 2)
5811 {
5812 /* Reversed arguments on faddp, fsubp, etc. */
5813 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
5814 register_prefix, i.op[!intel_syntax].regs->reg_name,
5815 register_prefix, i.op[intel_syntax].regs->reg_name);
5816 }
5817 else
5818 {
5819 /* Extraneous `l' suffix on fp insn. */
5820 as_warn (_("translating to `%s %s%s'"), i.tm.name,
5821 register_prefix, i.op[0].regs->reg_name);
5822 }
5823 }
5824 }
5825 }
5826 else if (i.tm.opcode_modifier.modrm)
5827 {
5828 /* The opcode is completed (modulo i.tm.extension_opcode which
5829 must be put into the modrm byte). Now, we make the modrm and
5830 index base bytes based on all the info we've collected. */
5831
5832 default_seg = build_modrm_byte ();
5833 }
5834 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
5835 {
5836 default_seg = &ds;
5837 }
5838 else if (i.tm.opcode_modifier.isstring)
5839 {
5840 /* For the string instructions that allow a segment override
5841 on one of their operands, the default segment is ds. */
5842 default_seg = &ds;
5843 }
5844
5845 if (i.tm.base_opcode == 0x8d /* lea */
5846 && i.seg[0]
5847 && !quiet_warnings)
5848 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
5849
5850 /* If a segment was explicitly specified, and the specified segment
5851 is not the default, use an opcode prefix to select it. If we
5852 never figured out what the default segment is, then default_seg
5853 will be zero at this point, and the specified segment prefix will
5854 always be used. */
5855 if ((i.seg[0]) && (i.seg[0] != default_seg))
5856 {
5857 if (!add_prefix (i.seg[0]->seg_prefix))
5858 return 0;
5859 }
5860 return 1;
5861 }
5862
5863 static const seg_entry *
5864 build_modrm_byte (void)
5865 {
5866 const seg_entry *default_seg = 0;
5867 unsigned int source, dest;
5868 int vex_3_sources;
5869
5870 /* The first operand of instructions with VEX prefix and 3 sources
5871 must be VEX_Imm4. */
5872 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
5873 if (vex_3_sources)
5874 {
5875 unsigned int nds, reg_slot;
5876 expressionS *exp;
5877
5878 if (i.tm.opcode_modifier.veximmext
5879 && i.tm.opcode_modifier.immext)
5880 {
5881 dest = i.operands - 2;
5882 gas_assert (dest == 3);
5883 }
5884 else
5885 dest = i.operands - 1;
5886 nds = dest - 1;
5887
5888 /* There are 2 kinds of instructions:
5889 1. 5 operands: 4 register operands or 3 register operands
5890 plus 1 memory operand plus one Vec_Imm4 operand, VexXDS, and
5891 VexW0 or VexW1. The destination must be either XMM, YMM or
5892 ZMM register.
5893 2. 4 operands: 4 register operands or 3 register operands
5894 plus 1 memory operand, VexXDS, and VexImmExt */
5895 gas_assert ((i.reg_operands == 4
5896 || (i.reg_operands == 3 && i.mem_operands == 1))
5897 && i.tm.opcode_modifier.vexvvvv == VEXXDS
5898 && (i.tm.opcode_modifier.veximmext
5899 || (i.imm_operands == 1
5900 && i.types[0].bitfield.vec_imm4
5901 && (i.tm.opcode_modifier.vexw == VEXW0
5902 || i.tm.opcode_modifier.vexw == VEXW1)
5903 && (operand_type_equal (&i.tm.operand_types[dest], &regxmm)
5904 || operand_type_equal (&i.tm.operand_types[dest], &regymm)
5905 || operand_type_equal (&i.tm.operand_types[dest], &regzmm)))));
5906
5907 if (i.imm_operands == 0)
5908 {
5909 /* When there is no immediate operand, generate an 8bit
5910 immediate operand to encode the first operand. */
5911 exp = &im_expressions[i.imm_operands++];
5912 i.op[i.operands].imms = exp;
5913 i.types[i.operands] = imm8;
5914 i.operands++;
5915 /* If VexW1 is set, the first operand is the source and
5916 the second operand is encoded in the immediate operand. */
5917 if (i.tm.opcode_modifier.vexw == VEXW1)
5918 {
5919 source = 0;
5920 reg_slot = 1;
5921 }
5922 else
5923 {
5924 source = 1;
5925 reg_slot = 0;
5926 }
5927
5928 /* FMA swaps REG and NDS. */
5929 if (i.tm.cpu_flags.bitfield.cpufma)
5930 {
5931 unsigned int tmp;
5932 tmp = reg_slot;
5933 reg_slot = nds;
5934 nds = tmp;
5935 }
5936
5937 gas_assert (operand_type_equal (&i.tm.operand_types[reg_slot],
5938 &regxmm)
5939 || operand_type_equal (&i.tm.operand_types[reg_slot],
5940 &regymm)
5941 || operand_type_equal (&i.tm.operand_types[reg_slot],
5942 &regzmm));
5943 exp->X_op = O_constant;
5944 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
5945 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
5946 }
5947 else
5948 {
5949 unsigned int imm_slot;
5950
5951 if (i.tm.opcode_modifier.vexw == VEXW0)
5952 {
5953 /* If VexW0 is set, the third operand is the source and
5954 the second operand is encoded in the immediate
5955 operand. */
5956 source = 2;
5957 reg_slot = 1;
5958 }
5959 else
5960 {
5961 /* VexW1 is set, the second operand is the source and
5962 the third operand is encoded in the immediate
5963 operand. */
5964 source = 1;
5965 reg_slot = 2;
5966 }
5967
5968 if (i.tm.opcode_modifier.immext)
5969 {
5970 /* When ImmExt is set, the immdiate byte is the last
5971 operand. */
5972 imm_slot = i.operands - 1;
5973 source--;
5974 reg_slot--;
5975 }
5976 else
5977 {
5978 imm_slot = 0;
5979
5980 /* Turn on Imm8 so that output_imm will generate it. */
5981 i.types[imm_slot].bitfield.imm8 = 1;
5982 }
5983
5984 gas_assert (operand_type_equal (&i.tm.operand_types[reg_slot],
5985 &regxmm)
5986 || operand_type_equal (&i.tm.operand_types[reg_slot],
5987 &regymm)
5988 || operand_type_equal (&i.tm.operand_types[reg_slot],
5989 &regzmm));
5990 i.op[imm_slot].imms->X_add_number
5991 |= register_number (i.op[reg_slot].regs) << 4;
5992 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
5993 }
5994
5995 gas_assert (operand_type_equal (&i.tm.operand_types[nds], &regxmm)
5996 || operand_type_equal (&i.tm.operand_types[nds],
5997 &regymm)
5998 || operand_type_equal (&i.tm.operand_types[nds],
5999 &regzmm));
6000 i.vex.register_specifier = i.op[nds].regs;
6001 }
6002 else
6003 source = dest = 0;
6004
6005 /* i.reg_operands MUST be the number of real register operands;
6006 implicit registers do not count. If there are 3 register
6007 operands, it must be a instruction with VexNDS. For a
6008 instruction with VexNDD, the destination register is encoded
6009 in VEX prefix. If there are 4 register operands, it must be
6010 a instruction with VEX prefix and 3 sources. */
6011 if (i.mem_operands == 0
6012 && ((i.reg_operands == 2
6013 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
6014 || (i.reg_operands == 3
6015 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
6016 || (i.reg_operands == 4 && vex_3_sources)))
6017 {
6018 switch (i.operands)
6019 {
6020 case 2:
6021 source = 0;
6022 break;
6023 case 3:
6024 /* When there are 3 operands, one of them may be immediate,
6025 which may be the first or the last operand. Otherwise,
6026 the first operand must be shift count register (cl) or it
6027 is an instruction with VexNDS. */
6028 gas_assert (i.imm_operands == 1
6029 || (i.imm_operands == 0
6030 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
6031 || i.types[0].bitfield.shiftcount)));
6032 if (operand_type_check (i.types[0], imm)
6033 || i.types[0].bitfield.shiftcount)
6034 source = 1;
6035 else
6036 source = 0;
6037 break;
6038 case 4:
6039 /* When there are 4 operands, the first two must be 8bit
6040 immediate operands. The source operand will be the 3rd
6041 one.
6042
6043 For instructions with VexNDS, if the first operand
6044 an imm8, the source operand is the 2nd one. If the last
6045 operand is imm8, the source operand is the first one. */
6046 gas_assert ((i.imm_operands == 2
6047 && i.types[0].bitfield.imm8
6048 && i.types[1].bitfield.imm8)
6049 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
6050 && i.imm_operands == 1
6051 && (i.types[0].bitfield.imm8
6052 || i.types[i.operands - 1].bitfield.imm8
6053 || i.rounding)));
6054 if (i.imm_operands == 2)
6055 source = 2;
6056 else
6057 {
6058 if (i.types[0].bitfield.imm8)
6059 source = 1;
6060 else
6061 source = 0;
6062 }
6063 break;
6064 case 5:
6065 if (i.tm.opcode_modifier.evex)
6066 {
6067 /* For EVEX instructions, when there are 5 operands, the
6068 first one must be immediate operand. If the second one
6069 is immediate operand, the source operand is the 3th
6070 one. If the last one is immediate operand, the source
6071 operand is the 2nd one. */
6072 gas_assert (i.imm_operands == 2
6073 && i.tm.opcode_modifier.sae
6074 && operand_type_check (i.types[0], imm));
6075 if (operand_type_check (i.types[1], imm))
6076 source = 2;
6077 else if (operand_type_check (i.types[4], imm))
6078 source = 1;
6079 else
6080 abort ();
6081 }
6082 break;
6083 default:
6084 abort ();
6085 }
6086
6087 if (!vex_3_sources)
6088 {
6089 dest = source + 1;
6090
6091 /* RC/SAE operand could be between DEST and SRC. That happens
6092 when one operand is GPR and the other one is XMM/YMM/ZMM
6093 register. */
6094 if (i.rounding && i.rounding->operand == (int) dest)
6095 dest++;
6096
6097 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
6098 {
6099 /* For instructions with VexNDS, the register-only source
6100 operand must be 32/64bit integer, XMM, YMM or ZMM
6101 register. It is encoded in VEX prefix. We need to
6102 clear RegMem bit before calling operand_type_equal. */
6103
6104 i386_operand_type op;
6105 unsigned int vvvv;
6106
6107 /* Check register-only source operand when two source
6108 operands are swapped. */
6109 if (!i.tm.operand_types[source].bitfield.baseindex
6110 && i.tm.operand_types[dest].bitfield.baseindex)
6111 {
6112 vvvv = source;
6113 source = dest;
6114 }
6115 else
6116 vvvv = dest;
6117
6118 op = i.tm.operand_types[vvvv];
6119 op.bitfield.regmem = 0;
6120 if ((dest + 1) >= i.operands
6121 || (!op.bitfield.reg32
6122 && op.bitfield.reg64
6123 && !operand_type_equal (&op, &regxmm)
6124 && !operand_type_equal (&op, &regymm)
6125 && !operand_type_equal (&op, &regzmm)
6126 && !operand_type_equal (&op, &regmask)))
6127 abort ();
6128 i.vex.register_specifier = i.op[vvvv].regs;
6129 dest++;
6130 }
6131 }
6132
6133 i.rm.mode = 3;
6134 /* One of the register operands will be encoded in the i.tm.reg
6135 field, the other in the combined i.tm.mode and i.tm.regmem
6136 fields. If no form of this instruction supports a memory
6137 destination operand, then we assume the source operand may
6138 sometimes be a memory operand and so we need to store the
6139 destination in the i.rm.reg field. */
6140 if (!i.tm.operand_types[dest].bitfield.regmem
6141 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
6142 {
6143 i.rm.reg = i.op[dest].regs->reg_num;
6144 i.rm.regmem = i.op[source].regs->reg_num;
6145 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
6146 i.rex |= REX_R;
6147 if ((i.op[dest].regs->reg_flags & RegVRex) != 0)
6148 i.vrex |= REX_R;
6149 if ((i.op[source].regs->reg_flags & RegRex) != 0)
6150 i.rex |= REX_B;
6151 if ((i.op[source].regs->reg_flags & RegVRex) != 0)
6152 i.vrex |= REX_B;
6153 }
6154 else
6155 {
6156 i.rm.reg = i.op[source].regs->reg_num;
6157 i.rm.regmem = i.op[dest].regs->reg_num;
6158 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
6159 i.rex |= REX_B;
6160 if ((i.op[dest].regs->reg_flags & RegVRex) != 0)
6161 i.vrex |= REX_B;
6162 if ((i.op[source].regs->reg_flags & RegRex) != 0)
6163 i.rex |= REX_R;
6164 if ((i.op[source].regs->reg_flags & RegVRex) != 0)
6165 i.vrex |= REX_R;
6166 }
6167 if (flag_code != CODE_64BIT && (i.rex & (REX_R | REX_B)))
6168 {
6169 if (!i.types[0].bitfield.control
6170 && !i.types[1].bitfield.control)
6171 abort ();
6172 i.rex &= ~(REX_R | REX_B);
6173 add_prefix (LOCK_PREFIX_OPCODE);
6174 }
6175 }
6176 else
6177 { /* If it's not 2 reg operands... */
6178 unsigned int mem;
6179
6180 if (i.mem_operands)
6181 {
6182 unsigned int fake_zero_displacement = 0;
6183 unsigned int op;
6184
6185 for (op = 0; op < i.operands; op++)
6186 if (operand_type_check (i.types[op], anymem))
6187 break;
6188 gas_assert (op < i.operands);
6189
6190 if (i.tm.opcode_modifier.vecsib)
6191 {
6192 if (i.index_reg->reg_num == RegEiz
6193 || i.index_reg->reg_num == RegRiz)
6194 abort ();
6195
6196 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
6197 if (!i.base_reg)
6198 {
6199 i.sib.base = NO_BASE_REGISTER;
6200 i.sib.scale = i.log2_scale_factor;
6201 /* No Vec_Disp8 if there is no base. */
6202 i.types[op].bitfield.vec_disp8 = 0;
6203 i.types[op].bitfield.disp8 = 0;
6204 i.types[op].bitfield.disp16 = 0;
6205 i.types[op].bitfield.disp64 = 0;
6206 if (flag_code != CODE_64BIT)
6207 {
6208 /* Must be 32 bit */
6209 i.types[op].bitfield.disp32 = 1;
6210 i.types[op].bitfield.disp32s = 0;
6211 }
6212 else
6213 {
6214 i.types[op].bitfield.disp32 = 0;
6215 i.types[op].bitfield.disp32s = 1;
6216 }
6217 }
6218 i.sib.index = i.index_reg->reg_num;
6219 if ((i.index_reg->reg_flags & RegRex) != 0)
6220 i.rex |= REX_X;
6221 if ((i.index_reg->reg_flags & RegVRex) != 0)
6222 i.vrex |= REX_X;
6223 }
6224
6225 default_seg = &ds;
6226
6227 if (i.base_reg == 0)
6228 {
6229 i.rm.mode = 0;
6230 if (!i.disp_operands)
6231 {
6232 fake_zero_displacement = 1;
6233 /* Instructions with VSIB byte need 32bit displacement
6234 if there is no base register. */
6235 if (i.tm.opcode_modifier.vecsib)
6236 i.types[op].bitfield.disp32 = 1;
6237 }
6238 if (i.index_reg == 0)
6239 {
6240 gas_assert (!i.tm.opcode_modifier.vecsib);
6241 /* Operand is just <disp> */
6242 if (flag_code == CODE_64BIT)
6243 {
6244 /* 64bit mode overwrites the 32bit absolute
6245 addressing by RIP relative addressing and
6246 absolute addressing is encoded by one of the
6247 redundant SIB forms. */
6248 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
6249 i.sib.base = NO_BASE_REGISTER;
6250 i.sib.index = NO_INDEX_REGISTER;
6251 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0)
6252 ? disp32s : disp32);
6253 }
6254 else if ((flag_code == CODE_16BIT)
6255 ^ (i.prefix[ADDR_PREFIX] != 0))
6256 {
6257 i.rm.regmem = NO_BASE_REGISTER_16;
6258 i.types[op] = disp16;
6259 }
6260 else
6261 {
6262 i.rm.regmem = NO_BASE_REGISTER;
6263 i.types[op] = disp32;
6264 }
6265 }
6266 else if (!i.tm.opcode_modifier.vecsib)
6267 {
6268 /* !i.base_reg && i.index_reg */
6269 if (i.index_reg->reg_num == RegEiz
6270 || i.index_reg->reg_num == RegRiz)
6271 i.sib.index = NO_INDEX_REGISTER;
6272 else
6273 i.sib.index = i.index_reg->reg_num;
6274 i.sib.base = NO_BASE_REGISTER;
6275 i.sib.scale = i.log2_scale_factor;
6276 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
6277 /* No Vec_Disp8 if there is no base. */
6278 i.types[op].bitfield.vec_disp8 = 0;
6279 i.types[op].bitfield.disp8 = 0;
6280 i.types[op].bitfield.disp16 = 0;
6281 i.types[op].bitfield.disp64 = 0;
6282 if (flag_code != CODE_64BIT)
6283 {
6284 /* Must be 32 bit */
6285 i.types[op].bitfield.disp32 = 1;
6286 i.types[op].bitfield.disp32s = 0;
6287 }
6288 else
6289 {
6290 i.types[op].bitfield.disp32 = 0;
6291 i.types[op].bitfield.disp32s = 1;
6292 }
6293 if ((i.index_reg->reg_flags & RegRex) != 0)
6294 i.rex |= REX_X;
6295 }
6296 }
6297 /* RIP addressing for 64bit mode. */
6298 else if (i.base_reg->reg_num == RegRip ||
6299 i.base_reg->reg_num == RegEip)
6300 {
6301 gas_assert (!i.tm.opcode_modifier.vecsib);
6302 i.rm.regmem = NO_BASE_REGISTER;
6303 i.types[op].bitfield.disp8 = 0;
6304 i.types[op].bitfield.disp16 = 0;
6305 i.types[op].bitfield.disp32 = 0;
6306 i.types[op].bitfield.disp32s = 1;
6307 i.types[op].bitfield.disp64 = 0;
6308 i.types[op].bitfield.vec_disp8 = 0;
6309 i.flags[op] |= Operand_PCrel;
6310 if (! i.disp_operands)
6311 fake_zero_displacement = 1;
6312 }
6313 else if (i.base_reg->reg_type.bitfield.reg16)
6314 {
6315 gas_assert (!i.tm.opcode_modifier.vecsib);
6316 switch (i.base_reg->reg_num)
6317 {
6318 case 3: /* (%bx) */
6319 if (i.index_reg == 0)
6320 i.rm.regmem = 7;
6321 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
6322 i.rm.regmem = i.index_reg->reg_num - 6;
6323 break;
6324 case 5: /* (%bp) */
6325 default_seg = &ss;
6326 if (i.index_reg == 0)
6327 {
6328 i.rm.regmem = 6;
6329 if (operand_type_check (i.types[op], disp) == 0)
6330 {
6331 /* fake (%bp) into 0(%bp) */
6332 if (i.tm.operand_types[op].bitfield.vec_disp8)
6333 i.types[op].bitfield.vec_disp8 = 1;
6334 else
6335 i.types[op].bitfield.disp8 = 1;
6336 fake_zero_displacement = 1;
6337 }
6338 }
6339 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
6340 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
6341 break;
6342 default: /* (%si) -> 4 or (%di) -> 5 */
6343 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
6344 }
6345 i.rm.mode = mode_from_disp_size (i.types[op]);
6346 }
6347 else /* i.base_reg and 32/64 bit mode */
6348 {
6349 if (flag_code == CODE_64BIT
6350 && operand_type_check (i.types[op], disp))
6351 {
6352 i386_operand_type temp;
6353 operand_type_set (&temp, 0);
6354 temp.bitfield.disp8 = i.types[op].bitfield.disp8;
6355 temp.bitfield.vec_disp8
6356 = i.types[op].bitfield.vec_disp8;
6357 i.types[op] = temp;
6358 if (i.prefix[ADDR_PREFIX] == 0)
6359 i.types[op].bitfield.disp32s = 1;
6360 else
6361 i.types[op].bitfield.disp32 = 1;
6362 }
6363
6364 if (!i.tm.opcode_modifier.vecsib)
6365 i.rm.regmem = i.base_reg->reg_num;
6366 if ((i.base_reg->reg_flags & RegRex) != 0)
6367 i.rex |= REX_B;
6368 i.sib.base = i.base_reg->reg_num;
6369 /* x86-64 ignores REX prefix bit here to avoid decoder
6370 complications. */
6371 if (!(i.base_reg->reg_flags & RegRex)
6372 && (i.base_reg->reg_num == EBP_REG_NUM
6373 || i.base_reg->reg_num == ESP_REG_NUM))
6374 default_seg = &ss;
6375 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
6376 {
6377 fake_zero_displacement = 1;
6378 if (i.tm.operand_types [op].bitfield.vec_disp8)
6379 i.types[op].bitfield.vec_disp8 = 1;
6380 else
6381 i.types[op].bitfield.disp8 = 1;
6382 }
6383 i.sib.scale = i.log2_scale_factor;
6384 if (i.index_reg == 0)
6385 {
6386 gas_assert (!i.tm.opcode_modifier.vecsib);
6387 /* <disp>(%esp) becomes two byte modrm with no index
6388 register. We've already stored the code for esp
6389 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
6390 Any base register besides %esp will not use the
6391 extra modrm byte. */
6392 i.sib.index = NO_INDEX_REGISTER;
6393 }
6394 else if (!i.tm.opcode_modifier.vecsib)
6395 {
6396 if (i.index_reg->reg_num == RegEiz
6397 || i.index_reg->reg_num == RegRiz)
6398 i.sib.index = NO_INDEX_REGISTER;
6399 else
6400 i.sib.index = i.index_reg->reg_num;
6401 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
6402 if ((i.index_reg->reg_flags & RegRex) != 0)
6403 i.rex |= REX_X;
6404 }
6405
6406 if (i.disp_operands
6407 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6408 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
6409 i.rm.mode = 0;
6410 else
6411 {
6412 if (!fake_zero_displacement
6413 && !i.disp_operands
6414 && i.disp_encoding)
6415 {
6416 fake_zero_displacement = 1;
6417 if (i.disp_encoding == disp_encoding_8bit)
6418 i.types[op].bitfield.disp8 = 1;
6419 else
6420 i.types[op].bitfield.disp32 = 1;
6421 }
6422 i.rm.mode = mode_from_disp_size (i.types[op]);
6423 }
6424 }
6425
6426 if (fake_zero_displacement)
6427 {
6428 /* Fakes a zero displacement assuming that i.types[op]
6429 holds the correct displacement size. */
6430 expressionS *exp;
6431
6432 gas_assert (i.op[op].disps == 0);
6433 exp = &disp_expressions[i.disp_operands++];
6434 i.op[op].disps = exp;
6435 exp->X_op = O_constant;
6436 exp->X_add_number = 0;
6437 exp->X_add_symbol = (symbolS *) 0;
6438 exp->X_op_symbol = (symbolS *) 0;
6439 }
6440
6441 mem = op;
6442 }
6443 else
6444 mem = ~0;
6445
6446 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
6447 {
6448 if (operand_type_check (i.types[0], imm))
6449 i.vex.register_specifier = NULL;
6450 else
6451 {
6452 /* VEX.vvvv encodes one of the sources when the first
6453 operand is not an immediate. */
6454 if (i.tm.opcode_modifier.vexw == VEXW0)
6455 i.vex.register_specifier = i.op[0].regs;
6456 else
6457 i.vex.register_specifier = i.op[1].regs;
6458 }
6459
6460 /* Destination is a XMM register encoded in the ModRM.reg
6461 and VEX.R bit. */
6462 i.rm.reg = i.op[2].regs->reg_num;
6463 if ((i.op[2].regs->reg_flags & RegRex) != 0)
6464 i.rex |= REX_R;
6465
6466 /* ModRM.rm and VEX.B encodes the other source. */
6467 if (!i.mem_operands)
6468 {
6469 i.rm.mode = 3;
6470
6471 if (i.tm.opcode_modifier.vexw == VEXW0)
6472 i.rm.regmem = i.op[1].regs->reg_num;
6473 else
6474 i.rm.regmem = i.op[0].regs->reg_num;
6475
6476 if ((i.op[1].regs->reg_flags & RegRex) != 0)
6477 i.rex |= REX_B;
6478 }
6479 }
6480 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
6481 {
6482 i.vex.register_specifier = i.op[2].regs;
6483 if (!i.mem_operands)
6484 {
6485 i.rm.mode = 3;
6486 i.rm.regmem = i.op[1].regs->reg_num;
6487 if ((i.op[1].regs->reg_flags & RegRex) != 0)
6488 i.rex |= REX_B;
6489 }
6490 }
6491 /* Fill in i.rm.reg or i.rm.regmem field with register operand
6492 (if any) based on i.tm.extension_opcode. Again, we must be
6493 careful to make sure that segment/control/debug/test/MMX
6494 registers are coded into the i.rm.reg field. */
6495 else if (i.reg_operands)
6496 {
6497 unsigned int op;
6498 unsigned int vex_reg = ~0;
6499
6500 for (op = 0; op < i.operands; op++)
6501 if (i.types[op].bitfield.reg8
6502 || i.types[op].bitfield.reg16
6503 || i.types[op].bitfield.reg32
6504 || i.types[op].bitfield.reg64
6505 || i.types[op].bitfield.regmmx
6506 || i.types[op].bitfield.regxmm
6507 || i.types[op].bitfield.regymm
6508 || i.types[op].bitfield.regbnd
6509 || i.types[op].bitfield.regzmm
6510 || i.types[op].bitfield.regmask
6511 || i.types[op].bitfield.sreg2
6512 || i.types[op].bitfield.sreg3
6513 || i.types[op].bitfield.control
6514 || i.types[op].bitfield.debug
6515 || i.types[op].bitfield.test)
6516 break;
6517
6518 if (vex_3_sources)
6519 op = dest;
6520 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
6521 {
6522 /* For instructions with VexNDS, the register-only
6523 source operand is encoded in VEX prefix. */
6524 gas_assert (mem != (unsigned int) ~0);
6525
6526 if (op > mem)
6527 {
6528 vex_reg = op++;
6529 gas_assert (op < i.operands);
6530 }
6531 else
6532 {
6533 /* Check register-only source operand when two source
6534 operands are swapped. */
6535 if (!i.tm.operand_types[op].bitfield.baseindex
6536 && i.tm.operand_types[op + 1].bitfield.baseindex)
6537 {
6538 vex_reg = op;
6539 op += 2;
6540 gas_assert (mem == (vex_reg + 1)
6541 && op < i.operands);
6542 }
6543 else
6544 {
6545 vex_reg = op + 1;
6546 gas_assert (vex_reg < i.operands);
6547 }
6548 }
6549 }
6550 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
6551 {
6552 /* For instructions with VexNDD, the register destination
6553 is encoded in VEX prefix. */
6554 if (i.mem_operands == 0)
6555 {
6556 /* There is no memory operand. */
6557 gas_assert ((op + 2) == i.operands);
6558 vex_reg = op + 1;
6559 }
6560 else
6561 {
6562 /* There are only 2 operands. */
6563 gas_assert (op < 2 && i.operands == 2);
6564 vex_reg = 1;
6565 }
6566 }
6567 else
6568 gas_assert (op < i.operands);
6569
6570 if (vex_reg != (unsigned int) ~0)
6571 {
6572 i386_operand_type *type = &i.tm.operand_types[vex_reg];
6573
6574 if (type->bitfield.reg32 != 1
6575 && type->bitfield.reg64 != 1
6576 && !operand_type_equal (type, &regxmm)
6577 && !operand_type_equal (type, &regymm)
6578 && !operand_type_equal (type, &regzmm)
6579 && !operand_type_equal (type, &regmask))
6580 abort ();
6581
6582 i.vex.register_specifier = i.op[vex_reg].regs;
6583 }
6584
6585 /* Don't set OP operand twice. */
6586 if (vex_reg != op)
6587 {
6588 /* If there is an extension opcode to put here, the
6589 register number must be put into the regmem field. */
6590 if (i.tm.extension_opcode != None)
6591 {
6592 i.rm.regmem = i.op[op].regs->reg_num;
6593 if ((i.op[op].regs->reg_flags & RegRex) != 0)
6594 i.rex |= REX_B;
6595 if ((i.op[op].regs->reg_flags & RegVRex) != 0)
6596 i.vrex |= REX_B;
6597 }
6598 else
6599 {
6600 i.rm.reg = i.op[op].regs->reg_num;
6601 if ((i.op[op].regs->reg_flags & RegRex) != 0)
6602 i.rex |= REX_R;
6603 if ((i.op[op].regs->reg_flags & RegVRex) != 0)
6604 i.vrex |= REX_R;
6605 }
6606 }
6607
6608 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
6609 must set it to 3 to indicate this is a register operand
6610 in the regmem field. */
6611 if (!i.mem_operands)
6612 i.rm.mode = 3;
6613 }
6614
6615 /* Fill in i.rm.reg field with extension opcode (if any). */
6616 if (i.tm.extension_opcode != None)
6617 i.rm.reg = i.tm.extension_opcode;
6618 }
6619 return default_seg;
6620 }
6621
6622 static void
6623 output_branch (void)
6624 {
6625 char *p;
6626 int size;
6627 int code16;
6628 int prefix;
6629 relax_substateT subtype;
6630 symbolS *sym;
6631 offsetT off;
6632
6633 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
6634 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
6635
6636 prefix = 0;
6637 if (i.prefix[DATA_PREFIX] != 0)
6638 {
6639 prefix = 1;
6640 i.prefixes -= 1;
6641 code16 ^= CODE16;
6642 }
6643 /* Pentium4 branch hints. */
6644 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
6645 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
6646 {
6647 prefix++;
6648 i.prefixes--;
6649 }
6650 if (i.prefix[REX_PREFIX] != 0)
6651 {
6652 prefix++;
6653 i.prefixes--;
6654 }
6655
6656 /* BND prefixed jump. */
6657 if (i.prefix[BND_PREFIX] != 0)
6658 {
6659 FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]);
6660 i.prefixes -= 1;
6661 }
6662
6663 if (i.prefixes != 0 && !intel_syntax)
6664 as_warn (_("skipping prefixes on this instruction"));
6665
6666 /* It's always a symbol; End frag & setup for relax.
6667 Make sure there is enough room in this frag for the largest
6668 instruction we may generate in md_convert_frag. This is 2
6669 bytes for the opcode and room for the prefix and largest
6670 displacement. */
6671 frag_grow (prefix + 2 + 4);
6672 /* Prefix and 1 opcode byte go in fr_fix. */
6673 p = frag_more (prefix + 1);
6674 if (i.prefix[DATA_PREFIX] != 0)
6675 *p++ = DATA_PREFIX_OPCODE;
6676 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
6677 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
6678 *p++ = i.prefix[SEG_PREFIX];
6679 if (i.prefix[REX_PREFIX] != 0)
6680 *p++ = i.prefix[REX_PREFIX];
6681 *p = i.tm.base_opcode;
6682
6683 if ((unsigned char) *p == JUMP_PC_RELATIVE)
6684 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
6685 else if (cpu_arch_flags.bitfield.cpui386)
6686 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
6687 else
6688 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
6689 subtype |= code16;
6690
6691 sym = i.op[0].disps->X_add_symbol;
6692 off = i.op[0].disps->X_add_number;
6693
6694 if (i.op[0].disps->X_op != O_constant
6695 && i.op[0].disps->X_op != O_symbol)
6696 {
6697 /* Handle complex expressions. */
6698 sym = make_expr_symbol (i.op[0].disps);
6699 off = 0;
6700 }
6701
6702 /* 1 possible extra opcode + 4 byte displacement go in var part.
6703 Pass reloc in fr_var. */
6704 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
6705 }
6706
6707 static void
6708 output_jump (void)
6709 {
6710 char *p;
6711 int size;
6712 fixS *fixP;
6713
6714 if (i.tm.opcode_modifier.jumpbyte)
6715 {
6716 /* This is a loop or jecxz type instruction. */
6717 size = 1;
6718 if (i.prefix[ADDR_PREFIX] != 0)
6719 {
6720 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
6721 i.prefixes -= 1;
6722 }
6723 /* Pentium4 branch hints. */
6724 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
6725 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
6726 {
6727 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
6728 i.prefixes--;
6729 }
6730 }
6731 else
6732 {
6733 int code16;
6734
6735 code16 = 0;
6736 if (flag_code == CODE_16BIT)
6737 code16 = CODE16;
6738
6739 if (i.prefix[DATA_PREFIX] != 0)
6740 {
6741 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
6742 i.prefixes -= 1;
6743 code16 ^= CODE16;
6744 }
6745
6746 size = 4;
6747 if (code16)
6748 size = 2;
6749 }
6750
6751 if (i.prefix[REX_PREFIX] != 0)
6752 {
6753 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
6754 i.prefixes -= 1;
6755 }
6756
6757 /* BND prefixed jump. */
6758 if (i.prefix[BND_PREFIX] != 0)
6759 {
6760 FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]);
6761 i.prefixes -= 1;
6762 }
6763
6764 if (i.prefixes != 0 && !intel_syntax)
6765 as_warn (_("skipping prefixes on this instruction"));
6766
6767 p = frag_more (i.tm.opcode_length + size);
6768 switch (i.tm.opcode_length)
6769 {
6770 case 2:
6771 *p++ = i.tm.base_opcode >> 8;
6772 case 1:
6773 *p++ = i.tm.base_opcode;
6774 break;
6775 default:
6776 abort ();
6777 }
6778
6779 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6780 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
6781
6782 /* All jumps handled here are signed, but don't use a signed limit
6783 check for 32 and 16 bit jumps as we want to allow wrap around at
6784 4G and 64k respectively. */
6785 if (size == 1)
6786 fixP->fx_signed = 1;
6787 }
6788
6789 static void
6790 output_interseg_jump (void)
6791 {
6792 char *p;
6793 int size;
6794 int prefix;
6795 int code16;
6796
6797 code16 = 0;
6798 if (flag_code == CODE_16BIT)
6799 code16 = CODE16;
6800
6801 prefix = 0;
6802 if (i.prefix[DATA_PREFIX] != 0)
6803 {
6804 prefix = 1;
6805 i.prefixes -= 1;
6806 code16 ^= CODE16;
6807 }
6808 if (i.prefix[REX_PREFIX] != 0)
6809 {
6810 prefix++;
6811 i.prefixes -= 1;
6812 }
6813
6814 size = 4;
6815 if (code16)
6816 size = 2;
6817
6818 if (i.prefixes != 0 && !intel_syntax)
6819 as_warn (_("skipping prefixes on this instruction"));
6820
6821 /* 1 opcode; 2 segment; offset */
6822 p = frag_more (prefix + 1 + 2 + size);
6823
6824 if (i.prefix[DATA_PREFIX] != 0)
6825 *p++ = DATA_PREFIX_OPCODE;
6826
6827 if (i.prefix[REX_PREFIX] != 0)
6828 *p++ = i.prefix[REX_PREFIX];
6829
6830 *p++ = i.tm.base_opcode;
6831 if (i.op[1].imms->X_op == O_constant)
6832 {
6833 offsetT n = i.op[1].imms->X_add_number;
6834
6835 if (size == 2
6836 && !fits_in_unsigned_word (n)
6837 && !fits_in_signed_word (n))
6838 {
6839 as_bad (_("16-bit jump out of range"));
6840 return;
6841 }
6842 md_number_to_chars (p, n, size);
6843 }
6844 else
6845 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6846 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6847 if (i.op[0].imms->X_op != O_constant)
6848 as_bad (_("can't handle non absolute segment in `%s'"),
6849 i.tm.name);
6850 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
6851 }
6852
6853 static void
6854 output_insn (void)
6855 {
6856 fragS *insn_start_frag;
6857 offsetT insn_start_off;
6858
6859 /* Tie dwarf2 debug info to the address at the start of the insn.
6860 We can't do this after the insn has been output as the current
6861 frag may have been closed off. eg. by frag_var. */
6862 dwarf2_emit_insn (0);
6863
6864 insn_start_frag = frag_now;
6865 insn_start_off = frag_now_fix ();
6866
6867 /* Output jumps. */
6868 if (i.tm.opcode_modifier.jump)
6869 output_branch ();
6870 else if (i.tm.opcode_modifier.jumpbyte
6871 || i.tm.opcode_modifier.jumpdword)
6872 output_jump ();
6873 else if (i.tm.opcode_modifier.jumpintersegment)
6874 output_interseg_jump ();
6875 else
6876 {
6877 /* Output normal instructions here. */
6878 char *p;
6879 unsigned char *q;
6880 unsigned int j;
6881 unsigned int prefix;
6882
6883 /* Some processors fail on LOCK prefix. This options makes
6884 assembler ignore LOCK prefix and serves as a workaround. */
6885 if (omit_lock_prefix)
6886 {
6887 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE)
6888 return;
6889 i.prefix[LOCK_PREFIX] = 0;
6890 }
6891
6892 /* Since the VEX/EVEX prefix contains the implicit prefix, we
6893 don't need the explicit prefix. */
6894 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
6895 {
6896 switch (i.tm.opcode_length)
6897 {
6898 case 3:
6899 if (i.tm.base_opcode & 0xff000000)
6900 {
6901 prefix = (i.tm.base_opcode >> 24) & 0xff;
6902 goto check_prefix;
6903 }
6904 break;
6905 case 2:
6906 if ((i.tm.base_opcode & 0xff0000) != 0)
6907 {
6908 prefix = (i.tm.base_opcode >> 16) & 0xff;
6909 if (i.tm.cpu_flags.bitfield.cpupadlock)
6910 {
6911 check_prefix:
6912 if (prefix != REPE_PREFIX_OPCODE
6913 || (i.prefix[REP_PREFIX]
6914 != REPE_PREFIX_OPCODE))
6915 add_prefix (prefix);
6916 }
6917 else
6918 add_prefix (prefix);
6919 }
6920 break;
6921 case 1:
6922 break;
6923 default:
6924 abort ();
6925 }
6926
6927 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
6928 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
6929 R_X86_64_GOTTPOFF relocation so that linker can safely
6930 perform IE->LE optimization. */
6931 if (x86_elf_abi == X86_64_X32_ABI
6932 && i.operands == 2
6933 && i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
6934 && i.prefix[REX_PREFIX] == 0)
6935 add_prefix (REX_OPCODE);
6936 #endif
6937
6938 /* The prefix bytes. */
6939 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
6940 if (*q)
6941 FRAG_APPEND_1_CHAR (*q);
6942 }
6943 else
6944 {
6945 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
6946 if (*q)
6947 switch (j)
6948 {
6949 case REX_PREFIX:
6950 /* REX byte is encoded in VEX prefix. */
6951 break;
6952 case SEG_PREFIX:
6953 case ADDR_PREFIX:
6954 FRAG_APPEND_1_CHAR (*q);
6955 break;
6956 default:
6957 /* There should be no other prefixes for instructions
6958 with VEX prefix. */
6959 abort ();
6960 }
6961
6962 /* For EVEX instructions i.vrex should become 0 after
6963 build_evex_prefix. For VEX instructions upper 16 registers
6964 aren't available, so VREX should be 0. */
6965 if (i.vrex)
6966 abort ();
6967 /* Now the VEX prefix. */
6968 p = frag_more (i.vex.length);
6969 for (j = 0; j < i.vex.length; j++)
6970 p[j] = i.vex.bytes[j];
6971 }
6972
6973 /* Now the opcode; be careful about word order here! */
6974 if (i.tm.opcode_length == 1)
6975 {
6976 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
6977 }
6978 else
6979 {
6980 switch (i.tm.opcode_length)
6981 {
6982 case 4:
6983 p = frag_more (4);
6984 *p++ = (i.tm.base_opcode >> 24) & 0xff;
6985 *p++ = (i.tm.base_opcode >> 16) & 0xff;
6986 break;
6987 case 3:
6988 p = frag_more (3);
6989 *p++ = (i.tm.base_opcode >> 16) & 0xff;
6990 break;
6991 case 2:
6992 p = frag_more (2);
6993 break;
6994 default:
6995 abort ();
6996 break;
6997 }
6998
6999 /* Put out high byte first: can't use md_number_to_chars! */
7000 *p++ = (i.tm.base_opcode >> 8) & 0xff;
7001 *p = i.tm.base_opcode & 0xff;
7002 }
7003
7004 /* Now the modrm byte and sib byte (if present). */
7005 if (i.tm.opcode_modifier.modrm)
7006 {
7007 FRAG_APPEND_1_CHAR ((i.rm.regmem << 0
7008 | i.rm.reg << 3
7009 | i.rm.mode << 6));
7010 /* If i.rm.regmem == ESP (4)
7011 && i.rm.mode != (Register mode)
7012 && not 16 bit
7013 ==> need second modrm byte. */
7014 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
7015 && i.rm.mode != 3
7016 && !(i.base_reg && i.base_reg->reg_type.bitfield.reg16))
7017 FRAG_APPEND_1_CHAR ((i.sib.base << 0
7018 | i.sib.index << 3
7019 | i.sib.scale << 6));
7020 }
7021
7022 if (i.disp_operands)
7023 output_disp (insn_start_frag, insn_start_off);
7024
7025 if (i.imm_operands)
7026 output_imm (insn_start_frag, insn_start_off);
7027 }
7028
7029 #ifdef DEBUG386
7030 if (flag_debug)
7031 {
7032 pi ("" /*line*/, &i);
7033 }
7034 #endif /* DEBUG386 */
7035 }
7036
7037 /* Return the size of the displacement operand N. */
7038
7039 static int
7040 disp_size (unsigned int n)
7041 {
7042 int size = 4;
7043
7044 /* Vec_Disp8 has to be 8bit. */
7045 if (i.types[n].bitfield.vec_disp8)
7046 size = 1;
7047 else if (i.types[n].bitfield.disp64)
7048 size = 8;
7049 else if (i.types[n].bitfield.disp8)
7050 size = 1;
7051 else if (i.types[n].bitfield.disp16)
7052 size = 2;
7053 return size;
7054 }
7055
7056 /* Return the size of the immediate operand N. */
7057
7058 static int
7059 imm_size (unsigned int n)
7060 {
7061 int size = 4;
7062 if (i.types[n].bitfield.imm64)
7063 size = 8;
7064 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
7065 size = 1;
7066 else if (i.types[n].bitfield.imm16)
7067 size = 2;
7068 return size;
7069 }
7070
7071 static void
7072 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
7073 {
7074 char *p;
7075 unsigned int n;
7076
7077 for (n = 0; n < i.operands; n++)
7078 {
7079 if (i.types[n].bitfield.vec_disp8
7080 || operand_type_check (i.types[n], disp))
7081 {
7082 if (i.op[n].disps->X_op == O_constant)
7083 {
7084 int size = disp_size (n);
7085 offsetT val = i.op[n].disps->X_add_number;
7086
7087 if (i.types[n].bitfield.vec_disp8)
7088 val >>= i.memshift;
7089 val = offset_in_range (val, size);
7090 p = frag_more (size);
7091 md_number_to_chars (p, val, size);
7092 }
7093 else
7094 {
7095 enum bfd_reloc_code_real reloc_type;
7096 int size = disp_size (n);
7097 int sign = i.types[n].bitfield.disp32s;
7098 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
7099
7100 /* We can't have 8 bit displacement here. */
7101 gas_assert (!i.types[n].bitfield.disp8);
7102
7103 /* The PC relative address is computed relative
7104 to the instruction boundary, so in case immediate
7105 fields follows, we need to adjust the value. */
7106 if (pcrel && i.imm_operands)
7107 {
7108 unsigned int n1;
7109 int sz = 0;
7110
7111 for (n1 = 0; n1 < i.operands; n1++)
7112 if (operand_type_check (i.types[n1], imm))
7113 {
7114 /* Only one immediate is allowed for PC
7115 relative address. */
7116 gas_assert (sz == 0);
7117 sz = imm_size (n1);
7118 i.op[n].disps->X_add_number -= sz;
7119 }
7120 /* We should find the immediate. */
7121 gas_assert (sz != 0);
7122 }
7123
7124 p = frag_more (size);
7125 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
7126 if (GOT_symbol
7127 && GOT_symbol == i.op[n].disps->X_add_symbol
7128 && (((reloc_type == BFD_RELOC_32
7129 || reloc_type == BFD_RELOC_X86_64_32S
7130 || (reloc_type == BFD_RELOC_64
7131 && object_64bit))
7132 && (i.op[n].disps->X_op == O_symbol
7133 || (i.op[n].disps->X_op == O_add
7134 && ((symbol_get_value_expression
7135 (i.op[n].disps->X_op_symbol)->X_op)
7136 == O_subtract))))
7137 || reloc_type == BFD_RELOC_32_PCREL))
7138 {
7139 offsetT add;
7140
7141 if (insn_start_frag == frag_now)
7142 add = (p - frag_now->fr_literal) - insn_start_off;
7143 else
7144 {
7145 fragS *fr;
7146
7147 add = insn_start_frag->fr_fix - insn_start_off;
7148 for (fr = insn_start_frag->fr_next;
7149 fr && fr != frag_now; fr = fr->fr_next)
7150 add += fr->fr_fix;
7151 add += p - frag_now->fr_literal;
7152 }
7153
7154 if (!object_64bit)
7155 {
7156 reloc_type = BFD_RELOC_386_GOTPC;
7157 i.op[n].imms->X_add_number += add;
7158 }
7159 else if (reloc_type == BFD_RELOC_64)
7160 reloc_type = BFD_RELOC_X86_64_GOTPC64;
7161 else
7162 /* Don't do the adjustment for x86-64, as there
7163 the pcrel addressing is relative to the _next_
7164 insn, and that is taken care of in other code. */
7165 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7166 }
7167 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
7168 i.op[n].disps, pcrel, reloc_type);
7169 }
7170 }
7171 }
7172 }
7173
7174 static void
7175 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
7176 {
7177 char *p;
7178 unsigned int n;
7179
7180 for (n = 0; n < i.operands; n++)
7181 {
7182 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
7183 if (i.rounding && (int) n == i.rounding->operand)
7184 continue;
7185
7186 if (operand_type_check (i.types[n], imm))
7187 {
7188 if (i.op[n].imms->X_op == O_constant)
7189 {
7190 int size = imm_size (n);
7191 offsetT val;
7192
7193 val = offset_in_range (i.op[n].imms->X_add_number,
7194 size);
7195 p = frag_more (size);
7196 md_number_to_chars (p, val, size);
7197 }
7198 else
7199 {
7200 /* Not absolute_section.
7201 Need a 32-bit fixup (don't support 8bit
7202 non-absolute imms). Try to support other
7203 sizes ... */
7204 enum bfd_reloc_code_real reloc_type;
7205 int size = imm_size (n);
7206 int sign;
7207
7208 if (i.types[n].bitfield.imm32s
7209 && (i.suffix == QWORD_MNEM_SUFFIX
7210 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
7211 sign = 1;
7212 else
7213 sign = 0;
7214
7215 p = frag_more (size);
7216 reloc_type = reloc (size, 0, sign, i.reloc[n]);
7217
7218 /* This is tough to explain. We end up with this one if we
7219 * have operands that look like
7220 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
7221 * obtain the absolute address of the GOT, and it is strongly
7222 * preferable from a performance point of view to avoid using
7223 * a runtime relocation for this. The actual sequence of
7224 * instructions often look something like:
7225 *
7226 * call .L66
7227 * .L66:
7228 * popl %ebx
7229 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
7230 *
7231 * The call and pop essentially return the absolute address
7232 * of the label .L66 and store it in %ebx. The linker itself
7233 * will ultimately change the first operand of the addl so
7234 * that %ebx points to the GOT, but to keep things simple, the
7235 * .o file must have this operand set so that it generates not
7236 * the absolute address of .L66, but the absolute address of
7237 * itself. This allows the linker itself simply treat a GOTPC
7238 * relocation as asking for a pcrel offset to the GOT to be
7239 * added in, and the addend of the relocation is stored in the
7240 * operand field for the instruction itself.
7241 *
7242 * Our job here is to fix the operand so that it would add
7243 * the correct offset so that %ebx would point to itself. The
7244 * thing that is tricky is that .-.L66 will point to the
7245 * beginning of the instruction, so we need to further modify
7246 * the operand so that it will point to itself. There are
7247 * other cases where you have something like:
7248 *
7249 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
7250 *
7251 * and here no correction would be required. Internally in
7252 * the assembler we treat operands of this form as not being
7253 * pcrel since the '.' is explicitly mentioned, and I wonder
7254 * whether it would simplify matters to do it this way. Who
7255 * knows. In earlier versions of the PIC patches, the
7256 * pcrel_adjust field was used to store the correction, but
7257 * since the expression is not pcrel, I felt it would be
7258 * confusing to do it this way. */
7259
7260 if ((reloc_type == BFD_RELOC_32
7261 || reloc_type == BFD_RELOC_X86_64_32S
7262 || reloc_type == BFD_RELOC_64)
7263 && GOT_symbol
7264 && GOT_symbol == i.op[n].imms->X_add_symbol
7265 && (i.op[n].imms->X_op == O_symbol
7266 || (i.op[n].imms->X_op == O_add
7267 && ((symbol_get_value_expression
7268 (i.op[n].imms->X_op_symbol)->X_op)
7269 == O_subtract))))
7270 {
7271 offsetT add;
7272
7273 if (insn_start_frag == frag_now)
7274 add = (p - frag_now->fr_literal) - insn_start_off;
7275 else
7276 {
7277 fragS *fr;
7278
7279 add = insn_start_frag->fr_fix - insn_start_off;
7280 for (fr = insn_start_frag->fr_next;
7281 fr && fr != frag_now; fr = fr->fr_next)
7282 add += fr->fr_fix;
7283 add += p - frag_now->fr_literal;
7284 }
7285
7286 if (!object_64bit)
7287 reloc_type = BFD_RELOC_386_GOTPC;
7288 else if (size == 4)
7289 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7290 else if (size == 8)
7291 reloc_type = BFD_RELOC_X86_64_GOTPC64;
7292 i.op[n].imms->X_add_number += add;
7293 }
7294 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
7295 i.op[n].imms, 0, reloc_type);
7296 }
7297 }
7298 }
7299 }
7300 \f
7301 /* x86_cons_fix_new is called via the expression parsing code when a
7302 reloc is needed. We use this hook to get the correct .got reloc. */
7303 static int cons_sign = -1;
7304
7305 void
7306 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
7307 expressionS *exp, bfd_reloc_code_real_type r)
7308 {
7309 r = reloc (len, 0, cons_sign, r);
7310
7311 #ifdef TE_PE
7312 if (exp->X_op == O_secrel)
7313 {
7314 exp->X_op = O_symbol;
7315 r = BFD_RELOC_32_SECREL;
7316 }
7317 #endif
7318
7319 fix_new_exp (frag, off, len, exp, 0, r);
7320 }
7321
7322 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
7323 purpose of the `.dc.a' internal pseudo-op. */
7324
7325 int
7326 x86_address_bytes (void)
7327 {
7328 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
7329 return 4;
7330 return stdoutput->arch_info->bits_per_address / 8;
7331 }
7332
7333 #if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
7334 || defined (LEX_AT)
7335 # define lex_got(reloc, adjust, types) NULL
7336 #else
7337 /* Parse operands of the form
7338 <symbol>@GOTOFF+<nnn>
7339 and similar .plt or .got references.
7340
7341 If we find one, set up the correct relocation in RELOC and copy the
7342 input string, minus the `@GOTOFF' into a malloc'd buffer for
7343 parsing by the calling routine. Return this buffer, and if ADJUST
7344 is non-null set it to the length of the string we removed from the
7345 input line. Otherwise return NULL. */
7346 static char *
7347 lex_got (enum bfd_reloc_code_real *rel,
7348 int *adjust,
7349 i386_operand_type *types)
7350 {
7351 /* Some of the relocations depend on the size of what field is to
7352 be relocated. But in our callers i386_immediate and i386_displacement
7353 we don't yet know the operand size (this will be set by insn
7354 matching). Hence we record the word32 relocation here,
7355 and adjust the reloc according to the real size in reloc(). */
7356 static const struct {
7357 const char *str;
7358 int len;
7359 const enum bfd_reloc_code_real rel[2];
7360 const i386_operand_type types64;
7361 } gotrel[] = {
7362 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7363 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
7364 BFD_RELOC_SIZE32 },
7365 OPERAND_TYPE_IMM32_64 },
7366 #endif
7367 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
7368 BFD_RELOC_X86_64_PLTOFF64 },
7369 OPERAND_TYPE_IMM64 },
7370 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
7371 BFD_RELOC_X86_64_PLT32 },
7372 OPERAND_TYPE_IMM32_32S_DISP32 },
7373 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
7374 BFD_RELOC_X86_64_GOTPLT64 },
7375 OPERAND_TYPE_IMM64_DISP64 },
7376 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
7377 BFD_RELOC_X86_64_GOTOFF64 },
7378 OPERAND_TYPE_IMM64_DISP64 },
7379 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
7380 BFD_RELOC_X86_64_GOTPCREL },
7381 OPERAND_TYPE_IMM32_32S_DISP32 },
7382 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
7383 BFD_RELOC_X86_64_TLSGD },
7384 OPERAND_TYPE_IMM32_32S_DISP32 },
7385 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
7386 _dummy_first_bfd_reloc_code_real },
7387 OPERAND_TYPE_NONE },
7388 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
7389 BFD_RELOC_X86_64_TLSLD },
7390 OPERAND_TYPE_IMM32_32S_DISP32 },
7391 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
7392 BFD_RELOC_X86_64_GOTTPOFF },
7393 OPERAND_TYPE_IMM32_32S_DISP32 },
7394 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
7395 BFD_RELOC_X86_64_TPOFF32 },
7396 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
7397 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
7398 _dummy_first_bfd_reloc_code_real },
7399 OPERAND_TYPE_NONE },
7400 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
7401 BFD_RELOC_X86_64_DTPOFF32 },
7402 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
7403 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
7404 _dummy_first_bfd_reloc_code_real },
7405 OPERAND_TYPE_NONE },
7406 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
7407 _dummy_first_bfd_reloc_code_real },
7408 OPERAND_TYPE_NONE },
7409 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
7410 BFD_RELOC_X86_64_GOT32 },
7411 OPERAND_TYPE_IMM32_32S_64_DISP32 },
7412 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
7413 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
7414 OPERAND_TYPE_IMM32_32S_DISP32 },
7415 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
7416 BFD_RELOC_X86_64_TLSDESC_CALL },
7417 OPERAND_TYPE_IMM32_32S_DISP32 },
7418 };
7419 char *cp;
7420 unsigned int j;
7421
7422 #if defined (OBJ_MAYBE_ELF)
7423 if (!IS_ELF)
7424 return NULL;
7425 #endif
7426
7427 for (cp = input_line_pointer; *cp != '@'; cp++)
7428 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
7429 return NULL;
7430
7431 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
7432 {
7433 int len = gotrel[j].len;
7434 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
7435 {
7436 if (gotrel[j].rel[object_64bit] != 0)
7437 {
7438 int first, second;
7439 char *tmpbuf, *past_reloc;
7440
7441 *rel = gotrel[j].rel[object_64bit];
7442
7443 if (types)
7444 {
7445 if (flag_code != CODE_64BIT)
7446 {
7447 types->bitfield.imm32 = 1;
7448 types->bitfield.disp32 = 1;
7449 }
7450 else
7451 *types = gotrel[j].types64;
7452 }
7453
7454 if (j != 0 && GOT_symbol == NULL)
7455 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
7456
7457 /* The length of the first part of our input line. */
7458 first = cp - input_line_pointer;
7459
7460 /* The second part goes from after the reloc token until
7461 (and including) an end_of_line char or comma. */
7462 past_reloc = cp + 1 + len;
7463 cp = past_reloc;
7464 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
7465 ++cp;
7466 second = cp + 1 - past_reloc;
7467
7468 /* Allocate and copy string. The trailing NUL shouldn't
7469 be necessary, but be safe. */
7470 tmpbuf = (char *) xmalloc (first + second + 2);
7471 memcpy (tmpbuf, input_line_pointer, first);
7472 if (second != 0 && *past_reloc != ' ')
7473 /* Replace the relocation token with ' ', so that
7474 errors like foo@GOTOFF1 will be detected. */
7475 tmpbuf[first++] = ' ';
7476 else
7477 /* Increment length by 1 if the relocation token is
7478 removed. */
7479 len++;
7480 if (adjust)
7481 *adjust = len;
7482 memcpy (tmpbuf + first, past_reloc, second);
7483 tmpbuf[first + second] = '\0';
7484 return tmpbuf;
7485 }
7486
7487 as_bad (_("@%s reloc is not supported with %d-bit output format"),
7488 gotrel[j].str, 1 << (5 + object_64bit));
7489 return NULL;
7490 }
7491 }
7492
7493 /* Might be a symbol version string. Don't as_bad here. */
7494 return NULL;
7495 }
7496 #endif
7497
7498 #ifdef TE_PE
7499 #ifdef lex_got
7500 #undef lex_got
7501 #endif
7502 /* Parse operands of the form
7503 <symbol>@SECREL32+<nnn>
7504
7505 If we find one, set up the correct relocation in RELOC and copy the
7506 input string, minus the `@SECREL32' into a malloc'd buffer for
7507 parsing by the calling routine. Return this buffer, and if ADJUST
7508 is non-null set it to the length of the string we removed from the
7509 input line. Otherwise return NULL.
7510
7511 This function is copied from the ELF version above adjusted for PE targets. */
7512
7513 static char *
7514 lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
7515 int *adjust ATTRIBUTE_UNUSED,
7516 i386_operand_type *types)
7517 {
7518 static const struct
7519 {
7520 const char *str;
7521 int len;
7522 const enum bfd_reloc_code_real rel[2];
7523 const i386_operand_type types64;
7524 }
7525 gotrel[] =
7526 {
7527 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
7528 BFD_RELOC_32_SECREL },
7529 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
7530 };
7531
7532 char *cp;
7533 unsigned j;
7534
7535 for (cp = input_line_pointer; *cp != '@'; cp++)
7536 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
7537 return NULL;
7538
7539 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
7540 {
7541 int len = gotrel[j].len;
7542
7543 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
7544 {
7545 if (gotrel[j].rel[object_64bit] != 0)
7546 {
7547 int first, second;
7548 char *tmpbuf, *past_reloc;
7549
7550 *rel = gotrel[j].rel[object_64bit];
7551 if (adjust)
7552 *adjust = len;
7553
7554 if (types)
7555 {
7556 if (flag_code != CODE_64BIT)
7557 {
7558 types->bitfield.imm32 = 1;
7559 types->bitfield.disp32 = 1;
7560 }
7561 else
7562 *types = gotrel[j].types64;
7563 }
7564
7565 /* The length of the first part of our input line. */
7566 first = cp - input_line_pointer;
7567
7568 /* The second part goes from after the reloc token until
7569 (and including) an end_of_line char or comma. */
7570 past_reloc = cp + 1 + len;
7571 cp = past_reloc;
7572 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
7573 ++cp;
7574 second = cp + 1 - past_reloc;
7575
7576 /* Allocate and copy string. The trailing NUL shouldn't
7577 be necessary, but be safe. */
7578 tmpbuf = (char *) xmalloc (first + second + 2);
7579 memcpy (tmpbuf, input_line_pointer, first);
7580 if (second != 0 && *past_reloc != ' ')
7581 /* Replace the relocation token with ' ', so that
7582 errors like foo@SECLREL321 will be detected. */
7583 tmpbuf[first++] = ' ';
7584 memcpy (tmpbuf + first, past_reloc, second);
7585 tmpbuf[first + second] = '\0';
7586 return tmpbuf;
7587 }
7588
7589 as_bad (_("@%s reloc is not supported with %d-bit output format"),
7590 gotrel[j].str, 1 << (5 + object_64bit));
7591 return NULL;
7592 }
7593 }
7594
7595 /* Might be a symbol version string. Don't as_bad here. */
7596 return NULL;
7597 }
7598
7599 #endif /* TE_PE */
7600
7601 bfd_reloc_code_real_type
7602 x86_cons (expressionS *exp, int size)
7603 {
7604 bfd_reloc_code_real_type got_reloc = NO_RELOC;
7605
7606 intel_syntax = -intel_syntax;
7607
7608 exp->X_md = 0;
7609 if (size == 4 || (object_64bit && size == 8))
7610 {
7611 /* Handle @GOTOFF and the like in an expression. */
7612 char *save;
7613 char *gotfree_input_line;
7614 int adjust = 0;
7615
7616 save = input_line_pointer;
7617 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
7618 if (gotfree_input_line)
7619 input_line_pointer = gotfree_input_line;
7620
7621 expression (exp);
7622
7623 if (gotfree_input_line)
7624 {
7625 /* expression () has merrily parsed up to the end of line,
7626 or a comma - in the wrong buffer. Transfer how far
7627 input_line_pointer has moved to the right buffer. */
7628 input_line_pointer = (save
7629 + (input_line_pointer - gotfree_input_line)
7630 + adjust);
7631 free (gotfree_input_line);
7632 if (exp->X_op == O_constant
7633 || exp->X_op == O_absent
7634 || exp->X_op == O_illegal
7635 || exp->X_op == O_register
7636 || exp->X_op == O_big)
7637 {
7638 char c = *input_line_pointer;
7639 *input_line_pointer = 0;
7640 as_bad (_("missing or invalid expression `%s'"), save);
7641 *input_line_pointer = c;
7642 }
7643 }
7644 }
7645 else
7646 expression (exp);
7647
7648 intel_syntax = -intel_syntax;
7649
7650 if (intel_syntax)
7651 i386_intel_simplify (exp);
7652
7653 return got_reloc;
7654 }
7655
7656 static void
7657 signed_cons (int size)
7658 {
7659 if (flag_code == CODE_64BIT)
7660 cons_sign = 1;
7661 cons (size);
7662 cons_sign = -1;
7663 }
7664
7665 #ifdef TE_PE
7666 static void
7667 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
7668 {
7669 expressionS exp;
7670
7671 do
7672 {
7673 expression (&exp);
7674 if (exp.X_op == O_symbol)
7675 exp.X_op = O_secrel;
7676
7677 emit_expr (&exp, 4);
7678 }
7679 while (*input_line_pointer++ == ',');
7680
7681 input_line_pointer--;
7682 demand_empty_rest_of_line ();
7683 }
7684 #endif
7685
7686 /* Handle Vector operations. */
7687
7688 static char *
7689 check_VecOperations (char *op_string, char *op_end)
7690 {
7691 const reg_entry *mask;
7692 const char *saved;
7693 char *end_op;
7694
7695 while (*op_string
7696 && (op_end == NULL || op_string < op_end))
7697 {
7698 saved = op_string;
7699 if (*op_string == '{')
7700 {
7701 op_string++;
7702
7703 /* Check broadcasts. */
7704 if (strncmp (op_string, "1to", 3) == 0)
7705 {
7706 int bcst_type;
7707
7708 if (i.broadcast)
7709 goto duplicated_vec_op;
7710
7711 op_string += 3;
7712 if (*op_string == '8')
7713 bcst_type = BROADCAST_1TO8;
7714 else if (*op_string == '4')
7715 bcst_type = BROADCAST_1TO4;
7716 else if (*op_string == '2')
7717 bcst_type = BROADCAST_1TO2;
7718 else if (*op_string == '1'
7719 && *(op_string+1) == '6')
7720 {
7721 bcst_type = BROADCAST_1TO16;
7722 op_string++;
7723 }
7724 else
7725 {
7726 as_bad (_("Unsupported broadcast: `%s'"), saved);
7727 return NULL;
7728 }
7729 op_string++;
7730
7731 broadcast_op.type = bcst_type;
7732 broadcast_op.operand = this_operand;
7733 i.broadcast = &broadcast_op;
7734 }
7735 /* Check masking operation. */
7736 else if ((mask = parse_register (op_string, &end_op)) != NULL)
7737 {
7738 /* k0 can't be used for write mask. */
7739 if (mask->reg_num == 0)
7740 {
7741 as_bad (_("`%s' can't be used for write mask"),
7742 op_string);
7743 return NULL;
7744 }
7745
7746 if (!i.mask)
7747 {
7748 mask_op.mask = mask;
7749 mask_op.zeroing = 0;
7750 mask_op.operand = this_operand;
7751 i.mask = &mask_op;
7752 }
7753 else
7754 {
7755 if (i.mask->mask)
7756 goto duplicated_vec_op;
7757
7758 i.mask->mask = mask;
7759
7760 /* Only "{z}" is allowed here. No need to check
7761 zeroing mask explicitly. */
7762 if (i.mask->operand != this_operand)
7763 {
7764 as_bad (_("invalid write mask `%s'"), saved);
7765 return NULL;
7766 }
7767 }
7768
7769 op_string = end_op;
7770 }
7771 /* Check zeroing-flag for masking operation. */
7772 else if (*op_string == 'z')
7773 {
7774 if (!i.mask)
7775 {
7776 mask_op.mask = NULL;
7777 mask_op.zeroing = 1;
7778 mask_op.operand = this_operand;
7779 i.mask = &mask_op;
7780 }
7781 else
7782 {
7783 if (i.mask->zeroing)
7784 {
7785 duplicated_vec_op:
7786 as_bad (_("duplicated `%s'"), saved);
7787 return NULL;
7788 }
7789
7790 i.mask->zeroing = 1;
7791
7792 /* Only "{%k}" is allowed here. No need to check mask
7793 register explicitly. */
7794 if (i.mask->operand != this_operand)
7795 {
7796 as_bad (_("invalid zeroing-masking `%s'"),
7797 saved);
7798 return NULL;
7799 }
7800 }
7801
7802 op_string++;
7803 }
7804 else
7805 goto unknown_vec_op;
7806
7807 if (*op_string != '}')
7808 {
7809 as_bad (_("missing `}' in `%s'"), saved);
7810 return NULL;
7811 }
7812 op_string++;
7813 continue;
7814 }
7815 unknown_vec_op:
7816 /* We don't know this one. */
7817 as_bad (_("unknown vector operation: `%s'"), saved);
7818 return NULL;
7819 }
7820
7821 return op_string;
7822 }
7823
7824 static int
7825 i386_immediate (char *imm_start)
7826 {
7827 char *save_input_line_pointer;
7828 char *gotfree_input_line;
7829 segT exp_seg = 0;
7830 expressionS *exp;
7831 i386_operand_type types;
7832
7833 operand_type_set (&types, ~0);
7834
7835 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
7836 {
7837 as_bad (_("at most %d immediate operands are allowed"),
7838 MAX_IMMEDIATE_OPERANDS);
7839 return 0;
7840 }
7841
7842 exp = &im_expressions[i.imm_operands++];
7843 i.op[this_operand].imms = exp;
7844
7845 if (is_space_char (*imm_start))
7846 ++imm_start;
7847
7848 save_input_line_pointer = input_line_pointer;
7849 input_line_pointer = imm_start;
7850
7851 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
7852 if (gotfree_input_line)
7853 input_line_pointer = gotfree_input_line;
7854
7855 exp_seg = expression (exp);
7856
7857 SKIP_WHITESPACE ();
7858
7859 /* Handle vector operations. */
7860 if (*input_line_pointer == '{')
7861 {
7862 input_line_pointer = check_VecOperations (input_line_pointer,
7863 NULL);
7864 if (input_line_pointer == NULL)
7865 return 0;
7866 }
7867
7868 if (*input_line_pointer)
7869 as_bad (_("junk `%s' after expression"), input_line_pointer);
7870
7871 input_line_pointer = save_input_line_pointer;
7872 if (gotfree_input_line)
7873 {
7874 free (gotfree_input_line);
7875
7876 if (exp->X_op == O_constant || exp->X_op == O_register)
7877 exp->X_op = O_illegal;
7878 }
7879
7880 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
7881 }
7882
7883 static int
7884 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
7885 i386_operand_type types, const char *imm_start)
7886 {
7887 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
7888 {
7889 if (imm_start)
7890 as_bad (_("missing or invalid immediate expression `%s'"),
7891 imm_start);
7892 return 0;
7893 }
7894 else if (exp->X_op == O_constant)
7895 {
7896 /* Size it properly later. */
7897 i.types[this_operand].bitfield.imm64 = 1;
7898 /* If not 64bit, sign extend val. */
7899 if (flag_code != CODE_64BIT
7900 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
7901 exp->X_add_number
7902 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
7903 }
7904 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
7905 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
7906 && exp_seg != absolute_section
7907 && exp_seg != text_section
7908 && exp_seg != data_section
7909 && exp_seg != bss_section
7910 && exp_seg != undefined_section
7911 && !bfd_is_com_section (exp_seg))
7912 {
7913 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
7914 return 0;
7915 }
7916 #endif
7917 else if (!intel_syntax && exp_seg == reg_section)
7918 {
7919 if (imm_start)
7920 as_bad (_("illegal immediate register operand %s"), imm_start);
7921 return 0;
7922 }
7923 else
7924 {
7925 /* This is an address. The size of the address will be
7926 determined later, depending on destination register,
7927 suffix, or the default for the section. */
7928 i.types[this_operand].bitfield.imm8 = 1;
7929 i.types[this_operand].bitfield.imm16 = 1;
7930 i.types[this_operand].bitfield.imm32 = 1;
7931 i.types[this_operand].bitfield.imm32s = 1;
7932 i.types[this_operand].bitfield.imm64 = 1;
7933 i.types[this_operand] = operand_type_and (i.types[this_operand],
7934 types);
7935 }
7936
7937 return 1;
7938 }
7939
7940 static char *
7941 i386_scale (char *scale)
7942 {
7943 offsetT val;
7944 char *save = input_line_pointer;
7945
7946 input_line_pointer = scale;
7947 val = get_absolute_expression ();
7948
7949 switch (val)
7950 {
7951 case 1:
7952 i.log2_scale_factor = 0;
7953 break;
7954 case 2:
7955 i.log2_scale_factor = 1;
7956 break;
7957 case 4:
7958 i.log2_scale_factor = 2;
7959 break;
7960 case 8:
7961 i.log2_scale_factor = 3;
7962 break;
7963 default:
7964 {
7965 char sep = *input_line_pointer;
7966
7967 *input_line_pointer = '\0';
7968 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
7969 scale);
7970 *input_line_pointer = sep;
7971 input_line_pointer = save;
7972 return NULL;
7973 }
7974 }
7975 if (i.log2_scale_factor != 0 && i.index_reg == 0)
7976 {
7977 as_warn (_("scale factor of %d without an index register"),
7978 1 << i.log2_scale_factor);
7979 i.log2_scale_factor = 0;
7980 }
7981 scale = input_line_pointer;
7982 input_line_pointer = save;
7983 return scale;
7984 }
7985
7986 static int
7987 i386_displacement (char *disp_start, char *disp_end)
7988 {
7989 expressionS *exp;
7990 segT exp_seg = 0;
7991 char *save_input_line_pointer;
7992 char *gotfree_input_line;
7993 int override;
7994 i386_operand_type bigdisp, types = anydisp;
7995 int ret;
7996
7997 if (i.disp_operands == MAX_MEMORY_OPERANDS)
7998 {
7999 as_bad (_("at most %d displacement operands are allowed"),
8000 MAX_MEMORY_OPERANDS);
8001 return 0;
8002 }
8003
8004 operand_type_set (&bigdisp, 0);
8005 if ((i.types[this_operand].bitfield.jumpabsolute)
8006 || (!current_templates->start->opcode_modifier.jump
8007 && !current_templates->start->opcode_modifier.jumpdword))
8008 {
8009 bigdisp.bitfield.disp32 = 1;
8010 override = (i.prefix[ADDR_PREFIX] != 0);
8011 if (flag_code == CODE_64BIT)
8012 {
8013 if (!override)
8014 {
8015 bigdisp.bitfield.disp32s = 1;
8016 bigdisp.bitfield.disp64 = 1;
8017 }
8018 }
8019 else if ((flag_code == CODE_16BIT) ^ override)
8020 {
8021 bigdisp.bitfield.disp32 = 0;
8022 bigdisp.bitfield.disp16 = 1;
8023 }
8024 }
8025 else
8026 {
8027 /* For PC-relative branches, the width of the displacement
8028 is dependent upon data size, not address size. */
8029 override = (i.prefix[DATA_PREFIX] != 0);
8030 if (flag_code == CODE_64BIT)
8031 {
8032 if (override || i.suffix == WORD_MNEM_SUFFIX)
8033 bigdisp.bitfield.disp16 = 1;
8034 else
8035 {
8036 bigdisp.bitfield.disp32 = 1;
8037 bigdisp.bitfield.disp32s = 1;
8038 }
8039 }
8040 else
8041 {
8042 if (!override)
8043 override = (i.suffix == (flag_code != CODE_16BIT
8044 ? WORD_MNEM_SUFFIX
8045 : LONG_MNEM_SUFFIX));
8046 bigdisp.bitfield.disp32 = 1;
8047 if ((flag_code == CODE_16BIT) ^ override)
8048 {
8049 bigdisp.bitfield.disp32 = 0;
8050 bigdisp.bitfield.disp16 = 1;
8051 }
8052 }
8053 }
8054 i.types[this_operand] = operand_type_or (i.types[this_operand],
8055 bigdisp);
8056
8057 exp = &disp_expressions[i.disp_operands];
8058 i.op[this_operand].disps = exp;
8059 i.disp_operands++;
8060 save_input_line_pointer = input_line_pointer;
8061 input_line_pointer = disp_start;
8062 END_STRING_AND_SAVE (disp_end);
8063
8064 #ifndef GCC_ASM_O_HACK
8065 #define GCC_ASM_O_HACK 0
8066 #endif
8067 #if GCC_ASM_O_HACK
8068 END_STRING_AND_SAVE (disp_end + 1);
8069 if (i.types[this_operand].bitfield.baseIndex
8070 && displacement_string_end[-1] == '+')
8071 {
8072 /* This hack is to avoid a warning when using the "o"
8073 constraint within gcc asm statements.
8074 For instance:
8075
8076 #define _set_tssldt_desc(n,addr,limit,type) \
8077 __asm__ __volatile__ ( \
8078 "movw %w2,%0\n\t" \
8079 "movw %w1,2+%0\n\t" \
8080 "rorl $16,%1\n\t" \
8081 "movb %b1,4+%0\n\t" \
8082 "movb %4,5+%0\n\t" \
8083 "movb $0,6+%0\n\t" \
8084 "movb %h1,7+%0\n\t" \
8085 "rorl $16,%1" \
8086 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
8087
8088 This works great except that the output assembler ends
8089 up looking a bit weird if it turns out that there is
8090 no offset. You end up producing code that looks like:
8091
8092 #APP
8093 movw $235,(%eax)
8094 movw %dx,2+(%eax)
8095 rorl $16,%edx
8096 movb %dl,4+(%eax)
8097 movb $137,5+(%eax)
8098 movb $0,6+(%eax)
8099 movb %dh,7+(%eax)
8100 rorl $16,%edx
8101 #NO_APP
8102
8103 So here we provide the missing zero. */
8104
8105 *displacement_string_end = '0';
8106 }
8107 #endif
8108 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
8109 if (gotfree_input_line)
8110 input_line_pointer = gotfree_input_line;
8111
8112 exp_seg = expression (exp);
8113
8114 SKIP_WHITESPACE ();
8115 if (*input_line_pointer)
8116 as_bad (_("junk `%s' after expression"), input_line_pointer);
8117 #if GCC_ASM_O_HACK
8118 RESTORE_END_STRING (disp_end + 1);
8119 #endif
8120 input_line_pointer = save_input_line_pointer;
8121 if (gotfree_input_line)
8122 {
8123 free (gotfree_input_line);
8124
8125 if (exp->X_op == O_constant || exp->X_op == O_register)
8126 exp->X_op = O_illegal;
8127 }
8128
8129 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
8130
8131 RESTORE_END_STRING (disp_end);
8132
8133 return ret;
8134 }
8135
8136 static int
8137 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
8138 i386_operand_type types, const char *disp_start)
8139 {
8140 i386_operand_type bigdisp;
8141 int ret = 1;
8142
8143 /* We do this to make sure that the section symbol is in
8144 the symbol table. We will ultimately change the relocation
8145 to be relative to the beginning of the section. */
8146 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
8147 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
8148 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
8149 {
8150 if (exp->X_op != O_symbol)
8151 goto inv_disp;
8152
8153 if (S_IS_LOCAL (exp->X_add_symbol)
8154 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
8155 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
8156 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
8157 exp->X_op = O_subtract;
8158 exp->X_op_symbol = GOT_symbol;
8159 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
8160 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
8161 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
8162 i.reloc[this_operand] = BFD_RELOC_64;
8163 else
8164 i.reloc[this_operand] = BFD_RELOC_32;
8165 }
8166
8167 else if (exp->X_op == O_absent
8168 || exp->X_op == O_illegal
8169 || exp->X_op == O_big)
8170 {
8171 inv_disp:
8172 as_bad (_("missing or invalid displacement expression `%s'"),
8173 disp_start);
8174 ret = 0;
8175 }
8176
8177 else if (flag_code == CODE_64BIT
8178 && !i.prefix[ADDR_PREFIX]
8179 && exp->X_op == O_constant)
8180 {
8181 /* Since displacement is signed extended to 64bit, don't allow
8182 disp32 and turn off disp32s if they are out of range. */
8183 i.types[this_operand].bitfield.disp32 = 0;
8184 if (!fits_in_signed_long (exp->X_add_number))
8185 {
8186 i.types[this_operand].bitfield.disp32s = 0;
8187 if (i.types[this_operand].bitfield.baseindex)
8188 {
8189 as_bad (_("0x%lx out range of signed 32bit displacement"),
8190 (long) exp->X_add_number);
8191 ret = 0;
8192 }
8193 }
8194 }
8195
8196 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
8197 else if (exp->X_op != O_constant
8198 && OUTPUT_FLAVOR == bfd_target_aout_flavour
8199 && exp_seg != absolute_section
8200 && exp_seg != text_section
8201 && exp_seg != data_section
8202 && exp_seg != bss_section
8203 && exp_seg != undefined_section
8204 && !bfd_is_com_section (exp_seg))
8205 {
8206 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
8207 ret = 0;
8208 }
8209 #endif
8210
8211 /* Check if this is a displacement only operand. */
8212 bigdisp = i.types[this_operand];
8213 bigdisp.bitfield.disp8 = 0;
8214 bigdisp.bitfield.disp16 = 0;
8215 bigdisp.bitfield.disp32 = 0;
8216 bigdisp.bitfield.disp32s = 0;
8217 bigdisp.bitfield.disp64 = 0;
8218 if (operand_type_all_zero (&bigdisp))
8219 i.types[this_operand] = operand_type_and (i.types[this_operand],
8220 types);
8221
8222 return ret;
8223 }
8224
8225 /* Make sure the memory operand we've been dealt is valid.
8226 Return 1 on success, 0 on a failure. */
8227
8228 static int
8229 i386_index_check (const char *operand_string)
8230 {
8231 const char *kind = "base/index";
8232 enum flag_code addr_mode;
8233
8234 if (i.prefix[ADDR_PREFIX])
8235 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
8236 else
8237 {
8238 addr_mode = flag_code;
8239
8240 #if INFER_ADDR_PREFIX
8241 if (i.mem_operands == 0)
8242 {
8243 /* Infer address prefix from the first memory operand. */
8244 const reg_entry *addr_reg = i.base_reg;
8245
8246 if (addr_reg == NULL)
8247 addr_reg = i.index_reg;
8248
8249 if (addr_reg)
8250 {
8251 if (addr_reg->reg_num == RegEip
8252 || addr_reg->reg_num == RegEiz
8253 || addr_reg->reg_type.bitfield.reg32)
8254 addr_mode = CODE_32BIT;
8255 else if (flag_code != CODE_64BIT
8256 && addr_reg->reg_type.bitfield.reg16)
8257 addr_mode = CODE_16BIT;
8258
8259 if (addr_mode != flag_code)
8260 {
8261 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
8262 i.prefixes += 1;
8263 /* Change the size of any displacement too. At most one
8264 of Disp16 or Disp32 is set.
8265 FIXME. There doesn't seem to be any real need for
8266 separate Disp16 and Disp32 flags. The same goes for
8267 Imm16 and Imm32. Removing them would probably clean
8268 up the code quite a lot. */
8269 if (flag_code != CODE_64BIT
8270 && (i.types[this_operand].bitfield.disp16
8271 || i.types[this_operand].bitfield.disp32))
8272 i.types[this_operand]
8273 = operand_type_xor (i.types[this_operand], disp16_32);
8274 }
8275 }
8276 }
8277 #endif
8278 }
8279
8280 if (current_templates->start->opcode_modifier.isstring
8281 && !current_templates->start->opcode_modifier.immext
8282 && (current_templates->end[-1].opcode_modifier.isstring
8283 || i.mem_operands))
8284 {
8285 /* Memory operands of string insns are special in that they only allow
8286 a single register (rDI, rSI, or rBX) as their memory address. */
8287 const reg_entry *expected_reg;
8288 static const char *di_si[][2] =
8289 {
8290 { "esi", "edi" },
8291 { "si", "di" },
8292 { "rsi", "rdi" }
8293 };
8294 static const char *bx[] = { "ebx", "bx", "rbx" };
8295
8296 kind = "string address";
8297
8298 if (current_templates->start->opcode_modifier.w)
8299 {
8300 i386_operand_type type = current_templates->end[-1].operand_types[0];
8301
8302 if (!type.bitfield.baseindex
8303 || ((!i.mem_operands != !intel_syntax)
8304 && current_templates->end[-1].operand_types[1]
8305 .bitfield.baseindex))
8306 type = current_templates->end[-1].operand_types[1];
8307 expected_reg = hash_find (reg_hash,
8308 di_si[addr_mode][type.bitfield.esseg]);
8309
8310 }
8311 else
8312 expected_reg = hash_find (reg_hash, bx[addr_mode]);
8313
8314 if (i.base_reg != expected_reg
8315 || i.index_reg
8316 || operand_type_check (i.types[this_operand], disp))
8317 {
8318 /* The second memory operand must have the same size as
8319 the first one. */
8320 if (i.mem_operands
8321 && i.base_reg
8322 && !((addr_mode == CODE_64BIT
8323 && i.base_reg->reg_type.bitfield.reg64)
8324 || (addr_mode == CODE_32BIT
8325 ? i.base_reg->reg_type.bitfield.reg32
8326 : i.base_reg->reg_type.bitfield.reg16)))
8327 goto bad_address;
8328
8329 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
8330 operand_string,
8331 intel_syntax ? '[' : '(',
8332 register_prefix,
8333 expected_reg->reg_name,
8334 intel_syntax ? ']' : ')');
8335 return 1;
8336 }
8337 else
8338 return 1;
8339
8340 bad_address:
8341 as_bad (_("`%s' is not a valid %s expression"),
8342 operand_string, kind);
8343 return 0;
8344 }
8345 else
8346 {
8347 if (addr_mode != CODE_16BIT)
8348 {
8349 /* 32-bit/64-bit checks. */
8350 if ((i.base_reg
8351 && (addr_mode == CODE_64BIT
8352 ? !i.base_reg->reg_type.bitfield.reg64
8353 : !i.base_reg->reg_type.bitfield.reg32)
8354 && (i.index_reg
8355 || (i.base_reg->reg_num
8356 != (addr_mode == CODE_64BIT ? RegRip : RegEip))))
8357 || (i.index_reg
8358 && !i.index_reg->reg_type.bitfield.regxmm
8359 && !i.index_reg->reg_type.bitfield.regymm
8360 && !i.index_reg->reg_type.bitfield.regzmm
8361 && ((addr_mode == CODE_64BIT
8362 ? !(i.index_reg->reg_type.bitfield.reg64
8363 || i.index_reg->reg_num == RegRiz)
8364 : !(i.index_reg->reg_type.bitfield.reg32
8365 || i.index_reg->reg_num == RegEiz))
8366 || !i.index_reg->reg_type.bitfield.baseindex)))
8367 goto bad_address;
8368 }
8369 else
8370 {
8371 /* 16-bit checks. */
8372 if ((i.base_reg
8373 && (!i.base_reg->reg_type.bitfield.reg16
8374 || !i.base_reg->reg_type.bitfield.baseindex))
8375 || (i.index_reg
8376 && (!i.index_reg->reg_type.bitfield.reg16
8377 || !i.index_reg->reg_type.bitfield.baseindex
8378 || !(i.base_reg
8379 && i.base_reg->reg_num < 6
8380 && i.index_reg->reg_num >= 6
8381 && i.log2_scale_factor == 0))))
8382 goto bad_address;
8383 }
8384 }
8385 return 1;
8386 }
8387
8388 /* Handle vector immediates. */
8389
8390 static int
8391 RC_SAE_immediate (const char *imm_start)
8392 {
8393 unsigned int match_found, j;
8394 const char *pstr = imm_start;
8395 expressionS *exp;
8396
8397 if (*pstr != '{')
8398 return 0;
8399
8400 pstr++;
8401 match_found = 0;
8402 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
8403 {
8404 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
8405 {
8406 if (!i.rounding)
8407 {
8408 rc_op.type = RC_NamesTable[j].type;
8409 rc_op.operand = this_operand;
8410 i.rounding = &rc_op;
8411 }
8412 else
8413 {
8414 as_bad (_("duplicated `%s'"), imm_start);
8415 return 0;
8416 }
8417 pstr += RC_NamesTable[j].len;
8418 match_found = 1;
8419 break;
8420 }
8421 }
8422 if (!match_found)
8423 return 0;
8424
8425 if (*pstr++ != '}')
8426 {
8427 as_bad (_("Missing '}': '%s'"), imm_start);
8428 return 0;
8429 }
8430 /* RC/SAE immediate string should contain nothing more. */;
8431 if (*pstr != 0)
8432 {
8433 as_bad (_("Junk after '}': '%s'"), imm_start);
8434 return 0;
8435 }
8436
8437 exp = &im_expressions[i.imm_operands++];
8438 i.op[this_operand].imms = exp;
8439
8440 exp->X_op = O_constant;
8441 exp->X_add_number = 0;
8442 exp->X_add_symbol = (symbolS *) 0;
8443 exp->X_op_symbol = (symbolS *) 0;
8444
8445 i.types[this_operand].bitfield.imm8 = 1;
8446 return 1;
8447 }
8448
8449 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
8450 on error. */
8451
8452 static int
8453 i386_att_operand (char *operand_string)
8454 {
8455 const reg_entry *r;
8456 char *end_op;
8457 char *op_string = operand_string;
8458
8459 if (is_space_char (*op_string))
8460 ++op_string;
8461
8462 /* We check for an absolute prefix (differentiating,
8463 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
8464 if (*op_string == ABSOLUTE_PREFIX)
8465 {
8466 ++op_string;
8467 if (is_space_char (*op_string))
8468 ++op_string;
8469 i.types[this_operand].bitfield.jumpabsolute = 1;
8470 }
8471
8472 /* Check if operand is a register. */
8473 if ((r = parse_register (op_string, &end_op)) != NULL)
8474 {
8475 i386_operand_type temp;
8476
8477 /* Check for a segment override by searching for ':' after a
8478 segment register. */
8479 op_string = end_op;
8480 if (is_space_char (*op_string))
8481 ++op_string;
8482 if (*op_string == ':'
8483 && (r->reg_type.bitfield.sreg2
8484 || r->reg_type.bitfield.sreg3))
8485 {
8486 switch (r->reg_num)
8487 {
8488 case 0:
8489 i.seg[i.mem_operands] = &es;
8490 break;
8491 case 1:
8492 i.seg[i.mem_operands] = &cs;
8493 break;
8494 case 2:
8495 i.seg[i.mem_operands] = &ss;
8496 break;
8497 case 3:
8498 i.seg[i.mem_operands] = &ds;
8499 break;
8500 case 4:
8501 i.seg[i.mem_operands] = &fs;
8502 break;
8503 case 5:
8504 i.seg[i.mem_operands] = &gs;
8505 break;
8506 }
8507
8508 /* Skip the ':' and whitespace. */
8509 ++op_string;
8510 if (is_space_char (*op_string))
8511 ++op_string;
8512
8513 if (!is_digit_char (*op_string)
8514 && !is_identifier_char (*op_string)
8515 && *op_string != '('
8516 && *op_string != ABSOLUTE_PREFIX)
8517 {
8518 as_bad (_("bad memory operand `%s'"), op_string);
8519 return 0;
8520 }
8521 /* Handle case of %es:*foo. */
8522 if (*op_string == ABSOLUTE_PREFIX)
8523 {
8524 ++op_string;
8525 if (is_space_char (*op_string))
8526 ++op_string;
8527 i.types[this_operand].bitfield.jumpabsolute = 1;
8528 }
8529 goto do_memory_reference;
8530 }
8531
8532 /* Handle vector operations. */
8533 if (*op_string == '{')
8534 {
8535 op_string = check_VecOperations (op_string, NULL);
8536 if (op_string == NULL)
8537 return 0;
8538 }
8539
8540 if (*op_string)
8541 {
8542 as_bad (_("junk `%s' after register"), op_string);
8543 return 0;
8544 }
8545 temp = r->reg_type;
8546 temp.bitfield.baseindex = 0;
8547 i.types[this_operand] = operand_type_or (i.types[this_operand],
8548 temp);
8549 i.types[this_operand].bitfield.unspecified = 0;
8550 i.op[this_operand].regs = r;
8551 i.reg_operands++;
8552 }
8553 else if (*op_string == REGISTER_PREFIX)
8554 {
8555 as_bad (_("bad register name `%s'"), op_string);
8556 return 0;
8557 }
8558 else if (*op_string == IMMEDIATE_PREFIX)
8559 {
8560 ++op_string;
8561 if (i.types[this_operand].bitfield.jumpabsolute)
8562 {
8563 as_bad (_("immediate operand illegal with absolute jump"));
8564 return 0;
8565 }
8566 if (!i386_immediate (op_string))
8567 return 0;
8568 }
8569 else if (RC_SAE_immediate (operand_string))
8570 {
8571 /* If it is a RC or SAE immediate, do nothing. */
8572 ;
8573 }
8574 else if (is_digit_char (*op_string)
8575 || is_identifier_char (*op_string)
8576 || *op_string == '(')
8577 {
8578 /* This is a memory reference of some sort. */
8579 char *base_string;
8580
8581 /* Start and end of displacement string expression (if found). */
8582 char *displacement_string_start;
8583 char *displacement_string_end;
8584 char *vop_start;
8585
8586 do_memory_reference:
8587 if ((i.mem_operands == 1
8588 && !current_templates->start->opcode_modifier.isstring)
8589 || i.mem_operands == 2)
8590 {
8591 as_bad (_("too many memory references for `%s'"),
8592 current_templates->start->name);
8593 return 0;
8594 }
8595
8596 /* Check for base index form. We detect the base index form by
8597 looking for an ')' at the end of the operand, searching
8598 for the '(' matching it, and finding a REGISTER_PREFIX or ','
8599 after the '('. */
8600 base_string = op_string + strlen (op_string);
8601
8602 /* Handle vector operations. */
8603 vop_start = strchr (op_string, '{');
8604 if (vop_start && vop_start < base_string)
8605 {
8606 if (check_VecOperations (vop_start, base_string) == NULL)
8607 return 0;
8608 base_string = vop_start;
8609 }
8610
8611 --base_string;
8612 if (is_space_char (*base_string))
8613 --base_string;
8614
8615 /* If we only have a displacement, set-up for it to be parsed later. */
8616 displacement_string_start = op_string;
8617 displacement_string_end = base_string + 1;
8618
8619 if (*base_string == ')')
8620 {
8621 char *temp_string;
8622 unsigned int parens_balanced = 1;
8623 /* We've already checked that the number of left & right ()'s are
8624 equal, so this loop will not be infinite. */
8625 do
8626 {
8627 base_string--;
8628 if (*base_string == ')')
8629 parens_balanced++;
8630 if (*base_string == '(')
8631 parens_balanced--;
8632 }
8633 while (parens_balanced);
8634
8635 temp_string = base_string;
8636
8637 /* Skip past '(' and whitespace. */
8638 ++base_string;
8639 if (is_space_char (*base_string))
8640 ++base_string;
8641
8642 if (*base_string == ','
8643 || ((i.base_reg = parse_register (base_string, &end_op))
8644 != NULL))
8645 {
8646 displacement_string_end = temp_string;
8647
8648 i.types[this_operand].bitfield.baseindex = 1;
8649
8650 if (i.base_reg)
8651 {
8652 base_string = end_op;
8653 if (is_space_char (*base_string))
8654 ++base_string;
8655 }
8656
8657 /* There may be an index reg or scale factor here. */
8658 if (*base_string == ',')
8659 {
8660 ++base_string;
8661 if (is_space_char (*base_string))
8662 ++base_string;
8663
8664 if ((i.index_reg = parse_register (base_string, &end_op))
8665 != NULL)
8666 {
8667 base_string = end_op;
8668 if (is_space_char (*base_string))
8669 ++base_string;
8670 if (*base_string == ',')
8671 {
8672 ++base_string;
8673 if (is_space_char (*base_string))
8674 ++base_string;
8675 }
8676 else if (*base_string != ')')
8677 {
8678 as_bad (_("expecting `,' or `)' "
8679 "after index register in `%s'"),
8680 operand_string);
8681 return 0;
8682 }
8683 }
8684 else if (*base_string == REGISTER_PREFIX)
8685 {
8686 end_op = strchr (base_string, ',');
8687 if (end_op)
8688 *end_op = '\0';
8689 as_bad (_("bad register name `%s'"), base_string);
8690 return 0;
8691 }
8692
8693 /* Check for scale factor. */
8694 if (*base_string != ')')
8695 {
8696 char *end_scale = i386_scale (base_string);
8697
8698 if (!end_scale)
8699 return 0;
8700
8701 base_string = end_scale;
8702 if (is_space_char (*base_string))
8703 ++base_string;
8704 if (*base_string != ')')
8705 {
8706 as_bad (_("expecting `)' "
8707 "after scale factor in `%s'"),
8708 operand_string);
8709 return 0;
8710 }
8711 }
8712 else if (!i.index_reg)
8713 {
8714 as_bad (_("expecting index register or scale factor "
8715 "after `,'; got '%c'"),
8716 *base_string);
8717 return 0;
8718 }
8719 }
8720 else if (*base_string != ')')
8721 {
8722 as_bad (_("expecting `,' or `)' "
8723 "after base register in `%s'"),
8724 operand_string);
8725 return 0;
8726 }
8727 }
8728 else if (*base_string == REGISTER_PREFIX)
8729 {
8730 end_op = strchr (base_string, ',');
8731 if (end_op)
8732 *end_op = '\0';
8733 as_bad (_("bad register name `%s'"), base_string);
8734 return 0;
8735 }
8736 }
8737
8738 /* If there's an expression beginning the operand, parse it,
8739 assuming displacement_string_start and
8740 displacement_string_end are meaningful. */
8741 if (displacement_string_start != displacement_string_end)
8742 {
8743 if (!i386_displacement (displacement_string_start,
8744 displacement_string_end))
8745 return 0;
8746 }
8747
8748 /* Special case for (%dx) while doing input/output op. */
8749 if (i.base_reg
8750 && operand_type_equal (&i.base_reg->reg_type,
8751 &reg16_inoutportreg)
8752 && i.index_reg == 0
8753 && i.log2_scale_factor == 0
8754 && i.seg[i.mem_operands] == 0
8755 && !operand_type_check (i.types[this_operand], disp))
8756 {
8757 i.types[this_operand] = inoutportreg;
8758 return 1;
8759 }
8760
8761 if (i386_index_check (operand_string) == 0)
8762 return 0;
8763 i.types[this_operand].bitfield.mem = 1;
8764 i.mem_operands++;
8765 }
8766 else
8767 {
8768 /* It's not a memory operand; argh! */
8769 as_bad (_("invalid char %s beginning operand %d `%s'"),
8770 output_invalid (*op_string),
8771 this_operand + 1,
8772 op_string);
8773 return 0;
8774 }
8775 return 1; /* Normal return. */
8776 }
8777 \f
8778 /* Calculate the maximum variable size (i.e., excluding fr_fix)
8779 that an rs_machine_dependent frag may reach. */
8780
8781 unsigned int
8782 i386_frag_max_var (fragS *frag)
8783 {
8784 /* The only relaxable frags are for jumps.
8785 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
8786 gas_assert (frag->fr_type == rs_machine_dependent);
8787 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
8788 }
8789
8790 /* md_estimate_size_before_relax()
8791
8792 Called just before relax() for rs_machine_dependent frags. The x86
8793 assembler uses these frags to handle variable size jump
8794 instructions.
8795
8796 Any symbol that is now undefined will not become defined.
8797 Return the correct fr_subtype in the frag.
8798 Return the initial "guess for variable size of frag" to caller.
8799 The guess is actually the growth beyond the fixed part. Whatever
8800 we do to grow the fixed or variable part contributes to our
8801 returned value. */
8802
8803 int
8804 md_estimate_size_before_relax (fragS *fragP, segT segment)
8805 {
8806 /* We've already got fragP->fr_subtype right; all we have to do is
8807 check for un-relaxable symbols. On an ELF system, we can't relax
8808 an externally visible symbol, because it may be overridden by a
8809 shared library. */
8810 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
8811 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8812 || (IS_ELF
8813 && (S_IS_EXTERNAL (fragP->fr_symbol)
8814 || S_IS_WEAK (fragP->fr_symbol)
8815 || ((symbol_get_bfdsym (fragP->fr_symbol)->flags
8816 & BSF_GNU_INDIRECT_FUNCTION))))
8817 #endif
8818 #if defined (OBJ_COFF) && defined (TE_PE)
8819 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
8820 && S_IS_WEAK (fragP->fr_symbol))
8821 #endif
8822 )
8823 {
8824 /* Symbol is undefined in this segment, or we need to keep a
8825 reloc so that weak symbols can be overridden. */
8826 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
8827 enum bfd_reloc_code_real reloc_type;
8828 unsigned char *opcode;
8829 int old_fr_fix;
8830
8831 if (fragP->fr_var != NO_RELOC)
8832 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
8833 else if (size == 2)
8834 reloc_type = BFD_RELOC_16_PCREL;
8835 else
8836 reloc_type = BFD_RELOC_32_PCREL;
8837
8838 old_fr_fix = fragP->fr_fix;
8839 opcode = (unsigned char *) fragP->fr_opcode;
8840
8841 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
8842 {
8843 case UNCOND_JUMP:
8844 /* Make jmp (0xeb) a (d)word displacement jump. */
8845 opcode[0] = 0xe9;
8846 fragP->fr_fix += size;
8847 fix_new (fragP, old_fr_fix, size,
8848 fragP->fr_symbol,
8849 fragP->fr_offset, 1,
8850 reloc_type);
8851 break;
8852
8853 case COND_JUMP86:
8854 if (size == 2
8855 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
8856 {
8857 /* Negate the condition, and branch past an
8858 unconditional jump. */
8859 opcode[0] ^= 1;
8860 opcode[1] = 3;
8861 /* Insert an unconditional jump. */
8862 opcode[2] = 0xe9;
8863 /* We added two extra opcode bytes, and have a two byte
8864 offset. */
8865 fragP->fr_fix += 2 + 2;
8866 fix_new (fragP, old_fr_fix + 2, 2,
8867 fragP->fr_symbol,
8868 fragP->fr_offset, 1,
8869 reloc_type);
8870 break;
8871 }
8872 /* Fall through. */
8873
8874 case COND_JUMP:
8875 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
8876 {
8877 fixS *fixP;
8878
8879 fragP->fr_fix += 1;
8880 fixP = fix_new (fragP, old_fr_fix, 1,
8881 fragP->fr_symbol,
8882 fragP->fr_offset, 1,
8883 BFD_RELOC_8_PCREL);
8884 fixP->fx_signed = 1;
8885 break;
8886 }
8887
8888 /* This changes the byte-displacement jump 0x7N
8889 to the (d)word-displacement jump 0x0f,0x8N. */
8890 opcode[1] = opcode[0] + 0x10;
8891 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
8892 /* We've added an opcode byte. */
8893 fragP->fr_fix += 1 + size;
8894 fix_new (fragP, old_fr_fix + 1, size,
8895 fragP->fr_symbol,
8896 fragP->fr_offset, 1,
8897 reloc_type);
8898 break;
8899
8900 default:
8901 BAD_CASE (fragP->fr_subtype);
8902 break;
8903 }
8904 frag_wane (fragP);
8905 return fragP->fr_fix - old_fr_fix;
8906 }
8907
8908 /* Guess size depending on current relax state. Initially the relax
8909 state will correspond to a short jump and we return 1, because
8910 the variable part of the frag (the branch offset) is one byte
8911 long. However, we can relax a section more than once and in that
8912 case we must either set fr_subtype back to the unrelaxed state,
8913 or return the value for the appropriate branch. */
8914 return md_relax_table[fragP->fr_subtype].rlx_length;
8915 }
8916
8917 /* Called after relax() is finished.
8918
8919 In: Address of frag.
8920 fr_type == rs_machine_dependent.
8921 fr_subtype is what the address relaxed to.
8922
8923 Out: Any fixSs and constants are set up.
8924 Caller will turn frag into a ".space 0". */
8925
8926 void
8927 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
8928 fragS *fragP)
8929 {
8930 unsigned char *opcode;
8931 unsigned char *where_to_put_displacement = NULL;
8932 offsetT target_address;
8933 offsetT opcode_address;
8934 unsigned int extension = 0;
8935 offsetT displacement_from_opcode_start;
8936
8937 opcode = (unsigned char *) fragP->fr_opcode;
8938
8939 /* Address we want to reach in file space. */
8940 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
8941
8942 /* Address opcode resides at in file space. */
8943 opcode_address = fragP->fr_address + fragP->fr_fix;
8944
8945 /* Displacement from opcode start to fill into instruction. */
8946 displacement_from_opcode_start = target_address - opcode_address;
8947
8948 if ((fragP->fr_subtype & BIG) == 0)
8949 {
8950 /* Don't have to change opcode. */
8951 extension = 1; /* 1 opcode + 1 displacement */
8952 where_to_put_displacement = &opcode[1];
8953 }
8954 else
8955 {
8956 if (no_cond_jump_promotion
8957 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
8958 as_warn_where (fragP->fr_file, fragP->fr_line,
8959 _("long jump required"));
8960
8961 switch (fragP->fr_subtype)
8962 {
8963 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
8964 extension = 4; /* 1 opcode + 4 displacement */
8965 opcode[0] = 0xe9;
8966 where_to_put_displacement = &opcode[1];
8967 break;
8968
8969 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
8970 extension = 2; /* 1 opcode + 2 displacement */
8971 opcode[0] = 0xe9;
8972 where_to_put_displacement = &opcode[1];
8973 break;
8974
8975 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
8976 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
8977 extension = 5; /* 2 opcode + 4 displacement */
8978 opcode[1] = opcode[0] + 0x10;
8979 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
8980 where_to_put_displacement = &opcode[2];
8981 break;
8982
8983 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
8984 extension = 3; /* 2 opcode + 2 displacement */
8985 opcode[1] = opcode[0] + 0x10;
8986 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
8987 where_to_put_displacement = &opcode[2];
8988 break;
8989
8990 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
8991 extension = 4;
8992 opcode[0] ^= 1;
8993 opcode[1] = 3;
8994 opcode[2] = 0xe9;
8995 where_to_put_displacement = &opcode[3];
8996 break;
8997
8998 default:
8999 BAD_CASE (fragP->fr_subtype);
9000 break;
9001 }
9002 }
9003
9004 /* If size if less then four we are sure that the operand fits,
9005 but if it's 4, then it could be that the displacement is larger
9006 then -/+ 2GB. */
9007 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
9008 && object_64bit
9009 && ((addressT) (displacement_from_opcode_start - extension
9010 + ((addressT) 1 << 31))
9011 > (((addressT) 2 << 31) - 1)))
9012 {
9013 as_bad_where (fragP->fr_file, fragP->fr_line,
9014 _("jump target out of range"));
9015 /* Make us emit 0. */
9016 displacement_from_opcode_start = extension;
9017 }
9018 /* Now put displacement after opcode. */
9019 md_number_to_chars ((char *) where_to_put_displacement,
9020 (valueT) (displacement_from_opcode_start - extension),
9021 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
9022 fragP->fr_fix += extension;
9023 }
9024 \f
9025 /* Apply a fixup (fixP) to segment data, once it has been determined
9026 by our caller that we have all the info we need to fix it up.
9027
9028 Parameter valP is the pointer to the value of the bits.
9029
9030 On the 386, immediates, displacements, and data pointers are all in
9031 the same (little-endian) format, so we don't need to care about which
9032 we are handling. */
9033
9034 void
9035 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
9036 {
9037 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
9038 valueT value = *valP;
9039
9040 #if !defined (TE_Mach)
9041 if (fixP->fx_pcrel)
9042 {
9043 switch (fixP->fx_r_type)
9044 {
9045 default:
9046 break;
9047
9048 case BFD_RELOC_64:
9049 fixP->fx_r_type = BFD_RELOC_64_PCREL;
9050 break;
9051 case BFD_RELOC_32:
9052 case BFD_RELOC_X86_64_32S:
9053 fixP->fx_r_type = BFD_RELOC_32_PCREL;
9054 break;
9055 case BFD_RELOC_16:
9056 fixP->fx_r_type = BFD_RELOC_16_PCREL;
9057 break;
9058 case BFD_RELOC_8:
9059 fixP->fx_r_type = BFD_RELOC_8_PCREL;
9060 break;
9061 }
9062 }
9063
9064 if (fixP->fx_addsy != NULL
9065 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
9066 || fixP->fx_r_type == BFD_RELOC_64_PCREL
9067 || fixP->fx_r_type == BFD_RELOC_16_PCREL
9068 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
9069 && !use_rela_relocations)
9070 {
9071 /* This is a hack. There should be a better way to handle this.
9072 This covers for the fact that bfd_install_relocation will
9073 subtract the current location (for partial_inplace, PC relative
9074 relocations); see more below. */
9075 #ifndef OBJ_AOUT
9076 if (IS_ELF
9077 #ifdef TE_PE
9078 || OUTPUT_FLAVOR == bfd_target_coff_flavour
9079 #endif
9080 )
9081 value += fixP->fx_where + fixP->fx_frag->fr_address;
9082 #endif
9083 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9084 if (IS_ELF)
9085 {
9086 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
9087
9088 if ((sym_seg == seg
9089 || (symbol_section_p (fixP->fx_addsy)
9090 && sym_seg != absolute_section))
9091 && !generic_force_reloc (fixP))
9092 {
9093 /* Yes, we add the values in twice. This is because
9094 bfd_install_relocation subtracts them out again. I think
9095 bfd_install_relocation is broken, but I don't dare change
9096 it. FIXME. */
9097 value += fixP->fx_where + fixP->fx_frag->fr_address;
9098 }
9099 }
9100 #endif
9101 #if defined (OBJ_COFF) && defined (TE_PE)
9102 /* For some reason, the PE format does not store a
9103 section address offset for a PC relative symbol. */
9104 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
9105 || S_IS_WEAK (fixP->fx_addsy))
9106 value += md_pcrel_from (fixP);
9107 #endif
9108 }
9109 #if defined (OBJ_COFF) && defined (TE_PE)
9110 if (fixP->fx_addsy != NULL
9111 && S_IS_WEAK (fixP->fx_addsy)
9112 /* PR 16858: Do not modify weak function references. */
9113 && ! fixP->fx_pcrel)
9114 {
9115 #if !defined (TE_PEP)
9116 /* For x86 PE weak function symbols are neither PC-relative
9117 nor do they set S_IS_FUNCTION. So the only reliable way
9118 to detect them is to check the flags of their containing
9119 section. */
9120 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
9121 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
9122 ;
9123 else
9124 #endif
9125 value -= S_GET_VALUE (fixP->fx_addsy);
9126 }
9127 #endif
9128
9129 /* Fix a few things - the dynamic linker expects certain values here,
9130 and we must not disappoint it. */
9131 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9132 if (IS_ELF && fixP->fx_addsy)
9133 switch (fixP->fx_r_type)
9134 {
9135 case BFD_RELOC_386_PLT32:
9136 case BFD_RELOC_X86_64_PLT32:
9137 /* Make the jump instruction point to the address of the operand. At
9138 runtime we merely add the offset to the actual PLT entry. */
9139 value = -4;
9140 break;
9141
9142 case BFD_RELOC_386_TLS_GD:
9143 case BFD_RELOC_386_TLS_LDM:
9144 case BFD_RELOC_386_TLS_IE_32:
9145 case BFD_RELOC_386_TLS_IE:
9146 case BFD_RELOC_386_TLS_GOTIE:
9147 case BFD_RELOC_386_TLS_GOTDESC:
9148 case BFD_RELOC_X86_64_TLSGD:
9149 case BFD_RELOC_X86_64_TLSLD:
9150 case BFD_RELOC_X86_64_GOTTPOFF:
9151 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9152 value = 0; /* Fully resolved at runtime. No addend. */
9153 /* Fallthrough */
9154 case BFD_RELOC_386_TLS_LE:
9155 case BFD_RELOC_386_TLS_LDO_32:
9156 case BFD_RELOC_386_TLS_LE_32:
9157 case BFD_RELOC_X86_64_DTPOFF32:
9158 case BFD_RELOC_X86_64_DTPOFF64:
9159 case BFD_RELOC_X86_64_TPOFF32:
9160 case BFD_RELOC_X86_64_TPOFF64:
9161 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9162 break;
9163
9164 case BFD_RELOC_386_TLS_DESC_CALL:
9165 case BFD_RELOC_X86_64_TLSDESC_CALL:
9166 value = 0; /* Fully resolved at runtime. No addend. */
9167 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9168 fixP->fx_done = 0;
9169 return;
9170
9171 case BFD_RELOC_386_GOT32:
9172 case BFD_RELOC_X86_64_GOT32:
9173 value = 0; /* Fully resolved at runtime. No addend. */
9174 break;
9175
9176 case BFD_RELOC_VTABLE_INHERIT:
9177 case BFD_RELOC_VTABLE_ENTRY:
9178 fixP->fx_done = 0;
9179 return;
9180
9181 default:
9182 break;
9183 }
9184 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
9185 *valP = value;
9186 #endif /* !defined (TE_Mach) */
9187
9188 /* Are we finished with this relocation now? */
9189 if (fixP->fx_addsy == NULL)
9190 fixP->fx_done = 1;
9191 #if defined (OBJ_COFF) && defined (TE_PE)
9192 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
9193 {
9194 fixP->fx_done = 0;
9195 /* Remember value for tc_gen_reloc. */
9196 fixP->fx_addnumber = value;
9197 /* Clear out the frag for now. */
9198 value = 0;
9199 }
9200 #endif
9201 else if (use_rela_relocations)
9202 {
9203 fixP->fx_no_overflow = 1;
9204 /* Remember value for tc_gen_reloc. */
9205 fixP->fx_addnumber = value;
9206 value = 0;
9207 }
9208
9209 md_number_to_chars (p, value, fixP->fx_size);
9210 }
9211 \f
9212 char *
9213 md_atof (int type, char *litP, int *sizeP)
9214 {
9215 /* This outputs the LITTLENUMs in REVERSE order;
9216 in accord with the bigendian 386. */
9217 return ieee_md_atof (type, litP, sizeP, FALSE);
9218 }
9219 \f
9220 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
9221
9222 static char *
9223 output_invalid (int c)
9224 {
9225 if (ISPRINT (c))
9226 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
9227 "'%c'", c);
9228 else
9229 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
9230 "(0x%x)", (unsigned char) c);
9231 return output_invalid_buf;
9232 }
9233
9234 /* REG_STRING starts *before* REGISTER_PREFIX. */
9235
9236 static const reg_entry *
9237 parse_real_register (char *reg_string, char **end_op)
9238 {
9239 char *s = reg_string;
9240 char *p;
9241 char reg_name_given[MAX_REG_NAME_SIZE + 1];
9242 const reg_entry *r;
9243
9244 /* Skip possible REGISTER_PREFIX and possible whitespace. */
9245 if (*s == REGISTER_PREFIX)
9246 ++s;
9247
9248 if (is_space_char (*s))
9249 ++s;
9250
9251 p = reg_name_given;
9252 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
9253 {
9254 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
9255 return (const reg_entry *) NULL;
9256 s++;
9257 }
9258
9259 /* For naked regs, make sure that we are not dealing with an identifier.
9260 This prevents confusing an identifier like `eax_var' with register
9261 `eax'. */
9262 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
9263 return (const reg_entry *) NULL;
9264
9265 *end_op = s;
9266
9267 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
9268
9269 /* Handle floating point regs, allowing spaces in the (i) part. */
9270 if (r == i386_regtab /* %st is first entry of table */)
9271 {
9272 if (is_space_char (*s))
9273 ++s;
9274 if (*s == '(')
9275 {
9276 ++s;
9277 if (is_space_char (*s))
9278 ++s;
9279 if (*s >= '0' && *s <= '7')
9280 {
9281 int fpr = *s - '0';
9282 ++s;
9283 if (is_space_char (*s))
9284 ++s;
9285 if (*s == ')')
9286 {
9287 *end_op = s + 1;
9288 r = (const reg_entry *) hash_find (reg_hash, "st(0)");
9289 know (r);
9290 return r + fpr;
9291 }
9292 }
9293 /* We have "%st(" then garbage. */
9294 return (const reg_entry *) NULL;
9295 }
9296 }
9297
9298 if (r == NULL || allow_pseudo_reg)
9299 return r;
9300
9301 if (operand_type_all_zero (&r->reg_type))
9302 return (const reg_entry *) NULL;
9303
9304 if ((r->reg_type.bitfield.reg32
9305 || r->reg_type.bitfield.sreg3
9306 || r->reg_type.bitfield.control
9307 || r->reg_type.bitfield.debug
9308 || r->reg_type.bitfield.test)
9309 && !cpu_arch_flags.bitfield.cpui386)
9310 return (const reg_entry *) NULL;
9311
9312 if (r->reg_type.bitfield.floatreg
9313 && !cpu_arch_flags.bitfield.cpu8087
9314 && !cpu_arch_flags.bitfield.cpu287
9315 && !cpu_arch_flags.bitfield.cpu387)
9316 return (const reg_entry *) NULL;
9317
9318 if (r->reg_type.bitfield.regmmx && !cpu_arch_flags.bitfield.cpummx)
9319 return (const reg_entry *) NULL;
9320
9321 if (r->reg_type.bitfield.regxmm && !cpu_arch_flags.bitfield.cpusse)
9322 return (const reg_entry *) NULL;
9323
9324 if (r->reg_type.bitfield.regymm && !cpu_arch_flags.bitfield.cpuavx)
9325 return (const reg_entry *) NULL;
9326
9327 if ((r->reg_type.bitfield.regzmm || r->reg_type.bitfield.regmask)
9328 && !cpu_arch_flags.bitfield.cpuavx512f)
9329 return (const reg_entry *) NULL;
9330
9331 /* Don't allow fake index register unless allow_index_reg isn't 0. */
9332 if (!allow_index_reg
9333 && (r->reg_num == RegEiz || r->reg_num == RegRiz))
9334 return (const reg_entry *) NULL;
9335
9336 /* Upper 16 vector register is only available with VREX in 64bit
9337 mode. */
9338 if ((r->reg_flags & RegVRex))
9339 {
9340 if (!cpu_arch_flags.bitfield.cpuvrex
9341 || flag_code != CODE_64BIT)
9342 return (const reg_entry *) NULL;
9343
9344 i.need_vrex = 1;
9345 }
9346
9347 if (((r->reg_flags & (RegRex64 | RegRex))
9348 || r->reg_type.bitfield.reg64)
9349 && (!cpu_arch_flags.bitfield.cpulm
9350 || !operand_type_equal (&r->reg_type, &control))
9351 && flag_code != CODE_64BIT)
9352 return (const reg_entry *) NULL;
9353
9354 if (r->reg_type.bitfield.sreg3 && r->reg_num == RegFlat && !intel_syntax)
9355 return (const reg_entry *) NULL;
9356
9357 return r;
9358 }
9359
9360 /* REG_STRING starts *before* REGISTER_PREFIX. */
9361
9362 static const reg_entry *
9363 parse_register (char *reg_string, char **end_op)
9364 {
9365 const reg_entry *r;
9366
9367 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
9368 r = parse_real_register (reg_string, end_op);
9369 else
9370 r = NULL;
9371 if (!r)
9372 {
9373 char *save = input_line_pointer;
9374 char c;
9375 symbolS *symbolP;
9376
9377 input_line_pointer = reg_string;
9378 c = get_symbol_end ();
9379 symbolP = symbol_find (reg_string);
9380 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
9381 {
9382 const expressionS *e = symbol_get_value_expression (symbolP);
9383
9384 know (e->X_op == O_register);
9385 know (e->X_add_number >= 0
9386 && (valueT) e->X_add_number < i386_regtab_size);
9387 r = i386_regtab + e->X_add_number;
9388 if ((r->reg_flags & RegVRex))
9389 i.need_vrex = 1;
9390 *end_op = input_line_pointer;
9391 }
9392 *input_line_pointer = c;
9393 input_line_pointer = save;
9394 }
9395 return r;
9396 }
9397
9398 int
9399 i386_parse_name (char *name, expressionS *e, char *nextcharP)
9400 {
9401 const reg_entry *r;
9402 char *end = input_line_pointer;
9403
9404 *end = *nextcharP;
9405 r = parse_register (name, &input_line_pointer);
9406 if (r && end <= input_line_pointer)
9407 {
9408 *nextcharP = *input_line_pointer;
9409 *input_line_pointer = 0;
9410 e->X_op = O_register;
9411 e->X_add_number = r - i386_regtab;
9412 return 1;
9413 }
9414 input_line_pointer = end;
9415 *end = 0;
9416 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
9417 }
9418
9419 void
9420 md_operand (expressionS *e)
9421 {
9422 char *end;
9423 const reg_entry *r;
9424
9425 switch (*input_line_pointer)
9426 {
9427 case REGISTER_PREFIX:
9428 r = parse_real_register (input_line_pointer, &end);
9429 if (r)
9430 {
9431 e->X_op = O_register;
9432 e->X_add_number = r - i386_regtab;
9433 input_line_pointer = end;
9434 }
9435 break;
9436
9437 case '[':
9438 gas_assert (intel_syntax);
9439 end = input_line_pointer++;
9440 expression (e);
9441 if (*input_line_pointer == ']')
9442 {
9443 ++input_line_pointer;
9444 e->X_op_symbol = make_expr_symbol (e);
9445 e->X_add_symbol = NULL;
9446 e->X_add_number = 0;
9447 e->X_op = O_index;
9448 }
9449 else
9450 {
9451 e->X_op = O_absent;
9452 input_line_pointer = end;
9453 }
9454 break;
9455 }
9456 }
9457
9458 \f
9459 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9460 const char *md_shortopts = "kVQ:sqn";
9461 #else
9462 const char *md_shortopts = "qn";
9463 #endif
9464
9465 #define OPTION_32 (OPTION_MD_BASE + 0)
9466 #define OPTION_64 (OPTION_MD_BASE + 1)
9467 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9468 #define OPTION_MARCH (OPTION_MD_BASE + 3)
9469 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
9470 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
9471 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
9472 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
9473 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
9474 #define OPTION_MOLD_GCC (OPTION_MD_BASE + 9)
9475 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
9476 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
9477 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
9478 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
9479 #define OPTION_X32 (OPTION_MD_BASE + 14)
9480 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
9481 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
9482 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
9483 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
9484 #define OPTION_OMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
9485 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
9486
9487 struct option md_longopts[] =
9488 {
9489 {"32", no_argument, NULL, OPTION_32},
9490 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
9491 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
9492 {"64", no_argument, NULL, OPTION_64},
9493 #endif
9494 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9495 {"x32", no_argument, NULL, OPTION_X32},
9496 #endif
9497 {"divide", no_argument, NULL, OPTION_DIVIDE},
9498 {"march", required_argument, NULL, OPTION_MARCH},
9499 {"mtune", required_argument, NULL, OPTION_MTUNE},
9500 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
9501 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
9502 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
9503 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
9504 {"mold-gcc", no_argument, NULL, OPTION_MOLD_GCC},
9505 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
9506 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
9507 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
9508 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
9509 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
9510 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
9511 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
9512 # if defined (TE_PE) || defined (TE_PEP)
9513 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
9514 #endif
9515 {"momit-lock-prefix", required_argument, NULL, OPTION_OMIT_LOCK_PREFIX},
9516 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
9517 {NULL, no_argument, NULL, 0}
9518 };
9519 size_t md_longopts_size = sizeof (md_longopts);
9520
9521 int
9522 md_parse_option (int c, char *arg)
9523 {
9524 unsigned int j;
9525 char *arch, *next;
9526
9527 switch (c)
9528 {
9529 case 'n':
9530 optimize_align_code = 0;
9531 break;
9532
9533 case 'q':
9534 quiet_warnings = 1;
9535 break;
9536
9537 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9538 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
9539 should be emitted or not. FIXME: Not implemented. */
9540 case 'Q':
9541 break;
9542
9543 /* -V: SVR4 argument to print version ID. */
9544 case 'V':
9545 print_version_id ();
9546 break;
9547
9548 /* -k: Ignore for FreeBSD compatibility. */
9549 case 'k':
9550 break;
9551
9552 case 's':
9553 /* -s: On i386 Solaris, this tells the native assembler to use
9554 .stab instead of .stab.excl. We always use .stab anyhow. */
9555 break;
9556 #endif
9557 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
9558 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
9559 case OPTION_64:
9560 {
9561 const char **list, **l;
9562
9563 list = bfd_target_list ();
9564 for (l = list; *l != NULL; l++)
9565 if (CONST_STRNEQ (*l, "elf64-x86-64")
9566 || strcmp (*l, "coff-x86-64") == 0
9567 || strcmp (*l, "pe-x86-64") == 0
9568 || strcmp (*l, "pei-x86-64") == 0
9569 || strcmp (*l, "mach-o-x86-64") == 0)
9570 {
9571 default_arch = "x86_64";
9572 break;
9573 }
9574 if (*l == NULL)
9575 as_fatal (_("no compiled in support for x86_64"));
9576 free (list);
9577 }
9578 break;
9579 #endif
9580
9581 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9582 case OPTION_X32:
9583 if (IS_ELF)
9584 {
9585 const char **list, **l;
9586
9587 list = bfd_target_list ();
9588 for (l = list; *l != NULL; l++)
9589 if (CONST_STRNEQ (*l, "elf32-x86-64"))
9590 {
9591 default_arch = "x86_64:32";
9592 break;
9593 }
9594 if (*l == NULL)
9595 as_fatal (_("no compiled in support for 32bit x86_64"));
9596 free (list);
9597 }
9598 else
9599 as_fatal (_("32bit x86_64 is only supported for ELF"));
9600 break;
9601 #endif
9602
9603 case OPTION_32:
9604 default_arch = "i386";
9605 break;
9606
9607 case OPTION_DIVIDE:
9608 #ifdef SVR4_COMMENT_CHARS
9609 {
9610 char *n, *t;
9611 const char *s;
9612
9613 n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
9614 t = n;
9615 for (s = i386_comment_chars; *s != '\0'; s++)
9616 if (*s != '/')
9617 *t++ = *s;
9618 *t = '\0';
9619 i386_comment_chars = n;
9620 }
9621 #endif
9622 break;
9623
9624 case OPTION_MARCH:
9625 arch = xstrdup (arg);
9626 do
9627 {
9628 if (*arch == '.')
9629 as_fatal (_("invalid -march= option: `%s'"), arg);
9630 next = strchr (arch, '+');
9631 if (next)
9632 *next++ = '\0';
9633 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9634 {
9635 if (strcmp (arch, cpu_arch [j].name) == 0)
9636 {
9637 /* Processor. */
9638 if (! cpu_arch[j].flags.bitfield.cpui386)
9639 continue;
9640
9641 cpu_arch_name = cpu_arch[j].name;
9642 cpu_sub_arch_name = NULL;
9643 cpu_arch_flags = cpu_arch[j].flags;
9644 cpu_arch_isa = cpu_arch[j].type;
9645 cpu_arch_isa_flags = cpu_arch[j].flags;
9646 if (!cpu_arch_tune_set)
9647 {
9648 cpu_arch_tune = cpu_arch_isa;
9649 cpu_arch_tune_flags = cpu_arch_isa_flags;
9650 }
9651 break;
9652 }
9653 else if (*cpu_arch [j].name == '.'
9654 && strcmp (arch, cpu_arch [j].name + 1) == 0)
9655 {
9656 /* ISA entension. */
9657 i386_cpu_flags flags;
9658
9659 if (!cpu_arch[j].negated)
9660 flags = cpu_flags_or (cpu_arch_flags,
9661 cpu_arch[j].flags);
9662 else
9663 flags = cpu_flags_and_not (cpu_arch_flags,
9664 cpu_arch[j].flags);
9665 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
9666 {
9667 if (cpu_sub_arch_name)
9668 {
9669 char *name = cpu_sub_arch_name;
9670 cpu_sub_arch_name = concat (name,
9671 cpu_arch[j].name,
9672 (const char *) NULL);
9673 free (name);
9674 }
9675 else
9676 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
9677 cpu_arch_flags = flags;
9678 cpu_arch_isa_flags = flags;
9679 }
9680 break;
9681 }
9682 }
9683
9684 if (j >= ARRAY_SIZE (cpu_arch))
9685 as_fatal (_("invalid -march= option: `%s'"), arg);
9686
9687 arch = next;
9688 }
9689 while (next != NULL );
9690 break;
9691
9692 case OPTION_MTUNE:
9693 if (*arg == '.')
9694 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9695 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9696 {
9697 if (strcmp (arg, cpu_arch [j].name) == 0)
9698 {
9699 cpu_arch_tune_set = 1;
9700 cpu_arch_tune = cpu_arch [j].type;
9701 cpu_arch_tune_flags = cpu_arch[j].flags;
9702 break;
9703 }
9704 }
9705 if (j >= ARRAY_SIZE (cpu_arch))
9706 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9707 break;
9708
9709 case OPTION_MMNEMONIC:
9710 if (strcasecmp (arg, "att") == 0)
9711 intel_mnemonic = 0;
9712 else if (strcasecmp (arg, "intel") == 0)
9713 intel_mnemonic = 1;
9714 else
9715 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
9716 break;
9717
9718 case OPTION_MSYNTAX:
9719 if (strcasecmp (arg, "att") == 0)
9720 intel_syntax = 0;
9721 else if (strcasecmp (arg, "intel") == 0)
9722 intel_syntax = 1;
9723 else
9724 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
9725 break;
9726
9727 case OPTION_MINDEX_REG:
9728 allow_index_reg = 1;
9729 break;
9730
9731 case OPTION_MNAKED_REG:
9732 allow_naked_reg = 1;
9733 break;
9734
9735 case OPTION_MOLD_GCC:
9736 old_gcc = 1;
9737 break;
9738
9739 case OPTION_MSSE2AVX:
9740 sse2avx = 1;
9741 break;
9742
9743 case OPTION_MSSE_CHECK:
9744 if (strcasecmp (arg, "error") == 0)
9745 sse_check = check_error;
9746 else if (strcasecmp (arg, "warning") == 0)
9747 sse_check = check_warning;
9748 else if (strcasecmp (arg, "none") == 0)
9749 sse_check = check_none;
9750 else
9751 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
9752 break;
9753
9754 case OPTION_MOPERAND_CHECK:
9755 if (strcasecmp (arg, "error") == 0)
9756 operand_check = check_error;
9757 else if (strcasecmp (arg, "warning") == 0)
9758 operand_check = check_warning;
9759 else if (strcasecmp (arg, "none") == 0)
9760 operand_check = check_none;
9761 else
9762 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
9763 break;
9764
9765 case OPTION_MAVXSCALAR:
9766 if (strcasecmp (arg, "128") == 0)
9767 avxscalar = vex128;
9768 else if (strcasecmp (arg, "256") == 0)
9769 avxscalar = vex256;
9770 else
9771 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
9772 break;
9773
9774 case OPTION_MADD_BND_PREFIX:
9775 add_bnd_prefix = 1;
9776 break;
9777
9778 case OPTION_MEVEXLIG:
9779 if (strcmp (arg, "128") == 0)
9780 evexlig = evexl128;
9781 else if (strcmp (arg, "256") == 0)
9782 evexlig = evexl256;
9783 else if (strcmp (arg, "512") == 0)
9784 evexlig = evexl512;
9785 else
9786 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
9787 break;
9788
9789 case OPTION_MEVEXRCIG:
9790 if (strcmp (arg, "rne") == 0)
9791 evexrcig = rne;
9792 else if (strcmp (arg, "rd") == 0)
9793 evexrcig = rd;
9794 else if (strcmp (arg, "ru") == 0)
9795 evexrcig = ru;
9796 else if (strcmp (arg, "rz") == 0)
9797 evexrcig = rz;
9798 else
9799 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
9800 break;
9801
9802 case OPTION_MEVEXWIG:
9803 if (strcmp (arg, "0") == 0)
9804 evexwig = evexw0;
9805 else if (strcmp (arg, "1") == 0)
9806 evexwig = evexw1;
9807 else
9808 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
9809 break;
9810
9811 # if defined (TE_PE) || defined (TE_PEP)
9812 case OPTION_MBIG_OBJ:
9813 use_big_obj = 1;
9814 break;
9815 #endif
9816
9817 case OPTION_OMIT_LOCK_PREFIX:
9818 if (strcasecmp (arg, "yes") == 0)
9819 omit_lock_prefix = 1;
9820 else if (strcasecmp (arg, "no") == 0)
9821 omit_lock_prefix = 0;
9822 else
9823 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
9824 break;
9825
9826 default:
9827 return 0;
9828 }
9829 return 1;
9830 }
9831
9832 #define MESSAGE_TEMPLATE \
9833 " "
9834
9835 static void
9836 show_arch (FILE *stream, int ext, int check)
9837 {
9838 static char message[] = MESSAGE_TEMPLATE;
9839 char *start = message + 27;
9840 char *p;
9841 int size = sizeof (MESSAGE_TEMPLATE);
9842 int left;
9843 const char *name;
9844 int len;
9845 unsigned int j;
9846
9847 p = start;
9848 left = size - (start - message);
9849 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9850 {
9851 /* Should it be skipped? */
9852 if (cpu_arch [j].skip)
9853 continue;
9854
9855 name = cpu_arch [j].name;
9856 len = cpu_arch [j].len;
9857 if (*name == '.')
9858 {
9859 /* It is an extension. Skip if we aren't asked to show it. */
9860 if (ext)
9861 {
9862 name++;
9863 len--;
9864 }
9865 else
9866 continue;
9867 }
9868 else if (ext)
9869 {
9870 /* It is an processor. Skip if we show only extension. */
9871 continue;
9872 }
9873 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
9874 {
9875 /* It is an impossible processor - skip. */
9876 continue;
9877 }
9878
9879 /* Reserve 2 spaces for ", " or ",\0" */
9880 left -= len + 2;
9881
9882 /* Check if there is any room. */
9883 if (left >= 0)
9884 {
9885 if (p != start)
9886 {
9887 *p++ = ',';
9888 *p++ = ' ';
9889 }
9890 p = mempcpy (p, name, len);
9891 }
9892 else
9893 {
9894 /* Output the current message now and start a new one. */
9895 *p++ = ',';
9896 *p = '\0';
9897 fprintf (stream, "%s\n", message);
9898 p = start;
9899 left = size - (start - message) - len - 2;
9900
9901 gas_assert (left >= 0);
9902
9903 p = mempcpy (p, name, len);
9904 }
9905 }
9906
9907 *p = '\0';
9908 fprintf (stream, "%s\n", message);
9909 }
9910
9911 void
9912 md_show_usage (FILE *stream)
9913 {
9914 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9915 fprintf (stream, _("\
9916 -Q ignored\n\
9917 -V print assembler version number\n\
9918 -k ignored\n"));
9919 #endif
9920 fprintf (stream, _("\
9921 -n Do not optimize code alignment\n\
9922 -q quieten some warnings\n"));
9923 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9924 fprintf (stream, _("\
9925 -s ignored\n"));
9926 #endif
9927 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
9928 || defined (TE_PE) || defined (TE_PEP))
9929 fprintf (stream, _("\
9930 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
9931 #endif
9932 #ifdef SVR4_COMMENT_CHARS
9933 fprintf (stream, _("\
9934 --divide do not treat `/' as a comment character\n"));
9935 #else
9936 fprintf (stream, _("\
9937 --divide ignored\n"));
9938 #endif
9939 fprintf (stream, _("\
9940 -march=CPU[,+EXTENSION...]\n\
9941 generate code for CPU and EXTENSION, CPU is one of:\n"));
9942 show_arch (stream, 0, 1);
9943 fprintf (stream, _("\
9944 EXTENSION is combination of:\n"));
9945 show_arch (stream, 1, 0);
9946 fprintf (stream, _("\
9947 -mtune=CPU optimize for CPU, CPU is one of:\n"));
9948 show_arch (stream, 0, 0);
9949 fprintf (stream, _("\
9950 -msse2avx encode SSE instructions with VEX prefix\n"));
9951 fprintf (stream, _("\
9952 -msse-check=[none|error|warning]\n\
9953 check SSE instructions\n"));
9954 fprintf (stream, _("\
9955 -moperand-check=[none|error|warning]\n\
9956 check operand combinations for validity\n"));
9957 fprintf (stream, _("\
9958 -mavxscalar=[128|256] encode scalar AVX instructions with specific vector\n\
9959 length\n"));
9960 fprintf (stream, _("\
9961 -mevexlig=[128|256|512] encode scalar EVEX instructions with specific vector\n\
9962 length\n"));
9963 fprintf (stream, _("\
9964 -mevexwig=[0|1] encode EVEX instructions with specific EVEX.W value\n\
9965 for EVEX.W bit ignored instructions\n"));
9966 fprintf (stream, _("\
9967 -mevexrcig=[rne|rd|ru|rz]\n\
9968 encode EVEX instructions with specific EVEX.RC value\n\
9969 for SAE-only ignored instructions\n"));
9970 fprintf (stream, _("\
9971 -mmnemonic=[att|intel] use AT&T/Intel mnemonic\n"));
9972 fprintf (stream, _("\
9973 -msyntax=[att|intel] use AT&T/Intel syntax\n"));
9974 fprintf (stream, _("\
9975 -mindex-reg support pseudo index registers\n"));
9976 fprintf (stream, _("\
9977 -mnaked-reg don't require `%%' prefix for registers\n"));
9978 fprintf (stream, _("\
9979 -mold-gcc support old (<= 2.8.1) versions of gcc\n"));
9980 fprintf (stream, _("\
9981 -madd-bnd-prefix add BND prefix for all valid branches\n"));
9982 # if defined (TE_PE) || defined (TE_PEP)
9983 fprintf (stream, _("\
9984 -mbig-obj generate big object files\n"));
9985 #endif
9986 fprintf (stream, _("\
9987 -momit-lock-prefix=[no|yes]\n\
9988 strip all lock prefixes\n"));
9989 }
9990
9991 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
9992 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
9993 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
9994
9995 /* Pick the target format to use. */
9996
9997 const char *
9998 i386_target_format (void)
9999 {
10000 if (!strncmp (default_arch, "x86_64", 6))
10001 {
10002 update_code_flag (CODE_64BIT, 1);
10003 if (default_arch[6] == '\0')
10004 x86_elf_abi = X86_64_ABI;
10005 else
10006 x86_elf_abi = X86_64_X32_ABI;
10007 }
10008 else if (!strcmp (default_arch, "i386"))
10009 update_code_flag (CODE_32BIT, 1);
10010 else
10011 as_fatal (_("unknown architecture"));
10012
10013 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
10014 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
10015 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
10016 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
10017
10018 switch (OUTPUT_FLAVOR)
10019 {
10020 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
10021 case bfd_target_aout_flavour:
10022 return AOUT_TARGET_FORMAT;
10023 #endif
10024 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
10025 # if defined (TE_PE) || defined (TE_PEP)
10026 case bfd_target_coff_flavour:
10027 if (flag_code == CODE_64BIT)
10028 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
10029 else
10030 return "pe-i386";
10031 # elif defined (TE_GO32)
10032 case bfd_target_coff_flavour:
10033 return "coff-go32";
10034 # else
10035 case bfd_target_coff_flavour:
10036 return "coff-i386";
10037 # endif
10038 #endif
10039 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
10040 case bfd_target_elf_flavour:
10041 {
10042 const char *format;
10043
10044 switch (x86_elf_abi)
10045 {
10046 default:
10047 format = ELF_TARGET_FORMAT;
10048 break;
10049 case X86_64_ABI:
10050 use_rela_relocations = 1;
10051 object_64bit = 1;
10052 format = ELF_TARGET_FORMAT64;
10053 break;
10054 case X86_64_X32_ABI:
10055 use_rela_relocations = 1;
10056 object_64bit = 1;
10057 disallow_64bit_reloc = 1;
10058 format = ELF_TARGET_FORMAT32;
10059 break;
10060 }
10061 if (cpu_arch_isa == PROCESSOR_L1OM)
10062 {
10063 if (x86_elf_abi != X86_64_ABI)
10064 as_fatal (_("Intel L1OM is 64bit only"));
10065 return ELF_TARGET_L1OM_FORMAT;
10066 }
10067 if (cpu_arch_isa == PROCESSOR_K1OM)
10068 {
10069 if (x86_elf_abi != X86_64_ABI)
10070 as_fatal (_("Intel K1OM is 64bit only"));
10071 return ELF_TARGET_K1OM_FORMAT;
10072 }
10073 else
10074 return format;
10075 }
10076 #endif
10077 #if defined (OBJ_MACH_O)
10078 case bfd_target_mach_o_flavour:
10079 if (flag_code == CODE_64BIT)
10080 {
10081 use_rela_relocations = 1;
10082 object_64bit = 1;
10083 return "mach-o-x86-64";
10084 }
10085 else
10086 return "mach-o-i386";
10087 #endif
10088 default:
10089 abort ();
10090 return NULL;
10091 }
10092 }
10093
10094 #endif /* OBJ_MAYBE_ more than one */
10095
10096 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
10097 void
10098 i386_elf_emit_arch_note (void)
10099 {
10100 if (IS_ELF && cpu_arch_name != NULL)
10101 {
10102 char *p;
10103 asection *seg = now_seg;
10104 subsegT subseg = now_subseg;
10105 Elf_Internal_Note i_note;
10106 Elf_External_Note e_note;
10107 asection *note_secp;
10108 int len;
10109
10110 /* Create the .note section. */
10111 note_secp = subseg_new (".note", 0);
10112 bfd_set_section_flags (stdoutput,
10113 note_secp,
10114 SEC_HAS_CONTENTS | SEC_READONLY);
10115
10116 /* Process the arch string. */
10117 len = strlen (cpu_arch_name);
10118
10119 i_note.namesz = len + 1;
10120 i_note.descsz = 0;
10121 i_note.type = NT_ARCH;
10122 p = frag_more (sizeof (e_note.namesz));
10123 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
10124 p = frag_more (sizeof (e_note.descsz));
10125 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
10126 p = frag_more (sizeof (e_note.type));
10127 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
10128 p = frag_more (len + 1);
10129 strcpy (p, cpu_arch_name);
10130
10131 frag_align (2, 0, 0);
10132
10133 subseg_set (seg, subseg);
10134 }
10135 }
10136 #endif
10137 \f
10138 symbolS *
10139 md_undefined_symbol (char *name)
10140 {
10141 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
10142 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
10143 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
10144 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
10145 {
10146 if (!GOT_symbol)
10147 {
10148 if (symbol_find (name))
10149 as_bad (_("GOT already in symbol table"));
10150 GOT_symbol = symbol_new (name, undefined_section,
10151 (valueT) 0, &zero_address_frag);
10152 };
10153 return GOT_symbol;
10154 }
10155 return 0;
10156 }
10157
10158 /* Round up a section size to the appropriate boundary. */
10159
10160 valueT
10161 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
10162 {
10163 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
10164 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
10165 {
10166 /* For a.out, force the section size to be aligned. If we don't do
10167 this, BFD will align it for us, but it will not write out the
10168 final bytes of the section. This may be a bug in BFD, but it is
10169 easier to fix it here since that is how the other a.out targets
10170 work. */
10171 int align;
10172
10173 align = bfd_get_section_alignment (stdoutput, segment);
10174 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
10175 }
10176 #endif
10177
10178 return size;
10179 }
10180
10181 /* On the i386, PC-relative offsets are relative to the start of the
10182 next instruction. That is, the address of the offset, plus its
10183 size, since the offset is always the last part of the insn. */
10184
10185 long
10186 md_pcrel_from (fixS *fixP)
10187 {
10188 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10189 }
10190
10191 #ifndef I386COFF
10192
10193 static void
10194 s_bss (int ignore ATTRIBUTE_UNUSED)
10195 {
10196 int temp;
10197
10198 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10199 if (IS_ELF)
10200 obj_elf_section_change_hook ();
10201 #endif
10202 temp = get_absolute_expression ();
10203 subseg_set (bss_section, (subsegT) temp);
10204 demand_empty_rest_of_line ();
10205 }
10206
10207 #endif
10208
10209 void
10210 i386_validate_fix (fixS *fixp)
10211 {
10212 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
10213 {
10214 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
10215 {
10216 if (!object_64bit)
10217 abort ();
10218 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
10219 }
10220 else
10221 {
10222 if (!object_64bit)
10223 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
10224 else
10225 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
10226 }
10227 fixp->fx_subsy = 0;
10228 }
10229 }
10230
10231 arelent *
10232 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
10233 {
10234 arelent *rel;
10235 bfd_reloc_code_real_type code;
10236
10237 switch (fixp->fx_r_type)
10238 {
10239 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10240 case BFD_RELOC_SIZE32:
10241 case BFD_RELOC_SIZE64:
10242 if (S_IS_DEFINED (fixp->fx_addsy)
10243 && !S_IS_EXTERNAL (fixp->fx_addsy))
10244 {
10245 /* Resolve size relocation against local symbol to size of
10246 the symbol plus addend. */
10247 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
10248 if (fixp->fx_r_type == BFD_RELOC_SIZE32
10249 && !fits_in_unsigned_long (value))
10250 as_bad_where (fixp->fx_file, fixp->fx_line,
10251 _("symbol size computation overflow"));
10252 fixp->fx_addsy = NULL;
10253 fixp->fx_subsy = NULL;
10254 md_apply_fix (fixp, (valueT *) &value, NULL);
10255 return NULL;
10256 }
10257 #endif
10258
10259 case BFD_RELOC_X86_64_PLT32:
10260 case BFD_RELOC_X86_64_GOT32:
10261 case BFD_RELOC_X86_64_GOTPCREL:
10262 case BFD_RELOC_386_PLT32:
10263 case BFD_RELOC_386_GOT32:
10264 case BFD_RELOC_386_GOTOFF:
10265 case BFD_RELOC_386_GOTPC:
10266 case BFD_RELOC_386_TLS_GD:
10267 case BFD_RELOC_386_TLS_LDM:
10268 case BFD_RELOC_386_TLS_LDO_32:
10269 case BFD_RELOC_386_TLS_IE_32:
10270 case BFD_RELOC_386_TLS_IE:
10271 case BFD_RELOC_386_TLS_GOTIE:
10272 case BFD_RELOC_386_TLS_LE_32:
10273 case BFD_RELOC_386_TLS_LE:
10274 case BFD_RELOC_386_TLS_GOTDESC:
10275 case BFD_RELOC_386_TLS_DESC_CALL:
10276 case BFD_RELOC_X86_64_TLSGD:
10277 case BFD_RELOC_X86_64_TLSLD:
10278 case BFD_RELOC_X86_64_DTPOFF32:
10279 case BFD_RELOC_X86_64_DTPOFF64:
10280 case BFD_RELOC_X86_64_GOTTPOFF:
10281 case BFD_RELOC_X86_64_TPOFF32:
10282 case BFD_RELOC_X86_64_TPOFF64:
10283 case BFD_RELOC_X86_64_GOTOFF64:
10284 case BFD_RELOC_X86_64_GOTPC32:
10285 case BFD_RELOC_X86_64_GOT64:
10286 case BFD_RELOC_X86_64_GOTPCREL64:
10287 case BFD_RELOC_X86_64_GOTPC64:
10288 case BFD_RELOC_X86_64_GOTPLT64:
10289 case BFD_RELOC_X86_64_PLTOFF64:
10290 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10291 case BFD_RELOC_X86_64_TLSDESC_CALL:
10292 case BFD_RELOC_RVA:
10293 case BFD_RELOC_VTABLE_ENTRY:
10294 case BFD_RELOC_VTABLE_INHERIT:
10295 #ifdef TE_PE
10296 case BFD_RELOC_32_SECREL:
10297 #endif
10298 code = fixp->fx_r_type;
10299 break;
10300 case BFD_RELOC_X86_64_32S:
10301 if (!fixp->fx_pcrel)
10302 {
10303 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
10304 code = fixp->fx_r_type;
10305 break;
10306 }
10307 default:
10308 if (fixp->fx_pcrel)
10309 {
10310 switch (fixp->fx_size)
10311 {
10312 default:
10313 as_bad_where (fixp->fx_file, fixp->fx_line,
10314 _("can not do %d byte pc-relative relocation"),
10315 fixp->fx_size);
10316 code = BFD_RELOC_32_PCREL;
10317 break;
10318 case 1: code = BFD_RELOC_8_PCREL; break;
10319 case 2: code = BFD_RELOC_16_PCREL; break;
10320 case 4: code = BFD_RELOC_32_PCREL; break;
10321 #ifdef BFD64
10322 case 8: code = BFD_RELOC_64_PCREL; break;
10323 #endif
10324 }
10325 }
10326 else
10327 {
10328 switch (fixp->fx_size)
10329 {
10330 default:
10331 as_bad_where (fixp->fx_file, fixp->fx_line,
10332 _("can not do %d byte relocation"),
10333 fixp->fx_size);
10334 code = BFD_RELOC_32;
10335 break;
10336 case 1: code = BFD_RELOC_8; break;
10337 case 2: code = BFD_RELOC_16; break;
10338 case 4: code = BFD_RELOC_32; break;
10339 #ifdef BFD64
10340 case 8: code = BFD_RELOC_64; break;
10341 #endif
10342 }
10343 }
10344 break;
10345 }
10346
10347 if ((code == BFD_RELOC_32
10348 || code == BFD_RELOC_32_PCREL
10349 || code == BFD_RELOC_X86_64_32S)
10350 && GOT_symbol
10351 && fixp->fx_addsy == GOT_symbol)
10352 {
10353 if (!object_64bit)
10354 code = BFD_RELOC_386_GOTPC;
10355 else
10356 code = BFD_RELOC_X86_64_GOTPC32;
10357 }
10358 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
10359 && GOT_symbol
10360 && fixp->fx_addsy == GOT_symbol)
10361 {
10362 code = BFD_RELOC_X86_64_GOTPC64;
10363 }
10364
10365 rel = (arelent *) xmalloc (sizeof (arelent));
10366 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
10367 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
10368
10369 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
10370
10371 if (!use_rela_relocations)
10372 {
10373 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
10374 vtable entry to be used in the relocation's section offset. */
10375 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10376 rel->address = fixp->fx_offset;
10377 #if defined (OBJ_COFF) && defined (TE_PE)
10378 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
10379 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
10380 else
10381 #endif
10382 rel->addend = 0;
10383 }
10384 /* Use the rela in 64bit mode. */
10385 else
10386 {
10387 if (disallow_64bit_reloc)
10388 switch (code)
10389 {
10390 case BFD_RELOC_X86_64_DTPOFF64:
10391 case BFD_RELOC_X86_64_TPOFF64:
10392 case BFD_RELOC_64_PCREL:
10393 case BFD_RELOC_X86_64_GOTOFF64:
10394 case BFD_RELOC_X86_64_GOT64:
10395 case BFD_RELOC_X86_64_GOTPCREL64:
10396 case BFD_RELOC_X86_64_GOTPC64:
10397 case BFD_RELOC_X86_64_GOTPLT64:
10398 case BFD_RELOC_X86_64_PLTOFF64:
10399 as_bad_where (fixp->fx_file, fixp->fx_line,
10400 _("cannot represent relocation type %s in x32 mode"),
10401 bfd_get_reloc_code_name (code));
10402 break;
10403 default:
10404 break;
10405 }
10406
10407 if (!fixp->fx_pcrel)
10408 rel->addend = fixp->fx_offset;
10409 else
10410 switch (code)
10411 {
10412 case BFD_RELOC_X86_64_PLT32:
10413 case BFD_RELOC_X86_64_GOT32:
10414 case BFD_RELOC_X86_64_GOTPCREL:
10415 case BFD_RELOC_X86_64_TLSGD:
10416 case BFD_RELOC_X86_64_TLSLD:
10417 case BFD_RELOC_X86_64_GOTTPOFF:
10418 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10419 case BFD_RELOC_X86_64_TLSDESC_CALL:
10420 rel->addend = fixp->fx_offset - fixp->fx_size;
10421 break;
10422 default:
10423 rel->addend = (section->vma
10424 - fixp->fx_size
10425 + fixp->fx_addnumber
10426 + md_pcrel_from (fixp));
10427 break;
10428 }
10429 }
10430
10431 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
10432 if (rel->howto == NULL)
10433 {
10434 as_bad_where (fixp->fx_file, fixp->fx_line,
10435 _("cannot represent relocation type %s"),
10436 bfd_get_reloc_code_name (code));
10437 /* Set howto to a garbage value so that we can keep going. */
10438 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
10439 gas_assert (rel->howto != NULL);
10440 }
10441
10442 return rel;
10443 }
10444
10445 #include "tc-i386-intel.c"
10446
10447 void
10448 tc_x86_parse_to_dw2regnum (expressionS *exp)
10449 {
10450 int saved_naked_reg;
10451 char saved_register_dot;
10452
10453 saved_naked_reg = allow_naked_reg;
10454 allow_naked_reg = 1;
10455 saved_register_dot = register_chars['.'];
10456 register_chars['.'] = '.';
10457 allow_pseudo_reg = 1;
10458 expression_and_evaluate (exp);
10459 allow_pseudo_reg = 0;
10460 register_chars['.'] = saved_register_dot;
10461 allow_naked_reg = saved_naked_reg;
10462
10463 if (exp->X_op == O_register && exp->X_add_number >= 0)
10464 {
10465 if ((addressT) exp->X_add_number < i386_regtab_size)
10466 {
10467 exp->X_op = O_constant;
10468 exp->X_add_number = i386_regtab[exp->X_add_number]
10469 .dw2_regnum[flag_code >> 1];
10470 }
10471 else
10472 exp->X_op = O_illegal;
10473 }
10474 }
10475
10476 void
10477 tc_x86_frame_initial_instructions (void)
10478 {
10479 static unsigned int sp_regno[2];
10480
10481 if (!sp_regno[flag_code >> 1])
10482 {
10483 char *saved_input = input_line_pointer;
10484 char sp[][4] = {"esp", "rsp"};
10485 expressionS exp;
10486
10487 input_line_pointer = sp[flag_code >> 1];
10488 tc_x86_parse_to_dw2regnum (&exp);
10489 gas_assert (exp.X_op == O_constant);
10490 sp_regno[flag_code >> 1] = exp.X_add_number;
10491 input_line_pointer = saved_input;
10492 }
10493
10494 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
10495 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
10496 }
10497
10498 int
10499 x86_dwarf2_addr_size (void)
10500 {
10501 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
10502 if (x86_elf_abi == X86_64_X32_ABI)
10503 return 4;
10504 #endif
10505 return bfd_arch_bits_per_address (stdoutput) / 8;
10506 }
10507
10508 int
10509 i386_elf_section_type (const char *str, size_t len)
10510 {
10511 if (flag_code == CODE_64BIT
10512 && len == sizeof ("unwind") - 1
10513 && strncmp (str, "unwind", 6) == 0)
10514 return SHT_X86_64_UNWIND;
10515
10516 return -1;
10517 }
10518
10519 #ifdef TE_SOLARIS
10520 void
10521 i386_solaris_fix_up_eh_frame (segT sec)
10522 {
10523 if (flag_code == CODE_64BIT)
10524 elf_section_type (sec) = SHT_X86_64_UNWIND;
10525 }
10526 #endif
10527
10528 #ifdef TE_PE
10529 void
10530 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
10531 {
10532 expressionS exp;
10533
10534 exp.X_op = O_secrel;
10535 exp.X_add_symbol = symbol;
10536 exp.X_add_number = 0;
10537 emit_expr (&exp, size);
10538 }
10539 #endif
10540
10541 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10542 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
10543
10544 bfd_vma
10545 x86_64_section_letter (int letter, char **ptr_msg)
10546 {
10547 if (flag_code == CODE_64BIT)
10548 {
10549 if (letter == 'l')
10550 return SHF_X86_64_LARGE;
10551
10552 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
10553 }
10554 else
10555 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
10556 return -1;
10557 }
10558
10559 bfd_vma
10560 x86_64_section_word (char *str, size_t len)
10561 {
10562 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
10563 return SHF_X86_64_LARGE;
10564
10565 return -1;
10566 }
10567
10568 static void
10569 handle_large_common (int small ATTRIBUTE_UNUSED)
10570 {
10571 if (flag_code != CODE_64BIT)
10572 {
10573 s_comm_internal (0, elf_common_parse);
10574 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
10575 }
10576 else
10577 {
10578 static segT lbss_section;
10579 asection *saved_com_section_ptr = elf_com_section_ptr;
10580 asection *saved_bss_section = bss_section;
10581
10582 if (lbss_section == NULL)
10583 {
10584 flagword applicable;
10585 segT seg = now_seg;
10586 subsegT subseg = now_subseg;
10587
10588 /* The .lbss section is for local .largecomm symbols. */
10589 lbss_section = subseg_new (".lbss", 0);
10590 applicable = bfd_applicable_section_flags (stdoutput);
10591 bfd_set_section_flags (stdoutput, lbss_section,
10592 applicable & SEC_ALLOC);
10593 seg_info (lbss_section)->bss = 1;
10594
10595 subseg_set (seg, subseg);
10596 }
10597
10598 elf_com_section_ptr = &_bfd_elf_large_com_section;
10599 bss_section = lbss_section;
10600
10601 s_comm_internal (0, elf_common_parse);
10602
10603 elf_com_section_ptr = saved_com_section_ptr;
10604 bss_section = saved_bss_section;
10605 }
10606 }
10607 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 0.25496 seconds and 4 git commands to generate.