binutils/
[binutils.git] / gas / config / tc-i386.c
blobe8e8031811d7f1bda34cc3fd82be54fefb9846cf
1 /* tc-i386.c -- Assemble code for the Intel 80386
2 Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* Intel 80386 machine specific gas.
24 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
25 x86_64 support by Jan Hubicka (jh@suse.cz)
26 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
27 Bugs & suggestions are completely welcome. This is free software.
28 Please help us make it better. */
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "subsegs.h"
33 #include "dwarf2dbg.h"
34 #include "dw2gencfi.h"
35 #include "elf/x86-64.h"
36 #include "opcodes/i386-init.h"
38 #ifndef REGISTER_WARNINGS
39 #define REGISTER_WARNINGS 1
40 #endif
42 #ifndef INFER_ADDR_PREFIX
43 #define INFER_ADDR_PREFIX 1
44 #endif
46 #ifndef DEFAULT_ARCH
47 #define DEFAULT_ARCH "i386"
48 #endif
50 #ifndef INLINE
51 #if __GNUC__ >= 2
52 #define INLINE __inline__
53 #else
54 #define INLINE
55 #endif
56 #endif
58 /* Prefixes will be emitted in the order defined below.
59 WAIT_PREFIX must be the first prefix since FWAIT is really is an
60 instruction, and so must come before any prefixes.
61 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
62 LOCKREP_PREFIX. */
63 #define WAIT_PREFIX 0
64 #define SEG_PREFIX 1
65 #define ADDR_PREFIX 2
66 #define DATA_PREFIX 3
67 #define LOCKREP_PREFIX 4
68 #define REX_PREFIX 5 /* must come last. */
69 #define MAX_PREFIXES 6 /* max prefixes per opcode */
71 /* we define the syntax here (modulo base,index,scale syntax) */
72 #define REGISTER_PREFIX '%'
73 #define IMMEDIATE_PREFIX '$'
74 #define ABSOLUTE_PREFIX '*'
76 /* these are the instruction mnemonic suffixes in AT&T syntax or
77 memory operand size in Intel syntax. */
78 #define WORD_MNEM_SUFFIX 'w'
79 #define BYTE_MNEM_SUFFIX 'b'
80 #define SHORT_MNEM_SUFFIX 's'
81 #define LONG_MNEM_SUFFIX 'l'
82 #define QWORD_MNEM_SUFFIX 'q'
83 #define XMMWORD_MNEM_SUFFIX 'x'
84 #define YMMWORD_MNEM_SUFFIX 'y'
85 /* Intel Syntax. Use a non-ascii letter since since it never appears
86 in instructions. */
87 #define LONG_DOUBLE_MNEM_SUFFIX '\1'
89 #define END_OF_INSN '\0'
92 'templates' is for grouping together 'template' structures for opcodes
93 of the same name. This is only used for storing the insns in the grand
94 ole hash table of insns.
95 The templates themselves start at START and range up to (but not including)
96 END.
98 typedef struct
100 const template *start;
101 const template *end;
103 templates;
105 /* 386 operand encoding bytes: see 386 book for details of this. */
106 typedef struct
108 unsigned int regmem; /* codes register or memory operand */
109 unsigned int reg; /* codes register operand (or extended opcode) */
110 unsigned int mode; /* how to interpret regmem & reg */
112 modrm_byte;
114 /* x86-64 extension prefix. */
115 typedef int rex_byte;
117 /* The SSE5 instructions have a two bit instruction modifier (OC) that
118 is stored in two separate bytes in the instruction. Pick apart OC
119 into the 2 separate bits for instruction. */
120 #define DREX_OC0(x) (((x) & 1) != 0)
121 #define DREX_OC1(x) (((x) & 2) != 0)
123 #define DREX_OC0_MASK (1 << 3) /* set OC0 in byte 4 */
124 #define DREX_OC1_MASK (1 << 2) /* set OC1 in byte 3 */
126 /* OC mappings */
127 #define DREX_XMEM_X1_X2_X2 0 /* 4 op insn, dest = src3, src1 = reg/mem */
128 #define DREX_X1_XMEM_X2_X2 1 /* 4 op insn, dest = src3, src2 = reg/mem */
129 #define DREX_X1_XMEM_X2_X1 2 /* 4 op insn, dest = src1, src2 = reg/mem */
130 #define DREX_X1_X2_XMEM_X1 3 /* 4 op insn, dest = src1, src3 = reg/mem */
132 #define DREX_XMEM_X1_X2 0 /* 3 op insn, src1 = reg/mem */
133 #define DREX_X1_XMEM_X2 1 /* 3 op insn, src1 = reg/mem */
135 /* Information needed to create the DREX byte in SSE5 instructions. */
136 typedef struct
138 unsigned int reg; /* register */
139 unsigned int rex; /* REX flags */
140 unsigned int modrm_reg; /* which arg goes in the modrm.reg field */
141 unsigned int modrm_regmem; /* which arg goes in the modrm.regmem field */
142 } drex_byte;
144 /* 386 opcode byte to code indirect addressing. */
145 typedef struct
147 unsigned base;
148 unsigned index;
149 unsigned scale;
151 sib_byte;
153 enum processor_type
155 PROCESSOR_UNKNOWN,
156 PROCESSOR_I386,
157 PROCESSOR_I486,
158 PROCESSOR_PENTIUM,
159 PROCESSOR_PENTIUMPRO,
160 PROCESSOR_PENTIUM4,
161 PROCESSOR_NOCONA,
162 PROCESSOR_CORE,
163 PROCESSOR_CORE2,
164 PROCESSOR_K6,
165 PROCESSOR_ATHLON,
166 PROCESSOR_K8,
167 PROCESSOR_GENERIC32,
168 PROCESSOR_GENERIC64,
169 PROCESSOR_AMDFAM10
172 /* x86 arch names, types and features */
173 typedef struct
175 const char *name; /* arch name */
176 enum processor_type type; /* arch type */
177 i386_cpu_flags flags; /* cpu feature flags */
179 arch_entry;
181 static void set_code_flag (int);
182 static void set_16bit_gcc_code_flag (int);
183 static void set_intel_syntax (int);
184 static void set_intel_mnemonic (int);
185 static void set_allow_index_reg (int);
186 static void set_cpu_arch (int);
187 #ifdef TE_PE
188 static void pe_directive_secrel (int);
189 #endif
190 static void signed_cons (int);
191 static char *output_invalid (int c);
192 static int i386_att_operand (char *);
193 static int i386_intel_operand (char *, int);
194 static const reg_entry *parse_register (char *, char **);
195 static char *parse_insn (char *, char *);
196 static char *parse_operands (char *, const char *);
197 static void swap_operands (void);
198 static void swap_2_operands (int, int);
199 static void optimize_imm (void);
200 static void optimize_disp (void);
201 static int match_template (void);
202 static int check_string (void);
203 static int process_suffix (void);
204 static int check_byte_reg (void);
205 static int check_long_reg (void);
206 static int check_qword_reg (void);
207 static int check_word_reg (void);
208 static int finalize_imm (void);
209 static void process_drex (void);
210 static int process_operands (void);
211 static const seg_entry *build_modrm_byte (void);
212 static void output_insn (void);
213 static void output_imm (fragS *, offsetT);
214 static void output_disp (fragS *, offsetT);
215 #ifndef I386COFF
216 static void s_bss (int);
217 #endif
218 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
219 static void handle_large_common (int small ATTRIBUTE_UNUSED);
220 #endif
222 static const char *default_arch = DEFAULT_ARCH;
224 /* VEX prefix. */
225 typedef struct
227 /* VEX prefix is either 2 byte or 3 byte. */
228 unsigned char bytes[3];
229 unsigned int length;
230 /* Destination or source register specifier. */
231 const reg_entry *register_specifier;
232 } vex_prefix;
234 /* 'md_assemble ()' gathers together information and puts it into a
235 i386_insn. */
237 union i386_op
239 expressionS *disps;
240 expressionS *imms;
241 const reg_entry *regs;
244 struct _i386_insn
246 /* TM holds the template for the insn were currently assembling. */
247 template tm;
249 /* SUFFIX holds the instruction size suffix for byte, word, dword
250 or qword, if given. */
251 char suffix;
253 /* OPERANDS gives the number of given operands. */
254 unsigned int operands;
256 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
257 of given register, displacement, memory operands and immediate
258 operands. */
259 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
261 /* TYPES [i] is the type (see above #defines) which tells us how to
262 use OP[i] for the corresponding operand. */
263 i386_operand_type types[MAX_OPERANDS];
265 /* Displacement expression, immediate expression, or register for each
266 operand. */
267 union i386_op op[MAX_OPERANDS];
269 /* Flags for operands. */
270 unsigned int flags[MAX_OPERANDS];
271 #define Operand_PCrel 1
273 /* Relocation type for operand */
274 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
276 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
277 the base index byte below. */
278 const reg_entry *base_reg;
279 const reg_entry *index_reg;
280 unsigned int log2_scale_factor;
282 /* SEG gives the seg_entries of this insn. They are zero unless
283 explicit segment overrides are given. */
284 const seg_entry *seg[2];
286 /* PREFIX holds all the given prefix opcodes (usually null).
287 PREFIXES is the number of prefix opcodes. */
288 unsigned int prefixes;
289 unsigned char prefix[MAX_PREFIXES];
291 /* RM and SIB are the modrm byte and the sib byte where the
292 addressing modes of this insn are encoded. DREX is the byte
293 added by the SSE5 instructions. */
295 modrm_byte rm;
296 rex_byte rex;
297 sib_byte sib;
298 drex_byte drex;
299 vex_prefix vex;
302 typedef struct _i386_insn i386_insn;
304 /* List of chars besides those in app.c:symbol_chars that can start an
305 operand. Used to prevent the scrubber eating vital white-space. */
306 const char extra_symbol_chars[] = "*%-(["
307 #ifdef LEX_AT
309 #endif
310 #ifdef LEX_QM
312 #endif
315 #if (defined (TE_I386AIX) \
316 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
317 && !defined (TE_GNU) \
318 && !defined (TE_LINUX) \
319 && !defined (TE_NETWARE) \
320 && !defined (TE_FreeBSD) \
321 && !defined (TE_NetBSD)))
322 /* This array holds the chars that always start a comment. If the
323 pre-processor is disabled, these aren't very useful. The option
324 --divide will remove '/' from this list. */
325 const char *i386_comment_chars = "#/";
326 #define SVR4_COMMENT_CHARS 1
327 #define PREFIX_SEPARATOR '\\'
329 #else
330 const char *i386_comment_chars = "#";
331 #define PREFIX_SEPARATOR '/'
332 #endif
334 /* This array holds the chars that only start a comment at the beginning of
335 a line. If the line seems to have the form '# 123 filename'
336 .line and .file directives will appear in the pre-processed output.
337 Note that input_file.c hand checks for '#' at the beginning of the
338 first line of the input file. This is because the compiler outputs
339 #NO_APP at the beginning of its output.
340 Also note that comments started like this one will always work if
341 '/' isn't otherwise defined. */
342 const char line_comment_chars[] = "#/";
344 const char line_separator_chars[] = ";";
346 /* Chars that can be used to separate mant from exp in floating point
347 nums. */
348 const char EXP_CHARS[] = "eE";
350 /* Chars that mean this number is a floating point constant
351 As in 0f12.456
352 or 0d1.2345e12. */
353 const char FLT_CHARS[] = "fFdDxX";
355 /* Tables for lexical analysis. */
356 static char mnemonic_chars[256];
357 static char register_chars[256];
358 static char operand_chars[256];
359 static char identifier_chars[256];
360 static char digit_chars[256];
362 /* Lexical macros. */
363 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
364 #define is_operand_char(x) (operand_chars[(unsigned char) x])
365 #define is_register_char(x) (register_chars[(unsigned char) x])
366 #define is_space_char(x) ((x) == ' ')
367 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
368 #define is_digit_char(x) (digit_chars[(unsigned char) x])
370 /* All non-digit non-letter characters that may occur in an operand. */
371 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
373 /* md_assemble() always leaves the strings it's passed unaltered. To
374 effect this we maintain a stack of saved characters that we've smashed
375 with '\0's (indicating end of strings for various sub-fields of the
376 assembler instruction). */
377 static char save_stack[32];
378 static char *save_stack_p;
379 #define END_STRING_AND_SAVE(s) \
380 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
381 #define RESTORE_END_STRING(s) \
382 do { *(s) = *--save_stack_p; } while (0)
384 /* The instruction we're assembling. */
385 static i386_insn i;
387 /* Possible templates for current insn. */
388 static const templates *current_templates;
390 /* Per instruction expressionS buffers: max displacements & immediates. */
391 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
392 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
394 /* Current operand we are working on. */
395 static int this_operand;
397 /* We support four different modes. FLAG_CODE variable is used to distinguish
398 these. */
400 enum flag_code {
401 CODE_32BIT,
402 CODE_16BIT,
403 CODE_64BIT };
405 static enum flag_code flag_code;
406 static unsigned int object_64bit;
407 static int use_rela_relocations = 0;
409 /* The names used to print error messages. */
410 static const char *flag_code_names[] =
412 "32",
413 "16",
414 "64"
417 /* 1 for intel syntax,
418 0 if att syntax. */
419 static int intel_syntax = 0;
421 /* 1 for intel mnemonic,
422 0 if att mnemonic. */
423 static int intel_mnemonic = !SYSV386_COMPAT;
425 /* 1 if support old (<= 2.8.1) versions of gcc. */
426 static int old_gcc = OLDGCC_COMPAT;
428 /* 1 if pseudo registers are permitted. */
429 static int allow_pseudo_reg = 0;
431 /* 1 if register prefix % not required. */
432 static int allow_naked_reg = 0;
434 /* 1 if pseudo index register, eiz/riz, is allowed . */
435 static int allow_index_reg = 0;
437 /* Register prefix used for error message. */
438 static const char *register_prefix = "%";
440 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
441 leave, push, and pop instructions so that gcc has the same stack
442 frame as in 32 bit mode. */
443 static char stackop_size = '\0';
445 /* Non-zero to optimize code alignment. */
446 int optimize_align_code = 1;
448 /* Non-zero to quieten some warnings. */
449 static int quiet_warnings = 0;
451 /* CPU name. */
452 static const char *cpu_arch_name = NULL;
453 static char *cpu_sub_arch_name = NULL;
455 /* CPU feature flags. */
456 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
458 /* If we have selected a cpu we are generating instructions for. */
459 static int cpu_arch_tune_set = 0;
461 /* Cpu we are generating instructions for. */
462 static enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
464 /* CPU feature flags of cpu we are generating instructions for. */
465 static i386_cpu_flags cpu_arch_tune_flags;
467 /* CPU instruction set architecture used. */
468 static enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
470 /* CPU feature flags of instruction set architecture used. */
471 static i386_cpu_flags cpu_arch_isa_flags;
473 /* If set, conditional jumps are not automatically promoted to handle
474 larger than a byte offset. */
475 static unsigned int no_cond_jump_promotion = 0;
477 /* Encode SSE instructions with VEX prefix. */
478 static unsigned int sse2avx;
480 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
481 static symbolS *GOT_symbol;
483 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
484 unsigned int x86_dwarf2_return_column;
486 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
487 int x86_cie_data_alignment;
489 /* Interface to relax_segment.
490 There are 3 major relax states for 386 jump insns because the
491 different types of jumps add different sizes to frags when we're
492 figuring out what sort of jump to choose to reach a given label. */
494 /* Types. */
495 #define UNCOND_JUMP 0
496 #define COND_JUMP 1
497 #define COND_JUMP86 2
499 /* Sizes. */
500 #define CODE16 1
501 #define SMALL 0
502 #define SMALL16 (SMALL | CODE16)
503 #define BIG 2
504 #define BIG16 (BIG | CODE16)
506 #ifndef INLINE
507 #ifdef __GNUC__
508 #define INLINE __inline__
509 #else
510 #define INLINE
511 #endif
512 #endif
514 #define ENCODE_RELAX_STATE(type, size) \
515 ((relax_substateT) (((type) << 2) | (size)))
516 #define TYPE_FROM_RELAX_STATE(s) \
517 ((s) >> 2)
518 #define DISP_SIZE_FROM_RELAX_STATE(s) \
519 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
521 /* This table is used by relax_frag to promote short jumps to long
522 ones where necessary. SMALL (short) jumps may be promoted to BIG
523 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
524 don't allow a short jump in a 32 bit code segment to be promoted to
525 a 16 bit offset jump because it's slower (requires data size
526 prefix), and doesn't work, unless the destination is in the bottom
527 64k of the code segment (The top 16 bits of eip are zeroed). */
529 const relax_typeS md_relax_table[] =
531 /* The fields are:
532 1) most positive reach of this state,
533 2) most negative reach of this state,
534 3) how many bytes this mode will have in the variable part of the frag
535 4) which index into the table to try if we can't fit into this one. */
537 /* UNCOND_JUMP states. */
538 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
539 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
540 /* dword jmp adds 4 bytes to frag:
541 0 extra opcode bytes, 4 displacement bytes. */
542 {0, 0, 4, 0},
543 /* word jmp adds 2 byte2 to frag:
544 0 extra opcode bytes, 2 displacement bytes. */
545 {0, 0, 2, 0},
547 /* COND_JUMP states. */
548 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
549 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
550 /* dword conditionals adds 5 bytes to frag:
551 1 extra opcode byte, 4 displacement bytes. */
552 {0, 0, 5, 0},
553 /* word conditionals add 3 bytes to frag:
554 1 extra opcode byte, 2 displacement bytes. */
555 {0, 0, 3, 0},
557 /* COND_JUMP86 states. */
558 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
559 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
560 /* dword conditionals adds 5 bytes to frag:
561 1 extra opcode byte, 4 displacement bytes. */
562 {0, 0, 5, 0},
563 /* word conditionals add 4 bytes to frag:
564 1 displacement byte and a 3 byte long branch insn. */
565 {0, 0, 4, 0}
568 static const arch_entry cpu_arch[] =
570 { "generic32", PROCESSOR_GENERIC32,
571 CPU_GENERIC32_FLAGS },
572 { "generic64", PROCESSOR_GENERIC64,
573 CPU_GENERIC64_FLAGS },
574 { "i8086", PROCESSOR_UNKNOWN,
575 CPU_NONE_FLAGS },
576 { "i186", PROCESSOR_UNKNOWN,
577 CPU_I186_FLAGS },
578 { "i286", PROCESSOR_UNKNOWN,
579 CPU_I286_FLAGS },
580 { "i386", PROCESSOR_I386,
581 CPU_I386_FLAGS },
582 { "i486", PROCESSOR_I486,
583 CPU_I486_FLAGS },
584 { "i586", PROCESSOR_PENTIUM,
585 CPU_I586_FLAGS },
586 { "i686", PROCESSOR_PENTIUMPRO,
587 CPU_I686_FLAGS },
588 { "pentium", PROCESSOR_PENTIUM,
589 CPU_I586_FLAGS },
590 { "pentiumpro", PROCESSOR_PENTIUMPRO,
591 CPU_I686_FLAGS },
592 { "pentiumii", PROCESSOR_PENTIUMPRO,
593 CPU_P2_FLAGS },
594 { "pentiumiii",PROCESSOR_PENTIUMPRO,
595 CPU_P3_FLAGS },
596 { "pentium4", PROCESSOR_PENTIUM4,
597 CPU_P4_FLAGS },
598 { "prescott", PROCESSOR_NOCONA,
599 CPU_CORE_FLAGS },
600 { "nocona", PROCESSOR_NOCONA,
601 CPU_NOCONA_FLAGS },
602 { "yonah", PROCESSOR_CORE,
603 CPU_CORE_FLAGS },
604 { "core", PROCESSOR_CORE,
605 CPU_CORE_FLAGS },
606 { "merom", PROCESSOR_CORE2,
607 CPU_CORE2_FLAGS },
608 { "core2", PROCESSOR_CORE2,
609 CPU_CORE2_FLAGS },
610 { "k6", PROCESSOR_K6,
611 CPU_K6_FLAGS },
612 { "k6_2", PROCESSOR_K6,
613 CPU_K6_2_FLAGS },
614 { "athlon", PROCESSOR_ATHLON,
615 CPU_ATHLON_FLAGS },
616 { "sledgehammer", PROCESSOR_K8,
617 CPU_K8_FLAGS },
618 { "opteron", PROCESSOR_K8,
619 CPU_K8_FLAGS },
620 { "k8", PROCESSOR_K8,
621 CPU_K8_FLAGS },
622 { "amdfam10", PROCESSOR_AMDFAM10,
623 CPU_AMDFAM10_FLAGS },
624 { ".mmx", PROCESSOR_UNKNOWN,
625 CPU_MMX_FLAGS },
626 { ".sse", PROCESSOR_UNKNOWN,
627 CPU_SSE_FLAGS },
628 { ".sse2", PROCESSOR_UNKNOWN,
629 CPU_SSE2_FLAGS },
630 { ".sse3", PROCESSOR_UNKNOWN,
631 CPU_SSE3_FLAGS },
632 { ".ssse3", PROCESSOR_UNKNOWN,
633 CPU_SSSE3_FLAGS },
634 { ".sse4.1", PROCESSOR_UNKNOWN,
635 CPU_SSE4_1_FLAGS },
636 { ".sse4.2", PROCESSOR_UNKNOWN,
637 CPU_SSE4_2_FLAGS },
638 { ".sse4", PROCESSOR_UNKNOWN,
639 CPU_SSE4_2_FLAGS },
640 { ".avx", PROCESSOR_UNKNOWN,
641 CPU_AVX_FLAGS },
642 { ".vmx", PROCESSOR_UNKNOWN,
643 CPU_VMX_FLAGS },
644 { ".smx", PROCESSOR_UNKNOWN,
645 CPU_SMX_FLAGS },
646 { ".xsave", PROCESSOR_UNKNOWN,
647 CPU_XSAVE_FLAGS },
648 { ".aes", PROCESSOR_UNKNOWN,
649 CPU_AES_FLAGS },
650 { ".clmul", PROCESSOR_UNKNOWN,
651 CPU_CLMUL_FLAGS },
652 { ".fma", PROCESSOR_UNKNOWN,
653 CPU_FMA_FLAGS },
654 { ".3dnow", PROCESSOR_UNKNOWN,
655 CPU_3DNOW_FLAGS },
656 { ".3dnowa", PROCESSOR_UNKNOWN,
657 CPU_3DNOWA_FLAGS },
658 { ".padlock", PROCESSOR_UNKNOWN,
659 CPU_PADLOCK_FLAGS },
660 { ".pacifica", PROCESSOR_UNKNOWN,
661 CPU_SVME_FLAGS },
662 { ".svme", PROCESSOR_UNKNOWN,
663 CPU_SVME_FLAGS },
664 { ".sse4a", PROCESSOR_UNKNOWN,
665 CPU_SSE4A_FLAGS },
666 { ".abm", PROCESSOR_UNKNOWN,
667 CPU_ABM_FLAGS },
668 { ".sse5", PROCESSOR_UNKNOWN,
669 CPU_SSE5_FLAGS },
672 const pseudo_typeS md_pseudo_table[] =
674 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
675 {"align", s_align_bytes, 0},
676 #else
677 {"align", s_align_ptwo, 0},
678 #endif
679 {"arch", set_cpu_arch, 0},
680 #ifndef I386COFF
681 {"bss", s_bss, 0},
682 #endif
683 {"ffloat", float_cons, 'f'},
684 {"dfloat", float_cons, 'd'},
685 {"tfloat", float_cons, 'x'},
686 {"value", cons, 2},
687 {"slong", signed_cons, 4},
688 {"noopt", s_ignore, 0},
689 {"optim", s_ignore, 0},
690 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
691 {"code16", set_code_flag, CODE_16BIT},
692 {"code32", set_code_flag, CODE_32BIT},
693 {"code64", set_code_flag, CODE_64BIT},
694 {"intel_syntax", set_intel_syntax, 1},
695 {"att_syntax", set_intel_syntax, 0},
696 {"intel_mnemonic", set_intel_mnemonic, 1},
697 {"att_mnemonic", set_intel_mnemonic, 0},
698 {"allow_index_reg", set_allow_index_reg, 1},
699 {"disallow_index_reg", set_allow_index_reg, 0},
700 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
701 {"largecomm", handle_large_common, 0},
702 #else
703 {"file", (void (*) (int)) dwarf2_directive_file, 0},
704 {"loc", dwarf2_directive_loc, 0},
705 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
706 #endif
707 #ifdef TE_PE
708 {"secrel32", pe_directive_secrel, 0},
709 #endif
710 {0, 0, 0}
713 /* For interface with expression (). */
714 extern char *input_line_pointer;
716 /* Hash table for instruction mnemonic lookup. */
717 static struct hash_control *op_hash;
719 /* Hash table for register lookup. */
720 static struct hash_control *reg_hash;
722 void
723 i386_align_code (fragS *fragP, int count)
725 /* Various efficient no-op patterns for aligning code labels.
726 Note: Don't try to assemble the instructions in the comments.
727 0L and 0w are not legal. */
728 static const char f32_1[] =
729 {0x90}; /* nop */
730 static const char f32_2[] =
731 {0x66,0x90}; /* xchg %ax,%ax */
732 static const char f32_3[] =
733 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
734 static const char f32_4[] =
735 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
736 static const char f32_5[] =
737 {0x90, /* nop */
738 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
739 static const char f32_6[] =
740 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
741 static const char f32_7[] =
742 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
743 static const char f32_8[] =
744 {0x90, /* nop */
745 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
746 static const char f32_9[] =
747 {0x89,0xf6, /* movl %esi,%esi */
748 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
749 static const char f32_10[] =
750 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
751 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
752 static const char f32_11[] =
753 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
754 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
755 static const char f32_12[] =
756 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
757 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
758 static const char f32_13[] =
759 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
760 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
761 static const char f32_14[] =
762 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
763 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
764 static const char f16_3[] =
765 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
766 static const char f16_4[] =
767 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
768 static const char f16_5[] =
769 {0x90, /* nop */
770 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
771 static const char f16_6[] =
772 {0x89,0xf6, /* mov %si,%si */
773 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
774 static const char f16_7[] =
775 {0x8d,0x74,0x00, /* lea 0(%si),%si */
776 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
777 static const char f16_8[] =
778 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
779 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
780 static const char jump_31[] =
781 {0xeb,0x1d,0x90,0x90,0x90,0x90,0x90, /* jmp .+31; lotsa nops */
782 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
783 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
784 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
785 static const char *const f32_patt[] = {
786 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
787 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14
789 static const char *const f16_patt[] = {
790 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8
792 /* nopl (%[re]ax) */
793 static const char alt_3[] =
794 {0x0f,0x1f,0x00};
795 /* nopl 0(%[re]ax) */
796 static const char alt_4[] =
797 {0x0f,0x1f,0x40,0x00};
798 /* nopl 0(%[re]ax,%[re]ax,1) */
799 static const char alt_5[] =
800 {0x0f,0x1f,0x44,0x00,0x00};
801 /* nopw 0(%[re]ax,%[re]ax,1) */
802 static const char alt_6[] =
803 {0x66,0x0f,0x1f,0x44,0x00,0x00};
804 /* nopl 0L(%[re]ax) */
805 static const char alt_7[] =
806 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
807 /* nopl 0L(%[re]ax,%[re]ax,1) */
808 static const char alt_8[] =
809 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
810 /* nopw 0L(%[re]ax,%[re]ax,1) */
811 static const char alt_9[] =
812 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
813 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
814 static const char alt_10[] =
815 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
816 /* data16
817 nopw %cs:0L(%[re]ax,%[re]ax,1) */
818 static const char alt_long_11[] =
819 {0x66,
820 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
821 /* data16
822 data16
823 nopw %cs:0L(%[re]ax,%[re]ax,1) */
824 static const char alt_long_12[] =
825 {0x66,
826 0x66,
827 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
828 /* data16
829 data16
830 data16
831 nopw %cs:0L(%[re]ax,%[re]ax,1) */
832 static const char alt_long_13[] =
833 {0x66,
834 0x66,
835 0x66,
836 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
837 /* data16
838 data16
839 data16
840 data16
841 nopw %cs:0L(%[re]ax,%[re]ax,1) */
842 static const char alt_long_14[] =
843 {0x66,
844 0x66,
845 0x66,
846 0x66,
847 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
848 /* data16
849 data16
850 data16
851 data16
852 data16
853 nopw %cs:0L(%[re]ax,%[re]ax,1) */
854 static const char alt_long_15[] =
855 {0x66,
856 0x66,
857 0x66,
858 0x66,
859 0x66,
860 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
861 /* nopl 0(%[re]ax,%[re]ax,1)
862 nopw 0(%[re]ax,%[re]ax,1) */
863 static const char alt_short_11[] =
864 {0x0f,0x1f,0x44,0x00,0x00,
865 0x66,0x0f,0x1f,0x44,0x00,0x00};
866 /* nopw 0(%[re]ax,%[re]ax,1)
867 nopw 0(%[re]ax,%[re]ax,1) */
868 static const char alt_short_12[] =
869 {0x66,0x0f,0x1f,0x44,0x00,0x00,
870 0x66,0x0f,0x1f,0x44,0x00,0x00};
871 /* nopw 0(%[re]ax,%[re]ax,1)
872 nopl 0L(%[re]ax) */
873 static const char alt_short_13[] =
874 {0x66,0x0f,0x1f,0x44,0x00,0x00,
875 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
876 /* nopl 0L(%[re]ax)
877 nopl 0L(%[re]ax) */
878 static const char alt_short_14[] =
879 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
880 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
881 /* nopl 0L(%[re]ax)
882 nopl 0L(%[re]ax,%[re]ax,1) */
883 static const char alt_short_15[] =
884 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
885 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
886 static const char *const alt_short_patt[] = {
887 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
888 alt_9, alt_10, alt_short_11, alt_short_12, alt_short_13,
889 alt_short_14, alt_short_15
891 static const char *const alt_long_patt[] = {
892 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
893 alt_9, alt_10, alt_long_11, alt_long_12, alt_long_13,
894 alt_long_14, alt_long_15
897 /* Only align for at least a positive non-zero boundary. */
898 if (count <= 0 || count > MAX_MEM_FOR_RS_ALIGN_CODE)
899 return;
901 /* We need to decide which NOP sequence to use for 32bit and
902 64bit. When -mtune= is used:
904 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
905 PROCESSOR_GENERIC32, f32_patt will be used.
906 2. For PROCESSOR_PENTIUMPRO, PROCESSOR_PENTIUM4, PROCESSOR_NOCONA,
907 PROCESSOR_CORE, PROCESSOR_CORE2, and PROCESSOR_GENERIC64,
908 alt_long_patt will be used.
909 3. For PROCESSOR_ATHLON, PROCESSOR_K6, PROCESSOR_K8 and
910 PROCESSOR_AMDFAM10, alt_short_patt will be used.
912 When -mtune= isn't used, alt_long_patt will be used if
913 cpu_arch_isa_flags has Cpu686. Otherwise, f32_patt will
914 be used.
916 When -march= or .arch is used, we can't use anything beyond
917 cpu_arch_isa_flags. */
919 if (flag_code == CODE_16BIT)
921 if (count > 8)
923 memcpy (fragP->fr_literal + fragP->fr_fix,
924 jump_31, count);
925 /* Adjust jump offset. */
926 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
928 else
929 memcpy (fragP->fr_literal + fragP->fr_fix,
930 f16_patt[count - 1], count);
932 else
934 const char *const *patt = NULL;
936 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
938 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
939 switch (cpu_arch_tune)
941 case PROCESSOR_UNKNOWN:
942 /* We use cpu_arch_isa_flags to check if we SHOULD
943 optimize for Cpu686. */
944 if (cpu_arch_isa_flags.bitfield.cpui686)
945 patt = alt_long_patt;
946 else
947 patt = f32_patt;
948 break;
949 case PROCESSOR_PENTIUMPRO:
950 case PROCESSOR_PENTIUM4:
951 case PROCESSOR_NOCONA:
952 case PROCESSOR_CORE:
953 case PROCESSOR_CORE2:
954 case PROCESSOR_GENERIC64:
955 patt = alt_long_patt;
956 break;
957 case PROCESSOR_K6:
958 case PROCESSOR_ATHLON:
959 case PROCESSOR_K8:
960 case PROCESSOR_AMDFAM10:
961 patt = alt_short_patt;
962 break;
963 case PROCESSOR_I386:
964 case PROCESSOR_I486:
965 case PROCESSOR_PENTIUM:
966 case PROCESSOR_GENERIC32:
967 patt = f32_patt;
968 break;
971 else
973 switch (cpu_arch_tune)
975 case PROCESSOR_UNKNOWN:
976 /* When cpu_arch_isa is net, cpu_arch_tune shouldn't be
977 PROCESSOR_UNKNOWN. */
978 abort ();
979 break;
981 case PROCESSOR_I386:
982 case PROCESSOR_I486:
983 case PROCESSOR_PENTIUM:
984 case PROCESSOR_K6:
985 case PROCESSOR_ATHLON:
986 case PROCESSOR_K8:
987 case PROCESSOR_AMDFAM10:
988 case PROCESSOR_GENERIC32:
989 /* We use cpu_arch_isa_flags to check if we CAN optimize
990 for Cpu686. */
991 if (cpu_arch_isa_flags.bitfield.cpui686)
992 patt = alt_short_patt;
993 else
994 patt = f32_patt;
995 break;
996 case PROCESSOR_PENTIUMPRO:
997 case PROCESSOR_PENTIUM4:
998 case PROCESSOR_NOCONA:
999 case PROCESSOR_CORE:
1000 case PROCESSOR_CORE2:
1001 if (cpu_arch_isa_flags.bitfield.cpui686)
1002 patt = alt_long_patt;
1003 else
1004 patt = f32_patt;
1005 break;
1006 case PROCESSOR_GENERIC64:
1007 patt = alt_long_patt;
1008 break;
1012 if (patt == f32_patt)
1014 /* If the padding is less than 15 bytes, we use the normal
1015 ones. Otherwise, we use a jump instruction and adjust
1016 its offset. */
1017 if (count < 15)
1018 memcpy (fragP->fr_literal + fragP->fr_fix,
1019 patt[count - 1], count);
1020 else
1022 memcpy (fragP->fr_literal + fragP->fr_fix,
1023 jump_31, count);
1024 /* Adjust jump offset. */
1025 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
1028 else
1030 /* Maximum length of an instruction is 15 byte. If the
1031 padding is greater than 15 bytes and we don't use jump,
1032 we have to break it into smaller pieces. */
1033 int padding = count;
1034 while (padding > 15)
1036 padding -= 15;
1037 memcpy (fragP->fr_literal + fragP->fr_fix + padding,
1038 patt [14], 15);
1041 if (padding)
1042 memcpy (fragP->fr_literal + fragP->fr_fix,
1043 patt [padding - 1], padding);
1046 fragP->fr_var = count;
1049 static INLINE int
1050 operand_type_all_zero (const union i386_operand_type *x)
1052 switch (ARRAY_SIZE(x->array))
1054 case 3:
1055 if (x->array[2])
1056 return 0;
1057 case 2:
1058 if (x->array[1])
1059 return 0;
1060 case 1:
1061 return !x->array[0];
1062 default:
1063 abort ();
1067 static INLINE void
1068 operand_type_set (union i386_operand_type *x, unsigned int v)
1070 switch (ARRAY_SIZE(x->array))
1072 case 3:
1073 x->array[2] = v;
1074 case 2:
1075 x->array[1] = v;
1076 case 1:
1077 x->array[0] = v;
1078 break;
1079 default:
1080 abort ();
1084 static INLINE int
1085 operand_type_equal (const union i386_operand_type *x,
1086 const union i386_operand_type *y)
1088 switch (ARRAY_SIZE(x->array))
1090 case 3:
1091 if (x->array[2] != y->array[2])
1092 return 0;
1093 case 2:
1094 if (x->array[1] != y->array[1])
1095 return 0;
1096 case 1:
1097 return x->array[0] == y->array[0];
1098 break;
1099 default:
1100 abort ();
1104 static INLINE int
1105 cpu_flags_all_zero (const union i386_cpu_flags *x)
1107 switch (ARRAY_SIZE(x->array))
1109 case 3:
1110 if (x->array[2])
1111 return 0;
1112 case 2:
1113 if (x->array[1])
1114 return 0;
1115 case 1:
1116 return !x->array[0];
1117 default:
1118 abort ();
1122 static INLINE void
1123 cpu_flags_set (union i386_cpu_flags *x, unsigned int v)
1125 switch (ARRAY_SIZE(x->array))
1127 case 3:
1128 x->array[2] = v;
1129 case 2:
1130 x->array[1] = v;
1131 case 1:
1132 x->array[0] = v;
1133 break;
1134 default:
1135 abort ();
1139 static INLINE int
1140 cpu_flags_equal (const union i386_cpu_flags *x,
1141 const union i386_cpu_flags *y)
1143 switch (ARRAY_SIZE(x->array))
1145 case 3:
1146 if (x->array[2] != y->array[2])
1147 return 0;
1148 case 2:
1149 if (x->array[1] != y->array[1])
1150 return 0;
1151 case 1:
1152 return x->array[0] == y->array[0];
1153 break;
1154 default:
1155 abort ();
1159 static INLINE int
1160 cpu_flags_check_cpu64 (i386_cpu_flags f)
1162 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1163 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1166 static INLINE i386_cpu_flags
1167 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1169 switch (ARRAY_SIZE (x.array))
1171 case 3:
1172 x.array [2] &= y.array [2];
1173 case 2:
1174 x.array [1] &= y.array [1];
1175 case 1:
1176 x.array [0] &= y.array [0];
1177 break;
1178 default:
1179 abort ();
1181 return x;
1184 static INLINE i386_cpu_flags
1185 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1187 switch (ARRAY_SIZE (x.array))
1189 case 3:
1190 x.array [2] |= y.array [2];
1191 case 2:
1192 x.array [1] |= y.array [1];
1193 case 1:
1194 x.array [0] |= y.array [0];
1195 break;
1196 default:
1197 abort ();
1199 return x;
1202 #define CPU_FLAGS_ARCH_MATCH 0x1
1203 #define CPU_FLAGS_64BIT_MATCH 0x2
1205 #define CPU_FLAGS_32BIT_MATCH CPU_FLAGS_ARCH_MATCH
1206 #define CPU_FLAGS_PERFECT_MATCH \
1207 (CPU_FLAGS_32BIT_MATCH | CPU_FLAGS_64BIT_MATCH)
1209 /* Return CPU flags match bits. */
1211 static int
1212 cpu_flags_match (const template *t)
1214 i386_cpu_flags x = t->cpu_flags;
1215 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1217 x.bitfield.cpu64 = 0;
1218 x.bitfield.cpuno64 = 0;
1220 if (cpu_flags_all_zero (&x))
1222 /* This instruction is available on all archs. */
1223 match |= CPU_FLAGS_32BIT_MATCH;
1225 else
1227 /* This instruction is available only on some archs. */
1228 i386_cpu_flags cpu = cpu_arch_flags;
1230 cpu.bitfield.cpu64 = 0;
1231 cpu.bitfield.cpuno64 = 0;
1232 cpu = cpu_flags_and (x, cpu);
1233 if (!cpu_flags_all_zero (&cpu))
1235 /* Check SSE2AVX */
1236 if (!t->opcode_modifier.sse2avx || sse2avx)
1237 match |= CPU_FLAGS_32BIT_MATCH;
1240 return match;
1243 static INLINE i386_operand_type
1244 operand_type_and (i386_operand_type x, i386_operand_type y)
1246 switch (ARRAY_SIZE (x.array))
1248 case 3:
1249 x.array [2] &= y.array [2];
1250 case 2:
1251 x.array [1] &= y.array [1];
1252 case 1:
1253 x.array [0] &= y.array [0];
1254 break;
1255 default:
1256 abort ();
1258 return x;
1261 static INLINE i386_operand_type
1262 operand_type_or (i386_operand_type x, i386_operand_type y)
1264 switch (ARRAY_SIZE (x.array))
1266 case 3:
1267 x.array [2] |= y.array [2];
1268 case 2:
1269 x.array [1] |= y.array [1];
1270 case 1:
1271 x.array [0] |= y.array [0];
1272 break;
1273 default:
1274 abort ();
1276 return x;
1279 static INLINE i386_operand_type
1280 operand_type_xor (i386_operand_type x, i386_operand_type y)
1282 switch (ARRAY_SIZE (x.array))
1284 case 3:
1285 x.array [2] ^= y.array [2];
1286 case 2:
1287 x.array [1] ^= y.array [1];
1288 case 1:
1289 x.array [0] ^= y.array [0];
1290 break;
1291 default:
1292 abort ();
1294 return x;
1297 static const i386_operand_type acc32 = OPERAND_TYPE_ACC32;
1298 static const i386_operand_type acc64 = OPERAND_TYPE_ACC64;
1299 static const i386_operand_type control = OPERAND_TYPE_CONTROL;
1300 static const i386_operand_type inoutportreg
1301 = OPERAND_TYPE_INOUTPORTREG;
1302 static const i386_operand_type reg16_inoutportreg
1303 = OPERAND_TYPE_REG16_INOUTPORTREG;
1304 static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
1305 static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
1306 static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
1307 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
1308 static const i386_operand_type anydisp
1309 = OPERAND_TYPE_ANYDISP;
1310 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1311 static const i386_operand_type regymm = OPERAND_TYPE_REGYMM;
1312 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1313 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1314 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1315 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1316 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1317 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1318 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1319 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1320 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1321 static const i386_operand_type vex_imm4 = OPERAND_TYPE_VEX_IMM4;
1323 enum operand_type
1325 reg,
1326 imm,
1327 disp,
1328 anymem
1331 static INLINE int
1332 operand_type_check (i386_operand_type t, enum operand_type c)
1334 switch (c)
1336 case reg:
1337 return (t.bitfield.reg8
1338 || t.bitfield.reg16
1339 || t.bitfield.reg32
1340 || t.bitfield.reg64);
1342 case imm:
1343 return (t.bitfield.imm8
1344 || t.bitfield.imm8s
1345 || t.bitfield.imm16
1346 || t.bitfield.imm32
1347 || t.bitfield.imm32s
1348 || t.bitfield.imm64);
1350 case disp:
1351 return (t.bitfield.disp8
1352 || t.bitfield.disp16
1353 || t.bitfield.disp32
1354 || t.bitfield.disp32s
1355 || t.bitfield.disp64);
1357 case anymem:
1358 return (t.bitfield.disp8
1359 || t.bitfield.disp16
1360 || t.bitfield.disp32
1361 || t.bitfield.disp32s
1362 || t.bitfield.disp64
1363 || t.bitfield.baseindex);
1365 default:
1366 abort ();
1370 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit on
1371 operand J for instruction template T. */
1373 static INLINE int
1374 match_reg_size (const template *t, unsigned int j)
1376 return !((i.types[j].bitfield.byte
1377 && !t->operand_types[j].bitfield.byte)
1378 || (i.types[j].bitfield.word
1379 && !t->operand_types[j].bitfield.word)
1380 || (i.types[j].bitfield.dword
1381 && !t->operand_types[j].bitfield.dword)
1382 || (i.types[j].bitfield.qword
1383 && !t->operand_types[j].bitfield.qword));
1386 /* Return 1 if there is no conflict in any size on operand J for
1387 instruction template T. */
1389 static INLINE int
1390 match_mem_size (const template *t, unsigned int j)
1392 return (match_reg_size (t, j)
1393 && !((i.types[j].bitfield.unspecified
1394 && !t->operand_types[j].bitfield.unspecified)
1395 || (i.types[j].bitfield.fword
1396 && !t->operand_types[j].bitfield.fword)
1397 || (i.types[j].bitfield.tbyte
1398 && !t->operand_types[j].bitfield.tbyte)
1399 || (i.types[j].bitfield.xmmword
1400 && !t->operand_types[j].bitfield.xmmword)
1401 || (i.types[j].bitfield.ymmword
1402 && !t->operand_types[j].bitfield.ymmword)));
1405 /* Return 1 if there is no size conflict on any operands for
1406 instruction template T. */
1408 static INLINE int
1409 operand_size_match (const template *t)
1411 unsigned int j;
1412 int match = 1;
1414 /* Don't check jump instructions. */
1415 if (t->opcode_modifier.jump
1416 || t->opcode_modifier.jumpbyte
1417 || t->opcode_modifier.jumpdword
1418 || t->opcode_modifier.jumpintersegment)
1419 return match;
1421 /* Check memory and accumulator operand size. */
1422 for (j = 0; j < i.operands; j++)
1424 if (t->operand_types[j].bitfield.anysize)
1425 continue;
1427 if (t->operand_types[j].bitfield.acc && !match_reg_size (t, j))
1429 match = 0;
1430 break;
1433 if (i.types[j].bitfield.mem && !match_mem_size (t, j))
1435 match = 0;
1436 break;
1440 if (match
1441 || (!t->opcode_modifier.d && !t->opcode_modifier.floatd))
1442 return match;
1444 /* Check reverse. */
1445 assert (i.operands == 2);
1447 match = 1;
1448 for (j = 0; j < 2; j++)
1450 if (t->operand_types[j].bitfield.acc
1451 && !match_reg_size (t, j ? 0 : 1))
1453 match = 0;
1454 break;
1457 if (i.types[j].bitfield.mem
1458 && !match_mem_size (t, j ? 0 : 1))
1460 match = 0;
1461 break;
1465 return match;
1468 static INLINE int
1469 operand_type_match (i386_operand_type overlap,
1470 i386_operand_type given)
1472 i386_operand_type temp = overlap;
1474 temp.bitfield.jumpabsolute = 0;
1475 temp.bitfield.unspecified = 0;
1476 temp.bitfield.byte = 0;
1477 temp.bitfield.word = 0;
1478 temp.bitfield.dword = 0;
1479 temp.bitfield.fword = 0;
1480 temp.bitfield.qword = 0;
1481 temp.bitfield.tbyte = 0;
1482 temp.bitfield.xmmword = 0;
1483 temp.bitfield.ymmword = 0;
1484 if (operand_type_all_zero (&temp))
1485 return 0;
1487 return (given.bitfield.baseindex == overlap.bitfield.baseindex
1488 && given.bitfield.jumpabsolute == overlap.bitfield.jumpabsolute);
1491 /* If given types g0 and g1 are registers they must be of the same type
1492 unless the expected operand type register overlap is null.
1493 Note that Acc in a template matches every size of reg. */
1495 static INLINE int
1496 operand_type_register_match (i386_operand_type m0,
1497 i386_operand_type g0,
1498 i386_operand_type t0,
1499 i386_operand_type m1,
1500 i386_operand_type g1,
1501 i386_operand_type t1)
1503 if (!operand_type_check (g0, reg))
1504 return 1;
1506 if (!operand_type_check (g1, reg))
1507 return 1;
1509 if (g0.bitfield.reg8 == g1.bitfield.reg8
1510 && g0.bitfield.reg16 == g1.bitfield.reg16
1511 && g0.bitfield.reg32 == g1.bitfield.reg32
1512 && g0.bitfield.reg64 == g1.bitfield.reg64)
1513 return 1;
1515 if (m0.bitfield.acc)
1517 t0.bitfield.reg8 = 1;
1518 t0.bitfield.reg16 = 1;
1519 t0.bitfield.reg32 = 1;
1520 t0.bitfield.reg64 = 1;
1523 if (m1.bitfield.acc)
1525 t1.bitfield.reg8 = 1;
1526 t1.bitfield.reg16 = 1;
1527 t1.bitfield.reg32 = 1;
1528 t1.bitfield.reg64 = 1;
1531 return (!(t0.bitfield.reg8 & t1.bitfield.reg8)
1532 && !(t0.bitfield.reg16 & t1.bitfield.reg16)
1533 && !(t0.bitfield.reg32 & t1.bitfield.reg32)
1534 && !(t0.bitfield.reg64 & t1.bitfield.reg64));
1537 static INLINE unsigned int
1538 mode_from_disp_size (i386_operand_type t)
1540 if (t.bitfield.disp8)
1541 return 1;
1542 else if (t.bitfield.disp16
1543 || t.bitfield.disp32
1544 || t.bitfield.disp32s)
1545 return 2;
1546 else
1547 return 0;
1550 static INLINE int
1551 fits_in_signed_byte (offsetT num)
1553 return (num >= -128) && (num <= 127);
1556 static INLINE int
1557 fits_in_unsigned_byte (offsetT num)
1559 return (num & 0xff) == num;
1562 static INLINE int
1563 fits_in_unsigned_word (offsetT num)
1565 return (num & 0xffff) == num;
1568 static INLINE int
1569 fits_in_signed_word (offsetT num)
1571 return (-32768 <= num) && (num <= 32767);
1574 static INLINE int
1575 fits_in_signed_long (offsetT num ATTRIBUTE_UNUSED)
1577 #ifndef BFD64
1578 return 1;
1579 #else
1580 return (!(((offsetT) -1 << 31) & num)
1581 || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
1582 #endif
1583 } /* fits_in_signed_long() */
1585 static INLINE int
1586 fits_in_unsigned_long (offsetT num ATTRIBUTE_UNUSED)
1588 #ifndef BFD64
1589 return 1;
1590 #else
1591 return (num & (((offsetT) 2 << 31) - 1)) == num;
1592 #endif
1593 } /* fits_in_unsigned_long() */
1595 static INLINE int
1596 fits_in_imm4 (offsetT num)
1598 return (num & 0xf) == num;
1601 static i386_operand_type
1602 smallest_imm_type (offsetT num)
1604 i386_operand_type t;
1606 operand_type_set (&t, 0);
1607 t.bitfield.imm64 = 1;
1609 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
1611 /* This code is disabled on the 486 because all the Imm1 forms
1612 in the opcode table are slower on the i486. They're the
1613 versions with the implicitly specified single-position
1614 displacement, which has another syntax if you really want to
1615 use that form. */
1616 t.bitfield.imm1 = 1;
1617 t.bitfield.imm8 = 1;
1618 t.bitfield.imm8s = 1;
1619 t.bitfield.imm16 = 1;
1620 t.bitfield.imm32 = 1;
1621 t.bitfield.imm32s = 1;
1623 else if (fits_in_signed_byte (num))
1625 t.bitfield.imm8 = 1;
1626 t.bitfield.imm8s = 1;
1627 t.bitfield.imm16 = 1;
1628 t.bitfield.imm32 = 1;
1629 t.bitfield.imm32s = 1;
1631 else if (fits_in_unsigned_byte (num))
1633 t.bitfield.imm8 = 1;
1634 t.bitfield.imm16 = 1;
1635 t.bitfield.imm32 = 1;
1636 t.bitfield.imm32s = 1;
1638 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
1640 t.bitfield.imm16 = 1;
1641 t.bitfield.imm32 = 1;
1642 t.bitfield.imm32s = 1;
1644 else if (fits_in_signed_long (num))
1646 t.bitfield.imm32 = 1;
1647 t.bitfield.imm32s = 1;
1649 else if (fits_in_unsigned_long (num))
1650 t.bitfield.imm32 = 1;
1652 return t;
1655 static offsetT
1656 offset_in_range (offsetT val, int size)
1658 addressT mask;
1660 switch (size)
1662 case 1: mask = ((addressT) 1 << 8) - 1; break;
1663 case 2: mask = ((addressT) 1 << 16) - 1; break;
1664 case 4: mask = ((addressT) 2 << 31) - 1; break;
1665 #ifdef BFD64
1666 case 8: mask = ((addressT) 2 << 63) - 1; break;
1667 #endif
1668 default: abort ();
1671 /* If BFD64, sign extend val. */
1672 if (!use_rela_relocations)
1673 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
1674 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
1676 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
1678 char buf1[40], buf2[40];
1680 sprint_value (buf1, val);
1681 sprint_value (buf2, val & mask);
1682 as_warn (_("%s shortened to %s"), buf1, buf2);
1684 return val & mask;
1687 /* Returns 0 if attempting to add a prefix where one from the same
1688 class already exists, 1 if non rep/repne added, 2 if rep/repne
1689 added. */
1690 static int
1691 add_prefix (unsigned int prefix)
1693 int ret = 1;
1694 unsigned int q;
1696 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
1697 && flag_code == CODE_64BIT)
1699 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
1700 || ((i.prefix[REX_PREFIX] & (REX_R | REX_X | REX_B))
1701 && (prefix & (REX_R | REX_X | REX_B))))
1702 ret = 0;
1703 q = REX_PREFIX;
1705 else
1707 switch (prefix)
1709 default:
1710 abort ();
1712 case CS_PREFIX_OPCODE:
1713 case DS_PREFIX_OPCODE:
1714 case ES_PREFIX_OPCODE:
1715 case FS_PREFIX_OPCODE:
1716 case GS_PREFIX_OPCODE:
1717 case SS_PREFIX_OPCODE:
1718 q = SEG_PREFIX;
1719 break;
1721 case REPNE_PREFIX_OPCODE:
1722 case REPE_PREFIX_OPCODE:
1723 ret = 2;
1724 /* fall thru */
1725 case LOCK_PREFIX_OPCODE:
1726 q = LOCKREP_PREFIX;
1727 break;
1729 case FWAIT_OPCODE:
1730 q = WAIT_PREFIX;
1731 break;
1733 case ADDR_PREFIX_OPCODE:
1734 q = ADDR_PREFIX;
1735 break;
1737 case DATA_PREFIX_OPCODE:
1738 q = DATA_PREFIX;
1739 break;
1741 if (i.prefix[q] != 0)
1742 ret = 0;
1745 if (ret)
1747 if (!i.prefix[q])
1748 ++i.prefixes;
1749 i.prefix[q] |= prefix;
1751 else
1752 as_bad (_("same type of prefix used twice"));
1754 return ret;
1757 static void
1758 set_code_flag (int value)
1760 flag_code = value;
1761 if (flag_code == CODE_64BIT)
1763 cpu_arch_flags.bitfield.cpu64 = 1;
1764 cpu_arch_flags.bitfield.cpuno64 = 0;
1766 else
1768 cpu_arch_flags.bitfield.cpu64 = 0;
1769 cpu_arch_flags.bitfield.cpuno64 = 1;
1771 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
1773 as_bad (_("64bit mode not supported on this CPU."));
1775 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
1777 as_bad (_("32bit mode not supported on this CPU."));
1779 stackop_size = '\0';
1782 static void
1783 set_16bit_gcc_code_flag (int new_code_flag)
1785 flag_code = new_code_flag;
1786 if (flag_code != CODE_16BIT)
1787 abort ();
1788 cpu_arch_flags.bitfield.cpu64 = 0;
1789 cpu_arch_flags.bitfield.cpuno64 = 1;
1790 stackop_size = LONG_MNEM_SUFFIX;
1793 static void
1794 set_intel_syntax (int syntax_flag)
1796 /* Find out if register prefixing is specified. */
1797 int ask_naked_reg = 0;
1799 SKIP_WHITESPACE ();
1800 if (!is_end_of_line[(unsigned char) *input_line_pointer])
1802 char *string = input_line_pointer;
1803 int e = get_symbol_end ();
1805 if (strcmp (string, "prefix") == 0)
1806 ask_naked_reg = 1;
1807 else if (strcmp (string, "noprefix") == 0)
1808 ask_naked_reg = -1;
1809 else
1810 as_bad (_("bad argument to syntax directive."));
1811 *input_line_pointer = e;
1813 demand_empty_rest_of_line ();
1815 intel_syntax = syntax_flag;
1817 if (ask_naked_reg == 0)
1818 allow_naked_reg = (intel_syntax
1819 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
1820 else
1821 allow_naked_reg = (ask_naked_reg < 0);
1823 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
1824 identifier_chars['$'] = intel_syntax ? '$' : 0;
1825 register_prefix = allow_naked_reg ? "" : "%";
1828 static void
1829 set_intel_mnemonic (int mnemonic_flag)
1831 intel_mnemonic = mnemonic_flag;
1834 static void
1835 set_allow_index_reg (int flag)
1837 allow_index_reg = flag;
1840 static void
1841 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
1843 SKIP_WHITESPACE ();
1845 if (!is_end_of_line[(unsigned char) *input_line_pointer])
1847 char *string = input_line_pointer;
1848 int e = get_symbol_end ();
1849 unsigned int i;
1850 i386_cpu_flags flags;
1852 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
1854 if (strcmp (string, cpu_arch[i].name) == 0)
1856 if (*string != '.')
1858 cpu_arch_name = cpu_arch[i].name;
1859 cpu_sub_arch_name = NULL;
1860 cpu_arch_flags = cpu_arch[i].flags;
1861 if (flag_code == CODE_64BIT)
1863 cpu_arch_flags.bitfield.cpu64 = 1;
1864 cpu_arch_flags.bitfield.cpuno64 = 0;
1866 else
1868 cpu_arch_flags.bitfield.cpu64 = 0;
1869 cpu_arch_flags.bitfield.cpuno64 = 1;
1871 cpu_arch_isa = cpu_arch[i].type;
1872 cpu_arch_isa_flags = cpu_arch[i].flags;
1873 if (!cpu_arch_tune_set)
1875 cpu_arch_tune = cpu_arch_isa;
1876 cpu_arch_tune_flags = cpu_arch_isa_flags;
1878 break;
1881 flags = cpu_flags_or (cpu_arch_flags,
1882 cpu_arch[i].flags);
1883 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
1885 if (cpu_sub_arch_name)
1887 char *name = cpu_sub_arch_name;
1888 cpu_sub_arch_name = concat (name,
1889 cpu_arch[i].name,
1890 (const char *) NULL);
1891 free (name);
1893 else
1894 cpu_sub_arch_name = xstrdup (cpu_arch[i].name);
1895 cpu_arch_flags = flags;
1897 *input_line_pointer = e;
1898 demand_empty_rest_of_line ();
1899 return;
1902 if (i >= ARRAY_SIZE (cpu_arch))
1903 as_bad (_("no such architecture: `%s'"), string);
1905 *input_line_pointer = e;
1907 else
1908 as_bad (_("missing cpu architecture"));
1910 no_cond_jump_promotion = 0;
1911 if (*input_line_pointer == ','
1912 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
1914 char *string = ++input_line_pointer;
1915 int e = get_symbol_end ();
1917 if (strcmp (string, "nojumps") == 0)
1918 no_cond_jump_promotion = 1;
1919 else if (strcmp (string, "jumps") == 0)
1921 else
1922 as_bad (_("no such architecture modifier: `%s'"), string);
1924 *input_line_pointer = e;
1927 demand_empty_rest_of_line ();
1930 unsigned long
1931 i386_mach ()
1933 if (!strcmp (default_arch, "x86_64"))
1934 return bfd_mach_x86_64;
1935 else if (!strcmp (default_arch, "i386"))
1936 return bfd_mach_i386_i386;
1937 else
1938 as_fatal (_("Unknown architecture"));
1941 void
1942 md_begin ()
1944 const char *hash_err;
1946 /* Initialize op_hash hash table. */
1947 op_hash = hash_new ();
1950 const template *optab;
1951 templates *core_optab;
1953 /* Setup for loop. */
1954 optab = i386_optab;
1955 core_optab = (templates *) xmalloc (sizeof (templates));
1956 core_optab->start = optab;
1958 while (1)
1960 ++optab;
1961 if (optab->name == NULL
1962 || strcmp (optab->name, (optab - 1)->name) != 0)
1964 /* different name --> ship out current template list;
1965 add to hash table; & begin anew. */
1966 core_optab->end = optab;
1967 hash_err = hash_insert (op_hash,
1968 (optab - 1)->name,
1969 (PTR) core_optab);
1970 if (hash_err)
1972 as_fatal (_("Internal Error: Can't hash %s: %s"),
1973 (optab - 1)->name,
1974 hash_err);
1976 if (optab->name == NULL)
1977 break;
1978 core_optab = (templates *) xmalloc (sizeof (templates));
1979 core_optab->start = optab;
1984 /* Initialize reg_hash hash table. */
1985 reg_hash = hash_new ();
1987 const reg_entry *regtab;
1988 unsigned int regtab_size = i386_regtab_size;
1990 for (regtab = i386_regtab; regtab_size--; regtab++)
1992 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
1993 if (hash_err)
1994 as_fatal (_("Internal Error: Can't hash %s: %s"),
1995 regtab->reg_name,
1996 hash_err);
2000 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
2002 int c;
2003 char *p;
2005 for (c = 0; c < 256; c++)
2007 if (ISDIGIT (c))
2009 digit_chars[c] = c;
2010 mnemonic_chars[c] = c;
2011 register_chars[c] = c;
2012 operand_chars[c] = c;
2014 else if (ISLOWER (c))
2016 mnemonic_chars[c] = c;
2017 register_chars[c] = c;
2018 operand_chars[c] = c;
2020 else if (ISUPPER (c))
2022 mnemonic_chars[c] = TOLOWER (c);
2023 register_chars[c] = mnemonic_chars[c];
2024 operand_chars[c] = c;
2027 if (ISALPHA (c) || ISDIGIT (c))
2028 identifier_chars[c] = c;
2029 else if (c >= 128)
2031 identifier_chars[c] = c;
2032 operand_chars[c] = c;
2036 #ifdef LEX_AT
2037 identifier_chars['@'] = '@';
2038 #endif
2039 #ifdef LEX_QM
2040 identifier_chars['?'] = '?';
2041 operand_chars['?'] = '?';
2042 #endif
2043 digit_chars['-'] = '-';
2044 mnemonic_chars['_'] = '_';
2045 mnemonic_chars['-'] = '-';
2046 mnemonic_chars['.'] = '.';
2047 identifier_chars['_'] = '_';
2048 identifier_chars['.'] = '.';
2050 for (p = operand_special_chars; *p != '\0'; p++)
2051 operand_chars[(unsigned char) *p] = *p;
2054 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2055 if (IS_ELF)
2057 record_alignment (text_section, 2);
2058 record_alignment (data_section, 2);
2059 record_alignment (bss_section, 2);
2061 #endif
2063 if (flag_code == CODE_64BIT)
2065 x86_dwarf2_return_column = 16;
2066 x86_cie_data_alignment = -8;
2068 else
2070 x86_dwarf2_return_column = 8;
2071 x86_cie_data_alignment = -4;
2075 void
2076 i386_print_statistics (FILE *file)
2078 hash_print_statistics (file, "i386 opcode", op_hash);
2079 hash_print_statistics (file, "i386 register", reg_hash);
2082 #ifdef DEBUG386
2084 /* Debugging routines for md_assemble. */
2085 static void pte (template *);
2086 static void pt (i386_operand_type);
2087 static void pe (expressionS *);
2088 static void ps (symbolS *);
2090 static void
2091 pi (char *line, i386_insn *x)
2093 unsigned int i;
2095 fprintf (stdout, "%s: template ", line);
2096 pte (&x->tm);
2097 fprintf (stdout, " address: base %s index %s scale %x\n",
2098 x->base_reg ? x->base_reg->reg_name : "none",
2099 x->index_reg ? x->index_reg->reg_name : "none",
2100 x->log2_scale_factor);
2101 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
2102 x->rm.mode, x->rm.reg, x->rm.regmem);
2103 fprintf (stdout, " sib: base %x index %x scale %x\n",
2104 x->sib.base, x->sib.index, x->sib.scale);
2105 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
2106 (x->rex & REX_W) != 0,
2107 (x->rex & REX_R) != 0,
2108 (x->rex & REX_X) != 0,
2109 (x->rex & REX_B) != 0);
2110 fprintf (stdout, " drex: reg %d rex 0x%x\n",
2111 x->drex.reg, x->drex.rex);
2112 for (i = 0; i < x->operands; i++)
2114 fprintf (stdout, " #%d: ", i + 1);
2115 pt (x->types[i]);
2116 fprintf (stdout, "\n");
2117 if (x->types[i].bitfield.reg8
2118 || x->types[i].bitfield.reg16
2119 || x->types[i].bitfield.reg32
2120 || x->types[i].bitfield.reg64
2121 || x->types[i].bitfield.regmmx
2122 || x->types[i].bitfield.regxmm
2123 || x->types[i].bitfield.regymm
2124 || x->types[i].bitfield.sreg2
2125 || x->types[i].bitfield.sreg3
2126 || x->types[i].bitfield.control
2127 || x->types[i].bitfield.debug
2128 || x->types[i].bitfield.test)
2129 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
2130 if (operand_type_check (x->types[i], imm))
2131 pe (x->op[i].imms);
2132 if (operand_type_check (x->types[i], disp))
2133 pe (x->op[i].disps);
2137 static void
2138 pte (template *t)
2140 unsigned int i;
2141 fprintf (stdout, " %d operands ", t->operands);
2142 fprintf (stdout, "opcode %x ", t->base_opcode);
2143 if (t->extension_opcode != None)
2144 fprintf (stdout, "ext %x ", t->extension_opcode);
2145 if (t->opcode_modifier.d)
2146 fprintf (stdout, "D");
2147 if (t->opcode_modifier.w)
2148 fprintf (stdout, "W");
2149 fprintf (stdout, "\n");
2150 for (i = 0; i < t->operands; i++)
2152 fprintf (stdout, " #%d type ", i + 1);
2153 pt (t->operand_types[i]);
2154 fprintf (stdout, "\n");
2158 static void
2159 pe (expressionS *e)
2161 fprintf (stdout, " operation %d\n", e->X_op);
2162 fprintf (stdout, " add_number %ld (%lx)\n",
2163 (long) e->X_add_number, (long) e->X_add_number);
2164 if (e->X_add_symbol)
2166 fprintf (stdout, " add_symbol ");
2167 ps (e->X_add_symbol);
2168 fprintf (stdout, "\n");
2170 if (e->X_op_symbol)
2172 fprintf (stdout, " op_symbol ");
2173 ps (e->X_op_symbol);
2174 fprintf (stdout, "\n");
2178 static void
2179 ps (symbolS *s)
2181 fprintf (stdout, "%s type %s%s",
2182 S_GET_NAME (s),
2183 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
2184 segment_name (S_GET_SEGMENT (s)));
2187 static struct type_name
2189 i386_operand_type mask;
2190 const char *name;
2192 const type_names[] =
2194 { OPERAND_TYPE_REG8, "r8" },
2195 { OPERAND_TYPE_REG16, "r16" },
2196 { OPERAND_TYPE_REG32, "r32" },
2197 { OPERAND_TYPE_REG64, "r64" },
2198 { OPERAND_TYPE_IMM8, "i8" },
2199 { OPERAND_TYPE_IMM8, "i8s" },
2200 { OPERAND_TYPE_IMM16, "i16" },
2201 { OPERAND_TYPE_IMM32, "i32" },
2202 { OPERAND_TYPE_IMM32S, "i32s" },
2203 { OPERAND_TYPE_IMM64, "i64" },
2204 { OPERAND_TYPE_IMM1, "i1" },
2205 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
2206 { OPERAND_TYPE_DISP8, "d8" },
2207 { OPERAND_TYPE_DISP16, "d16" },
2208 { OPERAND_TYPE_DISP32, "d32" },
2209 { OPERAND_TYPE_DISP32S, "d32s" },
2210 { OPERAND_TYPE_DISP64, "d64" },
2211 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
2212 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
2213 { OPERAND_TYPE_CONTROL, "control reg" },
2214 { OPERAND_TYPE_TEST, "test reg" },
2215 { OPERAND_TYPE_DEBUG, "debug reg" },
2216 { OPERAND_TYPE_FLOATREG, "FReg" },
2217 { OPERAND_TYPE_FLOATACC, "FAcc" },
2218 { OPERAND_TYPE_SREG2, "SReg2" },
2219 { OPERAND_TYPE_SREG3, "SReg3" },
2220 { OPERAND_TYPE_ACC, "Acc" },
2221 { OPERAND_TYPE_JUMPABSOLUTE, "Jump Absolute" },
2222 { OPERAND_TYPE_REGMMX, "rMMX" },
2223 { OPERAND_TYPE_REGXMM, "rXMM" },
2224 { OPERAND_TYPE_ESSEG, "es" },
2225 { OPERAND_TYPE_VEX_IMM4, "VEX i4" },
2228 static void
2229 pt (i386_operand_type t)
2231 unsigned int j;
2232 i386_operand_type a;
2234 for (j = 0; j < ARRAY_SIZE (type_names); j++)
2236 a = operand_type_and (t, type_names[j].mask);
2237 if (!UINTS_ALL_ZERO (a))
2238 fprintf (stdout, "%s, ", type_names[j].name);
2240 fflush (stdout);
2243 #endif /* DEBUG386 */
2245 static bfd_reloc_code_real_type
2246 reloc (unsigned int size,
2247 int pcrel,
2248 int sign,
2249 bfd_reloc_code_real_type other)
2251 if (other != NO_RELOC)
2253 reloc_howto_type *reloc;
2255 if (size == 8)
2256 switch (other)
2258 case BFD_RELOC_X86_64_GOT32:
2259 return BFD_RELOC_X86_64_GOT64;
2260 break;
2261 case BFD_RELOC_X86_64_PLTOFF64:
2262 return BFD_RELOC_X86_64_PLTOFF64;
2263 break;
2264 case BFD_RELOC_X86_64_GOTPC32:
2265 other = BFD_RELOC_X86_64_GOTPC64;
2266 break;
2267 case BFD_RELOC_X86_64_GOTPCREL:
2268 other = BFD_RELOC_X86_64_GOTPCREL64;
2269 break;
2270 case BFD_RELOC_X86_64_TPOFF32:
2271 other = BFD_RELOC_X86_64_TPOFF64;
2272 break;
2273 case BFD_RELOC_X86_64_DTPOFF32:
2274 other = BFD_RELOC_X86_64_DTPOFF64;
2275 break;
2276 default:
2277 break;
2280 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
2281 if (size == 4 && flag_code != CODE_64BIT)
2282 sign = -1;
2284 reloc = bfd_reloc_type_lookup (stdoutput, other);
2285 if (!reloc)
2286 as_bad (_("unknown relocation (%u)"), other);
2287 else if (size != bfd_get_reloc_size (reloc))
2288 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
2289 bfd_get_reloc_size (reloc),
2290 size);
2291 else if (pcrel && !reloc->pc_relative)
2292 as_bad (_("non-pc-relative relocation for pc-relative field"));
2293 else if ((reloc->complain_on_overflow == complain_overflow_signed
2294 && !sign)
2295 || (reloc->complain_on_overflow == complain_overflow_unsigned
2296 && sign > 0))
2297 as_bad (_("relocated field and relocation type differ in signedness"));
2298 else
2299 return other;
2300 return NO_RELOC;
2303 if (pcrel)
2305 if (!sign)
2306 as_bad (_("there are no unsigned pc-relative relocations"));
2307 switch (size)
2309 case 1: return BFD_RELOC_8_PCREL;
2310 case 2: return BFD_RELOC_16_PCREL;
2311 case 4: return BFD_RELOC_32_PCREL;
2312 case 8: return BFD_RELOC_64_PCREL;
2314 as_bad (_("cannot do %u byte pc-relative relocation"), size);
2316 else
2318 if (sign > 0)
2319 switch (size)
2321 case 4: return BFD_RELOC_X86_64_32S;
2323 else
2324 switch (size)
2326 case 1: return BFD_RELOC_8;
2327 case 2: return BFD_RELOC_16;
2328 case 4: return BFD_RELOC_32;
2329 case 8: return BFD_RELOC_64;
2331 as_bad (_("cannot do %s %u byte relocation"),
2332 sign > 0 ? "signed" : "unsigned", size);
2335 abort ();
2336 return BFD_RELOC_NONE;
2339 /* Here we decide which fixups can be adjusted to make them relative to
2340 the beginning of the section instead of the symbol. Basically we need
2341 to make sure that the dynamic relocations are done correctly, so in
2342 some cases we force the original symbol to be used. */
2345 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
2347 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2348 if (!IS_ELF)
2349 return 1;
2351 /* Don't adjust pc-relative references to merge sections in 64-bit
2352 mode. */
2353 if (use_rela_relocations
2354 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
2355 && fixP->fx_pcrel)
2356 return 0;
2358 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
2359 and changed later by validate_fix. */
2360 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
2361 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
2362 return 0;
2364 /* adjust_reloc_syms doesn't know about the GOT. */
2365 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
2366 || fixP->fx_r_type == BFD_RELOC_386_PLT32
2367 || fixP->fx_r_type == BFD_RELOC_386_GOT32
2368 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
2369 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
2370 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
2371 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
2372 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
2373 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
2374 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
2375 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
2376 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
2377 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
2378 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
2379 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
2380 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
2381 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
2382 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
2383 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
2384 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
2385 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
2386 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
2387 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
2388 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
2389 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
2390 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
2391 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2392 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2393 return 0;
2394 #endif
2395 return 1;
2398 static int
2399 intel_float_operand (const char *mnemonic)
2401 /* Note that the value returned is meaningful only for opcodes with (memory)
2402 operands, hence the code here is free to improperly handle opcodes that
2403 have no operands (for better performance and smaller code). */
2405 if (mnemonic[0] != 'f')
2406 return 0; /* non-math */
2408 switch (mnemonic[1])
2410 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
2411 the fs segment override prefix not currently handled because no
2412 call path can make opcodes without operands get here */
2413 case 'i':
2414 return 2 /* integer op */;
2415 case 'l':
2416 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
2417 return 3; /* fldcw/fldenv */
2418 break;
2419 case 'n':
2420 if (mnemonic[2] != 'o' /* fnop */)
2421 return 3; /* non-waiting control op */
2422 break;
2423 case 'r':
2424 if (mnemonic[2] == 's')
2425 return 3; /* frstor/frstpm */
2426 break;
2427 case 's':
2428 if (mnemonic[2] == 'a')
2429 return 3; /* fsave */
2430 if (mnemonic[2] == 't')
2432 switch (mnemonic[3])
2434 case 'c': /* fstcw */
2435 case 'd': /* fstdw */
2436 case 'e': /* fstenv */
2437 case 's': /* fsts[gw] */
2438 return 3;
2441 break;
2442 case 'x':
2443 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
2444 return 0; /* fxsave/fxrstor are not really math ops */
2445 break;
2448 return 1;
2451 /* Build the VEX prefix. */
2453 static void
2454 build_vex_prefix (void)
2456 unsigned int register_specifier;
2457 unsigned int implied_prefix;
2458 unsigned int vector_length;
2460 /* Check register specifier. */
2461 if (i.vex.register_specifier)
2463 register_specifier = i.vex.register_specifier->reg_num;
2464 if ((i.vex.register_specifier->reg_flags & RegRex))
2465 register_specifier += 8;
2466 register_specifier = ~register_specifier & 0xf;
2468 else
2469 register_specifier = 0xf;
2471 vector_length = i.tm.opcode_modifier.vex256 ? 1 : 0;
2473 switch ((i.tm.base_opcode >> 8) & 0xff)
2475 case 0:
2476 implied_prefix = 0;
2477 break;
2478 case DATA_PREFIX_OPCODE:
2479 implied_prefix = 1;
2480 break;
2481 case REPE_PREFIX_OPCODE:
2482 implied_prefix = 2;
2483 break;
2484 case REPNE_PREFIX_OPCODE:
2485 implied_prefix = 3;
2486 break;
2487 default:
2488 abort ();
2491 /* Use 2-byte VEX prefix if possible. */
2492 if (i.tm.opcode_modifier.vex0f
2493 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
2495 /* 2-byte VEX prefix. */
2496 unsigned int r;
2498 i.vex.length = 2;
2499 i.vex.bytes[0] = 0xc5;
2501 /* Check the REX.R bit. */
2502 r = (i.rex & REX_R) ? 0 : 1;
2503 i.vex.bytes[1] = (r << 7
2504 | register_specifier << 3
2505 | vector_length << 2
2506 | implied_prefix);
2508 else
2510 /* 3-byte VEX prefix. */
2511 unsigned int m, w;
2513 if (i.tm.opcode_modifier.vex0f)
2514 m = 0x1;
2515 else if (i.tm.opcode_modifier.vex0f38)
2516 m = 0x2;
2517 else if (i.tm.opcode_modifier.vex0f3a)
2518 m = 0x3;
2519 else
2520 abort ();
2522 i.vex.length = 3;
2523 i.vex.bytes[0] = 0xc4;
2525 /* The high 3 bits of the second VEX byte are 1's compliment
2526 of RXB bits from REX. */
2527 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
2529 /* Check the REX.W bit. */
2530 w = (i.rex & REX_W) ? 1 : 0;
2531 if (i.tm.opcode_modifier.vexw0 || i.tm.opcode_modifier.vexw1)
2533 if (w)
2534 abort ();
2536 if (i.tm.opcode_modifier.vexw1)
2537 w = 1;
2540 i.vex.bytes[2] = (w << 7
2541 | register_specifier << 3
2542 | vector_length << 2
2543 | implied_prefix);
2547 static void
2548 process_immext (void)
2550 expressionS *exp;
2552 if (i.tm.cpu_flags.bitfield.cpusse3 && i.operands > 0)
2554 /* SSE3 Instructions have the fixed operands with an opcode
2555 suffix which is coded in the same place as an 8-bit immediate
2556 field would be. Here we check those operands and remove them
2557 afterwards. */
2558 unsigned int x;
2560 for (x = 0; x < i.operands; x++)
2561 if (i.op[x].regs->reg_num != x)
2562 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
2563 register_prefix, i.op[x].regs->reg_name, x + 1,
2564 i.tm.name);
2566 i.operands = 0;
2569 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
2570 which is coded in the same place as an 8-bit immediate field
2571 would be. Here we fake an 8-bit immediate operand from the
2572 opcode suffix stored in tm.extension_opcode.
2574 SSE5 and AVX instructions also use this encoding, for some of
2575 3 argument instructions. */
2577 assert (i.imm_operands == 0
2578 && (i.operands <= 2
2579 || (i.tm.cpu_flags.bitfield.cpusse5
2580 && i.operands <= 3)
2581 || (i.tm.opcode_modifier.vex
2582 && i.operands <= 4)));
2584 exp = &im_expressions[i.imm_operands++];
2585 i.op[i.operands].imms = exp;
2586 i.types[i.operands] = imm8;
2587 i.operands++;
2588 exp->X_op = O_constant;
2589 exp->X_add_number = i.tm.extension_opcode;
2590 i.tm.extension_opcode = None;
2593 /* This is the guts of the machine-dependent assembler. LINE points to a
2594 machine dependent instruction. This function is supposed to emit
2595 the frags/bytes it assembles to. */
2597 void
2598 md_assemble (char *line)
2600 unsigned int j;
2601 char mnemonic[MAX_MNEM_SIZE];
2603 /* Initialize globals. */
2604 memset (&i, '\0', sizeof (i));
2605 for (j = 0; j < MAX_OPERANDS; j++)
2606 i.reloc[j] = NO_RELOC;
2607 memset (disp_expressions, '\0', sizeof (disp_expressions));
2608 memset (im_expressions, '\0', sizeof (im_expressions));
2609 save_stack_p = save_stack;
2611 /* First parse an instruction mnemonic & call i386_operand for the operands.
2612 We assume that the scrubber has arranged it so that line[0] is the valid
2613 start of a (possibly prefixed) mnemonic. */
2615 line = parse_insn (line, mnemonic);
2616 if (line == NULL)
2617 return;
2619 line = parse_operands (line, mnemonic);
2620 if (line == NULL)
2621 return;
2623 /* Now we've parsed the mnemonic into a set of templates, and have the
2624 operands at hand. */
2626 /* All intel opcodes have reversed operands except for "bound" and
2627 "enter". We also don't reverse intersegment "jmp" and "call"
2628 instructions with 2 immediate operands so that the immediate segment
2629 precedes the offset, as it does when in AT&T mode. */
2630 if (intel_syntax
2631 && i.operands > 1
2632 && (strcmp (mnemonic, "bound") != 0)
2633 && (strcmp (mnemonic, "invlpga") != 0)
2634 && !(operand_type_check (i.types[0], imm)
2635 && operand_type_check (i.types[1], imm)))
2636 swap_operands ();
2638 /* The order of the immediates should be reversed
2639 for 2 immediates extrq and insertq instructions */
2640 if (i.imm_operands == 2
2641 && (strcmp (mnemonic, "extrq") == 0
2642 || strcmp (mnemonic, "insertq") == 0))
2643 swap_2_operands (0, 1);
2645 if (i.imm_operands)
2646 optimize_imm ();
2648 /* Don't optimize displacement for movabs since it only takes 64bit
2649 displacement. */
2650 if (i.disp_operands
2651 && (flag_code != CODE_64BIT
2652 || strcmp (mnemonic, "movabs") != 0))
2653 optimize_disp ();
2655 /* Next, we find a template that matches the given insn,
2656 making sure the overlap of the given operands types is consistent
2657 with the template operand types. */
2659 if (!match_template ())
2660 return;
2662 /* Zap movzx and movsx suffix. The suffix has been set from
2663 "word ptr" or "byte ptr" on the source operand in Intel syntax
2664 or extracted from mnemonic in AT&T syntax. But we'll use
2665 the destination register to choose the suffix for encoding. */
2666 if ((i.tm.base_opcode & ~9) == 0x0fb6)
2668 /* In Intel syntax, there must be a suffix. In AT&T syntax, if
2669 there is no suffix, the default will be byte extension. */
2670 if (i.reg_operands != 2
2671 && !i.suffix
2672 && intel_syntax)
2673 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
2675 i.suffix = 0;
2678 if (i.tm.opcode_modifier.fwait)
2679 if (!add_prefix (FWAIT_OPCODE))
2680 return;
2682 /* Check string instruction segment overrides. */
2683 if (i.tm.opcode_modifier.isstring && i.mem_operands != 0)
2685 if (!check_string ())
2686 return;
2689 if (!process_suffix ())
2690 return;
2692 /* Make still unresolved immediate matches conform to size of immediate
2693 given in i.suffix. */
2694 if (!finalize_imm ())
2695 return;
2697 if (i.types[0].bitfield.imm1)
2698 i.imm_operands = 0; /* kludge for shift insns. */
2700 for (j = 0; j < 3; j++)
2701 if (i.types[j].bitfield.inoutportreg
2702 || i.types[j].bitfield.shiftcount
2703 || i.types[j].bitfield.acc
2704 || i.types[j].bitfield.floatacc)
2705 i.reg_operands--;
2707 /* ImmExt should be processed after SSE2AVX. */
2708 if (!i.tm.opcode_modifier.sse2avx
2709 && i.tm.opcode_modifier.immext)
2710 process_immext ();
2712 /* For insns with operands there are more diddles to do to the opcode. */
2713 if (i.operands)
2715 if (!process_operands ())
2716 return;
2718 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
2720 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
2721 as_warn (_("translating to `%sp'"), i.tm.name);
2724 if (i.tm.opcode_modifier.vex)
2725 build_vex_prefix ();
2727 /* Handle conversion of 'int $3' --> special int3 insn. */
2728 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
2730 i.tm.base_opcode = INT3_OPCODE;
2731 i.imm_operands = 0;
2734 if ((i.tm.opcode_modifier.jump
2735 || i.tm.opcode_modifier.jumpbyte
2736 || i.tm.opcode_modifier.jumpdword)
2737 && i.op[0].disps->X_op == O_constant)
2739 /* Convert "jmp constant" (and "call constant") to a jump (call) to
2740 the absolute address given by the constant. Since ix86 jumps and
2741 calls are pc relative, we need to generate a reloc. */
2742 i.op[0].disps->X_add_symbol = &abs_symbol;
2743 i.op[0].disps->X_op = O_symbol;
2746 if (i.tm.opcode_modifier.rex64)
2747 i.rex |= REX_W;
2749 /* For 8 bit registers we need an empty rex prefix. Also if the
2750 instruction already has a prefix, we need to convert old
2751 registers to new ones. */
2753 if ((i.types[0].bitfield.reg8
2754 && (i.op[0].regs->reg_flags & RegRex64) != 0)
2755 || (i.types[1].bitfield.reg8
2756 && (i.op[1].regs->reg_flags & RegRex64) != 0)
2757 || ((i.types[0].bitfield.reg8
2758 || i.types[1].bitfield.reg8)
2759 && i.rex != 0))
2761 int x;
2763 i.rex |= REX_OPCODE;
2764 for (x = 0; x < 2; x++)
2766 /* Look for 8 bit operand that uses old registers. */
2767 if (i.types[x].bitfield.reg8
2768 && (i.op[x].regs->reg_flags & RegRex64) == 0)
2770 /* In case it is "hi" register, give up. */
2771 if (i.op[x].regs->reg_num > 3)
2772 as_bad (_("can't encode register '%s%s' in an "
2773 "instruction requiring REX prefix."),
2774 register_prefix, i.op[x].regs->reg_name);
2776 /* Otherwise it is equivalent to the extended register.
2777 Since the encoding doesn't change this is merely
2778 cosmetic cleanup for debug output. */
2780 i.op[x].regs = i.op[x].regs + 8;
2785 /* If the instruction has the DREX attribute (aka SSE5), don't emit a
2786 REX prefix. */
2787 if (i.tm.opcode_modifier.drex || i.tm.opcode_modifier.drexc)
2789 i.drex.rex = i.rex;
2790 i.rex = 0;
2792 else if (i.rex != 0)
2793 add_prefix (REX_OPCODE | i.rex);
2795 /* We are ready to output the insn. */
2796 output_insn ();
2799 static char *
2800 parse_insn (char *line, char *mnemonic)
2802 char *l = line;
2803 char *token_start = l;
2804 char *mnem_p;
2805 int supported;
2806 const template *t;
2808 /* Non-zero if we found a prefix only acceptable with string insns. */
2809 const char *expecting_string_instruction = NULL;
2811 while (1)
2813 mnem_p = mnemonic;
2814 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
2816 mnem_p++;
2817 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
2819 as_bad (_("no such instruction: `%s'"), token_start);
2820 return NULL;
2822 l++;
2824 if (!is_space_char (*l)
2825 && *l != END_OF_INSN
2826 && (intel_syntax
2827 || (*l != PREFIX_SEPARATOR
2828 && *l != ',')))
2830 as_bad (_("invalid character %s in mnemonic"),
2831 output_invalid (*l));
2832 return NULL;
2834 if (token_start == l)
2836 if (!intel_syntax && *l == PREFIX_SEPARATOR)
2837 as_bad (_("expecting prefix; got nothing"));
2838 else
2839 as_bad (_("expecting mnemonic; got nothing"));
2840 return NULL;
2843 /* Look up instruction (or prefix) via hash table. */
2844 current_templates = hash_find (op_hash, mnemonic);
2846 if (*l != END_OF_INSN
2847 && (!is_space_char (*l) || l[1] != END_OF_INSN)
2848 && current_templates
2849 && current_templates->start->opcode_modifier.isprefix)
2851 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2853 as_bad ((flag_code != CODE_64BIT
2854 ? _("`%s' is only supported in 64-bit mode")
2855 : _("`%s' is not supported in 64-bit mode")),
2856 current_templates->start->name);
2857 return NULL;
2859 /* If we are in 16-bit mode, do not allow addr16 or data16.
2860 Similarly, in 32-bit mode, do not allow addr32 or data32. */
2861 if ((current_templates->start->opcode_modifier.size16
2862 || current_templates->start->opcode_modifier.size32)
2863 && flag_code != CODE_64BIT
2864 && (current_templates->start->opcode_modifier.size32
2865 ^ (flag_code == CODE_16BIT)))
2867 as_bad (_("redundant %s prefix"),
2868 current_templates->start->name);
2869 return NULL;
2871 /* Add prefix, checking for repeated prefixes. */
2872 switch (add_prefix (current_templates->start->base_opcode))
2874 case 0:
2875 return NULL;
2876 case 2:
2877 expecting_string_instruction = current_templates->start->name;
2878 break;
2880 /* Skip past PREFIX_SEPARATOR and reset token_start. */
2881 token_start = ++l;
2883 else
2884 break;
2887 if (!current_templates)
2889 /* See if we can get a match by trimming off a suffix. */
2890 switch (mnem_p[-1])
2892 case WORD_MNEM_SUFFIX:
2893 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
2894 i.suffix = SHORT_MNEM_SUFFIX;
2895 else
2896 case BYTE_MNEM_SUFFIX:
2897 case QWORD_MNEM_SUFFIX:
2898 i.suffix = mnem_p[-1];
2899 mnem_p[-1] = '\0';
2900 current_templates = hash_find (op_hash, mnemonic);
2901 break;
2902 case SHORT_MNEM_SUFFIX:
2903 case LONG_MNEM_SUFFIX:
2904 if (!intel_syntax)
2906 i.suffix = mnem_p[-1];
2907 mnem_p[-1] = '\0';
2908 current_templates = hash_find (op_hash, mnemonic);
2910 break;
2912 /* Intel Syntax. */
2913 case 'd':
2914 if (intel_syntax)
2916 if (intel_float_operand (mnemonic) == 1)
2917 i.suffix = SHORT_MNEM_SUFFIX;
2918 else
2919 i.suffix = LONG_MNEM_SUFFIX;
2920 mnem_p[-1] = '\0';
2921 current_templates = hash_find (op_hash, mnemonic);
2923 break;
2925 if (!current_templates)
2927 as_bad (_("no such instruction: `%s'"), token_start);
2928 return NULL;
2932 if (current_templates->start->opcode_modifier.jump
2933 || current_templates->start->opcode_modifier.jumpbyte)
2935 /* Check for a branch hint. We allow ",pt" and ",pn" for
2936 predict taken and predict not taken respectively.
2937 I'm not sure that branch hints actually do anything on loop
2938 and jcxz insns (JumpByte) for current Pentium4 chips. They
2939 may work in the future and it doesn't hurt to accept them
2940 now. */
2941 if (l[0] == ',' && l[1] == 'p')
2943 if (l[2] == 't')
2945 if (!add_prefix (DS_PREFIX_OPCODE))
2946 return NULL;
2947 l += 3;
2949 else if (l[2] == 'n')
2951 if (!add_prefix (CS_PREFIX_OPCODE))
2952 return NULL;
2953 l += 3;
2957 /* Any other comma loses. */
2958 if (*l == ',')
2960 as_bad (_("invalid character %s in mnemonic"),
2961 output_invalid (*l));
2962 return NULL;
2965 /* Check if instruction is supported on specified architecture. */
2966 supported = 0;
2967 for (t = current_templates->start; t < current_templates->end; ++t)
2969 supported |= cpu_flags_match (t);
2970 if (supported == CPU_FLAGS_PERFECT_MATCH)
2971 goto skip;
2974 if (!(supported & CPU_FLAGS_64BIT_MATCH))
2976 as_bad (flag_code == CODE_64BIT
2977 ? _("`%s' is not supported in 64-bit mode")
2978 : _("`%s' is only supported in 64-bit mode"),
2979 current_templates->start->name);
2980 return NULL;
2982 if (supported != CPU_FLAGS_PERFECT_MATCH)
2984 as_bad (_("`%s' is not supported on `%s%s'"),
2985 current_templates->start->name, cpu_arch_name,
2986 cpu_sub_arch_name ? cpu_sub_arch_name : "");
2987 return NULL;
2990 skip:
2991 if (!cpu_arch_flags.bitfield.cpui386
2992 && (flag_code != CODE_16BIT))
2994 as_warn (_("use .code16 to ensure correct addressing mode"));
2997 /* Check for rep/repne without a string instruction. */
2998 if (expecting_string_instruction)
3000 static templates override;
3002 for (t = current_templates->start; t < current_templates->end; ++t)
3003 if (t->opcode_modifier.isstring)
3004 break;
3005 if (t >= current_templates->end)
3007 as_bad (_("expecting string instruction after `%s'"),
3008 expecting_string_instruction);
3009 return NULL;
3011 for (override.start = t; t < current_templates->end; ++t)
3012 if (!t->opcode_modifier.isstring)
3013 break;
3014 override.end = t;
3015 current_templates = &override;
3018 return l;
3021 static char *
3022 parse_operands (char *l, const char *mnemonic)
3024 char *token_start;
3026 /* 1 if operand is pending after ','. */
3027 unsigned int expecting_operand = 0;
3029 /* Non-zero if operand parens not balanced. */
3030 unsigned int paren_not_balanced;
3032 while (*l != END_OF_INSN)
3034 /* Skip optional white space before operand. */
3035 if (is_space_char (*l))
3036 ++l;
3037 if (!is_operand_char (*l) && *l != END_OF_INSN)
3039 as_bad (_("invalid character %s before operand %d"),
3040 output_invalid (*l),
3041 i.operands + 1);
3042 return NULL;
3044 token_start = l; /* after white space */
3045 paren_not_balanced = 0;
3046 while (paren_not_balanced || *l != ',')
3048 if (*l == END_OF_INSN)
3050 if (paren_not_balanced)
3052 if (!intel_syntax)
3053 as_bad (_("unbalanced parenthesis in operand %d."),
3054 i.operands + 1);
3055 else
3056 as_bad (_("unbalanced brackets in operand %d."),
3057 i.operands + 1);
3058 return NULL;
3060 else
3061 break; /* we are done */
3063 else if (!is_operand_char (*l) && !is_space_char (*l))
3065 as_bad (_("invalid character %s in operand %d"),
3066 output_invalid (*l),
3067 i.operands + 1);
3068 return NULL;
3070 if (!intel_syntax)
3072 if (*l == '(')
3073 ++paren_not_balanced;
3074 if (*l == ')')
3075 --paren_not_balanced;
3077 else
3079 if (*l == '[')
3080 ++paren_not_balanced;
3081 if (*l == ']')
3082 --paren_not_balanced;
3084 l++;
3086 if (l != token_start)
3087 { /* Yes, we've read in another operand. */
3088 unsigned int operand_ok;
3089 this_operand = i.operands++;
3090 i.types[this_operand].bitfield.unspecified = 1;
3091 if (i.operands > MAX_OPERANDS)
3093 as_bad (_("spurious operands; (%d operands/instruction max)"),
3094 MAX_OPERANDS);
3095 return NULL;
3097 /* Now parse operand adding info to 'i' as we go along. */
3098 END_STRING_AND_SAVE (l);
3100 if (intel_syntax)
3101 operand_ok =
3102 i386_intel_operand (token_start,
3103 intel_float_operand (mnemonic));
3104 else
3105 operand_ok = i386_att_operand (token_start);
3107 RESTORE_END_STRING (l);
3108 if (!operand_ok)
3109 return NULL;
3111 else
3113 if (expecting_operand)
3115 expecting_operand_after_comma:
3116 as_bad (_("expecting operand after ','; got nothing"));
3117 return NULL;
3119 if (*l == ',')
3121 as_bad (_("expecting operand before ','; got nothing"));
3122 return NULL;
3126 /* Now *l must be either ',' or END_OF_INSN. */
3127 if (*l == ',')
3129 if (*++l == END_OF_INSN)
3131 /* Just skip it, if it's \n complain. */
3132 goto expecting_operand_after_comma;
3134 expecting_operand = 1;
3137 return l;
3140 static void
3141 swap_2_operands (int xchg1, int xchg2)
3143 union i386_op temp_op;
3144 i386_operand_type temp_type;
3145 enum bfd_reloc_code_real temp_reloc;
3147 temp_type = i.types[xchg2];
3148 i.types[xchg2] = i.types[xchg1];
3149 i.types[xchg1] = temp_type;
3150 temp_op = i.op[xchg2];
3151 i.op[xchg2] = i.op[xchg1];
3152 i.op[xchg1] = temp_op;
3153 temp_reloc = i.reloc[xchg2];
3154 i.reloc[xchg2] = i.reloc[xchg1];
3155 i.reloc[xchg1] = temp_reloc;
3158 static void
3159 swap_operands (void)
3161 switch (i.operands)
3163 case 5:
3164 case 4:
3165 swap_2_operands (1, i.operands - 2);
3166 case 3:
3167 case 2:
3168 swap_2_operands (0, i.operands - 1);
3169 break;
3170 default:
3171 abort ();
3174 if (i.mem_operands == 2)
3176 const seg_entry *temp_seg;
3177 temp_seg = i.seg[0];
3178 i.seg[0] = i.seg[1];
3179 i.seg[1] = temp_seg;
3183 /* Try to ensure constant immediates are represented in the smallest
3184 opcode possible. */
3185 static void
3186 optimize_imm (void)
3188 char guess_suffix = 0;
3189 int op;
3191 if (i.suffix)
3192 guess_suffix = i.suffix;
3193 else if (i.reg_operands)
3195 /* Figure out a suffix from the last register operand specified.
3196 We can't do this properly yet, ie. excluding InOutPortReg,
3197 but the following works for instructions with immediates.
3198 In any case, we can't set i.suffix yet. */
3199 for (op = i.operands; --op >= 0;)
3200 if (i.types[op].bitfield.reg8)
3202 guess_suffix = BYTE_MNEM_SUFFIX;
3203 break;
3205 else if (i.types[op].bitfield.reg16)
3207 guess_suffix = WORD_MNEM_SUFFIX;
3208 break;
3210 else if (i.types[op].bitfield.reg32)
3212 guess_suffix = LONG_MNEM_SUFFIX;
3213 break;
3215 else if (i.types[op].bitfield.reg64)
3217 guess_suffix = QWORD_MNEM_SUFFIX;
3218 break;
3221 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
3222 guess_suffix = WORD_MNEM_SUFFIX;
3224 for (op = i.operands; --op >= 0;)
3225 if (operand_type_check (i.types[op], imm))
3227 switch (i.op[op].imms->X_op)
3229 case O_constant:
3230 /* If a suffix is given, this operand may be shortened. */
3231 switch (guess_suffix)
3233 case LONG_MNEM_SUFFIX:
3234 i.types[op].bitfield.imm32 = 1;
3235 i.types[op].bitfield.imm64 = 1;
3236 break;
3237 case WORD_MNEM_SUFFIX:
3238 i.types[op].bitfield.imm16 = 1;
3239 i.types[op].bitfield.imm32 = 1;
3240 i.types[op].bitfield.imm32s = 1;
3241 i.types[op].bitfield.imm64 = 1;
3242 break;
3243 case BYTE_MNEM_SUFFIX:
3244 i.types[op].bitfield.imm8 = 1;
3245 i.types[op].bitfield.imm8s = 1;
3246 i.types[op].bitfield.imm16 = 1;
3247 i.types[op].bitfield.imm32 = 1;
3248 i.types[op].bitfield.imm32s = 1;
3249 i.types[op].bitfield.imm64 = 1;
3250 break;
3253 /* If this operand is at most 16 bits, convert it
3254 to a signed 16 bit number before trying to see
3255 whether it will fit in an even smaller size.
3256 This allows a 16-bit operand such as $0xffe0 to
3257 be recognised as within Imm8S range. */
3258 if ((i.types[op].bitfield.imm16)
3259 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
3261 i.op[op].imms->X_add_number =
3262 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
3264 if ((i.types[op].bitfield.imm32)
3265 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
3266 == 0))
3268 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
3269 ^ ((offsetT) 1 << 31))
3270 - ((offsetT) 1 << 31));
3272 i.types[op]
3273 = operand_type_or (i.types[op],
3274 smallest_imm_type (i.op[op].imms->X_add_number));
3276 /* We must avoid matching of Imm32 templates when 64bit
3277 only immediate is available. */
3278 if (guess_suffix == QWORD_MNEM_SUFFIX)
3279 i.types[op].bitfield.imm32 = 0;
3280 break;
3282 case O_absent:
3283 case O_register:
3284 abort ();
3286 /* Symbols and expressions. */
3287 default:
3288 /* Convert symbolic operand to proper sizes for matching, but don't
3289 prevent matching a set of insns that only supports sizes other
3290 than those matching the insn suffix. */
3292 i386_operand_type mask, allowed;
3293 const template *t;
3295 operand_type_set (&mask, 0);
3296 operand_type_set (&allowed, 0);
3298 for (t = current_templates->start;
3299 t < current_templates->end;
3300 ++t)
3301 allowed = operand_type_or (allowed,
3302 t->operand_types[op]);
3303 switch (guess_suffix)
3305 case QWORD_MNEM_SUFFIX:
3306 mask.bitfield.imm64 = 1;
3307 mask.bitfield.imm32s = 1;
3308 break;
3309 case LONG_MNEM_SUFFIX:
3310 mask.bitfield.imm32 = 1;
3311 break;
3312 case WORD_MNEM_SUFFIX:
3313 mask.bitfield.imm16 = 1;
3314 break;
3315 case BYTE_MNEM_SUFFIX:
3316 mask.bitfield.imm8 = 1;
3317 break;
3318 default:
3319 break;
3321 allowed = operand_type_and (mask, allowed);
3322 if (!operand_type_all_zero (&allowed))
3323 i.types[op] = operand_type_and (i.types[op], mask);
3325 break;
3330 /* Try to use the smallest displacement type too. */
3331 static void
3332 optimize_disp (void)
3334 int op;
3336 for (op = i.operands; --op >= 0;)
3337 if (operand_type_check (i.types[op], disp))
3339 if (i.op[op].disps->X_op == O_constant)
3341 offsetT disp = i.op[op].disps->X_add_number;
3343 if (i.types[op].bitfield.disp16
3344 && (disp & ~(offsetT) 0xffff) == 0)
3346 /* If this operand is at most 16 bits, convert
3347 to a signed 16 bit number and don't use 64bit
3348 displacement. */
3349 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
3350 i.types[op].bitfield.disp64 = 0;
3352 if (i.types[op].bitfield.disp32
3353 && (disp & ~(((offsetT) 2 << 31) - 1)) == 0)
3355 /* If this operand is at most 32 bits, convert
3356 to a signed 32 bit number and don't use 64bit
3357 displacement. */
3358 disp &= (((offsetT) 2 << 31) - 1);
3359 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
3360 i.types[op].bitfield.disp64 = 0;
3362 if (!disp && i.types[op].bitfield.baseindex)
3364 i.types[op].bitfield.disp8 = 0;
3365 i.types[op].bitfield.disp16 = 0;
3366 i.types[op].bitfield.disp32 = 0;
3367 i.types[op].bitfield.disp32s = 0;
3368 i.types[op].bitfield.disp64 = 0;
3369 i.op[op].disps = 0;
3370 i.disp_operands--;
3372 else if (flag_code == CODE_64BIT)
3374 if (fits_in_signed_long (disp))
3376 i.types[op].bitfield.disp64 = 0;
3377 i.types[op].bitfield.disp32s = 1;
3379 if (fits_in_unsigned_long (disp))
3380 i.types[op].bitfield.disp32 = 1;
3382 if ((i.types[op].bitfield.disp32
3383 || i.types[op].bitfield.disp32s
3384 || i.types[op].bitfield.disp16)
3385 && fits_in_signed_byte (disp))
3386 i.types[op].bitfield.disp8 = 1;
3388 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
3389 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
3391 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
3392 i.op[op].disps, 0, i.reloc[op]);
3393 i.types[op].bitfield.disp8 = 0;
3394 i.types[op].bitfield.disp16 = 0;
3395 i.types[op].bitfield.disp32 = 0;
3396 i.types[op].bitfield.disp32s = 0;
3397 i.types[op].bitfield.disp64 = 0;
3399 else
3400 /* We only support 64bit displacement on constants. */
3401 i.types[op].bitfield.disp64 = 0;
3405 /* Check if operands are valid for the instrucrtion. Update VEX
3406 operand types. */
3408 static int
3409 VEX_check_operands (const template *t)
3411 if (!t->opcode_modifier.vex)
3412 return 0;
3414 /* Only check VEX_Imm4, which must be the first operand. */
3415 if (t->operand_types[0].bitfield.vex_imm4)
3417 if (i.op[0].imms->X_op != O_constant
3418 || !fits_in_imm4 (i.op[0].imms->X_add_number))
3419 return 1;
3421 /* Turn off Imm8 so that update_imm won't complain. */
3422 i.types[0] = vex_imm4;
3425 return 0;
3428 static int
3429 match_template (void)
3431 /* Points to template once we've found it. */
3432 const template *t;
3433 i386_operand_type overlap0, overlap1, overlap2, overlap3;
3434 i386_operand_type overlap4;
3435 unsigned int found_reverse_match;
3436 i386_opcode_modifier suffix_check;
3437 i386_operand_type operand_types [MAX_OPERANDS];
3438 int addr_prefix_disp;
3439 unsigned int j;
3440 unsigned int found_cpu_match;
3441 unsigned int check_register;
3443 #if MAX_OPERANDS != 5
3444 # error "MAX_OPERANDS must be 5."
3445 #endif
3447 found_reverse_match = 0;
3448 addr_prefix_disp = -1;
3450 memset (&suffix_check, 0, sizeof (suffix_check));
3451 if (i.suffix == BYTE_MNEM_SUFFIX)
3452 suffix_check.no_bsuf = 1;
3453 else if (i.suffix == WORD_MNEM_SUFFIX)
3454 suffix_check.no_wsuf = 1;
3455 else if (i.suffix == SHORT_MNEM_SUFFIX)
3456 suffix_check.no_ssuf = 1;
3457 else if (i.suffix == LONG_MNEM_SUFFIX)
3458 suffix_check.no_lsuf = 1;
3459 else if (i.suffix == QWORD_MNEM_SUFFIX)
3460 suffix_check.no_qsuf = 1;
3461 else if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
3462 suffix_check.no_ldsuf = 1;
3464 for (t = current_templates->start; t < current_templates->end; t++)
3466 addr_prefix_disp = -1;
3468 /* Must have right number of operands. */
3469 if (i.operands != t->operands)
3470 continue;
3472 /* Check processor support. */
3473 found_cpu_match = (cpu_flags_match (t)
3474 == CPU_FLAGS_PERFECT_MATCH);
3475 if (!found_cpu_match)
3476 continue;
3478 /* Check old gcc support. */
3479 if (!old_gcc && t->opcode_modifier.oldgcc)
3480 continue;
3482 /* Check AT&T mnemonic. */
3483 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
3484 continue;
3486 /* Check AT&T syntax Intel syntax. */
3487 if ((intel_syntax && t->opcode_modifier.attsyntax)
3488 || (!intel_syntax && t->opcode_modifier.intelsyntax))
3489 continue;
3491 /* Check the suffix, except for some instructions in intel mode. */
3492 if ((!intel_syntax || !t->opcode_modifier.ignoresize)
3493 && ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
3494 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
3495 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
3496 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
3497 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
3498 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf)))
3499 continue;
3501 if (!operand_size_match (t))
3502 continue;
3504 for (j = 0; j < MAX_OPERANDS; j++)
3505 operand_types[j] = t->operand_types[j];
3507 /* In general, don't allow 64-bit operands in 32-bit mode. */
3508 if (i.suffix == QWORD_MNEM_SUFFIX
3509 && flag_code != CODE_64BIT
3510 && (intel_syntax
3511 ? (!t->opcode_modifier.ignoresize
3512 && !intel_float_operand (t->name))
3513 : intel_float_operand (t->name) != 2)
3514 && ((!operand_types[0].bitfield.regmmx
3515 && !operand_types[0].bitfield.regxmm
3516 && !operand_types[0].bitfield.regymm)
3517 || (!operand_types[t->operands > 1].bitfield.regmmx
3518 && !!operand_types[t->operands > 1].bitfield.regxmm
3519 && !!operand_types[t->operands > 1].bitfield.regymm))
3520 && (t->base_opcode != 0x0fc7
3521 || t->extension_opcode != 1 /* cmpxchg8b */))
3522 continue;
3524 /* In general, don't allow 32-bit operands on pre-386. */
3525 else if (i.suffix == LONG_MNEM_SUFFIX
3526 && !cpu_arch_flags.bitfield.cpui386
3527 && (intel_syntax
3528 ? (!t->opcode_modifier.ignoresize
3529 && !intel_float_operand (t->name))
3530 : intel_float_operand (t->name) != 2)
3531 && ((!operand_types[0].bitfield.regmmx
3532 && !operand_types[0].bitfield.regxmm)
3533 || (!operand_types[t->operands > 1].bitfield.regmmx
3534 && !!operand_types[t->operands > 1].bitfield.regxmm)))
3535 continue;
3537 /* Do not verify operands when there are none. */
3538 else
3540 if (!t->operands)
3541 /* We've found a match; break out of loop. */
3542 break;
3545 /* Address size prefix will turn Disp64/Disp32/Disp16 operand
3546 into Disp32/Disp16/Disp32 operand. */
3547 if (i.prefix[ADDR_PREFIX] != 0)
3549 /* There should be only one Disp operand. */
3550 switch (flag_code)
3552 case CODE_16BIT:
3553 for (j = 0; j < MAX_OPERANDS; j++)
3555 if (operand_types[j].bitfield.disp16)
3557 addr_prefix_disp = j;
3558 operand_types[j].bitfield.disp32 = 1;
3559 operand_types[j].bitfield.disp16 = 0;
3560 break;
3563 break;
3564 case CODE_32BIT:
3565 for (j = 0; j < MAX_OPERANDS; j++)
3567 if (operand_types[j].bitfield.disp32)
3569 addr_prefix_disp = j;
3570 operand_types[j].bitfield.disp32 = 0;
3571 operand_types[j].bitfield.disp16 = 1;
3572 break;
3575 break;
3576 case CODE_64BIT:
3577 for (j = 0; j < MAX_OPERANDS; j++)
3579 if (operand_types[j].bitfield.disp64)
3581 addr_prefix_disp = j;
3582 operand_types[j].bitfield.disp64 = 0;
3583 operand_types[j].bitfield.disp32 = 1;
3584 break;
3587 break;
3591 /* We check register size only if size of operands can be
3592 encoded the canonical way. */
3593 check_register = t->opcode_modifier.w;
3594 overlap0 = operand_type_and (i.types[0], operand_types[0]);
3595 switch (t->operands)
3597 case 1:
3598 if (!operand_type_match (overlap0, i.types[0]))
3599 continue;
3600 break;
3601 case 2:
3602 /* xchg %eax, %eax is a special case. It is an aliase for nop
3603 only in 32bit mode and we can use opcode 0x90. In 64bit
3604 mode, we can't use 0x90 for xchg %eax, %eax since it should
3605 zero-extend %eax to %rax. */
3606 if (flag_code == CODE_64BIT
3607 && t->base_opcode == 0x90
3608 && operand_type_equal (&i.types [0], &acc32)
3609 && operand_type_equal (&i.types [1], &acc32))
3610 continue;
3611 case 3:
3612 case 4:
3613 case 5:
3614 overlap1 = operand_type_and (i.types[1], operand_types[1]);
3615 if (!operand_type_match (overlap0, i.types[0])
3616 || !operand_type_match (overlap1, i.types[1])
3617 || (check_register
3618 && !operand_type_register_match (overlap0, i.types[0],
3619 operand_types[0],
3620 overlap1, i.types[1],
3621 operand_types[1])))
3623 /* Check if other direction is valid ... */
3624 if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
3625 continue;
3627 /* Try reversing direction of operands. */
3628 overlap0 = operand_type_and (i.types[0], operand_types[1]);
3629 overlap1 = operand_type_and (i.types[1], operand_types[0]);
3630 if (!operand_type_match (overlap0, i.types[0])
3631 || !operand_type_match (overlap1, i.types[1])
3632 || (check_register
3633 && !operand_type_register_match (overlap0,
3634 i.types[0],
3635 operand_types[1],
3636 overlap1,
3637 i.types[1],
3638 operand_types[0])))
3640 /* Does not match either direction. */
3641 continue;
3643 /* found_reverse_match holds which of D or FloatDR
3644 we've found. */
3645 if (t->opcode_modifier.d)
3646 found_reverse_match = Opcode_D;
3647 else if (t->opcode_modifier.floatd)
3648 found_reverse_match = Opcode_FloatD;
3649 else
3650 found_reverse_match = 0;
3651 if (t->opcode_modifier.floatr)
3652 found_reverse_match |= Opcode_FloatR;
3654 else
3656 /* Found a forward 2 operand match here. */
3657 switch (t->operands)
3659 case 5:
3660 overlap4 = operand_type_and (i.types[4],
3661 operand_types[4]);
3662 case 4:
3663 overlap3 = operand_type_and (i.types[3],
3664 operand_types[3]);
3665 case 3:
3666 overlap2 = operand_type_and (i.types[2],
3667 operand_types[2]);
3668 break;
3671 switch (t->operands)
3673 case 5:
3674 if (!operand_type_match (overlap4, i.types[4])
3675 || !operand_type_register_match (overlap3,
3676 i.types[3],
3677 operand_types[3],
3678 overlap4,
3679 i.types[4],
3680 operand_types[4]))
3681 continue;
3682 case 4:
3683 if (!operand_type_match (overlap3, i.types[3])
3684 || (check_register
3685 && !operand_type_register_match (overlap2,
3686 i.types[2],
3687 operand_types[2],
3688 overlap3,
3689 i.types[3],
3690 operand_types[3])))
3691 continue;
3692 case 3:
3693 /* Here we make use of the fact that there are no
3694 reverse match 3 operand instructions, and all 3
3695 operand instructions only need to be checked for
3696 register consistency between operands 2 and 3. */
3697 if (!operand_type_match (overlap2, i.types[2])
3698 || (check_register
3699 && !operand_type_register_match (overlap1,
3700 i.types[1],
3701 operand_types[1],
3702 overlap2,
3703 i.types[2],
3704 operand_types[2])))
3705 continue;
3706 break;
3709 /* Found either forward/reverse 2, 3 or 4 operand match here:
3710 slip through to break. */
3712 if (!found_cpu_match)
3714 found_reverse_match = 0;
3715 continue;
3718 /* Check if VEX operands are valid. */
3719 if (VEX_check_operands (t))
3720 continue;
3722 /* We've found a match; break out of loop. */
3723 break;
3726 if (t == current_templates->end)
3728 /* We found no match. */
3729 as_bad (_("suffix or operands invalid for `%s'"),
3730 current_templates->start->name);
3731 return 0;
3734 if (!quiet_warnings)
3736 if (!intel_syntax
3737 && (i.types[0].bitfield.jumpabsolute
3738 != operand_types[0].bitfield.jumpabsolute))
3740 as_warn (_("indirect %s without `*'"), t->name);
3743 if (t->opcode_modifier.isprefix
3744 && t->opcode_modifier.ignoresize)
3746 /* Warn them that a data or address size prefix doesn't
3747 affect assembly of the next line of code. */
3748 as_warn (_("stand-alone `%s' prefix"), t->name);
3752 /* Copy the template we found. */
3753 i.tm = *t;
3755 if (addr_prefix_disp != -1)
3756 i.tm.operand_types[addr_prefix_disp]
3757 = operand_types[addr_prefix_disp];
3759 if (found_reverse_match)
3761 /* If we found a reverse match we must alter the opcode
3762 direction bit. found_reverse_match holds bits to change
3763 (different for int & float insns). */
3765 i.tm.base_opcode ^= found_reverse_match;
3767 i.tm.operand_types[0] = operand_types[1];
3768 i.tm.operand_types[1] = operand_types[0];
3771 return 1;
3774 static int
3775 check_string (void)
3777 int mem_op = operand_type_check (i.types[0], anymem) ? 0 : 1;
3778 if (i.tm.operand_types[mem_op].bitfield.esseg)
3780 if (i.seg[0] != NULL && i.seg[0] != &es)
3782 as_bad (_("`%s' operand %d must use `%%es' segment"),
3783 i.tm.name,
3784 mem_op + 1);
3785 return 0;
3787 /* There's only ever one segment override allowed per instruction.
3788 This instruction possibly has a legal segment override on the
3789 second operand, so copy the segment to where non-string
3790 instructions store it, allowing common code. */
3791 i.seg[0] = i.seg[1];
3793 else if (i.tm.operand_types[mem_op + 1].bitfield.esseg)
3795 if (i.seg[1] != NULL && i.seg[1] != &es)
3797 as_bad (_("`%s' operand %d must use `%%es' segment"),
3798 i.tm.name,
3799 mem_op + 2);
3800 return 0;
3803 return 1;
3806 static int
3807 process_suffix (void)
3809 /* If matched instruction specifies an explicit instruction mnemonic
3810 suffix, use it. */
3811 if (i.tm.opcode_modifier.size16)
3812 i.suffix = WORD_MNEM_SUFFIX;
3813 else if (i.tm.opcode_modifier.size32)
3814 i.suffix = LONG_MNEM_SUFFIX;
3815 else if (i.tm.opcode_modifier.size64)
3816 i.suffix = QWORD_MNEM_SUFFIX;
3817 else if (i.reg_operands)
3819 /* If there's no instruction mnemonic suffix we try to invent one
3820 based on register operands. */
3821 if (!i.suffix)
3823 /* We take i.suffix from the last register operand specified,
3824 Destination register type is more significant than source
3825 register type. crc32 in SSE4.2 prefers source register
3826 type. */
3827 if (i.tm.base_opcode == 0xf20f38f1)
3829 if (i.types[0].bitfield.reg16)
3830 i.suffix = WORD_MNEM_SUFFIX;
3831 else if (i.types[0].bitfield.reg32)
3832 i.suffix = LONG_MNEM_SUFFIX;
3833 else if (i.types[0].bitfield.reg64)
3834 i.suffix = QWORD_MNEM_SUFFIX;
3836 else if (i.tm.base_opcode == 0xf20f38f0)
3838 if (i.types[0].bitfield.reg8)
3839 i.suffix = BYTE_MNEM_SUFFIX;
3842 if (!i.suffix)
3844 int op;
3846 if (i.tm.base_opcode == 0xf20f38f1
3847 || i.tm.base_opcode == 0xf20f38f0)
3849 /* We have to know the operand size for crc32. */
3850 as_bad (_("ambiguous memory operand size for `%s`"),
3851 i.tm.name);
3852 return 0;
3855 for (op = i.operands; --op >= 0;)
3856 if (!i.tm.operand_types[op].bitfield.inoutportreg)
3858 if (i.types[op].bitfield.reg8)
3860 i.suffix = BYTE_MNEM_SUFFIX;
3861 break;
3863 else if (i.types[op].bitfield.reg16)
3865 i.suffix = WORD_MNEM_SUFFIX;
3866 break;
3868 else if (i.types[op].bitfield.reg32)
3870 i.suffix = LONG_MNEM_SUFFIX;
3871 break;
3873 else if (i.types[op].bitfield.reg64)
3875 i.suffix = QWORD_MNEM_SUFFIX;
3876 break;
3881 else if (i.suffix == BYTE_MNEM_SUFFIX)
3883 if (!check_byte_reg ())
3884 return 0;
3886 else if (i.suffix == LONG_MNEM_SUFFIX)
3888 if (!check_long_reg ())
3889 return 0;
3891 else if (i.suffix == QWORD_MNEM_SUFFIX)
3893 if (intel_syntax
3894 && i.tm.opcode_modifier.ignoresize
3895 && i.tm.opcode_modifier.no_qsuf)
3896 i.suffix = 0;
3897 else if (!check_qword_reg ())
3898 return 0;
3900 else if (i.suffix == WORD_MNEM_SUFFIX)
3902 if (!check_word_reg ())
3903 return 0;
3905 else if (i.suffix == XMMWORD_MNEM_SUFFIX
3906 || i.suffix == YMMWORD_MNEM_SUFFIX)
3908 /* Skip if the instruction has x/y suffix. match_template
3909 should check if it is a valid suffix. */
3911 else if (intel_syntax && i.tm.opcode_modifier.ignoresize)
3912 /* Do nothing if the instruction is going to ignore the prefix. */
3914 else
3915 abort ();
3917 else if (i.tm.opcode_modifier.defaultsize
3918 && !i.suffix
3919 /* exclude fldenv/frstor/fsave/fstenv */
3920 && i.tm.opcode_modifier.no_ssuf)
3922 i.suffix = stackop_size;
3924 else if (intel_syntax
3925 && !i.suffix
3926 && (i.tm.operand_types[0].bitfield.jumpabsolute
3927 || i.tm.opcode_modifier.jumpbyte
3928 || i.tm.opcode_modifier.jumpintersegment
3929 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
3930 && i.tm.extension_opcode <= 3)))
3932 switch (flag_code)
3934 case CODE_64BIT:
3935 if (!i.tm.opcode_modifier.no_qsuf)
3937 i.suffix = QWORD_MNEM_SUFFIX;
3938 break;
3940 case CODE_32BIT:
3941 if (!i.tm.opcode_modifier.no_lsuf)
3942 i.suffix = LONG_MNEM_SUFFIX;
3943 break;
3944 case CODE_16BIT:
3945 if (!i.tm.opcode_modifier.no_wsuf)
3946 i.suffix = WORD_MNEM_SUFFIX;
3947 break;
3951 if (!i.suffix)
3953 if (!intel_syntax)
3955 if (i.tm.opcode_modifier.w)
3957 as_bad (_("no instruction mnemonic suffix given and "
3958 "no register operands; can't size instruction"));
3959 return 0;
3962 else
3964 unsigned int suffixes;
3966 suffixes = !i.tm.opcode_modifier.no_bsuf;
3967 if (!i.tm.opcode_modifier.no_wsuf)
3968 suffixes |= 1 << 1;
3969 if (!i.tm.opcode_modifier.no_lsuf)
3970 suffixes |= 1 << 2;
3971 if (!i.tm.opcode_modifier.no_ldsuf)
3972 suffixes |= 1 << 3;
3973 if (!i.tm.opcode_modifier.no_ssuf)
3974 suffixes |= 1 << 4;
3975 if (!i.tm.opcode_modifier.no_qsuf)
3976 suffixes |= 1 << 5;
3978 /* There are more than suffix matches. */
3979 if (i.tm.opcode_modifier.w
3980 || ((suffixes & (suffixes - 1))
3981 && !i.tm.opcode_modifier.defaultsize
3982 && !i.tm.opcode_modifier.ignoresize))
3984 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
3985 return 0;
3990 /* Change the opcode based on the operand size given by i.suffix;
3991 We don't need to change things for byte insns. */
3993 if (i.suffix
3994 && i.suffix != BYTE_MNEM_SUFFIX
3995 && i.suffix != XMMWORD_MNEM_SUFFIX
3996 && i.suffix != YMMWORD_MNEM_SUFFIX)
3998 /* It's not a byte, select word/dword operation. */
3999 if (i.tm.opcode_modifier.w)
4001 if (i.tm.opcode_modifier.shortform)
4002 i.tm.base_opcode |= 8;
4003 else
4004 i.tm.base_opcode |= 1;
4007 /* Now select between word & dword operations via the operand
4008 size prefix, except for instructions that will ignore this
4009 prefix anyway. */
4010 if (i.tm.opcode_modifier.addrprefixop0)
4012 /* The address size override prefix changes the size of the
4013 first operand. */
4014 if ((flag_code == CODE_32BIT
4015 && i.op->regs[0].reg_type.bitfield.reg16)
4016 || (flag_code != CODE_32BIT
4017 && i.op->regs[0].reg_type.bitfield.reg32))
4018 if (!add_prefix (ADDR_PREFIX_OPCODE))
4019 return 0;
4021 else if (i.suffix != QWORD_MNEM_SUFFIX
4022 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
4023 && !i.tm.opcode_modifier.ignoresize
4024 && !i.tm.opcode_modifier.floatmf
4025 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
4026 || (flag_code == CODE_64BIT
4027 && i.tm.opcode_modifier.jumpbyte)))
4029 unsigned int prefix = DATA_PREFIX_OPCODE;
4031 if (i.tm.opcode_modifier.jumpbyte) /* jcxz, loop */
4032 prefix = ADDR_PREFIX_OPCODE;
4034 if (!add_prefix (prefix))
4035 return 0;
4038 /* Set mode64 for an operand. */
4039 if (i.suffix == QWORD_MNEM_SUFFIX
4040 && flag_code == CODE_64BIT
4041 && !i.tm.opcode_modifier.norex64)
4043 /* Special case for xchg %rax,%rax. It is NOP and doesn't
4044 need rex64. cmpxchg8b is also a special case. */
4045 if (! (i.operands == 2
4046 && i.tm.base_opcode == 0x90
4047 && i.tm.extension_opcode == None
4048 && operand_type_equal (&i.types [0], &acc64)
4049 && operand_type_equal (&i.types [1], &acc64))
4050 && ! (i.operands == 1
4051 && i.tm.base_opcode == 0xfc7
4052 && i.tm.extension_opcode == 1
4053 && !operand_type_check (i.types [0], reg)
4054 && operand_type_check (i.types [0], anymem)))
4055 i.rex |= REX_W;
4058 /* Size floating point instruction. */
4059 if (i.suffix == LONG_MNEM_SUFFIX)
4060 if (i.tm.opcode_modifier.floatmf)
4061 i.tm.base_opcode ^= 4;
4064 return 1;
4067 static int
4068 check_byte_reg (void)
4070 int op;
4072 for (op = i.operands; --op >= 0;)
4074 /* If this is an eight bit register, it's OK. If it's the 16 or
4075 32 bit version of an eight bit register, we will just use the
4076 low portion, and that's OK too. */
4077 if (i.types[op].bitfield.reg8)
4078 continue;
4080 /* Don't generate this warning if not needed. */
4081 if (intel_syntax && i.tm.opcode_modifier.byteokintel)
4082 continue;
4084 /* crc32 doesn't generate this warning. */
4085 if (i.tm.base_opcode == 0xf20f38f0)
4086 continue;
4088 if ((i.types[op].bitfield.reg16
4089 || i.types[op].bitfield.reg32
4090 || i.types[op].bitfield.reg64)
4091 && i.op[op].regs->reg_num < 4)
4093 /* Prohibit these changes in the 64bit mode, since the
4094 lowering is more complicated. */
4095 if (flag_code == CODE_64BIT
4096 && !i.tm.operand_types[op].bitfield.inoutportreg)
4098 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4099 register_prefix, i.op[op].regs->reg_name,
4100 i.suffix);
4101 return 0;
4103 #if REGISTER_WARNINGS
4104 if (!quiet_warnings
4105 && !i.tm.operand_types[op].bitfield.inoutportreg)
4106 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4107 register_prefix,
4108 (i.op[op].regs + (i.types[op].bitfield.reg16
4109 ? REGNAM_AL - REGNAM_AX
4110 : REGNAM_AL - REGNAM_EAX))->reg_name,
4111 register_prefix,
4112 i.op[op].regs->reg_name,
4113 i.suffix);
4114 #endif
4115 continue;
4117 /* Any other register is bad. */
4118 if (i.types[op].bitfield.reg16
4119 || i.types[op].bitfield.reg32
4120 || i.types[op].bitfield.reg64
4121 || i.types[op].bitfield.regmmx
4122 || i.types[op].bitfield.regxmm
4123 || i.types[op].bitfield.regymm
4124 || i.types[op].bitfield.sreg2
4125 || i.types[op].bitfield.sreg3
4126 || i.types[op].bitfield.control
4127 || i.types[op].bitfield.debug
4128 || i.types[op].bitfield.test
4129 || i.types[op].bitfield.floatreg
4130 || i.types[op].bitfield.floatacc)
4132 as_bad (_("`%s%s' not allowed with `%s%c'"),
4133 register_prefix,
4134 i.op[op].regs->reg_name,
4135 i.tm.name,
4136 i.suffix);
4137 return 0;
4140 return 1;
4143 static int
4144 check_long_reg (void)
4146 int op;
4148 for (op = i.operands; --op >= 0;)
4149 /* Reject eight bit registers, except where the template requires
4150 them. (eg. movzb) */
4151 if (i.types[op].bitfield.reg8
4152 && (i.tm.operand_types[op].bitfield.reg16
4153 || i.tm.operand_types[op].bitfield.reg32
4154 || i.tm.operand_types[op].bitfield.acc))
4156 as_bad (_("`%s%s' not allowed with `%s%c'"),
4157 register_prefix,
4158 i.op[op].regs->reg_name,
4159 i.tm.name,
4160 i.suffix);
4161 return 0;
4163 /* Warn if the e prefix on a general reg is missing. */
4164 else if ((!quiet_warnings || flag_code == CODE_64BIT)
4165 && i.types[op].bitfield.reg16
4166 && (i.tm.operand_types[op].bitfield.reg32
4167 || i.tm.operand_types[op].bitfield.acc))
4169 /* Prohibit these changes in the 64bit mode, since the
4170 lowering is more complicated. */
4171 if (flag_code == CODE_64BIT)
4173 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4174 register_prefix, i.op[op].regs->reg_name,
4175 i.suffix);
4176 return 0;
4178 #if REGISTER_WARNINGS
4179 else
4180 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4181 register_prefix,
4182 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
4183 register_prefix,
4184 i.op[op].regs->reg_name,
4185 i.suffix);
4186 #endif
4188 /* Warn if the r prefix on a general reg is missing. */
4189 else if (i.types[op].bitfield.reg64
4190 && (i.tm.operand_types[op].bitfield.reg32
4191 || i.tm.operand_types[op].bitfield.acc))
4193 if (intel_syntax
4194 && i.tm.opcode_modifier.toqword
4195 && !i.types[0].bitfield.regxmm)
4197 /* Convert to QWORD. We want REX byte. */
4198 i.suffix = QWORD_MNEM_SUFFIX;
4200 else
4202 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4203 register_prefix, i.op[op].regs->reg_name,
4204 i.suffix);
4205 return 0;
4208 return 1;
4211 static int
4212 check_qword_reg (void)
4214 int op;
4216 for (op = i.operands; --op >= 0; )
4217 /* Reject eight bit registers, except where the template requires
4218 them. (eg. movzb) */
4219 if (i.types[op].bitfield.reg8
4220 && (i.tm.operand_types[op].bitfield.reg16
4221 || i.tm.operand_types[op].bitfield.reg32
4222 || i.tm.operand_types[op].bitfield.acc))
4224 as_bad (_("`%s%s' not allowed with `%s%c'"),
4225 register_prefix,
4226 i.op[op].regs->reg_name,
4227 i.tm.name,
4228 i.suffix);
4229 return 0;
4231 /* Warn if the e prefix on a general reg is missing. */
4232 else if ((i.types[op].bitfield.reg16
4233 || i.types[op].bitfield.reg32)
4234 && (i.tm.operand_types[op].bitfield.reg32
4235 || i.tm.operand_types[op].bitfield.acc))
4237 /* Prohibit these changes in the 64bit mode, since the
4238 lowering is more complicated. */
4239 if (intel_syntax
4240 && i.tm.opcode_modifier.todword
4241 && !i.types[0].bitfield.regxmm)
4243 /* Convert to DWORD. We don't want REX byte. */
4244 i.suffix = LONG_MNEM_SUFFIX;
4246 else
4248 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4249 register_prefix, i.op[op].regs->reg_name,
4250 i.suffix);
4251 return 0;
4254 return 1;
4257 static int
4258 check_word_reg (void)
4260 int op;
4261 for (op = i.operands; --op >= 0;)
4262 /* Reject eight bit registers, except where the template requires
4263 them. (eg. movzb) */
4264 if (i.types[op].bitfield.reg8
4265 && (i.tm.operand_types[op].bitfield.reg16
4266 || i.tm.operand_types[op].bitfield.reg32
4267 || i.tm.operand_types[op].bitfield.acc))
4269 as_bad (_("`%s%s' not allowed with `%s%c'"),
4270 register_prefix,
4271 i.op[op].regs->reg_name,
4272 i.tm.name,
4273 i.suffix);
4274 return 0;
4276 /* Warn if the e prefix on a general reg is present. */
4277 else if ((!quiet_warnings || flag_code == CODE_64BIT)
4278 && i.types[op].bitfield.reg32
4279 && (i.tm.operand_types[op].bitfield.reg16
4280 || i.tm.operand_types[op].bitfield.acc))
4282 /* Prohibit these changes in the 64bit mode, since the
4283 lowering is more complicated. */
4284 if (flag_code == CODE_64BIT)
4286 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4287 register_prefix, i.op[op].regs->reg_name,
4288 i.suffix);
4289 return 0;
4291 else
4292 #if REGISTER_WARNINGS
4293 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4294 register_prefix,
4295 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
4296 register_prefix,
4297 i.op[op].regs->reg_name,
4298 i.suffix);
4299 #endif
4301 return 1;
4304 static int
4305 update_imm (unsigned int j)
4307 i386_operand_type overlap;
4309 overlap = operand_type_and (i.types[j], i.tm.operand_types[j]);
4310 if ((overlap.bitfield.imm8
4311 || overlap.bitfield.imm8s
4312 || overlap.bitfield.imm16
4313 || overlap.bitfield.imm32
4314 || overlap.bitfield.imm32s
4315 || overlap.bitfield.imm64)
4316 && !operand_type_equal (&overlap, &imm8)
4317 && !operand_type_equal (&overlap, &imm8s)
4318 && !operand_type_equal (&overlap, &imm16)
4319 && !operand_type_equal (&overlap, &imm32)
4320 && !operand_type_equal (&overlap, &imm32s)
4321 && !operand_type_equal (&overlap, &imm64))
4323 if (i.suffix)
4325 i386_operand_type temp;
4327 operand_type_set (&temp, 0);
4328 if (i.suffix == BYTE_MNEM_SUFFIX)
4330 temp.bitfield.imm8 = overlap.bitfield.imm8;
4331 temp.bitfield.imm8s = overlap.bitfield.imm8s;
4333 else if (i.suffix == WORD_MNEM_SUFFIX)
4334 temp.bitfield.imm16 = overlap.bitfield.imm16;
4335 else if (i.suffix == QWORD_MNEM_SUFFIX)
4337 temp.bitfield.imm64 = overlap.bitfield.imm64;
4338 temp.bitfield.imm32s = overlap.bitfield.imm32s;
4340 else
4341 temp.bitfield.imm32 = overlap.bitfield.imm32;
4342 overlap = temp;
4344 else if (operand_type_equal (&overlap, &imm16_32_32s)
4345 || operand_type_equal (&overlap, &imm16_32)
4346 || operand_type_equal (&overlap, &imm16_32s))
4348 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
4349 overlap = imm16;
4350 else
4351 overlap = imm32s;
4353 if (!operand_type_equal (&overlap, &imm8)
4354 && !operand_type_equal (&overlap, &imm8s)
4355 && !operand_type_equal (&overlap, &imm16)
4356 && !operand_type_equal (&overlap, &imm32)
4357 && !operand_type_equal (&overlap, &imm32s)
4358 && !operand_type_equal (&overlap, &imm64))
4360 as_bad (_("no instruction mnemonic suffix given; "
4361 "can't determine immediate size"));
4362 return 0;
4365 i.types[j] = overlap;
4367 return 1;
4370 static int
4371 finalize_imm (void)
4373 unsigned int j;
4375 for (j = 0; j < 2; j++)
4376 if (update_imm (j) == 0)
4377 return 0;
4379 i.types[2] = operand_type_and (i.types[2], i.tm.operand_types[2]);
4380 assert (operand_type_check (i.types[2], imm) == 0);
4382 return 1;
4385 static void
4386 process_drex (void)
4388 i.drex.modrm_reg = 0;
4389 i.drex.modrm_regmem = 0;
4391 /* SSE5 4 operand instructions must have the destination the same as
4392 one of the inputs. Figure out the destination register and cache
4393 it away in the drex field, and remember which fields to use for
4394 the modrm byte. */
4395 if (i.tm.opcode_modifier.drex
4396 && i.tm.opcode_modifier.drexv
4397 && i.operands == 4)
4399 i.tm.extension_opcode = None;
4401 /* Case 1: 4 operand insn, dest = src1, src3 = register. */
4402 if (i.types[0].bitfield.regxmm != 0
4403 && i.types[1].bitfield.regxmm != 0
4404 && i.types[2].bitfield.regxmm != 0
4405 && i.types[3].bitfield.regxmm != 0
4406 && i.op[0].regs->reg_num == i.op[3].regs->reg_num
4407 && i.op[0].regs->reg_flags == i.op[3].regs->reg_flags)
4409 /* Clear the arguments that are stored in drex. */
4410 operand_type_set (&i.types[0], 0);
4411 operand_type_set (&i.types[3], 0);
4412 i.reg_operands -= 2;
4414 /* There are two different ways to encode a 4 operand
4415 instruction with all registers that uses OC1 set to
4416 0 or 1. Favor setting OC1 to 0 since this mimics the
4417 actions of other SSE5 assemblers. Use modrm encoding 2
4418 for register/register. Include the high order bit that
4419 is normally stored in the REX byte in the register
4420 field. */
4421 i.tm.extension_opcode = DREX_X1_XMEM_X2_X1;
4422 i.drex.modrm_reg = 2;
4423 i.drex.modrm_regmem = 1;
4424 i.drex.reg = (i.op[3].regs->reg_num
4425 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4428 /* Case 2: 4 operand insn, dest = src1, src3 = memory. */
4429 else if (i.types[0].bitfield.regxmm != 0
4430 && i.types[1].bitfield.regxmm != 0
4431 && (i.types[2].bitfield.regxmm
4432 || operand_type_check (i.types[2], anymem))
4433 && i.types[3].bitfield.regxmm != 0
4434 && i.op[0].regs->reg_num == i.op[3].regs->reg_num
4435 && i.op[0].regs->reg_flags == i.op[3].regs->reg_flags)
4437 /* clear the arguments that are stored in drex */
4438 operand_type_set (&i.types[0], 0);
4439 operand_type_set (&i.types[3], 0);
4440 i.reg_operands -= 2;
4442 /* Specify the modrm encoding for memory addressing. Include
4443 the high order bit that is normally stored in the REX byte
4444 in the register field. */
4445 i.tm.extension_opcode = DREX_X1_X2_XMEM_X1;
4446 i.drex.modrm_reg = 1;
4447 i.drex.modrm_regmem = 2;
4448 i.drex.reg = (i.op[3].regs->reg_num
4449 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4452 /* Case 3: 4 operand insn, dest = src1, src2 = memory. */
4453 else if (i.types[0].bitfield.regxmm != 0
4454 && operand_type_check (i.types[1], anymem) != 0
4455 && i.types[2].bitfield.regxmm != 0
4456 && i.types[3].bitfield.regxmm != 0
4457 && i.op[0].regs->reg_num == i.op[3].regs->reg_num
4458 && i.op[0].regs->reg_flags == i.op[3].regs->reg_flags)
4460 /* Clear the arguments that are stored in drex. */
4461 operand_type_set (&i.types[0], 0);
4462 operand_type_set (&i.types[3], 0);
4463 i.reg_operands -= 2;
4465 /* Specify the modrm encoding for memory addressing. Include
4466 the high order bit that is normally stored in the REX byte
4467 in the register field. */
4468 i.tm.extension_opcode = DREX_X1_XMEM_X2_X1;
4469 i.drex.modrm_reg = 2;
4470 i.drex.modrm_regmem = 1;
4471 i.drex.reg = (i.op[3].regs->reg_num
4472 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4475 /* Case 4: 4 operand insn, dest = src3, src2 = register. */
4476 else if (i.types[0].bitfield.regxmm != 0
4477 && i.types[1].bitfield.regxmm != 0
4478 && i.types[2].bitfield.regxmm != 0
4479 && i.types[3].bitfield.regxmm != 0
4480 && i.op[2].regs->reg_num == i.op[3].regs->reg_num
4481 && i.op[2].regs->reg_flags == i.op[3].regs->reg_flags)
4483 /* clear the arguments that are stored in drex */
4484 operand_type_set (&i.types[2], 0);
4485 operand_type_set (&i.types[3], 0);
4486 i.reg_operands -= 2;
4488 /* There are two different ways to encode a 4 operand
4489 instruction with all registers that uses OC1 set to
4490 0 or 1. Favor setting OC1 to 0 since this mimics the
4491 actions of other SSE5 assemblers. Use modrm encoding
4492 2 for register/register. Include the high order bit that
4493 is normally stored in the REX byte in the register
4494 field. */
4495 i.tm.extension_opcode = DREX_XMEM_X1_X2_X2;
4496 i.drex.modrm_reg = 1;
4497 i.drex.modrm_regmem = 0;
4499 /* Remember the register, including the upper bits */
4500 i.drex.reg = (i.op[3].regs->reg_num
4501 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4504 /* Case 5: 4 operand insn, dest = src3, src2 = memory. */
4505 else if (i.types[0].bitfield.regxmm != 0
4506 && (i.types[1].bitfield.regxmm
4507 || operand_type_check (i.types[1], anymem))
4508 && i.types[2].bitfield.regxmm != 0
4509 && i.types[3].bitfield.regxmm != 0
4510 && i.op[2].regs->reg_num == i.op[3].regs->reg_num
4511 && i.op[2].regs->reg_flags == i.op[3].regs->reg_flags)
4513 /* Clear the arguments that are stored in drex. */
4514 operand_type_set (&i.types[2], 0);
4515 operand_type_set (&i.types[3], 0);
4516 i.reg_operands -= 2;
4518 /* Specify the modrm encoding and remember the register
4519 including the bits normally stored in the REX byte. */
4520 i.tm.extension_opcode = DREX_X1_XMEM_X2_X2;
4521 i.drex.modrm_reg = 0;
4522 i.drex.modrm_regmem = 1;
4523 i.drex.reg = (i.op[3].regs->reg_num
4524 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4527 /* Case 6: 4 operand insn, dest = src3, src1 = memory. */
4528 else if (operand_type_check (i.types[0], anymem) != 0
4529 && i.types[1].bitfield.regxmm != 0
4530 && i.types[2].bitfield.regxmm != 0
4531 && i.types[3].bitfield.regxmm != 0
4532 && i.op[2].regs->reg_num == i.op[3].regs->reg_num
4533 && i.op[2].regs->reg_flags == i.op[3].regs->reg_flags)
4535 /* clear the arguments that are stored in drex */
4536 operand_type_set (&i.types[2], 0);
4537 operand_type_set (&i.types[3], 0);
4538 i.reg_operands -= 2;
4540 /* Specify the modrm encoding and remember the register
4541 including the bits normally stored in the REX byte. */
4542 i.tm.extension_opcode = DREX_XMEM_X1_X2_X2;
4543 i.drex.modrm_reg = 1;
4544 i.drex.modrm_regmem = 0;
4545 i.drex.reg = (i.op[3].regs->reg_num
4546 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4549 else
4550 as_bad (_("Incorrect operands for the '%s' instruction"),
4551 i.tm.name);
4554 /* SSE5 instructions with the DREX byte where the only memory operand
4555 is in the 2nd argument, and the first and last xmm register must
4556 match, and is encoded in the DREX byte. */
4557 else if (i.tm.opcode_modifier.drex
4558 && !i.tm.opcode_modifier.drexv
4559 && i.operands == 4)
4561 /* Case 1: 4 operand insn, dest = src1, src3 = reg/mem. */
4562 if (i.types[0].bitfield.regxmm != 0
4563 && (i.types[1].bitfield.regxmm
4564 || operand_type_check(i.types[1], anymem))
4565 && i.types[2].bitfield.regxmm != 0
4566 && i.types[3].bitfield.regxmm != 0
4567 && i.op[0].regs->reg_num == i.op[3].regs->reg_num
4568 && i.op[0].regs->reg_flags == i.op[3].regs->reg_flags)
4570 /* clear the arguments that are stored in drex */
4571 operand_type_set (&i.types[0], 0);
4572 operand_type_set (&i.types[3], 0);
4573 i.reg_operands -= 2;
4575 /* Specify the modrm encoding and remember the register
4576 including the high bit normally stored in the REX
4577 byte. */
4578 i.drex.modrm_reg = 2;
4579 i.drex.modrm_regmem = 1;
4580 i.drex.reg = (i.op[3].regs->reg_num
4581 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4584 else
4585 as_bad (_("Incorrect operands for the '%s' instruction"),
4586 i.tm.name);
4589 /* SSE5 3 operand instructions that the result is a register, being
4590 either operand can be a memory operand, using OC0 to note which
4591 one is the memory. */
4592 else if (i.tm.opcode_modifier.drex
4593 && i.tm.opcode_modifier.drexv
4594 && i.operands == 3)
4596 i.tm.extension_opcode = None;
4598 /* Case 1: 3 operand insn, src1 = register. */
4599 if (i.types[0].bitfield.regxmm != 0
4600 && i.types[1].bitfield.regxmm != 0
4601 && i.types[2].bitfield.regxmm != 0)
4603 /* Clear the arguments that are stored in drex. */
4604 operand_type_set (&i.types[2], 0);
4605 i.reg_operands--;
4607 /* Specify the modrm encoding and remember the register
4608 including the high bit normally stored in the REX byte. */
4609 i.tm.extension_opcode = DREX_XMEM_X1_X2;
4610 i.drex.modrm_reg = 1;
4611 i.drex.modrm_regmem = 0;
4612 i.drex.reg = (i.op[2].regs->reg_num
4613 + ((i.op[2].regs->reg_flags & RegRex) ? 8 : 0));
4616 /* Case 2: 3 operand insn, src1 = memory. */
4617 else if (operand_type_check (i.types[0], anymem) != 0
4618 && i.types[1].bitfield.regxmm != 0
4619 && i.types[2].bitfield.regxmm != 0)
4621 /* Clear the arguments that are stored in drex. */
4622 operand_type_set (&i.types[2], 0);
4623 i.reg_operands--;
4625 /* Specify the modrm encoding and remember the register
4626 including the high bit normally stored in the REX
4627 byte. */
4628 i.tm.extension_opcode = DREX_XMEM_X1_X2;
4629 i.drex.modrm_reg = 1;
4630 i.drex.modrm_regmem = 0;
4631 i.drex.reg = (i.op[2].regs->reg_num
4632 + ((i.op[2].regs->reg_flags & RegRex) ? 8 : 0));
4635 /* Case 3: 3 operand insn, src2 = memory. */
4636 else if (i.types[0].bitfield.regxmm != 0
4637 && operand_type_check (i.types[1], anymem) != 0
4638 && i.types[2].bitfield.regxmm != 0)
4640 /* Clear the arguments that are stored in drex. */
4641 operand_type_set (&i.types[2], 0);
4642 i.reg_operands--;
4644 /* Specify the modrm encoding and remember the register
4645 including the high bit normally stored in the REX byte. */
4646 i.tm.extension_opcode = DREX_X1_XMEM_X2;
4647 i.drex.modrm_reg = 0;
4648 i.drex.modrm_regmem = 1;
4649 i.drex.reg = (i.op[2].regs->reg_num
4650 + ((i.op[2].regs->reg_flags & RegRex) ? 8 : 0));
4653 else
4654 as_bad (_("Incorrect operands for the '%s' instruction"),
4655 i.tm.name);
4658 /* SSE5 4 operand instructions that are the comparison instructions
4659 where the first operand is the immediate value of the comparison
4660 to be done. */
4661 else if (i.tm.opcode_modifier.drexc != 0 && i.operands == 4)
4663 /* Case 1: 4 operand insn, src1 = reg/memory. */
4664 if (operand_type_check (i.types[0], imm) != 0
4665 && (i.types[1].bitfield.regxmm
4666 || operand_type_check (i.types[1], anymem))
4667 && i.types[2].bitfield.regxmm != 0
4668 && i.types[3].bitfield.regxmm != 0)
4670 /* clear the arguments that are stored in drex */
4671 operand_type_set (&i.types[3], 0);
4672 i.reg_operands--;
4674 /* Specify the modrm encoding and remember the register
4675 including the high bit normally stored in the REX byte. */
4676 i.drex.modrm_reg = 2;
4677 i.drex.modrm_regmem = 1;
4678 i.drex.reg = (i.op[3].regs->reg_num
4679 + ((i.op[3].regs->reg_flags & RegRex) ? 8 : 0));
4682 /* Case 2: 3 operand insn with ImmExt that places the
4683 opcode_extension as an immediate argument. This is used for
4684 all of the varients of comparison that supplies the appropriate
4685 value as part of the instruction. */
4686 else if ((i.types[0].bitfield.regxmm
4687 || operand_type_check (i.types[0], anymem))
4688 && i.types[1].bitfield.regxmm != 0
4689 && i.types[2].bitfield.regxmm != 0
4690 && operand_type_check (i.types[3], imm) != 0)
4692 /* clear the arguments that are stored in drex */
4693 operand_type_set (&i.types[2], 0);
4694 i.reg_operands--;
4696 /* Specify the modrm encoding and remember the register
4697 including the high bit normally stored in the REX byte. */
4698 i.drex.modrm_reg = 1;
4699 i.drex.modrm_regmem = 0;
4700 i.drex.reg = (i.op[2].regs->reg_num
4701 + ((i.op[2].regs->reg_flags & RegRex) ? 8 : 0));
4704 else
4705 as_bad (_("Incorrect operands for the '%s' instruction"),
4706 i.tm.name);
4709 else if (i.tm.opcode_modifier.drex
4710 || i.tm.opcode_modifier.drexv
4711 || i.tm.opcode_modifier.drexc)
4712 as_bad (_("Internal error for the '%s' instruction"), i.tm.name);
4715 static int
4716 bad_implicit_operand (int xmm)
4718 const char *reg = xmm ? "xmm0" : "ymm0";
4719 if (intel_syntax)
4720 as_bad (_("the last operand of `%s' must be `%s%s'"),
4721 i.tm.name, register_prefix, reg);
4722 else
4723 as_bad (_("the first operand of `%s' must be `%s%s'"),
4724 i.tm.name, register_prefix, reg);
4725 return 0;
4728 static int
4729 process_operands (void)
4731 /* Default segment register this instruction will use for memory
4732 accesses. 0 means unknown. This is only for optimizing out
4733 unnecessary segment overrides. */
4734 const seg_entry *default_seg = 0;
4736 /* Handle all of the DREX munging that SSE5 needs. */
4737 if (i.tm.opcode_modifier.drex
4738 || i.tm.opcode_modifier.drexv
4739 || i.tm.opcode_modifier.drexc)
4740 process_drex ();
4742 if (i.tm.opcode_modifier.sse2avx
4743 && (i.tm.opcode_modifier.vexnds
4744 || i.tm.opcode_modifier.vexndd))
4746 unsigned int dup = i.operands;
4747 unsigned int dest = dup - 1;
4748 unsigned int j;
4750 /* The destination must be an xmm register. */
4751 assert (i.reg_operands
4752 && MAX_OPERANDS > dup
4753 && operand_type_equal (&i.types[dest], &regxmm));
4755 if (i.tm.opcode_modifier.firstxmm0)
4757 /* The first operand is implicit and must be xmm0. */
4758 assert (operand_type_equal (&i.types[0], &regxmm));
4759 if (i.op[0].regs->reg_num != 0)
4760 return bad_implicit_operand (1);
4762 if (i.tm.opcode_modifier.vex3sources)
4764 /* Keep xmm0 for instructions with VEX prefix and 3
4765 sources. */
4766 goto duplicate;
4768 else
4770 /* We remove the first xmm0 and keep the number of
4771 operands unchanged, which in fact duplicates the
4772 destination. */
4773 for (j = 1; j < i.operands; j++)
4775 i.op[j - 1] = i.op[j];
4776 i.types[j - 1] = i.types[j];
4777 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
4781 else if (i.tm.opcode_modifier.implicit1stxmm0)
4783 assert ((MAX_OPERANDS - 1) > dup
4784 && i.tm.opcode_modifier.vex3sources);
4786 /* Add the implicit xmm0 for instructions with VEX prefix
4787 and 3 sources. */
4788 for (j = i.operands; j > 0; j--)
4790 i.op[j] = i.op[j - 1];
4791 i.types[j] = i.types[j - 1];
4792 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
4794 i.op[0].regs
4795 = (const reg_entry *) hash_find (reg_hash, "xmm0");
4796 i.types[0] = regxmm;
4797 i.tm.operand_types[0] = regxmm;
4799 i.operands += 2;
4800 i.reg_operands += 2;
4801 i.tm.operands += 2;
4803 dup++;
4804 dest++;
4805 i.op[dup] = i.op[dest];
4806 i.types[dup] = i.types[dest];
4807 i.tm.operand_types[dup] = i.tm.operand_types[dest];
4809 else
4811 duplicate:
4812 i.operands++;
4813 i.reg_operands++;
4814 i.tm.operands++;
4816 i.op[dup] = i.op[dest];
4817 i.types[dup] = i.types[dest];
4818 i.tm.operand_types[dup] = i.tm.operand_types[dest];
4821 if (i.tm.opcode_modifier.immext)
4822 process_immext ();
4824 else if (i.tm.opcode_modifier.firstxmm0)
4826 unsigned int j;
4828 /* The first operand is implicit and must be xmm0/ymm0. */
4829 assert (i.reg_operands
4830 && (operand_type_equal (&i.types[0], &regxmm)
4831 || operand_type_equal (&i.types[0], &regymm)));
4832 if (i.op[0].regs->reg_num != 0)
4833 return bad_implicit_operand (i.types[0].bitfield.regxmm);
4835 for (j = 1; j < i.operands; j++)
4837 i.op[j - 1] = i.op[j];
4838 i.types[j - 1] = i.types[j];
4840 /* We need to adjust fields in i.tm since they are used by
4841 build_modrm_byte. */
4842 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
4845 i.operands--;
4846 i.reg_operands--;
4847 i.tm.operands--;
4849 else if (i.tm.opcode_modifier.regkludge)
4851 /* The imul $imm, %reg instruction is converted into
4852 imul $imm, %reg, %reg, and the clr %reg instruction
4853 is converted into xor %reg, %reg. */
4855 unsigned int first_reg_op;
4857 if (operand_type_check (i.types[0], reg))
4858 first_reg_op = 0;
4859 else
4860 first_reg_op = 1;
4861 /* Pretend we saw the extra register operand. */
4862 assert (i.reg_operands == 1
4863 && i.op[first_reg_op + 1].regs == 0);
4864 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
4865 i.types[first_reg_op + 1] = i.types[first_reg_op];
4866 i.operands++;
4867 i.reg_operands++;
4870 if (i.tm.opcode_modifier.shortform)
4872 if (i.types[0].bitfield.sreg2
4873 || i.types[0].bitfield.sreg3)
4875 if (i.tm.base_opcode == POP_SEG_SHORT
4876 && i.op[0].regs->reg_num == 1)
4878 as_bad (_("you can't `pop %%cs'"));
4879 return 0;
4881 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
4882 if ((i.op[0].regs->reg_flags & RegRex) != 0)
4883 i.rex |= REX_B;
4885 else
4887 /* The register or float register operand is in operand
4888 0 or 1. */
4889 unsigned int op;
4891 if (i.types[0].bitfield.floatreg
4892 || operand_type_check (i.types[0], reg))
4893 op = 0;
4894 else
4895 op = 1;
4896 /* Register goes in low 3 bits of opcode. */
4897 i.tm.base_opcode |= i.op[op].regs->reg_num;
4898 if ((i.op[op].regs->reg_flags & RegRex) != 0)
4899 i.rex |= REX_B;
4900 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
4902 /* Warn about some common errors, but press on regardless.
4903 The first case can be generated by gcc (<= 2.8.1). */
4904 if (i.operands == 2)
4906 /* Reversed arguments on faddp, fsubp, etc. */
4907 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
4908 register_prefix, i.op[1].regs->reg_name,
4909 register_prefix, i.op[0].regs->reg_name);
4911 else
4913 /* Extraneous `l' suffix on fp insn. */
4914 as_warn (_("translating to `%s %s%s'"), i.tm.name,
4915 register_prefix, i.op[0].regs->reg_name);
4920 else if (i.tm.opcode_modifier.modrm)
4922 /* The opcode is completed (modulo i.tm.extension_opcode which
4923 must be put into the modrm byte). Now, we make the modrm and
4924 index base bytes based on all the info we've collected. */
4926 default_seg = build_modrm_byte ();
4928 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
4930 default_seg = &ds;
4932 else if (i.tm.opcode_modifier.isstring)
4934 /* For the string instructions that allow a segment override
4935 on one of their operands, the default segment is ds. */
4936 default_seg = &ds;
4939 if (i.tm.base_opcode == 0x8d /* lea */
4940 && i.seg[0]
4941 && !quiet_warnings)
4942 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
4944 /* If a segment was explicitly specified, and the specified segment
4945 is not the default, use an opcode prefix to select it. If we
4946 never figured out what the default segment is, then default_seg
4947 will be zero at this point, and the specified segment prefix will
4948 always be used. */
4949 if ((i.seg[0]) && (i.seg[0] != default_seg))
4951 if (!add_prefix (i.seg[0]->seg_prefix))
4952 return 0;
4954 return 1;
4957 static const seg_entry *
4958 build_modrm_byte (void)
4960 const seg_entry *default_seg = 0;
4961 unsigned int source, dest;
4962 int vex_3_sources;
4964 /* The first operand of instructions with VEX prefix and 3 sources
4965 must be VEX_Imm4. */
4966 vex_3_sources = i.tm.opcode_modifier.vex3sources;
4967 if (vex_3_sources)
4969 unsigned int nds, reg;
4971 if (i.tm.opcode_modifier.veximmext
4972 && i.tm.opcode_modifier.immext)
4974 dest = i.operands - 2;
4975 assert (dest == 3);
4977 else
4978 dest = i.operands - 1;
4979 nds = dest - 1;
4981 /* There are 2 kinds of instructions:
4982 1. 5 operands: one immediate operand and 4 register
4983 operands or 3 register operands plus 1 memory operand.
4984 It must have VexNDS and VexW0 or VexW1. The destination
4985 must be either XMM or YMM register.
4986 2. 4 operands: 4 register operands or 3 register operands
4987 plus 1 memory operand. It must have VexNDS and VexImmExt. */
4988 if (!((i.reg_operands == 4
4989 || (i.reg_operands == 3 && i.mem_operands == 1))
4990 && i.tm.opcode_modifier.vexnds
4991 && (operand_type_equal (&i.tm.operand_types[dest], &regxmm)
4992 || operand_type_equal (&i.tm.operand_types[dest], &regymm))
4993 && (operand_type_equal (&i.tm.operand_types[nds], &regxmm)
4994 || operand_type_equal (&i.tm.operand_types[nds], &regymm))
4995 && ((dest == 4
4996 && i.imm_operands == 1
4997 && i.types[0].bitfield.vex_imm4
4998 && (i.tm.opcode_modifier.vexw0
4999 || i.tm.opcode_modifier.vexw1))
5000 || (dest == 3
5001 && (i.imm_operands == 0
5002 || (i.imm_operands == 1
5003 && i.tm.opcode_modifier.immext))
5004 && i.tm.opcode_modifier.veximmext))))
5005 abort ();
5007 i.vex.register_specifier = i.op[nds].regs;
5009 if (i.imm_operands == 0)
5011 /* When there is no immediate operand, generate an 8bit
5012 immediate operand to encode the first operand. */
5013 expressionS *exp = &im_expressions[i.imm_operands++];
5014 i.op[i.operands].imms = exp;
5015 i.types[i.operands] = imm8;
5016 i.operands++;
5017 /* If VexW1 is set, the first operand is the source and
5018 the second operand is encoded in the immediate operand. */
5019 if (i.tm.opcode_modifier.vexw1)
5021 source = 0;
5022 reg = 1;
5024 else
5026 source = 1;
5027 reg = 0;
5029 assert (operand_type_equal (&i.tm.operand_types[reg], &regxmm)
5030 || operand_type_equal (&i.tm.operand_types[reg],
5031 &regymm));
5032 exp->X_op = O_constant;
5033 exp->X_add_number
5034 = ((i.op[reg].regs->reg_num
5035 + ((i.op[reg].regs->reg_flags & RegRex) ? 8 : 0)) << 4);
5037 else
5039 unsigned int imm;
5041 if (i.tm.opcode_modifier.vexw0)
5043 /* If VexW0 is set, the third operand is the source and
5044 the second operand is encoded in the immediate
5045 operand. */
5046 source = 2;
5047 reg = 1;
5049 else
5051 /* VexW1 is set, the second operand is the source and
5052 the third operand is encoded in the immediate
5053 operand. */
5054 source = 1;
5055 reg = 2;
5058 if (i.tm.opcode_modifier.immext)
5060 /* When ImmExt is set, the immdiate byte is the last
5061 operand. */
5062 imm = i.operands - 1;
5063 source--;
5064 reg--;
5066 else
5068 imm = 0;
5070 /* Turn on Imm8 so that output_imm will generate it. */
5071 i.types[imm].bitfield.imm8 = 1;
5074 assert (operand_type_equal (&i.tm.operand_types[reg], &regxmm)
5075 || operand_type_equal (&i.tm.operand_types[reg],
5076 &regymm));
5077 i.op[imm].imms->X_add_number
5078 |= ((i.op[reg].regs->reg_num
5079 + ((i.op[reg].regs->reg_flags & RegRex) ? 8 : 0)) << 4);
5082 else
5083 source = dest = 0;
5085 /* SSE5 4 operand instructions are encoded in such a way that one of
5086 the inputs must match the destination register. Process_drex hides
5087 the 3rd argument in the drex field, so that by the time we get
5088 here, it looks to GAS as if this is a 2 operand instruction. */
5089 if ((i.tm.opcode_modifier.drex
5090 || i.tm.opcode_modifier.drexv
5091 || i.tm.opcode_modifier.drexc)
5092 && i.reg_operands == 2)
5094 const reg_entry *reg = i.op[i.drex.modrm_reg].regs;
5095 const reg_entry *regmem = i.op[i.drex.modrm_regmem].regs;
5097 i.rm.reg = reg->reg_num;
5098 i.rm.regmem = regmem->reg_num;
5099 i.rm.mode = 3;
5100 if ((reg->reg_flags & RegRex) != 0)
5101 i.rex |= REX_R;
5102 if ((regmem->reg_flags & RegRex) != 0)
5103 i.rex |= REX_B;
5106 /* i.reg_operands MUST be the number of real register operands;
5107 implicit registers do not count. If there are 3 register
5108 operands, it must be a instruction with VexNDS. For a
5109 instruction with VexNDD, the destination register is encoded
5110 in VEX prefix. If there are 4 register operands, it must be
5111 a instruction with VEX prefix and 3 sources. */
5112 else if (i.mem_operands == 0
5113 && ((i.reg_operands == 2
5114 && !i.tm.opcode_modifier.vexndd)
5115 || (i.reg_operands == 3
5116 && i.tm.opcode_modifier.vexnds)
5117 || (i.reg_operands == 4 && vex_3_sources)))
5119 switch (i.operands)
5121 case 2:
5122 source = 0;
5123 break;
5124 case 3:
5125 /* When there are 3 operands, one of them may be immediate,
5126 which may be the first or the last operand. Otherwise,
5127 the first operand must be shift count register (cl) or it
5128 is an instruction with VexNDS. */
5129 assert (i.imm_operands == 1
5130 || (i.imm_operands == 0
5131 && (i.tm.opcode_modifier.vexnds
5132 || i.types[0].bitfield.shiftcount)));
5133 if (operand_type_check (i.types[0], imm)
5134 || i.types[0].bitfield.shiftcount)
5135 source = 1;
5136 else
5137 source = 0;
5138 break;
5139 case 4:
5140 /* When there are 4 operands, the first two must be 8bit
5141 immediate operands. The source operand will be the 3rd
5142 one.
5144 For instructions with VexNDS, if the first operand
5145 an imm8, the source operand is the 2nd one. If the last
5146 operand is imm8, the source operand is the first one. */
5147 assert ((i.imm_operands == 2
5148 && i.types[0].bitfield.imm8
5149 && i.types[1].bitfield.imm8)
5150 || (i.tm.opcode_modifier.vexnds
5151 && i.imm_operands == 1
5152 && (i.types[0].bitfield.imm8
5153 || i.types[i.operands - 1].bitfield.imm8)));
5154 if (i.tm.opcode_modifier.vexnds)
5156 if (i.types[0].bitfield.imm8)
5157 source = 1;
5158 else
5159 source = 0;
5161 else
5162 source = 2;
5163 break;
5164 case 5:
5165 break;
5166 default:
5167 abort ();
5170 if (!vex_3_sources)
5172 dest = source + 1;
5174 if (i.tm.opcode_modifier.vexnds)
5176 /* For instructions with VexNDS, the register-only
5177 source operand must be XMM or YMM register. It is
5178 encoded in VEX prefix. */
5179 if ((dest + 1) >= i.operands
5180 || (!operand_type_equal (&i.tm.operand_types[dest],
5181 &regxmm)
5182 && !operand_type_equal (&i.tm.operand_types[dest],
5183 &regymm)))
5184 abort ();
5185 i.vex.register_specifier = i.op[dest].regs;
5186 dest++;
5190 i.rm.mode = 3;
5191 /* One of the register operands will be encoded in the i.tm.reg
5192 field, the other in the combined i.tm.mode and i.tm.regmem
5193 fields. If no form of this instruction supports a memory
5194 destination operand, then we assume the source operand may
5195 sometimes be a memory operand and so we need to store the
5196 destination in the i.rm.reg field. */
5197 if (!i.tm.operand_types[dest].bitfield.regmem
5198 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
5200 i.rm.reg = i.op[dest].regs->reg_num;
5201 i.rm.regmem = i.op[source].regs->reg_num;
5202 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
5203 i.rex |= REX_R;
5204 if ((i.op[source].regs->reg_flags & RegRex) != 0)
5205 i.rex |= REX_B;
5207 else
5209 i.rm.reg = i.op[source].regs->reg_num;
5210 i.rm.regmem = i.op[dest].regs->reg_num;
5211 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
5212 i.rex |= REX_B;
5213 if ((i.op[source].regs->reg_flags & RegRex) != 0)
5214 i.rex |= REX_R;
5216 if (flag_code != CODE_64BIT && (i.rex & (REX_R | REX_B)))
5218 if (!i.types[0].bitfield.control
5219 && !i.types[1].bitfield.control)
5220 abort ();
5221 i.rex &= ~(REX_R | REX_B);
5222 add_prefix (LOCK_PREFIX_OPCODE);
5225 else
5226 { /* If it's not 2 reg operands... */
5227 unsigned int mem;
5229 if (i.mem_operands)
5231 unsigned int fake_zero_displacement = 0;
5232 unsigned int op;
5234 /* This has been precalculated for SSE5 instructions
5235 that have a DREX field earlier in process_drex. */
5236 if (i.tm.opcode_modifier.drex
5237 || i.tm.opcode_modifier.drexv
5238 || i.tm.opcode_modifier.drexc)
5239 op = i.drex.modrm_regmem;
5240 else
5242 for (op = 0; op < i.operands; op++)
5243 if (operand_type_check (i.types[op], anymem))
5244 break;
5245 assert (op < i.operands);
5248 default_seg = &ds;
5250 if (i.base_reg == 0)
5252 i.rm.mode = 0;
5253 if (!i.disp_operands)
5254 fake_zero_displacement = 1;
5255 if (i.index_reg == 0)
5257 /* Operand is just <disp> */
5258 if (flag_code == CODE_64BIT)
5260 /* 64bit mode overwrites the 32bit absolute
5261 addressing by RIP relative addressing and
5262 absolute addressing is encoded by one of the
5263 redundant SIB forms. */
5264 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5265 i.sib.base = NO_BASE_REGISTER;
5266 i.sib.index = NO_INDEX_REGISTER;
5267 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0)
5268 ? disp32s : disp32);
5270 else if ((flag_code == CODE_16BIT)
5271 ^ (i.prefix[ADDR_PREFIX] != 0))
5273 i.rm.regmem = NO_BASE_REGISTER_16;
5274 i.types[op] = disp16;
5276 else
5278 i.rm.regmem = NO_BASE_REGISTER;
5279 i.types[op] = disp32;
5282 else /* !i.base_reg && i.index_reg */
5284 if (i.index_reg->reg_num == RegEiz
5285 || i.index_reg->reg_num == RegRiz)
5286 i.sib.index = NO_INDEX_REGISTER;
5287 else
5288 i.sib.index = i.index_reg->reg_num;
5289 i.sib.base = NO_BASE_REGISTER;
5290 i.sib.scale = i.log2_scale_factor;
5291 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5292 i.types[op].bitfield.disp8 = 0;
5293 i.types[op].bitfield.disp16 = 0;
5294 i.types[op].bitfield.disp64 = 0;
5295 if (flag_code != CODE_64BIT)
5297 /* Must be 32 bit */
5298 i.types[op].bitfield.disp32 = 1;
5299 i.types[op].bitfield.disp32s = 0;
5301 else
5303 i.types[op].bitfield.disp32 = 0;
5304 i.types[op].bitfield.disp32s = 1;
5306 if ((i.index_reg->reg_flags & RegRex) != 0)
5307 i.rex |= REX_X;
5310 /* RIP addressing for 64bit mode. */
5311 else if (i.base_reg->reg_num == RegRip ||
5312 i.base_reg->reg_num == RegEip)
5314 i.rm.regmem = NO_BASE_REGISTER;
5315 i.types[op].bitfield.disp8 = 0;
5316 i.types[op].bitfield.disp16 = 0;
5317 i.types[op].bitfield.disp32 = 0;
5318 i.types[op].bitfield.disp32s = 1;
5319 i.types[op].bitfield.disp64 = 0;
5320 i.flags[op] |= Operand_PCrel;
5321 if (! i.disp_operands)
5322 fake_zero_displacement = 1;
5324 else if (i.base_reg->reg_type.bitfield.reg16)
5326 switch (i.base_reg->reg_num)
5328 case 3: /* (%bx) */
5329 if (i.index_reg == 0)
5330 i.rm.regmem = 7;
5331 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
5332 i.rm.regmem = i.index_reg->reg_num - 6;
5333 break;
5334 case 5: /* (%bp) */
5335 default_seg = &ss;
5336 if (i.index_reg == 0)
5338 i.rm.regmem = 6;
5339 if (operand_type_check (i.types[op], disp) == 0)
5341 /* fake (%bp) into 0(%bp) */
5342 i.types[op].bitfield.disp8 = 1;
5343 fake_zero_displacement = 1;
5346 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
5347 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
5348 break;
5349 default: /* (%si) -> 4 or (%di) -> 5 */
5350 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
5352 i.rm.mode = mode_from_disp_size (i.types[op]);
5354 else /* i.base_reg and 32/64 bit mode */
5356 if (flag_code == CODE_64BIT
5357 && operand_type_check (i.types[op], disp))
5359 i386_operand_type temp;
5360 operand_type_set (&temp, 0);
5361 temp.bitfield.disp8 = i.types[op].bitfield.disp8;
5362 i.types[op] = temp;
5363 if (i.prefix[ADDR_PREFIX] == 0)
5364 i.types[op].bitfield.disp32s = 1;
5365 else
5366 i.types[op].bitfield.disp32 = 1;
5369 i.rm.regmem = i.base_reg->reg_num;
5370 if ((i.base_reg->reg_flags & RegRex) != 0)
5371 i.rex |= REX_B;
5372 i.sib.base = i.base_reg->reg_num;
5373 /* x86-64 ignores REX prefix bit here to avoid decoder
5374 complications. */
5375 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
5377 default_seg = &ss;
5378 if (i.disp_operands == 0)
5380 fake_zero_displacement = 1;
5381 i.types[op].bitfield.disp8 = 1;
5384 else if (i.base_reg->reg_num == ESP_REG_NUM)
5386 default_seg = &ss;
5388 i.sib.scale = i.log2_scale_factor;
5389 if (i.index_reg == 0)
5391 /* <disp>(%esp) becomes two byte modrm with no index
5392 register. We've already stored the code for esp
5393 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
5394 Any base register besides %esp will not use the
5395 extra modrm byte. */
5396 i.sib.index = NO_INDEX_REGISTER;
5398 else
5400 if (i.index_reg->reg_num == RegEiz
5401 || i.index_reg->reg_num == RegRiz)
5402 i.sib.index = NO_INDEX_REGISTER;
5403 else
5404 i.sib.index = i.index_reg->reg_num;
5405 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5406 if ((i.index_reg->reg_flags & RegRex) != 0)
5407 i.rex |= REX_X;
5410 if (i.disp_operands
5411 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5412 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
5413 i.rm.mode = 0;
5414 else
5415 i.rm.mode = mode_from_disp_size (i.types[op]);
5418 if (fake_zero_displacement)
5420 /* Fakes a zero displacement assuming that i.types[op]
5421 holds the correct displacement size. */
5422 expressionS *exp;
5424 assert (i.op[op].disps == 0);
5425 exp = &disp_expressions[i.disp_operands++];
5426 i.op[op].disps = exp;
5427 exp->X_op = O_constant;
5428 exp->X_add_number = 0;
5429 exp->X_add_symbol = (symbolS *) 0;
5430 exp->X_op_symbol = (symbolS *) 0;
5433 mem = op;
5435 else
5436 mem = ~0;
5438 /* Fill in i.rm.reg or i.rm.regmem field with register operand
5439 (if any) based on i.tm.extension_opcode. Again, we must be
5440 careful to make sure that segment/control/debug/test/MMX
5441 registers are coded into the i.rm.reg field. */
5442 if (i.reg_operands)
5444 unsigned int op;
5446 /* This has been precalculated for SSE5 instructions
5447 that have a DREX field earlier in process_drex. */
5448 if (i.tm.opcode_modifier.drex
5449 || i.tm.opcode_modifier.drexv
5450 || i.tm.opcode_modifier.drexc)
5452 op = i.drex.modrm_reg;
5453 i.rm.reg = i.op[op].regs->reg_num;
5454 if ((i.op[op].regs->reg_flags & RegRex) != 0)
5455 i.rex |= REX_R;
5457 else
5459 unsigned int vex_reg = ~0;
5461 for (op = 0; op < i.operands; op++)
5462 if (i.types[op].bitfield.reg8
5463 || i.types[op].bitfield.reg16
5464 || i.types[op].bitfield.reg32
5465 || i.types[op].bitfield.reg64
5466 || i.types[op].bitfield.regmmx
5467 || i.types[op].bitfield.regxmm
5468 || i.types[op].bitfield.regymm
5469 || i.types[op].bitfield.sreg2
5470 || i.types[op].bitfield.sreg3
5471 || i.types[op].bitfield.control
5472 || i.types[op].bitfield.debug
5473 || i.types[op].bitfield.test)
5474 break;
5476 if (vex_3_sources)
5477 op = dest;
5478 else if (i.tm.opcode_modifier.vexnds)
5480 /* For instructions with VexNDS, the register-only
5481 source operand is encoded in VEX prefix. */
5482 assert (mem != (unsigned int) ~0);
5484 if (op > mem)
5486 vex_reg = op++;
5487 assert (op < i.operands);
5489 else
5491 vex_reg = op + 1;
5492 assert (vex_reg < i.operands);
5495 else if (i.tm.opcode_modifier.vexndd)
5497 /* For instructions with VexNDD, there should be
5498 no memory operand and the register destination
5499 is encoded in VEX prefix. */
5500 assert (i.mem_operands == 0
5501 && (op + 2) == i.operands);
5502 vex_reg = op + 1;
5504 else
5505 assert (op < i.operands);
5507 if (vex_reg != (unsigned int) ~0)
5509 assert (i.reg_operands == 2);
5511 if (!operand_type_equal (&i.tm.operand_types[vex_reg],
5512 & regxmm)
5513 && !operand_type_equal (&i.tm.operand_types[vex_reg],
5514 &regymm))
5515 abort ();
5516 i.vex.register_specifier = i.op[vex_reg].regs;
5519 /* If there is an extension opcode to put here, the
5520 register number must be put into the regmem field. */
5521 if (i.tm.extension_opcode != None)
5523 i.rm.regmem = i.op[op].regs->reg_num;
5524 if ((i.op[op].regs->reg_flags & RegRex) != 0)
5525 i.rex |= REX_B;
5527 else
5529 i.rm.reg = i.op[op].regs->reg_num;
5530 if ((i.op[op].regs->reg_flags & RegRex) != 0)
5531 i.rex |= REX_R;
5535 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
5536 must set it to 3 to indicate this is a register operand
5537 in the regmem field. */
5538 if (!i.mem_operands)
5539 i.rm.mode = 3;
5542 /* Fill in i.rm.reg field with extension opcode (if any). */
5543 if (i.tm.extension_opcode != None
5544 && !(i.tm.opcode_modifier.drex
5545 || i.tm.opcode_modifier.drexv
5546 || i.tm.opcode_modifier.drexc))
5547 i.rm.reg = i.tm.extension_opcode;
5549 return default_seg;
5552 static void
5553 output_branch (void)
5555 char *p;
5556 int code16;
5557 int prefix;
5558 relax_substateT subtype;
5559 symbolS *sym;
5560 offsetT off;
5562 code16 = 0;
5563 if (flag_code == CODE_16BIT)
5564 code16 = CODE16;
5566 prefix = 0;
5567 if (i.prefix[DATA_PREFIX] != 0)
5569 prefix = 1;
5570 i.prefixes -= 1;
5571 code16 ^= CODE16;
5573 /* Pentium4 branch hints. */
5574 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
5575 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
5577 prefix++;
5578 i.prefixes--;
5580 if (i.prefix[REX_PREFIX] != 0)
5582 prefix++;
5583 i.prefixes--;
5586 if (i.prefixes != 0 && !intel_syntax)
5587 as_warn (_("skipping prefixes on this instruction"));
5589 /* It's always a symbol; End frag & setup for relax.
5590 Make sure there is enough room in this frag for the largest
5591 instruction we may generate in md_convert_frag. This is 2
5592 bytes for the opcode and room for the prefix and largest
5593 displacement. */
5594 frag_grow (prefix + 2 + 4);
5595 /* Prefix and 1 opcode byte go in fr_fix. */
5596 p = frag_more (prefix + 1);
5597 if (i.prefix[DATA_PREFIX] != 0)
5598 *p++ = DATA_PREFIX_OPCODE;
5599 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
5600 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
5601 *p++ = i.prefix[SEG_PREFIX];
5602 if (i.prefix[REX_PREFIX] != 0)
5603 *p++ = i.prefix[REX_PREFIX];
5604 *p = i.tm.base_opcode;
5606 if ((unsigned char) *p == JUMP_PC_RELATIVE)
5607 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
5608 else if (cpu_arch_flags.bitfield.cpui386)
5609 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
5610 else
5611 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
5612 subtype |= code16;
5614 sym = i.op[0].disps->X_add_symbol;
5615 off = i.op[0].disps->X_add_number;
5617 if (i.op[0].disps->X_op != O_constant
5618 && i.op[0].disps->X_op != O_symbol)
5620 /* Handle complex expressions. */
5621 sym = make_expr_symbol (i.op[0].disps);
5622 off = 0;
5625 /* 1 possible extra opcode + 4 byte displacement go in var part.
5626 Pass reloc in fr_var. */
5627 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
5630 static void
5631 output_jump (void)
5633 char *p;
5634 int size;
5635 fixS *fixP;
5637 if (i.tm.opcode_modifier.jumpbyte)
5639 /* This is a loop or jecxz type instruction. */
5640 size = 1;
5641 if (i.prefix[ADDR_PREFIX] != 0)
5643 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
5644 i.prefixes -= 1;
5646 /* Pentium4 branch hints. */
5647 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
5648 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
5650 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
5651 i.prefixes--;
5654 else
5656 int code16;
5658 code16 = 0;
5659 if (flag_code == CODE_16BIT)
5660 code16 = CODE16;
5662 if (i.prefix[DATA_PREFIX] != 0)
5664 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
5665 i.prefixes -= 1;
5666 code16 ^= CODE16;
5669 size = 4;
5670 if (code16)
5671 size = 2;
5674 if (i.prefix[REX_PREFIX] != 0)
5676 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
5677 i.prefixes -= 1;
5680 if (i.prefixes != 0 && !intel_syntax)
5681 as_warn (_("skipping prefixes on this instruction"));
5683 p = frag_more (1 + size);
5684 *p++ = i.tm.base_opcode;
5686 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
5687 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
5689 /* All jumps handled here are signed, but don't use a signed limit
5690 check for 32 and 16 bit jumps as we want to allow wrap around at
5691 4G and 64k respectively. */
5692 if (size == 1)
5693 fixP->fx_signed = 1;
5696 static void
5697 output_interseg_jump (void)
5699 char *p;
5700 int size;
5701 int prefix;
5702 int code16;
5704 code16 = 0;
5705 if (flag_code == CODE_16BIT)
5706 code16 = CODE16;
5708 prefix = 0;
5709 if (i.prefix[DATA_PREFIX] != 0)
5711 prefix = 1;
5712 i.prefixes -= 1;
5713 code16 ^= CODE16;
5715 if (i.prefix[REX_PREFIX] != 0)
5717 prefix++;
5718 i.prefixes -= 1;
5721 size = 4;
5722 if (code16)
5723 size = 2;
5725 if (i.prefixes != 0 && !intel_syntax)
5726 as_warn (_("skipping prefixes on this instruction"));
5728 /* 1 opcode; 2 segment; offset */
5729 p = frag_more (prefix + 1 + 2 + size);
5731 if (i.prefix[DATA_PREFIX] != 0)
5732 *p++ = DATA_PREFIX_OPCODE;
5734 if (i.prefix[REX_PREFIX] != 0)
5735 *p++ = i.prefix[REX_PREFIX];
5737 *p++ = i.tm.base_opcode;
5738 if (i.op[1].imms->X_op == O_constant)
5740 offsetT n = i.op[1].imms->X_add_number;
5742 if (size == 2
5743 && !fits_in_unsigned_word (n)
5744 && !fits_in_signed_word (n))
5746 as_bad (_("16-bit jump out of range"));
5747 return;
5749 md_number_to_chars (p, n, size);
5751 else
5752 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
5753 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
5754 if (i.op[0].imms->X_op != O_constant)
5755 as_bad (_("can't handle non absolute segment in `%s'"),
5756 i.tm.name);
5757 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
5760 static void
5761 output_insn (void)
5763 fragS *insn_start_frag;
5764 offsetT insn_start_off;
5766 /* Tie dwarf2 debug info to the address at the start of the insn.
5767 We can't do this after the insn has been output as the current
5768 frag may have been closed off. eg. by frag_var. */
5769 dwarf2_emit_insn (0);
5771 insn_start_frag = frag_now;
5772 insn_start_off = frag_now_fix ();
5774 /* Output jumps. */
5775 if (i.tm.opcode_modifier.jump)
5776 output_branch ();
5777 else if (i.tm.opcode_modifier.jumpbyte
5778 || i.tm.opcode_modifier.jumpdword)
5779 output_jump ();
5780 else if (i.tm.opcode_modifier.jumpintersegment)
5781 output_interseg_jump ();
5782 else
5784 /* Output normal instructions here. */
5785 char *p;
5786 unsigned char *q;
5787 unsigned int j;
5788 unsigned int prefix;
5790 /* Since the VEX prefix contains the implicit prefix, we don't
5791 need the explicit prefix. */
5792 if (!i.tm.opcode_modifier.vex)
5794 switch (i.tm.opcode_length)
5796 case 3:
5797 if (i.tm.base_opcode & 0xff000000)
5799 prefix = (i.tm.base_opcode >> 24) & 0xff;
5800 goto check_prefix;
5802 break;
5803 case 2:
5804 if ((i.tm.base_opcode & 0xff0000) != 0)
5806 prefix = (i.tm.base_opcode >> 16) & 0xff;
5807 if (i.tm.cpu_flags.bitfield.cpupadlock)
5809 check_prefix:
5810 if (prefix != REPE_PREFIX_OPCODE
5811 || (i.prefix[LOCKREP_PREFIX]
5812 != REPE_PREFIX_OPCODE))
5813 add_prefix (prefix);
5815 else
5816 add_prefix (prefix);
5818 break;
5819 case 1:
5820 break;
5821 default:
5822 abort ();
5825 /* The prefix bytes. */
5826 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
5827 if (*q)
5828 FRAG_APPEND_1_CHAR (*q);
5831 if (i.tm.opcode_modifier.vex)
5833 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
5834 if (*q)
5835 switch (j)
5837 case REX_PREFIX:
5838 /* REX byte is encoded in VEX prefix. */
5839 break;
5840 case SEG_PREFIX:
5841 case ADDR_PREFIX:
5842 FRAG_APPEND_1_CHAR (*q);
5843 break;
5844 default:
5845 /* There should be no other prefixes for instructions
5846 with VEX prefix. */
5847 abort ();
5850 /* Now the VEX prefix. */
5851 p = frag_more (i.vex.length);
5852 for (j = 0; j < i.vex.length; j++)
5853 p[j] = i.vex.bytes[j];
5856 /* Now the opcode; be careful about word order here! */
5857 if (i.tm.opcode_length == 1)
5859 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
5861 else
5863 switch (i.tm.opcode_length)
5865 case 3:
5866 p = frag_more (3);
5867 *p++ = (i.tm.base_opcode >> 16) & 0xff;
5868 break;
5869 case 2:
5870 p = frag_more (2);
5871 break;
5872 default:
5873 abort ();
5874 break;
5877 /* Put out high byte first: can't use md_number_to_chars! */
5878 *p++ = (i.tm.base_opcode >> 8) & 0xff;
5879 *p = i.tm.base_opcode & 0xff;
5881 /* On SSE5, encode the OC1 bit in the DREX field if this
5882 encoding has multiple formats. */
5883 if (i.tm.opcode_modifier.drex
5884 && i.tm.opcode_modifier.drexv
5885 && DREX_OC1 (i.tm.extension_opcode))
5886 *p |= DREX_OC1_MASK;
5889 /* Now the modrm byte and sib byte (if present). */
5890 if (i.tm.opcode_modifier.modrm)
5892 FRAG_APPEND_1_CHAR ((i.rm.regmem << 0
5893 | i.rm.reg << 3
5894 | i.rm.mode << 6));
5895 /* If i.rm.regmem == ESP (4)
5896 && i.rm.mode != (Register mode)
5897 && not 16 bit
5898 ==> need second modrm byte. */
5899 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
5900 && i.rm.mode != 3
5901 && !(i.base_reg && i.base_reg->reg_type.bitfield.reg16))
5902 FRAG_APPEND_1_CHAR ((i.sib.base << 0
5903 | i.sib.index << 3
5904 | i.sib.scale << 6));
5907 /* Write the DREX byte if needed. */
5908 if (i.tm.opcode_modifier.drex || i.tm.opcode_modifier.drexc)
5910 p = frag_more (1);
5911 *p = (((i.drex.reg & 0xf) << 4) | (i.drex.rex & 0x7));
5913 /* Encode the OC0 bit if this encoding has multiple
5914 formats. */
5915 if ((i.tm.opcode_modifier.drex
5916 || i.tm.opcode_modifier.drexv)
5917 && DREX_OC0 (i.tm.extension_opcode))
5918 *p |= DREX_OC0_MASK;
5921 if (i.disp_operands)
5922 output_disp (insn_start_frag, insn_start_off);
5924 if (i.imm_operands)
5925 output_imm (insn_start_frag, insn_start_off);
5928 #ifdef DEBUG386
5929 if (flag_debug)
5931 pi ("" /*line*/, &i);
5933 #endif /* DEBUG386 */
5936 /* Return the size of the displacement operand N. */
5938 static int
5939 disp_size (unsigned int n)
5941 int size = 4;
5942 if (i.types[n].bitfield.disp64)
5943 size = 8;
5944 else if (i.types[n].bitfield.disp8)
5945 size = 1;
5946 else if (i.types[n].bitfield.disp16)
5947 size = 2;
5948 return size;
5951 /* Return the size of the immediate operand N. */
5953 static int
5954 imm_size (unsigned int n)
5956 int size = 4;
5957 if (i.types[n].bitfield.imm64)
5958 size = 8;
5959 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
5960 size = 1;
5961 else if (i.types[n].bitfield.imm16)
5962 size = 2;
5963 return size;
5966 static void
5967 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
5969 char *p;
5970 unsigned int n;
5972 for (n = 0; n < i.operands; n++)
5974 if (operand_type_check (i.types[n], disp))
5976 if (i.op[n].disps->X_op == O_constant)
5978 int size = disp_size (n);
5979 offsetT val;
5981 val = offset_in_range (i.op[n].disps->X_add_number,
5982 size);
5983 p = frag_more (size);
5984 md_number_to_chars (p, val, size);
5986 else
5988 enum bfd_reloc_code_real reloc_type;
5989 int size = disp_size (n);
5990 int sign = i.types[n].bitfield.disp32s;
5991 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
5993 /* We can't have 8 bit displacement here. */
5994 assert (!i.types[n].bitfield.disp8);
5996 /* The PC relative address is computed relative
5997 to the instruction boundary, so in case immediate
5998 fields follows, we need to adjust the value. */
5999 if (pcrel && i.imm_operands)
6001 unsigned int n1;
6002 int sz = 0;
6004 for (n1 = 0; n1 < i.operands; n1++)
6005 if (operand_type_check (i.types[n1], imm))
6007 /* Only one immediate is allowed for PC
6008 relative address. */
6009 assert (sz == 0);
6010 sz = imm_size (n1);
6011 i.op[n].disps->X_add_number -= sz;
6013 /* We should find the immediate. */
6014 assert (sz != 0);
6017 p = frag_more (size);
6018 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
6019 if (GOT_symbol
6020 && GOT_symbol == i.op[n].disps->X_add_symbol
6021 && (((reloc_type == BFD_RELOC_32
6022 || reloc_type == BFD_RELOC_X86_64_32S
6023 || (reloc_type == BFD_RELOC_64
6024 && object_64bit))
6025 && (i.op[n].disps->X_op == O_symbol
6026 || (i.op[n].disps->X_op == O_add
6027 && ((symbol_get_value_expression
6028 (i.op[n].disps->X_op_symbol)->X_op)
6029 == O_subtract))))
6030 || reloc_type == BFD_RELOC_32_PCREL))
6032 offsetT add;
6034 if (insn_start_frag == frag_now)
6035 add = (p - frag_now->fr_literal) - insn_start_off;
6036 else
6038 fragS *fr;
6040 add = insn_start_frag->fr_fix - insn_start_off;
6041 for (fr = insn_start_frag->fr_next;
6042 fr && fr != frag_now; fr = fr->fr_next)
6043 add += fr->fr_fix;
6044 add += p - frag_now->fr_literal;
6047 if (!object_64bit)
6049 reloc_type = BFD_RELOC_386_GOTPC;
6050 i.op[n].imms->X_add_number += add;
6052 else if (reloc_type == BFD_RELOC_64)
6053 reloc_type = BFD_RELOC_X86_64_GOTPC64;
6054 else
6055 /* Don't do the adjustment for x86-64, as there
6056 the pcrel addressing is relative to the _next_
6057 insn, and that is taken care of in other code. */
6058 reloc_type = BFD_RELOC_X86_64_GOTPC32;
6060 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6061 i.op[n].disps, pcrel, reloc_type);
6067 static void
6068 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
6070 char *p;
6071 unsigned int n;
6073 for (n = 0; n < i.operands; n++)
6075 if (operand_type_check (i.types[n], imm))
6077 if (i.op[n].imms->X_op == O_constant)
6079 int size = imm_size (n);
6080 offsetT val;
6082 val = offset_in_range (i.op[n].imms->X_add_number,
6083 size);
6084 p = frag_more (size);
6085 md_number_to_chars (p, val, size);
6087 else
6089 /* Not absolute_section.
6090 Need a 32-bit fixup (don't support 8bit
6091 non-absolute imms). Try to support other
6092 sizes ... */
6093 enum bfd_reloc_code_real reloc_type;
6094 int size = imm_size (n);
6095 int sign;
6097 if (i.types[n].bitfield.imm32s
6098 && (i.suffix == QWORD_MNEM_SUFFIX
6099 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
6100 sign = 1;
6101 else
6102 sign = 0;
6104 p = frag_more (size);
6105 reloc_type = reloc (size, 0, sign, i.reloc[n]);
6107 /* This is tough to explain. We end up with this one if we
6108 * have operands that look like
6109 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
6110 * obtain the absolute address of the GOT, and it is strongly
6111 * preferable from a performance point of view to avoid using
6112 * a runtime relocation for this. The actual sequence of
6113 * instructions often look something like:
6115 * call .L66
6116 * .L66:
6117 * popl %ebx
6118 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
6120 * The call and pop essentially return the absolute address
6121 * of the label .L66 and store it in %ebx. The linker itself
6122 * will ultimately change the first operand of the addl so
6123 * that %ebx points to the GOT, but to keep things simple, the
6124 * .o file must have this operand set so that it generates not
6125 * the absolute address of .L66, but the absolute address of
6126 * itself. This allows the linker itself simply treat a GOTPC
6127 * relocation as asking for a pcrel offset to the GOT to be
6128 * added in, and the addend of the relocation is stored in the
6129 * operand field for the instruction itself.
6131 * Our job here is to fix the operand so that it would add
6132 * the correct offset so that %ebx would point to itself. The
6133 * thing that is tricky is that .-.L66 will point to the
6134 * beginning of the instruction, so we need to further modify
6135 * the operand so that it will point to itself. There are
6136 * other cases where you have something like:
6138 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
6140 * and here no correction would be required. Internally in
6141 * the assembler we treat operands of this form as not being
6142 * pcrel since the '.' is explicitly mentioned, and I wonder
6143 * whether it would simplify matters to do it this way. Who
6144 * knows. In earlier versions of the PIC patches, the
6145 * pcrel_adjust field was used to store the correction, but
6146 * since the expression is not pcrel, I felt it would be
6147 * confusing to do it this way. */
6149 if ((reloc_type == BFD_RELOC_32
6150 || reloc_type == BFD_RELOC_X86_64_32S
6151 || reloc_type == BFD_RELOC_64)
6152 && GOT_symbol
6153 && GOT_symbol == i.op[n].imms->X_add_symbol
6154 && (i.op[n].imms->X_op == O_symbol
6155 || (i.op[n].imms->X_op == O_add
6156 && ((symbol_get_value_expression
6157 (i.op[n].imms->X_op_symbol)->X_op)
6158 == O_subtract))))
6160 offsetT add;
6162 if (insn_start_frag == frag_now)
6163 add = (p - frag_now->fr_literal) - insn_start_off;
6164 else
6166 fragS *fr;
6168 add = insn_start_frag->fr_fix - insn_start_off;
6169 for (fr = insn_start_frag->fr_next;
6170 fr && fr != frag_now; fr = fr->fr_next)
6171 add += fr->fr_fix;
6172 add += p - frag_now->fr_literal;
6175 if (!object_64bit)
6176 reloc_type = BFD_RELOC_386_GOTPC;
6177 else if (size == 4)
6178 reloc_type = BFD_RELOC_X86_64_GOTPC32;
6179 else if (size == 8)
6180 reloc_type = BFD_RELOC_X86_64_GOTPC64;
6181 i.op[n].imms->X_add_number += add;
6183 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6184 i.op[n].imms, 0, reloc_type);
6190 /* x86_cons_fix_new is called via the expression parsing code when a
6191 reloc is needed. We use this hook to get the correct .got reloc. */
6192 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
6193 static int cons_sign = -1;
6195 void
6196 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
6197 expressionS *exp)
6199 enum bfd_reloc_code_real r = reloc (len, 0, cons_sign, got_reloc);
6201 got_reloc = NO_RELOC;
6203 #ifdef TE_PE
6204 if (exp->X_op == O_secrel)
6206 exp->X_op = O_symbol;
6207 r = BFD_RELOC_32_SECREL;
6209 #endif
6211 fix_new_exp (frag, off, len, exp, 0, r);
6214 #if (!defined (OBJ_ELF) && !defined (OBJ_MAYBE_ELF)) || defined (LEX_AT)
6215 # define lex_got(reloc, adjust, types) NULL
6216 #else
6217 /* Parse operands of the form
6218 <symbol>@GOTOFF+<nnn>
6219 and similar .plt or .got references.
6221 If we find one, set up the correct relocation in RELOC and copy the
6222 input string, minus the `@GOTOFF' into a malloc'd buffer for
6223 parsing by the calling routine. Return this buffer, and if ADJUST
6224 is non-null set it to the length of the string we removed from the
6225 input line. Otherwise return NULL. */
6226 static char *
6227 lex_got (enum bfd_reloc_code_real *reloc,
6228 int *adjust,
6229 i386_operand_type *types)
6231 /* Some of the relocations depend on the size of what field is to
6232 be relocated. But in our callers i386_immediate and i386_displacement
6233 we don't yet know the operand size (this will be set by insn
6234 matching). Hence we record the word32 relocation here,
6235 and adjust the reloc according to the real size in reloc(). */
6236 static const struct {
6237 const char *str;
6238 const enum bfd_reloc_code_real rel[2];
6239 const i386_operand_type types64;
6240 } gotrel[] = {
6241 { "PLTOFF", { 0,
6242 BFD_RELOC_X86_64_PLTOFF64 },
6243 OPERAND_TYPE_IMM64 },
6244 { "PLT", { BFD_RELOC_386_PLT32,
6245 BFD_RELOC_X86_64_PLT32 },
6246 OPERAND_TYPE_IMM32_32S_DISP32 },
6247 { "GOTPLT", { 0,
6248 BFD_RELOC_X86_64_GOTPLT64 },
6249 OPERAND_TYPE_IMM64_DISP64 },
6250 { "GOTOFF", { BFD_RELOC_386_GOTOFF,
6251 BFD_RELOC_X86_64_GOTOFF64 },
6252 OPERAND_TYPE_IMM64_DISP64 },
6253 { "GOTPCREL", { 0,
6254 BFD_RELOC_X86_64_GOTPCREL },
6255 OPERAND_TYPE_IMM32_32S_DISP32 },
6256 { "TLSGD", { BFD_RELOC_386_TLS_GD,
6257 BFD_RELOC_X86_64_TLSGD },
6258 OPERAND_TYPE_IMM32_32S_DISP32 },
6259 { "TLSLDM", { BFD_RELOC_386_TLS_LDM,
6260 0 },
6261 OPERAND_TYPE_NONE },
6262 { "TLSLD", { 0,
6263 BFD_RELOC_X86_64_TLSLD },
6264 OPERAND_TYPE_IMM32_32S_DISP32 },
6265 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32,
6266 BFD_RELOC_X86_64_GOTTPOFF },
6267 OPERAND_TYPE_IMM32_32S_DISP32 },
6268 { "TPOFF", { BFD_RELOC_386_TLS_LE_32,
6269 BFD_RELOC_X86_64_TPOFF32 },
6270 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
6271 { "NTPOFF", { BFD_RELOC_386_TLS_LE,
6272 0 },
6273 OPERAND_TYPE_NONE },
6274 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32,
6275 BFD_RELOC_X86_64_DTPOFF32 },
6277 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
6278 { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE,
6279 0 },
6280 OPERAND_TYPE_NONE },
6281 { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE,
6282 0 },
6283 OPERAND_TYPE_NONE },
6284 { "GOT", { BFD_RELOC_386_GOT32,
6285 BFD_RELOC_X86_64_GOT32 },
6286 OPERAND_TYPE_IMM32_32S_64_DISP32 },
6287 { "TLSDESC", { BFD_RELOC_386_TLS_GOTDESC,
6288 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
6289 OPERAND_TYPE_IMM32_32S_DISP32 },
6290 { "TLSCALL", { BFD_RELOC_386_TLS_DESC_CALL,
6291 BFD_RELOC_X86_64_TLSDESC_CALL },
6292 OPERAND_TYPE_IMM32_32S_DISP32 },
6294 char *cp;
6295 unsigned int j;
6297 if (!IS_ELF)
6298 return NULL;
6300 for (cp = input_line_pointer; *cp != '@'; cp++)
6301 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
6302 return NULL;
6304 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
6306 int len;
6308 len = strlen (gotrel[j].str);
6309 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
6311 if (gotrel[j].rel[object_64bit] != 0)
6313 int first, second;
6314 char *tmpbuf, *past_reloc;
6316 *reloc = gotrel[j].rel[object_64bit];
6317 if (adjust)
6318 *adjust = len;
6320 if (types)
6322 if (flag_code != CODE_64BIT)
6324 types->bitfield.imm32 = 1;
6325 types->bitfield.disp32 = 1;
6327 else
6328 *types = gotrel[j].types64;
6331 if (GOT_symbol == NULL)
6332 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
6334 /* The length of the first part of our input line. */
6335 first = cp - input_line_pointer;
6337 /* The second part goes from after the reloc token until
6338 (and including) an end_of_line char or comma. */
6339 past_reloc = cp + 1 + len;
6340 cp = past_reloc;
6341 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
6342 ++cp;
6343 second = cp + 1 - past_reloc;
6345 /* Allocate and copy string. The trailing NUL shouldn't
6346 be necessary, but be safe. */
6347 tmpbuf = xmalloc (first + second + 2);
6348 memcpy (tmpbuf, input_line_pointer, first);
6349 if (second != 0 && *past_reloc != ' ')
6350 /* Replace the relocation token with ' ', so that
6351 errors like foo@GOTOFF1 will be detected. */
6352 tmpbuf[first++] = ' ';
6353 memcpy (tmpbuf + first, past_reloc, second);
6354 tmpbuf[first + second] = '\0';
6355 return tmpbuf;
6358 as_bad (_("@%s reloc is not supported with %d-bit output format"),
6359 gotrel[j].str, 1 << (5 + object_64bit));
6360 return NULL;
6364 /* Might be a symbol version string. Don't as_bad here. */
6365 return NULL;
6368 void
6369 x86_cons (expressionS *exp, int size)
6371 if (size == 4 || (object_64bit && size == 8))
6373 /* Handle @GOTOFF and the like in an expression. */
6374 char *save;
6375 char *gotfree_input_line;
6376 int adjust;
6378 save = input_line_pointer;
6379 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
6380 if (gotfree_input_line)
6381 input_line_pointer = gotfree_input_line;
6383 expression (exp);
6385 if (gotfree_input_line)
6387 /* expression () has merrily parsed up to the end of line,
6388 or a comma - in the wrong buffer. Transfer how far
6389 input_line_pointer has moved to the right buffer. */
6390 input_line_pointer = (save
6391 + (input_line_pointer - gotfree_input_line)
6392 + adjust);
6393 free (gotfree_input_line);
6394 if (exp->X_op == O_constant
6395 || exp->X_op == O_absent
6396 || exp->X_op == O_illegal
6397 || exp->X_op == O_register
6398 || exp->X_op == O_big)
6400 char c = *input_line_pointer;
6401 *input_line_pointer = 0;
6402 as_bad (_("missing or invalid expression `%s'"), save);
6403 *input_line_pointer = c;
6407 else
6408 expression (exp);
6410 #endif
6412 static void signed_cons (int size)
6414 if (flag_code == CODE_64BIT)
6415 cons_sign = 1;
6416 cons (size);
6417 cons_sign = -1;
6420 #ifdef TE_PE
6421 static void
6422 pe_directive_secrel (dummy)
6423 int dummy ATTRIBUTE_UNUSED;
6425 expressionS exp;
6429 expression (&exp);
6430 if (exp.X_op == O_symbol)
6431 exp.X_op = O_secrel;
6433 emit_expr (&exp, 4);
6435 while (*input_line_pointer++ == ',');
6437 input_line_pointer--;
6438 demand_empty_rest_of_line ();
6440 #endif
6442 static int
6443 i386_immediate (char *imm_start)
6445 char *save_input_line_pointer;
6446 char *gotfree_input_line;
6447 segT exp_seg = 0;
6448 expressionS *exp;
6449 i386_operand_type types;
6451 operand_type_set (&types, ~0);
6453 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
6455 as_bad (_("at most %d immediate operands are allowed"),
6456 MAX_IMMEDIATE_OPERANDS);
6457 return 0;
6460 exp = &im_expressions[i.imm_operands++];
6461 i.op[this_operand].imms = exp;
6463 if (is_space_char (*imm_start))
6464 ++imm_start;
6466 save_input_line_pointer = input_line_pointer;
6467 input_line_pointer = imm_start;
6469 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
6470 if (gotfree_input_line)
6471 input_line_pointer = gotfree_input_line;
6473 exp_seg = expression (exp);
6475 SKIP_WHITESPACE ();
6476 if (*input_line_pointer)
6477 as_bad (_("junk `%s' after expression"), input_line_pointer);
6479 input_line_pointer = save_input_line_pointer;
6480 if (gotfree_input_line)
6481 free (gotfree_input_line);
6483 if (exp->X_op == O_absent
6484 || exp->X_op == O_illegal
6485 || exp->X_op == O_big
6486 || (gotfree_input_line
6487 && (exp->X_op == O_constant
6488 || exp->X_op == O_register)))
6490 as_bad (_("missing or invalid immediate expression `%s'"),
6491 imm_start);
6492 return 0;
6494 else if (exp->X_op == O_constant)
6496 /* Size it properly later. */
6497 i.types[this_operand].bitfield.imm64 = 1;
6498 /* If BFD64, sign extend val. */
6499 if (!use_rela_relocations
6500 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
6501 exp->X_add_number
6502 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
6504 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6505 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
6506 && exp_seg != absolute_section
6507 && exp_seg != text_section
6508 && exp_seg != data_section
6509 && exp_seg != bss_section
6510 && exp_seg != undefined_section
6511 && !bfd_is_com_section (exp_seg))
6513 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
6514 return 0;
6516 #endif
6517 else if (!intel_syntax && exp->X_op == O_register)
6519 as_bad (_("illegal immediate register operand %s"), imm_start);
6520 return 0;
6522 else
6524 /* This is an address. The size of the address will be
6525 determined later, depending on destination register,
6526 suffix, or the default for the section. */
6527 i.types[this_operand].bitfield.imm8 = 1;
6528 i.types[this_operand].bitfield.imm16 = 1;
6529 i.types[this_operand].bitfield.imm32 = 1;
6530 i.types[this_operand].bitfield.imm32s = 1;
6531 i.types[this_operand].bitfield.imm64 = 1;
6532 i.types[this_operand] = operand_type_and (i.types[this_operand],
6533 types);
6536 return 1;
6539 static char *
6540 i386_scale (char *scale)
6542 offsetT val;
6543 char *save = input_line_pointer;
6545 input_line_pointer = scale;
6546 val = get_absolute_expression ();
6548 switch (val)
6550 case 1:
6551 i.log2_scale_factor = 0;
6552 break;
6553 case 2:
6554 i.log2_scale_factor = 1;
6555 break;
6556 case 4:
6557 i.log2_scale_factor = 2;
6558 break;
6559 case 8:
6560 i.log2_scale_factor = 3;
6561 break;
6562 default:
6564 char sep = *input_line_pointer;
6566 *input_line_pointer = '\0';
6567 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
6568 scale);
6569 *input_line_pointer = sep;
6570 input_line_pointer = save;
6571 return NULL;
6574 if (i.log2_scale_factor != 0 && i.index_reg == 0)
6576 as_warn (_("scale factor of %d without an index register"),
6577 1 << i.log2_scale_factor);
6578 i.log2_scale_factor = 0;
6580 scale = input_line_pointer;
6581 input_line_pointer = save;
6582 return scale;
6585 static int
6586 i386_displacement (char *disp_start, char *disp_end)
6588 expressionS *exp;
6589 segT exp_seg = 0;
6590 char *save_input_line_pointer;
6591 char *gotfree_input_line;
6592 int override;
6593 i386_operand_type bigdisp, types = anydisp;
6594 int ret;
6596 if (i.disp_operands == MAX_MEMORY_OPERANDS)
6598 as_bad (_("at most %d displacement operands are allowed"),
6599 MAX_MEMORY_OPERANDS);
6600 return 0;
6603 operand_type_set (&bigdisp, 0);
6604 if ((i.types[this_operand].bitfield.jumpabsolute)
6605 || (!current_templates->start->opcode_modifier.jump
6606 && !current_templates->start->opcode_modifier.jumpdword))
6608 bigdisp.bitfield.disp32 = 1;
6609 override = (i.prefix[ADDR_PREFIX] != 0);
6610 if (flag_code == CODE_64BIT)
6612 if (!override)
6614 bigdisp.bitfield.disp32s = 1;
6615 bigdisp.bitfield.disp64 = 1;
6618 else if ((flag_code == CODE_16BIT) ^ override)
6620 bigdisp.bitfield.disp32 = 0;
6621 bigdisp.bitfield.disp16 = 1;
6624 else
6626 /* For PC-relative branches, the width of the displacement
6627 is dependent upon data size, not address size. */
6628 override = (i.prefix[DATA_PREFIX] != 0);
6629 if (flag_code == CODE_64BIT)
6631 if (override || i.suffix == WORD_MNEM_SUFFIX)
6632 bigdisp.bitfield.disp16 = 1;
6633 else
6635 bigdisp.bitfield.disp32 = 1;
6636 bigdisp.bitfield.disp32s = 1;
6639 else
6641 if (!override)
6642 override = (i.suffix == (flag_code != CODE_16BIT
6643 ? WORD_MNEM_SUFFIX
6644 : LONG_MNEM_SUFFIX));
6645 bigdisp.bitfield.disp32 = 1;
6646 if ((flag_code == CODE_16BIT) ^ override)
6648 bigdisp.bitfield.disp32 = 0;
6649 bigdisp.bitfield.disp16 = 1;
6653 i.types[this_operand] = operand_type_or (i.types[this_operand],
6654 bigdisp);
6656 exp = &disp_expressions[i.disp_operands];
6657 i.op[this_operand].disps = exp;
6658 i.disp_operands++;
6659 save_input_line_pointer = input_line_pointer;
6660 input_line_pointer = disp_start;
6661 END_STRING_AND_SAVE (disp_end);
6663 #ifndef GCC_ASM_O_HACK
6664 #define GCC_ASM_O_HACK 0
6665 #endif
6666 #if GCC_ASM_O_HACK
6667 END_STRING_AND_SAVE (disp_end + 1);
6668 if (i.types[this_operand].bitfield.baseIndex
6669 && displacement_string_end[-1] == '+')
6671 /* This hack is to avoid a warning when using the "o"
6672 constraint within gcc asm statements.
6673 For instance:
6675 #define _set_tssldt_desc(n,addr,limit,type) \
6676 __asm__ __volatile__ ( \
6677 "movw %w2,%0\n\t" \
6678 "movw %w1,2+%0\n\t" \
6679 "rorl $16,%1\n\t" \
6680 "movb %b1,4+%0\n\t" \
6681 "movb %4,5+%0\n\t" \
6682 "movb $0,6+%0\n\t" \
6683 "movb %h1,7+%0\n\t" \
6684 "rorl $16,%1" \
6685 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
6687 This works great except that the output assembler ends
6688 up looking a bit weird if it turns out that there is
6689 no offset. You end up producing code that looks like:
6691 #APP
6692 movw $235,(%eax)
6693 movw %dx,2+(%eax)
6694 rorl $16,%edx
6695 movb %dl,4+(%eax)
6696 movb $137,5+(%eax)
6697 movb $0,6+(%eax)
6698 movb %dh,7+(%eax)
6699 rorl $16,%edx
6700 #NO_APP
6702 So here we provide the missing zero. */
6704 *displacement_string_end = '0';
6706 #endif
6707 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
6708 if (gotfree_input_line)
6709 input_line_pointer = gotfree_input_line;
6711 exp_seg = expression (exp);
6713 SKIP_WHITESPACE ();
6714 if (*input_line_pointer)
6715 as_bad (_("junk `%s' after expression"), input_line_pointer);
6716 #if GCC_ASM_O_HACK
6717 RESTORE_END_STRING (disp_end + 1);
6718 #endif
6719 input_line_pointer = save_input_line_pointer;
6720 if (gotfree_input_line)
6721 free (gotfree_input_line);
6722 ret = 1;
6724 /* We do this to make sure that the section symbol is in
6725 the symbol table. We will ultimately change the relocation
6726 to be relative to the beginning of the section. */
6727 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
6728 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
6729 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
6731 if (exp->X_op != O_symbol)
6732 goto inv_disp;
6734 if (S_IS_LOCAL (exp->X_add_symbol)
6735 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
6736 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
6737 exp->X_op = O_subtract;
6738 exp->X_op_symbol = GOT_symbol;
6739 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
6740 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
6741 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
6742 i.reloc[this_operand] = BFD_RELOC_64;
6743 else
6744 i.reloc[this_operand] = BFD_RELOC_32;
6747 else if (exp->X_op == O_absent
6748 || exp->X_op == O_illegal
6749 || exp->X_op == O_big
6750 || (gotfree_input_line
6751 && (exp->X_op == O_constant
6752 || exp->X_op == O_register)))
6754 inv_disp:
6755 as_bad (_("missing or invalid displacement expression `%s'"),
6756 disp_start);
6757 ret = 0;
6760 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6761 else if (exp->X_op != O_constant
6762 && OUTPUT_FLAVOR == bfd_target_aout_flavour
6763 && exp_seg != absolute_section
6764 && exp_seg != text_section
6765 && exp_seg != data_section
6766 && exp_seg != bss_section
6767 && exp_seg != undefined_section
6768 && !bfd_is_com_section (exp_seg))
6770 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
6771 ret = 0;
6773 #endif
6775 RESTORE_END_STRING (disp_end);
6777 /* Check if this is a displacement only operand. */
6778 bigdisp = i.types[this_operand];
6779 bigdisp.bitfield.disp8 = 0;
6780 bigdisp.bitfield.disp16 = 0;
6781 bigdisp.bitfield.disp32 = 0;
6782 bigdisp.bitfield.disp32s = 0;
6783 bigdisp.bitfield.disp64 = 0;
6784 if (operand_type_all_zero (&bigdisp))
6785 i.types[this_operand] = operand_type_and (i.types[this_operand],
6786 types);
6788 return ret;
6791 /* Make sure the memory operand we've been dealt is valid.
6792 Return 1 on success, 0 on a failure. */
6794 static int
6795 i386_index_check (const char *operand_string)
6797 int ok;
6798 #if INFER_ADDR_PREFIX
6799 int fudged = 0;
6801 tryprefix:
6802 #endif
6803 ok = 1;
6804 if (flag_code == CODE_64BIT)
6806 if ((i.base_reg
6807 && ((i.prefix[ADDR_PREFIX] == 0
6808 && !i.base_reg->reg_type.bitfield.reg64)
6809 || (i.prefix[ADDR_PREFIX]
6810 && !i.base_reg->reg_type.bitfield.reg32))
6811 && (i.index_reg
6812 || i.base_reg->reg_num !=
6813 (i.prefix[ADDR_PREFIX] == 0 ? RegRip : RegEip)))
6814 || (i.index_reg
6815 && (!i.index_reg->reg_type.bitfield.baseindex
6816 || (i.prefix[ADDR_PREFIX] == 0
6817 && i.index_reg->reg_num != RegRiz
6818 && !i.index_reg->reg_type.bitfield.reg64
6820 || (i.prefix[ADDR_PREFIX]
6821 && i.index_reg->reg_num != RegEiz
6822 && !i.index_reg->reg_type.bitfield.reg32))))
6823 ok = 0;
6825 else
6827 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
6829 /* 16bit checks. */
6830 if ((i.base_reg
6831 && (!i.base_reg->reg_type.bitfield.reg16
6832 || !i.base_reg->reg_type.bitfield.baseindex))
6833 || (i.index_reg
6834 && (!i.index_reg->reg_type.bitfield.reg16
6835 || !i.index_reg->reg_type.bitfield.baseindex
6836 || !(i.base_reg
6837 && i.base_reg->reg_num < 6
6838 && i.index_reg->reg_num >= 6
6839 && i.log2_scale_factor == 0))))
6840 ok = 0;
6842 else
6844 /* 32bit checks. */
6845 if ((i.base_reg
6846 && !i.base_reg->reg_type.bitfield.reg32)
6847 || (i.index_reg
6848 && ((!i.index_reg->reg_type.bitfield.reg32
6849 && i.index_reg->reg_num != RegEiz)
6850 || !i.index_reg->reg_type.bitfield.baseindex)))
6851 ok = 0;
6854 if (!ok)
6856 #if INFER_ADDR_PREFIX
6857 if (i.prefix[ADDR_PREFIX] == 0)
6859 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
6860 i.prefixes += 1;
6861 /* Change the size of any displacement too. At most one of
6862 Disp16 or Disp32 is set.
6863 FIXME. There doesn't seem to be any real need for separate
6864 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
6865 Removing them would probably clean up the code quite a lot. */
6866 if (flag_code != CODE_64BIT
6867 && (i.types[this_operand].bitfield.disp16
6868 || i.types[this_operand].bitfield.disp32))
6869 i.types[this_operand]
6870 = operand_type_xor (i.types[this_operand], disp16_32);
6871 fudged = 1;
6872 goto tryprefix;
6874 if (fudged)
6875 as_bad (_("`%s' is not a valid base/index expression"),
6876 operand_string);
6877 else
6878 #endif
6879 as_bad (_("`%s' is not a valid %s bit base/index expression"),
6880 operand_string,
6881 flag_code_names[flag_code]);
6883 return ok;
6886 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
6887 on error. */
6889 static int
6890 i386_att_operand (char *operand_string)
6892 const reg_entry *r;
6893 char *end_op;
6894 char *op_string = operand_string;
6896 if (is_space_char (*op_string))
6897 ++op_string;
6899 /* We check for an absolute prefix (differentiating,
6900 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
6901 if (*op_string == ABSOLUTE_PREFIX)
6903 ++op_string;
6904 if (is_space_char (*op_string))
6905 ++op_string;
6906 i.types[this_operand].bitfield.jumpabsolute = 1;
6909 /* Check if operand is a register. */
6910 if ((r = parse_register (op_string, &end_op)) != NULL)
6912 i386_operand_type temp;
6914 /* Check for a segment override by searching for ':' after a
6915 segment register. */
6916 op_string = end_op;
6917 if (is_space_char (*op_string))
6918 ++op_string;
6919 if (*op_string == ':'
6920 && (r->reg_type.bitfield.sreg2
6921 || r->reg_type.bitfield.sreg3))
6923 switch (r->reg_num)
6925 case 0:
6926 i.seg[i.mem_operands] = &es;
6927 break;
6928 case 1:
6929 i.seg[i.mem_operands] = &cs;
6930 break;
6931 case 2:
6932 i.seg[i.mem_operands] = &ss;
6933 break;
6934 case 3:
6935 i.seg[i.mem_operands] = &ds;
6936 break;
6937 case 4:
6938 i.seg[i.mem_operands] = &fs;
6939 break;
6940 case 5:
6941 i.seg[i.mem_operands] = &gs;
6942 break;
6945 /* Skip the ':' and whitespace. */
6946 ++op_string;
6947 if (is_space_char (*op_string))
6948 ++op_string;
6950 if (!is_digit_char (*op_string)
6951 && !is_identifier_char (*op_string)
6952 && *op_string != '('
6953 && *op_string != ABSOLUTE_PREFIX)
6955 as_bad (_("bad memory operand `%s'"), op_string);
6956 return 0;
6958 /* Handle case of %es:*foo. */
6959 if (*op_string == ABSOLUTE_PREFIX)
6961 ++op_string;
6962 if (is_space_char (*op_string))
6963 ++op_string;
6964 i.types[this_operand].bitfield.jumpabsolute = 1;
6966 goto do_memory_reference;
6968 if (*op_string)
6970 as_bad (_("junk `%s' after register"), op_string);
6971 return 0;
6973 temp = r->reg_type;
6974 temp.bitfield.baseindex = 0;
6975 i.types[this_operand] = operand_type_or (i.types[this_operand],
6976 temp);
6977 i.types[this_operand].bitfield.unspecified = 0;
6978 i.op[this_operand].regs = r;
6979 i.reg_operands++;
6981 else if (*op_string == REGISTER_PREFIX)
6983 as_bad (_("bad register name `%s'"), op_string);
6984 return 0;
6986 else if (*op_string == IMMEDIATE_PREFIX)
6988 ++op_string;
6989 if (i.types[this_operand].bitfield.jumpabsolute)
6991 as_bad (_("immediate operand illegal with absolute jump"));
6992 return 0;
6994 if (!i386_immediate (op_string))
6995 return 0;
6997 else if (is_digit_char (*op_string)
6998 || is_identifier_char (*op_string)
6999 || *op_string == '(')
7001 /* This is a memory reference of some sort. */
7002 char *base_string;
7004 /* Start and end of displacement string expression (if found). */
7005 char *displacement_string_start;
7006 char *displacement_string_end;
7008 do_memory_reference:
7009 if ((i.mem_operands == 1
7010 && !current_templates->start->opcode_modifier.isstring)
7011 || i.mem_operands == 2)
7013 as_bad (_("too many memory references for `%s'"),
7014 current_templates->start->name);
7015 return 0;
7018 /* Check for base index form. We detect the base index form by
7019 looking for an ')' at the end of the operand, searching
7020 for the '(' matching it, and finding a REGISTER_PREFIX or ','
7021 after the '('. */
7022 base_string = op_string + strlen (op_string);
7024 --base_string;
7025 if (is_space_char (*base_string))
7026 --base_string;
7028 /* If we only have a displacement, set-up for it to be parsed later. */
7029 displacement_string_start = op_string;
7030 displacement_string_end = base_string + 1;
7032 if (*base_string == ')')
7034 char *temp_string;
7035 unsigned int parens_balanced = 1;
7036 /* We've already checked that the number of left & right ()'s are
7037 equal, so this loop will not be infinite. */
7040 base_string--;
7041 if (*base_string == ')')
7042 parens_balanced++;
7043 if (*base_string == '(')
7044 parens_balanced--;
7046 while (parens_balanced);
7048 temp_string = base_string;
7050 /* Skip past '(' and whitespace. */
7051 ++base_string;
7052 if (is_space_char (*base_string))
7053 ++base_string;
7055 if (*base_string == ','
7056 || ((i.base_reg = parse_register (base_string, &end_op))
7057 != NULL))
7059 displacement_string_end = temp_string;
7061 i.types[this_operand].bitfield.baseindex = 1;
7063 if (i.base_reg)
7065 base_string = end_op;
7066 if (is_space_char (*base_string))
7067 ++base_string;
7070 /* There may be an index reg or scale factor here. */
7071 if (*base_string == ',')
7073 ++base_string;
7074 if (is_space_char (*base_string))
7075 ++base_string;
7077 if ((i.index_reg = parse_register (base_string, &end_op))
7078 != NULL)
7080 base_string = end_op;
7081 if (is_space_char (*base_string))
7082 ++base_string;
7083 if (*base_string == ',')
7085 ++base_string;
7086 if (is_space_char (*base_string))
7087 ++base_string;
7089 else if (*base_string != ')')
7091 as_bad (_("expecting `,' or `)' "
7092 "after index register in `%s'"),
7093 operand_string);
7094 return 0;
7097 else if (*base_string == REGISTER_PREFIX)
7099 as_bad (_("bad register name `%s'"), base_string);
7100 return 0;
7103 /* Check for scale factor. */
7104 if (*base_string != ')')
7106 char *end_scale = i386_scale (base_string);
7108 if (!end_scale)
7109 return 0;
7111 base_string = end_scale;
7112 if (is_space_char (*base_string))
7113 ++base_string;
7114 if (*base_string != ')')
7116 as_bad (_("expecting `)' "
7117 "after scale factor in `%s'"),
7118 operand_string);
7119 return 0;
7122 else if (!i.index_reg)
7124 as_bad (_("expecting index register or scale factor "
7125 "after `,'; got '%c'"),
7126 *base_string);
7127 return 0;
7130 else if (*base_string != ')')
7132 as_bad (_("expecting `,' or `)' "
7133 "after base register in `%s'"),
7134 operand_string);
7135 return 0;
7138 else if (*base_string == REGISTER_PREFIX)
7140 as_bad (_("bad register name `%s'"), base_string);
7141 return 0;
7145 /* If there's an expression beginning the operand, parse it,
7146 assuming displacement_string_start and
7147 displacement_string_end are meaningful. */
7148 if (displacement_string_start != displacement_string_end)
7150 if (!i386_displacement (displacement_string_start,
7151 displacement_string_end))
7152 return 0;
7155 /* Special case for (%dx) while doing input/output op. */
7156 if (i.base_reg
7157 && operand_type_equal (&i.base_reg->reg_type,
7158 &reg16_inoutportreg)
7159 && i.index_reg == 0
7160 && i.log2_scale_factor == 0
7161 && i.seg[i.mem_operands] == 0
7162 && !operand_type_check (i.types[this_operand], disp))
7164 i.types[this_operand] = inoutportreg;
7165 return 1;
7168 if (i386_index_check (operand_string) == 0)
7169 return 0;
7170 i.types[this_operand].bitfield.mem = 1;
7171 i.mem_operands++;
7173 else
7175 /* It's not a memory operand; argh! */
7176 as_bad (_("invalid char %s beginning operand %d `%s'"),
7177 output_invalid (*op_string),
7178 this_operand + 1,
7179 op_string);
7180 return 0;
7182 return 1; /* Normal return. */
7185 /* md_estimate_size_before_relax()
7187 Called just before relax() for rs_machine_dependent frags. The x86
7188 assembler uses these frags to handle variable size jump
7189 instructions.
7191 Any symbol that is now undefined will not become defined.
7192 Return the correct fr_subtype in the frag.
7193 Return the initial "guess for variable size of frag" to caller.
7194 The guess is actually the growth beyond the fixed part. Whatever
7195 we do to grow the fixed or variable part contributes to our
7196 returned value. */
7199 md_estimate_size_before_relax (fragP, segment)
7200 fragS *fragP;
7201 segT segment;
7203 /* We've already got fragP->fr_subtype right; all we have to do is
7204 check for un-relaxable symbols. On an ELF system, we can't relax
7205 an externally visible symbol, because it may be overridden by a
7206 shared library. */
7207 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
7208 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7209 || (IS_ELF
7210 && (S_IS_EXTERNAL (fragP->fr_symbol)
7211 || S_IS_WEAK (fragP->fr_symbol)))
7212 #endif
7215 /* Symbol is undefined in this segment, or we need to keep a
7216 reloc so that weak symbols can be overridden. */
7217 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
7218 enum bfd_reloc_code_real reloc_type;
7219 unsigned char *opcode;
7220 int old_fr_fix;
7222 if (fragP->fr_var != NO_RELOC)
7223 reloc_type = fragP->fr_var;
7224 else if (size == 2)
7225 reloc_type = BFD_RELOC_16_PCREL;
7226 else
7227 reloc_type = BFD_RELOC_32_PCREL;
7229 old_fr_fix = fragP->fr_fix;
7230 opcode = (unsigned char *) fragP->fr_opcode;
7232 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
7234 case UNCOND_JUMP:
7235 /* Make jmp (0xeb) a (d)word displacement jump. */
7236 opcode[0] = 0xe9;
7237 fragP->fr_fix += size;
7238 fix_new (fragP, old_fr_fix, size,
7239 fragP->fr_symbol,
7240 fragP->fr_offset, 1,
7241 reloc_type);
7242 break;
7244 case COND_JUMP86:
7245 if (size == 2
7246 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
7248 /* Negate the condition, and branch past an
7249 unconditional jump. */
7250 opcode[0] ^= 1;
7251 opcode[1] = 3;
7252 /* Insert an unconditional jump. */
7253 opcode[2] = 0xe9;
7254 /* We added two extra opcode bytes, and have a two byte
7255 offset. */
7256 fragP->fr_fix += 2 + 2;
7257 fix_new (fragP, old_fr_fix + 2, 2,
7258 fragP->fr_symbol,
7259 fragP->fr_offset, 1,
7260 reloc_type);
7261 break;
7263 /* Fall through. */
7265 case COND_JUMP:
7266 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
7268 fixS *fixP;
7270 fragP->fr_fix += 1;
7271 fixP = fix_new (fragP, old_fr_fix, 1,
7272 fragP->fr_symbol,
7273 fragP->fr_offset, 1,
7274 BFD_RELOC_8_PCREL);
7275 fixP->fx_signed = 1;
7276 break;
7279 /* This changes the byte-displacement jump 0x7N
7280 to the (d)word-displacement jump 0x0f,0x8N. */
7281 opcode[1] = opcode[0] + 0x10;
7282 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7283 /* We've added an opcode byte. */
7284 fragP->fr_fix += 1 + size;
7285 fix_new (fragP, old_fr_fix + 1, size,
7286 fragP->fr_symbol,
7287 fragP->fr_offset, 1,
7288 reloc_type);
7289 break;
7291 default:
7292 BAD_CASE (fragP->fr_subtype);
7293 break;
7295 frag_wane (fragP);
7296 return fragP->fr_fix - old_fr_fix;
7299 /* Guess size depending on current relax state. Initially the relax
7300 state will correspond to a short jump and we return 1, because
7301 the variable part of the frag (the branch offset) is one byte
7302 long. However, we can relax a section more than once and in that
7303 case we must either set fr_subtype back to the unrelaxed state,
7304 or return the value for the appropriate branch. */
7305 return md_relax_table[fragP->fr_subtype].rlx_length;
7308 /* Called after relax() is finished.
7310 In: Address of frag.
7311 fr_type == rs_machine_dependent.
7312 fr_subtype is what the address relaxed to.
7314 Out: Any fixSs and constants are set up.
7315 Caller will turn frag into a ".space 0". */
7317 void
7318 md_convert_frag (abfd, sec, fragP)
7319 bfd *abfd ATTRIBUTE_UNUSED;
7320 segT sec ATTRIBUTE_UNUSED;
7321 fragS *fragP;
7323 unsigned char *opcode;
7324 unsigned char *where_to_put_displacement = NULL;
7325 offsetT target_address;
7326 offsetT opcode_address;
7327 unsigned int extension = 0;
7328 offsetT displacement_from_opcode_start;
7330 opcode = (unsigned char *) fragP->fr_opcode;
7332 /* Address we want to reach in file space. */
7333 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
7335 /* Address opcode resides at in file space. */
7336 opcode_address = fragP->fr_address + fragP->fr_fix;
7338 /* Displacement from opcode start to fill into instruction. */
7339 displacement_from_opcode_start = target_address - opcode_address;
7341 if ((fragP->fr_subtype & BIG) == 0)
7343 /* Don't have to change opcode. */
7344 extension = 1; /* 1 opcode + 1 displacement */
7345 where_to_put_displacement = &opcode[1];
7347 else
7349 if (no_cond_jump_promotion
7350 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
7351 as_warn_where (fragP->fr_file, fragP->fr_line,
7352 _("long jump required"));
7354 switch (fragP->fr_subtype)
7356 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
7357 extension = 4; /* 1 opcode + 4 displacement */
7358 opcode[0] = 0xe9;
7359 where_to_put_displacement = &opcode[1];
7360 break;
7362 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
7363 extension = 2; /* 1 opcode + 2 displacement */
7364 opcode[0] = 0xe9;
7365 where_to_put_displacement = &opcode[1];
7366 break;
7368 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
7369 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
7370 extension = 5; /* 2 opcode + 4 displacement */
7371 opcode[1] = opcode[0] + 0x10;
7372 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7373 where_to_put_displacement = &opcode[2];
7374 break;
7376 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
7377 extension = 3; /* 2 opcode + 2 displacement */
7378 opcode[1] = opcode[0] + 0x10;
7379 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7380 where_to_put_displacement = &opcode[2];
7381 break;
7383 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
7384 extension = 4;
7385 opcode[0] ^= 1;
7386 opcode[1] = 3;
7387 opcode[2] = 0xe9;
7388 where_to_put_displacement = &opcode[3];
7389 break;
7391 default:
7392 BAD_CASE (fragP->fr_subtype);
7393 break;
7397 /* If size if less then four we are sure that the operand fits,
7398 but if it's 4, then it could be that the displacement is larger
7399 then -/+ 2GB. */
7400 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
7401 && object_64bit
7402 && ((addressT) (displacement_from_opcode_start - extension
7403 + ((addressT) 1 << 31))
7404 > (((addressT) 2 << 31) - 1)))
7406 as_bad_where (fragP->fr_file, fragP->fr_line,
7407 _("jump target out of range"));
7408 /* Make us emit 0. */
7409 displacement_from_opcode_start = extension;
7411 /* Now put displacement after opcode. */
7412 md_number_to_chars ((char *) where_to_put_displacement,
7413 (valueT) (displacement_from_opcode_start - extension),
7414 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
7415 fragP->fr_fix += extension;
7418 /* Apply a fixup (fixS) to segment data, once it has been determined
7419 by our caller that we have all the info we need to fix it up.
7421 On the 386, immediates, displacements, and data pointers are all in
7422 the same (little-endian) format, so we don't need to care about which
7423 we are handling. */
7425 void
7426 md_apply_fix (fixP, valP, seg)
7427 /* The fix we're to put in. */
7428 fixS *fixP;
7429 /* Pointer to the value of the bits. */
7430 valueT *valP;
7431 /* Segment fix is from. */
7432 segT seg ATTRIBUTE_UNUSED;
7434 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
7435 valueT value = *valP;
7437 #if !defined (TE_Mach)
7438 if (fixP->fx_pcrel)
7440 switch (fixP->fx_r_type)
7442 default:
7443 break;
7445 case BFD_RELOC_64:
7446 fixP->fx_r_type = BFD_RELOC_64_PCREL;
7447 break;
7448 case BFD_RELOC_32:
7449 case BFD_RELOC_X86_64_32S:
7450 fixP->fx_r_type = BFD_RELOC_32_PCREL;
7451 break;
7452 case BFD_RELOC_16:
7453 fixP->fx_r_type = BFD_RELOC_16_PCREL;
7454 break;
7455 case BFD_RELOC_8:
7456 fixP->fx_r_type = BFD_RELOC_8_PCREL;
7457 break;
7461 if (fixP->fx_addsy != NULL
7462 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
7463 || fixP->fx_r_type == BFD_RELOC_64_PCREL
7464 || fixP->fx_r_type == BFD_RELOC_16_PCREL
7465 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
7466 && !use_rela_relocations)
7468 /* This is a hack. There should be a better way to handle this.
7469 This covers for the fact that bfd_install_relocation will
7470 subtract the current location (for partial_inplace, PC relative
7471 relocations); see more below. */
7472 #ifndef OBJ_AOUT
7473 if (IS_ELF
7474 #ifdef TE_PE
7475 || OUTPUT_FLAVOR == bfd_target_coff_flavour
7476 #endif
7478 value += fixP->fx_where + fixP->fx_frag->fr_address;
7479 #endif
7480 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7481 if (IS_ELF)
7483 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
7485 if ((sym_seg == seg
7486 || (symbol_section_p (fixP->fx_addsy)
7487 && sym_seg != absolute_section))
7488 && !generic_force_reloc (fixP))
7490 /* Yes, we add the values in twice. This is because
7491 bfd_install_relocation subtracts them out again. I think
7492 bfd_install_relocation is broken, but I don't dare change
7493 it. FIXME. */
7494 value += fixP->fx_where + fixP->fx_frag->fr_address;
7497 #endif
7498 #if defined (OBJ_COFF) && defined (TE_PE)
7499 /* For some reason, the PE format does not store a
7500 section address offset for a PC relative symbol. */
7501 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7502 || S_IS_WEAK (fixP->fx_addsy))
7503 value += md_pcrel_from (fixP);
7504 #endif
7507 /* Fix a few things - the dynamic linker expects certain values here,
7508 and we must not disappoint it. */
7509 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7510 if (IS_ELF && fixP->fx_addsy)
7511 switch (fixP->fx_r_type)
7513 case BFD_RELOC_386_PLT32:
7514 case BFD_RELOC_X86_64_PLT32:
7515 /* Make the jump instruction point to the address of the operand. At
7516 runtime we merely add the offset to the actual PLT entry. */
7517 value = -4;
7518 break;
7520 case BFD_RELOC_386_TLS_GD:
7521 case BFD_RELOC_386_TLS_LDM:
7522 case BFD_RELOC_386_TLS_IE_32:
7523 case BFD_RELOC_386_TLS_IE:
7524 case BFD_RELOC_386_TLS_GOTIE:
7525 case BFD_RELOC_386_TLS_GOTDESC:
7526 case BFD_RELOC_X86_64_TLSGD:
7527 case BFD_RELOC_X86_64_TLSLD:
7528 case BFD_RELOC_X86_64_GOTTPOFF:
7529 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7530 value = 0; /* Fully resolved at runtime. No addend. */
7531 /* Fallthrough */
7532 case BFD_RELOC_386_TLS_LE:
7533 case BFD_RELOC_386_TLS_LDO_32:
7534 case BFD_RELOC_386_TLS_LE_32:
7535 case BFD_RELOC_X86_64_DTPOFF32:
7536 case BFD_RELOC_X86_64_DTPOFF64:
7537 case BFD_RELOC_X86_64_TPOFF32:
7538 case BFD_RELOC_X86_64_TPOFF64:
7539 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7540 break;
7542 case BFD_RELOC_386_TLS_DESC_CALL:
7543 case BFD_RELOC_X86_64_TLSDESC_CALL:
7544 value = 0; /* Fully resolved at runtime. No addend. */
7545 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7546 fixP->fx_done = 0;
7547 return;
7549 case BFD_RELOC_386_GOT32:
7550 case BFD_RELOC_X86_64_GOT32:
7551 value = 0; /* Fully resolved at runtime. No addend. */
7552 break;
7554 case BFD_RELOC_VTABLE_INHERIT:
7555 case BFD_RELOC_VTABLE_ENTRY:
7556 fixP->fx_done = 0;
7557 return;
7559 default:
7560 break;
7562 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
7563 *valP = value;
7564 #endif /* !defined (TE_Mach) */
7566 /* Are we finished with this relocation now? */
7567 if (fixP->fx_addsy == NULL)
7568 fixP->fx_done = 1;
7569 else if (use_rela_relocations)
7571 fixP->fx_no_overflow = 1;
7572 /* Remember value for tc_gen_reloc. */
7573 fixP->fx_addnumber = value;
7574 value = 0;
7577 md_number_to_chars (p, value, fixP->fx_size);
7580 char *
7581 md_atof (int type, char *litP, int *sizeP)
7583 /* This outputs the LITTLENUMs in REVERSE order;
7584 in accord with the bigendian 386. */
7585 return ieee_md_atof (type, litP, sizeP, FALSE);
7588 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
7590 static char *
7591 output_invalid (int c)
7593 if (ISPRINT (c))
7594 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
7595 "'%c'", c);
7596 else
7597 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
7598 "(0x%x)", (unsigned char) c);
7599 return output_invalid_buf;
7602 /* REG_STRING starts *before* REGISTER_PREFIX. */
7604 static const reg_entry *
7605 parse_real_register (char *reg_string, char **end_op)
7607 char *s = reg_string;
7608 char *p;
7609 char reg_name_given[MAX_REG_NAME_SIZE + 1];
7610 const reg_entry *r;
7612 /* Skip possible REGISTER_PREFIX and possible whitespace. */
7613 if (*s == REGISTER_PREFIX)
7614 ++s;
7616 if (is_space_char (*s))
7617 ++s;
7619 p = reg_name_given;
7620 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
7622 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
7623 return (const reg_entry *) NULL;
7624 s++;
7627 /* For naked regs, make sure that we are not dealing with an identifier.
7628 This prevents confusing an identifier like `eax_var' with register
7629 `eax'. */
7630 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
7631 return (const reg_entry *) NULL;
7633 *end_op = s;
7635 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
7637 /* Handle floating point regs, allowing spaces in the (i) part. */
7638 if (r == i386_regtab /* %st is first entry of table */)
7640 if (is_space_char (*s))
7641 ++s;
7642 if (*s == '(')
7644 ++s;
7645 if (is_space_char (*s))
7646 ++s;
7647 if (*s >= '0' && *s <= '7')
7649 int fpr = *s - '0';
7650 ++s;
7651 if (is_space_char (*s))
7652 ++s;
7653 if (*s == ')')
7655 *end_op = s + 1;
7656 r = hash_find (reg_hash, "st(0)");
7657 know (r);
7658 return r + fpr;
7661 /* We have "%st(" then garbage. */
7662 return (const reg_entry *) NULL;
7666 if (r == NULL || allow_pseudo_reg)
7667 return r;
7669 if (operand_type_all_zero (&r->reg_type))
7670 return (const reg_entry *) NULL;
7672 if ((r->reg_type.bitfield.reg32
7673 || r->reg_type.bitfield.sreg3
7674 || r->reg_type.bitfield.control
7675 || r->reg_type.bitfield.debug
7676 || r->reg_type.bitfield.test)
7677 && !cpu_arch_flags.bitfield.cpui386)
7678 return (const reg_entry *) NULL;
7680 if (r->reg_type.bitfield.regmmx && !cpu_arch_flags.bitfield.cpummx)
7681 return (const reg_entry *) NULL;
7683 if (r->reg_type.bitfield.regxmm && !cpu_arch_flags.bitfield.cpusse)
7684 return (const reg_entry *) NULL;
7686 /* Don't allow fake index register unless allow_index_reg isn't 0. */
7687 if (!allow_index_reg
7688 && (r->reg_num == RegEiz || r->reg_num == RegRiz))
7689 return (const reg_entry *) NULL;
7691 if (((r->reg_flags & (RegRex64 | RegRex))
7692 || r->reg_type.bitfield.reg64)
7693 && (!cpu_arch_flags.bitfield.cpulm
7694 || !operand_type_equal (&r->reg_type, &control))
7695 && flag_code != CODE_64BIT)
7696 return (const reg_entry *) NULL;
7698 if (r->reg_type.bitfield.sreg3 && r->reg_num == RegFlat && !intel_syntax)
7699 return (const reg_entry *) NULL;
7701 return r;
7704 /* REG_STRING starts *before* REGISTER_PREFIX. */
7706 static const reg_entry *
7707 parse_register (char *reg_string, char **end_op)
7709 const reg_entry *r;
7711 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
7712 r = parse_real_register (reg_string, end_op);
7713 else
7714 r = NULL;
7715 if (!r)
7717 char *save = input_line_pointer;
7718 char c;
7719 symbolS *symbolP;
7721 input_line_pointer = reg_string;
7722 c = get_symbol_end ();
7723 symbolP = symbol_find (reg_string);
7724 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
7726 const expressionS *e = symbol_get_value_expression (symbolP);
7728 know (e->X_op == O_register);
7729 know (e->X_add_number >= 0
7730 && (valueT) e->X_add_number < i386_regtab_size);
7731 r = i386_regtab + e->X_add_number;
7732 *end_op = input_line_pointer;
7734 *input_line_pointer = c;
7735 input_line_pointer = save;
7737 return r;
7741 i386_parse_name (char *name, expressionS *e, char *nextcharP)
7743 const reg_entry *r;
7744 char *end = input_line_pointer;
7746 *end = *nextcharP;
7747 r = parse_register (name, &input_line_pointer);
7748 if (r && end <= input_line_pointer)
7750 *nextcharP = *input_line_pointer;
7751 *input_line_pointer = 0;
7752 e->X_op = O_register;
7753 e->X_add_number = r - i386_regtab;
7754 return 1;
7756 input_line_pointer = end;
7757 *end = 0;
7758 return 0;
7761 void
7762 md_operand (expressionS *e)
7764 if (*input_line_pointer == REGISTER_PREFIX)
7766 char *end;
7767 const reg_entry *r = parse_real_register (input_line_pointer, &end);
7769 if (r)
7771 e->X_op = O_register;
7772 e->X_add_number = r - i386_regtab;
7773 input_line_pointer = end;
7779 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7780 const char *md_shortopts = "kVQ:sqn";
7781 #else
7782 const char *md_shortopts = "qn";
7783 #endif
7785 #define OPTION_32 (OPTION_MD_BASE + 0)
7786 #define OPTION_64 (OPTION_MD_BASE + 1)
7787 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
7788 #define OPTION_MARCH (OPTION_MD_BASE + 3)
7789 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
7790 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
7791 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
7792 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
7793 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
7794 #define OPTION_MOLD_GCC (OPTION_MD_BASE + 9)
7795 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
7797 struct option md_longopts[] =
7799 {"32", no_argument, NULL, OPTION_32},
7800 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
7801 {"64", no_argument, NULL, OPTION_64},
7802 #endif
7803 {"divide", no_argument, NULL, OPTION_DIVIDE},
7804 {"march", required_argument, NULL, OPTION_MARCH},
7805 {"mtune", required_argument, NULL, OPTION_MTUNE},
7806 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
7807 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
7808 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
7809 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
7810 {"mold-gcc", no_argument, NULL, OPTION_MOLD_GCC},
7811 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
7812 {NULL, no_argument, NULL, 0}
7814 size_t md_longopts_size = sizeof (md_longopts);
7817 md_parse_option (int c, char *arg)
7819 unsigned int i;
7820 char *arch, *next;
7822 switch (c)
7824 case 'n':
7825 optimize_align_code = 0;
7826 break;
7828 case 'q':
7829 quiet_warnings = 1;
7830 break;
7832 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7833 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
7834 should be emitted or not. FIXME: Not implemented. */
7835 case 'Q':
7836 break;
7838 /* -V: SVR4 argument to print version ID. */
7839 case 'V':
7840 print_version_id ();
7841 break;
7843 /* -k: Ignore for FreeBSD compatibility. */
7844 case 'k':
7845 break;
7847 case 's':
7848 /* -s: On i386 Solaris, this tells the native assembler to use
7849 .stab instead of .stab.excl. We always use .stab anyhow. */
7850 break;
7851 #endif
7852 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
7853 case OPTION_64:
7855 const char **list, **l;
7857 list = bfd_target_list ();
7858 for (l = list; *l != NULL; l++)
7859 if (CONST_STRNEQ (*l, "elf64-x86-64")
7860 || strcmp (*l, "coff-x86-64") == 0
7861 || strcmp (*l, "pe-x86-64") == 0
7862 || strcmp (*l, "pei-x86-64") == 0)
7864 default_arch = "x86_64";
7865 break;
7867 if (*l == NULL)
7868 as_fatal (_("No compiled in support for x86_64"));
7869 free (list);
7871 break;
7872 #endif
7874 case OPTION_32:
7875 default_arch = "i386";
7876 break;
7878 case OPTION_DIVIDE:
7879 #ifdef SVR4_COMMENT_CHARS
7881 char *n, *t;
7882 const char *s;
7884 n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
7885 t = n;
7886 for (s = i386_comment_chars; *s != '\0'; s++)
7887 if (*s != '/')
7888 *t++ = *s;
7889 *t = '\0';
7890 i386_comment_chars = n;
7892 #endif
7893 break;
7895 case OPTION_MARCH:
7896 arch = xstrdup (arg);
7899 if (*arch == '.')
7900 as_fatal (_("Invalid -march= option: `%s'"), arg);
7901 next = strchr (arch, '+');
7902 if (next)
7903 *next++ = '\0';
7904 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
7906 if (strcmp (arch, cpu_arch [i].name) == 0)
7908 /* Processor. */
7909 cpu_arch_name = cpu_arch[i].name;
7910 cpu_sub_arch_name = NULL;
7911 cpu_arch_flags = cpu_arch[i].flags;
7912 cpu_arch_isa = cpu_arch[i].type;
7913 cpu_arch_isa_flags = cpu_arch[i].flags;
7914 if (!cpu_arch_tune_set)
7916 cpu_arch_tune = cpu_arch_isa;
7917 cpu_arch_tune_flags = cpu_arch_isa_flags;
7919 break;
7921 else if (*cpu_arch [i].name == '.'
7922 && strcmp (arch, cpu_arch [i].name + 1) == 0)
7924 /* ISA entension. */
7925 i386_cpu_flags flags;
7926 flags = cpu_flags_or (cpu_arch_flags,
7927 cpu_arch[i].flags);
7928 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
7930 if (cpu_sub_arch_name)
7932 char *name = cpu_sub_arch_name;
7933 cpu_sub_arch_name = concat (name,
7934 cpu_arch[i].name,
7935 (const char *) NULL);
7936 free (name);
7938 else
7939 cpu_sub_arch_name = xstrdup (cpu_arch[i].name);
7940 cpu_arch_flags = flags;
7942 break;
7946 if (i >= ARRAY_SIZE (cpu_arch))
7947 as_fatal (_("Invalid -march= option: `%s'"), arg);
7949 arch = next;
7951 while (next != NULL );
7952 break;
7954 case OPTION_MTUNE:
7955 if (*arg == '.')
7956 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
7957 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
7959 if (strcmp (arg, cpu_arch [i].name) == 0)
7961 cpu_arch_tune_set = 1;
7962 cpu_arch_tune = cpu_arch [i].type;
7963 cpu_arch_tune_flags = cpu_arch[i].flags;
7964 break;
7967 if (i >= ARRAY_SIZE (cpu_arch))
7968 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
7969 break;
7971 case OPTION_MMNEMONIC:
7972 if (strcasecmp (arg, "att") == 0)
7973 intel_mnemonic = 0;
7974 else if (strcasecmp (arg, "intel") == 0)
7975 intel_mnemonic = 1;
7976 else
7977 as_fatal (_("Invalid -mmnemonic= option: `%s'"), arg);
7978 break;
7980 case OPTION_MSYNTAX:
7981 if (strcasecmp (arg, "att") == 0)
7982 intel_syntax = 0;
7983 else if (strcasecmp (arg, "intel") == 0)
7984 intel_syntax = 1;
7985 else
7986 as_fatal (_("Invalid -msyntax= option: `%s'"), arg);
7987 break;
7989 case OPTION_MINDEX_REG:
7990 allow_index_reg = 1;
7991 break;
7993 case OPTION_MNAKED_REG:
7994 allow_naked_reg = 1;
7995 break;
7997 case OPTION_MOLD_GCC:
7998 old_gcc = 1;
7999 break;
8001 case OPTION_MSSE2AVX:
8002 sse2avx = 1;
8003 break;
8005 default:
8006 return 0;
8008 return 1;
8011 void
8012 md_show_usage (stream)
8013 FILE *stream;
8015 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8016 fprintf (stream, _("\
8017 -Q ignored\n\
8018 -V print assembler version number\n\
8019 -k ignored\n"));
8020 #endif
8021 fprintf (stream, _("\
8022 -n Do not optimize code alignment\n\
8023 -q quieten some warnings\n"));
8024 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8025 fprintf (stream, _("\
8026 -s ignored\n"));
8027 #endif
8028 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
8029 fprintf (stream, _("\
8030 --32/--64 generate 32bit/64bit code\n"));
8031 #endif
8032 #ifdef SVR4_COMMENT_CHARS
8033 fprintf (stream, _("\
8034 --divide do not treat `/' as a comment character\n"));
8035 #else
8036 fprintf (stream, _("\
8037 --divide ignored\n"));
8038 #endif
8039 fprintf (stream, _("\
8040 -march=CPU[,+EXTENSION...]\n\
8041 generate code for CPU and EXTENSION, CPU is one of:\n\
8042 i8086, i186, i286, i386, i486, pentium, pentiumpro,\n\
8043 pentiumii, pentiumiii, pentium4, prescott, nocona,\n\
8044 core, core2, k6, k6_2, athlon, k8, amdfam10,\n\
8045 generic32, generic64\n\
8046 EXTENSION is combination of:\n\
8047 mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, sse4,\n\
8048 avx, vmx, smx, xsave, aes, clmul, fma, 3dnow,\n\
8049 3dnowa, sse4a, sse5, svme, abm, padlock\n"));
8050 fprintf (stream, _("\
8051 -mtune=CPU optimize for CPU, CPU is one of:\n\
8052 i8086, i186, i286, i386, i486, pentium, pentiumpro,\n\
8053 pentiumii, pentiumiii, pentium4, prescott, nocona,\n\
8054 core, core2, k6, k6_2, athlon, k8, amdfam10,\n\
8055 generic32, generic64\n"));
8056 fprintf (stream, _("\
8057 -msse2avx encode SSE instructions with VEX prefix\n"));
8058 fprintf (stream, _("\
8059 -mmnemonic=[att|intel] use AT&T/Intel mnemonic\n"));
8060 fprintf (stream, _("\
8061 -msyntax=[att|intel] use AT&T/Intel syntax\n"));
8062 fprintf (stream, _("\
8063 -mindex-reg support pseudo index registers\n"));
8064 fprintf (stream, _("\
8065 -mnaked-reg don't require `%%' prefix for registers\n"));
8066 fprintf (stream, _("\
8067 -mold-gcc support old (<= 2.8.1) versions of gcc\n"));
8070 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
8071 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (TE_PEP))
8073 /* Pick the target format to use. */
8075 const char *
8076 i386_target_format (void)
8078 if (!strcmp (default_arch, "x86_64"))
8080 set_code_flag (CODE_64BIT);
8081 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
8083 cpu_arch_isa_flags.bitfield.cpui186 = 1;
8084 cpu_arch_isa_flags.bitfield.cpui286 = 1;
8085 cpu_arch_isa_flags.bitfield.cpui386 = 1;
8086 cpu_arch_isa_flags.bitfield.cpui486 = 1;
8087 cpu_arch_isa_flags.bitfield.cpui586 = 1;
8088 cpu_arch_isa_flags.bitfield.cpui686 = 1;
8089 cpu_arch_isa_flags.bitfield.cpup4 = 1;
8090 cpu_arch_isa_flags.bitfield.cpummx= 1;
8091 cpu_arch_isa_flags.bitfield.cpusse = 1;
8092 cpu_arch_isa_flags.bitfield.cpusse2 = 1;
8094 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
8096 cpu_arch_tune_flags.bitfield.cpui186 = 1;
8097 cpu_arch_tune_flags.bitfield.cpui286 = 1;
8098 cpu_arch_tune_flags.bitfield.cpui386 = 1;
8099 cpu_arch_tune_flags.bitfield.cpui486 = 1;
8100 cpu_arch_tune_flags.bitfield.cpui586 = 1;
8101 cpu_arch_tune_flags.bitfield.cpui686 = 1;
8102 cpu_arch_tune_flags.bitfield.cpup4 = 1;
8103 cpu_arch_tune_flags.bitfield.cpummx= 1;
8104 cpu_arch_tune_flags.bitfield.cpusse = 1;
8105 cpu_arch_tune_flags.bitfield.cpusse2 = 1;
8108 else if (!strcmp (default_arch, "i386"))
8110 set_code_flag (CODE_32BIT);
8111 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
8113 cpu_arch_isa_flags.bitfield.cpui186 = 1;
8114 cpu_arch_isa_flags.bitfield.cpui286 = 1;
8115 cpu_arch_isa_flags.bitfield.cpui386 = 1;
8117 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
8119 cpu_arch_tune_flags.bitfield.cpui186 = 1;
8120 cpu_arch_tune_flags.bitfield.cpui286 = 1;
8121 cpu_arch_tune_flags.bitfield.cpui386 = 1;
8124 else
8125 as_fatal (_("Unknown architecture"));
8126 switch (OUTPUT_FLAVOR)
8128 #ifdef TE_PEP
8129 case bfd_target_coff_flavour:
8130 return flag_code == CODE_64BIT ? COFF_TARGET_FORMAT : "coff-i386";
8131 break;
8132 #endif
8133 #ifdef OBJ_MAYBE_AOUT
8134 case bfd_target_aout_flavour:
8135 return AOUT_TARGET_FORMAT;
8136 #endif
8137 #ifdef OBJ_MAYBE_COFF
8138 case bfd_target_coff_flavour:
8139 return "coff-i386";
8140 #endif
8141 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
8142 case bfd_target_elf_flavour:
8144 if (flag_code == CODE_64BIT)
8146 object_64bit = 1;
8147 use_rela_relocations = 1;
8149 return flag_code == CODE_64BIT ? ELF_TARGET_FORMAT64 : ELF_TARGET_FORMAT;
8151 #endif
8152 default:
8153 abort ();
8154 return NULL;
8158 #endif /* OBJ_MAYBE_ more than one */
8160 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
8161 void
8162 i386_elf_emit_arch_note (void)
8164 if (IS_ELF && cpu_arch_name != NULL)
8166 char *p;
8167 asection *seg = now_seg;
8168 subsegT subseg = now_subseg;
8169 Elf_Internal_Note i_note;
8170 Elf_External_Note e_note;
8171 asection *note_secp;
8172 int len;
8174 /* Create the .note section. */
8175 note_secp = subseg_new (".note", 0);
8176 bfd_set_section_flags (stdoutput,
8177 note_secp,
8178 SEC_HAS_CONTENTS | SEC_READONLY);
8180 /* Process the arch string. */
8181 len = strlen (cpu_arch_name);
8183 i_note.namesz = len + 1;
8184 i_note.descsz = 0;
8185 i_note.type = NT_ARCH;
8186 p = frag_more (sizeof (e_note.namesz));
8187 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
8188 p = frag_more (sizeof (e_note.descsz));
8189 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
8190 p = frag_more (sizeof (e_note.type));
8191 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
8192 p = frag_more (len + 1);
8193 strcpy (p, cpu_arch_name);
8195 frag_align (2, 0, 0);
8197 subseg_set (seg, subseg);
8200 #endif
8202 symbolS *
8203 md_undefined_symbol (name)
8204 char *name;
8206 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
8207 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
8208 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
8209 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
8211 if (!GOT_symbol)
8213 if (symbol_find (name))
8214 as_bad (_("GOT already in symbol table"));
8215 GOT_symbol = symbol_new (name, undefined_section,
8216 (valueT) 0, &zero_address_frag);
8218 return GOT_symbol;
8220 return 0;
8223 /* Round up a section size to the appropriate boundary. */
8225 valueT
8226 md_section_align (segment, size)
8227 segT segment ATTRIBUTE_UNUSED;
8228 valueT size;
8230 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
8231 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
8233 /* For a.out, force the section size to be aligned. If we don't do
8234 this, BFD will align it for us, but it will not write out the
8235 final bytes of the section. This may be a bug in BFD, but it is
8236 easier to fix it here since that is how the other a.out targets
8237 work. */
8238 int align;
8240 align = bfd_get_section_alignment (stdoutput, segment);
8241 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
8243 #endif
8245 return size;
8248 /* On the i386, PC-relative offsets are relative to the start of the
8249 next instruction. That is, the address of the offset, plus its
8250 size, since the offset is always the last part of the insn. */
8252 long
8253 md_pcrel_from (fixS *fixP)
8255 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
8258 #ifndef I386COFF
8260 static void
8261 s_bss (int ignore ATTRIBUTE_UNUSED)
8263 int temp;
8265 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8266 if (IS_ELF)
8267 obj_elf_section_change_hook ();
8268 #endif
8269 temp = get_absolute_expression ();
8270 subseg_set (bss_section, (subsegT) temp);
8271 demand_empty_rest_of_line ();
8274 #endif
8276 void
8277 i386_validate_fix (fixS *fixp)
8279 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
8281 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
8283 if (!object_64bit)
8284 abort ();
8285 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
8287 else
8289 if (!object_64bit)
8290 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
8291 else
8292 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
8294 fixp->fx_subsy = 0;
8298 arelent *
8299 tc_gen_reloc (section, fixp)
8300 asection *section ATTRIBUTE_UNUSED;
8301 fixS *fixp;
8303 arelent *rel;
8304 bfd_reloc_code_real_type code;
8306 switch (fixp->fx_r_type)
8308 case BFD_RELOC_X86_64_PLT32:
8309 case BFD_RELOC_X86_64_GOT32:
8310 case BFD_RELOC_X86_64_GOTPCREL:
8311 case BFD_RELOC_386_PLT32:
8312 case BFD_RELOC_386_GOT32:
8313 case BFD_RELOC_386_GOTOFF:
8314 case BFD_RELOC_386_GOTPC:
8315 case BFD_RELOC_386_TLS_GD:
8316 case BFD_RELOC_386_TLS_LDM:
8317 case BFD_RELOC_386_TLS_LDO_32:
8318 case BFD_RELOC_386_TLS_IE_32:
8319 case BFD_RELOC_386_TLS_IE:
8320 case BFD_RELOC_386_TLS_GOTIE:
8321 case BFD_RELOC_386_TLS_LE_32:
8322 case BFD_RELOC_386_TLS_LE:
8323 case BFD_RELOC_386_TLS_GOTDESC:
8324 case BFD_RELOC_386_TLS_DESC_CALL:
8325 case BFD_RELOC_X86_64_TLSGD:
8326 case BFD_RELOC_X86_64_TLSLD:
8327 case BFD_RELOC_X86_64_DTPOFF32:
8328 case BFD_RELOC_X86_64_DTPOFF64:
8329 case BFD_RELOC_X86_64_GOTTPOFF:
8330 case BFD_RELOC_X86_64_TPOFF32:
8331 case BFD_RELOC_X86_64_TPOFF64:
8332 case BFD_RELOC_X86_64_GOTOFF64:
8333 case BFD_RELOC_X86_64_GOTPC32:
8334 case BFD_RELOC_X86_64_GOT64:
8335 case BFD_RELOC_X86_64_GOTPCREL64:
8336 case BFD_RELOC_X86_64_GOTPC64:
8337 case BFD_RELOC_X86_64_GOTPLT64:
8338 case BFD_RELOC_X86_64_PLTOFF64:
8339 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
8340 case BFD_RELOC_X86_64_TLSDESC_CALL:
8341 case BFD_RELOC_RVA:
8342 case BFD_RELOC_VTABLE_ENTRY:
8343 case BFD_RELOC_VTABLE_INHERIT:
8344 #ifdef TE_PE
8345 case BFD_RELOC_32_SECREL:
8346 #endif
8347 code = fixp->fx_r_type;
8348 break;
8349 case BFD_RELOC_X86_64_32S:
8350 if (!fixp->fx_pcrel)
8352 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
8353 code = fixp->fx_r_type;
8354 break;
8356 default:
8357 if (fixp->fx_pcrel)
8359 switch (fixp->fx_size)
8361 default:
8362 as_bad_where (fixp->fx_file, fixp->fx_line,
8363 _("can not do %d byte pc-relative relocation"),
8364 fixp->fx_size);
8365 code = BFD_RELOC_32_PCREL;
8366 break;
8367 case 1: code = BFD_RELOC_8_PCREL; break;
8368 case 2: code = BFD_RELOC_16_PCREL; break;
8369 case 4: code = BFD_RELOC_32_PCREL; break;
8370 #ifdef BFD64
8371 case 8: code = BFD_RELOC_64_PCREL; break;
8372 #endif
8375 else
8377 switch (fixp->fx_size)
8379 default:
8380 as_bad_where (fixp->fx_file, fixp->fx_line,
8381 _("can not do %d byte relocation"),
8382 fixp->fx_size);
8383 code = BFD_RELOC_32;
8384 break;
8385 case 1: code = BFD_RELOC_8; break;
8386 case 2: code = BFD_RELOC_16; break;
8387 case 4: code = BFD_RELOC_32; break;
8388 #ifdef BFD64
8389 case 8: code = BFD_RELOC_64; break;
8390 #endif
8393 break;
8396 if ((code == BFD_RELOC_32
8397 || code == BFD_RELOC_32_PCREL
8398 || code == BFD_RELOC_X86_64_32S)
8399 && GOT_symbol
8400 && fixp->fx_addsy == GOT_symbol)
8402 if (!object_64bit)
8403 code = BFD_RELOC_386_GOTPC;
8404 else
8405 code = BFD_RELOC_X86_64_GOTPC32;
8407 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
8408 && GOT_symbol
8409 && fixp->fx_addsy == GOT_symbol)
8411 code = BFD_RELOC_X86_64_GOTPC64;
8414 rel = (arelent *) xmalloc (sizeof (arelent));
8415 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
8416 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
8418 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
8420 if (!use_rela_relocations)
8422 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
8423 vtable entry to be used in the relocation's section offset. */
8424 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
8425 rel->address = fixp->fx_offset;
8427 rel->addend = 0;
8429 /* Use the rela in 64bit mode. */
8430 else
8432 if (!fixp->fx_pcrel)
8433 rel->addend = fixp->fx_offset;
8434 else
8435 switch (code)
8437 case BFD_RELOC_X86_64_PLT32:
8438 case BFD_RELOC_X86_64_GOT32:
8439 case BFD_RELOC_X86_64_GOTPCREL:
8440 case BFD_RELOC_X86_64_TLSGD:
8441 case BFD_RELOC_X86_64_TLSLD:
8442 case BFD_RELOC_X86_64_GOTTPOFF:
8443 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
8444 case BFD_RELOC_X86_64_TLSDESC_CALL:
8445 rel->addend = fixp->fx_offset - fixp->fx_size;
8446 break;
8447 default:
8448 rel->addend = (section->vma
8449 - fixp->fx_size
8450 + fixp->fx_addnumber
8451 + md_pcrel_from (fixp));
8452 break;
8456 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
8457 if (rel->howto == NULL)
8459 as_bad_where (fixp->fx_file, fixp->fx_line,
8460 _("cannot represent relocation type %s"),
8461 bfd_get_reloc_code_name (code));
8462 /* Set howto to a garbage value so that we can keep going. */
8463 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
8464 assert (rel->howto != NULL);
8467 return rel;
8471 /* Parse operands using Intel syntax. This implements a recursive descent
8472 parser based on the BNF grammar published in Appendix B of the MASM 6.1
8473 Programmer's Guide.
8475 FIXME: We do not recognize the full operand grammar defined in the MASM
8476 documentation. In particular, all the structure/union and
8477 high-level macro operands are missing.
8479 Uppercase words are terminals, lower case words are non-terminals.
8480 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
8481 bars '|' denote choices. Most grammar productions are implemented in
8482 functions called 'intel_<production>'.
8484 Initial production is 'expr'.
8486 addOp + | -
8488 alpha [a-zA-Z]
8490 binOp & | AND | \| | OR | ^ | XOR
8492 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
8494 constant digits [[ radixOverride ]]
8496 dataType BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD | YMMWORD
8498 digits decdigit
8499 | digits decdigit
8500 | digits hexdigit
8502 decdigit [0-9]
8504 e04 e04 addOp e05
8505 | e05
8507 e05 e05 binOp e06
8508 | e06
8510 e06 e06 mulOp e09
8511 | e09
8513 e09 OFFSET e10
8514 | SHORT e10
8515 | + e10
8516 | - e10
8517 | ~ e10
8518 | NOT e10
8519 | e09 PTR e10
8520 | e09 : e10
8521 | e10
8523 e10 e10 [ expr ]
8524 | e11
8526 e11 ( expr )
8527 | [ expr ]
8528 | constant
8529 | dataType
8530 | id
8532 | register
8534 => expr expr cmpOp e04
8535 | e04
8537 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
8538 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
8540 hexdigit a | b | c | d | e | f
8541 | A | B | C | D | E | F
8543 id alpha
8544 | id alpha
8545 | id decdigit
8547 mulOp * | / | % | MOD | << | SHL | >> | SHR
8549 quote " | '
8551 register specialRegister
8552 | gpRegister
8553 | byteRegister
8555 segmentRegister CS | DS | ES | FS | GS | SS
8557 specialRegister CR0 | CR2 | CR3 | CR4
8558 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
8559 | TR3 | TR4 | TR5 | TR6 | TR7
8561 We simplify the grammar in obvious places (e.g., register parsing is
8562 done by calling parse_register) and eliminate immediate left recursion
8563 to implement a recursive-descent parser.
8565 expr e04 expr'
8567 expr' cmpOp e04 expr'
8568 | Empty
8570 e04 e05 e04'
8572 e04' addOp e05 e04'
8573 | Empty
8575 e05 e06 e05'
8577 e05' binOp e06 e05'
8578 | Empty
8580 e06 e09 e06'
8582 e06' mulOp e09 e06'
8583 | Empty
8585 e09 OFFSET e10 e09'
8586 | SHORT e10'
8587 | + e10'
8588 | - e10'
8589 | ~ e10'
8590 | NOT e10'
8591 | e10 e09'
8593 e09' PTR e10 e09'
8594 | : e10 e09'
8595 | Empty
8597 e10 e11 e10'
8599 e10' [ expr ] e10'
8600 | Empty
8602 e11 ( expr )
8603 | [ expr ]
8604 | BYTE
8605 | WORD
8606 | DWORD
8607 | FWORD
8608 | QWORD
8609 | TBYTE
8610 | OWORD
8611 | XMMWORD
8612 | YMMWORD
8615 | register
8616 | id
8617 | constant */
8619 /* Parsing structure for the intel syntax parser. Used to implement the
8620 semantic actions for the operand grammar. */
8621 struct intel_parser_s
8623 char *op_string; /* The string being parsed. */
8624 int got_a_float; /* Whether the operand is a float. */
8625 int op_modifier; /* Operand modifier. */
8626 int is_mem; /* 1 if operand is memory reference. */
8627 int in_offset; /* >=1 if parsing operand of offset. */
8628 int in_bracket; /* >=1 if parsing operand in brackets. */
8629 const reg_entry *reg; /* Last register reference found. */
8630 char *disp; /* Displacement string being built. */
8631 char *next_operand; /* Resume point when splitting operands. */
8634 static struct intel_parser_s intel_parser;
8636 /* Token structure for parsing intel syntax. */
8637 struct intel_token
8639 int code; /* Token code. */
8640 const reg_entry *reg; /* Register entry for register tokens. */
8641 char *str; /* String representation. */
8644 static struct intel_token cur_token, prev_token;
8646 /* Token codes for the intel parser. Since T_SHORT is already used
8647 by COFF, undefine it first to prevent a warning. */
8648 #define T_NIL -1
8649 #define T_CONST 1
8650 #define T_REG 2
8651 #define T_BYTE 3
8652 #define T_WORD 4
8653 #define T_DWORD 5
8654 #define T_FWORD 6
8655 #define T_QWORD 7
8656 #define T_TBYTE 8
8657 #define T_XMMWORD 9
8658 #undef T_SHORT
8659 #define T_SHORT 10
8660 #define T_OFFSET 11
8661 #define T_PTR 12
8662 #define T_ID 13
8663 #define T_SHL 14
8664 #define T_SHR 15
8665 #define T_YMMWORD 16
8667 /* Prototypes for intel parser functions. */
8668 static int intel_match_token (int);
8669 static void intel_putback_token (void);
8670 static void intel_get_token (void);
8671 static int intel_expr (void);
8672 static int intel_e04 (void);
8673 static int intel_e05 (void);
8674 static int intel_e06 (void);
8675 static int intel_e09 (void);
8676 static int intel_e10 (void);
8677 static int intel_e11 (void);
8679 static int
8680 i386_intel_operand (char *operand_string, int got_a_float)
8682 int ret;
8683 char *p;
8685 p = intel_parser.op_string = xstrdup (operand_string);
8686 intel_parser.disp = (char *) xmalloc (strlen (operand_string) + 1);
8688 for (;;)
8690 /* Initialize token holders. */
8691 cur_token.code = prev_token.code = T_NIL;
8692 cur_token.reg = prev_token.reg = NULL;
8693 cur_token.str = prev_token.str = NULL;
8695 /* Initialize parser structure. */
8696 intel_parser.got_a_float = got_a_float;
8697 intel_parser.op_modifier = 0;
8698 intel_parser.is_mem = 0;
8699 intel_parser.in_offset = 0;
8700 intel_parser.in_bracket = 0;
8701 intel_parser.reg = NULL;
8702 intel_parser.disp[0] = '\0';
8703 intel_parser.next_operand = NULL;
8705 /* Read the first token and start the parser. */
8706 intel_get_token ();
8707 ret = intel_expr ();
8709 if (!ret)
8710 break;
8712 if (cur_token.code != T_NIL)
8714 as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
8715 current_templates->start->name, cur_token.str);
8716 ret = 0;
8718 /* If we found a memory reference, hand it over to i386_displacement
8719 to fill in the rest of the operand fields. */
8720 else if (intel_parser.is_mem)
8722 if ((i.mem_operands == 1
8723 && !current_templates->start->opcode_modifier.isstring)
8724 || i.mem_operands == 2)
8726 as_bad (_("too many memory references for '%s'"),
8727 current_templates->start->name);
8728 ret = 0;
8730 else
8732 char *s = intel_parser.disp;
8733 i.types[this_operand].bitfield.mem = 1;
8734 i.mem_operands++;
8736 if (!quiet_warnings && intel_parser.is_mem < 0)
8737 /* See the comments in intel_bracket_expr. */
8738 as_warn (_("Treating `%s' as memory reference"), operand_string);
8740 /* Add the displacement expression. */
8741 if (*s != '\0')
8742 ret = i386_displacement (s, s + strlen (s));
8743 if (ret)
8745 /* Swap base and index in 16-bit memory operands like
8746 [si+bx]. Since i386_index_check is also used in AT&T
8747 mode we have to do that here. */
8748 if (i.base_reg
8749 && i.index_reg
8750 && i.base_reg->reg_type.bitfield.reg16
8751 && i.index_reg->reg_type.bitfield.reg16
8752 && i.base_reg->reg_num >= 6
8753 && i.index_reg->reg_num < 6)
8755 const reg_entry *base = i.index_reg;
8757 i.index_reg = i.base_reg;
8758 i.base_reg = base;
8760 ret = i386_index_check (operand_string);
8765 /* Constant and OFFSET expressions are handled by i386_immediate. */
8766 else if ((intel_parser.op_modifier & (1 << T_OFFSET))
8767 || intel_parser.reg == NULL)
8769 if (i.mem_operands < 2 && i.seg[i.mem_operands])
8771 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
8772 as_warn (_("Segment override ignored"));
8773 i.seg[i.mem_operands] = NULL;
8775 ret = i386_immediate (intel_parser.disp);
8778 if (intel_parser.next_operand && this_operand >= MAX_OPERANDS - 1)
8779 ret = 0;
8780 if (!ret || !intel_parser.next_operand)
8781 break;
8782 intel_parser.op_string = intel_parser.next_operand;
8783 this_operand = i.operands++;
8784 i.types[this_operand].bitfield.unspecified = 1;
8787 free (p);
8788 free (intel_parser.disp);
8790 return ret;
8793 #define NUM_ADDRESS_REGS (!!i.base_reg + !!i.index_reg)
8795 /* expr e04 expr'
8797 expr' cmpOp e04 expr'
8798 | Empty */
8799 static int
8800 intel_expr (void)
8802 /* XXX Implement the comparison operators. */
8803 return intel_e04 ();
8806 /* e04 e05 e04'
8808 e04' addOp e05 e04'
8809 | Empty */
8810 static int
8811 intel_e04 (void)
8813 int nregs = -1;
8815 for (;;)
8817 if (!intel_e05())
8818 return 0;
8820 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
8821 i.base_reg = i386_regtab + REGNAM_AL; /* al is invalid as base */
8823 if (cur_token.code == '+')
8824 nregs = -1;
8825 else if (cur_token.code == '-')
8826 nregs = NUM_ADDRESS_REGS;
8827 else
8828 return 1;
8830 strcat (intel_parser.disp, cur_token.str);
8831 intel_match_token (cur_token.code);
8835 /* e05 e06 e05'
8837 e05' binOp e06 e05'
8838 | Empty */
8839 static int
8840 intel_e05 (void)
8842 int nregs = ~NUM_ADDRESS_REGS;
8844 for (;;)
8846 if (!intel_e06())
8847 return 0;
8849 if (cur_token.code == '&'
8850 || cur_token.code == '|'
8851 || cur_token.code == '^')
8853 char str[2];
8855 str[0] = cur_token.code;
8856 str[1] = 0;
8857 strcat (intel_parser.disp, str);
8859 else
8860 break;
8862 intel_match_token (cur_token.code);
8864 if (nregs < 0)
8865 nregs = ~nregs;
8867 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
8868 i.base_reg = i386_regtab + REGNAM_AL + 1; /* cl is invalid as base */
8869 return 1;
8872 /* e06 e09 e06'
8874 e06' mulOp e09 e06'
8875 | Empty */
8876 static int
8877 intel_e06 (void)
8879 int nregs = ~NUM_ADDRESS_REGS;
8881 for (;;)
8883 if (!intel_e09())
8884 return 0;
8886 if (cur_token.code == '*'
8887 || cur_token.code == '/'
8888 || cur_token.code == '%')
8890 char str[2];
8892 str[0] = cur_token.code;
8893 str[1] = 0;
8894 strcat (intel_parser.disp, str);
8896 else if (cur_token.code == T_SHL)
8897 strcat (intel_parser.disp, "<<");
8898 else if (cur_token.code == T_SHR)
8899 strcat (intel_parser.disp, ">>");
8900 else
8901 break;
8903 intel_match_token (cur_token.code);
8905 if (nregs < 0)
8906 nregs = ~nregs;
8908 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
8909 i.base_reg = i386_regtab + REGNAM_AL + 2; /* dl is invalid as base */
8910 return 1;
8913 /* e09 OFFSET e09
8914 | SHORT e09
8915 | + e09
8916 | - e09
8917 | ~ e09
8918 | NOT e09
8919 | e10 e09'
8921 e09' PTR e10 e09'
8922 | : e10 e09'
8923 | Empty */
8924 static int
8925 intel_e09 (void)
8927 int nregs = ~NUM_ADDRESS_REGS;
8928 int in_offset = 0;
8930 for (;;)
8932 /* Don't consume constants here. */
8933 if (cur_token.code == '+' || cur_token.code == '-')
8935 /* Need to look one token ahead - if the next token
8936 is a constant, the current token is its sign. */
8937 int next_code;
8939 intel_match_token (cur_token.code);
8940 next_code = cur_token.code;
8941 intel_putback_token ();
8942 if (next_code == T_CONST)
8943 break;
8946 /* e09 OFFSET e09 */
8947 if (cur_token.code == T_OFFSET)
8949 if (!in_offset++)
8950 ++intel_parser.in_offset;
8953 /* e09 SHORT e09 */
8954 else if (cur_token.code == T_SHORT)
8955 intel_parser.op_modifier |= 1 << T_SHORT;
8957 /* e09 + e09 */
8958 else if (cur_token.code == '+')
8959 strcat (intel_parser.disp, "+");
8961 /* e09 - e09
8962 | ~ e09
8963 | NOT e09 */
8964 else if (cur_token.code == '-' || cur_token.code == '~')
8966 char str[2];
8968 if (nregs < 0)
8969 nregs = ~nregs;
8970 str[0] = cur_token.code;
8971 str[1] = 0;
8972 strcat (intel_parser.disp, str);
8975 /* e09 e10 e09' */
8976 else
8977 break;
8979 intel_match_token (cur_token.code);
8982 for (;;)
8984 if (!intel_e10 ())
8985 return 0;
8987 /* e09' PTR e10 e09' */
8988 if (cur_token.code == T_PTR)
8990 char suffix;
8992 if (prev_token.code == T_BYTE)
8994 suffix = BYTE_MNEM_SUFFIX;
8995 i.types[this_operand].bitfield.byte = 1;
8998 else if (prev_token.code == T_WORD)
9000 if ((current_templates->start->name[0] == 'l'
9001 && current_templates->start->name[2] == 's'
9002 && current_templates->start->name[3] == 0)
9003 || current_templates->start->base_opcode == 0x62 /* bound */)
9004 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
9005 else if (intel_parser.got_a_float == 2) /* "fi..." */
9006 suffix = SHORT_MNEM_SUFFIX;
9007 else
9008 suffix = WORD_MNEM_SUFFIX;
9009 i.types[this_operand].bitfield.word = 1;
9012 else if (prev_token.code == T_DWORD)
9014 if ((current_templates->start->name[0] == 'l'
9015 && current_templates->start->name[2] == 's'
9016 && current_templates->start->name[3] == 0)
9017 || current_templates->start->base_opcode == 0x62 /* bound */)
9018 suffix = WORD_MNEM_SUFFIX;
9019 else if (flag_code == CODE_16BIT
9020 && (current_templates->start->opcode_modifier.jump
9021 || current_templates->start->opcode_modifier.jumpdword))
9022 suffix = LONG_DOUBLE_MNEM_SUFFIX;
9023 else if (intel_parser.got_a_float == 1) /* "f..." */
9024 suffix = SHORT_MNEM_SUFFIX;
9025 else
9026 suffix = LONG_MNEM_SUFFIX;
9027 i.types[this_operand].bitfield.dword = 1;
9030 else if (prev_token.code == T_FWORD)
9032 if (current_templates->start->name[0] == 'l'
9033 && current_templates->start->name[2] == 's'
9034 && current_templates->start->name[3] == 0)
9035 suffix = LONG_MNEM_SUFFIX;
9036 else if (!intel_parser.got_a_float)
9038 if (flag_code == CODE_16BIT)
9039 add_prefix (DATA_PREFIX_OPCODE);
9040 suffix = LONG_DOUBLE_MNEM_SUFFIX;
9042 else
9043 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
9044 i.types[this_operand].bitfield.fword = 1;
9047 else if (prev_token.code == T_QWORD)
9049 if (current_templates->start->base_opcode == 0x62 /* bound */
9050 || intel_parser.got_a_float == 1) /* "f..." */
9051 suffix = LONG_MNEM_SUFFIX;
9052 else
9053 suffix = QWORD_MNEM_SUFFIX;
9054 i.types[this_operand].bitfield.qword = 1;
9057 else if (prev_token.code == T_TBYTE)
9059 if (intel_parser.got_a_float == 1)
9060 suffix = LONG_DOUBLE_MNEM_SUFFIX;
9061 else
9062 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
9065 else if (prev_token.code == T_XMMWORD)
9067 suffix = XMMWORD_MNEM_SUFFIX;
9068 i.types[this_operand].bitfield.xmmword = 1;
9071 else if (prev_token.code == T_YMMWORD)
9073 suffix = YMMWORD_MNEM_SUFFIX;
9074 i.types[this_operand].bitfield.ymmword = 1;
9077 else
9079 as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
9080 return 0;
9083 i.types[this_operand].bitfield.unspecified = 0;
9085 /* Operands for jump/call using 'ptr' notation denote absolute
9086 addresses. */
9087 if (current_templates->start->opcode_modifier.jump
9088 || current_templates->start->opcode_modifier.jumpdword)
9089 i.types[this_operand].bitfield.jumpabsolute = 1;
9091 if (current_templates->start->base_opcode == 0x8d /* lea */)
9093 else if (!i.suffix)
9094 i.suffix = suffix;
9095 else if (i.suffix != suffix)
9097 as_bad (_("Conflicting operand modifiers"));
9098 return 0;
9103 /* e09' : e10 e09' */
9104 else if (cur_token.code == ':')
9106 if (prev_token.code != T_REG)
9108 /* While {call,jmp} SSSS:OOOO is MASM syntax only when SSSS is a
9109 segment/group identifier (which we don't have), using comma
9110 as the operand separator there is even less consistent, since
9111 there all branches only have a single operand. */
9112 if (this_operand != 0
9113 || intel_parser.in_offset
9114 || intel_parser.in_bracket
9115 || (!current_templates->start->opcode_modifier.jump
9116 && !current_templates->start->opcode_modifier.jumpdword
9117 && !current_templates->start->opcode_modifier.jumpintersegment
9118 && !current_templates->start->operand_types[0].bitfield.jumpabsolute))
9119 return intel_match_token (T_NIL);
9120 /* Remember the start of the 2nd operand and terminate 1st
9121 operand here.
9122 XXX This isn't right, yet (when SSSS:OOOO is right operand of
9123 another expression), but it gets at least the simplest case
9124 (a plain number or symbol on the left side) right. */
9125 intel_parser.next_operand = intel_parser.op_string;
9126 *--intel_parser.op_string = '\0';
9127 return intel_match_token (':');
9131 /* e09' Empty */
9132 else
9133 break;
9135 intel_match_token (cur_token.code);
9139 if (in_offset)
9141 --intel_parser.in_offset;
9142 if (nregs < 0)
9143 nregs = ~nregs;
9144 if (NUM_ADDRESS_REGS > nregs)
9146 as_bad (_("Invalid operand to `OFFSET'"));
9147 return 0;
9149 intel_parser.op_modifier |= 1 << T_OFFSET;
9152 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
9153 i.base_reg = i386_regtab + REGNAM_AL + 3; /* bl is invalid as base */
9154 return 1;
9157 static int
9158 intel_bracket_expr (void)
9160 int was_offset = intel_parser.op_modifier & (1 << T_OFFSET);
9161 const char *start = intel_parser.op_string;
9162 int len;
9164 if (i.op[this_operand].regs)
9165 return intel_match_token (T_NIL);
9167 intel_match_token ('[');
9169 /* Mark as a memory operand only if it's not already known to be an
9170 offset expression. If it's an offset expression, we need to keep
9171 the brace in. */
9172 if (!intel_parser.in_offset)
9174 ++intel_parser.in_bracket;
9176 /* Operands for jump/call inside brackets denote absolute addresses. */
9177 if (current_templates->start->opcode_modifier.jump
9178 || current_templates->start->opcode_modifier.jumpdword)
9179 i.types[this_operand].bitfield.jumpabsolute = 1;
9181 /* Unfortunately gas always diverged from MASM in a respect that can't
9182 be easily fixed without risking to break code sequences likely to be
9183 encountered (the testsuite even check for this): MASM doesn't consider
9184 an expression inside brackets unconditionally as a memory reference.
9185 When that is e.g. a constant, an offset expression, or the sum of the
9186 two, this is still taken as a constant load. gas, however, always
9187 treated these as memory references. As a compromise, we'll try to make
9188 offset expressions inside brackets work the MASM way (since that's
9189 less likely to be found in real world code), but make constants alone
9190 continue to work the traditional gas way. In either case, issue a
9191 warning. */
9192 intel_parser.op_modifier &= ~was_offset;
9194 else
9195 strcat (intel_parser.disp, "[");
9197 /* Add a '+' to the displacement string if necessary. */
9198 if (*intel_parser.disp != '\0'
9199 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
9200 strcat (intel_parser.disp, "+");
9202 if (intel_expr ()
9203 && (len = intel_parser.op_string - start - 1,
9204 intel_match_token (']')))
9206 /* Preserve brackets when the operand is an offset expression. */
9207 if (intel_parser.in_offset)
9208 strcat (intel_parser.disp, "]");
9209 else
9211 --intel_parser.in_bracket;
9212 if (i.base_reg || i.index_reg)
9213 intel_parser.is_mem = 1;
9214 if (!intel_parser.is_mem)
9216 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
9217 /* Defer the warning until all of the operand was parsed. */
9218 intel_parser.is_mem = -1;
9219 else if (!quiet_warnings)
9220 as_warn (_("`[%.*s]' taken to mean just `%.*s'"),
9221 len, start, len, start);
9224 intel_parser.op_modifier |= was_offset;
9226 return 1;
9228 return 0;
9231 /* e10 e11 e10'
9233 e10' [ expr ] e10'
9234 | Empty */
9235 static int
9236 intel_e10 (void)
9238 if (!intel_e11 ())
9239 return 0;
9241 while (cur_token.code == '[')
9243 if (!intel_bracket_expr ())
9244 return 0;
9247 return 1;
9250 /* e11 ( expr )
9251 | [ expr ]
9252 | BYTE
9253 | WORD
9254 | DWORD
9255 | FWORD
9256 | QWORD
9257 | TBYTE
9258 | OWORD
9259 | XMMWORD
9260 | YMMWORD
9263 | register
9264 | id
9265 | constant */
9266 static int
9267 intel_e11 (void)
9269 switch (cur_token.code)
9271 /* e11 ( expr ) */
9272 case '(':
9273 intel_match_token ('(');
9274 strcat (intel_parser.disp, "(");
9276 if (intel_expr () && intel_match_token (')'))
9278 strcat (intel_parser.disp, ")");
9279 return 1;
9281 return 0;
9283 /* e11 [ expr ] */
9284 case '[':
9285 return intel_bracket_expr ();
9287 /* e11 $
9288 | . */
9289 case '.':
9290 strcat (intel_parser.disp, cur_token.str);
9291 intel_match_token (cur_token.code);
9293 /* Mark as a memory operand only if it's not already known to be an
9294 offset expression. */
9295 if (!intel_parser.in_offset)
9296 intel_parser.is_mem = 1;
9298 return 1;
9300 /* e11 register */
9301 case T_REG:
9303 const reg_entry *reg = intel_parser.reg = cur_token.reg;
9305 intel_match_token (T_REG);
9307 /* Check for segment change. */
9308 if (cur_token.code == ':')
9310 if (!reg->reg_type.bitfield.sreg2
9311 && !reg->reg_type.bitfield.sreg3)
9313 as_bad (_("`%s' is not a valid segment register"),
9314 reg->reg_name);
9315 return 0;
9317 else if (i.mem_operands >= 2)
9318 as_warn (_("Segment override ignored"));
9319 else if (i.seg[i.mem_operands])
9320 as_warn (_("Extra segment override ignored"));
9321 else
9323 if (!intel_parser.in_offset)
9324 intel_parser.is_mem = 1;
9325 switch (reg->reg_num)
9327 case 0:
9328 i.seg[i.mem_operands] = &es;
9329 break;
9330 case 1:
9331 i.seg[i.mem_operands] = &cs;
9332 break;
9333 case 2:
9334 i.seg[i.mem_operands] = &ss;
9335 break;
9336 case 3:
9337 i.seg[i.mem_operands] = &ds;
9338 break;
9339 case 4:
9340 i.seg[i.mem_operands] = &fs;
9341 break;
9342 case 5:
9343 i.seg[i.mem_operands] = &gs;
9344 break;
9349 else if (reg->reg_type.bitfield.sreg3 && reg->reg_num == RegFlat)
9351 as_bad (_("cannot use `FLAT' here"));
9352 return 0;
9355 /* Not a segment register. Check for register scaling. */
9356 else if (cur_token.code == '*')
9358 if (!intel_parser.in_bracket)
9360 as_bad (_("Register scaling only allowed in memory operands"));
9361 return 0;
9364 if (reg->reg_type.bitfield.reg16) /* Disallow things like [si*1]. */
9365 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
9366 else if (i.index_reg)
9367 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
9369 /* What follows must be a valid scale. */
9370 intel_match_token ('*');
9371 i.index_reg = reg;
9372 i.types[this_operand].bitfield.baseindex = 1;
9374 /* Set the scale after setting the register (otherwise,
9375 i386_scale will complain) */
9376 if (cur_token.code == '+' || cur_token.code == '-')
9378 char *str, sign = cur_token.code;
9379 intel_match_token (cur_token.code);
9380 if (cur_token.code != T_CONST)
9382 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
9383 cur_token.str);
9384 return 0;
9386 str = (char *) xmalloc (strlen (cur_token.str) + 2);
9387 strcpy (str + 1, cur_token.str);
9388 *str = sign;
9389 if (!i386_scale (str))
9390 return 0;
9391 free (str);
9393 else if (!i386_scale (cur_token.str))
9394 return 0;
9395 intel_match_token (cur_token.code);
9398 /* No scaling. If this is a memory operand, the register is either a
9399 base register (first occurrence) or an index register (second
9400 occurrence). */
9401 else if (intel_parser.in_bracket)
9404 if (!i.base_reg)
9405 i.base_reg = reg;
9406 else if (!i.index_reg)
9407 i.index_reg = reg;
9408 else
9410 as_bad (_("Too many register references in memory operand"));
9411 return 0;
9414 i.types[this_operand].bitfield.baseindex = 1;
9417 /* It's neither base nor index. */
9418 else if (!intel_parser.in_offset && !intel_parser.is_mem)
9420 i386_operand_type temp = reg->reg_type;
9421 temp.bitfield.baseindex = 0;
9422 i.types[this_operand] = operand_type_or (i.types[this_operand],
9423 temp);
9424 i.types[this_operand].bitfield.unspecified = 0;
9425 i.op[this_operand].regs = reg;
9426 i.reg_operands++;
9428 else
9430 as_bad (_("Invalid use of register"));
9431 return 0;
9434 /* Since registers are not part of the displacement string (except
9435 when we're parsing offset operands), we may need to remove any
9436 preceding '+' from the displacement string. */
9437 if (*intel_parser.disp != '\0'
9438 && !intel_parser.in_offset)
9440 char *s = intel_parser.disp;
9441 s += strlen (s) - 1;
9442 if (*s == '+')
9443 *s = '\0';
9446 return 1;
9449 /* e11 BYTE
9450 | WORD
9451 | DWORD
9452 | FWORD
9453 | QWORD
9454 | TBYTE
9455 | OWORD
9456 | XMMWORD
9457 | YMMWORD */
9458 case T_BYTE:
9459 case T_WORD:
9460 case T_DWORD:
9461 case T_FWORD:
9462 case T_QWORD:
9463 case T_TBYTE:
9464 case T_XMMWORD:
9465 case T_YMMWORD:
9466 intel_match_token (cur_token.code);
9468 if (cur_token.code == T_PTR)
9469 return 1;
9471 /* It must have been an identifier. */
9472 intel_putback_token ();
9473 cur_token.code = T_ID;
9474 /* FALLTHRU */
9476 /* e11 id
9477 | constant */
9478 case T_ID:
9479 if (!intel_parser.in_offset && intel_parser.is_mem <= 0)
9481 symbolS *symbolP;
9483 /* The identifier represents a memory reference only if it's not
9484 preceded by an offset modifier and if it's not an equate. */
9485 symbolP = symbol_find(cur_token.str);
9486 if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
9487 intel_parser.is_mem = 1;
9489 /* FALLTHRU */
9491 case T_CONST:
9492 case '-':
9493 case '+':
9495 char *save_str, sign = 0;
9497 /* Allow constants that start with `+' or `-'. */
9498 if (cur_token.code == '-' || cur_token.code == '+')
9500 sign = cur_token.code;
9501 intel_match_token (cur_token.code);
9502 if (cur_token.code != T_CONST)
9504 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
9505 cur_token.str);
9506 return 0;
9510 save_str = (char *) xmalloc (strlen (cur_token.str) + 2);
9511 strcpy (save_str + !!sign, cur_token.str);
9512 if (sign)
9513 *save_str = sign;
9515 /* Get the next token to check for register scaling. */
9516 intel_match_token (cur_token.code);
9518 /* Check if this constant is a scaling factor for an
9519 index register. */
9520 if (cur_token.code == '*')
9522 if (intel_match_token ('*') && cur_token.code == T_REG)
9524 const reg_entry *reg = cur_token.reg;
9526 if (!intel_parser.in_bracket)
9528 as_bad (_("Register scaling only allowed "
9529 "in memory operands"));
9530 return 0;
9533 /* Disallow things like [1*si].
9534 sp and esp are invalid as index. */
9535 if (reg->reg_type.bitfield.reg16)
9536 reg = i386_regtab + REGNAM_AX + 4;
9537 else if (i.index_reg)
9538 reg = i386_regtab + REGNAM_EAX + 4;
9540 /* The constant is followed by `* reg', so it must be
9541 a valid scale. */
9542 i.index_reg = reg;
9543 i.types[this_operand].bitfield.baseindex = 1;
9545 /* Set the scale after setting the register (otherwise,
9546 i386_scale will complain) */
9547 if (!i386_scale (save_str))
9548 return 0;
9549 intel_match_token (T_REG);
9551 /* Since registers are not part of the displacement
9552 string, we may need to remove any preceding '+' from
9553 the displacement string. */
9554 if (*intel_parser.disp != '\0')
9556 char *s = intel_parser.disp;
9557 s += strlen (s) - 1;
9558 if (*s == '+')
9559 *s = '\0';
9562 free (save_str);
9564 return 1;
9567 /* The constant was not used for register scaling. Since we have
9568 already consumed the token following `*' we now need to put it
9569 back in the stream. */
9570 intel_putback_token ();
9573 /* Add the constant to the displacement string. */
9574 strcat (intel_parser.disp, save_str);
9575 free (save_str);
9577 return 1;
9581 as_bad (_("Unrecognized token '%s'"), cur_token.str);
9582 return 0;
9585 /* Match the given token against cur_token. If they match, read the next
9586 token from the operand string. */
9587 static int
9588 intel_match_token (int code)
9590 if (cur_token.code == code)
9592 intel_get_token ();
9593 return 1;
9595 else
9597 as_bad (_("Unexpected token `%s'"), cur_token.str);
9598 return 0;
9602 /* Read a new token from intel_parser.op_string and store it in cur_token. */
9603 static void
9604 intel_get_token (void)
9606 char *end_op;
9607 const reg_entry *reg;
9608 struct intel_token new_token;
9610 new_token.code = T_NIL;
9611 new_token.reg = NULL;
9612 new_token.str = NULL;
9614 /* Free the memory allocated to the previous token and move
9615 cur_token to prev_token. */
9616 if (prev_token.str)
9617 free (prev_token.str);
9619 prev_token = cur_token;
9621 /* Skip whitespace. */
9622 while (is_space_char (*intel_parser.op_string))
9623 intel_parser.op_string++;
9625 /* Return an empty token if we find nothing else on the line. */
9626 if (*intel_parser.op_string == '\0')
9628 cur_token = new_token;
9629 return;
9632 /* The new token cannot be larger than the remainder of the operand
9633 string. */
9634 new_token.str = (char *) xmalloc (strlen (intel_parser.op_string) + 1);
9635 new_token.str[0] = '\0';
9637 if (strchr ("0123456789", *intel_parser.op_string))
9639 char *p = new_token.str;
9640 char *q = intel_parser.op_string;
9641 new_token.code = T_CONST;
9643 /* Allow any kind of identifier char to encompass floating point and
9644 hexadecimal numbers. */
9645 while (is_identifier_char (*q))
9646 *p++ = *q++;
9647 *p = '\0';
9649 /* Recognize special symbol names [0-9][bf]. */
9650 if (strlen (intel_parser.op_string) == 2
9651 && (intel_parser.op_string[1] == 'b'
9652 || intel_parser.op_string[1] == 'f'))
9653 new_token.code = T_ID;
9656 else if ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL)
9658 size_t len = end_op - intel_parser.op_string;
9660 new_token.code = T_REG;
9661 new_token.reg = reg;
9663 memcpy (new_token.str, intel_parser.op_string, len);
9664 new_token.str[len] = '\0';
9667 else if (is_identifier_char (*intel_parser.op_string))
9669 char *p = new_token.str;
9670 char *q = intel_parser.op_string;
9672 /* A '.' or '$' followed by an identifier char is an identifier.
9673 Otherwise, it's operator '.' followed by an expression. */
9674 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
9676 new_token.code = '.';
9677 new_token.str[0] = '.';
9678 new_token.str[1] = '\0';
9680 else
9682 while (is_identifier_char (*q) || *q == '@')
9683 *p++ = *q++;
9684 *p = '\0';
9686 if (strcasecmp (new_token.str, "NOT") == 0)
9687 new_token.code = '~';
9689 else if (strcasecmp (new_token.str, "MOD") == 0)
9690 new_token.code = '%';
9692 else if (strcasecmp (new_token.str, "AND") == 0)
9693 new_token.code = '&';
9695 else if (strcasecmp (new_token.str, "OR") == 0)
9696 new_token.code = '|';
9698 else if (strcasecmp (new_token.str, "XOR") == 0)
9699 new_token.code = '^';
9701 else if (strcasecmp (new_token.str, "SHL") == 0)
9702 new_token.code = T_SHL;
9704 else if (strcasecmp (new_token.str, "SHR") == 0)
9705 new_token.code = T_SHR;
9707 else if (strcasecmp (new_token.str, "BYTE") == 0)
9708 new_token.code = T_BYTE;
9710 else if (strcasecmp (new_token.str, "WORD") == 0)
9711 new_token.code = T_WORD;
9713 else if (strcasecmp (new_token.str, "DWORD") == 0)
9714 new_token.code = T_DWORD;
9716 else if (strcasecmp (new_token.str, "FWORD") == 0)
9717 new_token.code = T_FWORD;
9719 else if (strcasecmp (new_token.str, "QWORD") == 0)
9720 new_token.code = T_QWORD;
9722 else if (strcasecmp (new_token.str, "TBYTE") == 0
9723 /* XXX remove (gcc still uses it) */
9724 || strcasecmp (new_token.str, "XWORD") == 0)
9725 new_token.code = T_TBYTE;
9727 else if (strcasecmp (new_token.str, "XMMWORD") == 0
9728 || strcasecmp (new_token.str, "OWORD") == 0)
9729 new_token.code = T_XMMWORD;
9731 else if (strcasecmp (new_token.str, "YMMWORD") == 0)
9732 new_token.code = T_YMMWORD;
9734 else if (strcasecmp (new_token.str, "PTR") == 0)
9735 new_token.code = T_PTR;
9737 else if (strcasecmp (new_token.str, "SHORT") == 0)
9738 new_token.code = T_SHORT;
9740 else if (strcasecmp (new_token.str, "OFFSET") == 0)
9742 new_token.code = T_OFFSET;
9744 /* ??? This is not mentioned in the MASM grammar but gcc
9745 makes use of it with -mintel-syntax. OFFSET may be
9746 followed by FLAT: */
9747 if (strncasecmp (q, " FLAT:", 6) == 0)
9748 strcat (new_token.str, " FLAT:");
9751 else
9752 new_token.code = T_ID;
9756 else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
9758 new_token.code = *intel_parser.op_string;
9759 new_token.str[0] = *intel_parser.op_string;
9760 new_token.str[1] = '\0';
9763 else if (strchr ("<>", *intel_parser.op_string)
9764 && *intel_parser.op_string == *(intel_parser.op_string + 1))
9766 new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
9767 new_token.str[0] = *intel_parser.op_string;
9768 new_token.str[1] = *intel_parser.op_string;
9769 new_token.str[2] = '\0';
9772 else
9773 as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
9775 intel_parser.op_string += strlen (new_token.str);
9776 cur_token = new_token;
9779 /* Put cur_token back into the token stream and make cur_token point to
9780 prev_token. */
9781 static void
9782 intel_putback_token (void)
9784 if (cur_token.code != T_NIL)
9786 intel_parser.op_string -= strlen (cur_token.str);
9787 free (cur_token.str);
9789 cur_token = prev_token;
9791 /* Forget prev_token. */
9792 prev_token.code = T_NIL;
9793 prev_token.reg = NULL;
9794 prev_token.str = NULL;
9797 void
9798 tc_x86_parse_to_dw2regnum (expressionS *exp)
9800 int saved_naked_reg;
9801 char saved_register_dot;
9803 saved_naked_reg = allow_naked_reg;
9804 allow_naked_reg = 1;
9805 saved_register_dot = register_chars['.'];
9806 register_chars['.'] = '.';
9807 allow_pseudo_reg = 1;
9808 expression_and_evaluate (exp);
9809 allow_pseudo_reg = 0;
9810 register_chars['.'] = saved_register_dot;
9811 allow_naked_reg = saved_naked_reg;
9813 if (exp->X_op == O_register && exp->X_add_number >= 0)
9815 if ((addressT) exp->X_add_number < i386_regtab_size)
9817 exp->X_op = O_constant;
9818 exp->X_add_number = i386_regtab[exp->X_add_number]
9819 .dw2_regnum[flag_code >> 1];
9821 else
9822 exp->X_op = O_illegal;
9826 void
9827 tc_x86_frame_initial_instructions (void)
9829 static unsigned int sp_regno[2];
9831 if (!sp_regno[flag_code >> 1])
9833 char *saved_input = input_line_pointer;
9834 char sp[][4] = {"esp", "rsp"};
9835 expressionS exp;
9837 input_line_pointer = sp[flag_code >> 1];
9838 tc_x86_parse_to_dw2regnum (&exp);
9839 assert (exp.X_op == O_constant);
9840 sp_regno[flag_code >> 1] = exp.X_add_number;
9841 input_line_pointer = saved_input;
9844 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
9845 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
9849 i386_elf_section_type (const char *str, size_t len)
9851 if (flag_code == CODE_64BIT
9852 && len == sizeof ("unwind") - 1
9853 && strncmp (str, "unwind", 6) == 0)
9854 return SHT_X86_64_UNWIND;
9856 return -1;
9859 #ifdef TE_PE
9860 void
9861 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
9863 expressionS expr;
9865 expr.X_op = O_secrel;
9866 expr.X_add_symbol = symbol;
9867 expr.X_add_number = 0;
9868 emit_expr (&expr, size);
9870 #endif
9872 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9873 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
9876 x86_64_section_letter (int letter, char **ptr_msg)
9878 if (flag_code == CODE_64BIT)
9880 if (letter == 'l')
9881 return SHF_X86_64_LARGE;
9883 *ptr_msg = _("Bad .section directive: want a,l,w,x,M,S,G,T in string");
9885 else
9886 *ptr_msg = _("Bad .section directive: want a,w,x,M,S,G,T in string");
9887 return -1;
9891 x86_64_section_word (char *str, size_t len)
9893 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
9894 return SHF_X86_64_LARGE;
9896 return -1;
9899 static void
9900 handle_large_common (int small ATTRIBUTE_UNUSED)
9902 if (flag_code != CODE_64BIT)
9904 s_comm_internal (0, elf_common_parse);
9905 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
9907 else
9909 static segT lbss_section;
9910 asection *saved_com_section_ptr = elf_com_section_ptr;
9911 asection *saved_bss_section = bss_section;
9913 if (lbss_section == NULL)
9915 flagword applicable;
9916 segT seg = now_seg;
9917 subsegT subseg = now_subseg;
9919 /* The .lbss section is for local .largecomm symbols. */
9920 lbss_section = subseg_new (".lbss", 0);
9921 applicable = bfd_applicable_section_flags (stdoutput);
9922 bfd_set_section_flags (stdoutput, lbss_section,
9923 applicable & SEC_ALLOC);
9924 seg_info (lbss_section)->bss = 1;
9926 subseg_set (seg, subseg);
9929 elf_com_section_ptr = &_bfd_elf_large_com_section;
9930 bss_section = lbss_section;
9932 s_comm_internal (0, elf_common_parse);
9934 elf_com_section_ptr = saved_com_section_ptr;
9935 bss_section = saved_bss_section;
9938 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */