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