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