]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
gas/
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
252b5132 1/* i386.c -- Assemble code for the Intel 80386
f7e42eb4 2 Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
aef6203b 3 2000, 2001, 2002, 2003, 2004, 2005
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
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
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"
252b5132 35#include "opcode/i386.h"
d2b2c203 36#include "elf/x86-64.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
252b5132
RH
46#ifndef SCALE1_WHEN_NO_INDEX
47/* Specifying a scale factor besides 1 when there is no index is
48 futile. eg. `mov (%ebx,2),%al' does exactly the same as
49 `mov (%ebx),%al'. To slavishly follow what the programmer
50 specified, set SCALE1_WHEN_NO_INDEX to 0. */
51#define SCALE1_WHEN_NO_INDEX 1
52#endif
53
29b0f896
AM
54#ifndef DEFAULT_ARCH
55#define DEFAULT_ARCH "i386"
246fcdee 56#endif
252b5132 57
edde18a5
AM
58#ifndef INLINE
59#if __GNUC__ >= 2
60#define INLINE __inline__
61#else
62#define INLINE
63#endif
64#endif
65
29b0f896
AM
66static INLINE unsigned int mode_from_disp_size PARAMS ((unsigned int));
67static INLINE int fits_in_signed_byte PARAMS ((offsetT));
68static INLINE int fits_in_unsigned_byte PARAMS ((offsetT));
69static INLINE int fits_in_unsigned_word PARAMS ((offsetT));
70static INLINE int fits_in_signed_word PARAMS ((offsetT));
71static INLINE int fits_in_unsigned_long PARAMS ((offsetT));
72static INLINE int fits_in_signed_long PARAMS ((offsetT));
847f7ad4
AM
73static int smallest_imm_type PARAMS ((offsetT));
74static offsetT offset_in_range PARAMS ((offsetT, int));
252b5132 75static int add_prefix PARAMS ((unsigned int));
3e73aa7c 76static void set_code_flag PARAMS ((int));
47926f60 77static void set_16bit_gcc_code_flag PARAMS ((int));
252b5132 78static void set_intel_syntax PARAMS ((int));
e413e4e9 79static void set_cpu_arch PARAMS ((int));
6482c264
NC
80#ifdef TE_PE
81static void pe_directive_secrel PARAMS ((int));
82#endif
29b0f896
AM
83static char *output_invalid PARAMS ((int c));
84static int i386_operand PARAMS ((char *operand_string));
85static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
86static const reg_entry *parse_register PARAMS ((char *reg_string,
87 char **end_op));
88static char *parse_insn PARAMS ((char *, char *));
89static char *parse_operands PARAMS ((char *, const char *));
90static void swap_operands PARAMS ((void));
91static void optimize_imm PARAMS ((void));
92static void optimize_disp PARAMS ((void));
93static int match_template PARAMS ((void));
94static int check_string PARAMS ((void));
95static int process_suffix PARAMS ((void));
96static int check_byte_reg PARAMS ((void));
97static int check_long_reg PARAMS ((void));
98static int check_qword_reg PARAMS ((void));
99static int check_word_reg PARAMS ((void));
100static int finalize_imm PARAMS ((void));
101static int process_operands PARAMS ((void));
102static const seg_entry *build_modrm_byte PARAMS ((void));
103static void output_insn PARAMS ((void));
104static void output_branch PARAMS ((void));
105static void output_jump PARAMS ((void));
106static void output_interseg_jump PARAMS ((void));
2bbd9c25
JJ
107static void output_imm PARAMS ((fragS *insn_start_frag,
108 offsetT insn_start_off));
109static void output_disp PARAMS ((fragS *insn_start_frag,
110 offsetT insn_start_off));
29b0f896
AM
111#ifndef I386COFF
112static void s_bss PARAMS ((int));
252b5132
RH
113#endif
114
a847613f 115static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 116
252b5132 117/* 'md_assemble ()' gathers together information and puts it into a
47926f60 118 i386_insn. */
252b5132 119
520dc8e8
AM
120union i386_op
121 {
122 expressionS *disps;
123 expressionS *imms;
124 const reg_entry *regs;
125 };
126
252b5132
RH
127struct _i386_insn
128 {
47926f60 129 /* TM holds the template for the insn were currently assembling. */
252b5132
RH
130 template tm;
131
132 /* SUFFIX holds the instruction mnemonic suffix if given.
133 (e.g. 'l' for 'movl') */
134 char suffix;
135
47926f60 136 /* OPERANDS gives the number of given operands. */
252b5132
RH
137 unsigned int operands;
138
139 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
140 of given register, displacement, memory operands and immediate
47926f60 141 operands. */
252b5132
RH
142 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
143
144 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 145 use OP[i] for the corresponding operand. */
252b5132
RH
146 unsigned int types[MAX_OPERANDS];
147
520dc8e8
AM
148 /* Displacement expression, immediate expression, or register for each
149 operand. */
150 union i386_op op[MAX_OPERANDS];
252b5132 151
3e73aa7c
JH
152 /* Flags for operands. */
153 unsigned int flags[MAX_OPERANDS];
154#define Operand_PCrel 1
155
252b5132 156 /* Relocation type for operand */
f86103b7 157 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 158
252b5132
RH
159 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
160 the base index byte below. */
161 const reg_entry *base_reg;
162 const reg_entry *index_reg;
163 unsigned int log2_scale_factor;
164
165 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 166 explicit segment overrides are given. */
ce8a8b2f 167 const seg_entry *seg[2];
252b5132
RH
168
169 /* PREFIX holds all the given prefix opcodes (usually null).
170 PREFIXES is the number of prefix opcodes. */
171 unsigned int prefixes;
172 unsigned char prefix[MAX_PREFIXES];
173
174 /* RM and SIB are the modrm byte and the sib byte where the
175 addressing modes of this insn are encoded. */
176
177 modrm_byte rm;
3e73aa7c 178 rex_byte rex;
252b5132
RH
179 sib_byte sib;
180 };
181
182typedef struct _i386_insn i386_insn;
183
184/* List of chars besides those in app.c:symbol_chars that can start an
185 operand. Used to prevent the scrubber eating vital white-space. */
32137342 186const char extra_symbol_chars[] = "*%-(["
252b5132 187#ifdef LEX_AT
32137342
NC
188 "@"
189#endif
190#ifdef LEX_QM
191 "?"
252b5132 192#endif
32137342 193 ;
252b5132 194
29b0f896
AM
195#if (defined (TE_I386AIX) \
196 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
197 && !defined (TE_LINUX) \
32137342 198 && !defined (TE_NETWARE) \
29b0f896
AM
199 && !defined (TE_FreeBSD) \
200 && !defined (TE_NetBSD)))
252b5132 201/* This array holds the chars that always start a comment. If the
ce8a8b2f 202 pre-processor is disabled, these aren't very useful. */
252b5132
RH
203const char comment_chars[] = "#/";
204#define PREFIX_SEPARATOR '\\'
252b5132
RH
205
206/* This array holds the chars that only start a comment at the beginning of
207 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
208 .line and .file directives will appear in the pre-processed output.
209 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 210 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
211 #NO_APP at the beginning of its output.
212 Also note that comments started like this one will always work if
252b5132 213 '/' isn't otherwise defined. */
0d9f6d04 214const char line_comment_chars[] = "#";
29b0f896 215
252b5132 216#else
29b0f896
AM
217/* Putting '/' here makes it impossible to use the divide operator.
218 However, we need it for compatibility with SVR4 systems. */
219const char comment_chars[] = "#";
220#define PREFIX_SEPARATOR '/'
221
0d9f6d04 222const char line_comment_chars[] = "/#";
252b5132
RH
223#endif
224
63a0b638 225const char line_separator_chars[] = ";";
252b5132 226
ce8a8b2f
AM
227/* Chars that can be used to separate mant from exp in floating point
228 nums. */
252b5132
RH
229const char EXP_CHARS[] = "eE";
230
ce8a8b2f
AM
231/* Chars that mean this number is a floating point constant
232 As in 0f12.456
233 or 0d1.2345e12. */
252b5132
RH
234const char FLT_CHARS[] = "fFdDxX";
235
ce8a8b2f 236/* Tables for lexical analysis. */
252b5132
RH
237static char mnemonic_chars[256];
238static char register_chars[256];
239static char operand_chars[256];
240static char identifier_chars[256];
241static char digit_chars[256];
242
ce8a8b2f 243/* Lexical macros. */
252b5132
RH
244#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
245#define is_operand_char(x) (operand_chars[(unsigned char) x])
246#define is_register_char(x) (register_chars[(unsigned char) x])
247#define is_space_char(x) ((x) == ' ')
248#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
249#define is_digit_char(x) (digit_chars[(unsigned char) x])
250
0234cb7c 251/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
252static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
253
254/* md_assemble() always leaves the strings it's passed unaltered. To
255 effect this we maintain a stack of saved characters that we've smashed
256 with '\0's (indicating end of strings for various sub-fields of the
47926f60 257 assembler instruction). */
252b5132 258static char save_stack[32];
ce8a8b2f 259static char *save_stack_p;
252b5132
RH
260#define END_STRING_AND_SAVE(s) \
261 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
262#define RESTORE_END_STRING(s) \
263 do { *(s) = *--save_stack_p; } while (0)
264
47926f60 265/* The instruction we're assembling. */
252b5132
RH
266static i386_insn i;
267
268/* Possible templates for current insn. */
269static const templates *current_templates;
270
47926f60 271/* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
252b5132
RH
272static expressionS disp_expressions[2], im_expressions[2];
273
47926f60
KH
274/* Current operand we are working on. */
275static int this_operand;
252b5132 276
3e73aa7c
JH
277/* We support four different modes. FLAG_CODE variable is used to distinguish
278 these. */
279
280enum flag_code {
281 CODE_32BIT,
282 CODE_16BIT,
283 CODE_64BIT };
f3c180ae 284#define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
3e73aa7c
JH
285
286static enum flag_code flag_code;
287static int use_rela_relocations = 0;
288
289/* The names used to print error messages. */
b77a7acd 290static const char *flag_code_names[] =
3e73aa7c
JH
291 {
292 "32",
293 "16",
294 "64"
295 };
252b5132 296
47926f60
KH
297/* 1 for intel syntax,
298 0 if att syntax. */
299static int intel_syntax = 0;
252b5132 300
47926f60
KH
301/* 1 if register prefix % not required. */
302static int allow_naked_reg = 0;
252b5132 303
47926f60
KH
304/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
305 leave, push, and pop instructions so that gcc has the same stack
306 frame as in 32 bit mode. */
307static char stackop_size = '\0';
eecb386c 308
12b55ccc
L
309/* Non-zero to optimize code alignment. */
310int optimize_align_code = 1;
311
47926f60
KH
312/* Non-zero to quieten some warnings. */
313static int quiet_warnings = 0;
a38cf1db 314
47926f60
KH
315/* CPU name. */
316static const char *cpu_arch_name = NULL;
5c6af06e 317static const char *cpu_sub_arch_name = NULL;
a38cf1db 318
47926f60 319/* CPU feature flags. */
29b0f896 320static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
a38cf1db 321
fddf5b5b
AM
322/* If set, conditional jumps are not automatically promoted to handle
323 larger than a byte offset. */
324static unsigned int no_cond_jump_promotion = 0;
325
29b0f896 326/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 327static symbolS *GOT_symbol;
29b0f896 328
a4447b93
RH
329/* The dwarf2 return column, adjusted for 32 or 64 bit. */
330unsigned int x86_dwarf2_return_column;
331
332/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
333int x86_cie_data_alignment;
334
252b5132 335/* Interface to relax_segment.
fddf5b5b
AM
336 There are 3 major relax states for 386 jump insns because the
337 different types of jumps add different sizes to frags when we're
338 figuring out what sort of jump to choose to reach a given label. */
252b5132 339
47926f60 340/* Types. */
93c2a809
AM
341#define UNCOND_JUMP 0
342#define COND_JUMP 1
343#define COND_JUMP86 2
fddf5b5b 344
47926f60 345/* Sizes. */
252b5132
RH
346#define CODE16 1
347#define SMALL 0
29b0f896 348#define SMALL16 (SMALL | CODE16)
252b5132 349#define BIG 2
29b0f896 350#define BIG16 (BIG | CODE16)
252b5132
RH
351
352#ifndef INLINE
353#ifdef __GNUC__
354#define INLINE __inline__
355#else
356#define INLINE
357#endif
358#endif
359
fddf5b5b
AM
360#define ENCODE_RELAX_STATE(type, size) \
361 ((relax_substateT) (((type) << 2) | (size)))
362#define TYPE_FROM_RELAX_STATE(s) \
363 ((s) >> 2)
364#define DISP_SIZE_FROM_RELAX_STATE(s) \
365 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
366
367/* This table is used by relax_frag to promote short jumps to long
368 ones where necessary. SMALL (short) jumps may be promoted to BIG
369 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
370 don't allow a short jump in a 32 bit code segment to be promoted to
371 a 16 bit offset jump because it's slower (requires data size
372 prefix), and doesn't work, unless the destination is in the bottom
373 64k of the code segment (The top 16 bits of eip are zeroed). */
374
375const relax_typeS md_relax_table[] =
376{
24eab124
AM
377 /* The fields are:
378 1) most positive reach of this state,
379 2) most negative reach of this state,
93c2a809 380 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 381 4) which index into the table to try if we can't fit into this one. */
252b5132 382
fddf5b5b 383 /* UNCOND_JUMP states. */
93c2a809
AM
384 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
385 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
386 /* dword jmp adds 4 bytes to frag:
387 0 extra opcode bytes, 4 displacement bytes. */
252b5132 388 {0, 0, 4, 0},
93c2a809
AM
389 /* word jmp adds 2 byte2 to frag:
390 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
391 {0, 0, 2, 0},
392
93c2a809
AM
393 /* COND_JUMP states. */
394 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
395 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
396 /* dword conditionals adds 5 bytes to frag:
397 1 extra opcode byte, 4 displacement bytes. */
398 {0, 0, 5, 0},
fddf5b5b 399 /* word conditionals add 3 bytes to frag:
93c2a809
AM
400 1 extra opcode byte, 2 displacement bytes. */
401 {0, 0, 3, 0},
402
403 /* COND_JUMP86 states. */
404 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
405 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
406 /* dword conditionals adds 5 bytes to frag:
407 1 extra opcode byte, 4 displacement bytes. */
408 {0, 0, 5, 0},
409 /* word conditionals add 4 bytes to frag:
410 1 displacement byte and a 3 byte long branch insn. */
411 {0, 0, 4, 0}
252b5132
RH
412};
413
e413e4e9
AM
414static const arch_entry cpu_arch[] = {
415 {"i8086", Cpu086 },
416 {"i186", Cpu086|Cpu186 },
417 {"i286", Cpu086|Cpu186|Cpu286 },
418 {"i386", Cpu086|Cpu186|Cpu286|Cpu386 },
419 {"i486", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486 },
5c6af06e
JB
420 {"i586", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
421 {"i686", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
422 {"pentium", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
423 {"pentiumpro",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
424 {"pentiumii", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX },
425 {"pentiumiii",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuMMX2|CpuSSE },
426 {"pentium4", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
427 {"prescott", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuPNI },
428 {"k6", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX },
429 {"k6_2", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow },
430 {"athlon", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
431 {"sledgehammer",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2 },
30123838 432 {"opteron", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2 },
5c6af06e
JB
433 {".mmx", CpuMMX },
434 {".sse", CpuMMX|CpuMMX2|CpuSSE },
435 {".sse2", CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
436 {".3dnow", CpuMMX|Cpu3dnow },
437 {".3dnowa", CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
438 {".padlock", CpuPadLock },
30123838
JB
439 {".pacifica", CpuSVME },
440 {".svme", CpuSVME },
e413e4e9
AM
441 {NULL, 0 }
442};
443
29b0f896
AM
444const pseudo_typeS md_pseudo_table[] =
445{
446#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
447 {"align", s_align_bytes, 0},
448#else
449 {"align", s_align_ptwo, 0},
450#endif
451 {"arch", set_cpu_arch, 0},
452#ifndef I386COFF
453 {"bss", s_bss, 0},
454#endif
455 {"ffloat", float_cons, 'f'},
456 {"dfloat", float_cons, 'd'},
457 {"tfloat", float_cons, 'x'},
458 {"value", cons, 2},
459 {"noopt", s_ignore, 0},
460 {"optim", s_ignore, 0},
461 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
462 {"code16", set_code_flag, CODE_16BIT},
463 {"code32", set_code_flag, CODE_32BIT},
464 {"code64", set_code_flag, CODE_64BIT},
465 {"intel_syntax", set_intel_syntax, 1},
466 {"att_syntax", set_intel_syntax, 0},
c6682705 467 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
29b0f896 468 {"loc", dwarf2_directive_loc, 0},
6482c264
NC
469#ifdef TE_PE
470 {"secrel32", pe_directive_secrel, 0},
471#endif
29b0f896
AM
472 {0, 0, 0}
473};
474
475/* For interface with expression (). */
476extern char *input_line_pointer;
477
478/* Hash table for instruction mnemonic lookup. */
479static struct hash_control *op_hash;
480
481/* Hash table for register lookup. */
482static struct hash_control *reg_hash;
483\f
252b5132
RH
484void
485i386_align_code (fragP, count)
486 fragS *fragP;
487 int count;
488{
ce8a8b2f
AM
489 /* Various efficient no-op patterns for aligning code labels.
490 Note: Don't try to assemble the instructions in the comments.
491 0L and 0w are not legal. */
252b5132
RH
492 static const char f32_1[] =
493 {0x90}; /* nop */
494 static const char f32_2[] =
495 {0x89,0xf6}; /* movl %esi,%esi */
496 static const char f32_3[] =
497 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
498 static const char f32_4[] =
499 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
500 static const char f32_5[] =
501 {0x90, /* nop */
502 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
503 static const char f32_6[] =
504 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
505 static const char f32_7[] =
506 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
507 static const char f32_8[] =
508 {0x90, /* nop */
509 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
510 static const char f32_9[] =
511 {0x89,0xf6, /* movl %esi,%esi */
512 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
513 static const char f32_10[] =
514 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
515 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
516 static const char f32_11[] =
517 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
518 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
519 static const char f32_12[] =
520 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
521 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
522 static const char f32_13[] =
523 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
524 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
525 static const char f32_14[] =
526 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
527 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
528 static const char f32_15[] =
529 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
530 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
c3332e24
AM
531 static const char f16_3[] =
532 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
252b5132
RH
533 static const char f16_4[] =
534 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
535 static const char f16_5[] =
536 {0x90, /* nop */
537 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
538 static const char f16_6[] =
539 {0x89,0xf6, /* mov %si,%si */
540 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
541 static const char f16_7[] =
542 {0x8d,0x74,0x00, /* lea 0(%si),%si */
543 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
544 static const char f16_8[] =
545 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
546 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
547 static const char *const f32_patt[] = {
548 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
549 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
550 };
551 static const char *const f16_patt[] = {
c3332e24 552 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
252b5132
RH
553 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
554 };
555
33fef721
JH
556 if (count <= 0 || count > 15)
557 return;
3e73aa7c 558
33fef721
JH
559 /* The recommended way to pad 64bit code is to use NOPs preceded by
560 maximally four 0x66 prefixes. Balance the size of nops. */
561 if (flag_code == CODE_64BIT)
252b5132 562 {
33fef721
JH
563 int i;
564 int nnops = (count + 3) / 4;
565 int len = count / nnops;
566 int remains = count - nnops * len;
567 int pos = 0;
568
569 for (i = 0; i < remains; i++)
252b5132 570 {
33fef721
JH
571 memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len);
572 fragP->fr_literal[fragP->fr_fix + pos + len] = 0x90;
573 pos += len + 1;
574 }
575 for (; i < nnops; i++)
576 {
577 memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len - 1);
578 fragP->fr_literal[fragP->fr_fix + pos + len - 1] = 0x90;
579 pos += len;
252b5132 580 }
252b5132 581 }
33fef721
JH
582 else
583 if (flag_code == CODE_16BIT)
584 {
585 memcpy (fragP->fr_literal + fragP->fr_fix,
586 f16_patt[count - 1], count);
587 if (count > 8)
588 /* Adjust jump offset. */
589 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
590 }
591 else
592 memcpy (fragP->fr_literal + fragP->fr_fix,
593 f32_patt[count - 1], count);
594 fragP->fr_var = count;
252b5132
RH
595}
596
252b5132
RH
597static INLINE unsigned int
598mode_from_disp_size (t)
599 unsigned int t;
600{
3e73aa7c 601 return (t & Disp8) ? 1 : (t & (Disp16 | Disp32 | Disp32S)) ? 2 : 0;
252b5132
RH
602}
603
604static INLINE int
605fits_in_signed_byte (num)
847f7ad4 606 offsetT num;
252b5132
RH
607{
608 return (num >= -128) && (num <= 127);
47926f60 609}
252b5132
RH
610
611static INLINE int
612fits_in_unsigned_byte (num)
847f7ad4 613 offsetT num;
252b5132
RH
614{
615 return (num & 0xff) == num;
47926f60 616}
252b5132
RH
617
618static INLINE int
619fits_in_unsigned_word (num)
847f7ad4 620 offsetT num;
252b5132
RH
621{
622 return (num & 0xffff) == num;
47926f60 623}
252b5132
RH
624
625static INLINE int
626fits_in_signed_word (num)
847f7ad4 627 offsetT num;
252b5132
RH
628{
629 return (-32768 <= num) && (num <= 32767);
47926f60 630}
3e73aa7c
JH
631static INLINE int
632fits_in_signed_long (num)
633 offsetT num ATTRIBUTE_UNUSED;
634{
635#ifndef BFD64
636 return 1;
637#else
638 return (!(((offsetT) -1 << 31) & num)
639 || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
640#endif
641} /* fits_in_signed_long() */
642static INLINE int
643fits_in_unsigned_long (num)
644 offsetT num ATTRIBUTE_UNUSED;
645{
646#ifndef BFD64
647 return 1;
648#else
649 return (num & (((offsetT) 2 << 31) - 1)) == num;
650#endif
651} /* fits_in_unsigned_long() */
252b5132
RH
652
653static int
654smallest_imm_type (num)
847f7ad4 655 offsetT num;
252b5132 656{
a847613f 657 if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
e413e4e9
AM
658 {
659 /* This code is disabled on the 486 because all the Imm1 forms
660 in the opcode table are slower on the i486. They're the
661 versions with the implicitly specified single-position
662 displacement, which has another syntax if you really want to
663 use that form. */
664 if (num == 1)
3e73aa7c 665 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64;
e413e4e9 666 }
252b5132 667 return (fits_in_signed_byte (num)
3e73aa7c 668 ? (Imm8S | Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
252b5132 669 : fits_in_unsigned_byte (num)
3e73aa7c 670 ? (Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
252b5132 671 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
3e73aa7c
JH
672 ? (Imm16 | Imm32 | Imm32S | Imm64)
673 : fits_in_signed_long (num)
674 ? (Imm32 | Imm32S | Imm64)
675 : fits_in_unsigned_long (num)
676 ? (Imm32 | Imm64)
677 : Imm64);
47926f60 678}
252b5132 679
847f7ad4
AM
680static offsetT
681offset_in_range (val, size)
682 offsetT val;
683 int size;
684{
508866be 685 addressT mask;
ba2adb93 686
847f7ad4
AM
687 switch (size)
688 {
508866be
L
689 case 1: mask = ((addressT) 1 << 8) - 1; break;
690 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 691 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
692#ifdef BFD64
693 case 8: mask = ((addressT) 2 << 63) - 1; break;
694#endif
47926f60 695 default: abort ();
847f7ad4
AM
696 }
697
ba2adb93 698 /* If BFD64, sign extend val. */
3e73aa7c
JH
699 if (!use_rela_relocations)
700 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
701 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
ba2adb93 702
47926f60 703 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
704 {
705 char buf1[40], buf2[40];
706
707 sprint_value (buf1, val);
708 sprint_value (buf2, val & mask);
709 as_warn (_("%s shortened to %s"), buf1, buf2);
710 }
711 return val & mask;
712}
713
252b5132
RH
714/* Returns 0 if attempting to add a prefix where one from the same
715 class already exists, 1 if non rep/repne added, 2 if rep/repne
716 added. */
717static int
718add_prefix (prefix)
719 unsigned int prefix;
720{
721 int ret = 1;
722 int q;
723
29b0f896
AM
724 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
725 && flag_code == CODE_64BIT)
3e73aa7c
JH
726 q = REX_PREFIX;
727 else
728 switch (prefix)
729 {
730 default:
731 abort ();
732
733 case CS_PREFIX_OPCODE:
734 case DS_PREFIX_OPCODE:
735 case ES_PREFIX_OPCODE:
736 case FS_PREFIX_OPCODE:
737 case GS_PREFIX_OPCODE:
738 case SS_PREFIX_OPCODE:
739 q = SEG_PREFIX;
740 break;
252b5132 741
3e73aa7c
JH
742 case REPNE_PREFIX_OPCODE:
743 case REPE_PREFIX_OPCODE:
744 ret = 2;
745 /* fall thru */
746 case LOCK_PREFIX_OPCODE:
747 q = LOCKREP_PREFIX;
748 break;
252b5132 749
3e73aa7c
JH
750 case FWAIT_OPCODE:
751 q = WAIT_PREFIX;
752 break;
252b5132 753
3e73aa7c
JH
754 case ADDR_PREFIX_OPCODE:
755 q = ADDR_PREFIX;
756 break;
252b5132 757
3e73aa7c
JH
758 case DATA_PREFIX_OPCODE:
759 q = DATA_PREFIX;
760 break;
761 }
252b5132 762
29b0f896 763 if (i.prefix[q] != 0)
252b5132
RH
764 {
765 as_bad (_("same type of prefix used twice"));
766 return 0;
767 }
768
769 i.prefixes += 1;
770 i.prefix[q] = prefix;
771 return ret;
772}
773
774static void
3e73aa7c 775set_code_flag (value)
e5cb08ac 776 int value;
eecb386c 777{
3e73aa7c
JH
778 flag_code = value;
779 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
780 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
781 if (value == CODE_64BIT && !(cpu_arch_flags & CpuSledgehammer))
782 {
783 as_bad (_("64bit mode not supported on this CPU."));
784 }
785 if (value == CODE_32BIT && !(cpu_arch_flags & Cpu386))
786 {
787 as_bad (_("32bit mode not supported on this CPU."));
788 }
eecb386c
AM
789 stackop_size = '\0';
790}
791
792static void
3e73aa7c
JH
793set_16bit_gcc_code_flag (new_code_flag)
794 int new_code_flag;
252b5132 795{
3e73aa7c
JH
796 flag_code = new_code_flag;
797 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
798 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
9306ca4a 799 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
800}
801
802static void
803set_intel_syntax (syntax_flag)
eecb386c 804 int syntax_flag;
252b5132
RH
805{
806 /* Find out if register prefixing is specified. */
807 int ask_naked_reg = 0;
808
809 SKIP_WHITESPACE ();
29b0f896 810 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132
RH
811 {
812 char *string = input_line_pointer;
813 int e = get_symbol_end ();
814
47926f60 815 if (strcmp (string, "prefix") == 0)
252b5132 816 ask_naked_reg = 1;
47926f60 817 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
818 ask_naked_reg = -1;
819 else
d0b47220 820 as_bad (_("bad argument to syntax directive."));
252b5132
RH
821 *input_line_pointer = e;
822 }
823 demand_empty_rest_of_line ();
c3332e24 824
252b5132
RH
825 intel_syntax = syntax_flag;
826
827 if (ask_naked_reg == 0)
f86103b7
AM
828 allow_naked_reg = (intel_syntax
829 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
830 else
831 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a
JB
832
833 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
834 identifier_chars['$'] = intel_syntax ? '$' : 0;
252b5132
RH
835}
836
e413e4e9
AM
837static void
838set_cpu_arch (dummy)
47926f60 839 int dummy ATTRIBUTE_UNUSED;
e413e4e9 840{
47926f60 841 SKIP_WHITESPACE ();
e413e4e9 842
29b0f896 843 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9
AM
844 {
845 char *string = input_line_pointer;
846 int e = get_symbol_end ();
847 int i;
848
849 for (i = 0; cpu_arch[i].name; i++)
850 {
851 if (strcmp (string, cpu_arch[i].name) == 0)
852 {
5c6af06e
JB
853 if (*string != '.')
854 {
855 cpu_arch_name = cpu_arch[i].name;
856 cpu_sub_arch_name = NULL;
857 cpu_arch_flags = (cpu_arch[i].flags
858 | (flag_code == CODE_64BIT ? Cpu64 : CpuNo64));
859 break;
860 }
861 if ((cpu_arch_flags | cpu_arch[i].flags) != cpu_arch_flags)
862 {
863 cpu_sub_arch_name = cpu_arch[i].name;
864 cpu_arch_flags |= cpu_arch[i].flags;
865 }
866 *input_line_pointer = e;
867 demand_empty_rest_of_line ();
868 return;
e413e4e9
AM
869 }
870 }
871 if (!cpu_arch[i].name)
872 as_bad (_("no such architecture: `%s'"), string);
873
874 *input_line_pointer = e;
875 }
876 else
877 as_bad (_("missing cpu architecture"));
878
fddf5b5b
AM
879 no_cond_jump_promotion = 0;
880 if (*input_line_pointer == ','
29b0f896 881 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b
AM
882 {
883 char *string = ++input_line_pointer;
884 int e = get_symbol_end ();
885
886 if (strcmp (string, "nojumps") == 0)
887 no_cond_jump_promotion = 1;
888 else if (strcmp (string, "jumps") == 0)
889 ;
890 else
891 as_bad (_("no such architecture modifier: `%s'"), string);
892
893 *input_line_pointer = e;
894 }
895
e413e4e9
AM
896 demand_empty_rest_of_line ();
897}
898
b9d79e03
JH
899unsigned long
900i386_mach ()
901{
902 if (!strcmp (default_arch, "x86_64"))
903 return bfd_mach_x86_64;
904 else if (!strcmp (default_arch, "i386"))
905 return bfd_mach_i386_i386;
906 else
907 as_fatal (_("Unknown architecture"));
908}
b9d79e03 909\f
252b5132
RH
910void
911md_begin ()
912{
913 const char *hash_err;
914
47926f60 915 /* Initialize op_hash hash table. */
252b5132
RH
916 op_hash = hash_new ();
917
918 {
29b0f896
AM
919 const template *optab;
920 templates *core_optab;
252b5132 921
47926f60
KH
922 /* Setup for loop. */
923 optab = i386_optab;
252b5132
RH
924 core_optab = (templates *) xmalloc (sizeof (templates));
925 core_optab->start = optab;
926
927 while (1)
928 {
929 ++optab;
930 if (optab->name == NULL
931 || strcmp (optab->name, (optab - 1)->name) != 0)
932 {
933 /* different name --> ship out current template list;
47926f60 934 add to hash table; & begin anew. */
252b5132
RH
935 core_optab->end = optab;
936 hash_err = hash_insert (op_hash,
937 (optab - 1)->name,
938 (PTR) core_optab);
939 if (hash_err)
940 {
252b5132
RH
941 as_fatal (_("Internal Error: Can't hash %s: %s"),
942 (optab - 1)->name,
943 hash_err);
944 }
945 if (optab->name == NULL)
946 break;
947 core_optab = (templates *) xmalloc (sizeof (templates));
948 core_optab->start = optab;
949 }
950 }
951 }
952
47926f60 953 /* Initialize reg_hash hash table. */
252b5132
RH
954 reg_hash = hash_new ();
955 {
29b0f896 956 const reg_entry *regtab;
252b5132
RH
957
958 for (regtab = i386_regtab;
959 regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
960 regtab++)
961 {
962 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
963 if (hash_err)
3e73aa7c
JH
964 as_fatal (_("Internal Error: Can't hash %s: %s"),
965 regtab->reg_name,
966 hash_err);
252b5132
RH
967 }
968 }
969
47926f60 970 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 971 {
29b0f896
AM
972 int c;
973 char *p;
252b5132
RH
974
975 for (c = 0; c < 256; c++)
976 {
3882b010 977 if (ISDIGIT (c))
252b5132
RH
978 {
979 digit_chars[c] = c;
980 mnemonic_chars[c] = c;
981 register_chars[c] = c;
982 operand_chars[c] = c;
983 }
3882b010 984 else if (ISLOWER (c))
252b5132
RH
985 {
986 mnemonic_chars[c] = c;
987 register_chars[c] = c;
988 operand_chars[c] = c;
989 }
3882b010 990 else if (ISUPPER (c))
252b5132 991 {
3882b010 992 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
993 register_chars[c] = mnemonic_chars[c];
994 operand_chars[c] = c;
995 }
996
3882b010 997 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
998 identifier_chars[c] = c;
999 else if (c >= 128)
1000 {
1001 identifier_chars[c] = c;
1002 operand_chars[c] = c;
1003 }
1004 }
1005
1006#ifdef LEX_AT
1007 identifier_chars['@'] = '@';
32137342
NC
1008#endif
1009#ifdef LEX_QM
1010 identifier_chars['?'] = '?';
1011 operand_chars['?'] = '?';
252b5132 1012#endif
252b5132 1013 digit_chars['-'] = '-';
791fe849 1014 mnemonic_chars['-'] = '-';
252b5132
RH
1015 identifier_chars['_'] = '_';
1016 identifier_chars['.'] = '.';
1017
1018 for (p = operand_special_chars; *p != '\0'; p++)
1019 operand_chars[(unsigned char) *p] = *p;
1020 }
1021
1022#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1023 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1024 {
1025 record_alignment (text_section, 2);
1026 record_alignment (data_section, 2);
1027 record_alignment (bss_section, 2);
1028 }
1029#endif
a4447b93
RH
1030
1031 if (flag_code == CODE_64BIT)
1032 {
1033 x86_dwarf2_return_column = 16;
1034 x86_cie_data_alignment = -8;
1035 }
1036 else
1037 {
1038 x86_dwarf2_return_column = 8;
1039 x86_cie_data_alignment = -4;
1040 }
252b5132
RH
1041}
1042
1043void
1044i386_print_statistics (file)
1045 FILE *file;
1046{
1047 hash_print_statistics (file, "i386 opcode", op_hash);
1048 hash_print_statistics (file, "i386 register", reg_hash);
1049}
1050\f
252b5132
RH
1051#ifdef DEBUG386
1052
ce8a8b2f 1053/* Debugging routines for md_assemble. */
252b5132
RH
1054static void pi PARAMS ((char *, i386_insn *));
1055static void pte PARAMS ((template *));
1056static void pt PARAMS ((unsigned int));
1057static void pe PARAMS ((expressionS *));
1058static void ps PARAMS ((symbolS *));
1059
1060static void
1061pi (line, x)
1062 char *line;
1063 i386_insn *x;
1064{
09f131f2 1065 unsigned int i;
252b5132
RH
1066
1067 fprintf (stdout, "%s: template ", line);
1068 pte (&x->tm);
09f131f2
JH
1069 fprintf (stdout, " address: base %s index %s scale %x\n",
1070 x->base_reg ? x->base_reg->reg_name : "none",
1071 x->index_reg ? x->index_reg->reg_name : "none",
1072 x->log2_scale_factor);
1073 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 1074 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
1075 fprintf (stdout, " sib: base %x index %x scale %x\n",
1076 x->sib.base, x->sib.index, x->sib.scale);
1077 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
29b0f896
AM
1078 (x->rex & REX_MODE64) != 0,
1079 (x->rex & REX_EXTX) != 0,
1080 (x->rex & REX_EXTY) != 0,
1081 (x->rex & REX_EXTZ) != 0);
252b5132
RH
1082 for (i = 0; i < x->operands; i++)
1083 {
1084 fprintf (stdout, " #%d: ", i + 1);
1085 pt (x->types[i]);
1086 fprintf (stdout, "\n");
1087 if (x->types[i]
3f4438ab 1088 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
520dc8e8 1089 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
252b5132 1090 if (x->types[i] & Imm)
520dc8e8 1091 pe (x->op[i].imms);
252b5132 1092 if (x->types[i] & Disp)
520dc8e8 1093 pe (x->op[i].disps);
252b5132
RH
1094 }
1095}
1096
1097static void
1098pte (t)
1099 template *t;
1100{
09f131f2 1101 unsigned int i;
252b5132 1102 fprintf (stdout, " %d operands ", t->operands);
47926f60 1103 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
1104 if (t->extension_opcode != None)
1105 fprintf (stdout, "ext %x ", t->extension_opcode);
1106 if (t->opcode_modifier & D)
1107 fprintf (stdout, "D");
1108 if (t->opcode_modifier & W)
1109 fprintf (stdout, "W");
1110 fprintf (stdout, "\n");
1111 for (i = 0; i < t->operands; i++)
1112 {
1113 fprintf (stdout, " #%d type ", i + 1);
1114 pt (t->operand_types[i]);
1115 fprintf (stdout, "\n");
1116 }
1117}
1118
1119static void
1120pe (e)
1121 expressionS *e;
1122{
24eab124 1123 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
1124 fprintf (stdout, " add_number %ld (%lx)\n",
1125 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
1126 if (e->X_add_symbol)
1127 {
1128 fprintf (stdout, " add_symbol ");
1129 ps (e->X_add_symbol);
1130 fprintf (stdout, "\n");
1131 }
1132 if (e->X_op_symbol)
1133 {
1134 fprintf (stdout, " op_symbol ");
1135 ps (e->X_op_symbol);
1136 fprintf (stdout, "\n");
1137 }
1138}
1139
1140static void
1141ps (s)
1142 symbolS *s;
1143{
1144 fprintf (stdout, "%s type %s%s",
1145 S_GET_NAME (s),
1146 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
1147 segment_name (S_GET_SEGMENT (s)));
1148}
1149
1150struct type_name
1151 {
1152 unsigned int mask;
1153 char *tname;
1154 }
1155
29b0f896 1156static const type_names[] =
252b5132
RH
1157{
1158 { Reg8, "r8" },
1159 { Reg16, "r16" },
1160 { Reg32, "r32" },
09f131f2 1161 { Reg64, "r64" },
252b5132
RH
1162 { Imm8, "i8" },
1163 { Imm8S, "i8s" },
1164 { Imm16, "i16" },
1165 { Imm32, "i32" },
09f131f2
JH
1166 { Imm32S, "i32s" },
1167 { Imm64, "i64" },
252b5132
RH
1168 { Imm1, "i1" },
1169 { BaseIndex, "BaseIndex" },
1170 { Disp8, "d8" },
1171 { Disp16, "d16" },
1172 { Disp32, "d32" },
09f131f2
JH
1173 { Disp32S, "d32s" },
1174 { Disp64, "d64" },
252b5132
RH
1175 { InOutPortReg, "InOutPortReg" },
1176 { ShiftCount, "ShiftCount" },
1177 { Control, "control reg" },
1178 { Test, "test reg" },
1179 { Debug, "debug reg" },
1180 { FloatReg, "FReg" },
1181 { FloatAcc, "FAcc" },
1182 { SReg2, "SReg2" },
1183 { SReg3, "SReg3" },
1184 { Acc, "Acc" },
1185 { JumpAbsolute, "Jump Absolute" },
1186 { RegMMX, "rMMX" },
3f4438ab 1187 { RegXMM, "rXMM" },
252b5132
RH
1188 { EsSeg, "es" },
1189 { 0, "" }
1190};
1191
1192static void
1193pt (t)
1194 unsigned int t;
1195{
29b0f896 1196 const struct type_name *ty;
252b5132 1197
09f131f2
JH
1198 for (ty = type_names; ty->mask; ty++)
1199 if (t & ty->mask)
1200 fprintf (stdout, "%s, ", ty->tname);
252b5132
RH
1201 fflush (stdout);
1202}
1203
1204#endif /* DEBUG386 */
1205\f
29b0f896
AM
1206static bfd_reloc_code_real_type reloc
1207 PARAMS ((int, int, int, bfd_reloc_code_real_type));
252b5132
RH
1208
1209static bfd_reloc_code_real_type
3e73aa7c 1210reloc (size, pcrel, sign, other)
252b5132
RH
1211 int size;
1212 int pcrel;
3e73aa7c 1213 int sign;
252b5132
RH
1214 bfd_reloc_code_real_type other;
1215{
47926f60
KH
1216 if (other != NO_RELOC)
1217 return other;
252b5132
RH
1218
1219 if (pcrel)
1220 {
3e73aa7c 1221 if (!sign)
e5cb08ac 1222 as_bad (_("There are no unsigned pc-relative relocations"));
252b5132
RH
1223 switch (size)
1224 {
1225 case 1: return BFD_RELOC_8_PCREL;
1226 case 2: return BFD_RELOC_16_PCREL;
1227 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 1228 case 8: return BFD_RELOC_64_PCREL;
252b5132 1229 }
d0b47220 1230 as_bad (_("can not do %d byte pc-relative relocation"), size);
252b5132
RH
1231 }
1232 else
1233 {
3e73aa7c 1234 if (sign)
e5cb08ac 1235 switch (size)
3e73aa7c
JH
1236 {
1237 case 4: return BFD_RELOC_X86_64_32S;
1238 }
1239 else
1240 switch (size)
1241 {
1242 case 1: return BFD_RELOC_8;
1243 case 2: return BFD_RELOC_16;
1244 case 4: return BFD_RELOC_32;
1245 case 8: return BFD_RELOC_64;
1246 }
1247 as_bad (_("can not do %s %d byte relocation"),
1248 sign ? "signed" : "unsigned", size);
252b5132
RH
1249 }
1250
bfb32b52 1251 abort ();
252b5132
RH
1252 return BFD_RELOC_NONE;
1253}
1254
47926f60
KH
1255/* Here we decide which fixups can be adjusted to make them relative to
1256 the beginning of the section instead of the symbol. Basically we need
1257 to make sure that the dynamic relocations are done correctly, so in
1258 some cases we force the original symbol to be used. */
1259
252b5132 1260int
c0c949c7 1261tc_i386_fix_adjustable (fixP)
31312f95 1262 fixS *fixP ATTRIBUTE_UNUSED;
252b5132 1263{
6d249963 1264#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
31312f95
AM
1265 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
1266 return 1;
1267
a161fe53
AM
1268 /* Don't adjust pc-relative references to merge sections in 64-bit
1269 mode. */
1270 if (use_rela_relocations
1271 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
1272 && fixP->fx_pcrel)
252b5132 1273 return 0;
31312f95 1274
8d01d9a9
AJ
1275 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
1276 and changed later by validate_fix. */
1277 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
1278 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
1279 return 0;
1280
ce8a8b2f 1281 /* adjust_reloc_syms doesn't know about the GOT. */
252b5132
RH
1282 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1283 || fixP->fx_r_type == BFD_RELOC_386_PLT32
1284 || fixP->fx_r_type == BFD_RELOC_386_GOT32
13ae64f3
JJ
1285 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
1286 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
1287 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
1288 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
1289 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
1290 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
1291 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
1292 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3e73aa7c
JH
1293 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
1294 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 1295 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
bffbf940
JJ
1296 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
1297 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
1298 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 1299 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
1300 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
1301 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
1302 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
1303 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
252b5132
RH
1304 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1305 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1306 return 0;
31312f95 1307#endif
252b5132
RH
1308 return 1;
1309}
252b5132 1310
29b0f896 1311static int intel_float_operand PARAMS ((const char *mnemonic));
b4cac588
AM
1312
1313static int
252b5132 1314intel_float_operand (mnemonic)
29b0f896 1315 const char *mnemonic;
252b5132 1316{
9306ca4a
JB
1317 /* Note that the value returned is meaningful only for opcodes with (memory)
1318 operands, hence the code here is free to improperly handle opcodes that
1319 have no operands (for better performance and smaller code). */
1320
1321 if (mnemonic[0] != 'f')
1322 return 0; /* non-math */
1323
1324 switch (mnemonic[1])
1325 {
1326 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
1327 the fs segment override prefix not currently handled because no
1328 call path can make opcodes without operands get here */
1329 case 'i':
1330 return 2 /* integer op */;
1331 case 'l':
1332 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
1333 return 3; /* fldcw/fldenv */
1334 break;
1335 case 'n':
1336 if (mnemonic[2] != 'o' /* fnop */)
1337 return 3; /* non-waiting control op */
1338 break;
1339 case 'r':
1340 if (mnemonic[2] == 's')
1341 return 3; /* frstor/frstpm */
1342 break;
1343 case 's':
1344 if (mnemonic[2] == 'a')
1345 return 3; /* fsave */
1346 if (mnemonic[2] == 't')
1347 {
1348 switch (mnemonic[3])
1349 {
1350 case 'c': /* fstcw */
1351 case 'd': /* fstdw */
1352 case 'e': /* fstenv */
1353 case 's': /* fsts[gw] */
1354 return 3;
1355 }
1356 }
1357 break;
1358 case 'x':
1359 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
1360 return 0; /* fxsave/fxrstor are not really math ops */
1361 break;
1362 }
252b5132 1363
9306ca4a 1364 return 1;
252b5132
RH
1365}
1366
1367/* This is the guts of the machine-dependent assembler. LINE points to a
1368 machine dependent instruction. This function is supposed to emit
1369 the frags/bytes it assembles to. */
1370
1371void
1372md_assemble (line)
1373 char *line;
1374{
252b5132 1375 int j;
252b5132
RH
1376 char mnemonic[MAX_MNEM_SIZE];
1377
47926f60 1378 /* Initialize globals. */
252b5132
RH
1379 memset (&i, '\0', sizeof (i));
1380 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 1381 i.reloc[j] = NO_RELOC;
252b5132
RH
1382 memset (disp_expressions, '\0', sizeof (disp_expressions));
1383 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 1384 save_stack_p = save_stack;
252b5132
RH
1385
1386 /* First parse an instruction mnemonic & call i386_operand for the operands.
1387 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 1388 start of a (possibly prefixed) mnemonic. */
252b5132 1389
29b0f896
AM
1390 line = parse_insn (line, mnemonic);
1391 if (line == NULL)
1392 return;
252b5132 1393
29b0f896
AM
1394 line = parse_operands (line, mnemonic);
1395 if (line == NULL)
1396 return;
252b5132 1397
29b0f896
AM
1398 /* Now we've parsed the mnemonic into a set of templates, and have the
1399 operands at hand. */
1400
1401 /* All intel opcodes have reversed operands except for "bound" and
1402 "enter". We also don't reverse intersegment "jmp" and "call"
1403 instructions with 2 immediate operands so that the immediate segment
1404 precedes the offset, as it does when in AT&T mode. "enter" and the
1405 intersegment "jmp" and "call" instructions are the only ones that
1406 have two immediate operands. */
1407 if (intel_syntax && i.operands > 1
1408 && (strcmp (mnemonic, "bound") != 0)
30123838 1409 && (strcmp (mnemonic, "invlpga") != 0)
29b0f896
AM
1410 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1411 swap_operands ();
1412
1413 if (i.imm_operands)
1414 optimize_imm ();
1415
b300c311
L
1416 /* Don't optimize displacement for movabs since it only takes 64bit
1417 displacement. */
1418 if (i.disp_operands
1419 && (flag_code != CODE_64BIT
1420 || strcmp (mnemonic, "movabs") != 0))
29b0f896
AM
1421 optimize_disp ();
1422
1423 /* Next, we find a template that matches the given insn,
1424 making sure the overlap of the given operands types is consistent
1425 with the template operand types. */
252b5132 1426
29b0f896
AM
1427 if (!match_template ())
1428 return;
252b5132 1429
cd61ebfe
AM
1430 if (intel_syntax)
1431 {
1432 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1433 if (SYSV386_COMPAT
1434 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1435 i.tm.base_opcode ^= FloatR;
1436
1437 /* Zap movzx and movsx suffix. The suffix may have been set from
1438 "word ptr" or "byte ptr" on the source operand, but we'll use
1439 the suffix later to choose the destination register. */
1440 if ((i.tm.base_opcode & ~9) == 0x0fb6)
9306ca4a
JB
1441 {
1442 if (i.reg_operands < 2
1443 && !i.suffix
1444 && (~i.tm.opcode_modifier
1445 & (No_bSuf
1446 | No_wSuf
1447 | No_lSuf
1448 | No_sSuf
1449 | No_xSuf
1450 | No_qSuf)))
1451 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
1452
1453 i.suffix = 0;
1454 }
cd61ebfe 1455 }
24eab124 1456
29b0f896
AM
1457 if (i.tm.opcode_modifier & FWait)
1458 if (!add_prefix (FWAIT_OPCODE))
1459 return;
252b5132 1460
29b0f896
AM
1461 /* Check string instruction segment overrides. */
1462 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1463 {
1464 if (!check_string ())
5dd0794d 1465 return;
29b0f896 1466 }
5dd0794d 1467
29b0f896
AM
1468 if (!process_suffix ())
1469 return;
e413e4e9 1470
29b0f896
AM
1471 /* Make still unresolved immediate matches conform to size of immediate
1472 given in i.suffix. */
1473 if (!finalize_imm ())
1474 return;
252b5132 1475
29b0f896
AM
1476 if (i.types[0] & Imm1)
1477 i.imm_operands = 0; /* kludge for shift insns. */
1478 if (i.types[0] & ImplicitRegister)
1479 i.reg_operands--;
1480 if (i.types[1] & ImplicitRegister)
1481 i.reg_operands--;
1482 if (i.types[2] & ImplicitRegister)
1483 i.reg_operands--;
252b5132 1484
29b0f896
AM
1485 if (i.tm.opcode_modifier & ImmExt)
1486 {
02fc3089
L
1487 expressionS *exp;
1488
ca164297
L
1489 if ((i.tm.cpu_flags & CpuPNI) && i.operands > 0)
1490 {
67c1ffbe 1491 /* These Intel Prescott New Instructions have the fixed
ca164297
L
1492 operands with an opcode suffix which is coded in the same
1493 place as an 8-bit immediate field would be. Here we check
1494 those operands and remove them afterwards. */
1495 unsigned int x;
1496
a4622f40 1497 for (x = 0; x < i.operands; x++)
ca164297
L
1498 if (i.op[x].regs->reg_num != x)
1499 as_bad (_("can't use register '%%%s' as operand %d in '%s'."),
1500 i.op[x].regs->reg_name, x + 1, i.tm.name);
1501 i.operands = 0;
1502 }
1503
29b0f896
AM
1504 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1505 opcode suffix which is coded in the same place as an 8-bit
1506 immediate field would be. Here we fake an 8-bit immediate
1507 operand from the opcode suffix stored in tm.extension_opcode. */
252b5132 1508
29b0f896 1509 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
252b5132 1510
29b0f896
AM
1511 exp = &im_expressions[i.imm_operands++];
1512 i.op[i.operands].imms = exp;
1513 i.types[i.operands++] = Imm8;
1514 exp->X_op = O_constant;
1515 exp->X_add_number = i.tm.extension_opcode;
1516 i.tm.extension_opcode = None;
1517 }
252b5132 1518
29b0f896
AM
1519 /* For insns with operands there are more diddles to do to the opcode. */
1520 if (i.operands)
1521 {
1522 if (!process_operands ())
1523 return;
1524 }
1525 else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1526 {
1527 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
1528 as_warn (_("translating to `%sp'"), i.tm.name);
1529 }
252b5132 1530
29b0f896
AM
1531 /* Handle conversion of 'int $3' --> special int3 insn. */
1532 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
1533 {
1534 i.tm.base_opcode = INT3_OPCODE;
1535 i.imm_operands = 0;
1536 }
252b5132 1537
29b0f896
AM
1538 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
1539 && i.op[0].disps->X_op == O_constant)
1540 {
1541 /* Convert "jmp constant" (and "call constant") to a jump (call) to
1542 the absolute address given by the constant. Since ix86 jumps and
1543 calls are pc relative, we need to generate a reloc. */
1544 i.op[0].disps->X_add_symbol = &abs_symbol;
1545 i.op[0].disps->X_op = O_symbol;
1546 }
252b5132 1547
29b0f896
AM
1548 if ((i.tm.opcode_modifier & Rex64) != 0)
1549 i.rex |= REX_MODE64;
252b5132 1550
29b0f896
AM
1551 /* For 8 bit registers we need an empty rex prefix. Also if the
1552 instruction already has a prefix, we need to convert old
1553 registers to new ones. */
773f551c 1554
29b0f896
AM
1555 if (((i.types[0] & Reg8) != 0
1556 && (i.op[0].regs->reg_flags & RegRex64) != 0)
1557 || ((i.types[1] & Reg8) != 0
1558 && (i.op[1].regs->reg_flags & RegRex64) != 0)
1559 || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
1560 && i.rex != 0))
1561 {
1562 int x;
726c5dcd 1563
29b0f896
AM
1564 i.rex |= REX_OPCODE;
1565 for (x = 0; x < 2; x++)
1566 {
1567 /* Look for 8 bit operand that uses old registers. */
1568 if ((i.types[x] & Reg8) != 0
1569 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 1570 {
29b0f896
AM
1571 /* In case it is "hi" register, give up. */
1572 if (i.op[x].regs->reg_num > 3)
0477af35 1573 as_bad (_("can't encode register '%%%s' in an instruction requiring REX prefix."),
29b0f896 1574 i.op[x].regs->reg_name);
773f551c 1575
29b0f896
AM
1576 /* Otherwise it is equivalent to the extended register.
1577 Since the encoding doesn't change this is merely
1578 cosmetic cleanup for debug output. */
1579
1580 i.op[x].regs = i.op[x].regs + 8;
773f551c 1581 }
29b0f896
AM
1582 }
1583 }
773f551c 1584
29b0f896
AM
1585 if (i.rex != 0)
1586 add_prefix (REX_OPCODE | i.rex);
1587
1588 /* We are ready to output the insn. */
1589 output_insn ();
1590}
1591
1592static char *
1593parse_insn (line, mnemonic)
1594 char *line;
1595 char *mnemonic;
1596{
1597 char *l = line;
1598 char *token_start = l;
1599 char *mnem_p;
5c6af06e
JB
1600 int supported;
1601 const template *t;
29b0f896
AM
1602
1603 /* Non-zero if we found a prefix only acceptable with string insns. */
1604 const char *expecting_string_instruction = NULL;
45288df1 1605
29b0f896
AM
1606 while (1)
1607 {
1608 mnem_p = mnemonic;
1609 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1610 {
1611 mnem_p++;
1612 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 1613 {
29b0f896
AM
1614 as_bad (_("no such instruction: `%s'"), token_start);
1615 return NULL;
1616 }
1617 l++;
1618 }
1619 if (!is_space_char (*l)
1620 && *l != END_OF_INSN
e44823cf
JB
1621 && (intel_syntax
1622 || (*l != PREFIX_SEPARATOR
1623 && *l != ',')))
29b0f896
AM
1624 {
1625 as_bad (_("invalid character %s in mnemonic"),
1626 output_invalid (*l));
1627 return NULL;
1628 }
1629 if (token_start == l)
1630 {
e44823cf 1631 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
1632 as_bad (_("expecting prefix; got nothing"));
1633 else
1634 as_bad (_("expecting mnemonic; got nothing"));
1635 return NULL;
1636 }
45288df1 1637
29b0f896
AM
1638 /* Look up instruction (or prefix) via hash table. */
1639 current_templates = hash_find (op_hash, mnemonic);
47926f60 1640
29b0f896
AM
1641 if (*l != END_OF_INSN
1642 && (!is_space_char (*l) || l[1] != END_OF_INSN)
1643 && current_templates
1644 && (current_templates->start->opcode_modifier & IsPrefix))
1645 {
1646 /* If we are in 16-bit mode, do not allow addr16 or data16.
1647 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1648 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1649 && flag_code != CODE_64BIT
1650 && (((current_templates->start->opcode_modifier & Size32) != 0)
1651 ^ (flag_code == CODE_16BIT)))
1652 {
1653 as_bad (_("redundant %s prefix"),
1654 current_templates->start->name);
1655 return NULL;
45288df1 1656 }
29b0f896
AM
1657 /* Add prefix, checking for repeated prefixes. */
1658 switch (add_prefix (current_templates->start->base_opcode))
1659 {
1660 case 0:
1661 return NULL;
1662 case 2:
1663 expecting_string_instruction = current_templates->start->name;
1664 break;
1665 }
1666 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1667 token_start = ++l;
1668 }
1669 else
1670 break;
1671 }
45288df1 1672
29b0f896
AM
1673 if (!current_templates)
1674 {
1675 /* See if we can get a match by trimming off a suffix. */
1676 switch (mnem_p[-1])
1677 {
1678 case WORD_MNEM_SUFFIX:
9306ca4a
JB
1679 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
1680 i.suffix = SHORT_MNEM_SUFFIX;
1681 else
29b0f896
AM
1682 case BYTE_MNEM_SUFFIX:
1683 case QWORD_MNEM_SUFFIX:
1684 i.suffix = mnem_p[-1];
1685 mnem_p[-1] = '\0';
1686 current_templates = hash_find (op_hash, mnemonic);
1687 break;
1688 case SHORT_MNEM_SUFFIX:
1689 case LONG_MNEM_SUFFIX:
1690 if (!intel_syntax)
1691 {
1692 i.suffix = mnem_p[-1];
1693 mnem_p[-1] = '\0';
1694 current_templates = hash_find (op_hash, mnemonic);
1695 }
1696 break;
252b5132 1697
29b0f896
AM
1698 /* Intel Syntax. */
1699 case 'd':
1700 if (intel_syntax)
1701 {
9306ca4a 1702 if (intel_float_operand (mnemonic) == 1)
29b0f896
AM
1703 i.suffix = SHORT_MNEM_SUFFIX;
1704 else
1705 i.suffix = LONG_MNEM_SUFFIX;
1706 mnem_p[-1] = '\0';
1707 current_templates = hash_find (op_hash, mnemonic);
1708 }
1709 break;
1710 }
1711 if (!current_templates)
1712 {
1713 as_bad (_("no such instruction: `%s'"), token_start);
1714 return NULL;
1715 }
1716 }
252b5132 1717
29b0f896
AM
1718 if (current_templates->start->opcode_modifier & (Jump | JumpByte))
1719 {
1720 /* Check for a branch hint. We allow ",pt" and ",pn" for
1721 predict taken and predict not taken respectively.
1722 I'm not sure that branch hints actually do anything on loop
1723 and jcxz insns (JumpByte) for current Pentium4 chips. They
1724 may work in the future and it doesn't hurt to accept them
1725 now. */
1726 if (l[0] == ',' && l[1] == 'p')
1727 {
1728 if (l[2] == 't')
1729 {
1730 if (!add_prefix (DS_PREFIX_OPCODE))
1731 return NULL;
1732 l += 3;
1733 }
1734 else if (l[2] == 'n')
1735 {
1736 if (!add_prefix (CS_PREFIX_OPCODE))
1737 return NULL;
1738 l += 3;
1739 }
1740 }
1741 }
1742 /* Any other comma loses. */
1743 if (*l == ',')
1744 {
1745 as_bad (_("invalid character %s in mnemonic"),
1746 output_invalid (*l));
1747 return NULL;
1748 }
252b5132 1749
29b0f896 1750 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
1751 supported = 0;
1752 for (t = current_templates->start; t < current_templates->end; ++t)
1753 {
1754 if (!((t->cpu_flags & ~(Cpu64 | CpuNo64))
1755 & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64))))
1756 supported |= 1;
1757 if (!(t->cpu_flags & (flag_code == CODE_64BIT ? CpuNo64 : Cpu64)))
1758 supported |= 2;
1759 }
1760 if (!(supported & 2))
1761 {
1762 as_bad (flag_code == CODE_64BIT
1763 ? _("`%s' is not supported in 64-bit mode")
1764 : _("`%s' is only supported in 64-bit mode"),
1765 current_templates->start->name);
1766 return NULL;
1767 }
1768 if (!(supported & 1))
29b0f896 1769 {
5c6af06e
JB
1770 as_warn (_("`%s' is not supported on `%s%s'"),
1771 current_templates->start->name,
1772 cpu_arch_name,
1773 cpu_sub_arch_name ? cpu_sub_arch_name : "");
29b0f896
AM
1774 }
1775 else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
1776 {
1777 as_warn (_("use .code16 to ensure correct addressing mode"));
1778 }
252b5132 1779
29b0f896 1780 /* Check for rep/repne without a string instruction. */
f41bbced 1781 if (expecting_string_instruction)
29b0f896 1782 {
f41bbced
JB
1783 static templates override;
1784
1785 for (t = current_templates->start; t < current_templates->end; ++t)
1786 if (t->opcode_modifier & IsString)
1787 break;
1788 if (t >= current_templates->end)
1789 {
1790 as_bad (_("expecting string instruction after `%s'"),
1791 expecting_string_instruction);
1792 return NULL;
1793 }
1794 for (override.start = t; t < current_templates->end; ++t)
1795 if (!(t->opcode_modifier & IsString))
1796 break;
1797 override.end = t;
1798 current_templates = &override;
29b0f896 1799 }
252b5132 1800
29b0f896
AM
1801 return l;
1802}
252b5132 1803
29b0f896
AM
1804static char *
1805parse_operands (l, mnemonic)
1806 char *l;
1807 const char *mnemonic;
1808{
1809 char *token_start;
3138f287 1810
29b0f896
AM
1811 /* 1 if operand is pending after ','. */
1812 unsigned int expecting_operand = 0;
252b5132 1813
29b0f896
AM
1814 /* Non-zero if operand parens not balanced. */
1815 unsigned int paren_not_balanced;
1816
1817 while (*l != END_OF_INSN)
1818 {
1819 /* Skip optional white space before operand. */
1820 if (is_space_char (*l))
1821 ++l;
1822 if (!is_operand_char (*l) && *l != END_OF_INSN)
1823 {
1824 as_bad (_("invalid character %s before operand %d"),
1825 output_invalid (*l),
1826 i.operands + 1);
1827 return NULL;
1828 }
1829 token_start = l; /* after white space */
1830 paren_not_balanced = 0;
1831 while (paren_not_balanced || *l != ',')
1832 {
1833 if (*l == END_OF_INSN)
1834 {
1835 if (paren_not_balanced)
1836 {
1837 if (!intel_syntax)
1838 as_bad (_("unbalanced parenthesis in operand %d."),
1839 i.operands + 1);
1840 else
1841 as_bad (_("unbalanced brackets in operand %d."),
1842 i.operands + 1);
1843 return NULL;
1844 }
1845 else
1846 break; /* we are done */
1847 }
1848 else if (!is_operand_char (*l) && !is_space_char (*l))
1849 {
1850 as_bad (_("invalid character %s in operand %d"),
1851 output_invalid (*l),
1852 i.operands + 1);
1853 return NULL;
1854 }
1855 if (!intel_syntax)
1856 {
1857 if (*l == '(')
1858 ++paren_not_balanced;
1859 if (*l == ')')
1860 --paren_not_balanced;
1861 }
1862 else
1863 {
1864 if (*l == '[')
1865 ++paren_not_balanced;
1866 if (*l == ']')
1867 --paren_not_balanced;
1868 }
1869 l++;
1870 }
1871 if (l != token_start)
1872 { /* Yes, we've read in another operand. */
1873 unsigned int operand_ok;
1874 this_operand = i.operands++;
1875 if (i.operands > MAX_OPERANDS)
1876 {
1877 as_bad (_("spurious operands; (%d operands/instruction max)"),
1878 MAX_OPERANDS);
1879 return NULL;
1880 }
1881 /* Now parse operand adding info to 'i' as we go along. */
1882 END_STRING_AND_SAVE (l);
1883
1884 if (intel_syntax)
1885 operand_ok =
1886 i386_intel_operand (token_start,
1887 intel_float_operand (mnemonic));
1888 else
1889 operand_ok = i386_operand (token_start);
1890
1891 RESTORE_END_STRING (l);
1892 if (!operand_ok)
1893 return NULL;
1894 }
1895 else
1896 {
1897 if (expecting_operand)
1898 {
1899 expecting_operand_after_comma:
1900 as_bad (_("expecting operand after ','; got nothing"));
1901 return NULL;
1902 }
1903 if (*l == ',')
1904 {
1905 as_bad (_("expecting operand before ','; got nothing"));
1906 return NULL;
1907 }
1908 }
7f3f1ea2 1909
29b0f896
AM
1910 /* Now *l must be either ',' or END_OF_INSN. */
1911 if (*l == ',')
1912 {
1913 if (*++l == END_OF_INSN)
1914 {
1915 /* Just skip it, if it's \n complain. */
1916 goto expecting_operand_after_comma;
1917 }
1918 expecting_operand = 1;
1919 }
1920 }
1921 return l;
1922}
7f3f1ea2 1923
29b0f896
AM
1924static void
1925swap_operands ()
1926{
1927 union i386_op temp_op;
1928 unsigned int temp_type;
f86103b7 1929 enum bfd_reloc_code_real temp_reloc;
29b0f896
AM
1930 int xchg1 = 0;
1931 int xchg2 = 0;
252b5132 1932
29b0f896
AM
1933 if (i.operands == 2)
1934 {
1935 xchg1 = 0;
1936 xchg2 = 1;
1937 }
1938 else if (i.operands == 3)
1939 {
1940 xchg1 = 0;
1941 xchg2 = 2;
1942 }
1943 temp_type = i.types[xchg2];
1944 i.types[xchg2] = i.types[xchg1];
1945 i.types[xchg1] = temp_type;
1946 temp_op = i.op[xchg2];
1947 i.op[xchg2] = i.op[xchg1];
1948 i.op[xchg1] = temp_op;
1949 temp_reloc = i.reloc[xchg2];
1950 i.reloc[xchg2] = i.reloc[xchg1];
1951 i.reloc[xchg1] = temp_reloc;
1952
1953 if (i.mem_operands == 2)
1954 {
1955 const seg_entry *temp_seg;
1956 temp_seg = i.seg[0];
1957 i.seg[0] = i.seg[1];
1958 i.seg[1] = temp_seg;
1959 }
1960}
252b5132 1961
29b0f896
AM
1962/* Try to ensure constant immediates are represented in the smallest
1963 opcode possible. */
1964static void
1965optimize_imm ()
1966{
1967 char guess_suffix = 0;
1968 int op;
252b5132 1969
29b0f896
AM
1970 if (i.suffix)
1971 guess_suffix = i.suffix;
1972 else if (i.reg_operands)
1973 {
1974 /* Figure out a suffix from the last register operand specified.
1975 We can't do this properly yet, ie. excluding InOutPortReg,
1976 but the following works for instructions with immediates.
1977 In any case, we can't set i.suffix yet. */
1978 for (op = i.operands; --op >= 0;)
1979 if (i.types[op] & Reg)
252b5132 1980 {
29b0f896
AM
1981 if (i.types[op] & Reg8)
1982 guess_suffix = BYTE_MNEM_SUFFIX;
1983 else if (i.types[op] & Reg16)
1984 guess_suffix = WORD_MNEM_SUFFIX;
1985 else if (i.types[op] & Reg32)
1986 guess_suffix = LONG_MNEM_SUFFIX;
1987 else if (i.types[op] & Reg64)
1988 guess_suffix = QWORD_MNEM_SUFFIX;
1989 break;
252b5132 1990 }
29b0f896
AM
1991 }
1992 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
1993 guess_suffix = WORD_MNEM_SUFFIX;
1994
1995 for (op = i.operands; --op >= 0;)
1996 if (i.types[op] & Imm)
1997 {
1998 switch (i.op[op].imms->X_op)
252b5132 1999 {
29b0f896
AM
2000 case O_constant:
2001 /* If a suffix is given, this operand may be shortened. */
2002 switch (guess_suffix)
252b5132 2003 {
29b0f896
AM
2004 case LONG_MNEM_SUFFIX:
2005 i.types[op] |= Imm32 | Imm64;
2006 break;
2007 case WORD_MNEM_SUFFIX:
2008 i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
2009 break;
2010 case BYTE_MNEM_SUFFIX:
2011 i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
2012 break;
252b5132 2013 }
252b5132 2014
29b0f896
AM
2015 /* If this operand is at most 16 bits, convert it
2016 to a signed 16 bit number before trying to see
2017 whether it will fit in an even smaller size.
2018 This allows a 16-bit operand such as $0xffe0 to
2019 be recognised as within Imm8S range. */
2020 if ((i.types[op] & Imm16)
2021 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 2022 {
29b0f896
AM
2023 i.op[op].imms->X_add_number =
2024 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
2025 }
2026 if ((i.types[op] & Imm32)
2027 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
2028 == 0))
2029 {
2030 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
2031 ^ ((offsetT) 1 << 31))
2032 - ((offsetT) 1 << 31));
2033 }
2034 i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
252b5132 2035
29b0f896
AM
2036 /* We must avoid matching of Imm32 templates when 64bit
2037 only immediate is available. */
2038 if (guess_suffix == QWORD_MNEM_SUFFIX)
2039 i.types[op] &= ~Imm32;
2040 break;
252b5132 2041
29b0f896
AM
2042 case O_absent:
2043 case O_register:
2044 abort ();
2045
2046 /* Symbols and expressions. */
2047 default:
2048 /* Convert symbolic operand to proper sizes for matching. */
2049 switch (guess_suffix)
2050 {
2051 case QWORD_MNEM_SUFFIX:
2052 i.types[op] = Imm64 | Imm32S;
2053 break;
2054 case LONG_MNEM_SUFFIX:
20f0a1fc 2055 i.types[op] = Imm32;
29b0f896
AM
2056 break;
2057 case WORD_MNEM_SUFFIX:
20f0a1fc 2058 i.types[op] = Imm16;
29b0f896
AM
2059 break;
2060 case BYTE_MNEM_SUFFIX:
20f0a1fc 2061 i.types[op] = Imm8 | Imm8S;
29b0f896 2062 break;
252b5132 2063 }
29b0f896 2064 break;
252b5132 2065 }
29b0f896
AM
2066 }
2067}
47926f60 2068
29b0f896
AM
2069/* Try to use the smallest displacement type too. */
2070static void
2071optimize_disp ()
2072{
2073 int op;
3e73aa7c 2074
29b0f896 2075 for (op = i.operands; --op >= 0;)
b300c311 2076 if (i.types[op] & Disp)
252b5132 2077 {
b300c311 2078 if (i.op[op].disps->X_op == O_constant)
252b5132 2079 {
b300c311 2080 offsetT disp = i.op[op].disps->X_add_number;
29b0f896 2081
b300c311
L
2082 if ((i.types[op] & Disp16)
2083 && (disp & ~(offsetT) 0xffff) == 0)
2084 {
2085 /* If this operand is at most 16 bits, convert
2086 to a signed 16 bit number and don't use 64bit
2087 displacement. */
2088 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
2089 i.types[op] &= ~Disp64;
2090 }
2091 if ((i.types[op] & Disp32)
2092 && (disp & ~(((offsetT) 2 << 31) - 1)) == 0)
2093 {
2094 /* If this operand is at most 32 bits, convert
2095 to a signed 32 bit number and don't use 64bit
2096 displacement. */
2097 disp &= (((offsetT) 2 << 31) - 1);
2098 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
2099 i.types[op] &= ~Disp64;
2100 }
2101 if (!disp && (i.types[op] & BaseIndex))
2102 {
2103 i.types[op] &= ~Disp;
2104 i.op[op].disps = 0;
2105 i.disp_operands--;
2106 }
2107 else if (flag_code == CODE_64BIT)
2108 {
2109 if (fits_in_signed_long (disp))
2110 i.types[op] |= Disp32S;
2111 if (fits_in_unsigned_long (disp))
2112 i.types[op] |= Disp32;
2113 }
2114 if ((i.types[op] & (Disp32 | Disp32S | Disp16))
2115 && fits_in_signed_byte (disp))
2116 i.types[op] |= Disp8;
252b5132 2117 }
b300c311
L
2118 else
2119 /* We only support 64bit displacement on constants. */
2120 i.types[op] &= ~Disp64;
252b5132 2121 }
29b0f896
AM
2122}
2123
2124static int
2125match_template ()
2126{
2127 /* Points to template once we've found it. */
2128 const template *t;
2129 unsigned int overlap0, overlap1, overlap2;
2130 unsigned int found_reverse_match;
2131 int suffix_check;
2132
2133#define MATCH(overlap, given, template) \
2134 ((overlap & ~JumpAbsolute) \
2135 && (((given) & (BaseIndex | JumpAbsolute)) \
2136 == ((overlap) & (BaseIndex | JumpAbsolute))))
2137
2138 /* If given types r0 and r1 are registers they must be of the same type
2139 unless the expected operand type register overlap is null.
2140 Note that Acc in a template matches every size of reg. */
2141#define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
2142 (((g0) & Reg) == 0 || ((g1) & Reg) == 0 \
2143 || ((g0) & Reg) == ((g1) & Reg) \
2144 || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
2145
2146 overlap0 = 0;
2147 overlap1 = 0;
2148 overlap2 = 0;
2149 found_reverse_match = 0;
2150 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
2151 ? No_bSuf
2152 : (i.suffix == WORD_MNEM_SUFFIX
2153 ? No_wSuf
2154 : (i.suffix == SHORT_MNEM_SUFFIX
2155 ? No_sSuf
2156 : (i.suffix == LONG_MNEM_SUFFIX
2157 ? No_lSuf
2158 : (i.suffix == QWORD_MNEM_SUFFIX
2159 ? No_qSuf
2160 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
2161 ? No_xSuf : 0))))));
2162
20f0a1fc
NC
2163 t = current_templates->start;
2164 if (i.suffix == QWORD_MNEM_SUFFIX
2165 && flag_code != CODE_64BIT
9306ca4a
JB
2166 && (intel_syntax
2167 ? !(t->opcode_modifier & IgnoreSize)
2168 && !intel_float_operand (t->name)
2169 : intel_float_operand (t->name) != 2)
20f0a1fc
NC
2170 && (!(t->operand_types[0] & (RegMMX | RegXMM))
2171 || !(t->operand_types[t->operands > 1] & (RegMMX | RegXMM)))
2172 && (t->base_opcode != 0x0fc7
2173 || t->extension_opcode != 1 /* cmpxchg8b */))
2174 t = current_templates->end;
2175 for (; t < current_templates->end; t++)
29b0f896
AM
2176 {
2177 /* Must have right number of operands. */
2178 if (i.operands != t->operands)
2179 continue;
2180
2181 /* Check the suffix, except for some instructions in intel mode. */
2182 if ((t->opcode_modifier & suffix_check)
2183 && !(intel_syntax
9306ca4a 2184 && (t->opcode_modifier & IgnoreSize)))
29b0f896
AM
2185 continue;
2186
2187 /* Do not verify operands when there are none. */
2188 else if (!t->operands)
2189 {
2190 if (t->cpu_flags & ~cpu_arch_flags)
2191 continue;
2192 /* We've found a match; break out of loop. */
2193 break;
2194 }
252b5132 2195
29b0f896
AM
2196 overlap0 = i.types[0] & t->operand_types[0];
2197 switch (t->operands)
2198 {
2199 case 1:
2200 if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
2201 continue;
2202 break;
2203 case 2:
2204 case 3:
2205 overlap1 = i.types[1] & t->operand_types[1];
2206 if (!MATCH (overlap0, i.types[0], t->operand_types[0])
2207 || !MATCH (overlap1, i.types[1], t->operand_types[1])
2208 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2209 t->operand_types[0],
2210 overlap1, i.types[1],
2211 t->operand_types[1]))
2212 {
2213 /* Check if other direction is valid ... */
2214 if ((t->opcode_modifier & (D | FloatD)) == 0)
2215 continue;
2216
2217 /* Try reversing direction of operands. */
2218 overlap0 = i.types[0] & t->operand_types[1];
2219 overlap1 = i.types[1] & t->operand_types[0];
2220 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
2221 || !MATCH (overlap1, i.types[1], t->operand_types[0])
2222 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2223 t->operand_types[1],
2224 overlap1, i.types[1],
2225 t->operand_types[0]))
2226 {
2227 /* Does not match either direction. */
2228 continue;
2229 }
2230 /* found_reverse_match holds which of D or FloatDR
2231 we've found. */
2232 found_reverse_match = t->opcode_modifier & (D | FloatDR);
2233 }
2234 /* Found a forward 2 operand match here. */
2235 else if (t->operands == 3)
2236 {
2237 /* Here we make use of the fact that there are no
2238 reverse match 3 operand instructions, and all 3
2239 operand instructions only need to be checked for
2240 register consistency between operands 2 and 3. */
2241 overlap2 = i.types[2] & t->operand_types[2];
2242 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
2243 || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
2244 t->operand_types[1],
2245 overlap2, i.types[2],
2246 t->operand_types[2]))
2247
2248 continue;
2249 }
2250 /* Found either forward/reverse 2 or 3 operand match here:
2251 slip through to break. */
2252 }
2253 if (t->cpu_flags & ~cpu_arch_flags)
2254 {
2255 found_reverse_match = 0;
2256 continue;
2257 }
2258 /* We've found a match; break out of loop. */
2259 break;
2260 }
2261
2262 if (t == current_templates->end)
2263 {
2264 /* We found no match. */
2265 as_bad (_("suffix or operands invalid for `%s'"),
2266 current_templates->start->name);
2267 return 0;
2268 }
252b5132 2269
29b0f896
AM
2270 if (!quiet_warnings)
2271 {
2272 if (!intel_syntax
2273 && ((i.types[0] & JumpAbsolute)
2274 != (t->operand_types[0] & JumpAbsolute)))
2275 {
2276 as_warn (_("indirect %s without `*'"), t->name);
2277 }
2278
2279 if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
2280 == (IsPrefix | IgnoreSize))
2281 {
2282 /* Warn them that a data or address size prefix doesn't
2283 affect assembly of the next line of code. */
2284 as_warn (_("stand-alone `%s' prefix"), t->name);
2285 }
2286 }
2287
2288 /* Copy the template we found. */
2289 i.tm = *t;
2290 if (found_reverse_match)
2291 {
2292 /* If we found a reverse match we must alter the opcode
2293 direction bit. found_reverse_match holds bits to change
2294 (different for int & float insns). */
2295
2296 i.tm.base_opcode ^= found_reverse_match;
2297
2298 i.tm.operand_types[0] = t->operand_types[1];
2299 i.tm.operand_types[1] = t->operand_types[0];
2300 }
2301
2302 return 1;
2303}
2304
2305static int
2306check_string ()
2307{
2308 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
2309 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
2310 {
2311 if (i.seg[0] != NULL && i.seg[0] != &es)
2312 {
2313 as_bad (_("`%s' operand %d must use `%%es' segment"),
2314 i.tm.name,
2315 mem_op + 1);
2316 return 0;
2317 }
2318 /* There's only ever one segment override allowed per instruction.
2319 This instruction possibly has a legal segment override on the
2320 second operand, so copy the segment to where non-string
2321 instructions store it, allowing common code. */
2322 i.seg[0] = i.seg[1];
2323 }
2324 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
2325 {
2326 if (i.seg[1] != NULL && i.seg[1] != &es)
2327 {
2328 as_bad (_("`%s' operand %d must use `%%es' segment"),
2329 i.tm.name,
2330 mem_op + 2);
2331 return 0;
2332 }
2333 }
2334 return 1;
2335}
2336
2337static int
543613e9 2338process_suffix (void)
29b0f896
AM
2339{
2340 /* If matched instruction specifies an explicit instruction mnemonic
2341 suffix, use it. */
2342 if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
2343 {
2344 if (i.tm.opcode_modifier & Size16)
2345 i.suffix = WORD_MNEM_SUFFIX;
2346 else if (i.tm.opcode_modifier & Size64)
2347 i.suffix = QWORD_MNEM_SUFFIX;
2348 else
2349 i.suffix = LONG_MNEM_SUFFIX;
2350 }
2351 else if (i.reg_operands)
2352 {
2353 /* If there's no instruction mnemonic suffix we try to invent one
2354 based on register operands. */
2355 if (!i.suffix)
2356 {
2357 /* We take i.suffix from the last register operand specified,
2358 Destination register type is more significant than source
2359 register type. */
2360 int op;
543613e9 2361
29b0f896
AM
2362 for (op = i.operands; --op >= 0;)
2363 if ((i.types[op] & Reg)
2364 && !(i.tm.operand_types[op] & InOutPortReg))
2365 {
2366 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
2367 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
2368 (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
2369 LONG_MNEM_SUFFIX);
2370 break;
2371 }
2372 }
2373 else if (i.suffix == BYTE_MNEM_SUFFIX)
2374 {
2375 if (!check_byte_reg ())
2376 return 0;
2377 }
2378 else if (i.suffix == LONG_MNEM_SUFFIX)
2379 {
2380 if (!check_long_reg ())
2381 return 0;
2382 }
2383 else if (i.suffix == QWORD_MNEM_SUFFIX)
2384 {
2385 if (!check_qword_reg ())
2386 return 0;
2387 }
2388 else if (i.suffix == WORD_MNEM_SUFFIX)
2389 {
2390 if (!check_word_reg ())
2391 return 0;
2392 }
2393 else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
2394 /* Do nothing if the instruction is going to ignore the prefix. */
2395 ;
2396 else
2397 abort ();
2398 }
9306ca4a
JB
2399 else if ((i.tm.opcode_modifier & DefaultSize)
2400 && !i.suffix
2401 /* exclude fldenv/frstor/fsave/fstenv */
2402 && (i.tm.opcode_modifier & No_sSuf))
29b0f896
AM
2403 {
2404 i.suffix = stackop_size;
2405 }
9306ca4a
JB
2406 else if (intel_syntax
2407 && !i.suffix
2408 && ((i.tm.operand_types[0] & JumpAbsolute)
2409 || (i.tm.opcode_modifier & (JumpByte|JumpInterSegment))
2410 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
2411 && i.tm.extension_opcode <= 3)))
2412 {
2413 switch (flag_code)
2414 {
2415 case CODE_64BIT:
2416 if (!(i.tm.opcode_modifier & No_qSuf))
2417 {
2418 i.suffix = QWORD_MNEM_SUFFIX;
2419 break;
2420 }
2421 case CODE_32BIT:
2422 if (!(i.tm.opcode_modifier & No_lSuf))
2423 i.suffix = LONG_MNEM_SUFFIX;
2424 break;
2425 case CODE_16BIT:
2426 if (!(i.tm.opcode_modifier & No_wSuf))
2427 i.suffix = WORD_MNEM_SUFFIX;
2428 break;
2429 }
2430 }
252b5132 2431
9306ca4a 2432 if (!i.suffix)
29b0f896 2433 {
9306ca4a
JB
2434 if (!intel_syntax)
2435 {
2436 if (i.tm.opcode_modifier & W)
2437 {
2438 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
2439 return 0;
2440 }
2441 }
2442 else
2443 {
2444 unsigned int suffixes = ~i.tm.opcode_modifier
2445 & (No_bSuf
2446 | No_wSuf
2447 | No_lSuf
2448 | No_sSuf
2449 | No_xSuf
2450 | No_qSuf);
2451
2452 if ((i.tm.opcode_modifier & W)
2453 || ((suffixes & (suffixes - 1))
2454 && !(i.tm.opcode_modifier & (DefaultSize | IgnoreSize))))
2455 {
2456 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
2457 return 0;
2458 }
2459 }
29b0f896 2460 }
252b5132 2461
9306ca4a
JB
2462 /* Change the opcode based on the operand size given by i.suffix;
2463 We don't need to change things for byte insns. */
2464
29b0f896
AM
2465 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
2466 {
2467 /* It's not a byte, select word/dword operation. */
2468 if (i.tm.opcode_modifier & W)
2469 {
2470 if (i.tm.opcode_modifier & ShortForm)
2471 i.tm.base_opcode |= 8;
2472 else
2473 i.tm.base_opcode |= 1;
2474 }
0f3f3d8b 2475
29b0f896
AM
2476 /* Now select between word & dword operations via the operand
2477 size prefix, except for instructions that will ignore this
2478 prefix anyway. */
2479 if (i.suffix != QWORD_MNEM_SUFFIX
9306ca4a
JB
2480 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
2481 && !(i.tm.opcode_modifier & (IgnoreSize | FloatMF))
9146926a
AM
2482 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
2483 || (flag_code == CODE_64BIT
2484 && (i.tm.opcode_modifier & JumpByte))))
24eab124
AM
2485 {
2486 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 2487
29b0f896
AM
2488 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
2489 prefix = ADDR_PREFIX_OPCODE;
252b5132 2490
29b0f896
AM
2491 if (!add_prefix (prefix))
2492 return 0;
24eab124 2493 }
252b5132 2494
29b0f896
AM
2495 /* Set mode64 for an operand. */
2496 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 2497 && flag_code == CODE_64BIT
29b0f896 2498 && (i.tm.opcode_modifier & NoRex64) == 0)
9146926a 2499 i.rex |= REX_MODE64;
3e73aa7c 2500
29b0f896
AM
2501 /* Size floating point instruction. */
2502 if (i.suffix == LONG_MNEM_SUFFIX)
543613e9
NC
2503 if (i.tm.opcode_modifier & FloatMF)
2504 i.tm.base_opcode ^= 4;
29b0f896 2505 }
7ecd2f8b 2506
29b0f896
AM
2507 return 1;
2508}
3e73aa7c 2509
29b0f896 2510static int
543613e9 2511check_byte_reg (void)
29b0f896
AM
2512{
2513 int op;
543613e9 2514
29b0f896
AM
2515 for (op = i.operands; --op >= 0;)
2516 {
2517 /* If this is an eight bit register, it's OK. If it's the 16 or
2518 32 bit version of an eight bit register, we will just use the
2519 low portion, and that's OK too. */
2520 if (i.types[op] & Reg8)
2521 continue;
2522
2523 /* movzx and movsx should not generate this warning. */
2524 if (intel_syntax
2525 && (i.tm.base_opcode == 0xfb7
2526 || i.tm.base_opcode == 0xfb6
2527 || i.tm.base_opcode == 0x63
2528 || i.tm.base_opcode == 0xfbe
2529 || i.tm.base_opcode == 0xfbf))
2530 continue;
2531
65ec77d2 2532 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4)
29b0f896
AM
2533 {
2534 /* Prohibit these changes in the 64bit mode, since the
2535 lowering is more complicated. */
2536 if (flag_code == CODE_64BIT
2537 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2538 {
0f3f3d8b 2539 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
29b0f896
AM
2540 i.op[op].regs->reg_name,
2541 i.suffix);
2542 return 0;
2543 }
2544#if REGISTER_WARNINGS
2545 if (!quiet_warnings
2546 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2547 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2548 (i.op[op].regs + (i.types[op] & Reg16
2549 ? REGNAM_AL - REGNAM_AX
2550 : REGNAM_AL - REGNAM_EAX))->reg_name,
2551 i.op[op].regs->reg_name,
2552 i.suffix);
2553#endif
2554 continue;
2555 }
2556 /* Any other register is bad. */
2557 if (i.types[op] & (Reg | RegMMX | RegXMM
2558 | SReg2 | SReg3
2559 | Control | Debug | Test
2560 | FloatReg | FloatAcc))
2561 {
2562 as_bad (_("`%%%s' not allowed with `%s%c'"),
2563 i.op[op].regs->reg_name,
2564 i.tm.name,
2565 i.suffix);
2566 return 0;
2567 }
2568 }
2569 return 1;
2570}
2571
2572static int
2573check_long_reg ()
2574{
2575 int op;
2576
2577 for (op = i.operands; --op >= 0;)
2578 /* Reject eight bit registers, except where the template requires
2579 them. (eg. movzb) */
2580 if ((i.types[op] & Reg8) != 0
2581 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2582 {
2583 as_bad (_("`%%%s' not allowed with `%s%c'"),
2584 i.op[op].regs->reg_name,
2585 i.tm.name,
2586 i.suffix);
2587 return 0;
2588 }
2589 /* Warn if the e prefix on a general reg is missing. */
2590 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2591 && (i.types[op] & Reg16) != 0
2592 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2593 {
2594 /* Prohibit these changes in the 64bit mode, since the
2595 lowering is more complicated. */
2596 if (flag_code == CODE_64BIT)
252b5132 2597 {
0f3f3d8b 2598 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
29b0f896
AM
2599 i.op[op].regs->reg_name,
2600 i.suffix);
2601 return 0;
252b5132 2602 }
29b0f896
AM
2603#if REGISTER_WARNINGS
2604 else
2605 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2606 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
2607 i.op[op].regs->reg_name,
2608 i.suffix);
2609#endif
252b5132 2610 }
29b0f896
AM
2611 /* Warn if the r prefix on a general reg is missing. */
2612 else if ((i.types[op] & Reg64) != 0
2613 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
252b5132 2614 {
0f3f3d8b 2615 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
29b0f896
AM
2616 i.op[op].regs->reg_name,
2617 i.suffix);
2618 return 0;
2619 }
2620 return 1;
2621}
252b5132 2622
29b0f896
AM
2623static int
2624check_qword_reg ()
2625{
2626 int op;
252b5132 2627
29b0f896
AM
2628 for (op = i.operands; --op >= 0; )
2629 /* Reject eight bit registers, except where the template requires
2630 them. (eg. movzb) */
2631 if ((i.types[op] & Reg8) != 0
2632 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2633 {
2634 as_bad (_("`%%%s' not allowed with `%s%c'"),
2635 i.op[op].regs->reg_name,
2636 i.tm.name,
2637 i.suffix);
2638 return 0;
2639 }
2640 /* Warn if the e prefix on a general reg is missing. */
2641 else if (((i.types[op] & Reg16) != 0
2642 || (i.types[op] & Reg32) != 0)
2643 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2644 {
2645 /* Prohibit these changes in the 64bit mode, since the
2646 lowering is more complicated. */
0f3f3d8b 2647 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
29b0f896
AM
2648 i.op[op].regs->reg_name,
2649 i.suffix);
2650 return 0;
252b5132 2651 }
29b0f896
AM
2652 return 1;
2653}
252b5132 2654
29b0f896
AM
2655static int
2656check_word_reg ()
2657{
2658 int op;
2659 for (op = i.operands; --op >= 0;)
2660 /* Reject eight bit registers, except where the template requires
2661 them. (eg. movzb) */
2662 if ((i.types[op] & Reg8) != 0
2663 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2664 {
2665 as_bad (_("`%%%s' not allowed with `%s%c'"),
2666 i.op[op].regs->reg_name,
2667 i.tm.name,
2668 i.suffix);
2669 return 0;
2670 }
2671 /* Warn if the e prefix on a general reg is present. */
2672 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2673 && (i.types[op] & Reg32) != 0
2674 && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
252b5132 2675 {
29b0f896
AM
2676 /* Prohibit these changes in the 64bit mode, since the
2677 lowering is more complicated. */
2678 if (flag_code == CODE_64BIT)
252b5132 2679 {
0f3f3d8b 2680 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
29b0f896
AM
2681 i.op[op].regs->reg_name,
2682 i.suffix);
2683 return 0;
252b5132 2684 }
29b0f896
AM
2685 else
2686#if REGISTER_WARNINGS
2687 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2688 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
2689 i.op[op].regs->reg_name,
2690 i.suffix);
2691#endif
2692 }
2693 return 1;
2694}
252b5132 2695
29b0f896
AM
2696static int
2697finalize_imm ()
2698{
2699 unsigned int overlap0, overlap1, overlap2;
2700
2701 overlap0 = i.types[0] & i.tm.operand_types[0];
20f0a1fc 2702 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64))
29b0f896
AM
2703 && overlap0 != Imm8 && overlap0 != Imm8S
2704 && overlap0 != Imm16 && overlap0 != Imm32S
2705 && overlap0 != Imm32 && overlap0 != Imm64)
2706 {
2707 if (i.suffix)
2708 {
2709 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
2710 ? Imm8 | Imm8S
2711 : (i.suffix == WORD_MNEM_SUFFIX
2712 ? Imm16
2713 : (i.suffix == QWORD_MNEM_SUFFIX
2714 ? Imm64 | Imm32S
2715 : Imm32)));
2716 }
2717 else if (overlap0 == (Imm16 | Imm32S | Imm32)
2718 || overlap0 == (Imm16 | Imm32)
2719 || overlap0 == (Imm16 | Imm32S))
2720 {
2721 overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2722 ? Imm16 : Imm32S);
2723 }
2724 if (overlap0 != Imm8 && overlap0 != Imm8S
2725 && overlap0 != Imm16 && overlap0 != Imm32S
2726 && overlap0 != Imm32 && overlap0 != Imm64)
2727 {
2728 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
2729 return 0;
2730 }
2731 }
2732 i.types[0] = overlap0;
2733
2734 overlap1 = i.types[1] & i.tm.operand_types[1];
37edbb65 2735 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32 | Imm64))
29b0f896
AM
2736 && overlap1 != Imm8 && overlap1 != Imm8S
2737 && overlap1 != Imm16 && overlap1 != Imm32S
2738 && overlap1 != Imm32 && overlap1 != Imm64)
2739 {
2740 if (i.suffix)
2741 {
2742 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
2743 ? Imm8 | Imm8S
2744 : (i.suffix == WORD_MNEM_SUFFIX
2745 ? Imm16
2746 : (i.suffix == QWORD_MNEM_SUFFIX
2747 ? Imm64 | Imm32S
2748 : Imm32)));
2749 }
2750 else if (overlap1 == (Imm16 | Imm32 | Imm32S)
2751 || overlap1 == (Imm16 | Imm32)
2752 || overlap1 == (Imm16 | Imm32S))
2753 {
2754 overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2755 ? Imm16 : Imm32S);
2756 }
2757 if (overlap1 != Imm8 && overlap1 != Imm8S
2758 && overlap1 != Imm16 && overlap1 != Imm32S
2759 && overlap1 != Imm32 && overlap1 != Imm64)
2760 {
2761 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
2762 return 0;
2763 }
2764 }
2765 i.types[1] = overlap1;
2766
2767 overlap2 = i.types[2] & i.tm.operand_types[2];
2768 assert ((overlap2 & Imm) == 0);
2769 i.types[2] = overlap2;
2770
2771 return 1;
2772}
2773
2774static int
2775process_operands ()
2776{
2777 /* Default segment register this instruction will use for memory
2778 accesses. 0 means unknown. This is only for optimizing out
2779 unnecessary segment overrides. */
2780 const seg_entry *default_seg = 0;
2781
2782 /* The imul $imm, %reg instruction is converted into
2783 imul $imm, %reg, %reg, and the clr %reg instruction
2784 is converted into xor %reg, %reg. */
2785 if (i.tm.opcode_modifier & regKludge)
2786 {
2787 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
2788 /* Pretend we saw the extra register operand. */
2789 assert (i.op[first_reg_op + 1].regs == 0);
2790 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
2791 i.types[first_reg_op + 1] = i.types[first_reg_op];
2792 i.reg_operands = 2;
2793 }
2794
2795 if (i.tm.opcode_modifier & ShortForm)
2796 {
2797 /* The register or float register operand is in operand 0 or 1. */
2798 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
2799 /* Register goes in low 3 bits of opcode. */
2800 i.tm.base_opcode |= i.op[op].regs->reg_num;
2801 if ((i.op[op].regs->reg_flags & RegRex) != 0)
2802 i.rex |= REX_EXTZ;
2803 if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
2804 {
2805 /* Warn about some common errors, but press on regardless.
2806 The first case can be generated by gcc (<= 2.8.1). */
2807 if (i.operands == 2)
2808 {
2809 /* Reversed arguments on faddp, fsubp, etc. */
2810 as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
2811 i.op[1].regs->reg_name,
2812 i.op[0].regs->reg_name);
2813 }
2814 else
2815 {
2816 /* Extraneous `l' suffix on fp insn. */
2817 as_warn (_("translating to `%s %%%s'"), i.tm.name,
2818 i.op[0].regs->reg_name);
2819 }
2820 }
2821 }
2822 else if (i.tm.opcode_modifier & Modrm)
2823 {
2824 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
2825 must be put into the modrm byte). Now, we make the modrm and
2826 index base bytes based on all the info we've collected. */
29b0f896
AM
2827
2828 default_seg = build_modrm_byte ();
2829 }
2830 else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2831 {
2832 if (i.tm.base_opcode == POP_SEG_SHORT
2833 && i.op[0].regs->reg_num == 1)
2834 {
2835 as_bad (_("you can't `pop %%cs'"));
2836 return 0;
2837 }
2838 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2839 if ((i.op[0].regs->reg_flags & RegRex) != 0)
2840 i.rex |= REX_EXTZ;
2841 }
2842 else if ((i.tm.base_opcode & ~(D | W)) == MOV_AX_DISP32)
2843 {
2844 default_seg = &ds;
2845 }
2846 else if ((i.tm.opcode_modifier & IsString) != 0)
2847 {
2848 /* For the string instructions that allow a segment override
2849 on one of their operands, the default segment is ds. */
2850 default_seg = &ds;
2851 }
2852
30123838
JB
2853 if ((i.tm.base_opcode == 0x8d /* lea */
2854 || (i.tm.cpu_flags & CpuSVME))
2855 && i.seg[0] && !quiet_warnings)
2856 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
52271982
AM
2857
2858 /* If a segment was explicitly specified, and the specified segment
2859 is not the default, use an opcode prefix to select it. If we
2860 never figured out what the default segment is, then default_seg
2861 will be zero at this point, and the specified segment prefix will
2862 always be used. */
29b0f896
AM
2863 if ((i.seg[0]) && (i.seg[0] != default_seg))
2864 {
2865 if (!add_prefix (i.seg[0]->seg_prefix))
2866 return 0;
2867 }
2868 return 1;
2869}
2870
2871static const seg_entry *
2872build_modrm_byte ()
2873{
2874 const seg_entry *default_seg = 0;
2875
2876 /* i.reg_operands MUST be the number of real register operands;
2877 implicit registers do not count. */
2878 if (i.reg_operands == 2)
2879 {
2880 unsigned int source, dest;
2881 source = ((i.types[0]
2882 & (Reg | RegMMX | RegXMM
2883 | SReg2 | SReg3
2884 | Control | Debug | Test))
2885 ? 0 : 1);
2886 dest = source + 1;
2887
2888 i.rm.mode = 3;
2889 /* One of the register operands will be encoded in the i.tm.reg
2890 field, the other in the combined i.tm.mode and i.tm.regmem
2891 fields. If no form of this instruction supports a memory
2892 destination operand, then we assume the source operand may
2893 sometimes be a memory operand and so we need to store the
2894 destination in the i.rm.reg field. */
2895 if ((i.tm.operand_types[dest] & AnyMem) == 0)
2896 {
2897 i.rm.reg = i.op[dest].regs->reg_num;
2898 i.rm.regmem = i.op[source].regs->reg_num;
2899 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2900 i.rex |= REX_EXTX;
2901 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2902 i.rex |= REX_EXTZ;
2903 }
2904 else
2905 {
2906 i.rm.reg = i.op[source].regs->reg_num;
2907 i.rm.regmem = i.op[dest].regs->reg_num;
2908 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2909 i.rex |= REX_EXTZ;
2910 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2911 i.rex |= REX_EXTX;
2912 }
c4a530c5
JB
2913 if (flag_code != CODE_64BIT && (i.rex & (REX_EXTX | REX_EXTZ)))
2914 {
2915 if (!((i.types[0] | i.types[1]) & Control))
2916 abort ();
2917 i.rex &= ~(REX_EXTX | REX_EXTZ);
2918 add_prefix (LOCK_PREFIX_OPCODE);
2919 }
29b0f896
AM
2920 }
2921 else
2922 { /* If it's not 2 reg operands... */
2923 if (i.mem_operands)
2924 {
2925 unsigned int fake_zero_displacement = 0;
2926 unsigned int op = ((i.types[0] & AnyMem)
2927 ? 0
2928 : (i.types[1] & AnyMem) ? 1 : 2);
2929
2930 default_seg = &ds;
2931
2932 if (i.base_reg == 0)
2933 {
2934 i.rm.mode = 0;
2935 if (!i.disp_operands)
2936 fake_zero_displacement = 1;
2937 if (i.index_reg == 0)
2938 {
2939 /* Operand is just <disp> */
20f0a1fc 2940 if (flag_code == CODE_64BIT)
29b0f896
AM
2941 {
2942 /* 64bit mode overwrites the 32bit absolute
2943 addressing by RIP relative addressing and
2944 absolute addressing is encoded by one of the
2945 redundant SIB forms. */
2946 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2947 i.sib.base = NO_BASE_REGISTER;
2948 i.sib.index = NO_INDEX_REGISTER;
20f0a1fc
NC
2949 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0) ? Disp32S : Disp32);
2950 }
2951 else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
2952 {
2953 i.rm.regmem = NO_BASE_REGISTER_16;
2954 i.types[op] = Disp16;
2955 }
2956 else
2957 {
2958 i.rm.regmem = NO_BASE_REGISTER;
2959 i.types[op] = Disp32;
29b0f896
AM
2960 }
2961 }
2962 else /* !i.base_reg && i.index_reg */
2963 {
2964 i.sib.index = i.index_reg->reg_num;
2965 i.sib.base = NO_BASE_REGISTER;
2966 i.sib.scale = i.log2_scale_factor;
2967 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2968 i.types[op] &= ~Disp;
2969 if (flag_code != CODE_64BIT)
2970 i.types[op] |= Disp32; /* Must be 32 bit */
2971 else
2972 i.types[op] |= Disp32S;
2973 if ((i.index_reg->reg_flags & RegRex) != 0)
2974 i.rex |= REX_EXTY;
2975 }
2976 }
2977 /* RIP addressing for 64bit mode. */
2978 else if (i.base_reg->reg_type == BaseIndex)
2979 {
2980 i.rm.regmem = NO_BASE_REGISTER;
20f0a1fc 2981 i.types[op] &= ~ Disp;
29b0f896
AM
2982 i.types[op] |= Disp32S;
2983 i.flags[op] = Operand_PCrel;
20f0a1fc
NC
2984 if (! i.disp_operands)
2985 fake_zero_displacement = 1;
29b0f896
AM
2986 }
2987 else if (i.base_reg->reg_type & Reg16)
2988 {
2989 switch (i.base_reg->reg_num)
2990 {
2991 case 3: /* (%bx) */
2992 if (i.index_reg == 0)
2993 i.rm.regmem = 7;
2994 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
2995 i.rm.regmem = i.index_reg->reg_num - 6;
2996 break;
2997 case 5: /* (%bp) */
2998 default_seg = &ss;
2999 if (i.index_reg == 0)
3000 {
3001 i.rm.regmem = 6;
3002 if ((i.types[op] & Disp) == 0)
3003 {
3004 /* fake (%bp) into 0(%bp) */
3005 i.types[op] |= Disp8;
252b5132 3006 fake_zero_displacement = 1;
29b0f896
AM
3007 }
3008 }
3009 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
3010 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
3011 break;
3012 default: /* (%si) -> 4 or (%di) -> 5 */
3013 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
3014 }
3015 i.rm.mode = mode_from_disp_size (i.types[op]);
3016 }
3017 else /* i.base_reg and 32/64 bit mode */
3018 {
3019 if (flag_code == CODE_64BIT
3020 && (i.types[op] & Disp))
20f0a1fc
NC
3021 i.types[op] = (i.types[op] & Disp8) | (i.prefix[ADDR_PREFIX] == 0 ? Disp32S : Disp32);
3022
29b0f896
AM
3023 i.rm.regmem = i.base_reg->reg_num;
3024 if ((i.base_reg->reg_flags & RegRex) != 0)
3025 i.rex |= REX_EXTZ;
3026 i.sib.base = i.base_reg->reg_num;
3027 /* x86-64 ignores REX prefix bit here to avoid decoder
3028 complications. */
3029 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
3030 {
3031 default_seg = &ss;
3032 if (i.disp_operands == 0)
3033 {
3034 fake_zero_displacement = 1;
3035 i.types[op] |= Disp8;
3036 }
3037 }
3038 else if (i.base_reg->reg_num == ESP_REG_NUM)
3039 {
3040 default_seg = &ss;
3041 }
3042 i.sib.scale = i.log2_scale_factor;
3043 if (i.index_reg == 0)
3044 {
3045 /* <disp>(%esp) becomes two byte modrm with no index
3046 register. We've already stored the code for esp
3047 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
3048 Any base register besides %esp will not use the
3049 extra modrm byte. */
3050 i.sib.index = NO_INDEX_REGISTER;
3051#if !SCALE1_WHEN_NO_INDEX
3052 /* Another case where we force the second modrm byte. */
3053 if (i.log2_scale_factor)
3054 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
252b5132 3055#endif
29b0f896
AM
3056 }
3057 else
3058 {
3059 i.sib.index = i.index_reg->reg_num;
3060 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3061 if ((i.index_reg->reg_flags & RegRex) != 0)
3062 i.rex |= REX_EXTY;
3063 }
3064 i.rm.mode = mode_from_disp_size (i.types[op]);
3065 }
252b5132 3066
29b0f896
AM
3067 if (fake_zero_displacement)
3068 {
3069 /* Fakes a zero displacement assuming that i.types[op]
3070 holds the correct displacement size. */
3071 expressionS *exp;
3072
3073 assert (i.op[op].disps == 0);
3074 exp = &disp_expressions[i.disp_operands++];
3075 i.op[op].disps = exp;
3076 exp->X_op = O_constant;
3077 exp->X_add_number = 0;
3078 exp->X_add_symbol = (symbolS *) 0;
3079 exp->X_op_symbol = (symbolS *) 0;
3080 }
3081 }
252b5132 3082
29b0f896
AM
3083 /* Fill in i.rm.reg or i.rm.regmem field with register operand
3084 (if any) based on i.tm.extension_opcode. Again, we must be
3085 careful to make sure that segment/control/debug/test/MMX
3086 registers are coded into the i.rm.reg field. */
3087 if (i.reg_operands)
3088 {
3089 unsigned int op =
3090 ((i.types[0]
3091 & (Reg | RegMMX | RegXMM
3092 | SReg2 | SReg3
3093 | Control | Debug | Test))
3094 ? 0
3095 : ((i.types[1]
3096 & (Reg | RegMMX | RegXMM
3097 | SReg2 | SReg3
3098 | Control | Debug | Test))
3099 ? 1
3100 : 2));
3101 /* If there is an extension opcode to put here, the register
3102 number must be put into the regmem field. */
3103 if (i.tm.extension_opcode != None)
3104 {
3105 i.rm.regmem = i.op[op].regs->reg_num;
3106 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3107 i.rex |= REX_EXTZ;
3108 }
3109 else
3110 {
3111 i.rm.reg = i.op[op].regs->reg_num;
3112 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3113 i.rex |= REX_EXTX;
3114 }
252b5132 3115
29b0f896
AM
3116 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
3117 must set it to 3 to indicate this is a register operand
3118 in the regmem field. */
3119 if (!i.mem_operands)
3120 i.rm.mode = 3;
3121 }
252b5132 3122
29b0f896
AM
3123 /* Fill in i.rm.reg field with extension opcode (if any). */
3124 if (i.tm.extension_opcode != None)
3125 i.rm.reg = i.tm.extension_opcode;
3126 }
3127 return default_seg;
3128}
252b5132 3129
29b0f896
AM
3130static void
3131output_branch ()
3132{
3133 char *p;
3134 int code16;
3135 int prefix;
3136 relax_substateT subtype;
3137 symbolS *sym;
3138 offsetT off;
3139
3140 code16 = 0;
3141 if (flag_code == CODE_16BIT)
3142 code16 = CODE16;
3143
3144 prefix = 0;
3145 if (i.prefix[DATA_PREFIX] != 0)
252b5132 3146 {
29b0f896
AM
3147 prefix = 1;
3148 i.prefixes -= 1;
3149 code16 ^= CODE16;
252b5132 3150 }
29b0f896
AM
3151 /* Pentium4 branch hints. */
3152 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3153 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 3154 {
29b0f896
AM
3155 prefix++;
3156 i.prefixes--;
3157 }
3158 if (i.prefix[REX_PREFIX] != 0)
3159 {
3160 prefix++;
3161 i.prefixes--;
2f66722d
AM
3162 }
3163
29b0f896
AM
3164 if (i.prefixes != 0 && !intel_syntax)
3165 as_warn (_("skipping prefixes on this instruction"));
3166
3167 /* It's always a symbol; End frag & setup for relax.
3168 Make sure there is enough room in this frag for the largest
3169 instruction we may generate in md_convert_frag. This is 2
3170 bytes for the opcode and room for the prefix and largest
3171 displacement. */
3172 frag_grow (prefix + 2 + 4);
3173 /* Prefix and 1 opcode byte go in fr_fix. */
3174 p = frag_more (prefix + 1);
3175 if (i.prefix[DATA_PREFIX] != 0)
3176 *p++ = DATA_PREFIX_OPCODE;
3177 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
3178 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
3179 *p++ = i.prefix[SEG_PREFIX];
3180 if (i.prefix[REX_PREFIX] != 0)
3181 *p++ = i.prefix[REX_PREFIX];
3182 *p = i.tm.base_opcode;
3183
3184 if ((unsigned char) *p == JUMP_PC_RELATIVE)
3185 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
3186 else if ((cpu_arch_flags & Cpu386) != 0)
3187 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
3188 else
3189 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
3190 subtype |= code16;
3e73aa7c 3191
29b0f896
AM
3192 sym = i.op[0].disps->X_add_symbol;
3193 off = i.op[0].disps->X_add_number;
3e73aa7c 3194
29b0f896
AM
3195 if (i.op[0].disps->X_op != O_constant
3196 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 3197 {
29b0f896
AM
3198 /* Handle complex expressions. */
3199 sym = make_expr_symbol (i.op[0].disps);
3200 off = 0;
3201 }
3e73aa7c 3202
29b0f896
AM
3203 /* 1 possible extra opcode + 4 byte displacement go in var part.
3204 Pass reloc in fr_var. */
3205 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
3206}
3e73aa7c 3207
29b0f896
AM
3208static void
3209output_jump ()
3210{
3211 char *p;
3212 int size;
3e02c1cc 3213 fixS *fixP;
29b0f896
AM
3214
3215 if (i.tm.opcode_modifier & JumpByte)
3216 {
3217 /* This is a loop or jecxz type instruction. */
3218 size = 1;
3219 if (i.prefix[ADDR_PREFIX] != 0)
3220 {
3221 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
3222 i.prefixes -= 1;
3223 }
3224 /* Pentium4 branch hints. */
3225 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3226 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3227 {
3228 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
3229 i.prefixes--;
3e73aa7c
JH
3230 }
3231 }
29b0f896
AM
3232 else
3233 {
3234 int code16;
3e73aa7c 3235
29b0f896
AM
3236 code16 = 0;
3237 if (flag_code == CODE_16BIT)
3238 code16 = CODE16;
3e73aa7c 3239
29b0f896
AM
3240 if (i.prefix[DATA_PREFIX] != 0)
3241 {
3242 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
3243 i.prefixes -= 1;
3244 code16 ^= CODE16;
3245 }
252b5132 3246
29b0f896
AM
3247 size = 4;
3248 if (code16)
3249 size = 2;
3250 }
9fcc94b6 3251
29b0f896
AM
3252 if (i.prefix[REX_PREFIX] != 0)
3253 {
3254 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
3255 i.prefixes -= 1;
3256 }
252b5132 3257
29b0f896
AM
3258 if (i.prefixes != 0 && !intel_syntax)
3259 as_warn (_("skipping prefixes on this instruction"));
e0890092 3260
29b0f896
AM
3261 p = frag_more (1 + size);
3262 *p++ = i.tm.base_opcode;
e0890092 3263
3e02c1cc
AM
3264 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3265 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
3266
3267 /* All jumps handled here are signed, but don't use a signed limit
3268 check for 32 and 16 bit jumps as we want to allow wrap around at
3269 4G and 64k respectively. */
3270 if (size == 1)
3271 fixP->fx_signed = 1;
29b0f896 3272}
e0890092 3273
29b0f896
AM
3274static void
3275output_interseg_jump ()
3276{
3277 char *p;
3278 int size;
3279 int prefix;
3280 int code16;
252b5132 3281
29b0f896
AM
3282 code16 = 0;
3283 if (flag_code == CODE_16BIT)
3284 code16 = CODE16;
a217f122 3285
29b0f896
AM
3286 prefix = 0;
3287 if (i.prefix[DATA_PREFIX] != 0)
3288 {
3289 prefix = 1;
3290 i.prefixes -= 1;
3291 code16 ^= CODE16;
3292 }
3293 if (i.prefix[REX_PREFIX] != 0)
3294 {
3295 prefix++;
3296 i.prefixes -= 1;
3297 }
252b5132 3298
29b0f896
AM
3299 size = 4;
3300 if (code16)
3301 size = 2;
252b5132 3302
29b0f896
AM
3303 if (i.prefixes != 0 && !intel_syntax)
3304 as_warn (_("skipping prefixes on this instruction"));
252b5132 3305
29b0f896
AM
3306 /* 1 opcode; 2 segment; offset */
3307 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 3308
29b0f896
AM
3309 if (i.prefix[DATA_PREFIX] != 0)
3310 *p++ = DATA_PREFIX_OPCODE;
252b5132 3311
29b0f896
AM
3312 if (i.prefix[REX_PREFIX] != 0)
3313 *p++ = i.prefix[REX_PREFIX];
252b5132 3314
29b0f896
AM
3315 *p++ = i.tm.base_opcode;
3316 if (i.op[1].imms->X_op == O_constant)
3317 {
3318 offsetT n = i.op[1].imms->X_add_number;
252b5132 3319
29b0f896
AM
3320 if (size == 2
3321 && !fits_in_unsigned_word (n)
3322 && !fits_in_signed_word (n))
3323 {
3324 as_bad (_("16-bit jump out of range"));
3325 return;
3326 }
3327 md_number_to_chars (p, n, size);
3328 }
3329 else
3330 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3331 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
3332 if (i.op[0].imms->X_op != O_constant)
3333 as_bad (_("can't handle non absolute segment in `%s'"),
3334 i.tm.name);
3335 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
3336}
a217f122 3337
29b0f896
AM
3338static void
3339output_insn ()
3340{
2bbd9c25
JJ
3341 fragS *insn_start_frag;
3342 offsetT insn_start_off;
3343
29b0f896
AM
3344 /* Tie dwarf2 debug info to the address at the start of the insn.
3345 We can't do this after the insn has been output as the current
3346 frag may have been closed off. eg. by frag_var. */
3347 dwarf2_emit_insn (0);
3348
2bbd9c25
JJ
3349 insn_start_frag = frag_now;
3350 insn_start_off = frag_now_fix ();
3351
29b0f896
AM
3352 /* Output jumps. */
3353 if (i.tm.opcode_modifier & Jump)
3354 output_branch ();
3355 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
3356 output_jump ();
3357 else if (i.tm.opcode_modifier & JumpInterSegment)
3358 output_interseg_jump ();
3359 else
3360 {
3361 /* Output normal instructions here. */
3362 char *p;
3363 unsigned char *q;
252b5132 3364
bc4bd9ab
MK
3365 /* All opcodes on i386 have either 1 or 2 bytes. We may use one
3366 more higher byte to specify a prefix the instruction
3367 requires. */
3368 if ((i.tm.base_opcode & 0xff0000) != 0)
3369 {
3370 if ((i.tm.cpu_flags & CpuPadLock) != 0)
3371 {
3372 unsigned int prefix;
3373 prefix = (i.tm.base_opcode >> 16) & 0xff;
3374
3375 if (prefix != REPE_PREFIX_OPCODE
3376 || i.prefix[LOCKREP_PREFIX] != REPE_PREFIX_OPCODE)
3377 add_prefix (prefix);
3378 }
3379 else
3380 add_prefix ((i.tm.base_opcode >> 16) & 0xff);
0f10071e 3381 }
252b5132 3382
29b0f896
AM
3383 /* The prefix bytes. */
3384 for (q = i.prefix;
3385 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
3386 q++)
3387 {
3388 if (*q)
3389 {
3390 p = frag_more (1);
3391 md_number_to_chars (p, (valueT) *q, 1);
3392 }
3393 }
252b5132 3394
29b0f896
AM
3395 /* Now the opcode; be careful about word order here! */
3396 if (fits_in_unsigned_byte (i.tm.base_opcode))
3397 {
3398 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
3399 }
3400 else
3401 {
bc4bd9ab 3402 p = frag_more (2);
0f10071e 3403
29b0f896
AM
3404 /* Put out high byte first: can't use md_number_to_chars! */
3405 *p++ = (i.tm.base_opcode >> 8) & 0xff;
3406 *p = i.tm.base_opcode & 0xff;
3407 }
3e73aa7c 3408
29b0f896
AM
3409 /* Now the modrm byte and sib byte (if present). */
3410 if (i.tm.opcode_modifier & Modrm)
3411 {
3412 p = frag_more (1);
3413 md_number_to_chars (p,
3414 (valueT) (i.rm.regmem << 0
3415 | i.rm.reg << 3
3416 | i.rm.mode << 6),
3417 1);
3418 /* If i.rm.regmem == ESP (4)
3419 && i.rm.mode != (Register mode)
3420 && not 16 bit
3421 ==> need second modrm byte. */
3422 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
3423 && i.rm.mode != 3
3424 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
3425 {
3426 p = frag_more (1);
3427 md_number_to_chars (p,
3428 (valueT) (i.sib.base << 0
3429 | i.sib.index << 3
3430 | i.sib.scale << 6),
3431 1);
3432 }
3433 }
3e73aa7c 3434
29b0f896 3435 if (i.disp_operands)
2bbd9c25 3436 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 3437
29b0f896 3438 if (i.imm_operands)
2bbd9c25 3439 output_imm (insn_start_frag, insn_start_off);
29b0f896 3440 }
252b5132 3441
29b0f896
AM
3442#ifdef DEBUG386
3443 if (flag_debug)
3444 {
3445 pi (line, &i);
3446 }
3447#endif /* DEBUG386 */
3448}
252b5132 3449
29b0f896 3450static void
2bbd9c25
JJ
3451output_disp (insn_start_frag, insn_start_off)
3452 fragS *insn_start_frag;
3453 offsetT insn_start_off;
29b0f896
AM
3454{
3455 char *p;
3456 unsigned int n;
252b5132 3457
29b0f896
AM
3458 for (n = 0; n < i.operands; n++)
3459 {
3460 if (i.types[n] & Disp)
3461 {
3462 if (i.op[n].disps->X_op == O_constant)
3463 {
3464 int size;
3465 offsetT val;
252b5132 3466
29b0f896
AM
3467 size = 4;
3468 if (i.types[n] & (Disp8 | Disp16 | Disp64))
3469 {
3470 size = 2;
3471 if (i.types[n] & Disp8)
3472 size = 1;
3473 if (i.types[n] & Disp64)
3474 size = 8;
3475 }
3476 val = offset_in_range (i.op[n].disps->X_add_number,
3477 size);
3478 p = frag_more (size);
3479 md_number_to_chars (p, val, size);
3480 }
3481 else
3482 {
f86103b7 3483 enum bfd_reloc_code_real reloc_type;
29b0f896
AM
3484 int size = 4;
3485 int sign = 0;
3486 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
3487
3488 /* The PC relative address is computed relative
3489 to the instruction boundary, so in case immediate
3490 fields follows, we need to adjust the value. */
3491 if (pcrel && i.imm_operands)
3492 {
3493 int imm_size = 4;
3494 unsigned int n1;
252b5132 3495
29b0f896
AM
3496 for (n1 = 0; n1 < i.operands; n1++)
3497 if (i.types[n1] & Imm)
252b5132 3498 {
29b0f896 3499 if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
252b5132 3500 {
29b0f896
AM
3501 imm_size = 2;
3502 if (i.types[n1] & (Imm8 | Imm8S))
3503 imm_size = 1;
3504 if (i.types[n1] & Imm64)
3505 imm_size = 8;
252b5132 3506 }
29b0f896 3507 break;
252b5132 3508 }
29b0f896
AM
3509 /* We should find the immediate. */
3510 if (n1 == i.operands)
3511 abort ();
3512 i.op[n].disps->X_add_number -= imm_size;
3513 }
520dc8e8 3514
29b0f896
AM
3515 if (i.types[n] & Disp32S)
3516 sign = 1;
3e73aa7c 3517
29b0f896
AM
3518 if (i.types[n] & (Disp16 | Disp64))
3519 {
3520 size = 2;
3521 if (i.types[n] & Disp64)
3522 size = 8;
3523 }
520dc8e8 3524
29b0f896 3525 p = frag_more (size);
2bbd9c25 3526 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 3527 if (GOT_symbol
2bbd9c25 3528 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113
JB
3529 && (((reloc_type == BFD_RELOC_32
3530 || reloc_type == BFD_RELOC_X86_64_32S)
3531 && (i.op[n].disps->X_op == O_symbol
3532 || (i.op[n].disps->X_op == O_add
3533 && ((symbol_get_value_expression
3534 (i.op[n].disps->X_op_symbol)->X_op)
3535 == O_subtract))))
3536 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25
JJ
3537 {
3538 offsetT add;
3539
3540 if (insn_start_frag == frag_now)
3541 add = (p - frag_now->fr_literal) - insn_start_off;
3542 else
3543 {
3544 fragS *fr;
3545
3546 add = insn_start_frag->fr_fix - insn_start_off;
3547 for (fr = insn_start_frag->fr_next;
3548 fr && fr != frag_now; fr = fr->fr_next)
3549 add += fr->fr_fix;
3550 add += p - frag_now->fr_literal;
3551 }
3552
d6ab8113
JB
3553 if (flag_code != CODE_64BIT)
3554 reloc_type = BFD_RELOC_386_GOTPC;
3555 else
3556 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25
JJ
3557 i.op[n].disps->X_add_number += add;
3558 }
062cd5e7 3559 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2bbd9c25 3560 i.op[n].disps, pcrel, reloc_type);
29b0f896
AM
3561 }
3562 }
3563 }
3564}
252b5132 3565
29b0f896 3566static void
2bbd9c25
JJ
3567output_imm (insn_start_frag, insn_start_off)
3568 fragS *insn_start_frag;
3569 offsetT insn_start_off;
29b0f896
AM
3570{
3571 char *p;
3572 unsigned int n;
252b5132 3573
29b0f896
AM
3574 for (n = 0; n < i.operands; n++)
3575 {
3576 if (i.types[n] & Imm)
3577 {
3578 if (i.op[n].imms->X_op == O_constant)
3579 {
3580 int size;
3581 offsetT val;
b4cac588 3582
29b0f896
AM
3583 size = 4;
3584 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3585 {
3586 size = 2;
3587 if (i.types[n] & (Imm8 | Imm8S))
3588 size = 1;
3589 else if (i.types[n] & Imm64)
3590 size = 8;
3591 }
3592 val = offset_in_range (i.op[n].imms->X_add_number,
3593 size);
3594 p = frag_more (size);
3595 md_number_to_chars (p, val, size);
3596 }
3597 else
3598 {
3599 /* Not absolute_section.
3600 Need a 32-bit fixup (don't support 8bit
3601 non-absolute imms). Try to support other
3602 sizes ... */
f86103b7 3603 enum bfd_reloc_code_real reloc_type;
29b0f896
AM
3604 int size = 4;
3605 int sign = 0;
3606
3607 if ((i.types[n] & (Imm32S))
a7d61044
JB
3608 && (i.suffix == QWORD_MNEM_SUFFIX
3609 || (!i.suffix && (i.tm.opcode_modifier & No_lSuf))))
29b0f896
AM
3610 sign = 1;
3611 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3612 {
3613 size = 2;
3614 if (i.types[n] & (Imm8 | Imm8S))
3615 size = 1;
3616 if (i.types[n] & Imm64)
3617 size = 8;
3618 }
520dc8e8 3619
29b0f896
AM
3620 p = frag_more (size);
3621 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 3622
2bbd9c25
JJ
3623 /* This is tough to explain. We end up with this one if we
3624 * have operands that look like
3625 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
3626 * obtain the absolute address of the GOT, and it is strongly
3627 * preferable from a performance point of view to avoid using
3628 * a runtime relocation for this. The actual sequence of
3629 * instructions often look something like:
3630 *
3631 * call .L66
3632 * .L66:
3633 * popl %ebx
3634 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
3635 *
3636 * The call and pop essentially return the absolute address
3637 * of the label .L66 and store it in %ebx. The linker itself
3638 * will ultimately change the first operand of the addl so
3639 * that %ebx points to the GOT, but to keep things simple, the
3640 * .o file must have this operand set so that it generates not
3641 * the absolute address of .L66, but the absolute address of
3642 * itself. This allows the linker itself simply treat a GOTPC
3643 * relocation as asking for a pcrel offset to the GOT to be
3644 * added in, and the addend of the relocation is stored in the
3645 * operand field for the instruction itself.
3646 *
3647 * Our job here is to fix the operand so that it would add
3648 * the correct offset so that %ebx would point to itself. The
3649 * thing that is tricky is that .-.L66 will point to the
3650 * beginning of the instruction, so we need to further modify
3651 * the operand so that it will point to itself. There are
3652 * other cases where you have something like:
3653 *
3654 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
3655 *
3656 * and here no correction would be required. Internally in
3657 * the assembler we treat operands of this form as not being
3658 * pcrel since the '.' is explicitly mentioned, and I wonder
3659 * whether it would simplify matters to do it this way. Who
3660 * knows. In earlier versions of the PIC patches, the
3661 * pcrel_adjust field was used to store the correction, but
3662 * since the expression is not pcrel, I felt it would be
3663 * confusing to do it this way. */
3664
d6ab8113
JB
3665 if ((reloc_type == BFD_RELOC_32
3666 || reloc_type == BFD_RELOC_X86_64_32S)
29b0f896
AM
3667 && GOT_symbol
3668 && GOT_symbol == i.op[n].imms->X_add_symbol
3669 && (i.op[n].imms->X_op == O_symbol
3670 || (i.op[n].imms->X_op == O_add
3671 && ((symbol_get_value_expression
3672 (i.op[n].imms->X_op_symbol)->X_op)
3673 == O_subtract))))
3674 {
2bbd9c25
JJ
3675 offsetT add;
3676
3677 if (insn_start_frag == frag_now)
3678 add = (p - frag_now->fr_literal) - insn_start_off;
3679 else
3680 {
3681 fragS *fr;
3682
3683 add = insn_start_frag->fr_fix - insn_start_off;
3684 for (fr = insn_start_frag->fr_next;
3685 fr && fr != frag_now; fr = fr->fr_next)
3686 add += fr->fr_fix;
3687 add += p - frag_now->fr_literal;
3688 }
3689
d6ab8113
JB
3690 if (flag_code != CODE_64BIT)
3691 reloc_type = BFD_RELOC_386_GOTPC;
3692 else
3693 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 3694 i.op[n].imms->X_add_number += add;
29b0f896 3695 }
29b0f896
AM
3696 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3697 i.op[n].imms, 0, reloc_type);
3698 }
3699 }
3700 }
252b5132
RH
3701}
3702\f
f3c180ae 3703#ifndef LEX_AT
f86103b7 3704static char *lex_got PARAMS ((enum bfd_reloc_code_real *, int *));
f3c180ae
AM
3705
3706/* Parse operands of the form
3707 <symbol>@GOTOFF+<nnn>
3708 and similar .plt or .got references.
3709
3710 If we find one, set up the correct relocation in RELOC and copy the
3711 input string, minus the `@GOTOFF' into a malloc'd buffer for
3712 parsing by the calling routine. Return this buffer, and if ADJUST
3713 is non-null set it to the length of the string we removed from the
3714 input line. Otherwise return NULL. */
3715static char *
3716lex_got (reloc, adjust)
f86103b7 3717 enum bfd_reloc_code_real *reloc;
f3c180ae
AM
3718 int *adjust;
3719{
3720 static const char * const mode_name[NUM_FLAG_CODE] = { "32", "16", "64" };
3721 static const struct {
3722 const char *str;
f86103b7 3723 const enum bfd_reloc_code_real rel[NUM_FLAG_CODE];
f3c180ae 3724 } gotrel[] = {
13ae64f3 3725 { "PLT", { BFD_RELOC_386_PLT32, 0, BFD_RELOC_X86_64_PLT32 } },
d6ab8113 3726 { "GOTOFF", { BFD_RELOC_386_GOTOFF, 0, BFD_RELOC_X86_64_GOTOFF64 } },
13ae64f3 3727 { "GOTPCREL", { 0, 0, BFD_RELOC_X86_64_GOTPCREL } },
bffbf940 3728 { "TLSGD", { BFD_RELOC_386_TLS_GD, 0, BFD_RELOC_X86_64_TLSGD } },
13ae64f3 3729 { "TLSLDM", { BFD_RELOC_386_TLS_LDM, 0, 0 } },
bffbf940
JJ
3730 { "TLSLD", { 0, 0, BFD_RELOC_X86_64_TLSLD } },
3731 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32, 0, BFD_RELOC_X86_64_GOTTPOFF } },
3732 { "TPOFF", { BFD_RELOC_386_TLS_LE_32, 0, BFD_RELOC_X86_64_TPOFF32 } },
13ae64f3 3733 { "NTPOFF", { BFD_RELOC_386_TLS_LE, 0, 0 } },
bffbf940 3734 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32, 0, BFD_RELOC_X86_64_DTPOFF32 } },
37e55690
JJ
3735 { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE, 0, 0 } },
3736 { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE, 0, 0 } },
13ae64f3 3737 { "GOT", { BFD_RELOC_386_GOT32, 0, BFD_RELOC_X86_64_GOT32 } }
f3c180ae
AM
3738 };
3739 char *cp;
3740 unsigned int j;
3741
3742 for (cp = input_line_pointer; *cp != '@'; cp++)
3743 if (is_end_of_line[(unsigned char) *cp])
3744 return NULL;
3745
3746 for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
3747 {
3748 int len;
3749
3750 len = strlen (gotrel[j].str);
28f81592 3751 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae
AM
3752 {
3753 if (gotrel[j].rel[(unsigned int) flag_code] != 0)
3754 {
28f81592
AM
3755 int first, second;
3756 char *tmpbuf, *past_reloc;
f3c180ae
AM
3757
3758 *reloc = gotrel[j].rel[(unsigned int) flag_code];
28f81592
AM
3759 if (adjust)
3760 *adjust = len;
f3c180ae
AM
3761
3762 if (GOT_symbol == NULL)
3763 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
3764
3765 /* Replace the relocation token with ' ', so that
3766 errors like foo@GOTOFF1 will be detected. */
28f81592
AM
3767
3768 /* The length of the first part of our input line. */
f3c180ae 3769 first = cp - input_line_pointer;
28f81592
AM
3770
3771 /* The second part goes from after the reloc token until
3772 (and including) an end_of_line char. Don't use strlen
3773 here as the end_of_line char may not be a NUL. */
3774 past_reloc = cp + 1 + len;
3775 for (cp = past_reloc; !is_end_of_line[(unsigned char) *cp++]; )
3776 ;
3777 second = cp - past_reloc;
3778
3779 /* Allocate and copy string. The trailing NUL shouldn't
3780 be necessary, but be safe. */
3781 tmpbuf = xmalloc (first + second + 2);
f3c180ae
AM
3782 memcpy (tmpbuf, input_line_pointer, first);
3783 tmpbuf[first] = ' ';
28f81592
AM
3784 memcpy (tmpbuf + first + 1, past_reloc, second);
3785 tmpbuf[first + second + 1] = '\0';
f3c180ae
AM
3786 return tmpbuf;
3787 }
3788
3789 as_bad (_("@%s reloc is not supported in %s bit mode"),
3790 gotrel[j].str, mode_name[(unsigned int) flag_code]);
3791 return NULL;
3792 }
3793 }
3794
3795 /* Might be a symbol version string. Don't as_bad here. */
3796 return NULL;
3797}
3798
3799/* x86_cons_fix_new is called via the expression parsing code when a
3800 reloc is needed. We use this hook to get the correct .got reloc. */
f86103b7 3801static enum bfd_reloc_code_real got_reloc = NO_RELOC;
f3c180ae
AM
3802
3803void
3804x86_cons_fix_new (frag, off, len, exp)
3805 fragS *frag;
3806 unsigned int off;
3807 unsigned int len;
3808 expressionS *exp;
3809{
f86103b7 3810 enum bfd_reloc_code_real r = reloc (len, 0, 0, got_reloc);
f3c180ae
AM
3811 got_reloc = NO_RELOC;
3812 fix_new_exp (frag, off, len, exp, 0, r);
3813}
3814
3815void
3816x86_cons (exp, size)
3817 expressionS *exp;
3818 int size;
3819{
d6ab8113 3820 if (size == 4 || (flag_code == CODE_64BIT && size == 8))
f3c180ae
AM
3821 {
3822 /* Handle @GOTOFF and the like in an expression. */
3823 char *save;
3824 char *gotfree_input_line;
3825 int adjust;
3826
3827 save = input_line_pointer;
3828 gotfree_input_line = lex_got (&got_reloc, &adjust);
3829 if (gotfree_input_line)
3830 input_line_pointer = gotfree_input_line;
3831
3832 expression (exp);
3833
3834 if (gotfree_input_line)
3835 {
3836 /* expression () has merrily parsed up to the end of line,
3837 or a comma - in the wrong buffer. Transfer how far
3838 input_line_pointer has moved to the right buffer. */
3839 input_line_pointer = (save
3840 + (input_line_pointer - gotfree_input_line)
3841 + adjust);
3842 free (gotfree_input_line);
3843 }
3844 }
3845 else
3846 expression (exp);
3847}
3848#endif
3849
6482c264
NC
3850#ifdef TE_PE
3851
6482c264
NC
3852void
3853x86_pe_cons_fix_new (frag, off, len, exp)
3854 fragS *frag;
3855 unsigned int off;
3856 unsigned int len;
3857 expressionS *exp;
3858{
3859 enum bfd_reloc_code_real r = reloc (len, 0, 0, NO_RELOC);
3860
3861 if (exp->X_op == O_secrel)
3862 {
3863 exp->X_op = O_symbol;
3864 r = BFD_RELOC_32_SECREL;
3865 }
3866
3867 fix_new_exp (frag, off, len, exp, 0, r);
3868}
3869
3870static void
3871pe_directive_secrel (dummy)
3872 int dummy ATTRIBUTE_UNUSED;
3873{
3874 expressionS exp;
3875
3876 do
3877 {
3878 expression (&exp);
3879 if (exp.X_op == O_symbol)
3880 exp.X_op = O_secrel;
3881
3882 emit_expr (&exp, 4);
3883 }
3884 while (*input_line_pointer++ == ',');
3885
3886 input_line_pointer--;
3887 demand_empty_rest_of_line ();
3888}
3889
3890#endif
3891
252b5132
RH
3892static int i386_immediate PARAMS ((char *));
3893
3894static int
3895i386_immediate (imm_start)
3896 char *imm_start;
3897{
3898 char *save_input_line_pointer;
f3c180ae
AM
3899#ifndef LEX_AT
3900 char *gotfree_input_line;
3901#endif
252b5132 3902 segT exp_seg = 0;
47926f60 3903 expressionS *exp;
252b5132
RH
3904
3905 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
3906 {
d0b47220 3907 as_bad (_("only 1 or 2 immediate operands are allowed"));
252b5132
RH
3908 return 0;
3909 }
3910
3911 exp = &im_expressions[i.imm_operands++];
520dc8e8 3912 i.op[this_operand].imms = exp;
252b5132
RH
3913
3914 if (is_space_char (*imm_start))
3915 ++imm_start;
3916
3917 save_input_line_pointer = input_line_pointer;
3918 input_line_pointer = imm_start;
3919
3920#ifndef LEX_AT
f3c180ae
AM
3921 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
3922 if (gotfree_input_line)
3923 input_line_pointer = gotfree_input_line;
252b5132
RH
3924#endif
3925
3926 exp_seg = expression (exp);
3927
83183c0c 3928 SKIP_WHITESPACE ();
252b5132 3929 if (*input_line_pointer)
f3c180ae 3930 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
3931
3932 input_line_pointer = save_input_line_pointer;
f3c180ae
AM
3933#ifndef LEX_AT
3934 if (gotfree_input_line)
3935 free (gotfree_input_line);
3936#endif
252b5132 3937
2daf4fd8 3938 if (exp->X_op == O_absent || exp->X_op == O_big)
252b5132 3939 {
47926f60 3940 /* Missing or bad expr becomes absolute 0. */
d0b47220 3941 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
24eab124 3942 imm_start);
252b5132
RH
3943 exp->X_op = O_constant;
3944 exp->X_add_number = 0;
3945 exp->X_add_symbol = (symbolS *) 0;
3946 exp->X_op_symbol = (symbolS *) 0;
252b5132 3947 }
3e73aa7c 3948 else if (exp->X_op == O_constant)
252b5132 3949 {
47926f60 3950 /* Size it properly later. */
3e73aa7c
JH
3951 i.types[this_operand] |= Imm64;
3952 /* If BFD64, sign extend val. */
3953 if (!use_rela_relocations)
3954 if ((exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
3955 exp->X_add_number = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 3956 }
4c63da97 3957#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 3958 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 3959 && exp_seg != absolute_section
47926f60 3960 && exp_seg != text_section
24eab124
AM
3961 && exp_seg != data_section
3962 && exp_seg != bss_section
3963 && exp_seg != undefined_section
f86103b7 3964 && !bfd_is_com_section (exp_seg))
252b5132 3965 {
d0b47220 3966 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
3967 return 0;
3968 }
3969#endif
3970 else
3971 {
3972 /* This is an address. The size of the address will be
24eab124 3973 determined later, depending on destination register,
3e73aa7c
JH
3974 suffix, or the default for the section. */
3975 i.types[this_operand] |= Imm8 | Imm16 | Imm32 | Imm32S | Imm64;
252b5132
RH
3976 }
3977
3978 return 1;
3979}
3980
551c1ca1 3981static char *i386_scale PARAMS ((char *));
252b5132 3982
551c1ca1 3983static char *
252b5132
RH
3984i386_scale (scale)
3985 char *scale;
3986{
551c1ca1
AM
3987 offsetT val;
3988 char *save = input_line_pointer;
252b5132 3989
551c1ca1
AM
3990 input_line_pointer = scale;
3991 val = get_absolute_expression ();
3992
3993 switch (val)
252b5132 3994 {
551c1ca1 3995 case 1:
252b5132
RH
3996 i.log2_scale_factor = 0;
3997 break;
551c1ca1 3998 case 2:
252b5132
RH
3999 i.log2_scale_factor = 1;
4000 break;
551c1ca1 4001 case 4:
252b5132
RH
4002 i.log2_scale_factor = 2;
4003 break;
551c1ca1 4004 case 8:
252b5132
RH
4005 i.log2_scale_factor = 3;
4006 break;
4007 default:
a724f0f4
JB
4008 {
4009 char sep = *input_line_pointer;
4010
4011 *input_line_pointer = '\0';
4012 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
4013 scale);
4014 *input_line_pointer = sep;
4015 input_line_pointer = save;
4016 return NULL;
4017 }
252b5132 4018 }
29b0f896 4019 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
4020 {
4021 as_warn (_("scale factor of %d without an index register"),
24eab124 4022 1 << i.log2_scale_factor);
252b5132
RH
4023#if SCALE1_WHEN_NO_INDEX
4024 i.log2_scale_factor = 0;
4025#endif
4026 }
551c1ca1
AM
4027 scale = input_line_pointer;
4028 input_line_pointer = save;
4029 return scale;
252b5132
RH
4030}
4031
4032static int i386_displacement PARAMS ((char *, char *));
4033
4034static int
4035i386_displacement (disp_start, disp_end)
4036 char *disp_start;
4037 char *disp_end;
4038{
29b0f896 4039 expressionS *exp;
252b5132
RH
4040 segT exp_seg = 0;
4041 char *save_input_line_pointer;
f3c180ae
AM
4042#ifndef LEX_AT
4043 char *gotfree_input_line;
4044#endif
252b5132
RH
4045 int bigdisp = Disp32;
4046
3e73aa7c 4047 if (flag_code == CODE_64BIT)
7ecd2f8b 4048 {
29b0f896
AM
4049 if (i.prefix[ADDR_PREFIX] == 0)
4050 bigdisp = Disp64;
7ecd2f8b
JH
4051 }
4052 else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
4053 bigdisp = Disp16;
252b5132
RH
4054 i.types[this_operand] |= bigdisp;
4055
4056 exp = &disp_expressions[i.disp_operands];
520dc8e8 4057 i.op[this_operand].disps = exp;
252b5132
RH
4058 i.disp_operands++;
4059 save_input_line_pointer = input_line_pointer;
4060 input_line_pointer = disp_start;
4061 END_STRING_AND_SAVE (disp_end);
4062
4063#ifndef GCC_ASM_O_HACK
4064#define GCC_ASM_O_HACK 0
4065#endif
4066#if GCC_ASM_O_HACK
4067 END_STRING_AND_SAVE (disp_end + 1);
4068 if ((i.types[this_operand] & BaseIndex) != 0
24eab124 4069 && displacement_string_end[-1] == '+')
252b5132
RH
4070 {
4071 /* This hack is to avoid a warning when using the "o"
24eab124
AM
4072 constraint within gcc asm statements.
4073 For instance:
4074
4075 #define _set_tssldt_desc(n,addr,limit,type) \
4076 __asm__ __volatile__ ( \
4077 "movw %w2,%0\n\t" \
4078 "movw %w1,2+%0\n\t" \
4079 "rorl $16,%1\n\t" \
4080 "movb %b1,4+%0\n\t" \
4081 "movb %4,5+%0\n\t" \
4082 "movb $0,6+%0\n\t" \
4083 "movb %h1,7+%0\n\t" \
4084 "rorl $16,%1" \
4085 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
4086
4087 This works great except that the output assembler ends
4088 up looking a bit weird if it turns out that there is
4089 no offset. You end up producing code that looks like:
4090
4091 #APP
4092 movw $235,(%eax)
4093 movw %dx,2+(%eax)
4094 rorl $16,%edx
4095 movb %dl,4+(%eax)
4096 movb $137,5+(%eax)
4097 movb $0,6+(%eax)
4098 movb %dh,7+(%eax)
4099 rorl $16,%edx
4100 #NO_APP
4101
47926f60 4102 So here we provide the missing zero. */
24eab124
AM
4103
4104 *displacement_string_end = '0';
252b5132
RH
4105 }
4106#endif
4107#ifndef LEX_AT
f3c180ae
AM
4108 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
4109 if (gotfree_input_line)
4110 input_line_pointer = gotfree_input_line;
252b5132
RH
4111#endif
4112
24eab124 4113 exp_seg = expression (exp);
252b5132 4114
636c26b0
AM
4115 SKIP_WHITESPACE ();
4116 if (*input_line_pointer)
4117 as_bad (_("junk `%s' after expression"), input_line_pointer);
4118#if GCC_ASM_O_HACK
4119 RESTORE_END_STRING (disp_end + 1);
4120#endif
4121 RESTORE_END_STRING (disp_end);
4122 input_line_pointer = save_input_line_pointer;
4123#ifndef LEX_AT
4124 if (gotfree_input_line)
4125 free (gotfree_input_line);
4126#endif
4127
24eab124
AM
4128 /* We do this to make sure that the section symbol is in
4129 the symbol table. We will ultimately change the relocation
47926f60 4130 to be relative to the beginning of the section. */
1ae12ab7 4131 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
4132 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
4133 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 4134 {
636c26b0
AM
4135 if (exp->X_op != O_symbol)
4136 {
4137 as_bad (_("bad expression used with @%s"),
4138 (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
4139 ? "GOTPCREL"
4140 : "GOTOFF"));
4141 return 0;
4142 }
4143
e5cb08ac 4144 if (S_IS_LOCAL (exp->X_add_symbol)
24eab124
AM
4145 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
4146 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
4147 exp->X_op = O_subtract;
4148 exp->X_op_symbol = GOT_symbol;
1ae12ab7 4149 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 4150 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
4151 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
4152 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 4153 else
29b0f896 4154 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 4155 }
252b5132 4156
2daf4fd8
AM
4157 if (exp->X_op == O_absent || exp->X_op == O_big)
4158 {
47926f60 4159 /* Missing or bad expr becomes absolute 0. */
d0b47220 4160 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
2daf4fd8
AM
4161 disp_start);
4162 exp->X_op = O_constant;
4163 exp->X_add_number = 0;
4164 exp->X_add_symbol = (symbolS *) 0;
4165 exp->X_op_symbol = (symbolS *) 0;
4166 }
4167
4c63da97 4168#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
45288df1 4169 if (exp->X_op != O_constant
45288df1 4170 && OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 4171 && exp_seg != absolute_section
45288df1
AM
4172 && exp_seg != text_section
4173 && exp_seg != data_section
4174 && exp_seg != bss_section
31312f95 4175 && exp_seg != undefined_section
f86103b7 4176 && !bfd_is_com_section (exp_seg))
24eab124 4177 {
d0b47220 4178 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
24eab124
AM
4179 return 0;
4180 }
252b5132 4181#endif
3e73aa7c
JH
4182 else if (flag_code == CODE_64BIT)
4183 i.types[this_operand] |= Disp32S | Disp32;
252b5132
RH
4184 return 1;
4185}
4186
e5cb08ac 4187static int i386_index_check PARAMS ((const char *));
252b5132 4188
eecb386c 4189/* Make sure the memory operand we've been dealt is valid.
47926f60
KH
4190 Return 1 on success, 0 on a failure. */
4191
252b5132 4192static int
eecb386c
AM
4193i386_index_check (operand_string)
4194 const char *operand_string;
252b5132 4195{
3e73aa7c 4196 int ok;
24eab124 4197#if INFER_ADDR_PREFIX
eecb386c
AM
4198 int fudged = 0;
4199
24eab124
AM
4200 tryprefix:
4201#endif
3e73aa7c 4202 ok = 1;
30123838
JB
4203 if ((current_templates->start->cpu_flags & CpuSVME)
4204 && current_templates->end[-1].operand_types[0] == AnyMem)
4205 {
4206 /* Memory operands of SVME insns are special in that they only allow
4207 rAX as their memory address and ignore any segment override. */
4208 unsigned RegXX;
4209
4210 /* SKINIT is even more restrictive: it always requires EAX. */
4211 if (strcmp (current_templates->start->name, "skinit") == 0)
4212 RegXX = Reg32;
4213 else if (flag_code == CODE_64BIT)
4214 RegXX = i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32;
4215 else
4216 RegXX = (flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0)
4217 ? Reg16
4218 : Reg32;
4219 if (!i.base_reg
4220 || !(i.base_reg->reg_type & Acc)
4221 || !(i.base_reg->reg_type & RegXX)
4222 || i.index_reg
4223 || (i.types[0] & Disp))
4224 ok = 0;
4225 }
4226 else if (flag_code == CODE_64BIT)
20f0a1fc
NC
4227 {
4228 unsigned RegXX = (i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32);
4229
4230 if ((i.base_reg
4231 && ((i.base_reg->reg_type & RegXX) == 0)
4232 && (i.base_reg->reg_type != BaseIndex
4233 || i.index_reg))
4234 || (i.index_reg
4235 && ((i.index_reg->reg_type & (RegXX | BaseIndex))
4236 != (RegXX | BaseIndex))))
4237 ok = 0;
3e73aa7c
JH
4238 }
4239 else
4240 {
4241 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
4242 {
4243 /* 16bit checks. */
4244 if ((i.base_reg
29b0f896
AM
4245 && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
4246 != (Reg16 | BaseIndex)))
3e73aa7c 4247 || (i.index_reg
29b0f896
AM
4248 && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
4249 != (Reg16 | BaseIndex))
4250 || !(i.base_reg
4251 && i.base_reg->reg_num < 6
4252 && i.index_reg->reg_num >= 6
4253 && i.log2_scale_factor == 0))))
3e73aa7c
JH
4254 ok = 0;
4255 }
4256 else
e5cb08ac 4257 {
3e73aa7c
JH
4258 /* 32bit checks. */
4259 if ((i.base_reg
4260 && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
4261 || (i.index_reg
29b0f896
AM
4262 && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
4263 != (Reg32 | BaseIndex))))
e5cb08ac 4264 ok = 0;
3e73aa7c
JH
4265 }
4266 }
4267 if (!ok)
24eab124
AM
4268 {
4269#if INFER_ADDR_PREFIX
20f0a1fc 4270 if (i.prefix[ADDR_PREFIX] == 0)
24eab124
AM
4271 {
4272 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
4273 i.prefixes += 1;
b23bac36
AM
4274 /* Change the size of any displacement too. At most one of
4275 Disp16 or Disp32 is set.
4276 FIXME. There doesn't seem to be any real need for separate
4277 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
47926f60 4278 Removing them would probably clean up the code quite a lot. */
20f0a1fc 4279 if (flag_code != CODE_64BIT && (i.types[this_operand] & (Disp16 | Disp32)))
29b0f896 4280 i.types[this_operand] ^= (Disp16 | Disp32);
eecb386c 4281 fudged = 1;
24eab124
AM
4282 goto tryprefix;
4283 }
eecb386c
AM
4284 if (fudged)
4285 as_bad (_("`%s' is not a valid base/index expression"),
4286 operand_string);
4287 else
c388dee8 4288#endif
eecb386c
AM
4289 as_bad (_("`%s' is not a valid %s bit base/index expression"),
4290 operand_string,
3e73aa7c 4291 flag_code_names[flag_code]);
24eab124 4292 }
20f0a1fc 4293 return ok;
24eab124 4294}
252b5132 4295
252b5132 4296/* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
47926f60 4297 on error. */
252b5132 4298
252b5132
RH
4299static int
4300i386_operand (operand_string)
4301 char *operand_string;
4302{
af6bdddf
AM
4303 const reg_entry *r;
4304 char *end_op;
24eab124 4305 char *op_string = operand_string;
252b5132 4306
24eab124 4307 if (is_space_char (*op_string))
252b5132
RH
4308 ++op_string;
4309
24eab124 4310 /* We check for an absolute prefix (differentiating,
47926f60 4311 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
4312 if (*op_string == ABSOLUTE_PREFIX)
4313 {
4314 ++op_string;
4315 if (is_space_char (*op_string))
4316 ++op_string;
4317 i.types[this_operand] |= JumpAbsolute;
4318 }
252b5132 4319
47926f60 4320 /* Check if operand is a register. */
af6bdddf
AM
4321 if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
4322 && (r = parse_register (op_string, &end_op)) != NULL)
24eab124 4323 {
24eab124
AM
4324 /* Check for a segment override by searching for ':' after a
4325 segment register. */
4326 op_string = end_op;
4327 if (is_space_char (*op_string))
4328 ++op_string;
4329 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
4330 {
4331 switch (r->reg_num)
4332 {
4333 case 0:
4334 i.seg[i.mem_operands] = &es;
4335 break;
4336 case 1:
4337 i.seg[i.mem_operands] = &cs;
4338 break;
4339 case 2:
4340 i.seg[i.mem_operands] = &ss;
4341 break;
4342 case 3:
4343 i.seg[i.mem_operands] = &ds;
4344 break;
4345 case 4:
4346 i.seg[i.mem_operands] = &fs;
4347 break;
4348 case 5:
4349 i.seg[i.mem_operands] = &gs;
4350 break;
4351 }
252b5132 4352
24eab124 4353 /* Skip the ':' and whitespace. */
252b5132
RH
4354 ++op_string;
4355 if (is_space_char (*op_string))
24eab124 4356 ++op_string;
252b5132 4357
24eab124
AM
4358 if (!is_digit_char (*op_string)
4359 && !is_identifier_char (*op_string)
4360 && *op_string != '('
4361 && *op_string != ABSOLUTE_PREFIX)
4362 {
4363 as_bad (_("bad memory operand `%s'"), op_string);
4364 return 0;
4365 }
47926f60 4366 /* Handle case of %es:*foo. */
24eab124
AM
4367 if (*op_string == ABSOLUTE_PREFIX)
4368 {
4369 ++op_string;
4370 if (is_space_char (*op_string))
4371 ++op_string;
4372 i.types[this_operand] |= JumpAbsolute;
4373 }
4374 goto do_memory_reference;
4375 }
4376 if (*op_string)
4377 {
d0b47220 4378 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
4379 return 0;
4380 }
4381 i.types[this_operand] |= r->reg_type & ~BaseIndex;
520dc8e8 4382 i.op[this_operand].regs = r;
24eab124
AM
4383 i.reg_operands++;
4384 }
af6bdddf
AM
4385 else if (*op_string == REGISTER_PREFIX)
4386 {
4387 as_bad (_("bad register name `%s'"), op_string);
4388 return 0;
4389 }
24eab124 4390 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 4391 {
24eab124
AM
4392 ++op_string;
4393 if (i.types[this_operand] & JumpAbsolute)
4394 {
d0b47220 4395 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
4396 return 0;
4397 }
4398 if (!i386_immediate (op_string))
4399 return 0;
4400 }
4401 else if (is_digit_char (*op_string)
4402 || is_identifier_char (*op_string)
e5cb08ac 4403 || *op_string == '(')
24eab124 4404 {
47926f60 4405 /* This is a memory reference of some sort. */
af6bdddf 4406 char *base_string;
252b5132 4407
47926f60 4408 /* Start and end of displacement string expression (if found). */
eecb386c
AM
4409 char *displacement_string_start;
4410 char *displacement_string_end;
252b5132 4411
24eab124 4412 do_memory_reference:
24eab124
AM
4413 if ((i.mem_operands == 1
4414 && (current_templates->start->opcode_modifier & IsString) == 0)
4415 || i.mem_operands == 2)
4416 {
4417 as_bad (_("too many memory references for `%s'"),
4418 current_templates->start->name);
4419 return 0;
4420 }
252b5132 4421
24eab124
AM
4422 /* Check for base index form. We detect the base index form by
4423 looking for an ')' at the end of the operand, searching
4424 for the '(' matching it, and finding a REGISTER_PREFIX or ','
4425 after the '('. */
af6bdddf 4426 base_string = op_string + strlen (op_string);
c3332e24 4427
af6bdddf
AM
4428 --base_string;
4429 if (is_space_char (*base_string))
4430 --base_string;
252b5132 4431
47926f60 4432 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
4433 displacement_string_start = op_string;
4434 displacement_string_end = base_string + 1;
252b5132 4435
24eab124
AM
4436 if (*base_string == ')')
4437 {
af6bdddf 4438 char *temp_string;
24eab124
AM
4439 unsigned int parens_balanced = 1;
4440 /* We've already checked that the number of left & right ()'s are
47926f60 4441 equal, so this loop will not be infinite. */
24eab124
AM
4442 do
4443 {
4444 base_string--;
4445 if (*base_string == ')')
4446 parens_balanced++;
4447 if (*base_string == '(')
4448 parens_balanced--;
4449 }
4450 while (parens_balanced);
c3332e24 4451
af6bdddf 4452 temp_string = base_string;
c3332e24 4453
24eab124 4454 /* Skip past '(' and whitespace. */
252b5132
RH
4455 ++base_string;
4456 if (is_space_char (*base_string))
24eab124 4457 ++base_string;
252b5132 4458
af6bdddf
AM
4459 if (*base_string == ','
4460 || ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4461 && (i.base_reg = parse_register (base_string, &end_op)) != NULL))
252b5132 4462 {
af6bdddf 4463 displacement_string_end = temp_string;
252b5132 4464
af6bdddf 4465 i.types[this_operand] |= BaseIndex;
252b5132 4466
af6bdddf 4467 if (i.base_reg)
24eab124 4468 {
24eab124
AM
4469 base_string = end_op;
4470 if (is_space_char (*base_string))
4471 ++base_string;
af6bdddf
AM
4472 }
4473
4474 /* There may be an index reg or scale factor here. */
4475 if (*base_string == ',')
4476 {
4477 ++base_string;
4478 if (is_space_char (*base_string))
4479 ++base_string;
4480
4481 if ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4482 && (i.index_reg = parse_register (base_string, &end_op)) != NULL)
24eab124 4483 {
af6bdddf 4484 base_string = end_op;
24eab124
AM
4485 if (is_space_char (*base_string))
4486 ++base_string;
af6bdddf
AM
4487 if (*base_string == ',')
4488 {
4489 ++base_string;
4490 if (is_space_char (*base_string))
4491 ++base_string;
4492 }
e5cb08ac 4493 else if (*base_string != ')')
af6bdddf
AM
4494 {
4495 as_bad (_("expecting `,' or `)' after index register in `%s'"),
4496 operand_string);
4497 return 0;
4498 }
24eab124 4499 }
af6bdddf 4500 else if (*base_string == REGISTER_PREFIX)
24eab124 4501 {
af6bdddf 4502 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
4503 return 0;
4504 }
252b5132 4505
47926f60 4506 /* Check for scale factor. */
551c1ca1 4507 if (*base_string != ')')
af6bdddf 4508 {
551c1ca1
AM
4509 char *end_scale = i386_scale (base_string);
4510
4511 if (!end_scale)
af6bdddf 4512 return 0;
24eab124 4513
551c1ca1 4514 base_string = end_scale;
af6bdddf
AM
4515 if (is_space_char (*base_string))
4516 ++base_string;
4517 if (*base_string != ')')
4518 {
4519 as_bad (_("expecting `)' after scale factor in `%s'"),
4520 operand_string);
4521 return 0;
4522 }
4523 }
4524 else if (!i.index_reg)
24eab124 4525 {
af6bdddf
AM
4526 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
4527 *base_string);
24eab124
AM
4528 return 0;
4529 }
4530 }
af6bdddf 4531 else if (*base_string != ')')
24eab124 4532 {
af6bdddf
AM
4533 as_bad (_("expecting `,' or `)' after base register in `%s'"),
4534 operand_string);
24eab124
AM
4535 return 0;
4536 }
c3332e24 4537 }
af6bdddf 4538 else if (*base_string == REGISTER_PREFIX)
c3332e24 4539 {
af6bdddf 4540 as_bad (_("bad register name `%s'"), base_string);
24eab124 4541 return 0;
c3332e24 4542 }
24eab124
AM
4543 }
4544
4545 /* If there's an expression beginning the operand, parse it,
4546 assuming displacement_string_start and
4547 displacement_string_end are meaningful. */
4548 if (displacement_string_start != displacement_string_end)
4549 {
4550 if (!i386_displacement (displacement_string_start,
4551 displacement_string_end))
4552 return 0;
4553 }
4554
4555 /* Special case for (%dx) while doing input/output op. */
4556 if (i.base_reg
4557 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
4558 && i.index_reg == 0
4559 && i.log2_scale_factor == 0
4560 && i.seg[i.mem_operands] == 0
4561 && (i.types[this_operand] & Disp) == 0)
4562 {
4563 i.types[this_operand] = InOutPortReg;
4564 return 1;
4565 }
4566
eecb386c
AM
4567 if (i386_index_check (operand_string) == 0)
4568 return 0;
24eab124
AM
4569 i.mem_operands++;
4570 }
4571 else
ce8a8b2f
AM
4572 {
4573 /* It's not a memory operand; argh! */
24eab124
AM
4574 as_bad (_("invalid char %s beginning operand %d `%s'"),
4575 output_invalid (*op_string),
4576 this_operand + 1,
4577 op_string);
4578 return 0;
4579 }
47926f60 4580 return 1; /* Normal return. */
252b5132
RH
4581}
4582\f
ee7fcc42
AM
4583/* md_estimate_size_before_relax()
4584
4585 Called just before relax() for rs_machine_dependent frags. The x86
4586 assembler uses these frags to handle variable size jump
4587 instructions.
4588
4589 Any symbol that is now undefined will not become defined.
4590 Return the correct fr_subtype in the frag.
4591 Return the initial "guess for variable size of frag" to caller.
4592 The guess is actually the growth beyond the fixed part. Whatever
4593 we do to grow the fixed or variable part contributes to our
4594 returned value. */
4595
252b5132
RH
4596int
4597md_estimate_size_before_relax (fragP, segment)
29b0f896
AM
4598 fragS *fragP;
4599 segT segment;
252b5132 4600{
252b5132 4601 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
4602 check for un-relaxable symbols. On an ELF system, we can't relax
4603 an externally visible symbol, because it may be overridden by a
4604 shared library. */
4605 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 4606#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
31312f95
AM
4607 || (OUTPUT_FLAVOR == bfd_target_elf_flavour
4608 && (S_IS_EXTERNAL (fragP->fr_symbol)
4609 || S_IS_WEAK (fragP->fr_symbol)))
b98ef147
AM
4610#endif
4611 )
252b5132 4612 {
b98ef147
AM
4613 /* Symbol is undefined in this segment, or we need to keep a
4614 reloc so that weak symbols can be overridden. */
4615 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 4616 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
4617 unsigned char *opcode;
4618 int old_fr_fix;
f6af82bd 4619
ee7fcc42
AM
4620 if (fragP->fr_var != NO_RELOC)
4621 reloc_type = fragP->fr_var;
b98ef147 4622 else if (size == 2)
f6af82bd
AM
4623 reloc_type = BFD_RELOC_16_PCREL;
4624 else
4625 reloc_type = BFD_RELOC_32_PCREL;
252b5132 4626
ee7fcc42
AM
4627 old_fr_fix = fragP->fr_fix;
4628 opcode = (unsigned char *) fragP->fr_opcode;
4629
fddf5b5b 4630 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 4631 {
fddf5b5b
AM
4632 case UNCOND_JUMP:
4633 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 4634 opcode[0] = 0xe9;
252b5132 4635 fragP->fr_fix += size;
062cd5e7
AS
4636 fix_new (fragP, old_fr_fix, size,
4637 fragP->fr_symbol,
4638 fragP->fr_offset, 1,
4639 reloc_type);
252b5132
RH
4640 break;
4641
fddf5b5b 4642 case COND_JUMP86:
412167cb
AM
4643 if (size == 2
4644 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
4645 {
4646 /* Negate the condition, and branch past an
4647 unconditional jump. */
4648 opcode[0] ^= 1;
4649 opcode[1] = 3;
4650 /* Insert an unconditional jump. */
4651 opcode[2] = 0xe9;
4652 /* We added two extra opcode bytes, and have a two byte
4653 offset. */
4654 fragP->fr_fix += 2 + 2;
062cd5e7
AS
4655 fix_new (fragP, old_fr_fix + 2, 2,
4656 fragP->fr_symbol,
4657 fragP->fr_offset, 1,
4658 reloc_type);
fddf5b5b
AM
4659 break;
4660 }
4661 /* Fall through. */
4662
4663 case COND_JUMP:
412167cb
AM
4664 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
4665 {
3e02c1cc
AM
4666 fixS *fixP;
4667
412167cb 4668 fragP->fr_fix += 1;
3e02c1cc
AM
4669 fixP = fix_new (fragP, old_fr_fix, 1,
4670 fragP->fr_symbol,
4671 fragP->fr_offset, 1,
4672 BFD_RELOC_8_PCREL);
4673 fixP->fx_signed = 1;
412167cb
AM
4674 break;
4675 }
93c2a809 4676
24eab124 4677 /* This changes the byte-displacement jump 0x7N
fddf5b5b 4678 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 4679 opcode[1] = opcode[0] + 0x10;
f6af82bd 4680 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
4681 /* We've added an opcode byte. */
4682 fragP->fr_fix += 1 + size;
062cd5e7
AS
4683 fix_new (fragP, old_fr_fix + 1, size,
4684 fragP->fr_symbol,
4685 fragP->fr_offset, 1,
4686 reloc_type);
252b5132 4687 break;
fddf5b5b
AM
4688
4689 default:
4690 BAD_CASE (fragP->fr_subtype);
4691 break;
252b5132
RH
4692 }
4693 frag_wane (fragP);
ee7fcc42 4694 return fragP->fr_fix - old_fr_fix;
252b5132 4695 }
93c2a809 4696
93c2a809
AM
4697 /* Guess size depending on current relax state. Initially the relax
4698 state will correspond to a short jump and we return 1, because
4699 the variable part of the frag (the branch offset) is one byte
4700 long. However, we can relax a section more than once and in that
4701 case we must either set fr_subtype back to the unrelaxed state,
4702 or return the value for the appropriate branch. */
4703 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
4704}
4705
47926f60
KH
4706/* Called after relax() is finished.
4707
4708 In: Address of frag.
4709 fr_type == rs_machine_dependent.
4710 fr_subtype is what the address relaxed to.
4711
4712 Out: Any fixSs and constants are set up.
4713 Caller will turn frag into a ".space 0". */
4714
252b5132
RH
4715void
4716md_convert_frag (abfd, sec, fragP)
ab9da554
ILT
4717 bfd *abfd ATTRIBUTE_UNUSED;
4718 segT sec ATTRIBUTE_UNUSED;
29b0f896 4719 fragS *fragP;
252b5132 4720{
29b0f896 4721 unsigned char *opcode;
252b5132 4722 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
4723 offsetT target_address;
4724 offsetT opcode_address;
252b5132 4725 unsigned int extension = 0;
847f7ad4 4726 offsetT displacement_from_opcode_start;
252b5132
RH
4727
4728 opcode = (unsigned char *) fragP->fr_opcode;
4729
47926f60 4730 /* Address we want to reach in file space. */
252b5132 4731 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 4732
47926f60 4733 /* Address opcode resides at in file space. */
252b5132
RH
4734 opcode_address = fragP->fr_address + fragP->fr_fix;
4735
47926f60 4736 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
4737 displacement_from_opcode_start = target_address - opcode_address;
4738
fddf5b5b 4739 if ((fragP->fr_subtype & BIG) == 0)
252b5132 4740 {
47926f60
KH
4741 /* Don't have to change opcode. */
4742 extension = 1; /* 1 opcode + 1 displacement */
252b5132 4743 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
4744 }
4745 else
4746 {
4747 if (no_cond_jump_promotion
4748 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4749 as_warn_where (fragP->fr_file, fragP->fr_line, _("long jump required"));
252b5132 4750
fddf5b5b
AM
4751 switch (fragP->fr_subtype)
4752 {
4753 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
4754 extension = 4; /* 1 opcode + 4 displacement */
4755 opcode[0] = 0xe9;
4756 where_to_put_displacement = &opcode[1];
4757 break;
252b5132 4758
fddf5b5b
AM
4759 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
4760 extension = 2; /* 1 opcode + 2 displacement */
4761 opcode[0] = 0xe9;
4762 where_to_put_displacement = &opcode[1];
4763 break;
252b5132 4764
fddf5b5b
AM
4765 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
4766 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
4767 extension = 5; /* 2 opcode + 4 displacement */
4768 opcode[1] = opcode[0] + 0x10;
4769 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4770 where_to_put_displacement = &opcode[2];
4771 break;
252b5132 4772
fddf5b5b
AM
4773 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
4774 extension = 3; /* 2 opcode + 2 displacement */
4775 opcode[1] = opcode[0] + 0x10;
4776 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4777 where_to_put_displacement = &opcode[2];
4778 break;
252b5132 4779
fddf5b5b
AM
4780 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
4781 extension = 4;
4782 opcode[0] ^= 1;
4783 opcode[1] = 3;
4784 opcode[2] = 0xe9;
4785 where_to_put_displacement = &opcode[3];
4786 break;
4787
4788 default:
4789 BAD_CASE (fragP->fr_subtype);
4790 break;
4791 }
252b5132 4792 }
fddf5b5b 4793
47926f60 4794 /* Now put displacement after opcode. */
252b5132
RH
4795 md_number_to_chars ((char *) where_to_put_displacement,
4796 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 4797 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
4798 fragP->fr_fix += extension;
4799}
4800\f
47926f60
KH
4801/* Size of byte displacement jmp. */
4802int md_short_jump_size = 2;
4803
4804/* Size of dword displacement jmp. */
4805int md_long_jump_size = 5;
252b5132 4806
47926f60
KH
4807/* Size of relocation record. */
4808const int md_reloc_size = 8;
252b5132
RH
4809
4810void
4811md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
4812 char *ptr;
4813 addressT from_addr, to_addr;
ab9da554
ILT
4814 fragS *frag ATTRIBUTE_UNUSED;
4815 symbolS *to_symbol ATTRIBUTE_UNUSED;
252b5132 4816{
847f7ad4 4817 offsetT offset;
252b5132
RH
4818
4819 offset = to_addr - (from_addr + 2);
47926f60
KH
4820 /* Opcode for byte-disp jump. */
4821 md_number_to_chars (ptr, (valueT) 0xeb, 1);
252b5132
RH
4822 md_number_to_chars (ptr + 1, (valueT) offset, 1);
4823}
4824
4825void
4826md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
4827 char *ptr;
4828 addressT from_addr, to_addr;
a38cf1db
AM
4829 fragS *frag ATTRIBUTE_UNUSED;
4830 symbolS *to_symbol ATTRIBUTE_UNUSED;
252b5132 4831{
847f7ad4 4832 offsetT offset;
252b5132 4833
a38cf1db
AM
4834 offset = to_addr - (from_addr + 5);
4835 md_number_to_chars (ptr, (valueT) 0xe9, 1);
4836 md_number_to_chars (ptr + 1, (valueT) offset, 4);
252b5132
RH
4837}
4838\f
4839/* Apply a fixup (fixS) to segment data, once it has been determined
4840 by our caller that we have all the info we need to fix it up.
4841
4842 On the 386, immediates, displacements, and data pointers are all in
4843 the same (little-endian) format, so we don't need to care about which
4844 we are handling. */
4845
94f592af 4846void
55cf6793 4847md_apply_fix (fixP, valP, seg)
47926f60
KH
4848 /* The fix we're to put in. */
4849 fixS *fixP;
47926f60 4850 /* Pointer to the value of the bits. */
c6682705 4851 valueT *valP;
47926f60
KH
4852 /* Segment fix is from. */
4853 segT seg ATTRIBUTE_UNUSED;
252b5132 4854{
94f592af 4855 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 4856 valueT value = *valP;
252b5132 4857
f86103b7 4858#if !defined (TE_Mach)
93382f6d
AM
4859 if (fixP->fx_pcrel)
4860 {
4861 switch (fixP->fx_r_type)
4862 {
5865bb77
ILT
4863 default:
4864 break;
4865
d6ab8113
JB
4866 case BFD_RELOC_64:
4867 fixP->fx_r_type = BFD_RELOC_64_PCREL;
4868 break;
93382f6d 4869 case BFD_RELOC_32:
ae8887b5 4870 case BFD_RELOC_X86_64_32S:
93382f6d
AM
4871 fixP->fx_r_type = BFD_RELOC_32_PCREL;
4872 break;
4873 case BFD_RELOC_16:
4874 fixP->fx_r_type = BFD_RELOC_16_PCREL;
4875 break;
4876 case BFD_RELOC_8:
4877 fixP->fx_r_type = BFD_RELOC_8_PCREL;
4878 break;
4879 }
4880 }
252b5132 4881
a161fe53 4882 if (fixP->fx_addsy != NULL
31312f95 4883 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 4884 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95
AM
4885 || fixP->fx_r_type == BFD_RELOC_16_PCREL
4886 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
4887 && !use_rela_relocations)
252b5132 4888 {
31312f95
AM
4889 /* This is a hack. There should be a better way to handle this.
4890 This covers for the fact that bfd_install_relocation will
4891 subtract the current location (for partial_inplace, PC relative
4892 relocations); see more below. */
252b5132
RH
4893#ifndef OBJ_AOUT
4894 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4895#ifdef TE_PE
4896 || OUTPUT_FLAVOR == bfd_target_coff_flavour
4897#endif
4898 )
4899 value += fixP->fx_where + fixP->fx_frag->fr_address;
4900#endif
4901#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2f66722d 4902 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 4903 {
6539b54b 4904 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 4905
6539b54b 4906 if ((sym_seg == seg
2f66722d 4907 || (symbol_section_p (fixP->fx_addsy)
6539b54b 4908 && sym_seg != absolute_section))
ae6063d4 4909 && !generic_force_reloc (fixP))
2f66722d
AM
4910 {
4911 /* Yes, we add the values in twice. This is because
6539b54b
AM
4912 bfd_install_relocation subtracts them out again. I think
4913 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
4914 it. FIXME. */
4915 value += fixP->fx_where + fixP->fx_frag->fr_address;
4916 }
252b5132
RH
4917 }
4918#endif
4919#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
4920 /* For some reason, the PE format does not store a
4921 section address offset for a PC relative symbol. */
4922 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
4923#if defined(BFD_ASSEMBLER) || defined(S_IS_WEAK)
4924 || S_IS_WEAK (fixP->fx_addsy)
4925#endif
4926 )
252b5132
RH
4927 value += md_pcrel_from (fixP);
4928#endif
4929 }
4930
4931 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 4932 and we must not disappoint it. */
252b5132
RH
4933#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4934 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4935 && fixP->fx_addsy)
47926f60
KH
4936 switch (fixP->fx_r_type)
4937 {
4938 case BFD_RELOC_386_PLT32:
3e73aa7c 4939 case BFD_RELOC_X86_64_PLT32:
47926f60
KH
4940 /* Make the jump instruction point to the address of the operand. At
4941 runtime we merely add the offset to the actual PLT entry. */
4942 value = -4;
4943 break;
31312f95 4944
13ae64f3
JJ
4945 case BFD_RELOC_386_TLS_GD:
4946 case BFD_RELOC_386_TLS_LDM:
13ae64f3 4947 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
4948 case BFD_RELOC_386_TLS_IE:
4949 case BFD_RELOC_386_TLS_GOTIE:
bffbf940
JJ
4950 case BFD_RELOC_X86_64_TLSGD:
4951 case BFD_RELOC_X86_64_TLSLD:
4952 case BFD_RELOC_X86_64_GOTTPOFF:
00f7efb6
JJ
4953 value = 0; /* Fully resolved at runtime. No addend. */
4954 /* Fallthrough */
4955 case BFD_RELOC_386_TLS_LE:
4956 case BFD_RELOC_386_TLS_LDO_32:
4957 case BFD_RELOC_386_TLS_LE_32:
4958 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 4959 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 4960 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 4961 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
4962 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4963 break;
4964
4965 case BFD_RELOC_386_GOT32:
4966 case BFD_RELOC_X86_64_GOT32:
47926f60
KH
4967 value = 0; /* Fully resolved at runtime. No addend. */
4968 break;
47926f60
KH
4969
4970 case BFD_RELOC_VTABLE_INHERIT:
4971 case BFD_RELOC_VTABLE_ENTRY:
4972 fixP->fx_done = 0;
94f592af 4973 return;
47926f60
KH
4974
4975 default:
4976 break;
4977 }
4978#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 4979 *valP = value;
f86103b7 4980#endif /* !defined (TE_Mach) */
3e73aa7c 4981
3e73aa7c 4982 /* Are we finished with this relocation now? */
c6682705 4983 if (fixP->fx_addsy == NULL)
3e73aa7c
JH
4984 fixP->fx_done = 1;
4985 else if (use_rela_relocations)
4986 {
4987 fixP->fx_no_overflow = 1;
062cd5e7
AS
4988 /* Remember value for tc_gen_reloc. */
4989 fixP->fx_addnumber = value;
3e73aa7c
JH
4990 value = 0;
4991 }
f86103b7 4992
94f592af 4993 md_number_to_chars (p, value, fixP->fx_size);
252b5132 4994}
252b5132 4995\f
252b5132
RH
4996#define MAX_LITTLENUMS 6
4997
47926f60
KH
4998/* Turn the string pointed to by litP into a floating point constant
4999 of type TYPE, and emit the appropriate bytes. The number of
5000 LITTLENUMS emitted is stored in *SIZEP. An error message is
5001 returned, or NULL on OK. */
5002
252b5132
RH
5003char *
5004md_atof (type, litP, sizeP)
2ab9b79e 5005 int type;
252b5132
RH
5006 char *litP;
5007 int *sizeP;
5008{
5009 int prec;
5010 LITTLENUM_TYPE words[MAX_LITTLENUMS];
5011 LITTLENUM_TYPE *wordP;
5012 char *t;
5013
5014 switch (type)
5015 {
5016 case 'f':
5017 case 'F':
5018 prec = 2;
5019 break;
5020
5021 case 'd':
5022 case 'D':
5023 prec = 4;
5024 break;
5025
5026 case 'x':
5027 case 'X':
5028 prec = 5;
5029 break;
5030
5031 default:
5032 *sizeP = 0;
5033 return _("Bad call to md_atof ()");
5034 }
5035 t = atof_ieee (input_line_pointer, type, words);
5036 if (t)
5037 input_line_pointer = t;
5038
5039 *sizeP = prec * sizeof (LITTLENUM_TYPE);
5040 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
5041 the bigendian 386. */
5042 for (wordP = words + prec - 1; prec--;)
5043 {
5044 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
5045 litP += sizeof (LITTLENUM_TYPE);
5046 }
5047 return 0;
5048}
5049\f
87c245cc 5050static char output_invalid_buf[8];
252b5132 5051
252b5132
RH
5052static char *
5053output_invalid (c)
5054 int c;
5055{
3882b010 5056 if (ISPRINT (c))
252b5132
RH
5057 sprintf (output_invalid_buf, "'%c'", c);
5058 else
5059 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
5060 return output_invalid_buf;
5061}
5062
af6bdddf 5063/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
5064
5065static const reg_entry *
5066parse_register (reg_string, end_op)
5067 char *reg_string;
5068 char **end_op;
5069{
af6bdddf
AM
5070 char *s = reg_string;
5071 char *p;
252b5132
RH
5072 char reg_name_given[MAX_REG_NAME_SIZE + 1];
5073 const reg_entry *r;
5074
5075 /* Skip possible REGISTER_PREFIX and possible whitespace. */
5076 if (*s == REGISTER_PREFIX)
5077 ++s;
5078
5079 if (is_space_char (*s))
5080 ++s;
5081
5082 p = reg_name_given;
af6bdddf 5083 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
5084 {
5085 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
5086 return (const reg_entry *) NULL;
5087 s++;
252b5132
RH
5088 }
5089
6588847e
DN
5090 /* For naked regs, make sure that we are not dealing with an identifier.
5091 This prevents confusing an identifier like `eax_var' with register
5092 `eax'. */
5093 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
5094 return (const reg_entry *) NULL;
5095
af6bdddf 5096 *end_op = s;
252b5132
RH
5097
5098 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
5099
5f47d35b 5100 /* Handle floating point regs, allowing spaces in the (i) part. */
47926f60 5101 if (r == i386_regtab /* %st is first entry of table */)
5f47d35b 5102 {
5f47d35b
AM
5103 if (is_space_char (*s))
5104 ++s;
5105 if (*s == '(')
5106 {
af6bdddf 5107 ++s;
5f47d35b
AM
5108 if (is_space_char (*s))
5109 ++s;
5110 if (*s >= '0' && *s <= '7')
5111 {
5112 r = &i386_float_regtab[*s - '0'];
af6bdddf 5113 ++s;
5f47d35b
AM
5114 if (is_space_char (*s))
5115 ++s;
5116 if (*s == ')')
5117 {
5118 *end_op = s + 1;
5119 return r;
5120 }
5f47d35b 5121 }
47926f60 5122 /* We have "%st(" then garbage. */
5f47d35b
AM
5123 return (const reg_entry *) NULL;
5124 }
5125 }
5126
1ae00879 5127 if (r != NULL
20f0a1fc 5128 && ((r->reg_flags & (RegRex64 | RegRex)) | (r->reg_type & Reg64)) != 0
c4a530c5 5129 && (r->reg_type != Control || !(cpu_arch_flags & CpuSledgehammer))
1ae00879 5130 && flag_code != CODE_64BIT)
20f0a1fc 5131 return (const reg_entry *) NULL;
1ae00879 5132
252b5132
RH
5133 return r;
5134}
5135\f
4cc782b5 5136#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12b55ccc 5137const char *md_shortopts = "kVQ:sqn";
252b5132 5138#else
12b55ccc 5139const char *md_shortopts = "qn";
252b5132 5140#endif
6e0b89ee 5141
252b5132 5142struct option md_longopts[] = {
3e73aa7c
JH
5143#define OPTION_32 (OPTION_MD_BASE + 0)
5144 {"32", no_argument, NULL, OPTION_32},
6e0b89ee 5145#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3e73aa7c
JH
5146#define OPTION_64 (OPTION_MD_BASE + 1)
5147 {"64", no_argument, NULL, OPTION_64},
6e0b89ee 5148#endif
252b5132
RH
5149 {NULL, no_argument, NULL, 0}
5150};
5151size_t md_longopts_size = sizeof (md_longopts);
5152
5153int
5154md_parse_option (c, arg)
5155 int c;
ab9da554 5156 char *arg ATTRIBUTE_UNUSED;
252b5132
RH
5157{
5158 switch (c)
5159 {
12b55ccc
L
5160 case 'n':
5161 optimize_align_code = 0;
5162 break;
5163
a38cf1db
AM
5164 case 'q':
5165 quiet_warnings = 1;
252b5132
RH
5166 break;
5167
5168#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
5169 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
5170 should be emitted or not. FIXME: Not implemented. */
5171 case 'Q':
252b5132
RH
5172 break;
5173
5174 /* -V: SVR4 argument to print version ID. */
5175 case 'V':
5176 print_version_id ();
5177 break;
5178
a38cf1db
AM
5179 /* -k: Ignore for FreeBSD compatibility. */
5180 case 'k':
252b5132 5181 break;
4cc782b5
ILT
5182
5183 case 's':
5184 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 5185 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 5186 break;
6e0b89ee 5187
3e73aa7c
JH
5188 case OPTION_64:
5189 {
5190 const char **list, **l;
5191
3e73aa7c
JH
5192 list = bfd_target_list ();
5193 for (l = list; *l != NULL; l++)
6e0b89ee
AM
5194 if (strcmp (*l, "elf64-x86-64") == 0)
5195 {
5196 default_arch = "x86_64";
5197 break;
5198 }
3e73aa7c 5199 if (*l == NULL)
6e0b89ee 5200 as_fatal (_("No compiled in support for x86_64"));
3e73aa7c
JH
5201 free (list);
5202 }
5203 break;
5204#endif
252b5132 5205
6e0b89ee
AM
5206 case OPTION_32:
5207 default_arch = "i386";
5208 break;
5209
252b5132
RH
5210 default:
5211 return 0;
5212 }
5213 return 1;
5214}
5215
5216void
5217md_show_usage (stream)
5218 FILE *stream;
5219{
4cc782b5
ILT
5220#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5221 fprintf (stream, _("\
a38cf1db
AM
5222 -Q ignored\n\
5223 -V print assembler version number\n\
5224 -k ignored\n\
12b55ccc 5225 -n Do not optimize code alignment\n\
a38cf1db
AM
5226 -q quieten some warnings\n\
5227 -s ignored\n"));
5228#else
5229 fprintf (stream, _("\
12b55ccc 5230 -n Do not optimize code alignment\n\
a38cf1db 5231 -q quieten some warnings\n"));
4cc782b5 5232#endif
252b5132
RH
5233}
5234
3e73aa7c
JH
5235#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
5236 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
252b5132
RH
5237
5238/* Pick the target format to use. */
5239
47926f60 5240const char *
252b5132
RH
5241i386_target_format ()
5242{
3e73aa7c
JH
5243 if (!strcmp (default_arch, "x86_64"))
5244 set_code_flag (CODE_64BIT);
5245 else if (!strcmp (default_arch, "i386"))
5246 set_code_flag (CODE_32BIT);
5247 else
5248 as_fatal (_("Unknown architecture"));
252b5132
RH
5249 switch (OUTPUT_FLAVOR)
5250 {
4c63da97
AM
5251#ifdef OBJ_MAYBE_AOUT
5252 case bfd_target_aout_flavour:
47926f60 5253 return AOUT_TARGET_FORMAT;
4c63da97
AM
5254#endif
5255#ifdef OBJ_MAYBE_COFF
252b5132
RH
5256 case bfd_target_coff_flavour:
5257 return "coff-i386";
4c63da97 5258#endif
3e73aa7c 5259#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 5260 case bfd_target_elf_flavour:
3e73aa7c 5261 {
e5cb08ac
KH
5262 if (flag_code == CODE_64BIT)
5263 use_rela_relocations = 1;
4ada7262 5264 return flag_code == CODE_64BIT ? "elf64-x86-64" : ELF_TARGET_FORMAT;
3e73aa7c 5265 }
4c63da97 5266#endif
252b5132
RH
5267 default:
5268 abort ();
5269 return NULL;
5270 }
5271}
5272
47926f60 5273#endif /* OBJ_MAYBE_ more than one */
a847613f
AM
5274
5275#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
5276void i386_elf_emit_arch_note ()
5277{
5278 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
5279 && cpu_arch_name != NULL)
5280 {
5281 char *p;
5282 asection *seg = now_seg;
5283 subsegT subseg = now_subseg;
5284 Elf_Internal_Note i_note;
5285 Elf_External_Note e_note;
5286 asection *note_secp;
5287 int len;
5288
5289 /* Create the .note section. */
5290 note_secp = subseg_new (".note", 0);
5291 bfd_set_section_flags (stdoutput,
5292 note_secp,
5293 SEC_HAS_CONTENTS | SEC_READONLY);
5294
5295 /* Process the arch string. */
5296 len = strlen (cpu_arch_name);
5297
5298 i_note.namesz = len + 1;
5299 i_note.descsz = 0;
5300 i_note.type = NT_ARCH;
5301 p = frag_more (sizeof (e_note.namesz));
5302 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
5303 p = frag_more (sizeof (e_note.descsz));
5304 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
5305 p = frag_more (sizeof (e_note.type));
5306 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
5307 p = frag_more (len + 1);
5308 strcpy (p, cpu_arch_name);
5309
5310 frag_align (2, 0, 0);
5311
5312 subseg_set (seg, subseg);
5313 }
5314}
5315#endif
252b5132 5316\f
252b5132
RH
5317symbolS *
5318md_undefined_symbol (name)
5319 char *name;
5320{
18dc2407
ILT
5321 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
5322 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
5323 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
5324 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
5325 {
5326 if (!GOT_symbol)
5327 {
5328 if (symbol_find (name))
5329 as_bad (_("GOT already in symbol table"));
5330 GOT_symbol = symbol_new (name, undefined_section,
5331 (valueT) 0, &zero_address_frag);
5332 };
5333 return GOT_symbol;
5334 }
252b5132
RH
5335 return 0;
5336}
5337
5338/* Round up a section size to the appropriate boundary. */
47926f60 5339
252b5132
RH
5340valueT
5341md_section_align (segment, size)
ab9da554 5342 segT segment ATTRIBUTE_UNUSED;
252b5132
RH
5343 valueT size;
5344{
4c63da97
AM
5345#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5346 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
5347 {
5348 /* For a.out, force the section size to be aligned. If we don't do
5349 this, BFD will align it for us, but it will not write out the
5350 final bytes of the section. This may be a bug in BFD, but it is
5351 easier to fix it here since that is how the other a.out targets
5352 work. */
5353 int align;
5354
5355 align = bfd_get_section_alignment (stdoutput, segment);
5356 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
5357 }
252b5132
RH
5358#endif
5359
5360 return size;
5361}
5362
5363/* On the i386, PC-relative offsets are relative to the start of the
5364 next instruction. That is, the address of the offset, plus its
5365 size, since the offset is always the last part of the insn. */
5366
5367long
5368md_pcrel_from (fixP)
5369 fixS *fixP;
5370{
5371 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
5372}
5373
5374#ifndef I386COFF
5375
5376static void
5377s_bss (ignore)
ab9da554 5378 int ignore ATTRIBUTE_UNUSED;
252b5132 5379{
29b0f896 5380 int temp;
252b5132 5381
8a75718c
JB
5382#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5383 if (IS_ELF)
5384 obj_elf_section_change_hook ();
5385#endif
252b5132
RH
5386 temp = get_absolute_expression ();
5387 subseg_set (bss_section, (subsegT) temp);
5388 demand_empty_rest_of_line ();
5389}
5390
5391#endif
5392
252b5132
RH
5393void
5394i386_validate_fix (fixp)
5395 fixS *fixp;
5396{
5397 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
5398 {
23df1078
JH
5399 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
5400 {
5401 if (flag_code != CODE_64BIT)
5402 abort ();
5403 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
5404 }
5405 else
5406 {
d6ab8113
JB
5407 if (flag_code != CODE_64BIT)
5408 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
5409 else
5410 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
23df1078 5411 }
252b5132
RH
5412 fixp->fx_subsy = 0;
5413 }
5414}
5415
252b5132
RH
5416arelent *
5417tc_gen_reloc (section, fixp)
ab9da554 5418 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
5419 fixS *fixp;
5420{
5421 arelent *rel;
5422 bfd_reloc_code_real_type code;
5423
5424 switch (fixp->fx_r_type)
5425 {
3e73aa7c
JH
5426 case BFD_RELOC_X86_64_PLT32:
5427 case BFD_RELOC_X86_64_GOT32:
5428 case BFD_RELOC_X86_64_GOTPCREL:
252b5132
RH
5429 case BFD_RELOC_386_PLT32:
5430 case BFD_RELOC_386_GOT32:
5431 case BFD_RELOC_386_GOTOFF:
5432 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
5433 case BFD_RELOC_386_TLS_GD:
5434 case BFD_RELOC_386_TLS_LDM:
5435 case BFD_RELOC_386_TLS_LDO_32:
5436 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
5437 case BFD_RELOC_386_TLS_IE:
5438 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
5439 case BFD_RELOC_386_TLS_LE_32:
5440 case BFD_RELOC_386_TLS_LE:
bffbf940
JJ
5441 case BFD_RELOC_X86_64_TLSGD:
5442 case BFD_RELOC_X86_64_TLSLD:
5443 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 5444 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
5445 case BFD_RELOC_X86_64_GOTTPOFF:
5446 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
5447 case BFD_RELOC_X86_64_TPOFF64:
5448 case BFD_RELOC_X86_64_GOTOFF64:
5449 case BFD_RELOC_X86_64_GOTPC32:
252b5132
RH
5450 case BFD_RELOC_RVA:
5451 case BFD_RELOC_VTABLE_ENTRY:
5452 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
5453#ifdef TE_PE
5454 case BFD_RELOC_32_SECREL:
5455#endif
252b5132
RH
5456 code = fixp->fx_r_type;
5457 break;
dbbaec26
L
5458 case BFD_RELOC_X86_64_32S:
5459 if (!fixp->fx_pcrel)
5460 {
5461 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
5462 code = fixp->fx_r_type;
5463 break;
5464 }
252b5132 5465 default:
93382f6d 5466 if (fixp->fx_pcrel)
252b5132 5467 {
93382f6d
AM
5468 switch (fixp->fx_size)
5469 {
5470 default:
b091f402
AM
5471 as_bad_where (fixp->fx_file, fixp->fx_line,
5472 _("can not do %d byte pc-relative relocation"),
5473 fixp->fx_size);
93382f6d
AM
5474 code = BFD_RELOC_32_PCREL;
5475 break;
5476 case 1: code = BFD_RELOC_8_PCREL; break;
5477 case 2: code = BFD_RELOC_16_PCREL; break;
5478 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
5479#ifdef BFD64
5480 case 8: code = BFD_RELOC_64_PCREL; break;
5481#endif
93382f6d
AM
5482 }
5483 }
5484 else
5485 {
5486 switch (fixp->fx_size)
5487 {
5488 default:
b091f402
AM
5489 as_bad_where (fixp->fx_file, fixp->fx_line,
5490 _("can not do %d byte relocation"),
5491 fixp->fx_size);
93382f6d
AM
5492 code = BFD_RELOC_32;
5493 break;
5494 case 1: code = BFD_RELOC_8; break;
5495 case 2: code = BFD_RELOC_16; break;
5496 case 4: code = BFD_RELOC_32; break;
937149dd 5497#ifdef BFD64
3e73aa7c 5498 case 8: code = BFD_RELOC_64; break;
937149dd 5499#endif
93382f6d 5500 }
252b5132
RH
5501 }
5502 break;
5503 }
252b5132 5504
d6ab8113 5505 if ((code == BFD_RELOC_32 || code == BFD_RELOC_32_PCREL)
252b5132
RH
5506 && GOT_symbol
5507 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 5508 {
d6ab8113
JB
5509 if (flag_code != CODE_64BIT)
5510 code = BFD_RELOC_386_GOTPC;
5511 else
5512 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 5513 }
252b5132
RH
5514
5515 rel = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
5516 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5517 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
5518
5519 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 5520
3e73aa7c
JH
5521 if (!use_rela_relocations)
5522 {
5523 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
5524 vtable entry to be used in the relocation's section offset. */
5525 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5526 rel->address = fixp->fx_offset;
252b5132 5527
c6682705 5528 rel->addend = 0;
3e73aa7c
JH
5529 }
5530 /* Use the rela in 64bit mode. */
252b5132 5531 else
3e73aa7c 5532 {
062cd5e7
AS
5533 if (!fixp->fx_pcrel)
5534 rel->addend = fixp->fx_offset;
5535 else
5536 switch (code)
5537 {
5538 case BFD_RELOC_X86_64_PLT32:
5539 case BFD_RELOC_X86_64_GOT32:
5540 case BFD_RELOC_X86_64_GOTPCREL:
bffbf940
JJ
5541 case BFD_RELOC_X86_64_TLSGD:
5542 case BFD_RELOC_X86_64_TLSLD:
5543 case BFD_RELOC_X86_64_GOTTPOFF:
062cd5e7
AS
5544 rel->addend = fixp->fx_offset - fixp->fx_size;
5545 break;
5546 default:
5547 rel->addend = (section->vma
5548 - fixp->fx_size
5549 + fixp->fx_addnumber
5550 + md_pcrel_from (fixp));
5551 break;
5552 }
3e73aa7c
JH
5553 }
5554
252b5132
RH
5555 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
5556 if (rel->howto == NULL)
5557 {
5558 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 5559 _("cannot represent relocation type %s"),
252b5132
RH
5560 bfd_get_reloc_code_name (code));
5561 /* Set howto to a garbage value so that we can keep going. */
5562 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
5563 assert (rel->howto != NULL);
5564 }
5565
5566 return rel;
5567}
5568
64a0c779
DN
5569\f
5570/* Parse operands using Intel syntax. This implements a recursive descent
5571 parser based on the BNF grammar published in Appendix B of the MASM 6.1
5572 Programmer's Guide.
5573
5574 FIXME: We do not recognize the full operand grammar defined in the MASM
5575 documentation. In particular, all the structure/union and
5576 high-level macro operands are missing.
5577
5578 Uppercase words are terminals, lower case words are non-terminals.
5579 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
5580 bars '|' denote choices. Most grammar productions are implemented in
5581 functions called 'intel_<production>'.
5582
5583 Initial production is 'expr'.
5584
9306ca4a 5585 addOp + | -
64a0c779
DN
5586
5587 alpha [a-zA-Z]
5588
9306ca4a
JB
5589 binOp & | AND | \| | OR | ^ | XOR
5590
64a0c779
DN
5591 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
5592
5593 constant digits [[ radixOverride ]]
5594
9306ca4a 5595 dataType BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD
64a0c779
DN
5596
5597 digits decdigit
b77a7acd
AJ
5598 | digits decdigit
5599 | digits hexdigit
64a0c779
DN
5600
5601 decdigit [0-9]
5602
9306ca4a
JB
5603 e04 e04 addOp e05
5604 | e05
5605
5606 e05 e05 binOp e06
b77a7acd 5607 | e06
64a0c779
DN
5608
5609 e06 e06 mulOp e09
b77a7acd 5610 | e09
64a0c779
DN
5611
5612 e09 OFFSET e10
a724f0f4
JB
5613 | SHORT e10
5614 | + e10
5615 | - e10
9306ca4a
JB
5616 | ~ e10
5617 | NOT e10
64a0c779
DN
5618 | e09 PTR e10
5619 | e09 : e10
5620 | e10
5621
5622 e10 e10 [ expr ]
b77a7acd 5623 | e11
64a0c779
DN
5624
5625 e11 ( expr )
b77a7acd 5626 | [ expr ]
64a0c779
DN
5627 | constant
5628 | dataType
5629 | id
5630 | $
5631 | register
5632
a724f0f4 5633 => expr expr cmpOp e04
9306ca4a 5634 | e04
64a0c779
DN
5635
5636 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
b77a7acd 5637 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
64a0c779
DN
5638
5639 hexdigit a | b | c | d | e | f
b77a7acd 5640 | A | B | C | D | E | F
64a0c779
DN
5641
5642 id alpha
b77a7acd 5643 | id alpha
64a0c779
DN
5644 | id decdigit
5645
9306ca4a 5646 mulOp * | / | % | MOD | << | SHL | >> | SHR
64a0c779
DN
5647
5648 quote " | '
5649
5650 register specialRegister
b77a7acd 5651 | gpRegister
64a0c779
DN
5652 | byteRegister
5653
5654 segmentRegister CS | DS | ES | FS | GS | SS
5655
9306ca4a 5656 specialRegister CR0 | CR2 | CR3 | CR4
b77a7acd 5657 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
64a0c779
DN
5658 | TR3 | TR4 | TR5 | TR6 | TR7
5659
64a0c779
DN
5660 We simplify the grammar in obvious places (e.g., register parsing is
5661 done by calling parse_register) and eliminate immediate left recursion
5662 to implement a recursive-descent parser.
5663
a724f0f4
JB
5664 expr e04 expr'
5665
5666 expr' cmpOp e04 expr'
5667 | Empty
9306ca4a
JB
5668
5669 e04 e05 e04'
5670
5671 e04' addOp e05 e04'
5672 | Empty
64a0c779
DN
5673
5674 e05 e06 e05'
5675
9306ca4a 5676 e05' binOp e06 e05'
b77a7acd 5677 | Empty
64a0c779
DN
5678
5679 e06 e09 e06'
5680
5681 e06' mulOp e09 e06'
b77a7acd 5682 | Empty
64a0c779
DN
5683
5684 e09 OFFSET e10 e09'
a724f0f4
JB
5685 | SHORT e10'
5686 | + e10'
5687 | - e10'
5688 | ~ e10'
5689 | NOT e10'
b77a7acd 5690 | e10 e09'
64a0c779
DN
5691
5692 e09' PTR e10 e09'
b77a7acd 5693 | : e10 e09'
64a0c779
DN
5694 | Empty
5695
5696 e10 e11 e10'
5697
5698 e10' [ expr ] e10'
b77a7acd 5699 | Empty
64a0c779
DN
5700
5701 e11 ( expr )
b77a7acd 5702 | [ expr ]
64a0c779
DN
5703 | BYTE
5704 | WORD
5705 | DWORD
9306ca4a 5706 | FWORD
64a0c779 5707 | QWORD
9306ca4a
JB
5708 | TBYTE
5709 | OWORD
5710 | XMMWORD
64a0c779
DN
5711 | .
5712 | $
5713 | register
5714 | id
5715 | constant */
5716
5717/* Parsing structure for the intel syntax parser. Used to implement the
5718 semantic actions for the operand grammar. */
5719struct intel_parser_s
5720 {
5721 char *op_string; /* The string being parsed. */
5722 int got_a_float; /* Whether the operand is a float. */
4a1805b1 5723 int op_modifier; /* Operand modifier. */
64a0c779 5724 int is_mem; /* 1 if operand is memory reference. */
a724f0f4
JB
5725 int in_offset; /* >=1 if parsing operand of offset. */
5726 int in_bracket; /* >=1 if parsing operand in brackets. */
64a0c779
DN
5727 const reg_entry *reg; /* Last register reference found. */
5728 char *disp; /* Displacement string being built. */
a724f0f4 5729 char *next_operand; /* Resume point when splitting operands. */
64a0c779
DN
5730 };
5731
5732static struct intel_parser_s intel_parser;
5733
5734/* Token structure for parsing intel syntax. */
5735struct intel_token
5736 {
5737 int code; /* Token code. */
5738 const reg_entry *reg; /* Register entry for register tokens. */
5739 char *str; /* String representation. */
5740 };
5741
5742static struct intel_token cur_token, prev_token;
5743
50705ef4
AM
5744/* Token codes for the intel parser. Since T_SHORT is already used
5745 by COFF, undefine it first to prevent a warning. */
64a0c779
DN
5746#define T_NIL -1
5747#define T_CONST 1
5748#define T_REG 2
5749#define T_BYTE 3
5750#define T_WORD 4
9306ca4a
JB
5751#define T_DWORD 5
5752#define T_FWORD 6
5753#define T_QWORD 7
5754#define T_TBYTE 8
5755#define T_XMMWORD 9
50705ef4 5756#undef T_SHORT
9306ca4a
JB
5757#define T_SHORT 10
5758#define T_OFFSET 11
5759#define T_PTR 12
5760#define T_ID 13
5761#define T_SHL 14
5762#define T_SHR 15
64a0c779
DN
5763
5764/* Prototypes for intel parser functions. */
5765static int intel_match_token PARAMS ((int code));
cce0cbdc
DN
5766static void intel_get_token PARAMS ((void));
5767static void intel_putback_token PARAMS ((void));
5768static int intel_expr PARAMS ((void));
9306ca4a 5769static int intel_e04 PARAMS ((void));
cce0cbdc 5770static int intel_e05 PARAMS ((void));
cce0cbdc 5771static int intel_e06 PARAMS ((void));
cce0cbdc 5772static int intel_e09 PARAMS ((void));
a724f0f4 5773static int intel_bracket_expr PARAMS ((void));
cce0cbdc 5774static int intel_e10 PARAMS ((void));
cce0cbdc 5775static int intel_e11 PARAMS ((void));
64a0c779 5776
64a0c779
DN
5777static int
5778i386_intel_operand (operand_string, got_a_float)
5779 char *operand_string;
5780 int got_a_float;
5781{
5782 int ret;
5783 char *p;
5784
a724f0f4
JB
5785 p = intel_parser.op_string = xstrdup (operand_string);
5786 intel_parser.disp = (char *) xmalloc (strlen (operand_string) + 1);
5787
5788 for (;;)
64a0c779 5789 {
a724f0f4
JB
5790 /* Initialize token holders. */
5791 cur_token.code = prev_token.code = T_NIL;
5792 cur_token.reg = prev_token.reg = NULL;
5793 cur_token.str = prev_token.str = NULL;
5794
5795 /* Initialize parser structure. */
5796 intel_parser.got_a_float = got_a_float;
5797 intel_parser.op_modifier = 0;
5798 intel_parser.is_mem = 0;
5799 intel_parser.in_offset = 0;
5800 intel_parser.in_bracket = 0;
5801 intel_parser.reg = NULL;
5802 intel_parser.disp[0] = '\0';
5803 intel_parser.next_operand = NULL;
5804
5805 /* Read the first token and start the parser. */
5806 intel_get_token ();
5807 ret = intel_expr ();
5808
5809 if (!ret)
5810 break;
5811
9306ca4a
JB
5812 if (cur_token.code != T_NIL)
5813 {
5814 as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
5815 current_templates->start->name, cur_token.str);
5816 ret = 0;
5817 }
64a0c779
DN
5818 /* If we found a memory reference, hand it over to i386_displacement
5819 to fill in the rest of the operand fields. */
9306ca4a 5820 else if (intel_parser.is_mem)
64a0c779
DN
5821 {
5822 if ((i.mem_operands == 1
5823 && (current_templates->start->opcode_modifier & IsString) == 0)
5824 || i.mem_operands == 2)
5825 {
5826 as_bad (_("too many memory references for '%s'"),
5827 current_templates->start->name);
5828 ret = 0;
5829 }
5830 else
5831 {
5832 char *s = intel_parser.disp;
5833 i.mem_operands++;
5834
a724f0f4
JB
5835 if (!quiet_warnings && intel_parser.is_mem < 0)
5836 /* See the comments in intel_bracket_expr. */
5837 as_warn (_("Treating `%s' as memory reference"), operand_string);
5838
64a0c779
DN
5839 /* Add the displacement expression. */
5840 if (*s != '\0')
a4622f40
AM
5841 ret = i386_displacement (s, s + strlen (s));
5842 if (ret)
a724f0f4
JB
5843 {
5844 /* Swap base and index in 16-bit memory operands like
5845 [si+bx]. Since i386_index_check is also used in AT&T
5846 mode we have to do that here. */
5847 if (i.base_reg
5848 && i.index_reg
5849 && (i.base_reg->reg_type & Reg16)
5850 && (i.index_reg->reg_type & Reg16)
5851 && i.base_reg->reg_num >= 6
5852 && i.index_reg->reg_num < 6)
5853 {
5854 const reg_entry *base = i.index_reg;
5855
5856 i.index_reg = i.base_reg;
5857 i.base_reg = base;
5858 }
5859 ret = i386_index_check (operand_string);
5860 }
64a0c779
DN
5861 }
5862 }
5863
5864 /* Constant and OFFSET expressions are handled by i386_immediate. */
a724f0f4 5865 else if ((intel_parser.op_modifier & (1 << T_OFFSET))
64a0c779
DN
5866 || intel_parser.reg == NULL)
5867 ret = i386_immediate (intel_parser.disp);
a724f0f4
JB
5868
5869 if (intel_parser.next_operand && this_operand >= MAX_OPERANDS - 1)
5870 ret = 0;
5871 if (!ret || !intel_parser.next_operand)
5872 break;
5873 intel_parser.op_string = intel_parser.next_operand;
5874 this_operand = i.operands++;
64a0c779
DN
5875 }
5876
5877 free (p);
5878 free (intel_parser.disp);
5879
5880 return ret;
5881}
5882
a724f0f4
JB
5883#define NUM_ADDRESS_REGS (!!i.base_reg + !!i.index_reg)
5884
5885/* expr e04 expr'
5886
5887 expr' cmpOp e04 expr'
5888 | Empty */
64a0c779
DN
5889static int
5890intel_expr ()
5891{
a724f0f4
JB
5892 /* XXX Implement the comparison operators. */
5893 return intel_e04 ();
9306ca4a
JB
5894}
5895
a724f0f4 5896/* e04 e05 e04'
9306ca4a 5897
a724f0f4 5898 e04' addOp e05 e04'
9306ca4a
JB
5899 | Empty */
5900static int
5901intel_e04 ()
5902{
a724f0f4 5903 int nregs = -1;
9306ca4a 5904
a724f0f4 5905 for (;;)
9306ca4a 5906 {
a724f0f4
JB
5907 if (!intel_e05())
5908 return 0;
9306ca4a 5909
a724f0f4
JB
5910 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
5911 i.base_reg = i386_regtab + REGNAM_AL; /* al is invalid as base */
9306ca4a 5912
a724f0f4
JB
5913 if (cur_token.code == '+')
5914 nregs = -1;
5915 else if (cur_token.code == '-')
5916 nregs = NUM_ADDRESS_REGS;
5917 else
5918 return 1;
64a0c779 5919
a724f0f4
JB
5920 strcat (intel_parser.disp, cur_token.str);
5921 intel_match_token (cur_token.code);
5922 }
64a0c779
DN
5923}
5924
64a0c779
DN
5925/* e05 e06 e05'
5926
9306ca4a 5927 e05' binOp e06 e05'
64a0c779
DN
5928 | Empty */
5929static int
5930intel_e05 ()
5931{
a724f0f4 5932 int nregs = ~NUM_ADDRESS_REGS;
64a0c779 5933
a724f0f4 5934 for (;;)
64a0c779 5935 {
a724f0f4
JB
5936 if (!intel_e06())
5937 return 0;
5938
5939 if (cur_token.code == '&' || cur_token.code == '|' || cur_token.code == '^')
5940 {
5941 char str[2];
5942
5943 str[0] = cur_token.code;
5944 str[1] = 0;
5945 strcat (intel_parser.disp, str);
5946 }
5947 else
5948 break;
9306ca4a 5949
64a0c779
DN
5950 intel_match_token (cur_token.code);
5951
a724f0f4
JB
5952 if (nregs < 0)
5953 nregs = ~nregs;
64a0c779 5954 }
a724f0f4
JB
5955 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
5956 i.base_reg = i386_regtab + REGNAM_AL + 1; /* cl is invalid as base */
5957 return 1;
4a1805b1 5958}
64a0c779
DN
5959
5960/* e06 e09 e06'
5961
5962 e06' mulOp e09 e06'
b77a7acd 5963 | Empty */
64a0c779
DN
5964static int
5965intel_e06 ()
5966{
a724f0f4 5967 int nregs = ~NUM_ADDRESS_REGS;
64a0c779 5968
a724f0f4 5969 for (;;)
64a0c779 5970 {
a724f0f4
JB
5971 if (!intel_e09())
5972 return 0;
9306ca4a 5973
a724f0f4
JB
5974 if (cur_token.code == '*' || cur_token.code == '/' || cur_token.code == '%')
5975 {
5976 char str[2];
9306ca4a 5977
a724f0f4
JB
5978 str[0] = cur_token.code;
5979 str[1] = 0;
5980 strcat (intel_parser.disp, str);
5981 }
5982 else if (cur_token.code == T_SHL)
5983 strcat (intel_parser.disp, "<<");
5984 else if (cur_token.code == T_SHR)
5985 strcat (intel_parser.disp, ">>");
5986 else
5987 break;
9306ca4a 5988
a724f0f4 5989 intel_match_token (cur_token.code);
64a0c779 5990
a724f0f4
JB
5991 if (nregs < 0)
5992 nregs = ~nregs;
64a0c779 5993 }
a724f0f4
JB
5994 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
5995 i.base_reg = i386_regtab + REGNAM_AL + 2; /* dl is invalid as base */
5996 return 1;
64a0c779
DN
5997}
5998
a724f0f4
JB
5999/* e09 OFFSET e09
6000 | SHORT e09
6001 | + e09
6002 | - e09
6003 | ~ e09
6004 | NOT e09
9306ca4a
JB
6005 | e10 e09'
6006
64a0c779 6007 e09' PTR e10 e09'
b77a7acd 6008 | : e10 e09'
64a0c779
DN
6009 | Empty */
6010static int
6011intel_e09 ()
6012{
a724f0f4
JB
6013 int nregs = ~NUM_ADDRESS_REGS;
6014 int in_offset = 0;
6015
6016 for (;;)
64a0c779 6017 {
a724f0f4
JB
6018 /* Don't consume constants here. */
6019 if (cur_token.code == '+' || cur_token.code == '-')
6020 {
6021 /* Need to look one token ahead - if the next token
6022 is a constant, the current token is its sign. */
6023 int next_code;
6024
6025 intel_match_token (cur_token.code);
6026 next_code = cur_token.code;
6027 intel_putback_token ();
6028 if (next_code == T_CONST)
6029 break;
6030 }
6031
6032 /* e09 OFFSET e09 */
6033 if (cur_token.code == T_OFFSET)
6034 {
6035 if (!in_offset++)
6036 ++intel_parser.in_offset;
6037 }
6038
6039 /* e09 SHORT e09 */
6040 else if (cur_token.code == T_SHORT)
6041 intel_parser.op_modifier |= 1 << T_SHORT;
6042
6043 /* e09 + e09 */
6044 else if (cur_token.code == '+')
6045 strcat (intel_parser.disp, "+");
6046
6047 /* e09 - e09
6048 | ~ e09
6049 | NOT e09 */
6050 else if (cur_token.code == '-' || cur_token.code == '~')
6051 {
6052 char str[2];
64a0c779 6053
a724f0f4
JB
6054 if (nregs < 0)
6055 nregs = ~nregs;
6056 str[0] = cur_token.code;
6057 str[1] = 0;
6058 strcat (intel_parser.disp, str);
6059 }
6060
6061 /* e09 e10 e09' */
6062 else
6063 break;
6064
6065 intel_match_token (cur_token.code);
64a0c779
DN
6066 }
6067
a724f0f4 6068 for (;;)
9306ca4a 6069 {
a724f0f4
JB
6070 if (!intel_e10 ())
6071 return 0;
9306ca4a 6072
a724f0f4
JB
6073 /* e09' PTR e10 e09' */
6074 if (cur_token.code == T_PTR)
6075 {
6076 char suffix;
9306ca4a 6077
a724f0f4
JB
6078 if (prev_token.code == T_BYTE)
6079 suffix = BYTE_MNEM_SUFFIX;
9306ca4a 6080
a724f0f4
JB
6081 else if (prev_token.code == T_WORD)
6082 {
6083 if (current_templates->start->name[0] == 'l'
6084 && current_templates->start->name[2] == 's'
6085 && current_templates->start->name[3] == 0)
6086 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6087 else if (intel_parser.got_a_float == 2) /* "fi..." */
6088 suffix = SHORT_MNEM_SUFFIX;
6089 else
6090 suffix = WORD_MNEM_SUFFIX;
6091 }
64a0c779 6092
a724f0f4
JB
6093 else if (prev_token.code == T_DWORD)
6094 {
6095 if (current_templates->start->name[0] == 'l'
6096 && current_templates->start->name[2] == 's'
6097 && current_templates->start->name[3] == 0)
6098 suffix = WORD_MNEM_SUFFIX;
6099 else if (flag_code == CODE_16BIT
6100 && (current_templates->start->opcode_modifier
6101 & (Jump|JumpDword|JumpInterSegment)))
6102 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6103 else if (intel_parser.got_a_float == 1) /* "f..." */
6104 suffix = SHORT_MNEM_SUFFIX;
6105 else
6106 suffix = LONG_MNEM_SUFFIX;
6107 }
9306ca4a 6108
a724f0f4
JB
6109 else if (prev_token.code == T_FWORD)
6110 {
6111 if (current_templates->start->name[0] == 'l'
6112 && current_templates->start->name[2] == 's'
6113 && current_templates->start->name[3] == 0)
6114 suffix = LONG_MNEM_SUFFIX;
6115 else if (!intel_parser.got_a_float)
6116 {
6117 if (flag_code == CODE_16BIT)
6118 add_prefix (DATA_PREFIX_OPCODE);
6119 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6120 }
6121 else
6122 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6123 }
64a0c779 6124
a724f0f4
JB
6125 else if (prev_token.code == T_QWORD)
6126 {
6127 if (intel_parser.got_a_float == 1) /* "f..." */
6128 suffix = LONG_MNEM_SUFFIX;
6129 else
6130 suffix = QWORD_MNEM_SUFFIX;
6131 }
64a0c779 6132
a724f0f4
JB
6133 else if (prev_token.code == T_TBYTE)
6134 {
6135 if (intel_parser.got_a_float == 1)
6136 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6137 else
6138 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6139 }
9306ca4a 6140
a724f0f4 6141 else if (prev_token.code == T_XMMWORD)
9306ca4a 6142 {
a724f0f4
JB
6143 /* XXX ignored for now, but accepted since gcc uses it */
6144 suffix = 0;
9306ca4a 6145 }
64a0c779 6146
f16b83df 6147 else
a724f0f4
JB
6148 {
6149 as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
6150 return 0;
6151 }
6152
6153 if (current_templates->start->base_opcode == 0x8d /* lea */)
6154 ;
6155 else if (!i.suffix)
6156 i.suffix = suffix;
6157 else if (i.suffix != suffix)
6158 {
6159 as_bad (_("Conflicting operand modifiers"));
6160 return 0;
6161 }
64a0c779 6162
9306ca4a
JB
6163 }
6164
a724f0f4
JB
6165 /* e09' : e10 e09' */
6166 else if (cur_token.code == ':')
9306ca4a 6167 {
a724f0f4
JB
6168 if (prev_token.code != T_REG)
6169 {
6170 /* While {call,jmp} SSSS:OOOO is MASM syntax only when SSSS is a
6171 segment/group identifier (which we don't have), using comma
6172 as the operand separator there is even less consistent, since
6173 there all branches only have a single operand. */
6174 if (this_operand != 0
6175 || intel_parser.in_offset
6176 || intel_parser.in_bracket
6177 || (!(current_templates->start->opcode_modifier
6178 & (Jump|JumpDword|JumpInterSegment))
6179 && !(current_templates->start->operand_types[0]
6180 & JumpAbsolute)))
6181 return intel_match_token (T_NIL);
6182 /* Remember the start of the 2nd operand and terminate 1st
6183 operand here.
6184 XXX This isn't right, yet (when SSSS:OOOO is right operand of
6185 another expression), but it gets at least the simplest case
6186 (a plain number or symbol on the left side) right. */
6187 intel_parser.next_operand = intel_parser.op_string;
6188 *--intel_parser.op_string = '\0';
6189 return intel_match_token (':');
6190 }
9306ca4a 6191 }
64a0c779 6192
a724f0f4 6193 /* e09' Empty */
64a0c779 6194 else
a724f0f4 6195 break;
64a0c779 6196
a724f0f4
JB
6197 intel_match_token (cur_token.code);
6198
6199 }
6200
6201 if (in_offset)
6202 {
6203 --intel_parser.in_offset;
6204 if (nregs < 0)
6205 nregs = ~nregs;
6206 if (NUM_ADDRESS_REGS > nregs)
9306ca4a 6207 {
a724f0f4 6208 as_bad (_("Invalid operand to `OFFSET'"));
9306ca4a
JB
6209 return 0;
6210 }
a724f0f4
JB
6211 intel_parser.op_modifier |= 1 << T_OFFSET;
6212 }
9306ca4a 6213
a724f0f4
JB
6214 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
6215 i.base_reg = i386_regtab + REGNAM_AL + 3; /* bl is invalid as base */
6216 return 1;
6217}
64a0c779 6218
a724f0f4
JB
6219static int
6220intel_bracket_expr ()
6221{
6222 int was_offset = intel_parser.op_modifier & (1 << T_OFFSET);
6223 const char *start = intel_parser.op_string;
6224 int len;
6225
6226 if (i.op[this_operand].regs)
6227 return intel_match_token (T_NIL);
6228
6229 intel_match_token ('[');
6230
6231 /* Mark as a memory operand only if it's not already known to be an
6232 offset expression. If it's an offset expression, we need to keep
6233 the brace in. */
6234 if (!intel_parser.in_offset)
6235 {
6236 ++intel_parser.in_bracket;
6237 /* Unfortunately gas always diverged from MASM in a respect that can't
6238 be easily fixed without risking to break code sequences likely to be
6239 encountered (the testsuite even check for this): MASM doesn't consider
6240 an expression inside brackets unconditionally as a memory reference.
6241 When that is e.g. a constant, an offset expression, or the sum of the
6242 two, this is still taken as a constant load. gas, however, always
6243 treated these as memory references. As a compromise, we'll try to make
6244 offset expressions inside brackets work the MASM way (since that's
6245 less likely to be found in real world code), but make constants alone
6246 continue to work the traditional gas way. In either case, issue a
6247 warning. */
6248 intel_parser.op_modifier &= ~was_offset;
64a0c779 6249 }
a724f0f4
JB
6250 else
6251 strcat (intel_parser.disp, "[");
6252
6253 /* Add a '+' to the displacement string if necessary. */
6254 if (*intel_parser.disp != '\0'
6255 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
6256 strcat (intel_parser.disp, "+");
64a0c779 6257
a724f0f4
JB
6258 if (intel_expr ()
6259 && (len = intel_parser.op_string - start - 1,
6260 intel_match_token (']')))
64a0c779 6261 {
a724f0f4
JB
6262 /* Preserve brackets when the operand is an offset expression. */
6263 if (intel_parser.in_offset)
6264 strcat (intel_parser.disp, "]");
6265 else
6266 {
6267 --intel_parser.in_bracket;
6268 if (i.base_reg || i.index_reg)
6269 intel_parser.is_mem = 1;
6270 if (!intel_parser.is_mem)
6271 {
6272 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
6273 /* Defer the warning until all of the operand was parsed. */
6274 intel_parser.is_mem = -1;
6275 else if (!quiet_warnings)
6276 as_warn (_("`[%.*s]' taken to mean just `%.*s'"), len, start, len, start);
6277 }
6278 }
6279 intel_parser.op_modifier |= was_offset;
64a0c779 6280
a724f0f4 6281 return 1;
64a0c779 6282 }
a724f0f4 6283 return 0;
64a0c779
DN
6284}
6285
6286/* e10 e11 e10'
6287
6288 e10' [ expr ] e10'
b77a7acd 6289 | Empty */
64a0c779
DN
6290static int
6291intel_e10 ()
6292{
a724f0f4
JB
6293 if (!intel_e11 ())
6294 return 0;
64a0c779 6295
a724f0f4 6296 while (cur_token.code == '[')
64a0c779 6297 {
a724f0f4 6298 if (!intel_bracket_expr ())
21d6c4af 6299 return 0;
64a0c779
DN
6300 }
6301
a724f0f4 6302 return 1;
64a0c779
DN
6303}
6304
64a0c779 6305/* e11 ( expr )
b77a7acd 6306 | [ expr ]
64a0c779
DN
6307 | BYTE
6308 | WORD
6309 | DWORD
9306ca4a 6310 | FWORD
64a0c779 6311 | QWORD
9306ca4a
JB
6312 | TBYTE
6313 | OWORD
6314 | XMMWORD
4a1805b1 6315 | $
64a0c779
DN
6316 | .
6317 | register
6318 | id
6319 | constant */
6320static int
6321intel_e11 ()
6322{
a724f0f4 6323 switch (cur_token.code)
64a0c779 6324 {
a724f0f4
JB
6325 /* e11 ( expr ) */
6326 case '(':
64a0c779
DN
6327 intel_match_token ('(');
6328 strcat (intel_parser.disp, "(");
6329
6330 if (intel_expr () && intel_match_token (')'))
e5cb08ac
KH
6331 {
6332 strcat (intel_parser.disp, ")");
6333 return 1;
6334 }
a724f0f4 6335 return 0;
4a1805b1 6336
a724f0f4
JB
6337 /* e11 [ expr ] */
6338 case '[':
6339 /* Operands for jump/call inside brackets denote absolute addresses.
6340 XXX This shouldn't be needed anymore (or if it should rather live
6341 in intel_bracket_expr). */
9306ca4a
JB
6342 if (current_templates->start->opcode_modifier
6343 & (Jump|JumpDword|JumpByte|JumpInterSegment))
64a0c779
DN
6344 i.types[this_operand] |= JumpAbsolute;
6345
a724f0f4 6346 return intel_bracket_expr ();
64a0c779 6347
a724f0f4
JB
6348 /* e11 $
6349 | . */
6350 case '.':
64a0c779
DN
6351 strcat (intel_parser.disp, cur_token.str);
6352 intel_match_token (cur_token.code);
21d6c4af
DN
6353
6354 /* Mark as a memory operand only if it's not already known to be an
6355 offset expression. */
a724f0f4 6356 if (!intel_parser.in_offset)
21d6c4af 6357 intel_parser.is_mem = 1;
64a0c779
DN
6358
6359 return 1;
64a0c779 6360
a724f0f4
JB
6361 /* e11 register */
6362 case T_REG:
6363 {
6364 const reg_entry *reg = intel_parser.reg = cur_token.reg;
64a0c779 6365
a724f0f4 6366 intel_match_token (T_REG);
64a0c779 6367
a724f0f4
JB
6368 /* Check for segment change. */
6369 if (cur_token.code == ':')
6370 {
6371 if (!(reg->reg_type & (SReg2 | SReg3)))
6372 {
6373 as_bad (_("`%s' is not a valid segment register"), reg->reg_name);
6374 return 0;
6375 }
6376 else if (i.seg[i.mem_operands])
6377 as_warn (_("Extra segment override ignored"));
6378 else
6379 {
6380 if (!intel_parser.in_offset)
6381 intel_parser.is_mem = 1;
6382 switch (reg->reg_num)
6383 {
6384 case 0:
6385 i.seg[i.mem_operands] = &es;
6386 break;
6387 case 1:
6388 i.seg[i.mem_operands] = &cs;
6389 break;
6390 case 2:
6391 i.seg[i.mem_operands] = &ss;
6392 break;
6393 case 3:
6394 i.seg[i.mem_operands] = &ds;
6395 break;
6396 case 4:
6397 i.seg[i.mem_operands] = &fs;
6398 break;
6399 case 5:
6400 i.seg[i.mem_operands] = &gs;
6401 break;
6402 }
6403 }
6404 }
64a0c779 6405
a724f0f4
JB
6406 /* Not a segment register. Check for register scaling. */
6407 else if (cur_token.code == '*')
6408 {
6409 if (!intel_parser.in_bracket)
6410 {
6411 as_bad (_("Register scaling only allowed in memory operands"));
6412 return 0;
6413 }
64a0c779 6414
a724f0f4
JB
6415 if (reg->reg_type & Reg16) /* Disallow things like [si*1]. */
6416 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
6417 else if (i.index_reg)
6418 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
64a0c779 6419
a724f0f4
JB
6420 /* What follows must be a valid scale. */
6421 intel_match_token ('*');
6422 i.index_reg = reg;
6423 i.types[this_operand] |= BaseIndex;
64a0c779 6424
a724f0f4
JB
6425 /* Set the scale after setting the register (otherwise,
6426 i386_scale will complain) */
6427 if (cur_token.code == '+' || cur_token.code == '-')
6428 {
6429 char *str, sign = cur_token.code;
6430 intel_match_token (cur_token.code);
6431 if (cur_token.code != T_CONST)
6432 {
6433 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
6434 cur_token.str);
6435 return 0;
6436 }
6437 str = (char *) xmalloc (strlen (cur_token.str) + 2);
6438 strcpy (str + 1, cur_token.str);
6439 *str = sign;
6440 if (!i386_scale (str))
6441 return 0;
6442 free (str);
6443 }
6444 else if (!i386_scale (cur_token.str))
64a0c779 6445 return 0;
a724f0f4
JB
6446 intel_match_token (cur_token.code);
6447 }
64a0c779 6448
a724f0f4
JB
6449 /* No scaling. If this is a memory operand, the register is either a
6450 base register (first occurrence) or an index register (second
6451 occurrence). */
6452 else if (intel_parser.in_bracket && !(reg->reg_type & (SReg2 | SReg3)))
6453 {
64a0c779 6454
a724f0f4
JB
6455 if (!i.base_reg)
6456 i.base_reg = reg;
6457 else if (!i.index_reg)
6458 i.index_reg = reg;
6459 else
6460 {
6461 as_bad (_("Too many register references in memory operand"));
6462 return 0;
6463 }
64a0c779 6464
a724f0f4
JB
6465 i.types[this_operand] |= BaseIndex;
6466 }
4a1805b1 6467
a724f0f4
JB
6468 /* Offset modifier. Add the register to the displacement string to be
6469 parsed as an immediate expression after we're done. */
6470 else if (intel_parser.in_offset)
6471 {
6472 as_warn (_("Using register names in OFFSET expressions is deprecated"));
6473 strcat (intel_parser.disp, reg->reg_name);
6474 }
64a0c779 6475
a724f0f4
JB
6476 /* It's neither base nor index nor offset. */
6477 else if (!intel_parser.is_mem)
6478 {
6479 i.types[this_operand] |= reg->reg_type & ~BaseIndex;
6480 i.op[this_operand].regs = reg;
6481 i.reg_operands++;
6482 }
6483 else
6484 {
6485 as_bad (_("Invalid use of register"));
6486 return 0;
6487 }
64a0c779 6488
a724f0f4
JB
6489 /* Since registers are not part of the displacement string (except
6490 when we're parsing offset operands), we may need to remove any
6491 preceding '+' from the displacement string. */
6492 if (*intel_parser.disp != '\0'
6493 && !intel_parser.in_offset)
6494 {
6495 char *s = intel_parser.disp;
6496 s += strlen (s) - 1;
6497 if (*s == '+')
6498 *s = '\0';
6499 }
4a1805b1 6500
a724f0f4
JB
6501 return 1;
6502 }
6503
6504 /* e11 BYTE
6505 | WORD
6506 | DWORD
6507 | FWORD
6508 | QWORD
6509 | TBYTE
6510 | OWORD
6511 | XMMWORD */
6512 case T_BYTE:
6513 case T_WORD:
6514 case T_DWORD:
6515 case T_FWORD:
6516 case T_QWORD:
6517 case T_TBYTE:
6518 case T_XMMWORD:
6519 intel_match_token (cur_token.code);
64a0c779 6520
a724f0f4
JB
6521 if (cur_token.code == T_PTR)
6522 return 1;
6523
6524 /* It must have been an identifier. */
6525 intel_putback_token ();
6526 cur_token.code = T_ID;
6527 /* FALLTHRU */
6528
6529 /* e11 id
6530 | constant */
6531 case T_ID:
6532 if (!intel_parser.in_offset && intel_parser.is_mem <= 0)
9306ca4a
JB
6533 {
6534 symbolS *symbolP;
6535
a724f0f4
JB
6536 /* The identifier represents a memory reference only if it's not
6537 preceded by an offset modifier and if it's not an equate. */
9306ca4a
JB
6538 symbolP = symbol_find(cur_token.str);
6539 if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
6540 intel_parser.is_mem = 1;
6541 }
a724f0f4 6542 /* FALLTHRU */
64a0c779 6543
a724f0f4
JB
6544 case T_CONST:
6545 case '-':
6546 case '+':
6547 {
6548 char *save_str, sign = 0;
64a0c779 6549
a724f0f4
JB
6550 /* Allow constants that start with `+' or `-'. */
6551 if (cur_token.code == '-' || cur_token.code == '+')
6552 {
6553 sign = cur_token.code;
6554 intel_match_token (cur_token.code);
6555 if (cur_token.code != T_CONST)
6556 {
6557 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
6558 cur_token.str);
6559 return 0;
6560 }
6561 }
64a0c779 6562
a724f0f4
JB
6563 save_str = (char *) xmalloc (strlen (cur_token.str) + 2);
6564 strcpy (save_str + !!sign, cur_token.str);
6565 if (sign)
6566 *save_str = sign;
64a0c779 6567
a724f0f4
JB
6568 /* Get the next token to check for register scaling. */
6569 intel_match_token (cur_token.code);
64a0c779 6570
a724f0f4
JB
6571 /* Check if this constant is a scaling factor for an index register. */
6572 if (cur_token.code == '*')
6573 {
6574 if (intel_match_token ('*') && cur_token.code == T_REG)
6575 {
6576 const reg_entry *reg = cur_token.reg;
6577
6578 if (!intel_parser.in_bracket)
6579 {
6580 as_bad (_("Register scaling only allowed in memory operands"));
6581 return 0;
6582 }
6583
6584 if (reg->reg_type & Reg16) /* Disallow things like [1*si]. */
6585 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
6586 else if (i.index_reg)
6587 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
6588
6589 /* The constant is followed by `* reg', so it must be
6590 a valid scale. */
6591 i.index_reg = reg;
6592 i.types[this_operand] |= BaseIndex;
6593
6594 /* Set the scale after setting the register (otherwise,
6595 i386_scale will complain) */
6596 if (!i386_scale (save_str))
64a0c779 6597 return 0;
a724f0f4
JB
6598 intel_match_token (T_REG);
6599
6600 /* Since registers are not part of the displacement
6601 string, we may need to remove any preceding '+' from
6602 the displacement string. */
6603 if (*intel_parser.disp != '\0')
6604 {
6605 char *s = intel_parser.disp;
6606 s += strlen (s) - 1;
6607 if (*s == '+')
6608 *s = '\0';
6609 }
6610
6611 free (save_str);
6612
6613 return 1;
6614 }
64a0c779 6615
a724f0f4
JB
6616 /* The constant was not used for register scaling. Since we have
6617 already consumed the token following `*' we now need to put it
6618 back in the stream. */
64a0c779 6619 intel_putback_token ();
a724f0f4 6620 }
64a0c779 6621
a724f0f4
JB
6622 /* Add the constant to the displacement string. */
6623 strcat (intel_parser.disp, save_str);
6624 free (save_str);
64a0c779 6625
a724f0f4
JB
6626 return 1;
6627 }
64a0c779
DN
6628 }
6629
64a0c779
DN
6630 as_bad (_("Unrecognized token '%s'"), cur_token.str);
6631 return 0;
6632}
6633
64a0c779
DN
6634/* Match the given token against cur_token. If they match, read the next
6635 token from the operand string. */
6636static int
6637intel_match_token (code)
e5cb08ac 6638 int code;
64a0c779
DN
6639{
6640 if (cur_token.code == code)
6641 {
6642 intel_get_token ();
6643 return 1;
6644 }
6645 else
6646 {
0477af35 6647 as_bad (_("Unexpected token `%s'"), cur_token.str);
64a0c779
DN
6648 return 0;
6649 }
6650}
6651
64a0c779
DN
6652/* Read a new token from intel_parser.op_string and store it in cur_token. */
6653static void
6654intel_get_token ()
6655{
6656 char *end_op;
6657 const reg_entry *reg;
6658 struct intel_token new_token;
6659
6660 new_token.code = T_NIL;
6661 new_token.reg = NULL;
6662 new_token.str = NULL;
6663
4a1805b1 6664 /* Free the memory allocated to the previous token and move
64a0c779
DN
6665 cur_token to prev_token. */
6666 if (prev_token.str)
6667 free (prev_token.str);
6668
6669 prev_token = cur_token;
6670
6671 /* Skip whitespace. */
6672 while (is_space_char (*intel_parser.op_string))
6673 intel_parser.op_string++;
6674
6675 /* Return an empty token if we find nothing else on the line. */
6676 if (*intel_parser.op_string == '\0')
6677 {
6678 cur_token = new_token;
6679 return;
6680 }
6681
6682 /* The new token cannot be larger than the remainder of the operand
6683 string. */
a724f0f4 6684 new_token.str = (char *) xmalloc (strlen (intel_parser.op_string) + 1);
64a0c779
DN
6685 new_token.str[0] = '\0';
6686
6687 if (strchr ("0123456789", *intel_parser.op_string))
6688 {
6689 char *p = new_token.str;
6690 char *q = intel_parser.op_string;
6691 new_token.code = T_CONST;
6692
6693 /* Allow any kind of identifier char to encompass floating point and
6694 hexadecimal numbers. */
6695 while (is_identifier_char (*q))
6696 *p++ = *q++;
6697 *p = '\0';
6698
6699 /* Recognize special symbol names [0-9][bf]. */
6700 if (strlen (intel_parser.op_string) == 2
4a1805b1 6701 && (intel_parser.op_string[1] == 'b'
64a0c779
DN
6702 || intel_parser.op_string[1] == 'f'))
6703 new_token.code = T_ID;
6704 }
6705
64a0c779
DN
6706 else if ((*intel_parser.op_string == REGISTER_PREFIX || allow_naked_reg)
6707 && ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL))
6708 {
6709 new_token.code = T_REG;
6710 new_token.reg = reg;
6711
6712 if (*intel_parser.op_string == REGISTER_PREFIX)
6713 {
6714 new_token.str[0] = REGISTER_PREFIX;
6715 new_token.str[1] = '\0';
6716 }
6717
6718 strcat (new_token.str, reg->reg_name);
6719 }
6720
6721 else if (is_identifier_char (*intel_parser.op_string))
6722 {
6723 char *p = new_token.str;
6724 char *q = intel_parser.op_string;
6725
6726 /* A '.' or '$' followed by an identifier char is an identifier.
6727 Otherwise, it's operator '.' followed by an expression. */
6728 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
6729 {
9306ca4a
JB
6730 new_token.code = '.';
6731 new_token.str[0] = '.';
64a0c779
DN
6732 new_token.str[1] = '\0';
6733 }
6734 else
6735 {
6736 while (is_identifier_char (*q) || *q == '@')
6737 *p++ = *q++;
6738 *p = '\0';
6739
9306ca4a
JB
6740 if (strcasecmp (new_token.str, "NOT") == 0)
6741 new_token.code = '~';
6742
6743 else if (strcasecmp (new_token.str, "MOD") == 0)
6744 new_token.code = '%';
6745
6746 else if (strcasecmp (new_token.str, "AND") == 0)
6747 new_token.code = '&';
6748
6749 else if (strcasecmp (new_token.str, "OR") == 0)
6750 new_token.code = '|';
6751
6752 else if (strcasecmp (new_token.str, "XOR") == 0)
6753 new_token.code = '^';
6754
6755 else if (strcasecmp (new_token.str, "SHL") == 0)
6756 new_token.code = T_SHL;
6757
6758 else if (strcasecmp (new_token.str, "SHR") == 0)
6759 new_token.code = T_SHR;
6760
6761 else if (strcasecmp (new_token.str, "BYTE") == 0)
64a0c779
DN
6762 new_token.code = T_BYTE;
6763
6764 else if (strcasecmp (new_token.str, "WORD") == 0)
6765 new_token.code = T_WORD;
6766
6767 else if (strcasecmp (new_token.str, "DWORD") == 0)
6768 new_token.code = T_DWORD;
6769
9306ca4a
JB
6770 else if (strcasecmp (new_token.str, "FWORD") == 0)
6771 new_token.code = T_FWORD;
6772
64a0c779
DN
6773 else if (strcasecmp (new_token.str, "QWORD") == 0)
6774 new_token.code = T_QWORD;
6775
9306ca4a
JB
6776 else if (strcasecmp (new_token.str, "TBYTE") == 0
6777 /* XXX remove (gcc still uses it) */
6778 || strcasecmp (new_token.str, "XWORD") == 0)
6779 new_token.code = T_TBYTE;
6780
6781 else if (strcasecmp (new_token.str, "XMMWORD") == 0
6782 || strcasecmp (new_token.str, "OWORD") == 0)
6783 new_token.code = T_XMMWORD;
64a0c779
DN
6784
6785 else if (strcasecmp (new_token.str, "PTR") == 0)
6786 new_token.code = T_PTR;
6787
6788 else if (strcasecmp (new_token.str, "SHORT") == 0)
6789 new_token.code = T_SHORT;
6790
6791 else if (strcasecmp (new_token.str, "OFFSET") == 0)
6792 {
6793 new_token.code = T_OFFSET;
6794
6795 /* ??? This is not mentioned in the MASM grammar but gcc
6796 makes use of it with -mintel-syntax. OFFSET may be
6797 followed by FLAT: */
6798 if (strncasecmp (q, " FLAT:", 6) == 0)
6799 strcat (new_token.str, " FLAT:");
6800 }
6801
6802 /* ??? This is not mentioned in the MASM grammar. */
6803 else if (strcasecmp (new_token.str, "FLAT") == 0)
a724f0f4
JB
6804 {
6805 new_token.code = T_OFFSET;
6806 if (*q == ':')
6807 strcat (new_token.str, ":");
6808 else
6809 as_bad (_("`:' expected"));
6810 }
64a0c779
DN
6811
6812 else
6813 new_token.code = T_ID;
6814 }
6815 }
6816
9306ca4a
JB
6817 else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
6818 {
6819 new_token.code = *intel_parser.op_string;
6820 new_token.str[0] = *intel_parser.op_string;
6821 new_token.str[1] = '\0';
6822 }
6823
6824 else if (strchr ("<>", *intel_parser.op_string)
6825 && *intel_parser.op_string == *(intel_parser.op_string + 1))
6826 {
6827 new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
6828 new_token.str[0] = *intel_parser.op_string;
6829 new_token.str[1] = *intel_parser.op_string;
6830 new_token.str[2] = '\0';
6831 }
6832
64a0c779 6833 else
0477af35 6834 as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
64a0c779
DN
6835
6836 intel_parser.op_string += strlen (new_token.str);
6837 cur_token = new_token;
6838}
6839
64a0c779
DN
6840/* Put cur_token back into the token stream and make cur_token point to
6841 prev_token. */
6842static void
6843intel_putback_token ()
6844{
a724f0f4
JB
6845 if (cur_token.code != T_NIL)
6846 {
6847 intel_parser.op_string -= strlen (cur_token.str);
6848 free (cur_token.str);
6849 }
64a0c779 6850 cur_token = prev_token;
4a1805b1 6851
64a0c779
DN
6852 /* Forget prev_token. */
6853 prev_token.code = T_NIL;
6854 prev_token.reg = NULL;
6855 prev_token.str = NULL;
6856}
54cfded0 6857
a4447b93 6858int
54cfded0
AM
6859tc_x86_regname_to_dw2regnum (const char *regname)
6860{
6861 unsigned int regnum;
6862 unsigned int regnames_count;
089dfecd 6863 static const char *const regnames_32[] =
54cfded0 6864 {
a4447b93
RH
6865 "eax", "ecx", "edx", "ebx",
6866 "esp", "ebp", "esi", "edi",
089dfecd
JB
6867 "eip", "eflags", NULL,
6868 "st0", "st1", "st2", "st3",
6869 "st4", "st5", "st6", "st7",
6870 NULL, NULL,
6871 "xmm0", "xmm1", "xmm2", "xmm3",
6872 "xmm4", "xmm5", "xmm6", "xmm7",
6873 "mm0", "mm1", "mm2", "mm3",
6874 "mm4", "mm5", "mm6", "mm7"
54cfded0 6875 };
089dfecd 6876 static const char *const regnames_64[] =
54cfded0 6877 {
089dfecd
JB
6878 "rax", "rdx", "rcx", "rbx",
6879 "rsi", "rdi", "rbp", "rsp",
6880 "r8", "r9", "r10", "r11",
54cfded0 6881 "r12", "r13", "r14", "r15",
089dfecd
JB
6882 "rip",
6883 "xmm0", "xmm1", "xmm2", "xmm3",
6884 "xmm4", "xmm5", "xmm6", "xmm7",
6885 "xmm8", "xmm9", "xmm10", "xmm11",
6886 "xmm12", "xmm13", "xmm14", "xmm15",
6887 "st0", "st1", "st2", "st3",
6888 "st4", "st5", "st6", "st7",
6889 "mm0", "mm1", "mm2", "mm3",
6890 "mm4", "mm5", "mm6", "mm7"
54cfded0 6891 };
089dfecd 6892 const char *const *regnames;
54cfded0
AM
6893
6894 if (flag_code == CODE_64BIT)
6895 {
6896 regnames = regnames_64;
0cea6190 6897 regnames_count = ARRAY_SIZE (regnames_64);
54cfded0
AM
6898 }
6899 else
6900 {
6901 regnames = regnames_32;
0cea6190 6902 regnames_count = ARRAY_SIZE (regnames_32);
54cfded0
AM
6903 }
6904
6905 for (regnum = 0; regnum < regnames_count; regnum++)
089dfecd
JB
6906 if (regnames[regnum] != NULL
6907 && strcmp (regname, regnames[regnum]) == 0)
54cfded0
AM
6908 return regnum;
6909
54cfded0
AM
6910 return -1;
6911}
6912
6913void
6914tc_x86_frame_initial_instructions (void)
6915{
a4447b93
RH
6916 static unsigned int sp_regno;
6917
6918 if (!sp_regno)
6919 sp_regno = tc_x86_regname_to_dw2regnum (flag_code == CODE_64BIT
6920 ? "rsp" : "esp");
6921
6922 cfi_add_CFA_def_cfa (sp_regno, -x86_cie_data_alignment);
6923 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 6924}
d2b2c203
DJ
6925
6926int
6927i386_elf_section_type (const char *str, size_t len)
6928{
6929 if (flag_code == CODE_64BIT
6930 && len == sizeof ("unwind") - 1
6931 && strncmp (str, "unwind", 6) == 0)
6932 return SHT_X86_64_UNWIND;
6933
6934 return -1;
6935}
bb41ade5
AM
6936
6937#ifdef TE_PE
6938void
6939tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
6940{
6941 expressionS expr;
6942
6943 expr.X_op = O_secrel;
6944 expr.X_add_symbol = symbol;
6945 expr.X_add_number = 0;
6946 emit_expr (&expr, size);
6947}
6948#endif