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