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