2007-09-08 H.J. Lu <hongjiu.lu@intel.com>
[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 /* The order of the immediates should be reversed
2139 for 2 immediates extrq and insertq instructions */
2140 if ((i.imm_operands == 2)
2141 && ((strcmp (mnemonic, "extrq") == 0)
2142 || (strcmp (mnemonic, "insertq") == 0)))
2143 {
2144 swap_2_operands (0, 1);
2145 /* "extrq" and insertq" are the only two instructions whose operands
2146 have to be reversed even though they have two immediate operands.
2147 */
2148 if (intel_syntax)
2149 swap_operands ();
2150 }
2151
2152 /* Now we've parsed the mnemonic into a set of templates, and have the
2153 operands at hand. */
2154
2155 /* All intel opcodes have reversed operands except for "bound" and
2156 "enter". We also don't reverse intersegment "jmp" and "call"
2157 instructions with 2 immediate operands so that the immediate segment
2158 precedes the offset, as it does when in AT&T mode. */
2159 if (intel_syntax
2160 && i.operands > 1
2161 && (strcmp (mnemonic, "bound") != 0)
2162 && (strcmp (mnemonic, "invlpga") != 0)
2163 && !(operand_type_check (i.types[0], imm)
2164 && operand_type_check (i.types[1], imm)))
2165 swap_operands ();
2166
2167 if (i.imm_operands)
2168 optimize_imm ();
2169
2170 /* Don't optimize displacement for movabs since it only takes 64bit
2171 displacement. */
2172 if (i.disp_operands
2173 && (flag_code != CODE_64BIT
2174 || strcmp (mnemonic, "movabs") != 0))
2175 optimize_disp ();
2176
2177 /* Next, we find a template that matches the given insn,
2178 making sure the overlap of the given operands types is consistent
2179 with the template operand types. */
2180
2181 if (!match_template ())
2182 return;
2183
2184 if (intel_syntax)
2185 {
2186 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
2187 if (SYSV386_COMPAT
2188 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
2189 i.tm.base_opcode ^= Opcode_FloatR;
2190
2191 /* Zap movzx and movsx suffix. The suffix may have been set from
2192 "word ptr" or "byte ptr" on the source operand, but we'll use
2193 the suffix later to choose the destination register. */
2194 if ((i.tm.base_opcode & ~9) == 0x0fb6)
2195 {
2196 if (i.reg_operands < 2
2197 && !i.suffix
2198 && (!i.tm.opcode_modifier.no_bsuf
2199 || !i.tm.opcode_modifier.no_wsuf
2200 || !i.tm.opcode_modifier.no_lsuf
2201 || !i.tm.opcode_modifier.no_ssuf
2202 || !i.tm.opcode_modifier.no_xsuf
2203 || !i.tm.opcode_modifier.no_qsuf))
2204 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
2205
2206 i.suffix = 0;
2207 }
2208 }
2209
2210 if (i.tm.opcode_modifier.fwait)
2211 if (!add_prefix (FWAIT_OPCODE))
2212 return;
2213
2214 /* Check string instruction segment overrides. */
2215 if (i.tm.opcode_modifier.isstring && i.mem_operands != 0)
2216 {
2217 if (!check_string ())
2218 return;
2219 }
2220
2221 if (!process_suffix ())
2222 return;
2223
2224 /* Make still unresolved immediate matches conform to size of immediate
2225 given in i.suffix. */
2226 if (!finalize_imm ())
2227 return;
2228
2229 if (i.types[0].bitfield.imm1)
2230 i.imm_operands = 0; /* kludge for shift insns. */
2231
2232 for (j = 0; j < 3; j++)
2233 if (i.types[j].bitfield.inoutportreg
2234 || i.types[j].bitfield.shiftcount
2235 || i.types[j].bitfield.acc
2236 || i.types[j].bitfield.floatacc)
2237 i.reg_operands--;
2238
2239 if (i.tm.opcode_modifier.immext)
2240 {
2241 expressionS *exp;
2242
2243 if (i.tm.cpu_flags.bitfield.cpusse3 && i.operands > 0)
2244 {
2245 /* Streaming SIMD extensions 3 Instructions have the fixed
2246 operands with an opcode suffix which is coded in the same
2247 place as an 8-bit immediate field would be. Here we check
2248 those operands and remove them afterwards. */
2249 unsigned int x;
2250
2251 for (x = 0; x < i.operands; x++)
2252 if (i.op[x].regs->reg_num != x)
2253 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
2254 register_prefix,
2255 i.op[x].regs->reg_name,
2256 x + 1,
2257 i.tm.name);
2258 i.operands = 0;
2259 }
2260
2261 /* These AMD 3DNow! and Intel Katmai New Instructions have an
2262 opcode suffix which is coded in the same place as an 8-bit
2263 immediate field would be. Here we fake an 8-bit immediate
2264 operand from the opcode suffix stored in tm.extension_opcode. */
2265
2266 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
2267
2268 exp = &im_expressions[i.imm_operands++];
2269 i.op[i.operands].imms = exp;
2270 UINTS_CLEAR (i.types[i.operands]);
2271 i.types[i.operands].bitfield.imm8 = 1;
2272 i.operands++;
2273 exp->X_op = O_constant;
2274 exp->X_add_number = i.tm.extension_opcode;
2275 i.tm.extension_opcode = None;
2276 }
2277
2278 /* For insns with operands there are more diddles to do to the opcode. */
2279 if (i.operands)
2280 {
2281 if (!process_operands ())
2282 return;
2283 }
2284 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
2285 {
2286 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
2287 as_warn (_("translating to `%sp'"), i.tm.name);
2288 }
2289
2290 /* Handle conversion of 'int $3' --> special int3 insn. */
2291 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
2292 {
2293 i.tm.base_opcode = INT3_OPCODE;
2294 i.imm_operands = 0;
2295 }
2296
2297 if ((i.tm.opcode_modifier.jump
2298 || i.tm.opcode_modifier.jumpbyte
2299 || i.tm.opcode_modifier.jumpdword)
2300 && i.op[0].disps->X_op == O_constant)
2301 {
2302 /* Convert "jmp constant" (and "call constant") to a jump (call) to
2303 the absolute address given by the constant. Since ix86 jumps and
2304 calls are pc relative, we need to generate a reloc. */
2305 i.op[0].disps->X_add_symbol = &abs_symbol;
2306 i.op[0].disps->X_op = O_symbol;
2307 }
2308
2309 if (i.tm.opcode_modifier.rex64)
2310 i.rex |= REX_W;
2311
2312 /* For 8 bit registers we need an empty rex prefix. Also if the
2313 instruction already has a prefix, we need to convert old
2314 registers to new ones. */
2315
2316 if ((i.types[0].bitfield.reg8
2317 && (i.op[0].regs->reg_flags & RegRex64) != 0)
2318 || (i.types[1].bitfield.reg8
2319 && (i.op[1].regs->reg_flags & RegRex64) != 0)
2320 || ((i.types[0].bitfield.reg8
2321 || i.types[1].bitfield.reg8)
2322 && i.rex != 0))
2323 {
2324 int x;
2325
2326 i.rex |= REX_OPCODE;
2327 for (x = 0; x < 2; x++)
2328 {
2329 /* Look for 8 bit operand that uses old registers. */
2330 if (i.types[x].bitfield.reg8
2331 && (i.op[x].regs->reg_flags & RegRex64) == 0)
2332 {
2333 /* In case it is "hi" register, give up. */
2334 if (i.op[x].regs->reg_num > 3)
2335 as_bad (_("can't encode register '%s%s' in an "
2336 "instruction requiring REX prefix."),
2337 register_prefix, i.op[x].regs->reg_name);
2338
2339 /* Otherwise it is equivalent to the extended register.
2340 Since the encoding doesn't change this is merely
2341 cosmetic cleanup for debug output. */
2342
2343 i.op[x].regs = i.op[x].regs + 8;
2344 }
2345 }
2346 }
2347
2348 if (i.rex != 0)
2349 add_prefix (REX_OPCODE | i.rex);
2350
2351 /* We are ready to output the insn. */
2352 output_insn ();
2353 }
2354
2355 static char *
2356 parse_insn (char *line, char *mnemonic)
2357 {
2358 char *l = line;
2359 char *token_start = l;
2360 char *mnem_p;
2361 int supported;
2362 const template *t;
2363
2364 /* Non-zero if we found a prefix only acceptable with string insns. */
2365 const char *expecting_string_instruction = NULL;
2366
2367 while (1)
2368 {
2369 mnem_p = mnemonic;
2370 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
2371 {
2372 mnem_p++;
2373 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
2374 {
2375 as_bad (_("no such instruction: `%s'"), token_start);
2376 return NULL;
2377 }
2378 l++;
2379 }
2380 if (!is_space_char (*l)
2381 && *l != END_OF_INSN
2382 && (intel_syntax
2383 || (*l != PREFIX_SEPARATOR
2384 && *l != ',')))
2385 {
2386 as_bad (_("invalid character %s in mnemonic"),
2387 output_invalid (*l));
2388 return NULL;
2389 }
2390 if (token_start == l)
2391 {
2392 if (!intel_syntax && *l == PREFIX_SEPARATOR)
2393 as_bad (_("expecting prefix; got nothing"));
2394 else
2395 as_bad (_("expecting mnemonic; got nothing"));
2396 return NULL;
2397 }
2398
2399 /* Look up instruction (or prefix) via hash table. */
2400 current_templates = hash_find (op_hash, mnemonic);
2401
2402 if (*l != END_OF_INSN
2403 && (!is_space_char (*l) || l[1] != END_OF_INSN)
2404 && current_templates
2405 && current_templates->start->opcode_modifier.isprefix)
2406 {
2407 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2408 {
2409 as_bad ((flag_code != CODE_64BIT
2410 ? _("`%s' is only supported in 64-bit mode")
2411 : _("`%s' is not supported in 64-bit mode")),
2412 current_templates->start->name);
2413 return NULL;
2414 }
2415 /* If we are in 16-bit mode, do not allow addr16 or data16.
2416 Similarly, in 32-bit mode, do not allow addr32 or data32. */
2417 if ((current_templates->start->opcode_modifier.size16
2418 || current_templates->start->opcode_modifier.size32)
2419 && flag_code != CODE_64BIT
2420 && (current_templates->start->opcode_modifier.size32
2421 ^ (flag_code == CODE_16BIT)))
2422 {
2423 as_bad (_("redundant %s prefix"),
2424 current_templates->start->name);
2425 return NULL;
2426 }
2427 /* Add prefix, checking for repeated prefixes. */
2428 switch (add_prefix (current_templates->start->base_opcode))
2429 {
2430 case 0:
2431 return NULL;
2432 case 2:
2433 expecting_string_instruction = current_templates->start->name;
2434 break;
2435 }
2436 /* Skip past PREFIX_SEPARATOR and reset token_start. */
2437 token_start = ++l;
2438 }
2439 else
2440 break;
2441 }
2442
2443 if (!current_templates)
2444 {
2445 /* See if we can get a match by trimming off a suffix. */
2446 switch (mnem_p[-1])
2447 {
2448 case WORD_MNEM_SUFFIX:
2449 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
2450 i.suffix = SHORT_MNEM_SUFFIX;
2451 else
2452 case BYTE_MNEM_SUFFIX:
2453 case QWORD_MNEM_SUFFIX:
2454 i.suffix = mnem_p[-1];
2455 mnem_p[-1] = '\0';
2456 current_templates = hash_find (op_hash, mnemonic);
2457 break;
2458 case SHORT_MNEM_SUFFIX:
2459 case LONG_MNEM_SUFFIX:
2460 if (!intel_syntax)
2461 {
2462 i.suffix = mnem_p[-1];
2463 mnem_p[-1] = '\0';
2464 current_templates = hash_find (op_hash, mnemonic);
2465 }
2466 break;
2467
2468 /* Intel Syntax. */
2469 case 'd':
2470 if (intel_syntax)
2471 {
2472 if (intel_float_operand (mnemonic) == 1)
2473 i.suffix = SHORT_MNEM_SUFFIX;
2474 else
2475 i.suffix = LONG_MNEM_SUFFIX;
2476 mnem_p[-1] = '\0';
2477 current_templates = hash_find (op_hash, mnemonic);
2478 }
2479 break;
2480 }
2481 if (!current_templates)
2482 {
2483 as_bad (_("no such instruction: `%s'"), token_start);
2484 return NULL;
2485 }
2486 }
2487
2488 if (current_templates->start->opcode_modifier.jump
2489 || current_templates->start->opcode_modifier.jumpbyte)
2490 {
2491 /* Check for a branch hint. We allow ",pt" and ",pn" for
2492 predict taken and predict not taken respectively.
2493 I'm not sure that branch hints actually do anything on loop
2494 and jcxz insns (JumpByte) for current Pentium4 chips. They
2495 may work in the future and it doesn't hurt to accept them
2496 now. */
2497 if (l[0] == ',' && l[1] == 'p')
2498 {
2499 if (l[2] == 't')
2500 {
2501 if (!add_prefix (DS_PREFIX_OPCODE))
2502 return NULL;
2503 l += 3;
2504 }
2505 else if (l[2] == 'n')
2506 {
2507 if (!add_prefix (CS_PREFIX_OPCODE))
2508 return NULL;
2509 l += 3;
2510 }
2511 }
2512 }
2513 /* Any other comma loses. */
2514 if (*l == ',')
2515 {
2516 as_bad (_("invalid character %s in mnemonic"),
2517 output_invalid (*l));
2518 return NULL;
2519 }
2520
2521 /* Check if instruction is supported on specified architecture. */
2522 supported = 0;
2523 for (t = current_templates->start; t < current_templates->end; ++t)
2524 {
2525 if (cpu_flags_match (t->cpu_flags))
2526 supported |= 1;
2527 if (cpu_flags_check_cpu64 (t->cpu_flags))
2528 supported |= 2;
2529 }
2530 if (!(supported & 2))
2531 {
2532 as_bad (flag_code == CODE_64BIT
2533 ? _("`%s' is not supported in 64-bit mode")
2534 : _("`%s' is only supported in 64-bit mode"),
2535 current_templates->start->name);
2536 return NULL;
2537 }
2538 if (!(supported & 1))
2539 {
2540 as_warn (_("`%s' is not supported on `%s%s'"),
2541 current_templates->start->name,
2542 cpu_arch_name,
2543 cpu_sub_arch_name ? cpu_sub_arch_name : "");
2544 }
2545 else if (!cpu_arch_flags.bitfield.cpui386
2546 && (flag_code != CODE_16BIT))
2547 {
2548 as_warn (_("use .code16 to ensure correct addressing mode"));
2549 }
2550
2551 /* Check for rep/repne without a string instruction. */
2552 if (expecting_string_instruction)
2553 {
2554 static templates override;
2555
2556 for (t = current_templates->start; t < current_templates->end; ++t)
2557 if (t->opcode_modifier.isstring)
2558 break;
2559 if (t >= current_templates->end)
2560 {
2561 as_bad (_("expecting string instruction after `%s'"),
2562 expecting_string_instruction);
2563 return NULL;
2564 }
2565 for (override.start = t; t < current_templates->end; ++t)
2566 if (!t->opcode_modifier.isstring)
2567 break;
2568 override.end = t;
2569 current_templates = &override;
2570 }
2571
2572 return l;
2573 }
2574
2575 static char *
2576 parse_operands (char *l, const char *mnemonic)
2577 {
2578 char *token_start;
2579
2580 /* 1 if operand is pending after ','. */
2581 unsigned int expecting_operand = 0;
2582
2583 /* Non-zero if operand parens not balanced. */
2584 unsigned int paren_not_balanced;
2585
2586 while (*l != END_OF_INSN)
2587 {
2588 /* Skip optional white space before operand. */
2589 if (is_space_char (*l))
2590 ++l;
2591 if (!is_operand_char (*l) && *l != END_OF_INSN)
2592 {
2593 as_bad (_("invalid character %s before operand %d"),
2594 output_invalid (*l),
2595 i.operands + 1);
2596 return NULL;
2597 }
2598 token_start = l; /* after white space */
2599 paren_not_balanced = 0;
2600 while (paren_not_balanced || *l != ',')
2601 {
2602 if (*l == END_OF_INSN)
2603 {
2604 if (paren_not_balanced)
2605 {
2606 if (!intel_syntax)
2607 as_bad (_("unbalanced parenthesis in operand %d."),
2608 i.operands + 1);
2609 else
2610 as_bad (_("unbalanced brackets in operand %d."),
2611 i.operands + 1);
2612 return NULL;
2613 }
2614 else
2615 break; /* we are done */
2616 }
2617 else if (!is_operand_char (*l) && !is_space_char (*l))
2618 {
2619 as_bad (_("invalid character %s in operand %d"),
2620 output_invalid (*l),
2621 i.operands + 1);
2622 return NULL;
2623 }
2624 if (!intel_syntax)
2625 {
2626 if (*l == '(')
2627 ++paren_not_balanced;
2628 if (*l == ')')
2629 --paren_not_balanced;
2630 }
2631 else
2632 {
2633 if (*l == '[')
2634 ++paren_not_balanced;
2635 if (*l == ']')
2636 --paren_not_balanced;
2637 }
2638 l++;
2639 }
2640 if (l != token_start)
2641 { /* Yes, we've read in another operand. */
2642 unsigned int operand_ok;
2643 this_operand = i.operands++;
2644 if (i.operands > MAX_OPERANDS)
2645 {
2646 as_bad (_("spurious operands; (%d operands/instruction max)"),
2647 MAX_OPERANDS);
2648 return NULL;
2649 }
2650 /* Now parse operand adding info to 'i' as we go along. */
2651 END_STRING_AND_SAVE (l);
2652
2653 if (intel_syntax)
2654 operand_ok =
2655 i386_intel_operand (token_start,
2656 intel_float_operand (mnemonic));
2657 else
2658 operand_ok = i386_operand (token_start);
2659
2660 RESTORE_END_STRING (l);
2661 if (!operand_ok)
2662 return NULL;
2663 }
2664 else
2665 {
2666 if (expecting_operand)
2667 {
2668 expecting_operand_after_comma:
2669 as_bad (_("expecting operand after ','; got nothing"));
2670 return NULL;
2671 }
2672 if (*l == ',')
2673 {
2674 as_bad (_("expecting operand before ','; got nothing"));
2675 return NULL;
2676 }
2677 }
2678
2679 /* Now *l must be either ',' or END_OF_INSN. */
2680 if (*l == ',')
2681 {
2682 if (*++l == END_OF_INSN)
2683 {
2684 /* Just skip it, if it's \n complain. */
2685 goto expecting_operand_after_comma;
2686 }
2687 expecting_operand = 1;
2688 }
2689 }
2690 return l;
2691 }
2692
2693 static void
2694 swap_2_operands (int xchg1, int xchg2)
2695 {
2696 union i386_op temp_op;
2697 i386_operand_type temp_type;
2698 enum bfd_reloc_code_real temp_reloc;
2699
2700 temp_type = i.types[xchg2];
2701 i.types[xchg2] = i.types[xchg1];
2702 i.types[xchg1] = temp_type;
2703 temp_op = i.op[xchg2];
2704 i.op[xchg2] = i.op[xchg1];
2705 i.op[xchg1] = temp_op;
2706 temp_reloc = i.reloc[xchg2];
2707 i.reloc[xchg2] = i.reloc[xchg1];
2708 i.reloc[xchg1] = temp_reloc;
2709 }
2710
2711 static void
2712 swap_operands (void)
2713 {
2714 switch (i.operands)
2715 {
2716 case 4:
2717 swap_2_operands (1, i.operands - 2);
2718 case 3:
2719 case 2:
2720 swap_2_operands (0, i.operands - 1);
2721 break;
2722 default:
2723 abort ();
2724 }
2725
2726 if (i.mem_operands == 2)
2727 {
2728 const seg_entry *temp_seg;
2729 temp_seg = i.seg[0];
2730 i.seg[0] = i.seg[1];
2731 i.seg[1] = temp_seg;
2732 }
2733 }
2734
2735 /* Try to ensure constant immediates are represented in the smallest
2736 opcode possible. */
2737 static void
2738 optimize_imm (void)
2739 {
2740 char guess_suffix = 0;
2741 int op;
2742
2743 if (i.suffix)
2744 guess_suffix = i.suffix;
2745 else if (i.reg_operands)
2746 {
2747 /* Figure out a suffix from the last register operand specified.
2748 We can't do this properly yet, ie. excluding InOutPortReg,
2749 but the following works for instructions with immediates.
2750 In any case, we can't set i.suffix yet. */
2751 for (op = i.operands; --op >= 0;)
2752 if (i.types[op].bitfield.reg8)
2753 {
2754 guess_suffix = BYTE_MNEM_SUFFIX;
2755 break;
2756 }
2757 else if (i.types[op].bitfield.reg16)
2758 {
2759 guess_suffix = WORD_MNEM_SUFFIX;
2760 break;
2761 }
2762 else if (i.types[op].bitfield.reg32)
2763 {
2764 guess_suffix = LONG_MNEM_SUFFIX;
2765 break;
2766 }
2767 else if (i.types[op].bitfield.reg64)
2768 {
2769 guess_suffix = QWORD_MNEM_SUFFIX;
2770 break;
2771 }
2772 }
2773 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
2774 guess_suffix = WORD_MNEM_SUFFIX;
2775
2776 for (op = i.operands; --op >= 0;)
2777 if (operand_type_check (i.types[op], imm))
2778 {
2779 switch (i.op[op].imms->X_op)
2780 {
2781 case O_constant:
2782 /* If a suffix is given, this operand may be shortened. */
2783 switch (guess_suffix)
2784 {
2785 case LONG_MNEM_SUFFIX:
2786 i.types[op].bitfield.imm32 = 1;
2787 i.types[op].bitfield.imm64 = 1;
2788 break;
2789 case WORD_MNEM_SUFFIX:
2790 i.types[op].bitfield.imm16 = 1;
2791 i.types[op].bitfield.imm32 = 1;
2792 i.types[op].bitfield.imm32s = 1;
2793 i.types[op].bitfield.imm64 = 1;
2794 break;
2795 case BYTE_MNEM_SUFFIX:
2796 i.types[op].bitfield.imm8 = 1;
2797 i.types[op].bitfield.imm8s = 1;
2798 i.types[op].bitfield.imm16 = 1;
2799 i.types[op].bitfield.imm32 = 1;
2800 i.types[op].bitfield.imm32s = 1;
2801 i.types[op].bitfield.imm64 = 1;
2802 break;
2803 }
2804
2805 /* If this operand is at most 16 bits, convert it
2806 to a signed 16 bit number before trying to see
2807 whether it will fit in an even smaller size.
2808 This allows a 16-bit operand such as $0xffe0 to
2809 be recognised as within Imm8S range. */
2810 if ((i.types[op].bitfield.imm16)
2811 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
2812 {
2813 i.op[op].imms->X_add_number =
2814 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
2815 }
2816 if ((i.types[op].bitfield.imm32)
2817 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
2818 == 0))
2819 {
2820 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
2821 ^ ((offsetT) 1 << 31))
2822 - ((offsetT) 1 << 31));
2823 }
2824 i.types[op]
2825 = operand_type_or (i.types[op],
2826 smallest_imm_type (i.op[op].imms->X_add_number));
2827
2828 /* We must avoid matching of Imm32 templates when 64bit
2829 only immediate is available. */
2830 if (guess_suffix == QWORD_MNEM_SUFFIX)
2831 i.types[op].bitfield.imm32 = 0;
2832 break;
2833
2834 case O_absent:
2835 case O_register:
2836 abort ();
2837
2838 /* Symbols and expressions. */
2839 default:
2840 /* Convert symbolic operand to proper sizes for matching, but don't
2841 prevent matching a set of insns that only supports sizes other
2842 than those matching the insn suffix. */
2843 {
2844 i386_operand_type mask, allowed;
2845 const template *t;
2846
2847 UINTS_CLEAR (mask);
2848 UINTS_CLEAR (allowed);
2849
2850 for (t = current_templates->start;
2851 t < current_templates->end;
2852 ++t)
2853 allowed = operand_type_or (allowed,
2854 t->operand_types[op]);
2855 switch (guess_suffix)
2856 {
2857 case QWORD_MNEM_SUFFIX:
2858 mask.bitfield.imm64 = 1;
2859 mask.bitfield.imm32s = 1;
2860 break;
2861 case LONG_MNEM_SUFFIX:
2862 mask.bitfield.imm32 = 1;
2863 break;
2864 case WORD_MNEM_SUFFIX:
2865 mask.bitfield.imm16 = 1;
2866 break;
2867 case BYTE_MNEM_SUFFIX:
2868 mask.bitfield.imm8 = 1;
2869 break;
2870 default:
2871 break;
2872 }
2873 allowed = operand_type_and (mask, allowed);
2874 if (!UINTS_ALL_ZERO (allowed))
2875 i.types[op] = operand_type_and (i.types[op], mask);
2876 }
2877 break;
2878 }
2879 }
2880 }
2881
2882 /* Try to use the smallest displacement type too. */
2883 static void
2884 optimize_disp (void)
2885 {
2886 int op;
2887
2888 for (op = i.operands; --op >= 0;)
2889 if (operand_type_check (i.types[op], disp))
2890 {
2891 if (i.op[op].disps->X_op == O_constant)
2892 {
2893 offsetT disp = i.op[op].disps->X_add_number;
2894
2895 if (i.types[op].bitfield.disp16
2896 && (disp & ~(offsetT) 0xffff) == 0)
2897 {
2898 /* If this operand is at most 16 bits, convert
2899 to a signed 16 bit number and don't use 64bit
2900 displacement. */
2901 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
2902 i.types[op].bitfield.disp64 = 0;
2903 }
2904 if (i.types[op].bitfield.disp32
2905 && (disp & ~(((offsetT) 2 << 31) - 1)) == 0)
2906 {
2907 /* If this operand is at most 32 bits, convert
2908 to a signed 32 bit number and don't use 64bit
2909 displacement. */
2910 disp &= (((offsetT) 2 << 31) - 1);
2911 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
2912 i.types[op].bitfield.disp64 = 0;
2913 }
2914 if (!disp && i.types[op].bitfield.baseindex)
2915 {
2916 i.types[op].bitfield.disp8 = 0;
2917 i.types[op].bitfield.disp16 = 0;
2918 i.types[op].bitfield.disp32 = 0;
2919 i.types[op].bitfield.disp32s = 0;
2920 i.types[op].bitfield.disp64 = 0;
2921 i.op[op].disps = 0;
2922 i.disp_operands--;
2923 }
2924 else if (flag_code == CODE_64BIT)
2925 {
2926 if (fits_in_signed_long (disp))
2927 {
2928 i.types[op].bitfield.disp64 = 0;
2929 i.types[op].bitfield.disp32s = 1;
2930 }
2931 if (fits_in_unsigned_long (disp))
2932 i.types[op].bitfield.disp32 = 1;
2933 }
2934 if ((i.types[op].bitfield.disp32
2935 || i.types[op].bitfield.disp32s
2936 || i.types[op].bitfield.disp16)
2937 && fits_in_signed_byte (disp))
2938 i.types[op].bitfield.disp8 = 1;
2939 }
2940 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
2941 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
2942 {
2943 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
2944 i.op[op].disps, 0, i.reloc[op]);
2945 i.types[op].bitfield.disp8 = 0;
2946 i.types[op].bitfield.disp16 = 0;
2947 i.types[op].bitfield.disp32 = 0;
2948 i.types[op].bitfield.disp32s = 0;
2949 i.types[op].bitfield.disp64 = 0;
2950 }
2951 else
2952 /* We only support 64bit displacement on constants. */
2953 i.types[op].bitfield.disp64 = 0;
2954 }
2955 }
2956
2957 static int
2958 match_template (void)
2959 {
2960 /* Points to template once we've found it. */
2961 const template *t;
2962 i386_operand_type overlap0, overlap1, overlap2, overlap3;
2963 unsigned int found_reverse_match;
2964 i386_opcode_modifier suffix_check;
2965 i386_operand_type operand_types [MAX_OPERANDS];
2966 int addr_prefix_disp;
2967 unsigned int j;
2968 i386_cpu_flags overlap;
2969
2970 #if MAX_OPERANDS != 4
2971 # error "MAX_OPERANDS must be 4."
2972 #endif
2973
2974 found_reverse_match = 0;
2975 addr_prefix_disp = -1;
2976
2977 memset (&suffix_check, 0, sizeof (suffix_check));
2978 if (i.suffix == BYTE_MNEM_SUFFIX)
2979 suffix_check.no_bsuf = 1;
2980 else if (i.suffix == WORD_MNEM_SUFFIX)
2981 suffix_check.no_wsuf = 1;
2982 else if (i.suffix == SHORT_MNEM_SUFFIX)
2983 suffix_check.no_ssuf = 1;
2984 else if (i.suffix == LONG_MNEM_SUFFIX)
2985 suffix_check.no_lsuf = 1;
2986 else if (i.suffix == QWORD_MNEM_SUFFIX)
2987 suffix_check.no_qsuf = 1;
2988 else if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
2989 suffix_check.no_xsuf = 1;
2990
2991 for (t = current_templates->start; t < current_templates->end; t++)
2992 {
2993 addr_prefix_disp = -1;
2994
2995 /* Must have right number of operands. */
2996 if (i.operands != t->operands)
2997 continue;
2998
2999 /* Check the suffix, except for some instructions in intel mode. */
3000 if (((t->opcode_modifier.no_bsuf & suffix_check.no_bsuf)
3001 || (t->opcode_modifier.no_wsuf & suffix_check.no_wsuf)
3002 || (t->opcode_modifier.no_lsuf & suffix_check.no_lsuf)
3003 || (t->opcode_modifier.no_ssuf & suffix_check.no_ssuf)
3004 || (t->opcode_modifier.no_qsuf & suffix_check.no_qsuf)
3005 || (t->opcode_modifier.no_xsuf & suffix_check.no_xsuf))
3006 && !(intel_syntax && t->opcode_modifier.ignoresize))
3007 continue;
3008
3009 for (j = 0; j < MAX_OPERANDS; j++)
3010 operand_types [j] = t->operand_types [j];
3011
3012 /* In general, don't allow 64-bit operands in 32-bit mode. */
3013 if (i.suffix == QWORD_MNEM_SUFFIX
3014 && flag_code != CODE_64BIT
3015 && (intel_syntax
3016 ? (!t->opcode_modifier.ignoresize
3017 && !intel_float_operand (t->name))
3018 : intel_float_operand (t->name) != 2)
3019 && ((!operand_types[0].bitfield.regmmx
3020 && !operand_types[0].bitfield.regxmm)
3021 || (!operand_types[t->operands > 1].bitfield.regmmx
3022 && !!operand_types[t->operands > 1].bitfield.regxmm))
3023 && (t->base_opcode != 0x0fc7
3024 || t->extension_opcode != 1 /* cmpxchg8b */))
3025 continue;
3026
3027 /* Do not verify operands when there are none. */
3028 else
3029 {
3030 overlap = cpu_flags_and (t->cpu_flags, cpu_arch_flags_not);
3031 if (!t->operands)
3032 {
3033 if (!UINTS_ALL_ZERO (overlap))
3034 continue;
3035 /* We've found a match; break out of loop. */
3036 break;
3037 }
3038 }
3039
3040 /* Address size prefix will turn Disp64/Disp32/Disp16 operand
3041 into Disp32/Disp16/Disp32 operand. */
3042 if (i.prefix[ADDR_PREFIX] != 0)
3043 {
3044 /* There should be only one Disp operand. */
3045 switch (flag_code)
3046 {
3047 case CODE_16BIT:
3048 for (j = 0; j < MAX_OPERANDS; j++)
3049 {
3050 if (operand_types[j].bitfield.disp16)
3051 {
3052 addr_prefix_disp = j;
3053 operand_types[j].bitfield.disp32 = 1;
3054 operand_types[j].bitfield.disp16 = 0;
3055 break;
3056 }
3057 }
3058 break;
3059 case CODE_32BIT:
3060 for (j = 0; j < MAX_OPERANDS; j++)
3061 {
3062 if (operand_types[j].bitfield.disp32)
3063 {
3064 addr_prefix_disp = j;
3065 operand_types[j].bitfield.disp32 = 0;
3066 operand_types[j].bitfield.disp16 = 1;
3067 break;
3068 }
3069 }
3070 break;
3071 case CODE_64BIT:
3072 for (j = 0; j < MAX_OPERANDS; j++)
3073 {
3074 if (operand_types[j].bitfield.disp64)
3075 {
3076 addr_prefix_disp = j;
3077 operand_types[j].bitfield.disp64 = 0;
3078 operand_types[j].bitfield.disp32 = 1;
3079 break;
3080 }
3081 }
3082 break;
3083 }
3084 }
3085
3086 overlap0 = operand_type_and (i.types[0], operand_types[0]);
3087 switch (t->operands)
3088 {
3089 case 1:
3090 if (!operand_type_match (overlap0, i.types[0]))
3091 continue;
3092 break;
3093 case 2:
3094 /* xchg %eax, %eax is a special case. It is an aliase for nop
3095 only in 32bit mode and we can use opcode 0x90. In 64bit
3096 mode, we can't use 0x90 for xchg %eax, %eax since it should
3097 zero-extend %eax to %rax. */
3098 if (flag_code == CODE_64BIT
3099 && t->base_opcode == 0x90
3100 && UINTS_EQUAL (i.types [0], acc32)
3101 && UINTS_EQUAL (i.types [1], acc32))
3102 continue;
3103 case 3:
3104 case 4:
3105 overlap1 = operand_type_and (i.types[1], operand_types[1]);
3106 if (!operand_type_match (overlap0, i.types[0])
3107 || !operand_type_match (overlap1, i.types[1])
3108 /* monitor in SSE3 is a very special case. The first
3109 register and the second register may have different
3110 sizes. The same applies to crc32 in SSE4.2. It is
3111 also true for invlpga, vmload, vmrun and vmsave in
3112 SVME. */
3113 || !((t->base_opcode == 0x0f01
3114 && (t->extension_opcode == 0xc8
3115 || t->extension_opcode == 0xd8
3116 || t->extension_opcode == 0xda
3117 || t->extension_opcode == 0xdb
3118 || t->extension_opcode == 0xdf))
3119 || t->base_opcode == 0xf20f38f1
3120 || operand_type_register_match (overlap0, i.types[0],
3121 operand_types[0],
3122 overlap1, i.types[1],
3123 operand_types[1])))
3124 {
3125 /* Check if other direction is valid ... */
3126 if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
3127 continue;
3128
3129 /* Try reversing direction of operands. */
3130 overlap0 = operand_type_and (i.types[0], operand_types[1]);
3131 overlap1 = operand_type_and (i.types[1], operand_types[0]);
3132 if (!operand_type_match (overlap0, i.types[0])
3133 || !operand_type_match (overlap1, i.types[1])
3134 || !operand_type_register_match (overlap0, i.types[0],
3135 operand_types[1],
3136 overlap1, i.types[1],
3137 operand_types[0]))
3138 {
3139 /* Does not match either direction. */
3140 continue;
3141 }
3142 /* found_reverse_match holds which of D or FloatDR
3143 we've found. */
3144 if (t->opcode_modifier.d)
3145 found_reverse_match = Opcode_D;
3146 else if (t->opcode_modifier.floatd)
3147 found_reverse_match = Opcode_FloatD;
3148 else
3149 found_reverse_match = 0;
3150 if (t->opcode_modifier.floatr)
3151 found_reverse_match |= Opcode_FloatR;
3152 }
3153 else
3154 {
3155 /* Found a forward 2 operand match here. */
3156 switch (t->operands)
3157 {
3158 case 4:
3159 overlap3 = operand_type_and (i.types[3],
3160 operand_types[3]);
3161 case 3:
3162 overlap2 = operand_type_and (i.types[2],
3163 operand_types[2]);
3164 break;
3165 }
3166
3167 switch (t->operands)
3168 {
3169 case 4:
3170 if (!operand_type_match (overlap3, i.types[3])
3171 || !operand_type_register_match (overlap2,
3172 i.types[2],
3173 operand_types[2],
3174 overlap3,
3175 i.types[3],
3176 operand_types[3]))
3177 continue;
3178 case 3:
3179 /* Here we make use of the fact that there are no
3180 reverse match 3 operand instructions, and all 3
3181 operand instructions only need to be checked for
3182 register consistency between operands 2 and 3. */
3183 if (!operand_type_match (overlap2, i.types[2])
3184 || !operand_type_register_match (overlap1,
3185 i.types[1],
3186 operand_types[1],
3187 overlap2,
3188 i.types[2],
3189 operand_types[2]))
3190 continue;
3191 break;
3192 }
3193 }
3194 /* Found either forward/reverse 2, 3 or 4 operand match here:
3195 slip through to break. */
3196 }
3197 if (!UINTS_ALL_ZERO (overlap))
3198 {
3199 found_reverse_match = 0;
3200 continue;
3201 }
3202 /* We've found a match; break out of loop. */
3203 break;
3204 }
3205
3206 if (t == current_templates->end)
3207 {
3208 /* We found no match. */
3209 as_bad (_("suffix or operands invalid for `%s'"),
3210 current_templates->start->name);
3211 return 0;
3212 }
3213
3214 if (!quiet_warnings)
3215 {
3216 if (!intel_syntax
3217 && (i.types[0].bitfield.jumpabsolute
3218 != operand_types[0].bitfield.jumpabsolute))
3219 {
3220 as_warn (_("indirect %s without `*'"), t->name);
3221 }
3222
3223 if (t->opcode_modifier.isprefix
3224 && t->opcode_modifier.ignoresize)
3225 {
3226 /* Warn them that a data or address size prefix doesn't
3227 affect assembly of the next line of code. */
3228 as_warn (_("stand-alone `%s' prefix"), t->name);
3229 }
3230 }
3231
3232 /* Copy the template we found. */
3233 i.tm = *t;
3234
3235 if (addr_prefix_disp != -1)
3236 i.tm.operand_types[addr_prefix_disp]
3237 = operand_types[addr_prefix_disp];
3238
3239 if (found_reverse_match)
3240 {
3241 /* If we found a reverse match we must alter the opcode
3242 direction bit. found_reverse_match holds bits to change
3243 (different for int & float insns). */
3244
3245 i.tm.base_opcode ^= found_reverse_match;
3246
3247 i.tm.operand_types[0] = operand_types[1];
3248 i.tm.operand_types[1] = operand_types[0];
3249 }
3250
3251 return 1;
3252 }
3253
3254 static int
3255 check_string (void)
3256 {
3257 int mem_op = operand_type_check (i.types[0], anymem) ? 0 : 1;
3258 if (i.tm.operand_types[mem_op].bitfield.esseg)
3259 {
3260 if (i.seg[0] != NULL && i.seg[0] != &es)
3261 {
3262 as_bad (_("`%s' operand %d must use `%%es' segment"),
3263 i.tm.name,
3264 mem_op + 1);
3265 return 0;
3266 }
3267 /* There's only ever one segment override allowed per instruction.
3268 This instruction possibly has a legal segment override on the
3269 second operand, so copy the segment to where non-string
3270 instructions store it, allowing common code. */
3271 i.seg[0] = i.seg[1];
3272 }
3273 else if (i.tm.operand_types[mem_op + 1].bitfield.esseg)
3274 {
3275 if (i.seg[1] != NULL && i.seg[1] != &es)
3276 {
3277 as_bad (_("`%s' operand %d must use `%%es' segment"),
3278 i.tm.name,
3279 mem_op + 2);
3280 return 0;
3281 }
3282 }
3283 return 1;
3284 }
3285
3286 static int
3287 process_suffix (void)
3288 {
3289 /* If matched instruction specifies an explicit instruction mnemonic
3290 suffix, use it. */
3291 if (i.tm.opcode_modifier.size16)
3292 i.suffix = WORD_MNEM_SUFFIX;
3293 else if (i.tm.opcode_modifier.size32)
3294 i.suffix = LONG_MNEM_SUFFIX;
3295 else if (i.tm.opcode_modifier.size64)
3296 i.suffix = QWORD_MNEM_SUFFIX;
3297 else if (i.reg_operands)
3298 {
3299 /* If there's no instruction mnemonic suffix we try to invent one
3300 based on register operands. */
3301 if (!i.suffix)
3302 {
3303 /* We take i.suffix from the last register operand specified,
3304 Destination register type is more significant than source
3305 register type. crc32 in SSE4.2 prefers source register
3306 type. */
3307 if (i.tm.base_opcode == 0xf20f38f1)
3308 {
3309 if (i.types[0].bitfield.reg16)
3310 i.suffix = WORD_MNEM_SUFFIX;
3311 else if (i.types[0].bitfield.reg32)
3312 i.suffix = LONG_MNEM_SUFFIX;
3313 else if (i.types[0].bitfield.reg64)
3314 i.suffix = QWORD_MNEM_SUFFIX;
3315 }
3316 else if (i.tm.base_opcode == 0xf20f38f0)
3317 {
3318 if (i.types[0].bitfield.reg8)
3319 i.suffix = BYTE_MNEM_SUFFIX;
3320 }
3321
3322 if (!i.suffix)
3323 {
3324 int op;
3325
3326 if (i.tm.base_opcode == 0xf20f38f1
3327 || i.tm.base_opcode == 0xf20f38f0)
3328 {
3329 /* We have to know the operand size for crc32. */
3330 as_bad (_("ambiguous memory operand size for `%s`"),
3331 i.tm.name);
3332 return 0;
3333 }
3334
3335 for (op = i.operands; --op >= 0;)
3336 if (!i.tm.operand_types[op].bitfield.inoutportreg)
3337 {
3338 if (i.types[op].bitfield.reg8)
3339 {
3340 i.suffix = BYTE_MNEM_SUFFIX;
3341 break;
3342 }
3343 else if (i.types[op].bitfield.reg16)
3344 {
3345 i.suffix = WORD_MNEM_SUFFIX;
3346 break;
3347 }
3348 else if (i.types[op].bitfield.reg32)
3349 {
3350 i.suffix = LONG_MNEM_SUFFIX;
3351 break;
3352 }
3353 else if (i.types[op].bitfield.reg64)
3354 {
3355 i.suffix = QWORD_MNEM_SUFFIX;
3356 break;
3357 }
3358 }
3359 }
3360 }
3361 else if (i.suffix == BYTE_MNEM_SUFFIX)
3362 {
3363 if (!check_byte_reg ())
3364 return 0;
3365 }
3366 else if (i.suffix == LONG_MNEM_SUFFIX)
3367 {
3368 if (!check_long_reg ())
3369 return 0;
3370 }
3371 else if (i.suffix == QWORD_MNEM_SUFFIX)
3372 {
3373 if (!check_qword_reg ())
3374 return 0;
3375 }
3376 else if (i.suffix == WORD_MNEM_SUFFIX)
3377 {
3378 if (!check_word_reg ())
3379 return 0;
3380 }
3381 else if (intel_syntax && i.tm.opcode_modifier.ignoresize)
3382 /* Do nothing if the instruction is going to ignore the prefix. */
3383 ;
3384 else
3385 abort ();
3386 }
3387 else if (i.tm.opcode_modifier.defaultsize
3388 && !i.suffix
3389 /* exclude fldenv/frstor/fsave/fstenv */
3390 && i.tm.opcode_modifier.no_ssuf)
3391 {
3392 i.suffix = stackop_size;
3393 }
3394 else if (intel_syntax
3395 && !i.suffix
3396 && (i.tm.operand_types[0].bitfield.jumpabsolute
3397 || i.tm.opcode_modifier.jumpbyte
3398 || i.tm.opcode_modifier.jumpintersegment
3399 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
3400 && i.tm.extension_opcode <= 3)))
3401 {
3402 switch (flag_code)
3403 {
3404 case CODE_64BIT:
3405 if (!i.tm.opcode_modifier.no_qsuf)
3406 {
3407 i.suffix = QWORD_MNEM_SUFFIX;
3408 break;
3409 }
3410 case CODE_32BIT:
3411 if (!i.tm.opcode_modifier.no_lsuf)
3412 i.suffix = LONG_MNEM_SUFFIX;
3413 break;
3414 case CODE_16BIT:
3415 if (!i.tm.opcode_modifier.no_wsuf)
3416 i.suffix = WORD_MNEM_SUFFIX;
3417 break;
3418 }
3419 }
3420
3421 if (!i.suffix)
3422 {
3423 if (!intel_syntax)
3424 {
3425 if (i.tm.opcode_modifier.w)
3426 {
3427 as_bad (_("no instruction mnemonic suffix given and "
3428 "no register operands; can't size instruction"));
3429 return 0;
3430 }
3431 }
3432 else
3433 {
3434 unsigned int suffixes;
3435
3436 suffixes = !i.tm.opcode_modifier.no_bsuf;
3437 if (!i.tm.opcode_modifier.no_wsuf)
3438 suffixes |= 1 << 1;
3439 if (!i.tm.opcode_modifier.no_lsuf)
3440 suffixes |= 1 << 2;
3441 if (!i.tm.opcode_modifier.no_lsuf)
3442 suffixes |= 1 << 3;
3443 if (!i.tm.opcode_modifier.no_ssuf)
3444 suffixes |= 1 << 4;
3445 if (!i.tm.opcode_modifier.no_qsuf)
3446 suffixes |= 1 << 5;
3447
3448 /* There are more than suffix matches. */
3449 if (i.tm.opcode_modifier.w
3450 || ((suffixes & (suffixes - 1))
3451 && !i.tm.opcode_modifier.defaultsize
3452 && !i.tm.opcode_modifier.ignoresize))
3453 {
3454 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
3455 return 0;
3456 }
3457 }
3458 }
3459
3460 /* Change the opcode based on the operand size given by i.suffix;
3461 We don't need to change things for byte insns. */
3462
3463 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
3464 {
3465 /* It's not a byte, select word/dword operation. */
3466 if (i.tm.opcode_modifier.w)
3467 {
3468 if (i.tm.opcode_modifier.shortform)
3469 i.tm.base_opcode |= 8;
3470 else
3471 i.tm.base_opcode |= 1;
3472 }
3473
3474 /* Now select between word & dword operations via the operand
3475 size prefix, except for instructions that will ignore this
3476 prefix anyway. */
3477 if (i.tm.base_opcode == 0x0f01
3478 && (i.tm.extension_opcode == 0xc8
3479 || i.tm.extension_opcode == 0xd8
3480 || i.tm.extension_opcode == 0xda
3481 || i.tm.extension_opcode == 0xdb
3482 || i.tm.extension_opcode == 0xdf))
3483 {
3484 /* monitor in SSE3 is a very special case. The default size
3485 of AX is the size of mode. The address size override
3486 prefix will change the size of AX. It is also true for
3487 invlpga, vmload, vmrun and vmsave in SVME. */
3488 if ((flag_code == CODE_32BIT
3489 && i.op->regs[0].reg_type.bitfield.reg16)
3490 || (flag_code != CODE_32BIT
3491 && i.op->regs[0].reg_type.bitfield.reg32))
3492 if (!add_prefix (ADDR_PREFIX_OPCODE))
3493 return 0;
3494 }
3495 else if (i.suffix != QWORD_MNEM_SUFFIX
3496 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
3497 && !i.tm.opcode_modifier.ignoresize
3498 && !i.tm.opcode_modifier.floatmf
3499 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
3500 || (flag_code == CODE_64BIT
3501 && i.tm.opcode_modifier.jumpbyte)))
3502 {
3503 unsigned int prefix = DATA_PREFIX_OPCODE;
3504
3505 if (i.tm.opcode_modifier.jumpbyte) /* jcxz, loop */
3506 prefix = ADDR_PREFIX_OPCODE;
3507
3508 if (!add_prefix (prefix))
3509 return 0;
3510 }
3511
3512 /* Set mode64 for an operand. */
3513 if (i.suffix == QWORD_MNEM_SUFFIX
3514 && flag_code == CODE_64BIT
3515 && !i.tm.opcode_modifier.norex64)
3516 {
3517 /* Special case for xchg %rax,%rax. It is NOP and doesn't
3518 need rex64. cmpxchg8b is also a special case. */
3519 if (! (i.operands == 2
3520 && i.tm.base_opcode == 0x90
3521 && i.tm.extension_opcode == None
3522 && UINTS_EQUAL (i.types [0], acc64)
3523 && UINTS_EQUAL (i.types [1], acc64))
3524 && ! (i.operands == 1
3525 && i.tm.base_opcode == 0xfc7
3526 && i.tm.extension_opcode == 1
3527 && !operand_type_check (i.types [0], reg)
3528 && operand_type_check (i.types [0], anymem)))
3529 i.rex |= REX_W;
3530 }
3531
3532 /* Size floating point instruction. */
3533 if (i.suffix == LONG_MNEM_SUFFIX)
3534 if (i.tm.opcode_modifier.floatmf)
3535 i.tm.base_opcode ^= 4;
3536 }
3537
3538 return 1;
3539 }
3540
3541 static int
3542 check_byte_reg (void)
3543 {
3544 int op;
3545
3546 for (op = i.operands; --op >= 0;)
3547 {
3548 /* If this is an eight bit register, it's OK. If it's the 16 or
3549 32 bit version of an eight bit register, we will just use the
3550 low portion, and that's OK too. */
3551 if (i.types[op].bitfield.reg8)
3552 continue;
3553
3554 /* movzx, movsx, pextrb and pinsrb should not generate this
3555 warning. */
3556 if (intel_syntax
3557 && (i.tm.base_opcode == 0xfb7
3558 || i.tm.base_opcode == 0xfb6
3559 || i.tm.base_opcode == 0x63
3560 || i.tm.base_opcode == 0xfbe
3561 || i.tm.base_opcode == 0xfbf
3562 || i.tm.base_opcode == 0x660f3a14
3563 || i.tm.base_opcode == 0x660f3a20))
3564 continue;
3565
3566 /* crc32 doesn't generate this warning. */
3567 if (i.tm.base_opcode == 0xf20f38f0)
3568 continue;
3569
3570 if ((i.types[op].bitfield.reg16
3571 || i.types[op].bitfield.reg32
3572 || i.types[op].bitfield.reg64)
3573 && i.op[op].regs->reg_num < 4)
3574 {
3575 /* Prohibit these changes in the 64bit mode, since the
3576 lowering is more complicated. */
3577 if (flag_code == CODE_64BIT
3578 && !i.tm.operand_types[op].bitfield.inoutportreg)
3579 {
3580 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3581 register_prefix, i.op[op].regs->reg_name,
3582 i.suffix);
3583 return 0;
3584 }
3585 #if REGISTER_WARNINGS
3586 if (!quiet_warnings
3587 && !i.tm.operand_types[op].bitfield.inoutportreg)
3588 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3589 register_prefix,
3590 (i.op[op].regs + (i.types[op].bitfield.reg16
3591 ? REGNAM_AL - REGNAM_AX
3592 : REGNAM_AL - REGNAM_EAX))->reg_name,
3593 register_prefix,
3594 i.op[op].regs->reg_name,
3595 i.suffix);
3596 #endif
3597 continue;
3598 }
3599 /* Any other register is bad. */
3600 if (i.types[op].bitfield.reg16
3601 || i.types[op].bitfield.reg32
3602 || i.types[op].bitfield.reg64
3603 || i.types[op].bitfield.regmmx
3604 || i.types[op].bitfield.regxmm
3605 || i.types[op].bitfield.sreg2
3606 || i.types[op].bitfield.sreg3
3607 || i.types[op].bitfield.control
3608 || i.types[op].bitfield.debug
3609 || i.types[op].bitfield.test
3610 || i.types[op].bitfield.floatreg
3611 || i.types[op].bitfield.floatacc)
3612 {
3613 as_bad (_("`%s%s' not allowed with `%s%c'"),
3614 register_prefix,
3615 i.op[op].regs->reg_name,
3616 i.tm.name,
3617 i.suffix);
3618 return 0;
3619 }
3620 }
3621 return 1;
3622 }
3623
3624 static int
3625 check_long_reg (void)
3626 {
3627 int op;
3628
3629 for (op = i.operands; --op >= 0;)
3630 /* Reject eight bit registers, except where the template requires
3631 them. (eg. movzb) */
3632 if (i.types[op].bitfield.reg8
3633 && (i.tm.operand_types[op].bitfield.reg16
3634 || i.tm.operand_types[op].bitfield.reg32
3635 || i.tm.operand_types[op].bitfield.acc))
3636 {
3637 as_bad (_("`%s%s' not allowed with `%s%c'"),
3638 register_prefix,
3639 i.op[op].regs->reg_name,
3640 i.tm.name,
3641 i.suffix);
3642 return 0;
3643 }
3644 /* Warn if the e prefix on a general reg is missing. */
3645 else if ((!quiet_warnings || flag_code == CODE_64BIT)
3646 && i.types[op].bitfield.reg16
3647 && (i.tm.operand_types[op].bitfield.reg32
3648 || i.tm.operand_types[op].bitfield.acc))
3649 {
3650 /* Prohibit these changes in the 64bit mode, since the
3651 lowering is more complicated. */
3652 if (flag_code == CODE_64BIT)
3653 {
3654 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3655 register_prefix, i.op[op].regs->reg_name,
3656 i.suffix);
3657 return 0;
3658 }
3659 #if REGISTER_WARNINGS
3660 else
3661 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3662 register_prefix,
3663 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
3664 register_prefix,
3665 i.op[op].regs->reg_name,
3666 i.suffix);
3667 #endif
3668 }
3669 /* Warn if the r prefix on a general reg is missing. */
3670 else if (i.types[op].bitfield.reg64
3671 && (i.tm.operand_types[op].bitfield.reg32
3672 || i.tm.operand_types[op].bitfield.acc))
3673 {
3674 if (intel_syntax
3675 && i.tm.base_opcode == 0xf30f2d
3676 && !i.types[0].bitfield.regxmm)
3677 {
3678 /* cvtss2si converts DWORD memory to Reg64. We want
3679 REX byte. */
3680 i.suffix = QWORD_MNEM_SUFFIX;
3681 }
3682 else
3683 {
3684 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3685 register_prefix, i.op[op].regs->reg_name,
3686 i.suffix);
3687 return 0;
3688 }
3689 }
3690 return 1;
3691 }
3692
3693 static int
3694 check_qword_reg (void)
3695 {
3696 int op;
3697
3698 for (op = i.operands; --op >= 0; )
3699 /* Reject eight bit registers, except where the template requires
3700 them. (eg. movzb) */
3701 if (i.types[op].bitfield.reg8
3702 && (i.tm.operand_types[op].bitfield.reg16
3703 || i.tm.operand_types[op].bitfield.reg32
3704 || i.tm.operand_types[op].bitfield.acc))
3705 {
3706 as_bad (_("`%s%s' not allowed with `%s%c'"),
3707 register_prefix,
3708 i.op[op].regs->reg_name,
3709 i.tm.name,
3710 i.suffix);
3711 return 0;
3712 }
3713 /* Warn if the e prefix on a general reg is missing. */
3714 else if ((i.types[op].bitfield.reg16
3715 || i.types[op].bitfield.reg32)
3716 && (i.tm.operand_types[op].bitfield.reg32
3717 || i.tm.operand_types[op].bitfield.acc))
3718 {
3719 /* Prohibit these changes in the 64bit mode, since the
3720 lowering is more complicated. */
3721 if (intel_syntax
3722 && i.tm.base_opcode == 0xf20f2d
3723 && !i.types[0].bitfield.regxmm)
3724 {
3725 /* cvtsd2si converts QWORD memory to Reg32. We don't want
3726 REX byte. */
3727 i.suffix = LONG_MNEM_SUFFIX;
3728 }
3729 else
3730 {
3731 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3732 register_prefix, i.op[op].regs->reg_name,
3733 i.suffix);
3734 return 0;
3735 }
3736 }
3737 return 1;
3738 }
3739
3740 static int
3741 check_word_reg (void)
3742 {
3743 int op;
3744 for (op = i.operands; --op >= 0;)
3745 /* Reject eight bit registers, except where the template requires
3746 them. (eg. movzb) */
3747 if (i.types[op].bitfield.reg8
3748 && (i.tm.operand_types[op].bitfield.reg16
3749 || i.tm.operand_types[op].bitfield.reg32
3750 || i.tm.operand_types[op].bitfield.acc))
3751 {
3752 as_bad (_("`%s%s' not allowed with `%s%c'"),
3753 register_prefix,
3754 i.op[op].regs->reg_name,
3755 i.tm.name,
3756 i.suffix);
3757 return 0;
3758 }
3759 /* Warn if the e prefix on a general reg is present. */
3760 else if ((!quiet_warnings || flag_code == CODE_64BIT)
3761 && i.types[op].bitfield.reg32
3762 && (i.tm.operand_types[op].bitfield.reg16
3763 || i.tm.operand_types[op].bitfield.acc))
3764 {
3765 /* Prohibit these changes in the 64bit mode, since the
3766 lowering is more complicated. */
3767 if (flag_code == CODE_64BIT)
3768 {
3769 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3770 register_prefix, i.op[op].regs->reg_name,
3771 i.suffix);
3772 return 0;
3773 }
3774 else
3775 #if REGISTER_WARNINGS
3776 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3777 register_prefix,
3778 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
3779 register_prefix,
3780 i.op[op].regs->reg_name,
3781 i.suffix);
3782 #endif
3783 }
3784 return 1;
3785 }
3786
3787 static int
3788 update_imm (unsigned int j)
3789 {
3790 i386_operand_type overlap;
3791
3792 overlap = operand_type_and (i.types[j], i.tm.operand_types[j]);
3793 if ((overlap.bitfield.imm8
3794 || overlap.bitfield.imm8s
3795 || overlap.bitfield.imm16
3796 || overlap.bitfield.imm32
3797 || overlap.bitfield.imm32s
3798 || overlap.bitfield.imm64)
3799 && !UINTS_EQUAL (overlap, imm8)
3800 && !UINTS_EQUAL (overlap, imm8s)
3801 && !UINTS_EQUAL (overlap, imm16)
3802 && !UINTS_EQUAL (overlap, imm32)
3803 && !UINTS_EQUAL (overlap, imm32s)
3804 && !UINTS_EQUAL (overlap, imm64))
3805 {
3806 if (i.suffix)
3807 {
3808 i386_operand_type temp;
3809
3810 UINTS_CLEAR (temp);
3811 if (i.suffix == BYTE_MNEM_SUFFIX)
3812 {
3813 temp.bitfield.imm8 = overlap.bitfield.imm8;
3814 temp.bitfield.imm8s = overlap.bitfield.imm8s;
3815 }
3816 else if (i.suffix == WORD_MNEM_SUFFIX)
3817 temp.bitfield.imm16 = overlap.bitfield.imm16;
3818 else if (i.suffix == QWORD_MNEM_SUFFIX)
3819 {
3820 temp.bitfield.imm64 = overlap.bitfield.imm64;
3821 temp.bitfield.imm32s = overlap.bitfield.imm32s;
3822 }
3823 else
3824 temp.bitfield.imm32 = overlap.bitfield.imm32;
3825 overlap = temp;
3826 }
3827 else if (UINTS_EQUAL (overlap, imm16_32_32s)
3828 || UINTS_EQUAL (overlap, imm16_32)
3829 || UINTS_EQUAL (overlap, imm16_32s))
3830 {
3831 UINTS_CLEAR (overlap);
3832 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
3833 overlap.bitfield.imm16 = 1;
3834 else
3835 overlap.bitfield.imm32s = 1;
3836 }
3837 if (!UINTS_EQUAL (overlap, imm8)
3838 && !UINTS_EQUAL (overlap, imm8s)
3839 && !UINTS_EQUAL (overlap, imm16)
3840 && !UINTS_EQUAL (overlap, imm32)
3841 && !UINTS_EQUAL (overlap, imm32s)
3842 && !UINTS_EQUAL (overlap, imm64))
3843 {
3844 as_bad (_("no instruction mnemonic suffix given; "
3845 "can't determine immediate size"));
3846 return 0;
3847 }
3848 }
3849 i.types[j] = overlap;
3850
3851 return 1;
3852 }
3853
3854 static int
3855 finalize_imm (void)
3856 {
3857 unsigned int j;
3858
3859 for (j = 0; j < 2; j++)
3860 if (update_imm (j) == 0)
3861 return 0;
3862
3863 i.types[2] = operand_type_and (i.types[2], i.tm.operand_types[2]);
3864 assert (operand_type_check (i.types[2], imm) == 0);
3865
3866 return 1;
3867 }
3868
3869 static int
3870 process_operands (void)
3871 {
3872 /* Default segment register this instruction will use for memory
3873 accesses. 0 means unknown. This is only for optimizing out
3874 unnecessary segment overrides. */
3875 const seg_entry *default_seg = 0;
3876
3877 /* The imul $imm, %reg instruction is converted into
3878 imul $imm, %reg, %reg, and the clr %reg instruction
3879 is converted into xor %reg, %reg. */
3880 if (i.tm.opcode_modifier.regkludge)
3881 {
3882 if (i.tm.cpu_flags.bitfield.cpusse4_1)
3883 {
3884 /* The first operand in instruction blendvpd, blendvps and
3885 pblendvb in SSE4.1 is implicit and must be xmm0. */
3886 assert (i.operands == 3
3887 && i.reg_operands >= 2
3888 && UINTS_EQUAL (i.types[0], regxmm));
3889 if (i.op[0].regs->reg_num != 0)
3890 {
3891 if (intel_syntax)
3892 as_bad (_("the last operand of `%s' must be `%sxmm0'"),
3893 i.tm.name, register_prefix);
3894 else
3895 as_bad (_("the first operand of `%s' must be `%sxmm0'"),
3896 i.tm.name, register_prefix);
3897 return 0;
3898 }
3899 i.op[0] = i.op[1];
3900 i.op[1] = i.op[2];
3901 i.types[0] = i.types[1];
3902 i.types[1] = i.types[2];
3903 i.operands--;
3904 i.reg_operands--;
3905
3906 /* We need to adjust fields in i.tm since they are used by
3907 build_modrm_byte. */
3908 i.tm.operand_types [0] = i.tm.operand_types [1];
3909 i.tm.operand_types [1] = i.tm.operand_types [2];
3910 i.tm.operands--;
3911 }
3912 else
3913 {
3914 unsigned int first_reg_op;
3915
3916 if (operand_type_check (i.types[0], reg))
3917 first_reg_op = 0;
3918 else
3919 first_reg_op = 1;
3920 /* Pretend we saw the extra register operand. */
3921 assert (i.reg_operands == 1
3922 && i.op[first_reg_op + 1].regs == 0);
3923 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
3924 i.types[first_reg_op + 1] = i.types[first_reg_op];
3925 i.operands++;
3926 i.reg_operands++;
3927 }
3928 }
3929
3930 if (i.tm.opcode_modifier.shortform)
3931 {
3932 if (i.types[0].bitfield.sreg2
3933 || i.types[0].bitfield.sreg3)
3934 {
3935 if (i.tm.base_opcode == POP_SEG_SHORT
3936 && i.op[0].regs->reg_num == 1)
3937 {
3938 as_bad (_("you can't `pop %%cs'"));
3939 return 0;
3940 }
3941 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
3942 if ((i.op[0].regs->reg_flags & RegRex) != 0)
3943 i.rex |= REX_B;
3944 }
3945 else
3946 {
3947 /* The register or float register operand is in operand 0 or 1. */
3948 unsigned int op;
3949
3950 if (i.types[0].bitfield.floatreg
3951 || operand_type_check (i.types[0], reg))
3952 op = 0;
3953 else
3954 op = 1;
3955 /* Register goes in low 3 bits of opcode. */
3956 i.tm.base_opcode |= i.op[op].regs->reg_num;
3957 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3958 i.rex |= REX_B;
3959 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
3960 {
3961 /* Warn about some common errors, but press on regardless.
3962 The first case can be generated by gcc (<= 2.8.1). */
3963 if (i.operands == 2)
3964 {
3965 /* Reversed arguments on faddp, fsubp, etc. */
3966 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
3967 register_prefix, i.op[1].regs->reg_name,
3968 register_prefix, i.op[0].regs->reg_name);
3969 }
3970 else
3971 {
3972 /* Extraneous `l' suffix on fp insn. */
3973 as_warn (_("translating to `%s %s%s'"), i.tm.name,
3974 register_prefix, i.op[0].regs->reg_name);
3975 }
3976 }
3977 }
3978 }
3979 else if (i.tm.opcode_modifier.modrm)
3980 {
3981 /* The opcode is completed (modulo i.tm.extension_opcode which
3982 must be put into the modrm byte). Now, we make the modrm and
3983 index base bytes based on all the info we've collected. */
3984
3985 default_seg = build_modrm_byte ();
3986 }
3987 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
3988 {
3989 default_seg = &ds;
3990 }
3991 else if (i.tm.opcode_modifier.isstring)
3992 {
3993 /* For the string instructions that allow a segment override
3994 on one of their operands, the default segment is ds. */
3995 default_seg = &ds;
3996 }
3997
3998 if (i.tm.base_opcode == 0x8d /* lea */
3999 && i.seg[0]
4000 && !quiet_warnings)
4001 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
4002
4003 /* If a segment was explicitly specified, and the specified segment
4004 is not the default, use an opcode prefix to select it. If we
4005 never figured out what the default segment is, then default_seg
4006 will be zero at this point, and the specified segment prefix will
4007 always be used. */
4008 if ((i.seg[0]) && (i.seg[0] != default_seg))
4009 {
4010 if (!add_prefix (i.seg[0]->seg_prefix))
4011 return 0;
4012 }
4013 return 1;
4014 }
4015
4016 static const seg_entry *
4017 build_modrm_byte (void)
4018 {
4019 const seg_entry *default_seg = 0;
4020
4021 /* i.reg_operands MUST be the number of real register operands;
4022 implicit registers do not count. */
4023 if (i.reg_operands == 2)
4024 {
4025 unsigned int source, dest;
4026
4027 switch (i.operands)
4028 {
4029 case 2:
4030 source = 0;
4031 break;
4032 case 3:
4033 /* When there are 3 operands, one of them may be immediate,
4034 which may be the first or the last operand. Otherwise,
4035 the first operand must be shift count register (cl). */
4036 assert (i.imm_operands == 1
4037 || (i.imm_operands == 0
4038 && i.types[0].bitfield.shiftcount));
4039 if (operand_type_check (i.types[0], imm)
4040 || i.types[0].bitfield.shiftcount)
4041 source = 1;
4042 else
4043 source = 0;
4044 break;
4045 case 4:
4046 /* When there are 4 operands, the first two must be immediate
4047 operands. The source operand will be the 3rd one. */
4048 assert (i.imm_operands == 2
4049 && operand_type_check (i.types[0], imm)
4050 && operand_type_check (i.types[1], imm));
4051 source = 2;
4052 break;
4053 default:
4054 abort ();
4055 }
4056
4057 dest = source + 1;
4058
4059 i.rm.mode = 3;
4060 /* One of the register operands will be encoded in the i.tm.reg
4061 field, the other in the combined i.tm.mode and i.tm.regmem
4062 fields. If no form of this instruction supports a memory
4063 destination operand, then we assume the source operand may
4064 sometimes be a memory operand and so we need to store the
4065 destination in the i.rm.reg field. */
4066 if (!i.tm.operand_types[dest].bitfield.regmem
4067 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
4068 {
4069 i.rm.reg = i.op[dest].regs->reg_num;
4070 i.rm.regmem = i.op[source].regs->reg_num;
4071 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
4072 i.rex |= REX_R;
4073 if ((i.op[source].regs->reg_flags & RegRex) != 0)
4074 i.rex |= REX_B;
4075 }
4076 else
4077 {
4078 i.rm.reg = i.op[source].regs->reg_num;
4079 i.rm.regmem = i.op[dest].regs->reg_num;
4080 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
4081 i.rex |= REX_B;
4082 if ((i.op[source].regs->reg_flags & RegRex) != 0)
4083 i.rex |= REX_R;
4084 }
4085 if (flag_code != CODE_64BIT && (i.rex & (REX_R | REX_B)))
4086 {
4087 if (!i.types[0].bitfield.control
4088 && !i.types[1].bitfield.control)
4089 abort ();
4090 i.rex &= ~(REX_R | REX_B);
4091 add_prefix (LOCK_PREFIX_OPCODE);
4092 }
4093 }
4094 else
4095 { /* If it's not 2 reg operands... */
4096 if (i.mem_operands)
4097 {
4098 unsigned int fake_zero_displacement = 0;
4099 unsigned int op;
4100
4101 for (op = 0; op < i.operands; op++)
4102 if (operand_type_check (i.types[op], anymem))
4103 break;
4104 assert (op < i.operands);
4105
4106 default_seg = &ds;
4107
4108 if (i.base_reg == 0)
4109 {
4110 i.rm.mode = 0;
4111 if (!i.disp_operands)
4112 fake_zero_displacement = 1;
4113 if (i.index_reg == 0)
4114 {
4115 /* Operand is just <disp> */
4116 if (flag_code == CODE_64BIT)
4117 {
4118 /* 64bit mode overwrites the 32bit absolute
4119 addressing by RIP relative addressing and
4120 absolute addressing is encoded by one of the
4121 redundant SIB forms. */
4122 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
4123 i.sib.base = NO_BASE_REGISTER;
4124 i.sib.index = NO_INDEX_REGISTER;
4125 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0)
4126 ? disp32s : disp32);
4127 }
4128 else if ((flag_code == CODE_16BIT)
4129 ^ (i.prefix[ADDR_PREFIX] != 0))
4130 {
4131 i.rm.regmem = NO_BASE_REGISTER_16;
4132 i.types[op] = disp16;
4133 }
4134 else
4135 {
4136 i.rm.regmem = NO_BASE_REGISTER;
4137 i.types[op] = disp32;
4138 }
4139 }
4140 else /* !i.base_reg && i.index_reg */
4141 {
4142 i.sib.index = i.index_reg->reg_num;
4143 i.sib.base = NO_BASE_REGISTER;
4144 i.sib.scale = i.log2_scale_factor;
4145 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
4146 i.types[op].bitfield.disp8 = 0;
4147 i.types[op].bitfield.disp16 = 0;
4148 i.types[op].bitfield.disp64 = 0;
4149 if (flag_code != CODE_64BIT)
4150 {
4151 /* Must be 32 bit */
4152 i.types[op].bitfield.disp32 = 1;
4153 i.types[op].bitfield.disp32s = 0;
4154 }
4155 else
4156 {
4157 i.types[op].bitfield.disp32 = 0;
4158 i.types[op].bitfield.disp32s = 1;
4159 }
4160 if ((i.index_reg->reg_flags & RegRex) != 0)
4161 i.rex |= REX_X;
4162 }
4163 }
4164 /* RIP addressing for 64bit mode. */
4165 else if (UINTS_EQUAL (i.base_reg->reg_type, baseindex))
4166 {
4167 i.rm.regmem = NO_BASE_REGISTER;
4168 i.types[op].bitfield.disp8 = 0;
4169 i.types[op].bitfield.disp16 = 0;
4170 i.types[op].bitfield.disp32 = 0;
4171 i.types[op].bitfield.disp32s = 1;
4172 i.types[op].bitfield.disp64 = 0;
4173 i.flags[op] |= Operand_PCrel;
4174 if (! i.disp_operands)
4175 fake_zero_displacement = 1;
4176 }
4177 else if (i.base_reg->reg_type.bitfield.reg16)
4178 {
4179 switch (i.base_reg->reg_num)
4180 {
4181 case 3: /* (%bx) */
4182 if (i.index_reg == 0)
4183 i.rm.regmem = 7;
4184 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
4185 i.rm.regmem = i.index_reg->reg_num - 6;
4186 break;
4187 case 5: /* (%bp) */
4188 default_seg = &ss;
4189 if (i.index_reg == 0)
4190 {
4191 i.rm.regmem = 6;
4192 if (operand_type_check (i.types[op], disp) == 0)
4193 {
4194 /* fake (%bp) into 0(%bp) */
4195 i.types[op].bitfield.disp8 = 1;
4196 fake_zero_displacement = 1;
4197 }
4198 }
4199 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
4200 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
4201 break;
4202 default: /* (%si) -> 4 or (%di) -> 5 */
4203 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
4204 }
4205 i.rm.mode = mode_from_disp_size (i.types[op]);
4206 }
4207 else /* i.base_reg and 32/64 bit mode */
4208 {
4209 if (flag_code == CODE_64BIT
4210 && operand_type_check (i.types[op], disp))
4211 {
4212 i386_operand_type temp;
4213 UINTS_CLEAR (temp);
4214 temp.bitfield.disp8 = i.types[op].bitfield.disp8;
4215 i.types[op] = temp;
4216 if (i.prefix[ADDR_PREFIX] == 0)
4217 i.types[op].bitfield.disp32s = 1;
4218 else
4219 i.types[op].bitfield.disp32 = 1;
4220 }
4221
4222 i.rm.regmem = i.base_reg->reg_num;
4223 if ((i.base_reg->reg_flags & RegRex) != 0)
4224 i.rex |= REX_B;
4225 i.sib.base = i.base_reg->reg_num;
4226 /* x86-64 ignores REX prefix bit here to avoid decoder
4227 complications. */
4228 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
4229 {
4230 default_seg = &ss;
4231 if (i.disp_operands == 0)
4232 {
4233 fake_zero_displacement = 1;
4234 i.types[op].bitfield.disp8 = 1;
4235 }
4236 }
4237 else if (i.base_reg->reg_num == ESP_REG_NUM)
4238 {
4239 default_seg = &ss;
4240 }
4241 i.sib.scale = i.log2_scale_factor;
4242 if (i.index_reg == 0)
4243 {
4244 /* <disp>(%esp) becomes two byte modrm with no index
4245 register. We've already stored the code for esp
4246 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
4247 Any base register besides %esp will not use the
4248 extra modrm byte. */
4249 i.sib.index = NO_INDEX_REGISTER;
4250 #if !SCALE1_WHEN_NO_INDEX
4251 /* Another case where we force the second modrm byte. */
4252 if (i.log2_scale_factor)
4253 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
4254 #endif
4255 }
4256 else
4257 {
4258 i.sib.index = i.index_reg->reg_num;
4259 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
4260 if ((i.index_reg->reg_flags & RegRex) != 0)
4261 i.rex |= REX_X;
4262 }
4263
4264 if (i.disp_operands
4265 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
4266 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
4267 i.rm.mode = 0;
4268 else
4269 i.rm.mode = mode_from_disp_size (i.types[op]);
4270 }
4271
4272 if (fake_zero_displacement)
4273 {
4274 /* Fakes a zero displacement assuming that i.types[op]
4275 holds the correct displacement size. */
4276 expressionS *exp;
4277
4278 assert (i.op[op].disps == 0);
4279 exp = &disp_expressions[i.disp_operands++];
4280 i.op[op].disps = exp;
4281 exp->X_op = O_constant;
4282 exp->X_add_number = 0;
4283 exp->X_add_symbol = (symbolS *) 0;
4284 exp->X_op_symbol = (symbolS *) 0;
4285 }
4286 }
4287
4288 /* Fill in i.rm.reg or i.rm.regmem field with register operand
4289 (if any) based on i.tm.extension_opcode. Again, we must be
4290 careful to make sure that segment/control/debug/test/MMX
4291 registers are coded into the i.rm.reg field. */
4292 if (i.reg_operands)
4293 {
4294 unsigned int op;
4295
4296 for (op = 0; op < i.operands; op++)
4297 if (i.types[op].bitfield.reg8
4298 || i.types[op].bitfield.reg16
4299 || i.types[op].bitfield.reg32
4300 || i.types[op].bitfield.reg64
4301 || i.types[op].bitfield.regmmx
4302 || i.types[op].bitfield.regxmm
4303 || i.types[op].bitfield.sreg2
4304 || i.types[op].bitfield.sreg3
4305 || i.types[op].bitfield.control
4306 || i.types[op].bitfield.debug
4307 || i.types[op].bitfield.test)
4308 break;
4309 assert (op < i.operands);
4310
4311 /* If there is an extension opcode to put here, the register
4312 number must be put into the regmem field. */
4313 if (i.tm.extension_opcode != None)
4314 {
4315 i.rm.regmem = i.op[op].regs->reg_num;
4316 if ((i.op[op].regs->reg_flags & RegRex) != 0)
4317 i.rex |= REX_B;
4318 }
4319 else
4320 {
4321 i.rm.reg = i.op[op].regs->reg_num;
4322 if ((i.op[op].regs->reg_flags & RegRex) != 0)
4323 i.rex |= REX_R;
4324 }
4325
4326 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
4327 must set it to 3 to indicate this is a register operand
4328 in the regmem field. */
4329 if (!i.mem_operands)
4330 i.rm.mode = 3;
4331 }
4332
4333 /* Fill in i.rm.reg field with extension opcode (if any). */
4334 if (i.tm.extension_opcode != None)
4335 i.rm.reg = i.tm.extension_opcode;
4336 }
4337 return default_seg;
4338 }
4339
4340 static void
4341 output_branch (void)
4342 {
4343 char *p;
4344 int code16;
4345 int prefix;
4346 relax_substateT subtype;
4347 symbolS *sym;
4348 offsetT off;
4349
4350 code16 = 0;
4351 if (flag_code == CODE_16BIT)
4352 code16 = CODE16;
4353
4354 prefix = 0;
4355 if (i.prefix[DATA_PREFIX] != 0)
4356 {
4357 prefix = 1;
4358 i.prefixes -= 1;
4359 code16 ^= CODE16;
4360 }
4361 /* Pentium4 branch hints. */
4362 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
4363 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
4364 {
4365 prefix++;
4366 i.prefixes--;
4367 }
4368 if (i.prefix[REX_PREFIX] != 0)
4369 {
4370 prefix++;
4371 i.prefixes--;
4372 }
4373
4374 if (i.prefixes != 0 && !intel_syntax)
4375 as_warn (_("skipping prefixes on this instruction"));
4376
4377 /* It's always a symbol; End frag & setup for relax.
4378 Make sure there is enough room in this frag for the largest
4379 instruction we may generate in md_convert_frag. This is 2
4380 bytes for the opcode and room for the prefix and largest
4381 displacement. */
4382 frag_grow (prefix + 2 + 4);
4383 /* Prefix and 1 opcode byte go in fr_fix. */
4384 p = frag_more (prefix + 1);
4385 if (i.prefix[DATA_PREFIX] != 0)
4386 *p++ = DATA_PREFIX_OPCODE;
4387 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
4388 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
4389 *p++ = i.prefix[SEG_PREFIX];
4390 if (i.prefix[REX_PREFIX] != 0)
4391 *p++ = i.prefix[REX_PREFIX];
4392 *p = i.tm.base_opcode;
4393
4394 if ((unsigned char) *p == JUMP_PC_RELATIVE)
4395 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
4396 else if (cpu_arch_flags.bitfield.cpui386)
4397 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
4398 else
4399 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
4400 subtype |= code16;
4401
4402 sym = i.op[0].disps->X_add_symbol;
4403 off = i.op[0].disps->X_add_number;
4404
4405 if (i.op[0].disps->X_op != O_constant
4406 && i.op[0].disps->X_op != O_symbol)
4407 {
4408 /* Handle complex expressions. */
4409 sym = make_expr_symbol (i.op[0].disps);
4410 off = 0;
4411 }
4412
4413 /* 1 possible extra opcode + 4 byte displacement go in var part.
4414 Pass reloc in fr_var. */
4415 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
4416 }
4417
4418 static void
4419 output_jump (void)
4420 {
4421 char *p;
4422 int size;
4423 fixS *fixP;
4424
4425 if (i.tm.opcode_modifier.jumpbyte)
4426 {
4427 /* This is a loop or jecxz type instruction. */
4428 size = 1;
4429 if (i.prefix[ADDR_PREFIX] != 0)
4430 {
4431 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
4432 i.prefixes -= 1;
4433 }
4434 /* Pentium4 branch hints. */
4435 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
4436 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
4437 {
4438 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
4439 i.prefixes--;
4440 }
4441 }
4442 else
4443 {
4444 int code16;
4445
4446 code16 = 0;
4447 if (flag_code == CODE_16BIT)
4448 code16 = CODE16;
4449
4450 if (i.prefix[DATA_PREFIX] != 0)
4451 {
4452 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
4453 i.prefixes -= 1;
4454 code16 ^= CODE16;
4455 }
4456
4457 size = 4;
4458 if (code16)
4459 size = 2;
4460 }
4461
4462 if (i.prefix[REX_PREFIX] != 0)
4463 {
4464 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
4465 i.prefixes -= 1;
4466 }
4467
4468 if (i.prefixes != 0 && !intel_syntax)
4469 as_warn (_("skipping prefixes on this instruction"));
4470
4471 p = frag_more (1 + size);
4472 *p++ = i.tm.base_opcode;
4473
4474 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4475 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
4476
4477 /* All jumps handled here are signed, but don't use a signed limit
4478 check for 32 and 16 bit jumps as we want to allow wrap around at
4479 4G and 64k respectively. */
4480 if (size == 1)
4481 fixP->fx_signed = 1;
4482 }
4483
4484 static void
4485 output_interseg_jump (void)
4486 {
4487 char *p;
4488 int size;
4489 int prefix;
4490 int code16;
4491
4492 code16 = 0;
4493 if (flag_code == CODE_16BIT)
4494 code16 = CODE16;
4495
4496 prefix = 0;
4497 if (i.prefix[DATA_PREFIX] != 0)
4498 {
4499 prefix = 1;
4500 i.prefixes -= 1;
4501 code16 ^= CODE16;
4502 }
4503 if (i.prefix[REX_PREFIX] != 0)
4504 {
4505 prefix++;
4506 i.prefixes -= 1;
4507 }
4508
4509 size = 4;
4510 if (code16)
4511 size = 2;
4512
4513 if (i.prefixes != 0 && !intel_syntax)
4514 as_warn (_("skipping prefixes on this instruction"));
4515
4516 /* 1 opcode; 2 segment; offset */
4517 p = frag_more (prefix + 1 + 2 + size);
4518
4519 if (i.prefix[DATA_PREFIX] != 0)
4520 *p++ = DATA_PREFIX_OPCODE;
4521
4522 if (i.prefix[REX_PREFIX] != 0)
4523 *p++ = i.prefix[REX_PREFIX];
4524
4525 *p++ = i.tm.base_opcode;
4526 if (i.op[1].imms->X_op == O_constant)
4527 {
4528 offsetT n = i.op[1].imms->X_add_number;
4529
4530 if (size == 2
4531 && !fits_in_unsigned_word (n)
4532 && !fits_in_signed_word (n))
4533 {
4534 as_bad (_("16-bit jump out of range"));
4535 return;
4536 }
4537 md_number_to_chars (p, n, size);
4538 }
4539 else
4540 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4541 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
4542 if (i.op[0].imms->X_op != O_constant)
4543 as_bad (_("can't handle non absolute segment in `%s'"),
4544 i.tm.name);
4545 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
4546 }
4547
4548 static void
4549 output_insn (void)
4550 {
4551 fragS *insn_start_frag;
4552 offsetT insn_start_off;
4553
4554 /* Tie dwarf2 debug info to the address at the start of the insn.
4555 We can't do this after the insn has been output as the current
4556 frag may have been closed off. eg. by frag_var. */
4557 dwarf2_emit_insn (0);
4558
4559 insn_start_frag = frag_now;
4560 insn_start_off = frag_now_fix ();
4561
4562 /* Output jumps. */
4563 if (i.tm.opcode_modifier.jump)
4564 output_branch ();
4565 else if (i.tm.opcode_modifier.jumpbyte
4566 || i.tm.opcode_modifier.jumpdword)
4567 output_jump ();
4568 else if (i.tm.opcode_modifier.jumpintersegment)
4569 output_interseg_jump ();
4570 else
4571 {
4572 /* Output normal instructions here. */
4573 char *p;
4574 unsigned char *q;
4575 unsigned int prefix;
4576 int opc_3b;
4577
4578 /* All opcodes on i386 have either 1 or 2 bytes. SSSE3 and
4579 SSE4 instructions have 3 bytes. We may use one more higher
4580 byte to specify a prefix the instruction requires. Exclude
4581 instructions which are in both SSE4 and ABM. */
4582 opc_3b = ((i.tm.cpu_flags.bitfield.cpussse3
4583 || i.tm.cpu_flags.bitfield.cpusse4_1
4584 || i.tm.cpu_flags.bitfield.cpusse4_2)
4585 && !i.tm.cpu_flags.bitfield.cpuabm);
4586 if (opc_3b)
4587 {
4588 if (i.tm.base_opcode & 0xff000000)
4589 {
4590 prefix = (i.tm.base_opcode >> 24) & 0xff;
4591 goto check_prefix;
4592 }
4593 }
4594 else if ((i.tm.base_opcode & 0xff0000) != 0)
4595 {
4596 prefix = (i.tm.base_opcode >> 16) & 0xff;
4597 if (i.tm.cpu_flags.bitfield.cpupadlock)
4598 {
4599 check_prefix:
4600 if (prefix != REPE_PREFIX_OPCODE
4601 || i.prefix[LOCKREP_PREFIX] != REPE_PREFIX_OPCODE)
4602 add_prefix (prefix);
4603 }
4604 else
4605 add_prefix (prefix);
4606 }
4607
4608 /* The prefix bytes. */
4609 for (q = i.prefix;
4610 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
4611 q++)
4612 {
4613 if (*q)
4614 {
4615 p = frag_more (1);
4616 md_number_to_chars (p, (valueT) *q, 1);
4617 }
4618 }
4619
4620 /* Now the opcode; be careful about word order here! */
4621 if (fits_in_unsigned_byte (i.tm.base_opcode))
4622 {
4623 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
4624 }
4625 else
4626 {
4627 if (opc_3b)
4628 {
4629 p = frag_more (3);
4630 *p++ = (i.tm.base_opcode >> 16) & 0xff;
4631 }
4632 else
4633 p = frag_more (2);
4634
4635 /* Put out high byte first: can't use md_number_to_chars! */
4636 *p++ = (i.tm.base_opcode >> 8) & 0xff;
4637 *p = i.tm.base_opcode & 0xff;
4638 }
4639
4640 /* Now the modrm byte and sib byte (if present). */
4641 if (i.tm.opcode_modifier.modrm)
4642 {
4643 p = frag_more (1);
4644 md_number_to_chars (p,
4645 (valueT) (i.rm.regmem << 0
4646 | i.rm.reg << 3
4647 | i.rm.mode << 6),
4648 1);
4649 /* If i.rm.regmem == ESP (4)
4650 && i.rm.mode != (Register mode)
4651 && not 16 bit
4652 ==> need second modrm byte. */
4653 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
4654 && i.rm.mode != 3
4655 && !(i.base_reg && i.base_reg->reg_type.bitfield.reg16))
4656 {
4657 p = frag_more (1);
4658 md_number_to_chars (p,
4659 (valueT) (i.sib.base << 0
4660 | i.sib.index << 3
4661 | i.sib.scale << 6),
4662 1);
4663 }
4664 }
4665
4666 if (i.disp_operands)
4667 output_disp (insn_start_frag, insn_start_off);
4668
4669 if (i.imm_operands)
4670 output_imm (insn_start_frag, insn_start_off);
4671 }
4672
4673 #ifdef DEBUG386
4674 if (flag_debug)
4675 {
4676 pi ("" /*line*/, &i);
4677 }
4678 #endif /* DEBUG386 */
4679 }
4680
4681 /* Return the size of the displacement operand N. */
4682
4683 static int
4684 disp_size (unsigned int n)
4685 {
4686 int size = 4;
4687 if (i.types[n].bitfield.disp64)
4688 size = 8;
4689 else if (i.types[n].bitfield.disp8)
4690 size = 1;
4691 else if (i.types[n].bitfield.disp16)
4692 size = 2;
4693 return size;
4694 }
4695
4696 /* Return the size of the immediate operand N. */
4697
4698 static int
4699 imm_size (unsigned int n)
4700 {
4701 int size = 4;
4702 if (i.types[n].bitfield.imm64)
4703 size = 8;
4704 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
4705 size = 1;
4706 else if (i.types[n].bitfield.imm16)
4707 size = 2;
4708 return size;
4709 }
4710
4711 static void
4712 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
4713 {
4714 char *p;
4715 unsigned int n;
4716
4717 for (n = 0; n < i.operands; n++)
4718 {
4719 if (operand_type_check (i.types[n], disp))
4720 {
4721 if (i.op[n].disps->X_op == O_constant)
4722 {
4723 int size = disp_size (n);
4724 offsetT val;
4725
4726 val = offset_in_range (i.op[n].disps->X_add_number,
4727 size);
4728 p = frag_more (size);
4729 md_number_to_chars (p, val, size);
4730 }
4731 else
4732 {
4733 enum bfd_reloc_code_real reloc_type;
4734 int size = disp_size (n);
4735 int sign = i.types[n].bitfield.disp32s;
4736 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
4737
4738 /* We can't have 8 bit displacement here. */
4739 assert (!i.types[n].bitfield.disp8);
4740
4741 /* The PC relative address is computed relative
4742 to the instruction boundary, so in case immediate
4743 fields follows, we need to adjust the value. */
4744 if (pcrel && i.imm_operands)
4745 {
4746 unsigned int n1;
4747 int sz = 0;
4748
4749 for (n1 = 0; n1 < i.operands; n1++)
4750 if (operand_type_check (i.types[n1], imm))
4751 {
4752 /* Only one immediate is allowed for PC
4753 relative address. */
4754 assert (sz == 0);
4755 sz = imm_size (n1);
4756 i.op[n].disps->X_add_number -= sz;
4757 }
4758 /* We should find the immediate. */
4759 assert (sz != 0);
4760 }
4761
4762 p = frag_more (size);
4763 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
4764 if (GOT_symbol
4765 && GOT_symbol == i.op[n].disps->X_add_symbol
4766 && (((reloc_type == BFD_RELOC_32
4767 || reloc_type == BFD_RELOC_X86_64_32S
4768 || (reloc_type == BFD_RELOC_64
4769 && object_64bit))
4770 && (i.op[n].disps->X_op == O_symbol
4771 || (i.op[n].disps->X_op == O_add
4772 && ((symbol_get_value_expression
4773 (i.op[n].disps->X_op_symbol)->X_op)
4774 == O_subtract))))
4775 || reloc_type == BFD_RELOC_32_PCREL))
4776 {
4777 offsetT add;
4778
4779 if (insn_start_frag == frag_now)
4780 add = (p - frag_now->fr_literal) - insn_start_off;
4781 else
4782 {
4783 fragS *fr;
4784
4785 add = insn_start_frag->fr_fix - insn_start_off;
4786 for (fr = insn_start_frag->fr_next;
4787 fr && fr != frag_now; fr = fr->fr_next)
4788 add += fr->fr_fix;
4789 add += p - frag_now->fr_literal;
4790 }
4791
4792 if (!object_64bit)
4793 {
4794 reloc_type = BFD_RELOC_386_GOTPC;
4795 i.op[n].imms->X_add_number += add;
4796 }
4797 else if (reloc_type == BFD_RELOC_64)
4798 reloc_type = BFD_RELOC_X86_64_GOTPC64;
4799 else
4800 /* Don't do the adjustment for x86-64, as there
4801 the pcrel addressing is relative to the _next_
4802 insn, and that is taken care of in other code. */
4803 reloc_type = BFD_RELOC_X86_64_GOTPC32;
4804 }
4805 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4806 i.op[n].disps, pcrel, reloc_type);
4807 }
4808 }
4809 }
4810 }
4811
4812 static void
4813 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
4814 {
4815 char *p;
4816 unsigned int n;
4817
4818 for (n = 0; n < i.operands; n++)
4819 {
4820 if (operand_type_check (i.types[n], imm))
4821 {
4822 if (i.op[n].imms->X_op == O_constant)
4823 {
4824 int size = imm_size (n);
4825 offsetT val;
4826
4827 val = offset_in_range (i.op[n].imms->X_add_number,
4828 size);
4829 p = frag_more (size);
4830 md_number_to_chars (p, val, size);
4831 }
4832 else
4833 {
4834 /* Not absolute_section.
4835 Need a 32-bit fixup (don't support 8bit
4836 non-absolute imms). Try to support other
4837 sizes ... */
4838 enum bfd_reloc_code_real reloc_type;
4839 int size = imm_size (n);
4840 int sign;
4841
4842 if (i.types[n].bitfield.imm32s
4843 && (i.suffix == QWORD_MNEM_SUFFIX
4844 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
4845 sign = 1;
4846 else
4847 sign = 0;
4848
4849 p = frag_more (size);
4850 reloc_type = reloc (size, 0, sign, i.reloc[n]);
4851
4852 /* This is tough to explain. We end up with this one if we
4853 * have operands that look like
4854 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
4855 * obtain the absolute address of the GOT, and it is strongly
4856 * preferable from a performance point of view to avoid using
4857 * a runtime relocation for this. The actual sequence of
4858 * instructions often look something like:
4859 *
4860 * call .L66
4861 * .L66:
4862 * popl %ebx
4863 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
4864 *
4865 * The call and pop essentially return the absolute address
4866 * of the label .L66 and store it in %ebx. The linker itself
4867 * will ultimately change the first operand of the addl so
4868 * that %ebx points to the GOT, but to keep things simple, the
4869 * .o file must have this operand set so that it generates not
4870 * the absolute address of .L66, but the absolute address of
4871 * itself. This allows the linker itself simply treat a GOTPC
4872 * relocation as asking for a pcrel offset to the GOT to be
4873 * added in, and the addend of the relocation is stored in the
4874 * operand field for the instruction itself.
4875 *
4876 * Our job here is to fix the operand so that it would add
4877 * the correct offset so that %ebx would point to itself. The
4878 * thing that is tricky is that .-.L66 will point to the
4879 * beginning of the instruction, so we need to further modify
4880 * the operand so that it will point to itself. There are
4881 * other cases where you have something like:
4882 *
4883 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4884 *
4885 * and here no correction would be required. Internally in
4886 * the assembler we treat operands of this form as not being
4887 * pcrel since the '.' is explicitly mentioned, and I wonder
4888 * whether it would simplify matters to do it this way. Who
4889 * knows. In earlier versions of the PIC patches, the
4890 * pcrel_adjust field was used to store the correction, but
4891 * since the expression is not pcrel, I felt it would be
4892 * confusing to do it this way. */
4893
4894 if ((reloc_type == BFD_RELOC_32
4895 || reloc_type == BFD_RELOC_X86_64_32S
4896 || reloc_type == BFD_RELOC_64)
4897 && GOT_symbol
4898 && GOT_symbol == i.op[n].imms->X_add_symbol
4899 && (i.op[n].imms->X_op == O_symbol
4900 || (i.op[n].imms->X_op == O_add
4901 && ((symbol_get_value_expression
4902 (i.op[n].imms->X_op_symbol)->X_op)
4903 == O_subtract))))
4904 {
4905 offsetT add;
4906
4907 if (insn_start_frag == frag_now)
4908 add = (p - frag_now->fr_literal) - insn_start_off;
4909 else
4910 {
4911 fragS *fr;
4912
4913 add = insn_start_frag->fr_fix - insn_start_off;
4914 for (fr = insn_start_frag->fr_next;
4915 fr && fr != frag_now; fr = fr->fr_next)
4916 add += fr->fr_fix;
4917 add += p - frag_now->fr_literal;
4918 }
4919
4920 if (!object_64bit)
4921 reloc_type = BFD_RELOC_386_GOTPC;
4922 else if (size == 4)
4923 reloc_type = BFD_RELOC_X86_64_GOTPC32;
4924 else if (size == 8)
4925 reloc_type = BFD_RELOC_X86_64_GOTPC64;
4926 i.op[n].imms->X_add_number += add;
4927 }
4928 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4929 i.op[n].imms, 0, reloc_type);
4930 }
4931 }
4932 }
4933 }
4934 \f
4935 /* x86_cons_fix_new is called via the expression parsing code when a
4936 reloc is needed. We use this hook to get the correct .got reloc. */
4937 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
4938 static int cons_sign = -1;
4939
4940 void
4941 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
4942 expressionS *exp)
4943 {
4944 enum bfd_reloc_code_real r = reloc (len, 0, cons_sign, got_reloc);
4945
4946 got_reloc = NO_RELOC;
4947
4948 #ifdef TE_PE
4949 if (exp->X_op == O_secrel)
4950 {
4951 exp->X_op = O_symbol;
4952 r = BFD_RELOC_32_SECREL;
4953 }
4954 #endif
4955
4956 fix_new_exp (frag, off, len, exp, 0, r);
4957 }
4958
4959 #if (!defined (OBJ_ELF) && !defined (OBJ_MAYBE_ELF)) || defined (LEX_AT)
4960 # define lex_got(reloc, adjust, types) NULL
4961 #else
4962 /* Parse operands of the form
4963 <symbol>@GOTOFF+<nnn>
4964 and similar .plt or .got references.
4965
4966 If we find one, set up the correct relocation in RELOC and copy the
4967 input string, minus the `@GOTOFF' into a malloc'd buffer for
4968 parsing by the calling routine. Return this buffer, and if ADJUST
4969 is non-null set it to the length of the string we removed from the
4970 input line. Otherwise return NULL. */
4971 static char *
4972 lex_got (enum bfd_reloc_code_real *reloc,
4973 int *adjust,
4974 i386_operand_type *types)
4975 {
4976 /* Some of the relocations depend on the size of what field is to
4977 be relocated. But in our callers i386_immediate and i386_displacement
4978 we don't yet know the operand size (this will be set by insn
4979 matching). Hence we record the word32 relocation here,
4980 and adjust the reloc according to the real size in reloc(). */
4981 static const struct {
4982 const char *str;
4983 const enum bfd_reloc_code_real rel[2];
4984 const i386_operand_type types64;
4985 } gotrel[] = {
4986 { "PLTOFF", { 0,
4987 BFD_RELOC_X86_64_PLTOFF64 },
4988 OPERAND_TYPE_IMM64 },
4989 { "PLT", { BFD_RELOC_386_PLT32,
4990 BFD_RELOC_X86_64_PLT32 },
4991 OPERAND_TYPE_IMM32_32S_DISP32 },
4992 { "GOTPLT", { 0,
4993 BFD_RELOC_X86_64_GOTPLT64 },
4994 OPERAND_TYPE_IMM64_DISP64 },
4995 { "GOTOFF", { BFD_RELOC_386_GOTOFF,
4996 BFD_RELOC_X86_64_GOTOFF64 },
4997 OPERAND_TYPE_IMM64_DISP64 },
4998 { "GOTPCREL", { 0,
4999 BFD_RELOC_X86_64_GOTPCREL },
5000 OPERAND_TYPE_IMM32_32S_DISP32 },
5001 { "TLSGD", { BFD_RELOC_386_TLS_GD,
5002 BFD_RELOC_X86_64_TLSGD },
5003 OPERAND_TYPE_IMM32_32S_DISP32 },
5004 { "TLSLDM", { BFD_RELOC_386_TLS_LDM,
5005 0 },
5006 OPERAND_TYPE_NONE },
5007 { "TLSLD", { 0,
5008 BFD_RELOC_X86_64_TLSLD },
5009 OPERAND_TYPE_IMM32_32S_DISP32 },
5010 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32,
5011 BFD_RELOC_X86_64_GOTTPOFF },
5012 OPERAND_TYPE_IMM32_32S_DISP32 },
5013 { "TPOFF", { BFD_RELOC_386_TLS_LE_32,
5014 BFD_RELOC_X86_64_TPOFF32 },
5015 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
5016 { "NTPOFF", { BFD_RELOC_386_TLS_LE,
5017 0 },
5018 OPERAND_TYPE_NONE },
5019 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32,
5020 BFD_RELOC_X86_64_DTPOFF32 },
5021
5022 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
5023 { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE,
5024 0 },
5025 OPERAND_TYPE_NONE },
5026 { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE,
5027 0 },
5028 OPERAND_TYPE_NONE },
5029 { "GOT", { BFD_RELOC_386_GOT32,
5030 BFD_RELOC_X86_64_GOT32 },
5031 OPERAND_TYPE_IMM32_32S_64_DISP32 },
5032 { "TLSDESC", { BFD_RELOC_386_TLS_GOTDESC,
5033 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5034 OPERAND_TYPE_IMM32_32S_DISP32 },
5035 { "TLSCALL", { BFD_RELOC_386_TLS_DESC_CALL,
5036 BFD_RELOC_X86_64_TLSDESC_CALL },
5037 OPERAND_TYPE_IMM32_32S_DISP32 },
5038 };
5039 char *cp;
5040 unsigned int j;
5041
5042 if (!IS_ELF)
5043 return NULL;
5044
5045 for (cp = input_line_pointer; *cp != '@'; cp++)
5046 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
5047 return NULL;
5048
5049 for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
5050 {
5051 int len;
5052
5053 len = strlen (gotrel[j].str);
5054 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
5055 {
5056 if (gotrel[j].rel[object_64bit] != 0)
5057 {
5058 int first, second;
5059 char *tmpbuf, *past_reloc;
5060
5061 *reloc = gotrel[j].rel[object_64bit];
5062 if (adjust)
5063 *adjust = len;
5064
5065 if (types)
5066 {
5067 if (flag_code != CODE_64BIT)
5068 {
5069 types->bitfield.imm32 = 1;
5070 types->bitfield.disp32 = 1;
5071 }
5072 else
5073 *types = gotrel[j].types64;
5074 }
5075
5076 if (GOT_symbol == NULL)
5077 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
5078
5079 /* The length of the first part of our input line. */
5080 first = cp - input_line_pointer;
5081
5082 /* The second part goes from after the reloc token until
5083 (and including) an end_of_line char or comma. */
5084 past_reloc = cp + 1 + len;
5085 cp = past_reloc;
5086 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
5087 ++cp;
5088 second = cp + 1 - past_reloc;
5089
5090 /* Allocate and copy string. The trailing NUL shouldn't
5091 be necessary, but be safe. */
5092 tmpbuf = xmalloc (first + second + 2);
5093 memcpy (tmpbuf, input_line_pointer, first);
5094 if (second != 0 && *past_reloc != ' ')
5095 /* Replace the relocation token with ' ', so that
5096 errors like foo@GOTOFF1 will be detected. */
5097 tmpbuf[first++] = ' ';
5098 memcpy (tmpbuf + first, past_reloc, second);
5099 tmpbuf[first + second] = '\0';
5100 return tmpbuf;
5101 }
5102
5103 as_bad (_("@%s reloc is not supported with %d-bit output format"),
5104 gotrel[j].str, 1 << (5 + object_64bit));
5105 return NULL;
5106 }
5107 }
5108
5109 /* Might be a symbol version string. Don't as_bad here. */
5110 return NULL;
5111 }
5112
5113 void
5114 x86_cons (expressionS *exp, int size)
5115 {
5116 if (size == 4 || (object_64bit && size == 8))
5117 {
5118 /* Handle @GOTOFF and the like in an expression. */
5119 char *save;
5120 char *gotfree_input_line;
5121 int adjust;
5122
5123 save = input_line_pointer;
5124 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
5125 if (gotfree_input_line)
5126 input_line_pointer = gotfree_input_line;
5127
5128 expression (exp);
5129
5130 if (gotfree_input_line)
5131 {
5132 /* expression () has merrily parsed up to the end of line,
5133 or a comma - in the wrong buffer. Transfer how far
5134 input_line_pointer has moved to the right buffer. */
5135 input_line_pointer = (save
5136 + (input_line_pointer - gotfree_input_line)
5137 + adjust);
5138 free (gotfree_input_line);
5139 if (exp->X_op == O_constant
5140 || exp->X_op == O_absent
5141 || exp->X_op == O_illegal
5142 || exp->X_op == O_register
5143 || exp->X_op == O_big)
5144 {
5145 char c = *input_line_pointer;
5146 *input_line_pointer = 0;
5147 as_bad (_("missing or invalid expression `%s'"), save);
5148 *input_line_pointer = c;
5149 }
5150 }
5151 }
5152 else
5153 expression (exp);
5154 }
5155 #endif
5156
5157 static void signed_cons (int size)
5158 {
5159 if (flag_code == CODE_64BIT)
5160 cons_sign = 1;
5161 cons (size);
5162 cons_sign = -1;
5163 }
5164
5165 #ifdef TE_PE
5166 static void
5167 pe_directive_secrel (dummy)
5168 int dummy ATTRIBUTE_UNUSED;
5169 {
5170 expressionS exp;
5171
5172 do
5173 {
5174 expression (&exp);
5175 if (exp.X_op == O_symbol)
5176 exp.X_op = O_secrel;
5177
5178 emit_expr (&exp, 4);
5179 }
5180 while (*input_line_pointer++ == ',');
5181
5182 input_line_pointer--;
5183 demand_empty_rest_of_line ();
5184 }
5185 #endif
5186
5187 static int
5188 i386_immediate (char *imm_start)
5189 {
5190 char *save_input_line_pointer;
5191 char *gotfree_input_line;
5192 segT exp_seg = 0;
5193 expressionS *exp;
5194 i386_operand_type types;
5195
5196 UINTS_SET (types, ~0);
5197
5198 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
5199 {
5200 as_bad (_("at most %d immediate operands are allowed"),
5201 MAX_IMMEDIATE_OPERANDS);
5202 return 0;
5203 }
5204
5205 exp = &im_expressions[i.imm_operands++];
5206 i.op[this_operand].imms = exp;
5207
5208 if (is_space_char (*imm_start))
5209 ++imm_start;
5210
5211 save_input_line_pointer = input_line_pointer;
5212 input_line_pointer = imm_start;
5213
5214 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
5215 if (gotfree_input_line)
5216 input_line_pointer = gotfree_input_line;
5217
5218 exp_seg = expression (exp);
5219
5220 SKIP_WHITESPACE ();
5221 if (*input_line_pointer)
5222 as_bad (_("junk `%s' after expression"), input_line_pointer);
5223
5224 input_line_pointer = save_input_line_pointer;
5225 if (gotfree_input_line)
5226 free (gotfree_input_line);
5227
5228 if (exp->X_op == O_absent
5229 || exp->X_op == O_illegal
5230 || exp->X_op == O_big
5231 || (gotfree_input_line
5232 && (exp->X_op == O_constant
5233 || exp->X_op == O_register)))
5234 {
5235 as_bad (_("missing or invalid immediate expression `%s'"),
5236 imm_start);
5237 return 0;
5238 }
5239 else if (exp->X_op == O_constant)
5240 {
5241 /* Size it properly later. */
5242 i.types[this_operand].bitfield.imm64 = 1;
5243 /* If BFD64, sign extend val. */
5244 if (!use_rela_relocations
5245 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
5246 exp->X_add_number
5247 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
5248 }
5249 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5250 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
5251 && exp_seg != absolute_section
5252 && exp_seg != text_section
5253 && exp_seg != data_section
5254 && exp_seg != bss_section
5255 && exp_seg != undefined_section
5256 && !bfd_is_com_section (exp_seg))
5257 {
5258 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
5259 return 0;
5260 }
5261 #endif
5262 else if (!intel_syntax && exp->X_op == O_register)
5263 {
5264 as_bad (_("illegal immediate register operand %s"), imm_start);
5265 return 0;
5266 }
5267 else
5268 {
5269 /* This is an address. The size of the address will be
5270 determined later, depending on destination register,
5271 suffix, or the default for the section. */
5272 i.types[this_operand].bitfield.imm8 = 1;
5273 i.types[this_operand].bitfield.imm16 = 1;
5274 i.types[this_operand].bitfield.imm32 = 1;
5275 i.types[this_operand].bitfield.imm32s = 1;
5276 i.types[this_operand].bitfield.imm64 = 1;
5277 i.types[this_operand] = operand_type_and (i.types[this_operand],
5278 types);
5279 }
5280
5281 return 1;
5282 }
5283
5284 static char *
5285 i386_scale (char *scale)
5286 {
5287 offsetT val;
5288 char *save = input_line_pointer;
5289
5290 input_line_pointer = scale;
5291 val = get_absolute_expression ();
5292
5293 switch (val)
5294 {
5295 case 1:
5296 i.log2_scale_factor = 0;
5297 break;
5298 case 2:
5299 i.log2_scale_factor = 1;
5300 break;
5301 case 4:
5302 i.log2_scale_factor = 2;
5303 break;
5304 case 8:
5305 i.log2_scale_factor = 3;
5306 break;
5307 default:
5308 {
5309 char sep = *input_line_pointer;
5310
5311 *input_line_pointer = '\0';
5312 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
5313 scale);
5314 *input_line_pointer = sep;
5315 input_line_pointer = save;
5316 return NULL;
5317 }
5318 }
5319 if (i.log2_scale_factor != 0 && i.index_reg == 0)
5320 {
5321 as_warn (_("scale factor of %d without an index register"),
5322 1 << i.log2_scale_factor);
5323 #if SCALE1_WHEN_NO_INDEX
5324 i.log2_scale_factor = 0;
5325 #endif
5326 }
5327 scale = input_line_pointer;
5328 input_line_pointer = save;
5329 return scale;
5330 }
5331
5332 static int
5333 i386_displacement (char *disp_start, char *disp_end)
5334 {
5335 expressionS *exp;
5336 segT exp_seg = 0;
5337 char *save_input_line_pointer;
5338 char *gotfree_input_line;
5339 int override;
5340 i386_operand_type bigdisp, types = anydisp;
5341 int ret;
5342
5343 if (i.disp_operands == MAX_MEMORY_OPERANDS)
5344 {
5345 as_bad (_("at most %d displacement operands are allowed"),
5346 MAX_MEMORY_OPERANDS);
5347 return 0;
5348 }
5349
5350 UINTS_CLEAR (bigdisp);
5351 if ((i.types[this_operand].bitfield.jumpabsolute)
5352 || (!current_templates->start->opcode_modifier.jump
5353 && !current_templates->start->opcode_modifier.jumpdword))
5354 {
5355 bigdisp.bitfield.disp32 = 1;
5356 override = (i.prefix[ADDR_PREFIX] != 0);
5357 if (flag_code == CODE_64BIT)
5358 {
5359 if (!override)
5360 {
5361 bigdisp.bitfield.disp32s = 1;
5362 bigdisp.bitfield.disp64 = 1;
5363 }
5364 }
5365 else if ((flag_code == CODE_16BIT) ^ override)
5366 {
5367 bigdisp.bitfield.disp32 = 0;
5368 bigdisp.bitfield.disp16 = 1;
5369 }
5370 }
5371 else
5372 {
5373 /* For PC-relative branches, the width of the displacement
5374 is dependent upon data size, not address size. */
5375 override = (i.prefix[DATA_PREFIX] != 0);
5376 if (flag_code == CODE_64BIT)
5377 {
5378 if (override || i.suffix == WORD_MNEM_SUFFIX)
5379 bigdisp.bitfield.disp16 = 1;
5380 else
5381 {
5382 bigdisp.bitfield.disp32 = 1;
5383 bigdisp.bitfield.disp32s = 1;
5384 }
5385 }
5386 else
5387 {
5388 if (!override)
5389 override = (i.suffix == (flag_code != CODE_16BIT
5390 ? WORD_MNEM_SUFFIX
5391 : LONG_MNEM_SUFFIX));
5392 bigdisp.bitfield.disp32 = 1;
5393 if ((flag_code == CODE_16BIT) ^ override)
5394 {
5395 bigdisp.bitfield.disp32 = 0;
5396 bigdisp.bitfield.disp16 = 1;
5397 }
5398 }
5399 }
5400 i.types[this_operand] = operand_type_or (i.types[this_operand],
5401 bigdisp);
5402
5403 exp = &disp_expressions[i.disp_operands];
5404 i.op[this_operand].disps = exp;
5405 i.disp_operands++;
5406 save_input_line_pointer = input_line_pointer;
5407 input_line_pointer = disp_start;
5408 END_STRING_AND_SAVE (disp_end);
5409
5410 #ifndef GCC_ASM_O_HACK
5411 #define GCC_ASM_O_HACK 0
5412 #endif
5413 #if GCC_ASM_O_HACK
5414 END_STRING_AND_SAVE (disp_end + 1);
5415 if (i.types[this_operand].bitfield.baseIndex
5416 && displacement_string_end[-1] == '+')
5417 {
5418 /* This hack is to avoid a warning when using the "o"
5419 constraint within gcc asm statements.
5420 For instance:
5421
5422 #define _set_tssldt_desc(n,addr,limit,type) \
5423 __asm__ __volatile__ ( \
5424 "movw %w2,%0\n\t" \
5425 "movw %w1,2+%0\n\t" \
5426 "rorl $16,%1\n\t" \
5427 "movb %b1,4+%0\n\t" \
5428 "movb %4,5+%0\n\t" \
5429 "movb $0,6+%0\n\t" \
5430 "movb %h1,7+%0\n\t" \
5431 "rorl $16,%1" \
5432 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
5433
5434 This works great except that the output assembler ends
5435 up looking a bit weird if it turns out that there is
5436 no offset. You end up producing code that looks like:
5437
5438 #APP
5439 movw $235,(%eax)
5440 movw %dx,2+(%eax)
5441 rorl $16,%edx
5442 movb %dl,4+(%eax)
5443 movb $137,5+(%eax)
5444 movb $0,6+(%eax)
5445 movb %dh,7+(%eax)
5446 rorl $16,%edx
5447 #NO_APP
5448
5449 So here we provide the missing zero. */
5450
5451 *displacement_string_end = '0';
5452 }
5453 #endif
5454 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
5455 if (gotfree_input_line)
5456 input_line_pointer = gotfree_input_line;
5457
5458 exp_seg = expression (exp);
5459
5460 SKIP_WHITESPACE ();
5461 if (*input_line_pointer)
5462 as_bad (_("junk `%s' after expression"), input_line_pointer);
5463 #if GCC_ASM_O_HACK
5464 RESTORE_END_STRING (disp_end + 1);
5465 #endif
5466 input_line_pointer = save_input_line_pointer;
5467 if (gotfree_input_line)
5468 free (gotfree_input_line);
5469 ret = 1;
5470
5471 /* We do this to make sure that the section symbol is in
5472 the symbol table. We will ultimately change the relocation
5473 to be relative to the beginning of the section. */
5474 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
5475 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
5476 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
5477 {
5478 if (exp->X_op != O_symbol)
5479 goto inv_disp;
5480
5481 if (S_IS_LOCAL (exp->X_add_symbol)
5482 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
5483 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
5484 exp->X_op = O_subtract;
5485 exp->X_op_symbol = GOT_symbol;
5486 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
5487 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
5488 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
5489 i.reloc[this_operand] = BFD_RELOC_64;
5490 else
5491 i.reloc[this_operand] = BFD_RELOC_32;
5492 }
5493
5494 else if (exp->X_op == O_absent
5495 || exp->X_op == O_illegal
5496 || exp->X_op == O_big
5497 || (gotfree_input_line
5498 && (exp->X_op == O_constant
5499 || exp->X_op == O_register)))
5500 {
5501 inv_disp:
5502 as_bad (_("missing or invalid displacement expression `%s'"),
5503 disp_start);
5504 ret = 0;
5505 }
5506
5507 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5508 else if (exp->X_op != O_constant
5509 && OUTPUT_FLAVOR == bfd_target_aout_flavour
5510 && exp_seg != absolute_section
5511 && exp_seg != text_section
5512 && exp_seg != data_section
5513 && exp_seg != bss_section
5514 && exp_seg != undefined_section
5515 && !bfd_is_com_section (exp_seg))
5516 {
5517 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
5518 ret = 0;
5519 }
5520 #endif
5521
5522 RESTORE_END_STRING (disp_end);
5523
5524 /* Check if this is a displacement only operand. */
5525 bigdisp = i.types[this_operand];
5526 bigdisp.bitfield.disp8 = 0;
5527 bigdisp.bitfield.disp16 = 0;
5528 bigdisp.bitfield.disp32 = 0;
5529 bigdisp.bitfield.disp32s = 0;
5530 bigdisp.bitfield.disp64 = 0;
5531 if (UINTS_ALL_ZERO (bigdisp))
5532 i.types[this_operand] = operand_type_and (i.types[this_operand],
5533 types);
5534
5535 return ret;
5536 }
5537
5538 /* Make sure the memory operand we've been dealt is valid.
5539 Return 1 on success, 0 on a failure. */
5540
5541 static int
5542 i386_index_check (const char *operand_string)
5543 {
5544 int ok;
5545 #if INFER_ADDR_PREFIX
5546 int fudged = 0;
5547
5548 tryprefix:
5549 #endif
5550 ok = 1;
5551 if (flag_code == CODE_64BIT)
5552 {
5553 if ((i.base_reg
5554 && ((i.prefix[ADDR_PREFIX] == 0
5555 && !i.base_reg->reg_type.bitfield.reg64)
5556 || (i.prefix[ADDR_PREFIX]
5557 && !i.base_reg->reg_type.bitfield.reg32))
5558 && (i.index_reg
5559 || !UINTS_EQUAL (i.base_reg->reg_type, baseindex)))
5560 || (i.index_reg
5561 && (!i.index_reg->reg_type.bitfield.baseindex
5562 || (i.prefix[ADDR_PREFIX] == 0
5563 && !i.index_reg->reg_type.bitfield.reg64)
5564 || (i.prefix[ADDR_PREFIX]
5565 && !i.index_reg->reg_type.bitfield.reg32))))
5566 ok = 0;
5567 }
5568 else
5569 {
5570 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
5571 {
5572 /* 16bit checks. */
5573 if ((i.base_reg
5574 && (!i.base_reg->reg_type.bitfield.reg16
5575 || !i.base_reg->reg_type.bitfield.baseindex))
5576 || (i.index_reg
5577 && (!i.index_reg->reg_type.bitfield.reg16
5578 || !i.index_reg->reg_type.bitfield.baseindex
5579 || !(i.base_reg
5580 && i.base_reg->reg_num < 6
5581 && i.index_reg->reg_num >= 6
5582 && i.log2_scale_factor == 0))))
5583 ok = 0;
5584 }
5585 else
5586 {
5587 /* 32bit checks. */
5588 if ((i.base_reg
5589 && !i.base_reg->reg_type.bitfield.reg32)
5590 || (i.index_reg
5591 && (!i.index_reg->reg_type.bitfield.reg32
5592 || !i.index_reg->reg_type.bitfield.baseindex)))
5593 ok = 0;
5594 }
5595 }
5596 if (!ok)
5597 {
5598 #if INFER_ADDR_PREFIX
5599 if (i.prefix[ADDR_PREFIX] == 0)
5600 {
5601 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
5602 i.prefixes += 1;
5603 /* Change the size of any displacement too. At most one of
5604 Disp16 or Disp32 is set.
5605 FIXME. There doesn't seem to be any real need for separate
5606 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
5607 Removing them would probably clean up the code quite a lot. */
5608 if (flag_code != CODE_64BIT
5609 && (i.types[this_operand].bitfield.disp16
5610 || i.types[this_operand].bitfield.disp32))
5611 i.types[this_operand]
5612 = operand_type_xor (i.types[this_operand], disp16_32);
5613 fudged = 1;
5614 goto tryprefix;
5615 }
5616 if (fudged)
5617 as_bad (_("`%s' is not a valid base/index expression"),
5618 operand_string);
5619 else
5620 #endif
5621 as_bad (_("`%s' is not a valid %s bit base/index expression"),
5622 operand_string,
5623 flag_code_names[flag_code]);
5624 }
5625 return ok;
5626 }
5627
5628 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
5629 on error. */
5630
5631 static int
5632 i386_operand (char *operand_string)
5633 {
5634 const reg_entry *r;
5635 char *end_op;
5636 char *op_string = operand_string;
5637
5638 if (is_space_char (*op_string))
5639 ++op_string;
5640
5641 /* We check for an absolute prefix (differentiating,
5642 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
5643 if (*op_string == ABSOLUTE_PREFIX)
5644 {
5645 ++op_string;
5646 if (is_space_char (*op_string))
5647 ++op_string;
5648 i.types[this_operand].bitfield.jumpabsolute = 1;
5649 }
5650
5651 /* Check if operand is a register. */
5652 if ((r = parse_register (op_string, &end_op)) != NULL)
5653 {
5654 i386_operand_type temp;
5655
5656 /* Check for a segment override by searching for ':' after a
5657 segment register. */
5658 op_string = end_op;
5659 if (is_space_char (*op_string))
5660 ++op_string;
5661 if (*op_string == ':'
5662 && (r->reg_type.bitfield.sreg2
5663 || r->reg_type.bitfield.sreg3))
5664 {
5665 switch (r->reg_num)
5666 {
5667 case 0:
5668 i.seg[i.mem_operands] = &es;
5669 break;
5670 case 1:
5671 i.seg[i.mem_operands] = &cs;
5672 break;
5673 case 2:
5674 i.seg[i.mem_operands] = &ss;
5675 break;
5676 case 3:
5677 i.seg[i.mem_operands] = &ds;
5678 break;
5679 case 4:
5680 i.seg[i.mem_operands] = &fs;
5681 break;
5682 case 5:
5683 i.seg[i.mem_operands] = &gs;
5684 break;
5685 }
5686
5687 /* Skip the ':' and whitespace. */
5688 ++op_string;
5689 if (is_space_char (*op_string))
5690 ++op_string;
5691
5692 if (!is_digit_char (*op_string)
5693 && !is_identifier_char (*op_string)
5694 && *op_string != '('
5695 && *op_string != ABSOLUTE_PREFIX)
5696 {
5697 as_bad (_("bad memory operand `%s'"), op_string);
5698 return 0;
5699 }
5700 /* Handle case of %es:*foo. */
5701 if (*op_string == ABSOLUTE_PREFIX)
5702 {
5703 ++op_string;
5704 if (is_space_char (*op_string))
5705 ++op_string;
5706 i.types[this_operand].bitfield.jumpabsolute = 1;
5707 }
5708 goto do_memory_reference;
5709 }
5710 if (*op_string)
5711 {
5712 as_bad (_("junk `%s' after register"), op_string);
5713 return 0;
5714 }
5715 temp = r->reg_type;
5716 temp.bitfield.baseindex = 0;
5717 i.types[this_operand] = operand_type_or (i.types[this_operand],
5718 temp);
5719 i.op[this_operand].regs = r;
5720 i.reg_operands++;
5721 }
5722 else if (*op_string == REGISTER_PREFIX)
5723 {
5724 as_bad (_("bad register name `%s'"), op_string);
5725 return 0;
5726 }
5727 else if (*op_string == IMMEDIATE_PREFIX)
5728 {
5729 ++op_string;
5730 if (i.types[this_operand].bitfield.jumpabsolute)
5731 {
5732 as_bad (_("immediate operand illegal with absolute jump"));
5733 return 0;
5734 }
5735 if (!i386_immediate (op_string))
5736 return 0;
5737 }
5738 else if (is_digit_char (*op_string)
5739 || is_identifier_char (*op_string)
5740 || *op_string == '(')
5741 {
5742 /* This is a memory reference of some sort. */
5743 char *base_string;
5744
5745 /* Start and end of displacement string expression (if found). */
5746 char *displacement_string_start;
5747 char *displacement_string_end;
5748
5749 do_memory_reference:
5750 if ((i.mem_operands == 1
5751 && !current_templates->start->opcode_modifier.isstring)
5752 || i.mem_operands == 2)
5753 {
5754 as_bad (_("too many memory references for `%s'"),
5755 current_templates->start->name);
5756 return 0;
5757 }
5758
5759 /* Check for base index form. We detect the base index form by
5760 looking for an ')' at the end of the operand, searching
5761 for the '(' matching it, and finding a REGISTER_PREFIX or ','
5762 after the '('. */
5763 base_string = op_string + strlen (op_string);
5764
5765 --base_string;
5766 if (is_space_char (*base_string))
5767 --base_string;
5768
5769 /* If we only have a displacement, set-up for it to be parsed later. */
5770 displacement_string_start = op_string;
5771 displacement_string_end = base_string + 1;
5772
5773 if (*base_string == ')')
5774 {
5775 char *temp_string;
5776 unsigned int parens_balanced = 1;
5777 /* We've already checked that the number of left & right ()'s are
5778 equal, so this loop will not be infinite. */
5779 do
5780 {
5781 base_string--;
5782 if (*base_string == ')')
5783 parens_balanced++;
5784 if (*base_string == '(')
5785 parens_balanced--;
5786 }
5787 while (parens_balanced);
5788
5789 temp_string = base_string;
5790
5791 /* Skip past '(' and whitespace. */
5792 ++base_string;
5793 if (is_space_char (*base_string))
5794 ++base_string;
5795
5796 if (*base_string == ','
5797 || ((i.base_reg = parse_register (base_string, &end_op))
5798 != NULL))
5799 {
5800 displacement_string_end = temp_string;
5801
5802 i.types[this_operand].bitfield.baseindex = 1;
5803
5804 if (i.base_reg)
5805 {
5806 base_string = end_op;
5807 if (is_space_char (*base_string))
5808 ++base_string;
5809 }
5810
5811 /* There may be an index reg or scale factor here. */
5812 if (*base_string == ',')
5813 {
5814 ++base_string;
5815 if (is_space_char (*base_string))
5816 ++base_string;
5817
5818 if ((i.index_reg = parse_register (base_string, &end_op))
5819 != NULL)
5820 {
5821 base_string = end_op;
5822 if (is_space_char (*base_string))
5823 ++base_string;
5824 if (*base_string == ',')
5825 {
5826 ++base_string;
5827 if (is_space_char (*base_string))
5828 ++base_string;
5829 }
5830 else if (*base_string != ')')
5831 {
5832 as_bad (_("expecting `,' or `)' "
5833 "after index register in `%s'"),
5834 operand_string);
5835 return 0;
5836 }
5837 }
5838 else if (*base_string == REGISTER_PREFIX)
5839 {
5840 as_bad (_("bad register name `%s'"), base_string);
5841 return 0;
5842 }
5843
5844 /* Check for scale factor. */
5845 if (*base_string != ')')
5846 {
5847 char *end_scale = i386_scale (base_string);
5848
5849 if (!end_scale)
5850 return 0;
5851
5852 base_string = end_scale;
5853 if (is_space_char (*base_string))
5854 ++base_string;
5855 if (*base_string != ')')
5856 {
5857 as_bad (_("expecting `)' "
5858 "after scale factor in `%s'"),
5859 operand_string);
5860 return 0;
5861 }
5862 }
5863 else if (!i.index_reg)
5864 {
5865 as_bad (_("expecting index register or scale factor "
5866 "after `,'; got '%c'"),
5867 *base_string);
5868 return 0;
5869 }
5870 }
5871 else if (*base_string != ')')
5872 {
5873 as_bad (_("expecting `,' or `)' "
5874 "after base register in `%s'"),
5875 operand_string);
5876 return 0;
5877 }
5878 }
5879 else if (*base_string == REGISTER_PREFIX)
5880 {
5881 as_bad (_("bad register name `%s'"), base_string);
5882 return 0;
5883 }
5884 }
5885
5886 /* If there's an expression beginning the operand, parse it,
5887 assuming displacement_string_start and
5888 displacement_string_end are meaningful. */
5889 if (displacement_string_start != displacement_string_end)
5890 {
5891 if (!i386_displacement (displacement_string_start,
5892 displacement_string_end))
5893 return 0;
5894 }
5895
5896 /* Special case for (%dx) while doing input/output op. */
5897 if (i.base_reg
5898 && UINTS_EQUAL (i.base_reg->reg_type, reg16_inoutportreg)
5899 && i.index_reg == 0
5900 && i.log2_scale_factor == 0
5901 && i.seg[i.mem_operands] == 0
5902 && !operand_type_check (i.types[this_operand], disp))
5903 {
5904 UINTS_CLEAR (i.types[this_operand]);
5905 i.types[this_operand].bitfield.inoutportreg = 1;
5906 return 1;
5907 }
5908
5909 if (i386_index_check (operand_string) == 0)
5910 return 0;
5911 i.mem_operands++;
5912 }
5913 else
5914 {
5915 /* It's not a memory operand; argh! */
5916 as_bad (_("invalid char %s beginning operand %d `%s'"),
5917 output_invalid (*op_string),
5918 this_operand + 1,
5919 op_string);
5920 return 0;
5921 }
5922 return 1; /* Normal return. */
5923 }
5924 \f
5925 /* md_estimate_size_before_relax()
5926
5927 Called just before relax() for rs_machine_dependent frags. The x86
5928 assembler uses these frags to handle variable size jump
5929 instructions.
5930
5931 Any symbol that is now undefined will not become defined.
5932 Return the correct fr_subtype in the frag.
5933 Return the initial "guess for variable size of frag" to caller.
5934 The guess is actually the growth beyond the fixed part. Whatever
5935 we do to grow the fixed or variable part contributes to our
5936 returned value. */
5937
5938 int
5939 md_estimate_size_before_relax (fragP, segment)
5940 fragS *fragP;
5941 segT segment;
5942 {
5943 /* We've already got fragP->fr_subtype right; all we have to do is
5944 check for un-relaxable symbols. On an ELF system, we can't relax
5945 an externally visible symbol, because it may be overridden by a
5946 shared library. */
5947 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
5948 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5949 || (IS_ELF
5950 && (S_IS_EXTERNAL (fragP->fr_symbol)
5951 || S_IS_WEAK (fragP->fr_symbol)))
5952 #endif
5953 )
5954 {
5955 /* Symbol is undefined in this segment, or we need to keep a
5956 reloc so that weak symbols can be overridden. */
5957 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
5958 enum bfd_reloc_code_real reloc_type;
5959 unsigned char *opcode;
5960 int old_fr_fix;
5961
5962 if (fragP->fr_var != NO_RELOC)
5963 reloc_type = fragP->fr_var;
5964 else if (size == 2)
5965 reloc_type = BFD_RELOC_16_PCREL;
5966 else
5967 reloc_type = BFD_RELOC_32_PCREL;
5968
5969 old_fr_fix = fragP->fr_fix;
5970 opcode = (unsigned char *) fragP->fr_opcode;
5971
5972 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
5973 {
5974 case UNCOND_JUMP:
5975 /* Make jmp (0xeb) a (d)word displacement jump. */
5976 opcode[0] = 0xe9;
5977 fragP->fr_fix += size;
5978 fix_new (fragP, old_fr_fix, size,
5979 fragP->fr_symbol,
5980 fragP->fr_offset, 1,
5981 reloc_type);
5982 break;
5983
5984 case COND_JUMP86:
5985 if (size == 2
5986 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
5987 {
5988 /* Negate the condition, and branch past an
5989 unconditional jump. */
5990 opcode[0] ^= 1;
5991 opcode[1] = 3;
5992 /* Insert an unconditional jump. */
5993 opcode[2] = 0xe9;
5994 /* We added two extra opcode bytes, and have a two byte
5995 offset. */
5996 fragP->fr_fix += 2 + 2;
5997 fix_new (fragP, old_fr_fix + 2, 2,
5998 fragP->fr_symbol,
5999 fragP->fr_offset, 1,
6000 reloc_type);
6001 break;
6002 }
6003 /* Fall through. */
6004
6005 case COND_JUMP:
6006 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
6007 {
6008 fixS *fixP;
6009
6010 fragP->fr_fix += 1;
6011 fixP = fix_new (fragP, old_fr_fix, 1,
6012 fragP->fr_symbol,
6013 fragP->fr_offset, 1,
6014 BFD_RELOC_8_PCREL);
6015 fixP->fx_signed = 1;
6016 break;
6017 }
6018
6019 /* This changes the byte-displacement jump 0x7N
6020 to the (d)word-displacement jump 0x0f,0x8N. */
6021 opcode[1] = opcode[0] + 0x10;
6022 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
6023 /* We've added an opcode byte. */
6024 fragP->fr_fix += 1 + size;
6025 fix_new (fragP, old_fr_fix + 1, size,
6026 fragP->fr_symbol,
6027 fragP->fr_offset, 1,
6028 reloc_type);
6029 break;
6030
6031 default:
6032 BAD_CASE (fragP->fr_subtype);
6033 break;
6034 }
6035 frag_wane (fragP);
6036 return fragP->fr_fix - old_fr_fix;
6037 }
6038
6039 /* Guess size depending on current relax state. Initially the relax
6040 state will correspond to a short jump and we return 1, because
6041 the variable part of the frag (the branch offset) is one byte
6042 long. However, we can relax a section more than once and in that
6043 case we must either set fr_subtype back to the unrelaxed state,
6044 or return the value for the appropriate branch. */
6045 return md_relax_table[fragP->fr_subtype].rlx_length;
6046 }
6047
6048 /* Called after relax() is finished.
6049
6050 In: Address of frag.
6051 fr_type == rs_machine_dependent.
6052 fr_subtype is what the address relaxed to.
6053
6054 Out: Any fixSs and constants are set up.
6055 Caller will turn frag into a ".space 0". */
6056
6057 void
6058 md_convert_frag (abfd, sec, fragP)
6059 bfd *abfd ATTRIBUTE_UNUSED;
6060 segT sec ATTRIBUTE_UNUSED;
6061 fragS *fragP;
6062 {
6063 unsigned char *opcode;
6064 unsigned char *where_to_put_displacement = NULL;
6065 offsetT target_address;
6066 offsetT opcode_address;
6067 unsigned int extension = 0;
6068 offsetT displacement_from_opcode_start;
6069
6070 opcode = (unsigned char *) fragP->fr_opcode;
6071
6072 /* Address we want to reach in file space. */
6073 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
6074
6075 /* Address opcode resides at in file space. */
6076 opcode_address = fragP->fr_address + fragP->fr_fix;
6077
6078 /* Displacement from opcode start to fill into instruction. */
6079 displacement_from_opcode_start = target_address - opcode_address;
6080
6081 if ((fragP->fr_subtype & BIG) == 0)
6082 {
6083 /* Don't have to change opcode. */
6084 extension = 1; /* 1 opcode + 1 displacement */
6085 where_to_put_displacement = &opcode[1];
6086 }
6087 else
6088 {
6089 if (no_cond_jump_promotion
6090 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
6091 as_warn_where (fragP->fr_file, fragP->fr_line,
6092 _("long jump required"));
6093
6094 switch (fragP->fr_subtype)
6095 {
6096 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
6097 extension = 4; /* 1 opcode + 4 displacement */
6098 opcode[0] = 0xe9;
6099 where_to_put_displacement = &opcode[1];
6100 break;
6101
6102 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
6103 extension = 2; /* 1 opcode + 2 displacement */
6104 opcode[0] = 0xe9;
6105 where_to_put_displacement = &opcode[1];
6106 break;
6107
6108 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
6109 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
6110 extension = 5; /* 2 opcode + 4 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_JUMP, BIG16):
6117 extension = 3; /* 2 opcode + 2 displacement */
6118 opcode[1] = opcode[0] + 0x10;
6119 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
6120 where_to_put_displacement = &opcode[2];
6121 break;
6122
6123 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
6124 extension = 4;
6125 opcode[0] ^= 1;
6126 opcode[1] = 3;
6127 opcode[2] = 0xe9;
6128 where_to_put_displacement = &opcode[3];
6129 break;
6130
6131 default:
6132 BAD_CASE (fragP->fr_subtype);
6133 break;
6134 }
6135 }
6136
6137 /* If size if less then four we are sure that the operand fits,
6138 but if it's 4, then it could be that the displacement is larger
6139 then -/+ 2GB. */
6140 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
6141 && object_64bit
6142 && ((addressT) (displacement_from_opcode_start - extension
6143 + ((addressT) 1 << 31))
6144 > (((addressT) 2 << 31) - 1)))
6145 {
6146 as_bad_where (fragP->fr_file, fragP->fr_line,
6147 _("jump target out of range"));
6148 /* Make us emit 0. */
6149 displacement_from_opcode_start = extension;
6150 }
6151 /* Now put displacement after opcode. */
6152 md_number_to_chars ((char *) where_to_put_displacement,
6153 (valueT) (displacement_from_opcode_start - extension),
6154 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
6155 fragP->fr_fix += extension;
6156 }
6157 \f
6158 /* Size of byte displacement jmp. */
6159 int md_short_jump_size = 2;
6160
6161 /* Size of dword displacement jmp. */
6162 int md_long_jump_size = 5;
6163
6164 void
6165 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
6166 char *ptr;
6167 addressT from_addr, to_addr;
6168 fragS *frag ATTRIBUTE_UNUSED;
6169 symbolS *to_symbol ATTRIBUTE_UNUSED;
6170 {
6171 offsetT offset;
6172
6173 offset = to_addr - (from_addr + 2);
6174 /* Opcode for byte-disp jump. */
6175 md_number_to_chars (ptr, (valueT) 0xeb, 1);
6176 md_number_to_chars (ptr + 1, (valueT) offset, 1);
6177 }
6178
6179 void
6180 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
6181 char *ptr;
6182 addressT from_addr, to_addr;
6183 fragS *frag ATTRIBUTE_UNUSED;
6184 symbolS *to_symbol ATTRIBUTE_UNUSED;
6185 {
6186 offsetT offset;
6187
6188 offset = to_addr - (from_addr + 5);
6189 md_number_to_chars (ptr, (valueT) 0xe9, 1);
6190 md_number_to_chars (ptr + 1, (valueT) offset, 4);
6191 }
6192 \f
6193 /* Apply a fixup (fixS) to segment data, once it has been determined
6194 by our caller that we have all the info we need to fix it up.
6195
6196 On the 386, immediates, displacements, and data pointers are all in
6197 the same (little-endian) format, so we don't need to care about which
6198 we are handling. */
6199
6200 void
6201 md_apply_fix (fixP, valP, seg)
6202 /* The fix we're to put in. */
6203 fixS *fixP;
6204 /* Pointer to the value of the bits. */
6205 valueT *valP;
6206 /* Segment fix is from. */
6207 segT seg ATTRIBUTE_UNUSED;
6208 {
6209 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
6210 valueT value = *valP;
6211
6212 #if !defined (TE_Mach)
6213 if (fixP->fx_pcrel)
6214 {
6215 switch (fixP->fx_r_type)
6216 {
6217 default:
6218 break;
6219
6220 case BFD_RELOC_64:
6221 fixP->fx_r_type = BFD_RELOC_64_PCREL;
6222 break;
6223 case BFD_RELOC_32:
6224 case BFD_RELOC_X86_64_32S:
6225 fixP->fx_r_type = BFD_RELOC_32_PCREL;
6226 break;
6227 case BFD_RELOC_16:
6228 fixP->fx_r_type = BFD_RELOC_16_PCREL;
6229 break;
6230 case BFD_RELOC_8:
6231 fixP->fx_r_type = BFD_RELOC_8_PCREL;
6232 break;
6233 }
6234 }
6235
6236 if (fixP->fx_addsy != NULL
6237 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
6238 || fixP->fx_r_type == BFD_RELOC_64_PCREL
6239 || fixP->fx_r_type == BFD_RELOC_16_PCREL
6240 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
6241 && !use_rela_relocations)
6242 {
6243 /* This is a hack. There should be a better way to handle this.
6244 This covers for the fact that bfd_install_relocation will
6245 subtract the current location (for partial_inplace, PC relative
6246 relocations); see more below. */
6247 #ifndef OBJ_AOUT
6248 if (IS_ELF
6249 #ifdef TE_PE
6250 || OUTPUT_FLAVOR == bfd_target_coff_flavour
6251 #endif
6252 )
6253 value += fixP->fx_where + fixP->fx_frag->fr_address;
6254 #endif
6255 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6256 if (IS_ELF)
6257 {
6258 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
6259
6260 if ((sym_seg == seg
6261 || (symbol_section_p (fixP->fx_addsy)
6262 && sym_seg != absolute_section))
6263 && !generic_force_reloc (fixP))
6264 {
6265 /* Yes, we add the values in twice. This is because
6266 bfd_install_relocation subtracts them out again. I think
6267 bfd_install_relocation is broken, but I don't dare change
6268 it. FIXME. */
6269 value += fixP->fx_where + fixP->fx_frag->fr_address;
6270 }
6271 }
6272 #endif
6273 #if defined (OBJ_COFF) && defined (TE_PE)
6274 /* For some reason, the PE format does not store a
6275 section address offset for a PC relative symbol. */
6276 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
6277 || S_IS_WEAK (fixP->fx_addsy))
6278 value += md_pcrel_from (fixP);
6279 #endif
6280 }
6281
6282 /* Fix a few things - the dynamic linker expects certain values here,
6283 and we must not disappoint it. */
6284 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6285 if (IS_ELF && fixP->fx_addsy)
6286 switch (fixP->fx_r_type)
6287 {
6288 case BFD_RELOC_386_PLT32:
6289 case BFD_RELOC_X86_64_PLT32:
6290 /* Make the jump instruction point to the address of the operand. At
6291 runtime we merely add the offset to the actual PLT entry. */
6292 value = -4;
6293 break;
6294
6295 case BFD_RELOC_386_TLS_GD:
6296 case BFD_RELOC_386_TLS_LDM:
6297 case BFD_RELOC_386_TLS_IE_32:
6298 case BFD_RELOC_386_TLS_IE:
6299 case BFD_RELOC_386_TLS_GOTIE:
6300 case BFD_RELOC_386_TLS_GOTDESC:
6301 case BFD_RELOC_X86_64_TLSGD:
6302 case BFD_RELOC_X86_64_TLSLD:
6303 case BFD_RELOC_X86_64_GOTTPOFF:
6304 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
6305 value = 0; /* Fully resolved at runtime. No addend. */
6306 /* Fallthrough */
6307 case BFD_RELOC_386_TLS_LE:
6308 case BFD_RELOC_386_TLS_LDO_32:
6309 case BFD_RELOC_386_TLS_LE_32:
6310 case BFD_RELOC_X86_64_DTPOFF32:
6311 case BFD_RELOC_X86_64_DTPOFF64:
6312 case BFD_RELOC_X86_64_TPOFF32:
6313 case BFD_RELOC_X86_64_TPOFF64:
6314 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6315 break;
6316
6317 case BFD_RELOC_386_TLS_DESC_CALL:
6318 case BFD_RELOC_X86_64_TLSDESC_CALL:
6319 value = 0; /* Fully resolved at runtime. No addend. */
6320 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6321 fixP->fx_done = 0;
6322 return;
6323
6324 case BFD_RELOC_386_GOT32:
6325 case BFD_RELOC_X86_64_GOT32:
6326 value = 0; /* Fully resolved at runtime. No addend. */
6327 break;
6328
6329 case BFD_RELOC_VTABLE_INHERIT:
6330 case BFD_RELOC_VTABLE_ENTRY:
6331 fixP->fx_done = 0;
6332 return;
6333
6334 default:
6335 break;
6336 }
6337 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
6338 *valP = value;
6339 #endif /* !defined (TE_Mach) */
6340
6341 /* Are we finished with this relocation now? */
6342 if (fixP->fx_addsy == NULL)
6343 fixP->fx_done = 1;
6344 else if (use_rela_relocations)
6345 {
6346 fixP->fx_no_overflow = 1;
6347 /* Remember value for tc_gen_reloc. */
6348 fixP->fx_addnumber = value;
6349 value = 0;
6350 }
6351
6352 md_number_to_chars (p, value, fixP->fx_size);
6353 }
6354 \f
6355 #define MAX_LITTLENUMS 6
6356
6357 /* Turn the string pointed to by litP into a floating point constant
6358 of type TYPE, and emit the appropriate bytes. The number of
6359 LITTLENUMS emitted is stored in *SIZEP. An error message is
6360 returned, or NULL on OK. */
6361
6362 char *
6363 md_atof (type, litP, sizeP)
6364 int type;
6365 char *litP;
6366 int *sizeP;
6367 {
6368 int prec;
6369 LITTLENUM_TYPE words[MAX_LITTLENUMS];
6370 LITTLENUM_TYPE *wordP;
6371 char *t;
6372
6373 switch (type)
6374 {
6375 case 'f':
6376 case 'F':
6377 prec = 2;
6378 break;
6379
6380 case 'd':
6381 case 'D':
6382 prec = 4;
6383 break;
6384
6385 case 'x':
6386 case 'X':
6387 prec = 5;
6388 break;
6389
6390 default:
6391 *sizeP = 0;
6392 return _("Bad call to md_atof ()");
6393 }
6394 t = atof_ieee (input_line_pointer, type, words);
6395 if (t)
6396 input_line_pointer = t;
6397
6398 *sizeP = prec * sizeof (LITTLENUM_TYPE);
6399 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
6400 the bigendian 386. */
6401 for (wordP = words + prec - 1; prec--;)
6402 {
6403 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
6404 litP += sizeof (LITTLENUM_TYPE);
6405 }
6406 return 0;
6407 }
6408 \f
6409 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
6410
6411 static char *
6412 output_invalid (int c)
6413 {
6414 if (ISPRINT (c))
6415 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
6416 "'%c'", c);
6417 else
6418 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
6419 "(0x%x)", (unsigned char) c);
6420 return output_invalid_buf;
6421 }
6422
6423 /* REG_STRING starts *before* REGISTER_PREFIX. */
6424
6425 static const reg_entry *
6426 parse_real_register (char *reg_string, char **end_op)
6427 {
6428 char *s = reg_string;
6429 char *p;
6430 char reg_name_given[MAX_REG_NAME_SIZE + 1];
6431 const reg_entry *r;
6432
6433 /* Skip possible REGISTER_PREFIX and possible whitespace. */
6434 if (*s == REGISTER_PREFIX)
6435 ++s;
6436
6437 if (is_space_char (*s))
6438 ++s;
6439
6440 p = reg_name_given;
6441 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
6442 {
6443 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
6444 return (const reg_entry *) NULL;
6445 s++;
6446 }
6447
6448 /* For naked regs, make sure that we are not dealing with an identifier.
6449 This prevents confusing an identifier like `eax_var' with register
6450 `eax'. */
6451 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
6452 return (const reg_entry *) NULL;
6453
6454 *end_op = s;
6455
6456 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
6457
6458 /* Handle floating point regs, allowing spaces in the (i) part. */
6459 if (r == i386_regtab /* %st is first entry of table */)
6460 {
6461 if (is_space_char (*s))
6462 ++s;
6463 if (*s == '(')
6464 {
6465 ++s;
6466 if (is_space_char (*s))
6467 ++s;
6468 if (*s >= '0' && *s <= '7')
6469 {
6470 int fpr = *s - '0';
6471 ++s;
6472 if (is_space_char (*s))
6473 ++s;
6474 if (*s == ')')
6475 {
6476 *end_op = s + 1;
6477 r = hash_find (reg_hash, "st(0)");
6478 know (r);
6479 return r + fpr;
6480 }
6481 }
6482 /* We have "%st(" then garbage. */
6483 return (const reg_entry *) NULL;
6484 }
6485 }
6486
6487 if (r != NULL
6488 && ((r->reg_flags & (RegRex64 | RegRex))
6489 || r->reg_type.bitfield.reg64)
6490 && (!cpu_arch_flags.bitfield.cpulm
6491 || !UINTS_EQUAL (r->reg_type, control))
6492 && flag_code != CODE_64BIT)
6493 return (const reg_entry *) NULL;
6494
6495 return r;
6496 }
6497
6498 /* REG_STRING starts *before* REGISTER_PREFIX. */
6499
6500 static const reg_entry *
6501 parse_register (char *reg_string, char **end_op)
6502 {
6503 const reg_entry *r;
6504
6505 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
6506 r = parse_real_register (reg_string, end_op);
6507 else
6508 r = NULL;
6509 if (!r)
6510 {
6511 char *save = input_line_pointer;
6512 char c;
6513 symbolS *symbolP;
6514
6515 input_line_pointer = reg_string;
6516 c = get_symbol_end ();
6517 symbolP = symbol_find (reg_string);
6518 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
6519 {
6520 const expressionS *e = symbol_get_value_expression (symbolP);
6521
6522 know (e->X_op == O_register);
6523 know (e->X_add_number >= 0
6524 && (valueT) e->X_add_number < i386_regtab_size);
6525 r = i386_regtab + e->X_add_number;
6526 *end_op = input_line_pointer;
6527 }
6528 *input_line_pointer = c;
6529 input_line_pointer = save;
6530 }
6531 return r;
6532 }
6533
6534 int
6535 i386_parse_name (char *name, expressionS *e, char *nextcharP)
6536 {
6537 const reg_entry *r;
6538 char *end = input_line_pointer;
6539
6540 *end = *nextcharP;
6541 r = parse_register (name, &input_line_pointer);
6542 if (r && end <= input_line_pointer)
6543 {
6544 *nextcharP = *input_line_pointer;
6545 *input_line_pointer = 0;
6546 e->X_op = O_register;
6547 e->X_add_number = r - i386_regtab;
6548 return 1;
6549 }
6550 input_line_pointer = end;
6551 *end = 0;
6552 return 0;
6553 }
6554
6555 void
6556 md_operand (expressionS *e)
6557 {
6558 if (*input_line_pointer == REGISTER_PREFIX)
6559 {
6560 char *end;
6561 const reg_entry *r = parse_real_register (input_line_pointer, &end);
6562
6563 if (r)
6564 {
6565 e->X_op = O_register;
6566 e->X_add_number = r - i386_regtab;
6567 input_line_pointer = end;
6568 }
6569 }
6570 }
6571
6572 \f
6573 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6574 const char *md_shortopts = "kVQ:sqn";
6575 #else
6576 const char *md_shortopts = "qn";
6577 #endif
6578
6579 #define OPTION_32 (OPTION_MD_BASE + 0)
6580 #define OPTION_64 (OPTION_MD_BASE + 1)
6581 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
6582 #define OPTION_MARCH (OPTION_MD_BASE + 3)
6583 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
6584
6585 struct option md_longopts[] =
6586 {
6587 {"32", no_argument, NULL, OPTION_32},
6588 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6589 {"64", no_argument, NULL, OPTION_64},
6590 #endif
6591 {"divide", no_argument, NULL, OPTION_DIVIDE},
6592 {"march", required_argument, NULL, OPTION_MARCH},
6593 {"mtune", required_argument, NULL, OPTION_MTUNE},
6594 {NULL, no_argument, NULL, 0}
6595 };
6596 size_t md_longopts_size = sizeof (md_longopts);
6597
6598 int
6599 md_parse_option (int c, char *arg)
6600 {
6601 unsigned int i;
6602
6603 switch (c)
6604 {
6605 case 'n':
6606 optimize_align_code = 0;
6607 break;
6608
6609 case 'q':
6610 quiet_warnings = 1;
6611 break;
6612
6613 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6614 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
6615 should be emitted or not. FIXME: Not implemented. */
6616 case 'Q':
6617 break;
6618
6619 /* -V: SVR4 argument to print version ID. */
6620 case 'V':
6621 print_version_id ();
6622 break;
6623
6624 /* -k: Ignore for FreeBSD compatibility. */
6625 case 'k':
6626 break;
6627
6628 case 's':
6629 /* -s: On i386 Solaris, this tells the native assembler to use
6630 .stab instead of .stab.excl. We always use .stab anyhow. */
6631 break;
6632 #endif
6633 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6634 case OPTION_64:
6635 {
6636 const char **list, **l;
6637
6638 list = bfd_target_list ();
6639 for (l = list; *l != NULL; l++)
6640 if (CONST_STRNEQ (*l, "elf64-x86-64")
6641 || strcmp (*l, "coff-x86-64") == 0
6642 || strcmp (*l, "pe-x86-64") == 0
6643 || strcmp (*l, "pei-x86-64") == 0)
6644 {
6645 default_arch = "x86_64";
6646 break;
6647 }
6648 if (*l == NULL)
6649 as_fatal (_("No compiled in support for x86_64"));
6650 free (list);
6651 }
6652 break;
6653 #endif
6654
6655 case OPTION_32:
6656 default_arch = "i386";
6657 break;
6658
6659 case OPTION_DIVIDE:
6660 #ifdef SVR4_COMMENT_CHARS
6661 {
6662 char *n, *t;
6663 const char *s;
6664
6665 n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
6666 t = n;
6667 for (s = i386_comment_chars; *s != '\0'; s++)
6668 if (*s != '/')
6669 *t++ = *s;
6670 *t = '\0';
6671 i386_comment_chars = n;
6672 }
6673 #endif
6674 break;
6675
6676 case OPTION_MARCH:
6677 if (*arg == '.')
6678 as_fatal (_("Invalid -march= option: `%s'"), arg);
6679 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
6680 {
6681 if (strcmp (arg, cpu_arch [i].name) == 0)
6682 {
6683 cpu_arch_isa = cpu_arch[i].type;
6684 cpu_arch_isa_flags = cpu_arch[i].flags;
6685 if (!cpu_arch_tune_set)
6686 {
6687 cpu_arch_tune = cpu_arch_isa;
6688 cpu_arch_tune_flags = cpu_arch_isa_flags;
6689 }
6690 break;
6691 }
6692 }
6693 if (i >= ARRAY_SIZE (cpu_arch))
6694 as_fatal (_("Invalid -march= option: `%s'"), arg);
6695 break;
6696
6697 case OPTION_MTUNE:
6698 if (*arg == '.')
6699 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
6700 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
6701 {
6702 if (strcmp (arg, cpu_arch [i].name) == 0)
6703 {
6704 cpu_arch_tune_set = 1;
6705 cpu_arch_tune = cpu_arch [i].type;
6706 cpu_arch_tune_flags = cpu_arch[i].flags;
6707 break;
6708 }
6709 }
6710 if (i >= ARRAY_SIZE (cpu_arch))
6711 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
6712 break;
6713
6714 default:
6715 return 0;
6716 }
6717 return 1;
6718 }
6719
6720 void
6721 md_show_usage (stream)
6722 FILE *stream;
6723 {
6724 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6725 fprintf (stream, _("\
6726 -Q ignored\n\
6727 -V print assembler version number\n\
6728 -k ignored\n"));
6729 #endif
6730 fprintf (stream, _("\
6731 -n Do not optimize code alignment\n\
6732 -q quieten some warnings\n"));
6733 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6734 fprintf (stream, _("\
6735 -s ignored\n"));
6736 #endif
6737 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6738 fprintf (stream, _("\
6739 --32/--64 generate 32bit/64bit code\n"));
6740 #endif
6741 #ifdef SVR4_COMMENT_CHARS
6742 fprintf (stream, _("\
6743 --divide do not treat `/' as a comment character\n"));
6744 #else
6745 fprintf (stream, _("\
6746 --divide ignored\n"));
6747 #endif
6748 fprintf (stream, _("\
6749 -march=CPU/-mtune=CPU generate code/optimize for CPU, where CPU is one of:\n\
6750 i386, i486, pentium, pentiumpro, pentium4, nocona,\n\
6751 core, core2, k6, athlon, k8, generic32, generic64\n"));
6752
6753 }
6754
6755 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
6756 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (TE_PEP))
6757
6758 /* Pick the target format to use. */
6759
6760 const char *
6761 i386_target_format (void)
6762 {
6763 if (!strcmp (default_arch, "x86_64"))
6764 {
6765 set_code_flag (CODE_64BIT);
6766 if (UINTS_ALL_ZERO (cpu_arch_isa_flags))
6767 {
6768 cpu_arch_isa_flags.bitfield.cpui186 = 1;
6769 cpu_arch_isa_flags.bitfield.cpui286 = 1;
6770 cpu_arch_isa_flags.bitfield.cpui386 = 1;
6771 cpu_arch_isa_flags.bitfield.cpui486 = 1;
6772 cpu_arch_isa_flags.bitfield.cpui586 = 1;
6773 cpu_arch_isa_flags.bitfield.cpui686 = 1;
6774 cpu_arch_isa_flags.bitfield.cpup4 = 1;
6775 cpu_arch_isa_flags.bitfield.cpummx= 1;
6776 cpu_arch_isa_flags.bitfield.cpummx2 = 1;
6777 cpu_arch_isa_flags.bitfield.cpusse = 1;
6778 cpu_arch_isa_flags.bitfield.cpusse2 = 1;
6779 }
6780 if (UINTS_ALL_ZERO (cpu_arch_tune_flags))
6781 {
6782 cpu_arch_tune_flags.bitfield.cpui186 = 1;
6783 cpu_arch_tune_flags.bitfield.cpui286 = 1;
6784 cpu_arch_tune_flags.bitfield.cpui386 = 1;
6785 cpu_arch_tune_flags.bitfield.cpui486 = 1;
6786 cpu_arch_tune_flags.bitfield.cpui586 = 1;
6787 cpu_arch_tune_flags.bitfield.cpui686 = 1;
6788 cpu_arch_tune_flags.bitfield.cpup4 = 1;
6789 cpu_arch_tune_flags.bitfield.cpummx= 1;
6790 cpu_arch_tune_flags.bitfield.cpummx2 = 1;
6791 cpu_arch_tune_flags.bitfield.cpusse = 1;
6792 cpu_arch_tune_flags.bitfield.cpusse2 = 1;
6793 }
6794 }
6795 else if (!strcmp (default_arch, "i386"))
6796 {
6797 set_code_flag (CODE_32BIT);
6798 if (UINTS_ALL_ZERO (cpu_arch_isa_flags))
6799 {
6800 cpu_arch_isa_flags.bitfield.cpui186 = 1;
6801 cpu_arch_isa_flags.bitfield.cpui286 = 1;
6802 cpu_arch_isa_flags.bitfield.cpui386 = 1;
6803 }
6804 if (UINTS_ALL_ZERO (cpu_arch_tune_flags))
6805 {
6806 cpu_arch_tune_flags.bitfield.cpui186 = 1;
6807 cpu_arch_tune_flags.bitfield.cpui286 = 1;
6808 cpu_arch_tune_flags.bitfield.cpui386 = 1;
6809 }
6810 }
6811 else
6812 as_fatal (_("Unknown architecture"));
6813 switch (OUTPUT_FLAVOR)
6814 {
6815 #ifdef TE_PEP
6816 case bfd_target_coff_flavour:
6817 return flag_code == CODE_64BIT ? COFF_TARGET_FORMAT : "coff-i386";
6818 break;
6819 #endif
6820 #ifdef OBJ_MAYBE_AOUT
6821 case bfd_target_aout_flavour:
6822 return AOUT_TARGET_FORMAT;
6823 #endif
6824 #ifdef OBJ_MAYBE_COFF
6825 case bfd_target_coff_flavour:
6826 return "coff-i386";
6827 #endif
6828 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
6829 case bfd_target_elf_flavour:
6830 {
6831 if (flag_code == CODE_64BIT)
6832 {
6833 object_64bit = 1;
6834 use_rela_relocations = 1;
6835 }
6836 return flag_code == CODE_64BIT ? ELF_TARGET_FORMAT64 : ELF_TARGET_FORMAT;
6837 }
6838 #endif
6839 default:
6840 abort ();
6841 return NULL;
6842 }
6843 }
6844
6845 #endif /* OBJ_MAYBE_ more than one */
6846
6847 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
6848 void
6849 i386_elf_emit_arch_note (void)
6850 {
6851 if (IS_ELF && cpu_arch_name != NULL)
6852 {
6853 char *p;
6854 asection *seg = now_seg;
6855 subsegT subseg = now_subseg;
6856 Elf_Internal_Note i_note;
6857 Elf_External_Note e_note;
6858 asection *note_secp;
6859 int len;
6860
6861 /* Create the .note section. */
6862 note_secp = subseg_new (".note", 0);
6863 bfd_set_section_flags (stdoutput,
6864 note_secp,
6865 SEC_HAS_CONTENTS | SEC_READONLY);
6866
6867 /* Process the arch string. */
6868 len = strlen (cpu_arch_name);
6869
6870 i_note.namesz = len + 1;
6871 i_note.descsz = 0;
6872 i_note.type = NT_ARCH;
6873 p = frag_more (sizeof (e_note.namesz));
6874 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
6875 p = frag_more (sizeof (e_note.descsz));
6876 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
6877 p = frag_more (sizeof (e_note.type));
6878 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
6879 p = frag_more (len + 1);
6880 strcpy (p, cpu_arch_name);
6881
6882 frag_align (2, 0, 0);
6883
6884 subseg_set (seg, subseg);
6885 }
6886 }
6887 #endif
6888 \f
6889 symbolS *
6890 md_undefined_symbol (name)
6891 char *name;
6892 {
6893 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
6894 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
6895 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
6896 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
6897 {
6898 if (!GOT_symbol)
6899 {
6900 if (symbol_find (name))
6901 as_bad (_("GOT already in symbol table"));
6902 GOT_symbol = symbol_new (name, undefined_section,
6903 (valueT) 0, &zero_address_frag);
6904 };
6905 return GOT_symbol;
6906 }
6907 return 0;
6908 }
6909
6910 /* Round up a section size to the appropriate boundary. */
6911
6912 valueT
6913 md_section_align (segment, size)
6914 segT segment ATTRIBUTE_UNUSED;
6915 valueT size;
6916 {
6917 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6918 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
6919 {
6920 /* For a.out, force the section size to be aligned. If we don't do
6921 this, BFD will align it for us, but it will not write out the
6922 final bytes of the section. This may be a bug in BFD, but it is
6923 easier to fix it here since that is how the other a.out targets
6924 work. */
6925 int align;
6926
6927 align = bfd_get_section_alignment (stdoutput, segment);
6928 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
6929 }
6930 #endif
6931
6932 return size;
6933 }
6934
6935 /* On the i386, PC-relative offsets are relative to the start of the
6936 next instruction. That is, the address of the offset, plus its
6937 size, since the offset is always the last part of the insn. */
6938
6939 long
6940 md_pcrel_from (fixS *fixP)
6941 {
6942 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
6943 }
6944
6945 #ifndef I386COFF
6946
6947 static void
6948 s_bss (int ignore ATTRIBUTE_UNUSED)
6949 {
6950 int temp;
6951
6952 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6953 if (IS_ELF)
6954 obj_elf_section_change_hook ();
6955 #endif
6956 temp = get_absolute_expression ();
6957 subseg_set (bss_section, (subsegT) temp);
6958 demand_empty_rest_of_line ();
6959 }
6960
6961 #endif
6962
6963 void
6964 i386_validate_fix (fixS *fixp)
6965 {
6966 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
6967 {
6968 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
6969 {
6970 if (!object_64bit)
6971 abort ();
6972 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
6973 }
6974 else
6975 {
6976 if (!object_64bit)
6977 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
6978 else
6979 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
6980 }
6981 fixp->fx_subsy = 0;
6982 }
6983 }
6984
6985 arelent *
6986 tc_gen_reloc (section, fixp)
6987 asection *section ATTRIBUTE_UNUSED;
6988 fixS *fixp;
6989 {
6990 arelent *rel;
6991 bfd_reloc_code_real_type code;
6992
6993 switch (fixp->fx_r_type)
6994 {
6995 case BFD_RELOC_X86_64_PLT32:
6996 case BFD_RELOC_X86_64_GOT32:
6997 case BFD_RELOC_X86_64_GOTPCREL:
6998 case BFD_RELOC_386_PLT32:
6999 case BFD_RELOC_386_GOT32:
7000 case BFD_RELOC_386_GOTOFF:
7001 case BFD_RELOC_386_GOTPC:
7002 case BFD_RELOC_386_TLS_GD:
7003 case BFD_RELOC_386_TLS_LDM:
7004 case BFD_RELOC_386_TLS_LDO_32:
7005 case BFD_RELOC_386_TLS_IE_32:
7006 case BFD_RELOC_386_TLS_IE:
7007 case BFD_RELOC_386_TLS_GOTIE:
7008 case BFD_RELOC_386_TLS_LE_32:
7009 case BFD_RELOC_386_TLS_LE:
7010 case BFD_RELOC_386_TLS_GOTDESC:
7011 case BFD_RELOC_386_TLS_DESC_CALL:
7012 case BFD_RELOC_X86_64_TLSGD:
7013 case BFD_RELOC_X86_64_TLSLD:
7014 case BFD_RELOC_X86_64_DTPOFF32:
7015 case BFD_RELOC_X86_64_DTPOFF64:
7016 case BFD_RELOC_X86_64_GOTTPOFF:
7017 case BFD_RELOC_X86_64_TPOFF32:
7018 case BFD_RELOC_X86_64_TPOFF64:
7019 case BFD_RELOC_X86_64_GOTOFF64:
7020 case BFD_RELOC_X86_64_GOTPC32:
7021 case BFD_RELOC_X86_64_GOT64:
7022 case BFD_RELOC_X86_64_GOTPCREL64:
7023 case BFD_RELOC_X86_64_GOTPC64:
7024 case BFD_RELOC_X86_64_GOTPLT64:
7025 case BFD_RELOC_X86_64_PLTOFF64:
7026 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7027 case BFD_RELOC_X86_64_TLSDESC_CALL:
7028 case BFD_RELOC_RVA:
7029 case BFD_RELOC_VTABLE_ENTRY:
7030 case BFD_RELOC_VTABLE_INHERIT:
7031 #ifdef TE_PE
7032 case BFD_RELOC_32_SECREL:
7033 #endif
7034 code = fixp->fx_r_type;
7035 break;
7036 case BFD_RELOC_X86_64_32S:
7037 if (!fixp->fx_pcrel)
7038 {
7039 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
7040 code = fixp->fx_r_type;
7041 break;
7042 }
7043 default:
7044 if (fixp->fx_pcrel)
7045 {
7046 switch (fixp->fx_size)
7047 {
7048 default:
7049 as_bad_where (fixp->fx_file, fixp->fx_line,
7050 _("can not do %d byte pc-relative relocation"),
7051 fixp->fx_size);
7052 code = BFD_RELOC_32_PCREL;
7053 break;
7054 case 1: code = BFD_RELOC_8_PCREL; break;
7055 case 2: code = BFD_RELOC_16_PCREL; break;
7056 case 4: code = BFD_RELOC_32_PCREL; break;
7057 #ifdef BFD64
7058 case 8: code = BFD_RELOC_64_PCREL; break;
7059 #endif
7060 }
7061 }
7062 else
7063 {
7064 switch (fixp->fx_size)
7065 {
7066 default:
7067 as_bad_where (fixp->fx_file, fixp->fx_line,
7068 _("can not do %d byte relocation"),
7069 fixp->fx_size);
7070 code = BFD_RELOC_32;
7071 break;
7072 case 1: code = BFD_RELOC_8; break;
7073 case 2: code = BFD_RELOC_16; break;
7074 case 4: code = BFD_RELOC_32; break;
7075 #ifdef BFD64
7076 case 8: code = BFD_RELOC_64; break;
7077 #endif
7078 }
7079 }
7080 break;
7081 }
7082
7083 if ((code == BFD_RELOC_32
7084 || code == BFD_RELOC_32_PCREL
7085 || code == BFD_RELOC_X86_64_32S)
7086 && GOT_symbol
7087 && fixp->fx_addsy == GOT_symbol)
7088 {
7089 if (!object_64bit)
7090 code = BFD_RELOC_386_GOTPC;
7091 else
7092 code = BFD_RELOC_X86_64_GOTPC32;
7093 }
7094 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
7095 && GOT_symbol
7096 && fixp->fx_addsy == GOT_symbol)
7097 {
7098 code = BFD_RELOC_X86_64_GOTPC64;
7099 }
7100
7101 rel = (arelent *) xmalloc (sizeof (arelent));
7102 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
7103 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7104
7105 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
7106
7107 if (!use_rela_relocations)
7108 {
7109 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
7110 vtable entry to be used in the relocation's section offset. */
7111 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
7112 rel->address = fixp->fx_offset;
7113
7114 rel->addend = 0;
7115 }
7116 /* Use the rela in 64bit mode. */
7117 else
7118 {
7119 if (!fixp->fx_pcrel)
7120 rel->addend = fixp->fx_offset;
7121 else
7122 switch (code)
7123 {
7124 case BFD_RELOC_X86_64_PLT32:
7125 case BFD_RELOC_X86_64_GOT32:
7126 case BFD_RELOC_X86_64_GOTPCREL:
7127 case BFD_RELOC_X86_64_TLSGD:
7128 case BFD_RELOC_X86_64_TLSLD:
7129 case BFD_RELOC_X86_64_GOTTPOFF:
7130 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7131 case BFD_RELOC_X86_64_TLSDESC_CALL:
7132 rel->addend = fixp->fx_offset - fixp->fx_size;
7133 break;
7134 default:
7135 rel->addend = (section->vma
7136 - fixp->fx_size
7137 + fixp->fx_addnumber
7138 + md_pcrel_from (fixp));
7139 break;
7140 }
7141 }
7142
7143 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
7144 if (rel->howto == NULL)
7145 {
7146 as_bad_where (fixp->fx_file, fixp->fx_line,
7147 _("cannot represent relocation type %s"),
7148 bfd_get_reloc_code_name (code));
7149 /* Set howto to a garbage value so that we can keep going. */
7150 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
7151 assert (rel->howto != NULL);
7152 }
7153
7154 return rel;
7155 }
7156
7157 \f
7158 /* Parse operands using Intel syntax. This implements a recursive descent
7159 parser based on the BNF grammar published in Appendix B of the MASM 6.1
7160 Programmer's Guide.
7161
7162 FIXME: We do not recognize the full operand grammar defined in the MASM
7163 documentation. In particular, all the structure/union and
7164 high-level macro operands are missing.
7165
7166 Uppercase words are terminals, lower case words are non-terminals.
7167 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
7168 bars '|' denote choices. Most grammar productions are implemented in
7169 functions called 'intel_<production>'.
7170
7171 Initial production is 'expr'.
7172
7173 addOp + | -
7174
7175 alpha [a-zA-Z]
7176
7177 binOp & | AND | \| | OR | ^ | XOR
7178
7179 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
7180
7181 constant digits [[ radixOverride ]]
7182
7183 dataType BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD
7184
7185 digits decdigit
7186 | digits decdigit
7187 | digits hexdigit
7188
7189 decdigit [0-9]
7190
7191 e04 e04 addOp e05
7192 | e05
7193
7194 e05 e05 binOp e06
7195 | e06
7196
7197 e06 e06 mulOp e09
7198 | e09
7199
7200 e09 OFFSET e10
7201 | SHORT e10
7202 | + e10
7203 | - e10
7204 | ~ e10
7205 | NOT e10
7206 | e09 PTR e10
7207 | e09 : e10
7208 | e10
7209
7210 e10 e10 [ expr ]
7211 | e11
7212
7213 e11 ( expr )
7214 | [ expr ]
7215 | constant
7216 | dataType
7217 | id
7218 | $
7219 | register
7220
7221 => expr expr cmpOp e04
7222 | e04
7223
7224 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
7225 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
7226
7227 hexdigit a | b | c | d | e | f
7228 | A | B | C | D | E | F
7229
7230 id alpha
7231 | id alpha
7232 | id decdigit
7233
7234 mulOp * | / | % | MOD | << | SHL | >> | SHR
7235
7236 quote " | '
7237
7238 register specialRegister
7239 | gpRegister
7240 | byteRegister
7241
7242 segmentRegister CS | DS | ES | FS | GS | SS
7243
7244 specialRegister CR0 | CR2 | CR3 | CR4
7245 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
7246 | TR3 | TR4 | TR5 | TR6 | TR7
7247
7248 We simplify the grammar in obvious places (e.g., register parsing is
7249 done by calling parse_register) and eliminate immediate left recursion
7250 to implement a recursive-descent parser.
7251
7252 expr e04 expr'
7253
7254 expr' cmpOp e04 expr'
7255 | Empty
7256
7257 e04 e05 e04'
7258
7259 e04' addOp e05 e04'
7260 | Empty
7261
7262 e05 e06 e05'
7263
7264 e05' binOp e06 e05'
7265 | Empty
7266
7267 e06 e09 e06'
7268
7269 e06' mulOp e09 e06'
7270 | Empty
7271
7272 e09 OFFSET e10 e09'
7273 | SHORT e10'
7274 | + e10'
7275 | - e10'
7276 | ~ e10'
7277 | NOT e10'
7278 | e10 e09'
7279
7280 e09' PTR e10 e09'
7281 | : e10 e09'
7282 | Empty
7283
7284 e10 e11 e10'
7285
7286 e10' [ expr ] e10'
7287 | Empty
7288
7289 e11 ( expr )
7290 | [ expr ]
7291 | BYTE
7292 | WORD
7293 | DWORD
7294 | FWORD
7295 | QWORD
7296 | TBYTE
7297 | OWORD
7298 | XMMWORD
7299 | .
7300 | $
7301 | register
7302 | id
7303 | constant */
7304
7305 /* Parsing structure for the intel syntax parser. Used to implement the
7306 semantic actions for the operand grammar. */
7307 struct intel_parser_s
7308 {
7309 char *op_string; /* The string being parsed. */
7310 int got_a_float; /* Whether the operand is a float. */
7311 int op_modifier; /* Operand modifier. */
7312 int is_mem; /* 1 if operand is memory reference. */
7313 int in_offset; /* >=1 if parsing operand of offset. */
7314 int in_bracket; /* >=1 if parsing operand in brackets. */
7315 const reg_entry *reg; /* Last register reference found. */
7316 char *disp; /* Displacement string being built. */
7317 char *next_operand; /* Resume point when splitting operands. */
7318 };
7319
7320 static struct intel_parser_s intel_parser;
7321
7322 /* Token structure for parsing intel syntax. */
7323 struct intel_token
7324 {
7325 int code; /* Token code. */
7326 const reg_entry *reg; /* Register entry for register tokens. */
7327 char *str; /* String representation. */
7328 };
7329
7330 static struct intel_token cur_token, prev_token;
7331
7332 /* Token codes for the intel parser. Since T_SHORT is already used
7333 by COFF, undefine it first to prevent a warning. */
7334 #define T_NIL -1
7335 #define T_CONST 1
7336 #define T_REG 2
7337 #define T_BYTE 3
7338 #define T_WORD 4
7339 #define T_DWORD 5
7340 #define T_FWORD 6
7341 #define T_QWORD 7
7342 #define T_TBYTE 8
7343 #define T_XMMWORD 9
7344 #undef T_SHORT
7345 #define T_SHORT 10
7346 #define T_OFFSET 11
7347 #define T_PTR 12
7348 #define T_ID 13
7349 #define T_SHL 14
7350 #define T_SHR 15
7351
7352 /* Prototypes for intel parser functions. */
7353 static int intel_match_token (int);
7354 static void intel_putback_token (void);
7355 static void intel_get_token (void);
7356 static int intel_expr (void);
7357 static int intel_e04 (void);
7358 static int intel_e05 (void);
7359 static int intel_e06 (void);
7360 static int intel_e09 (void);
7361 static int intel_e10 (void);
7362 static int intel_e11 (void);
7363
7364 static int
7365 i386_intel_operand (char *operand_string, int got_a_float)
7366 {
7367 int ret;
7368 char *p;
7369
7370 p = intel_parser.op_string = xstrdup (operand_string);
7371 intel_parser.disp = (char *) xmalloc (strlen (operand_string) + 1);
7372
7373 for (;;)
7374 {
7375 /* Initialize token holders. */
7376 cur_token.code = prev_token.code = T_NIL;
7377 cur_token.reg = prev_token.reg = NULL;
7378 cur_token.str = prev_token.str = NULL;
7379
7380 /* Initialize parser structure. */
7381 intel_parser.got_a_float = got_a_float;
7382 intel_parser.op_modifier = 0;
7383 intel_parser.is_mem = 0;
7384 intel_parser.in_offset = 0;
7385 intel_parser.in_bracket = 0;
7386 intel_parser.reg = NULL;
7387 intel_parser.disp[0] = '\0';
7388 intel_parser.next_operand = NULL;
7389
7390 /* Read the first token and start the parser. */
7391 intel_get_token ();
7392 ret = intel_expr ();
7393
7394 if (!ret)
7395 break;
7396
7397 if (cur_token.code != T_NIL)
7398 {
7399 as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
7400 current_templates->start->name, cur_token.str);
7401 ret = 0;
7402 }
7403 /* If we found a memory reference, hand it over to i386_displacement
7404 to fill in the rest of the operand fields. */
7405 else if (intel_parser.is_mem)
7406 {
7407 if ((i.mem_operands == 1
7408 && !current_templates->start->opcode_modifier.isstring)
7409 || i.mem_operands == 2)
7410 {
7411 as_bad (_("too many memory references for '%s'"),
7412 current_templates->start->name);
7413 ret = 0;
7414 }
7415 else
7416 {
7417 char *s = intel_parser.disp;
7418 i.mem_operands++;
7419
7420 if (!quiet_warnings && intel_parser.is_mem < 0)
7421 /* See the comments in intel_bracket_expr. */
7422 as_warn (_("Treating `%s' as memory reference"), operand_string);
7423
7424 /* Add the displacement expression. */
7425 if (*s != '\0')
7426 ret = i386_displacement (s, s + strlen (s));
7427 if (ret)
7428 {
7429 /* Swap base and index in 16-bit memory operands like
7430 [si+bx]. Since i386_index_check is also used in AT&T
7431 mode we have to do that here. */
7432 if (i.base_reg
7433 && i.index_reg
7434 && i.base_reg->reg_type.bitfield.reg16
7435 && i.index_reg->reg_type.bitfield.reg16
7436 && i.base_reg->reg_num >= 6
7437 && i.index_reg->reg_num < 6)
7438 {
7439 const reg_entry *base = i.index_reg;
7440
7441 i.index_reg = i.base_reg;
7442 i.base_reg = base;
7443 }
7444 ret = i386_index_check (operand_string);
7445 }
7446 }
7447 }
7448
7449 /* Constant and OFFSET expressions are handled by i386_immediate. */
7450 else if ((intel_parser.op_modifier & (1 << T_OFFSET))
7451 || intel_parser.reg == NULL)
7452 ret = i386_immediate (intel_parser.disp);
7453
7454 if (intel_parser.next_operand && this_operand >= MAX_OPERANDS - 1)
7455 ret = 0;
7456 if (!ret || !intel_parser.next_operand)
7457 break;
7458 intel_parser.op_string = intel_parser.next_operand;
7459 this_operand = i.operands++;
7460 }
7461
7462 free (p);
7463 free (intel_parser.disp);
7464
7465 return ret;
7466 }
7467
7468 #define NUM_ADDRESS_REGS (!!i.base_reg + !!i.index_reg)
7469
7470 /* expr e04 expr'
7471
7472 expr' cmpOp e04 expr'
7473 | Empty */
7474 static int
7475 intel_expr (void)
7476 {
7477 /* XXX Implement the comparison operators. */
7478 return intel_e04 ();
7479 }
7480
7481 /* e04 e05 e04'
7482
7483 e04' addOp e05 e04'
7484 | Empty */
7485 static int
7486 intel_e04 (void)
7487 {
7488 int nregs = -1;
7489
7490 for (;;)
7491 {
7492 if (!intel_e05())
7493 return 0;
7494
7495 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7496 i.base_reg = i386_regtab + REGNAM_AL; /* al is invalid as base */
7497
7498 if (cur_token.code == '+')
7499 nregs = -1;
7500 else if (cur_token.code == '-')
7501 nregs = NUM_ADDRESS_REGS;
7502 else
7503 return 1;
7504
7505 strcat (intel_parser.disp, cur_token.str);
7506 intel_match_token (cur_token.code);
7507 }
7508 }
7509
7510 /* e05 e06 e05'
7511
7512 e05' binOp e06 e05'
7513 | Empty */
7514 static int
7515 intel_e05 (void)
7516 {
7517 int nregs = ~NUM_ADDRESS_REGS;
7518
7519 for (;;)
7520 {
7521 if (!intel_e06())
7522 return 0;
7523
7524 if (cur_token.code == '&'
7525 || cur_token.code == '|'
7526 || cur_token.code == '^')
7527 {
7528 char str[2];
7529
7530 str[0] = cur_token.code;
7531 str[1] = 0;
7532 strcat (intel_parser.disp, str);
7533 }
7534 else
7535 break;
7536
7537 intel_match_token (cur_token.code);
7538
7539 if (nregs < 0)
7540 nregs = ~nregs;
7541 }
7542 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7543 i.base_reg = i386_regtab + REGNAM_AL + 1; /* cl is invalid as base */
7544 return 1;
7545 }
7546
7547 /* e06 e09 e06'
7548
7549 e06' mulOp e09 e06'
7550 | Empty */
7551 static int
7552 intel_e06 (void)
7553 {
7554 int nregs = ~NUM_ADDRESS_REGS;
7555
7556 for (;;)
7557 {
7558 if (!intel_e09())
7559 return 0;
7560
7561 if (cur_token.code == '*'
7562 || cur_token.code == '/'
7563 || cur_token.code == '%')
7564 {
7565 char str[2];
7566
7567 str[0] = cur_token.code;
7568 str[1] = 0;
7569 strcat (intel_parser.disp, str);
7570 }
7571 else if (cur_token.code == T_SHL)
7572 strcat (intel_parser.disp, "<<");
7573 else if (cur_token.code == T_SHR)
7574 strcat (intel_parser.disp, ">>");
7575 else
7576 break;
7577
7578 intel_match_token (cur_token.code);
7579
7580 if (nregs < 0)
7581 nregs = ~nregs;
7582 }
7583 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7584 i.base_reg = i386_regtab + REGNAM_AL + 2; /* dl is invalid as base */
7585 return 1;
7586 }
7587
7588 /* e09 OFFSET e09
7589 | SHORT e09
7590 | + e09
7591 | - e09
7592 | ~ e09
7593 | NOT e09
7594 | e10 e09'
7595
7596 e09' PTR e10 e09'
7597 | : e10 e09'
7598 | Empty */
7599 static int
7600 intel_e09 (void)
7601 {
7602 int nregs = ~NUM_ADDRESS_REGS;
7603 int in_offset = 0;
7604
7605 for (;;)
7606 {
7607 /* Don't consume constants here. */
7608 if (cur_token.code == '+' || cur_token.code == '-')
7609 {
7610 /* Need to look one token ahead - if the next token
7611 is a constant, the current token is its sign. */
7612 int next_code;
7613
7614 intel_match_token (cur_token.code);
7615 next_code = cur_token.code;
7616 intel_putback_token ();
7617 if (next_code == T_CONST)
7618 break;
7619 }
7620
7621 /* e09 OFFSET e09 */
7622 if (cur_token.code == T_OFFSET)
7623 {
7624 if (!in_offset++)
7625 ++intel_parser.in_offset;
7626 }
7627
7628 /* e09 SHORT e09 */
7629 else if (cur_token.code == T_SHORT)
7630 intel_parser.op_modifier |= 1 << T_SHORT;
7631
7632 /* e09 + e09 */
7633 else if (cur_token.code == '+')
7634 strcat (intel_parser.disp, "+");
7635
7636 /* e09 - e09
7637 | ~ e09
7638 | NOT e09 */
7639 else if (cur_token.code == '-' || cur_token.code == '~')
7640 {
7641 char str[2];
7642
7643 if (nregs < 0)
7644 nregs = ~nregs;
7645 str[0] = cur_token.code;
7646 str[1] = 0;
7647 strcat (intel_parser.disp, str);
7648 }
7649
7650 /* e09 e10 e09' */
7651 else
7652 break;
7653
7654 intel_match_token (cur_token.code);
7655 }
7656
7657 for (;;)
7658 {
7659 if (!intel_e10 ())
7660 return 0;
7661
7662 /* e09' PTR e10 e09' */
7663 if (cur_token.code == T_PTR)
7664 {
7665 char suffix;
7666
7667 if (prev_token.code == T_BYTE)
7668 suffix = BYTE_MNEM_SUFFIX;
7669
7670 else if (prev_token.code == T_WORD)
7671 {
7672 if (current_templates->start->name[0] == 'l'
7673 && current_templates->start->name[2] == 's'
7674 && current_templates->start->name[3] == 0)
7675 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7676 else if (intel_parser.got_a_float == 2) /* "fi..." */
7677 suffix = SHORT_MNEM_SUFFIX;
7678 else
7679 suffix = WORD_MNEM_SUFFIX;
7680 }
7681
7682 else if (prev_token.code == T_DWORD)
7683 {
7684 if (current_templates->start->name[0] == 'l'
7685 && current_templates->start->name[2] == 's'
7686 && current_templates->start->name[3] == 0)
7687 suffix = WORD_MNEM_SUFFIX;
7688 else if (flag_code == CODE_16BIT
7689 && (current_templates->start->opcode_modifier.jump
7690 || current_templates->start->opcode_modifier.jumpdword))
7691 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7692 else if (intel_parser.got_a_float == 1) /* "f..." */
7693 suffix = SHORT_MNEM_SUFFIX;
7694 else
7695 suffix = LONG_MNEM_SUFFIX;
7696 }
7697
7698 else if (prev_token.code == T_FWORD)
7699 {
7700 if (current_templates->start->name[0] == 'l'
7701 && current_templates->start->name[2] == 's'
7702 && current_templates->start->name[3] == 0)
7703 suffix = LONG_MNEM_SUFFIX;
7704 else if (!intel_parser.got_a_float)
7705 {
7706 if (flag_code == CODE_16BIT)
7707 add_prefix (DATA_PREFIX_OPCODE);
7708 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7709 }
7710 else
7711 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7712 }
7713
7714 else if (prev_token.code == T_QWORD)
7715 {
7716 if (intel_parser.got_a_float == 1) /* "f..." */
7717 suffix = LONG_MNEM_SUFFIX;
7718 else
7719 suffix = QWORD_MNEM_SUFFIX;
7720 }
7721
7722 else if (prev_token.code == T_TBYTE)
7723 {
7724 if (intel_parser.got_a_float == 1)
7725 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7726 else
7727 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7728 }
7729
7730 else if (prev_token.code == T_XMMWORD)
7731 {
7732 /* XXX ignored for now, but accepted since gcc uses it */
7733 suffix = 0;
7734 }
7735
7736 else
7737 {
7738 as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
7739 return 0;
7740 }
7741
7742 /* Operands for jump/call using 'ptr' notation denote absolute
7743 addresses. */
7744 if (current_templates->start->opcode_modifier.jump
7745 || current_templates->start->opcode_modifier.jumpdword)
7746 i.types[this_operand].bitfield.jumpabsolute = 1;
7747
7748 if (current_templates->start->base_opcode == 0x8d /* lea */)
7749 ;
7750 else if (!i.suffix)
7751 i.suffix = suffix;
7752 else if (i.suffix != suffix)
7753 {
7754 as_bad (_("Conflicting operand modifiers"));
7755 return 0;
7756 }
7757
7758 }
7759
7760 /* e09' : e10 e09' */
7761 else if (cur_token.code == ':')
7762 {
7763 if (prev_token.code != T_REG)
7764 {
7765 /* While {call,jmp} SSSS:OOOO is MASM syntax only when SSSS is a
7766 segment/group identifier (which we don't have), using comma
7767 as the operand separator there is even less consistent, since
7768 there all branches only have a single operand. */
7769 if (this_operand != 0
7770 || intel_parser.in_offset
7771 || intel_parser.in_bracket
7772 || (!current_templates->start->opcode_modifier.jump
7773 && !current_templates->start->opcode_modifier.jumpdword
7774 && !current_templates->start->opcode_modifier.jumpintersegment
7775 && !current_templates->start->operand_types[0].bitfield.jumpabsolute))
7776 return intel_match_token (T_NIL);
7777 /* Remember the start of the 2nd operand and terminate 1st
7778 operand here.
7779 XXX This isn't right, yet (when SSSS:OOOO is right operand of
7780 another expression), but it gets at least the simplest case
7781 (a plain number or symbol on the left side) right. */
7782 intel_parser.next_operand = intel_parser.op_string;
7783 *--intel_parser.op_string = '\0';
7784 return intel_match_token (':');
7785 }
7786 }
7787
7788 /* e09' Empty */
7789 else
7790 break;
7791
7792 intel_match_token (cur_token.code);
7793
7794 }
7795
7796 if (in_offset)
7797 {
7798 --intel_parser.in_offset;
7799 if (nregs < 0)
7800 nregs = ~nregs;
7801 if (NUM_ADDRESS_REGS > nregs)
7802 {
7803 as_bad (_("Invalid operand to `OFFSET'"));
7804 return 0;
7805 }
7806 intel_parser.op_modifier |= 1 << T_OFFSET;
7807 }
7808
7809 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7810 i.base_reg = i386_regtab + REGNAM_AL + 3; /* bl is invalid as base */
7811 return 1;
7812 }
7813
7814 static int
7815 intel_bracket_expr (void)
7816 {
7817 int was_offset = intel_parser.op_modifier & (1 << T_OFFSET);
7818 const char *start = intel_parser.op_string;
7819 int len;
7820
7821 if (i.op[this_operand].regs)
7822 return intel_match_token (T_NIL);
7823
7824 intel_match_token ('[');
7825
7826 /* Mark as a memory operand only if it's not already known to be an
7827 offset expression. If it's an offset expression, we need to keep
7828 the brace in. */
7829 if (!intel_parser.in_offset)
7830 {
7831 ++intel_parser.in_bracket;
7832
7833 /* Operands for jump/call inside brackets denote absolute addresses. */
7834 if (current_templates->start->opcode_modifier.jump
7835 || current_templates->start->opcode_modifier.jumpdword)
7836 i.types[this_operand].bitfield.jumpabsolute = 1;
7837
7838 /* Unfortunately gas always diverged from MASM in a respect that can't
7839 be easily fixed without risking to break code sequences likely to be
7840 encountered (the testsuite even check for this): MASM doesn't consider
7841 an expression inside brackets unconditionally as a memory reference.
7842 When that is e.g. a constant, an offset expression, or the sum of the
7843 two, this is still taken as a constant load. gas, however, always
7844 treated these as memory references. As a compromise, we'll try to make
7845 offset expressions inside brackets work the MASM way (since that's
7846 less likely to be found in real world code), but make constants alone
7847 continue to work the traditional gas way. In either case, issue a
7848 warning. */
7849 intel_parser.op_modifier &= ~was_offset;
7850 }
7851 else
7852 strcat (intel_parser.disp, "[");
7853
7854 /* Add a '+' to the displacement string if necessary. */
7855 if (*intel_parser.disp != '\0'
7856 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
7857 strcat (intel_parser.disp, "+");
7858
7859 if (intel_expr ()
7860 && (len = intel_parser.op_string - start - 1,
7861 intel_match_token (']')))
7862 {
7863 /* Preserve brackets when the operand is an offset expression. */
7864 if (intel_parser.in_offset)
7865 strcat (intel_parser.disp, "]");
7866 else
7867 {
7868 --intel_parser.in_bracket;
7869 if (i.base_reg || i.index_reg)
7870 intel_parser.is_mem = 1;
7871 if (!intel_parser.is_mem)
7872 {
7873 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
7874 /* Defer the warning until all of the operand was parsed. */
7875 intel_parser.is_mem = -1;
7876 else if (!quiet_warnings)
7877 as_warn (_("`[%.*s]' taken to mean just `%.*s'"),
7878 len, start, len, start);
7879 }
7880 }
7881 intel_parser.op_modifier |= was_offset;
7882
7883 return 1;
7884 }
7885 return 0;
7886 }
7887
7888 /* e10 e11 e10'
7889
7890 e10' [ expr ] e10'
7891 | Empty */
7892 static int
7893 intel_e10 (void)
7894 {
7895 if (!intel_e11 ())
7896 return 0;
7897
7898 while (cur_token.code == '[')
7899 {
7900 if (!intel_bracket_expr ())
7901 return 0;
7902 }
7903
7904 return 1;
7905 }
7906
7907 /* e11 ( expr )
7908 | [ expr ]
7909 | BYTE
7910 | WORD
7911 | DWORD
7912 | FWORD
7913 | QWORD
7914 | TBYTE
7915 | OWORD
7916 | XMMWORD
7917 | $
7918 | .
7919 | register
7920 | id
7921 | constant */
7922 static int
7923 intel_e11 (void)
7924 {
7925 switch (cur_token.code)
7926 {
7927 /* e11 ( expr ) */
7928 case '(':
7929 intel_match_token ('(');
7930 strcat (intel_parser.disp, "(");
7931
7932 if (intel_expr () && intel_match_token (')'))
7933 {
7934 strcat (intel_parser.disp, ")");
7935 return 1;
7936 }
7937 return 0;
7938
7939 /* e11 [ expr ] */
7940 case '[':
7941 return intel_bracket_expr ();
7942
7943 /* e11 $
7944 | . */
7945 case '.':
7946 strcat (intel_parser.disp, cur_token.str);
7947 intel_match_token (cur_token.code);
7948
7949 /* Mark as a memory operand only if it's not already known to be an
7950 offset expression. */
7951 if (!intel_parser.in_offset)
7952 intel_parser.is_mem = 1;
7953
7954 return 1;
7955
7956 /* e11 register */
7957 case T_REG:
7958 {
7959 const reg_entry *reg = intel_parser.reg = cur_token.reg;
7960
7961 intel_match_token (T_REG);
7962
7963 /* Check for segment change. */
7964 if (cur_token.code == ':')
7965 {
7966 if (!reg->reg_type.bitfield.sreg2
7967 && !reg->reg_type.bitfield.sreg3)
7968 {
7969 as_bad (_("`%s' is not a valid segment register"),
7970 reg->reg_name);
7971 return 0;
7972 }
7973 else if (i.seg[i.mem_operands])
7974 as_warn (_("Extra segment override ignored"));
7975 else
7976 {
7977 if (!intel_parser.in_offset)
7978 intel_parser.is_mem = 1;
7979 switch (reg->reg_num)
7980 {
7981 case 0:
7982 i.seg[i.mem_operands] = &es;
7983 break;
7984 case 1:
7985 i.seg[i.mem_operands] = &cs;
7986 break;
7987 case 2:
7988 i.seg[i.mem_operands] = &ss;
7989 break;
7990 case 3:
7991 i.seg[i.mem_operands] = &ds;
7992 break;
7993 case 4:
7994 i.seg[i.mem_operands] = &fs;
7995 break;
7996 case 5:
7997 i.seg[i.mem_operands] = &gs;
7998 break;
7999 }
8000 }
8001 }
8002
8003 /* Not a segment register. Check for register scaling. */
8004 else if (cur_token.code == '*')
8005 {
8006 if (!intel_parser.in_bracket)
8007 {
8008 as_bad (_("Register scaling only allowed in memory operands"));
8009 return 0;
8010 }
8011
8012 if (reg->reg_type.bitfield.reg16) /* Disallow things like [si*1]. */
8013 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
8014 else if (i.index_reg)
8015 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
8016
8017 /* What follows must be a valid scale. */
8018 intel_match_token ('*');
8019 i.index_reg = reg;
8020 i.types[this_operand].bitfield.baseindex = 1;
8021
8022 /* Set the scale after setting the register (otherwise,
8023 i386_scale will complain) */
8024 if (cur_token.code == '+' || cur_token.code == '-')
8025 {
8026 char *str, sign = cur_token.code;
8027 intel_match_token (cur_token.code);
8028 if (cur_token.code != T_CONST)
8029 {
8030 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
8031 cur_token.str);
8032 return 0;
8033 }
8034 str = (char *) xmalloc (strlen (cur_token.str) + 2);
8035 strcpy (str + 1, cur_token.str);
8036 *str = sign;
8037 if (!i386_scale (str))
8038 return 0;
8039 free (str);
8040 }
8041 else if (!i386_scale (cur_token.str))
8042 return 0;
8043 intel_match_token (cur_token.code);
8044 }
8045
8046 /* No scaling. If this is a memory operand, the register is either a
8047 base register (first occurrence) or an index register (second
8048 occurrence). */
8049 else if (intel_parser.in_bracket)
8050 {
8051
8052 if (!i.base_reg)
8053 i.base_reg = reg;
8054 else if (!i.index_reg)
8055 i.index_reg = reg;
8056 else
8057 {
8058 as_bad (_("Too many register references in memory operand"));
8059 return 0;
8060 }
8061
8062 i.types[this_operand].bitfield.baseindex = 1;
8063 }
8064
8065 /* It's neither base nor index. */
8066 else if (!intel_parser.in_offset && !intel_parser.is_mem)
8067 {
8068 i386_operand_type temp = reg->reg_type;
8069 temp.bitfield.baseindex = 0;
8070 i.types[this_operand] = operand_type_or (i.types[this_operand],
8071 temp);
8072 i.op[this_operand].regs = reg;
8073 i.reg_operands++;
8074 }
8075 else
8076 {
8077 as_bad (_("Invalid use of register"));
8078 return 0;
8079 }
8080
8081 /* Since registers are not part of the displacement string (except
8082 when we're parsing offset operands), we may need to remove any
8083 preceding '+' from the displacement string. */
8084 if (*intel_parser.disp != '\0'
8085 && !intel_parser.in_offset)
8086 {
8087 char *s = intel_parser.disp;
8088 s += strlen (s) - 1;
8089 if (*s == '+')
8090 *s = '\0';
8091 }
8092
8093 return 1;
8094 }
8095
8096 /* e11 BYTE
8097 | WORD
8098 | DWORD
8099 | FWORD
8100 | QWORD
8101 | TBYTE
8102 | OWORD
8103 | XMMWORD */
8104 case T_BYTE:
8105 case T_WORD:
8106 case T_DWORD:
8107 case T_FWORD:
8108 case T_QWORD:
8109 case T_TBYTE:
8110 case T_XMMWORD:
8111 intel_match_token (cur_token.code);
8112
8113 if (cur_token.code == T_PTR)
8114 return 1;
8115
8116 /* It must have been an identifier. */
8117 intel_putback_token ();
8118 cur_token.code = T_ID;
8119 /* FALLTHRU */
8120
8121 /* e11 id
8122 | constant */
8123 case T_ID:
8124 if (!intel_parser.in_offset && intel_parser.is_mem <= 0)
8125 {
8126 symbolS *symbolP;
8127
8128 /* The identifier represents a memory reference only if it's not
8129 preceded by an offset modifier and if it's not an equate. */
8130 symbolP = symbol_find(cur_token.str);
8131 if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
8132 intel_parser.is_mem = 1;
8133 }
8134 /* FALLTHRU */
8135
8136 case T_CONST:
8137 case '-':
8138 case '+':
8139 {
8140 char *save_str, sign = 0;
8141
8142 /* Allow constants that start with `+' or `-'. */
8143 if (cur_token.code == '-' || cur_token.code == '+')
8144 {
8145 sign = cur_token.code;
8146 intel_match_token (cur_token.code);
8147 if (cur_token.code != T_CONST)
8148 {
8149 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
8150 cur_token.str);
8151 return 0;
8152 }
8153 }
8154
8155 save_str = (char *) xmalloc (strlen (cur_token.str) + 2);
8156 strcpy (save_str + !!sign, cur_token.str);
8157 if (sign)
8158 *save_str = sign;
8159
8160 /* Get the next token to check for register scaling. */
8161 intel_match_token (cur_token.code);
8162
8163 /* Check if this constant is a scaling factor for an
8164 index register. */
8165 if (cur_token.code == '*')
8166 {
8167 if (intel_match_token ('*') && cur_token.code == T_REG)
8168 {
8169 const reg_entry *reg = cur_token.reg;
8170
8171 if (!intel_parser.in_bracket)
8172 {
8173 as_bad (_("Register scaling only allowed "
8174 "in memory operands"));
8175 return 0;
8176 }
8177
8178 /* Disallow things like [1*si].
8179 sp and esp are invalid as index. */
8180 if (reg->reg_type.bitfield.reg16)
8181 reg = i386_regtab + REGNAM_AX + 4;
8182 else if (i.index_reg)
8183 reg = i386_regtab + REGNAM_EAX + 4;
8184
8185 /* The constant is followed by `* reg', so it must be
8186 a valid scale. */
8187 i.index_reg = reg;
8188 i.types[this_operand].bitfield.baseindex = 1;
8189
8190 /* Set the scale after setting the register (otherwise,
8191 i386_scale will complain) */
8192 if (!i386_scale (save_str))
8193 return 0;
8194 intel_match_token (T_REG);
8195
8196 /* Since registers are not part of the displacement
8197 string, we may need to remove any preceding '+' from
8198 the displacement string. */
8199 if (*intel_parser.disp != '\0')
8200 {
8201 char *s = intel_parser.disp;
8202 s += strlen (s) - 1;
8203 if (*s == '+')
8204 *s = '\0';
8205 }
8206
8207 free (save_str);
8208
8209 return 1;
8210 }
8211
8212 /* The constant was not used for register scaling. Since we have
8213 already consumed the token following `*' we now need to put it
8214 back in the stream. */
8215 intel_putback_token ();
8216 }
8217
8218 /* Add the constant to the displacement string. */
8219 strcat (intel_parser.disp, save_str);
8220 free (save_str);
8221
8222 return 1;
8223 }
8224 }
8225
8226 as_bad (_("Unrecognized token '%s'"), cur_token.str);
8227 return 0;
8228 }
8229
8230 /* Match the given token against cur_token. If they match, read the next
8231 token from the operand string. */
8232 static int
8233 intel_match_token (int code)
8234 {
8235 if (cur_token.code == code)
8236 {
8237 intel_get_token ();
8238 return 1;
8239 }
8240 else
8241 {
8242 as_bad (_("Unexpected token `%s'"), cur_token.str);
8243 return 0;
8244 }
8245 }
8246
8247 /* Read a new token from intel_parser.op_string and store it in cur_token. */
8248 static void
8249 intel_get_token (void)
8250 {
8251 char *end_op;
8252 const reg_entry *reg;
8253 struct intel_token new_token;
8254
8255 new_token.code = T_NIL;
8256 new_token.reg = NULL;
8257 new_token.str = NULL;
8258
8259 /* Free the memory allocated to the previous token and move
8260 cur_token to prev_token. */
8261 if (prev_token.str)
8262 free (prev_token.str);
8263
8264 prev_token = cur_token;
8265
8266 /* Skip whitespace. */
8267 while (is_space_char (*intel_parser.op_string))
8268 intel_parser.op_string++;
8269
8270 /* Return an empty token if we find nothing else on the line. */
8271 if (*intel_parser.op_string == '\0')
8272 {
8273 cur_token = new_token;
8274 return;
8275 }
8276
8277 /* The new token cannot be larger than the remainder of the operand
8278 string. */
8279 new_token.str = (char *) xmalloc (strlen (intel_parser.op_string) + 1);
8280 new_token.str[0] = '\0';
8281
8282 if (strchr ("0123456789", *intel_parser.op_string))
8283 {
8284 char *p = new_token.str;
8285 char *q = intel_parser.op_string;
8286 new_token.code = T_CONST;
8287
8288 /* Allow any kind of identifier char to encompass floating point and
8289 hexadecimal numbers. */
8290 while (is_identifier_char (*q))
8291 *p++ = *q++;
8292 *p = '\0';
8293
8294 /* Recognize special symbol names [0-9][bf]. */
8295 if (strlen (intel_parser.op_string) == 2
8296 && (intel_parser.op_string[1] == 'b'
8297 || intel_parser.op_string[1] == 'f'))
8298 new_token.code = T_ID;
8299 }
8300
8301 else if ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL)
8302 {
8303 size_t len = end_op - intel_parser.op_string;
8304
8305 new_token.code = T_REG;
8306 new_token.reg = reg;
8307
8308 memcpy (new_token.str, intel_parser.op_string, len);
8309 new_token.str[len] = '\0';
8310 }
8311
8312 else if (is_identifier_char (*intel_parser.op_string))
8313 {
8314 char *p = new_token.str;
8315 char *q = intel_parser.op_string;
8316
8317 /* A '.' or '$' followed by an identifier char is an identifier.
8318 Otherwise, it's operator '.' followed by an expression. */
8319 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
8320 {
8321 new_token.code = '.';
8322 new_token.str[0] = '.';
8323 new_token.str[1] = '\0';
8324 }
8325 else
8326 {
8327 while (is_identifier_char (*q) || *q == '@')
8328 *p++ = *q++;
8329 *p = '\0';
8330
8331 if (strcasecmp (new_token.str, "NOT") == 0)
8332 new_token.code = '~';
8333
8334 else if (strcasecmp (new_token.str, "MOD") == 0)
8335 new_token.code = '%';
8336
8337 else if (strcasecmp (new_token.str, "AND") == 0)
8338 new_token.code = '&';
8339
8340 else if (strcasecmp (new_token.str, "OR") == 0)
8341 new_token.code = '|';
8342
8343 else if (strcasecmp (new_token.str, "XOR") == 0)
8344 new_token.code = '^';
8345
8346 else if (strcasecmp (new_token.str, "SHL") == 0)
8347 new_token.code = T_SHL;
8348
8349 else if (strcasecmp (new_token.str, "SHR") == 0)
8350 new_token.code = T_SHR;
8351
8352 else if (strcasecmp (new_token.str, "BYTE") == 0)
8353 new_token.code = T_BYTE;
8354
8355 else if (strcasecmp (new_token.str, "WORD") == 0)
8356 new_token.code = T_WORD;
8357
8358 else if (strcasecmp (new_token.str, "DWORD") == 0)
8359 new_token.code = T_DWORD;
8360
8361 else if (strcasecmp (new_token.str, "FWORD") == 0)
8362 new_token.code = T_FWORD;
8363
8364 else if (strcasecmp (new_token.str, "QWORD") == 0)
8365 new_token.code = T_QWORD;
8366
8367 else if (strcasecmp (new_token.str, "TBYTE") == 0
8368 /* XXX remove (gcc still uses it) */
8369 || strcasecmp (new_token.str, "XWORD") == 0)
8370 new_token.code = T_TBYTE;
8371
8372 else if (strcasecmp (new_token.str, "XMMWORD") == 0
8373 || strcasecmp (new_token.str, "OWORD") == 0)
8374 new_token.code = T_XMMWORD;
8375
8376 else if (strcasecmp (new_token.str, "PTR") == 0)
8377 new_token.code = T_PTR;
8378
8379 else if (strcasecmp (new_token.str, "SHORT") == 0)
8380 new_token.code = T_SHORT;
8381
8382 else if (strcasecmp (new_token.str, "OFFSET") == 0)
8383 {
8384 new_token.code = T_OFFSET;
8385
8386 /* ??? This is not mentioned in the MASM grammar but gcc
8387 makes use of it with -mintel-syntax. OFFSET may be
8388 followed by FLAT: */
8389 if (strncasecmp (q, " FLAT:", 6) == 0)
8390 strcat (new_token.str, " FLAT:");
8391 }
8392
8393 /* ??? This is not mentioned in the MASM grammar. */
8394 else if (strcasecmp (new_token.str, "FLAT") == 0)
8395 {
8396 new_token.code = T_OFFSET;
8397 if (*q == ':')
8398 strcat (new_token.str, ":");
8399 else
8400 as_bad (_("`:' expected"));
8401 }
8402
8403 else
8404 new_token.code = T_ID;
8405 }
8406 }
8407
8408 else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
8409 {
8410 new_token.code = *intel_parser.op_string;
8411 new_token.str[0] = *intel_parser.op_string;
8412 new_token.str[1] = '\0';
8413 }
8414
8415 else if (strchr ("<>", *intel_parser.op_string)
8416 && *intel_parser.op_string == *(intel_parser.op_string + 1))
8417 {
8418 new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
8419 new_token.str[0] = *intel_parser.op_string;
8420 new_token.str[1] = *intel_parser.op_string;
8421 new_token.str[2] = '\0';
8422 }
8423
8424 else
8425 as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
8426
8427 intel_parser.op_string += strlen (new_token.str);
8428 cur_token = new_token;
8429 }
8430
8431 /* Put cur_token back into the token stream and make cur_token point to
8432 prev_token. */
8433 static void
8434 intel_putback_token (void)
8435 {
8436 if (cur_token.code != T_NIL)
8437 {
8438 intel_parser.op_string -= strlen (cur_token.str);
8439 free (cur_token.str);
8440 }
8441 cur_token = prev_token;
8442
8443 /* Forget prev_token. */
8444 prev_token.code = T_NIL;
8445 prev_token.reg = NULL;
8446 prev_token.str = NULL;
8447 }
8448
8449 int
8450 tc_x86_regname_to_dw2regnum (char *regname)
8451 {
8452 unsigned int regnum;
8453 unsigned int regnames_count;
8454 static const char *const regnames_32[] =
8455 {
8456 "eax", "ecx", "edx", "ebx",
8457 "esp", "ebp", "esi", "edi",
8458 "eip", "eflags", NULL,
8459 "st0", "st1", "st2", "st3",
8460 "st4", "st5", "st6", "st7",
8461 NULL, NULL,
8462 "xmm0", "xmm1", "xmm2", "xmm3",
8463 "xmm4", "xmm5", "xmm6", "xmm7",
8464 "mm0", "mm1", "mm2", "mm3",
8465 "mm4", "mm5", "mm6", "mm7",
8466 "fcw", "fsw", "mxcsr",
8467 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8468 "tr", "ldtr"
8469 };
8470 static const char *const regnames_64[] =
8471 {
8472 "rax", "rdx", "rcx", "rbx",
8473 "rsi", "rdi", "rbp", "rsp",
8474 "r8", "r9", "r10", "r11",
8475 "r12", "r13", "r14", "r15",
8476 "rip",
8477 "xmm0", "xmm1", "xmm2", "xmm3",
8478 "xmm4", "xmm5", "xmm6", "xmm7",
8479 "xmm8", "xmm9", "xmm10", "xmm11",
8480 "xmm12", "xmm13", "xmm14", "xmm15",
8481 "st0", "st1", "st2", "st3",
8482 "st4", "st5", "st6", "st7",
8483 "mm0", "mm1", "mm2", "mm3",
8484 "mm4", "mm5", "mm6", "mm7",
8485 "rflags",
8486 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8487 "fs.base", "gs.base", NULL, NULL,
8488 "tr", "ldtr",
8489 "mxcsr", "fcw", "fsw"
8490 };
8491 const char *const *regnames;
8492
8493 if (flag_code == CODE_64BIT)
8494 {
8495 regnames = regnames_64;
8496 regnames_count = ARRAY_SIZE (regnames_64);
8497 }
8498 else
8499 {
8500 regnames = regnames_32;
8501 regnames_count = ARRAY_SIZE (regnames_32);
8502 }
8503
8504 for (regnum = 0; regnum < regnames_count; regnum++)
8505 if (regnames[regnum] != NULL
8506 && strcmp (regname, regnames[regnum]) == 0)
8507 return regnum;
8508
8509 return -1;
8510 }
8511
8512 void
8513 tc_x86_frame_initial_instructions (void)
8514 {
8515 static unsigned int sp_regno;
8516
8517 if (!sp_regno)
8518 sp_regno = tc_x86_regname_to_dw2regnum (flag_code == CODE_64BIT
8519 ? "rsp" : "esp");
8520
8521 cfi_add_CFA_def_cfa (sp_regno, -x86_cie_data_alignment);
8522 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
8523 }
8524
8525 int
8526 i386_elf_section_type (const char *str, size_t len)
8527 {
8528 if (flag_code == CODE_64BIT
8529 && len == sizeof ("unwind") - 1
8530 && strncmp (str, "unwind", 6) == 0)
8531 return SHT_X86_64_UNWIND;
8532
8533 return -1;
8534 }
8535
8536 #ifdef TE_PE
8537 void
8538 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
8539 {
8540 expressionS expr;
8541
8542 expr.X_op = O_secrel;
8543 expr.X_add_symbol = symbol;
8544 expr.X_add_number = 0;
8545 emit_expr (&expr, size);
8546 }
8547 #endif
8548
8549 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8550 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
8551
8552 int
8553 x86_64_section_letter (int letter, char **ptr_msg)
8554 {
8555 if (flag_code == CODE_64BIT)
8556 {
8557 if (letter == 'l')
8558 return SHF_X86_64_LARGE;
8559
8560 *ptr_msg = _("Bad .section directive: want a,l,w,x,M,S,G,T in string");
8561 }
8562 else
8563 *ptr_msg = _("Bad .section directive: want a,w,x,M,S,G,T in string");
8564 return -1;
8565 }
8566
8567 int
8568 x86_64_section_word (char *str, size_t len)
8569 {
8570 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
8571 return SHF_X86_64_LARGE;
8572
8573 return -1;
8574 }
8575
8576 static void
8577 handle_large_common (int small ATTRIBUTE_UNUSED)
8578 {
8579 if (flag_code != CODE_64BIT)
8580 {
8581 s_comm_internal (0, elf_common_parse);
8582 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
8583 }
8584 else
8585 {
8586 static segT lbss_section;
8587 asection *saved_com_section_ptr = elf_com_section_ptr;
8588 asection *saved_bss_section = bss_section;
8589
8590 if (lbss_section == NULL)
8591 {
8592 flagword applicable;
8593 segT seg = now_seg;
8594 subsegT subseg = now_subseg;
8595
8596 /* The .lbss section is for local .largecomm symbols. */
8597 lbss_section = subseg_new (".lbss", 0);
8598 applicable = bfd_applicable_section_flags (stdoutput);
8599 bfd_set_section_flags (stdoutput, lbss_section,
8600 applicable & SEC_ALLOC);
8601 seg_info (lbss_section)->bss = 1;
8602
8603 subseg_set (seg, subseg);
8604 }
8605
8606 elf_com_section_ptr = &_bfd_elf_large_com_section;
8607 bss_section = lbss_section;
8608
8609 s_comm_internal (0, elf_common_parse);
8610
8611 elf_com_section_ptr = saved_com_section_ptr;
8612 bss_section = saved_bss_section;
8613 }
8614 }
8615 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 0.211126 seconds and 5 git commands to generate.