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