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