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