Set OSABI field back to 0 (SysV), to avoid interoperability problems
[nacl-binutils.git] / gas / config / tc-i386.c
blobcccfdfcc1b8387abd4839af0205acec9f5d0babe
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.
6 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
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. */
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"
37 #ifndef REGISTER_WARNINGS
38 #define REGISTER_WARNINGS 1
39 #endif
41 #ifndef INFER_ADDR_PREFIX
42 #define INFER_ADDR_PREFIX 1
43 #endif
45 #ifndef SCALE1_WHEN_NO_INDEX
46 /* Specifying a scale factor besides 1 when there is no index is
47 futile. eg. `mov (%ebx,2),%al' does exactly the same as
48 `mov (%ebx),%al'. To slavishly follow what the programmer
49 specified, set SCALE1_WHEN_NO_INDEX to 0. */
50 #define SCALE1_WHEN_NO_INDEX 1
51 #endif
53 #ifndef DEFAULT_ARCH
54 #define DEFAULT_ARCH "i386"
55 #endif
57 #ifndef INLINE
58 #if __GNUC__ >= 2
59 #define INLINE __inline__
60 #else
61 #define INLINE
62 #endif
63 #endif
65 static void set_code_flag (int);
66 static void set_16bit_gcc_code_flag (int);
67 static void set_intel_syntax (int);
68 static void set_cpu_arch (int);
69 #ifdef TE_PE
70 static void pe_directive_secrel (int);
71 #endif
72 static void signed_cons (int);
73 static char *output_invalid (int c);
74 static int i386_operand (char *);
75 static int i386_intel_operand (char *, int);
76 static const reg_entry *parse_register (char *, char **);
77 static char *parse_insn (char *, char *);
78 static char *parse_operands (char *, const char *);
79 static void swap_operands (void);
80 static void swap_2_operands (int, int);
81 static void optimize_imm (void);
82 static void optimize_disp (void);
83 static int match_template (void);
84 static int check_string (void);
85 static int process_suffix (void);
86 static int check_byte_reg (void);
87 static int check_long_reg (void);
88 static int check_qword_reg (void);
89 static int check_word_reg (void);
90 static int finalize_imm (void);
91 static int process_operands (void);
92 static const seg_entry *build_modrm_byte (void);
93 static void output_insn (void);
94 static void output_imm (fragS *, offsetT);
95 static void output_disp (fragS *, offsetT);
96 #ifndef I386COFF
97 static void s_bss (int);
98 #endif
99 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
100 static void handle_large_common (int small ATTRIBUTE_UNUSED);
101 #endif
102 static void nativeclient_symbol_init (void);
104 static const char *default_arch = DEFAULT_ARCH;
106 /* 'md_assemble ()' gathers together information and puts it into a
107 i386_insn. */
109 union i386_op
111 expressionS *disps;
112 expressionS *imms;
113 const reg_entry *regs;
116 struct _i386_insn
118 /* TM holds the template for the insn were currently assembling. */
119 template tm;
121 /* SUFFIX holds the instruction mnemonic suffix if given.
122 (e.g. 'l' for 'movl') */
123 char suffix;
125 /* OPERANDS gives the number of given operands. */
126 unsigned int operands;
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;
133 /* TYPES [i] is the type (see above #defines) which tells us how to
134 use OP[i] for the corresponding operand. */
135 unsigned int types[MAX_OPERANDS];
137 /* Displacement expression, immediate expression, or register for each
138 operand. */
139 union i386_op op[MAX_OPERANDS];
141 /* Flags for operands. */
142 unsigned int flags[MAX_OPERANDS];
143 #define Operand_PCrel 1
145 /* Relocation type for operand */
146 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
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;
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];
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];
163 /* RM and SIB are the modrm byte and the sib byte where the
164 addressing modes of this insn are encoded. */
166 modrm_byte rm;
167 rex_byte rex;
168 sib_byte sib;
171 typedef struct _i386_insn i386_insn;
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
178 #endif
179 #ifdef LEX_QM
181 #endif
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 '\\'
198 #else
199 const char *i386_comment_chars = "#";
200 #define PREFIX_SEPARATOR '/'
201 #endif
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[] = "#/";
213 const char line_separator_chars[] = ";";
215 /* Chars that can be used to separate mant from exp in floating point
216 nums. */
217 const char EXP_CHARS[] = "eE";
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";
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];
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])
239 /* All non-digit non-letter characters that may occur in an operand. */
240 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
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)
253 /* The instruction we're assembling. */
254 static i386_insn i;
256 /* Possible templates for current insn. */
257 static const templates *current_templates;
259 /* Per instruction expressionS buffers: max displacements & immediates. */
260 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
261 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
263 /* Current operand we are working on. */
264 static int this_operand;
266 /* We support four different modes. FLAG_CODE variable is used to distinguish
267 these. */
269 enum flag_code {
270 CODE_32BIT,
271 CODE_16BIT,
272 CODE_64BIT };
273 #define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
275 static enum flag_code flag_code;
276 static unsigned int object_64bit;
277 static int use_rela_relocations = 0;
279 /* The names used to print error messages. */
280 static const char *flag_code_names[] =
282 "32",
283 "16",
284 "64"
287 /* 1 for intel syntax,
288 0 if att syntax. */
289 static int intel_syntax = 0;
291 /* 1 if register prefix % not required. */
292 static int allow_naked_reg = 0;
294 /* Register prefix used for error message. */
295 static const char *register_prefix = "%";
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';
302 /* Non-zero to optimize code alignment. */
303 int optimize_align_code = 1;
305 /* Non-zero to quieten some warnings. */
306 static int quiet_warnings = 0;
308 /* CPU name. */
309 static const char *cpu_arch_name = NULL;
310 static const char *cpu_sub_arch_name = NULL;
312 /* CPU feature flags. */
313 static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
315 /* If we have selected a cpu we are generating instructions for. */
316 static int cpu_arch_tune_set = 0;
318 /* Cpu we are generating instructions for. */
319 static enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
321 /* CPU feature flags of cpu we are generating instructions for. */
322 static unsigned int cpu_arch_tune_flags = 0;
324 /* CPU instruction set architecture used. */
325 static enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
327 /* CPU feature flags of instruction set architecture used. */
328 static unsigned int cpu_arch_isa_flags = 0;
330 /* If set, conditional jumps are not automatically promoted to handle
331 larger than a byte offset. */
332 static unsigned int no_cond_jump_promotion = 0;
334 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
335 static symbolS *GOT_symbol;
337 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
338 unsigned int x86_dwarf2_return_column;
340 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
341 int x86_cie_data_alignment;
343 /* NativeClient support */
345 /* Default alignment. 0=OFF */
346 int nacl_alignment = NACL_ALIGN_POW2;
348 /* Use library mode. 0=OFF */
349 int nacl_library_mode = 0;
351 /* Interface to relax_segment.
352 There are 3 major relax states for 386 jump insns because the
353 different types of jumps add different sizes to frags when we're
354 figuring out what sort of jump to choose to reach a given label. */
356 /* Types. */
357 #define UNCOND_JUMP 0
358 #define COND_JUMP 1
359 #define COND_JUMP86 2
361 /* Sizes. */
362 #define CODE16 1
363 #define SMALL 0
364 #define SMALL16 (SMALL | CODE16)
365 #define BIG 2
366 #define BIG16 (BIG | CODE16)
368 #ifndef INLINE
369 #ifdef __GNUC__
370 #define INLINE __inline__
371 #else
372 #define INLINE
373 #endif
374 #endif
376 #define ENCODE_RELAX_STATE(type, size) \
377 ((relax_substateT) (((type) << 2) | (size)))
378 #define TYPE_FROM_RELAX_STATE(s) \
379 ((s) >> 2)
380 #define DISP_SIZE_FROM_RELAX_STATE(s) \
381 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
383 /* This table is used by relax_frag to promote short jumps to long
384 ones where necessary. SMALL (short) jumps may be promoted to BIG
385 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
386 don't allow a short jump in a 32 bit code segment to be promoted to
387 a 16 bit offset jump because it's slower (requires data size
388 prefix), and doesn't work, unless the destination is in the bottom
389 64k of the code segment (The top 16 bits of eip are zeroed). */
391 const relax_typeS md_relax_table[] =
393 /* The fields are:
394 1) most positive reach of this state,
395 2) most negative reach of this state,
396 3) how many bytes this mode will have in the variable part of the frag
397 4) which index into the table to try if we can't fit into this one. */
399 /* UNCOND_JUMP states. */
400 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
401 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
402 /* dword jmp adds 4 bytes to frag:
403 0 extra opcode bytes, 4 displacement bytes. */
404 {0, 0, 4, 0},
405 /* word jmp adds 2 byte2 to frag:
406 0 extra opcode bytes, 2 displacement bytes. */
407 {0, 0, 2, 0},
409 /* COND_JUMP states. */
410 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
411 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
412 /* dword conditionals adds 5 bytes to frag:
413 1 extra opcode byte, 4 displacement bytes. */
414 {0, 0, 5, 0},
415 /* word conditionals add 3 bytes to frag:
416 1 extra opcode byte, 2 displacement bytes. */
417 {0, 0, 3, 0},
419 /* COND_JUMP86 states. */
420 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
421 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
422 /* dword conditionals adds 5 bytes to frag:
423 1 extra opcode byte, 4 displacement bytes. */
424 {0, 0, 5, 0},
425 /* word conditionals add 4 bytes to frag:
426 1 displacement byte and a 3 byte long branch insn. */
427 {0, 0, 4, 0}
430 static const arch_entry cpu_arch[] =
432 {"generic32", PROCESSOR_GENERIC32,
433 Cpu186|Cpu286|Cpu386},
434 {"generic64", PROCESSOR_GENERIC64,
435 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
436 |CpuMMX2|CpuSSE|CpuSSE2},
437 {"i8086", PROCESSOR_UNKNOWN,
439 {"i186", PROCESSOR_UNKNOWN,
440 Cpu186},
441 {"i286", PROCESSOR_UNKNOWN,
442 Cpu186|Cpu286},
443 {"i386", PROCESSOR_I386,
444 Cpu186|Cpu286|Cpu386},
445 {"i486", PROCESSOR_I486,
446 Cpu186|Cpu286|Cpu386|Cpu486},
447 {"i586", PROCESSOR_PENTIUM,
448 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586},
449 {"i686", PROCESSOR_PENTIUMPRO,
450 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686},
451 {"pentium", PROCESSOR_PENTIUM,
452 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586},
453 {"pentiumpro",PROCESSOR_PENTIUMPRO,
454 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686},
455 {"pentiumii", PROCESSOR_PENTIUMPRO,
456 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX},
457 {"pentiumiii",PROCESSOR_PENTIUMPRO,
458 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuMMX2|CpuSSE},
459 {"pentium4", PROCESSOR_PENTIUM4,
460 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
461 |CpuMMX2|CpuSSE|CpuSSE2},
462 {"prescott", PROCESSOR_NOCONA,
463 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
464 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3},
465 {"nocona", PROCESSOR_NOCONA,
466 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
467 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3},
468 {"yonah", PROCESSOR_CORE,
469 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
470 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3},
471 {"core", PROCESSOR_CORE,
472 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
473 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3},
474 {"merom", PROCESSOR_CORE2,
475 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
476 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3},
477 {"core2", PROCESSOR_CORE2,
478 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX
479 |CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3},
480 {"k6", PROCESSOR_K6,
481 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX},
482 {"k6_2", PROCESSOR_K6,
483 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow},
484 {"athlon", PROCESSOR_ATHLON,
485 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6
486 |CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA},
487 {"sledgehammer", PROCESSOR_K8,
488 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6
489 |CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2},
490 {"opteron", PROCESSOR_K8,
491 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6
492 |CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2},
493 {"k8", PROCESSOR_K8,
494 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6
495 |CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2},
496 {"amdfam10", PROCESSOR_AMDFAM10,
497 Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuSledgehammer
498 |CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a
499 |CpuABM},
500 {".mmx", PROCESSOR_UNKNOWN,
501 CpuMMX},
502 {".sse", PROCESSOR_UNKNOWN,
503 CpuMMX|CpuMMX2|CpuSSE},
504 {".sse2", PROCESSOR_UNKNOWN,
505 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2},
506 {".sse3", PROCESSOR_UNKNOWN,
507 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3},
508 {".ssse3", PROCESSOR_UNKNOWN,
509 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3},
510 {".sse4.1", PROCESSOR_UNKNOWN,
511 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1},
512 {".sse4.2", PROCESSOR_UNKNOWN,
513 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4},
514 {".sse4", PROCESSOR_UNKNOWN,
515 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4},
516 {".3dnow", PROCESSOR_UNKNOWN,
517 CpuMMX|Cpu3dnow},
518 {".3dnowa", PROCESSOR_UNKNOWN,
519 CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA},
520 {".padlock", PROCESSOR_UNKNOWN,
521 CpuPadLock},
522 {".pacifica", PROCESSOR_UNKNOWN,
523 CpuSVME},
524 {".svme", PROCESSOR_UNKNOWN,
525 CpuSVME},
526 {".sse4a", PROCESSOR_UNKNOWN,
527 CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a},
528 {".abm", PROCESSOR_UNKNOWN,
529 CpuABM}
532 const pseudo_typeS md_pseudo_table[] =
534 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
535 {"align", s_align_bytes, 0},
536 #else
537 {"align", s_align_ptwo, 0},
538 #endif
539 {"arch", set_cpu_arch, 0},
540 #ifndef I386COFF
541 {"bss", s_bss, 0},
542 #endif
543 {"ffloat", float_cons, 'f'},
544 {"dfloat", float_cons, 'd'},
545 {"tfloat", float_cons, 'x'},
546 {"value", cons, 2},
547 {"slong", signed_cons, 4},
548 {"noopt", s_ignore, 0},
549 {"optim", s_ignore, 0},
550 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
551 {"code16", set_code_flag, CODE_16BIT},
552 {"code32", set_code_flag, CODE_32BIT},
553 {"code64", set_code_flag, CODE_64BIT},
554 {"intel_syntax", set_intel_syntax, 1},
555 {"att_syntax", set_intel_syntax, 0},
556 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
557 {"largecomm", handle_large_common, 0},
558 #else
559 {"file", (void (*) (int)) dwarf2_directive_file, 0},
560 {"loc", dwarf2_directive_loc, 0},
561 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
562 #endif
563 #ifdef TE_PE
564 {"secrel32", pe_directive_secrel, 0},
565 #endif
566 {0, 0, 0}
569 /* For interface with expression (). */
570 extern char *input_line_pointer;
572 /* Hash table for instruction mnemonic lookup. */
573 static struct hash_control *op_hash;
575 /* Hash table for register lookup. */
576 static struct hash_control *reg_hash;
578 void
579 i386_align_code (fragS *fragP, int count)
581 /* Various efficient no-op patterns for aligning code labels.
582 Note: Don't try to assemble the instructions in the comments.
583 0L and 0w are not legal. */
584 static const char f32_1[] =
585 {0x90}; /* nop */
586 static const char f32_2[] =
587 {0x66,0x90}; /* xchg %ax,%ax */
588 static const char f32_3[] =
589 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
590 static const char f32_4[] =
591 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
592 static const char f32_5[] =
593 {0x90, /* nop */
594 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
595 static const char f32_6[] =
596 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
597 static const char f32_7[] =
598 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
599 static const char f32_8[] =
600 {0x90, /* nop */
601 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
602 static const char f32_9[] =
603 {0x89,0xf6, /* movl %esi,%esi */
604 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
605 static const char f32_10[] =
606 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
607 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
608 static const char f32_11[] =
609 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
610 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
611 static const char f32_12[] =
612 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
613 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
614 static const char f32_13[] =
615 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
616 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
617 static const char f32_14[] =
618 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
619 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
620 static const char f16_3[] =
621 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
622 static const char f16_4[] =
623 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
624 static const char f16_5[] =
625 {0x90, /* nop */
626 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
627 static const char f16_6[] =
628 {0x89,0xf6, /* mov %si,%si */
629 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
630 static const char f16_7[] =
631 {0x8d,0x74,0x00, /* lea 0(%si),%si */
632 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
633 static const char f16_8[] =
634 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
635 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
636 static const char jump_31[] =
637 {0xeb,0x1d,0x90,0x90,0x90,0x90,0x90, /* jmp .+31; lotsa nops */
638 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
639 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
640 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
641 static const char *const f32_patt[] = {
642 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
643 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14
645 static const char *const f16_patt[] = {
646 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8
648 /* nopl (%[re]ax) */
649 static const char alt_3[] =
650 {0x0f,0x1f,0x00};
651 /* nopl 0(%[re]ax) */
652 static const char alt_4[] =
653 {0x0f,0x1f,0x40,0x00};
654 /* nopl 0(%[re]ax,%[re]ax,1) */
655 static const char alt_5[] =
656 {0x0f,0x1f,0x44,0x00,0x00};
657 /* nopw 0(%[re]ax,%[re]ax,1) */
658 static const char alt_6[] =
659 {0x66,0x0f,0x1f,0x44,0x00,0x00};
660 /* nopl 0L(%[re]ax) */
661 static const char alt_7[] =
662 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
663 /* nopl 0L(%[re]ax,%[re]ax,1) */
664 static const char alt_8[] =
665 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
666 /* nopw 0L(%[re]ax,%[re]ax,1) */
667 static const char alt_9[] =
668 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
669 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
670 static const char alt_10[] =
671 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
672 /* data16
673 nopw %cs:0L(%[re]ax,%[re]ax,1) */
674 static const char alt_long_11[] =
675 {0x66,
676 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
677 /* data16
678 data16
679 nopw %cs:0L(%[re]ax,%[re]ax,1) */
680 static const char alt_long_12[] =
681 {0x66,
682 0x66,
683 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
684 /* data16
685 data16
686 data16
687 nopw %cs:0L(%[re]ax,%[re]ax,1) */
688 static const char alt_long_13[] =
689 {0x66,
690 0x66,
691 0x66,
692 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
693 /* data16
694 data16
695 data16
696 data16
697 nopw %cs:0L(%[re]ax,%[re]ax,1) */
698 static const char alt_long_14[] =
699 {0x66,
700 0x66,
701 0x66,
702 0x66,
703 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
704 /* data16
705 data16
706 data16
707 data16
708 data16
709 nopw %cs:0L(%[re]ax,%[re]ax,1) */
710 static const char alt_long_15[] =
711 {0x66,
712 0x66,
713 0x66,
714 0x66,
715 0x66,
716 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
717 /* nopl 0(%[re]ax,%[re]ax,1)
718 nopw 0(%[re]ax,%[re]ax,1) */
719 static const char alt_short_11[] =
720 {0x0f,0x1f,0x44,0x00,0x00,
721 0x66,0x0f,0x1f,0x44,0x00,0x00};
722 /* nopw 0(%[re]ax,%[re]ax,1)
723 nopw 0(%[re]ax,%[re]ax,1) */
724 static const char alt_short_12[] =
725 {0x66,0x0f,0x1f,0x44,0x00,0x00,
726 0x66,0x0f,0x1f,0x44,0x00,0x00};
727 /* nopw 0(%[re]ax,%[re]ax,1)
728 nopl 0L(%[re]ax) */
729 static const char alt_short_13[] =
730 {0x66,0x0f,0x1f,0x44,0x00,0x00,
731 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
732 /* nopl 0L(%[re]ax)
733 nopl 0L(%[re]ax) */
734 static const char alt_short_14[] =
735 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
736 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
737 /* nopl 0L(%[re]ax)
738 nopl 0L(%[re]ax,%[re]ax,1) */
739 static const char alt_short_15[] =
740 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
741 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
742 static const char *const alt_short_patt[] = {
743 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
744 alt_9, alt_10, alt_short_11, alt_short_12, alt_short_13,
745 alt_short_14, alt_short_15
747 static const char *const alt_long_patt[] = {
748 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
749 alt_9, alt_10, alt_long_11, alt_long_12, alt_long_13,
750 alt_long_14, alt_long_15
753 /* Only align for at least a positive non-zero boundary. */
754 if (count <= 0 || count > MAX_MEM_FOR_RS_ALIGN_CODE)
755 return;
757 /* We need to decide which NOP sequence to use for 32bit and
758 64bit. When -mtune= is used:
760 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
761 PROCESSOR_GENERIC32, f32_patt will be used.
762 2. For PROCESSOR_PENTIUMPRO, PROCESSOR_PENTIUM4, PROCESSOR_NOCONA,
763 PROCESSOR_CORE, PROCESSOR_CORE2, and PROCESSOR_GENERIC64,
764 alt_long_patt will be used.
765 3. For PROCESSOR_ATHLON, PROCESSOR_K6, PROCESSOR_K8 and
766 PROCESSOR_AMDFAM10, alt_short_patt will be used.
768 When -mtune= isn't used, alt_long_patt will be used if
769 cpu_arch_isa_flags has Cpu686. Otherwise, f32_patt will
770 be used.
772 When -march= or .arch is used, we can't use anything beyond
773 cpu_arch_isa_flags. */
775 if (flag_code == CODE_16BIT)
777 if (count > 8)
779 memcpy (fragP->fr_literal + fragP->fr_fix,
780 jump_31, count);
781 /* Adjust jump offset. */
782 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
784 else
785 memcpy (fragP->fr_literal + fragP->fr_fix,
786 f16_patt[count - 1], count);
788 else
790 const char *const *patt = NULL;
792 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
794 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
795 switch (cpu_arch_tune)
797 case PROCESSOR_UNKNOWN:
798 /* We use cpu_arch_isa_flags to check if we SHOULD
799 optimize for Cpu686. */
800 if ((cpu_arch_isa_flags & Cpu686) != 0)
801 patt = alt_long_patt;
802 else
803 patt = f32_patt;
804 break;
805 case PROCESSOR_PENTIUMPRO:
806 case PROCESSOR_PENTIUM4:
807 case PROCESSOR_NOCONA:
808 case PROCESSOR_CORE:
809 case PROCESSOR_CORE2:
810 case PROCESSOR_GENERIC64:
811 patt = alt_long_patt;
812 break;
813 case PROCESSOR_K6:
814 case PROCESSOR_ATHLON:
815 case PROCESSOR_K8:
816 case PROCESSOR_AMDFAM10:
817 patt = alt_short_patt;
818 break;
819 case PROCESSOR_I386:
820 case PROCESSOR_I486:
821 case PROCESSOR_PENTIUM:
822 case PROCESSOR_GENERIC32:
823 patt = f32_patt;
824 break;
827 else
829 switch (cpu_arch_tune)
831 case PROCESSOR_UNKNOWN:
832 /* When cpu_arch_isa is net, cpu_arch_tune shouldn't be
833 PROCESSOR_UNKNOWN. */
834 abort ();
835 break;
837 case PROCESSOR_I386:
838 case PROCESSOR_I486:
839 case PROCESSOR_PENTIUM:
840 case PROCESSOR_K6:
841 case PROCESSOR_ATHLON:
842 case PROCESSOR_K8:
843 case PROCESSOR_AMDFAM10:
844 case PROCESSOR_GENERIC32:
845 /* We use cpu_arch_isa_flags to check if we CAN optimize
846 for Cpu686. */
847 if ((cpu_arch_isa_flags & Cpu686) != 0)
848 patt = alt_short_patt;
849 else
850 patt = f32_patt;
851 break;
852 case PROCESSOR_PENTIUMPRO:
853 case PROCESSOR_PENTIUM4:
854 case PROCESSOR_NOCONA:
855 case PROCESSOR_CORE:
856 case PROCESSOR_CORE2:
857 if ((cpu_arch_isa_flags & Cpu686) != 0)
858 patt = alt_long_patt;
859 else
860 patt = f32_patt;
861 break;
862 case PROCESSOR_GENERIC64:
863 patt = alt_long_patt;
864 break;
868 if (patt == f32_patt)
870 /* If the padding is less than 15 bytes, we use the normal
871 ones. Otherwise, we use a jump instruction and adjust
872 its offset. */
873 if (count < 15)
874 memcpy (fragP->fr_literal + fragP->fr_fix,
875 patt[count - 1], count);
876 else
878 memcpy (fragP->fr_literal + fragP->fr_fix,
879 jump_31, count);
880 /* Adjust jump offset. */
881 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
884 else
886 /* Maximum length of an instruction is 15 byte. If the
887 padding is greater than 15 bytes and we don't use jump,
888 we have to break it into smaller pieces. */
889 int padding = count;
890 while (padding > 15)
892 padding -= 15;
893 memcpy (fragP->fr_literal + fragP->fr_fix + padding,
894 patt [14], 15);
897 if (padding)
898 memcpy (fragP->fr_literal + fragP->fr_fix,
899 patt [padding - 1], padding);
902 fragP->fr_var = count;
905 static INLINE unsigned int
906 mode_from_disp_size (unsigned int t)
908 return (t & Disp8) ? 1 : (t & (Disp16 | Disp32 | Disp32S)) ? 2 : 0;
911 static INLINE int
912 fits_in_signed_byte (offsetT num)
914 return (num >= -128) && (num <= 127);
917 static INLINE int
918 fits_in_unsigned_byte (offsetT num)
920 return (num & 0xff) == num;
923 static INLINE int
924 fits_in_unsigned_word (offsetT num)
926 return (num & 0xffff) == num;
929 static INLINE int
930 fits_in_signed_word (offsetT num)
932 return (-32768 <= num) && (num <= 32767);
935 static INLINE int
936 fits_in_signed_long (offsetT num ATTRIBUTE_UNUSED)
938 #ifndef BFD64
939 return 1;
940 #else
941 return (!(((offsetT) -1 << 31) & num)
942 || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
943 #endif
944 } /* fits_in_signed_long() */
946 static INLINE int
947 fits_in_unsigned_long (offsetT num ATTRIBUTE_UNUSED)
949 #ifndef BFD64
950 return 1;
951 #else
952 return (num & (((offsetT) 2 << 31) - 1)) == num;
953 #endif
954 } /* fits_in_unsigned_long() */
956 static unsigned int
957 smallest_imm_type (offsetT num)
959 if (cpu_arch_flags != (Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
961 /* This code is disabled on the 486 because all the Imm1 forms
962 in the opcode table are slower on the i486. They're the
963 versions with the implicitly specified single-position
964 displacement, which has another syntax if you really want to
965 use that form. */
966 if (num == 1)
967 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64;
969 return (fits_in_signed_byte (num)
970 ? (Imm8S | Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
971 : fits_in_unsigned_byte (num)
972 ? (Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
973 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
974 ? (Imm16 | Imm32 | Imm32S | Imm64)
975 : fits_in_signed_long (num)
976 ? (Imm32 | Imm32S | Imm64)
977 : fits_in_unsigned_long (num)
978 ? (Imm32 | Imm64)
979 : Imm64);
982 static offsetT
983 offset_in_range (offsetT val, int size)
985 addressT mask;
987 switch (size)
989 case 1: mask = ((addressT) 1 << 8) - 1; break;
990 case 2: mask = ((addressT) 1 << 16) - 1; break;
991 case 4: mask = ((addressT) 2 << 31) - 1; break;
992 #ifdef BFD64
993 case 8: mask = ((addressT) 2 << 63) - 1; break;
994 #endif
995 default: abort ();
998 /* If BFD64, sign extend val. */
999 if (!use_rela_relocations)
1000 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
1001 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
1003 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
1005 char buf1[40], buf2[40];
1007 sprint_value (buf1, val);
1008 sprint_value (buf2, val & mask);
1009 as_warn (_("%s shortened to %s"), buf1, buf2);
1011 return val & mask;
1014 /* Returns 0 if attempting to add a prefix where one from the same
1015 class already exists, 1 if non rep/repne added, 2 if rep/repne
1016 added. */
1017 static int
1018 add_prefix (unsigned int prefix)
1020 int ret = 1;
1021 unsigned int q;
1023 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
1024 && flag_code == CODE_64BIT)
1026 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
1027 || ((i.prefix[REX_PREFIX] & (REX_R | REX_X | REX_B))
1028 && (prefix & (REX_R | REX_X | REX_B))))
1029 ret = 0;
1030 q = REX_PREFIX;
1032 else
1034 switch (prefix)
1036 default:
1037 abort ();
1039 case CS_PREFIX_OPCODE:
1040 case DS_PREFIX_OPCODE:
1041 case ES_PREFIX_OPCODE:
1042 case FS_PREFIX_OPCODE:
1043 case GS_PREFIX_OPCODE:
1044 case SS_PREFIX_OPCODE:
1045 q = SEG_PREFIX;
1046 break;
1048 case REPNE_PREFIX_OPCODE:
1049 case REPE_PREFIX_OPCODE:
1050 ret = 2;
1051 /* fall thru */
1052 case LOCK_PREFIX_OPCODE:
1053 q = LOCKREP_PREFIX;
1054 break;
1056 case FWAIT_OPCODE:
1057 q = WAIT_PREFIX;
1058 break;
1060 case ADDR_PREFIX_OPCODE:
1061 q = ADDR_PREFIX;
1062 break;
1064 case DATA_PREFIX_OPCODE:
1065 q = DATA_PREFIX;
1066 break;
1068 if (i.prefix[q] != 0)
1069 ret = 0;
1072 if (ret)
1074 if (!i.prefix[q])
1075 ++i.prefixes;
1076 i.prefix[q] |= prefix;
1078 else
1079 as_bad (_("same type of prefix used twice"));
1081 return ret;
1084 static void
1085 set_code_flag (int value)
1087 flag_code = value;
1088 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
1089 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
1090 if (value == CODE_64BIT && !(cpu_arch_flags & CpuSledgehammer))
1092 as_bad (_("64bit mode not supported on this CPU."));
1094 if (value == CODE_32BIT && !(cpu_arch_flags & Cpu386))
1096 as_bad (_("32bit mode not supported on this CPU."));
1098 stackop_size = '\0';
1101 static void
1102 set_16bit_gcc_code_flag (int new_code_flag)
1104 flag_code = new_code_flag;
1105 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
1106 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
1107 stackop_size = LONG_MNEM_SUFFIX;
1110 static void
1111 set_intel_syntax (int syntax_flag)
1113 /* Find out if register prefixing is specified. */
1114 int ask_naked_reg = 0;
1116 SKIP_WHITESPACE ();
1117 if (!is_end_of_line[(unsigned char) *input_line_pointer])
1119 char *string = input_line_pointer;
1120 int e = get_symbol_end ();
1122 if (strcmp (string, "prefix") == 0)
1123 ask_naked_reg = 1;
1124 else if (strcmp (string, "noprefix") == 0)
1125 ask_naked_reg = -1;
1126 else
1127 as_bad (_("bad argument to syntax directive."));
1128 *input_line_pointer = e;
1130 demand_empty_rest_of_line ();
1132 intel_syntax = syntax_flag;
1134 if (ask_naked_reg == 0)
1135 allow_naked_reg = (intel_syntax
1136 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
1137 else
1138 allow_naked_reg = (ask_naked_reg < 0);
1140 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
1141 identifier_chars['$'] = intel_syntax ? '$' : 0;
1142 register_prefix = allow_naked_reg ? "" : "%";
1145 static void
1146 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
1148 SKIP_WHITESPACE ();
1150 if (!is_end_of_line[(unsigned char) *input_line_pointer])
1152 char *string = input_line_pointer;
1153 int e = get_symbol_end ();
1154 unsigned int i;
1156 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
1158 if (strcmp (string, cpu_arch[i].name) == 0)
1160 if (*string != '.')
1162 cpu_arch_name = cpu_arch[i].name;
1163 cpu_sub_arch_name = NULL;
1164 cpu_arch_flags = (cpu_arch[i].flags
1165 | (flag_code == CODE_64BIT
1166 ? Cpu64 : CpuNo64));
1167 cpu_arch_isa = cpu_arch[i].type;
1168 cpu_arch_isa_flags = cpu_arch[i].flags;
1169 if (!cpu_arch_tune_set)
1171 cpu_arch_tune = cpu_arch_isa;
1172 cpu_arch_tune_flags = cpu_arch_isa_flags;
1174 break;
1176 if ((cpu_arch_flags | cpu_arch[i].flags) != cpu_arch_flags)
1178 cpu_sub_arch_name = cpu_arch[i].name;
1179 cpu_arch_flags |= cpu_arch[i].flags;
1181 *input_line_pointer = e;
1182 demand_empty_rest_of_line ();
1183 return;
1186 if (i >= ARRAY_SIZE (cpu_arch))
1187 as_bad (_("no such architecture: `%s'"), string);
1189 *input_line_pointer = e;
1191 else
1192 as_bad (_("missing cpu architecture"));
1194 no_cond_jump_promotion = 0;
1195 if (*input_line_pointer == ','
1196 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
1198 char *string = ++input_line_pointer;
1199 int e = get_symbol_end ();
1201 if (strcmp (string, "nojumps") == 0)
1202 no_cond_jump_promotion = 1;
1203 else if (strcmp (string, "jumps") == 0)
1205 else
1206 as_bad (_("no such architecture modifier: `%s'"), string);
1208 *input_line_pointer = e;
1211 demand_empty_rest_of_line ();
1214 unsigned long
1215 i386_mach ()
1217 if (!strcmp (default_arch, "x86_64"))
1218 return bfd_mach_x86_64;
1219 else if (!strcmp (default_arch, "i386"))
1220 return bfd_mach_i386_i386;
1221 else
1222 as_fatal (_("Unknown architecture"));
1225 void
1226 nativeclient_symbol_init ()
1228 symbolS *symbolP;
1229 int entry_align;
1232 * A symbol conveying the setting of nacl_alignment to assembler writers.
1234 symbolP = symbol_new ("NACLALIGN", absolute_section,
1235 (valueT) nacl_alignment, &zero_address_frag);
1236 symbol_table_insert (symbolP);
1239 * A symbol conveying the function entry alignment. This differs from
1240 * NACLALIGN in library mode.
1242 if (nacl_library_mode) {
1243 entry_align = 5;
1245 else {
1246 entry_align = nacl_alignment;
1248 symbolP = symbol_new ("NACLENTRYALIGN", absolute_section,
1249 (valueT) entry_align, &zero_address_frag);
1250 symbol_table_insert (symbolP);
1253 void
1254 md_begin ()
1256 const char *hash_err;
1258 /* Initialize op_hash hash table. */
1259 op_hash = hash_new ();
1262 const template *optab;
1263 templates *core_optab;
1265 /* Setup for loop. */
1266 optab = i386_optab;
1267 core_optab = (templates *) xmalloc (sizeof (templates));
1268 core_optab->start = optab;
1270 while (1)
1272 ++optab;
1273 if (optab->name == NULL
1274 || strcmp (optab->name, (optab - 1)->name) != 0)
1276 /* different name --> ship out current template list;
1277 add to hash table; & begin anew. */
1278 core_optab->end = optab;
1279 hash_err = hash_insert (op_hash,
1280 (optab - 1)->name,
1281 (PTR) core_optab);
1282 if (hash_err)
1284 as_fatal (_("Internal Error: Can't hash %s: %s"),
1285 (optab - 1)->name,
1286 hash_err);
1288 if (optab->name == NULL)
1289 break;
1290 core_optab = (templates *) xmalloc (sizeof (templates));
1291 core_optab->start = optab;
1296 /* Initialize reg_hash hash table. */
1297 reg_hash = hash_new ();
1299 const reg_entry *regtab;
1300 unsigned int regtab_size = i386_regtab_size;
1302 for (regtab = i386_regtab; regtab_size--; regtab++)
1304 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
1305 if (hash_err)
1306 as_fatal (_("Internal Error: Can't hash %s: %s"),
1307 regtab->reg_name,
1308 hash_err);
1312 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
1314 int c;
1315 char *p;
1317 for (c = 0; c < 256; c++)
1319 if (ISDIGIT (c))
1321 digit_chars[c] = c;
1322 mnemonic_chars[c] = c;
1323 register_chars[c] = c;
1324 operand_chars[c] = c;
1326 else if (ISLOWER (c))
1328 mnemonic_chars[c] = c;
1329 register_chars[c] = c;
1330 operand_chars[c] = c;
1332 else if (ISUPPER (c))
1334 mnemonic_chars[c] = TOLOWER (c);
1335 register_chars[c] = mnemonic_chars[c];
1336 operand_chars[c] = c;
1339 if (ISALPHA (c) || ISDIGIT (c))
1340 identifier_chars[c] = c;
1341 else if (c >= 128)
1343 identifier_chars[c] = c;
1344 operand_chars[c] = c;
1348 #ifdef LEX_AT
1349 identifier_chars['@'] = '@';
1350 #endif
1351 #ifdef LEX_QM
1352 identifier_chars['?'] = '?';
1353 operand_chars['?'] = '?';
1354 #endif
1355 digit_chars['-'] = '-';
1356 mnemonic_chars['-'] = '-';
1357 mnemonic_chars['.'] = '.';
1358 identifier_chars['_'] = '_';
1359 identifier_chars['.'] = '.';
1361 for (p = operand_special_chars; *p != '\0'; p++)
1362 operand_chars[(unsigned char) *p] = *p;
1365 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1366 if (IS_ELF)
1368 record_alignment (text_section, 2);
1369 record_alignment (data_section, 2);
1370 record_alignment (bss_section, 2);
1372 #endif
1374 if (flag_code == CODE_64BIT)
1376 x86_dwarf2_return_column = 16;
1377 x86_cie_data_alignment = -8;
1379 else
1381 x86_dwarf2_return_column = 8;
1382 x86_cie_data_alignment = -4;
1385 nativeclient_symbol_init();
1388 void
1389 i386_print_statistics (FILE *file)
1391 hash_print_statistics (file, "i386 opcode", op_hash);
1392 hash_print_statistics (file, "i386 register", reg_hash);
1395 #ifdef DEBUG386
1397 /* Debugging routines for md_assemble. */
1398 static void pte (template *);
1399 static void pt (unsigned int);
1400 static void pe (expressionS *);
1401 static void ps (symbolS *);
1403 static void
1404 pi (char *line, i386_insn *x)
1406 unsigned int i;
1408 fprintf (stdout, "%s: template ", line);
1409 pte (&x->tm);
1410 fprintf (stdout, " address: base %s index %s scale %x\n",
1411 x->base_reg ? x->base_reg->reg_name : "none",
1412 x->index_reg ? x->index_reg->reg_name : "none",
1413 x->log2_scale_factor);
1414 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
1415 x->rm.mode, x->rm.reg, x->rm.regmem);
1416 fprintf (stdout, " sib: base %x index %x scale %x\n",
1417 x->sib.base, x->sib.index, x->sib.scale);
1418 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
1419 (x->rex & REX_W) != 0,
1420 (x->rex & REX_R) != 0,
1421 (x->rex & REX_X) != 0,
1422 (x->rex & REX_B) != 0);
1423 for (i = 0; i < x->operands; i++)
1425 fprintf (stdout, " #%d: ", i + 1);
1426 pt (x->types[i]);
1427 fprintf (stdout, "\n");
1428 if (x->types[i]
1429 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
1430 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
1431 if (x->types[i] & Imm)
1432 pe (x->op[i].imms);
1433 if (x->types[i] & Disp)
1434 pe (x->op[i].disps);
1438 static void
1439 pte (template *t)
1441 unsigned int i;
1442 fprintf (stdout, " %d operands ", t->operands);
1443 fprintf (stdout, "opcode %x ", t->base_opcode);
1444 if (t->extension_opcode != None)
1445 fprintf (stdout, "ext %x ", t->extension_opcode);
1446 if (t->opcode_modifier & D)
1447 fprintf (stdout, "D");
1448 if (t->opcode_modifier & W)
1449 fprintf (stdout, "W");
1450 fprintf (stdout, "\n");
1451 for (i = 0; i < t->operands; i++)
1453 fprintf (stdout, " #%d type ", i + 1);
1454 pt (t->operand_types[i]);
1455 fprintf (stdout, "\n");
1459 static void
1460 pe (expressionS *e)
1462 fprintf (stdout, " operation %d\n", e->X_op);
1463 fprintf (stdout, " add_number %ld (%lx)\n",
1464 (long) e->X_add_number, (long) e->X_add_number);
1465 if (e->X_add_symbol)
1467 fprintf (stdout, " add_symbol ");
1468 ps (e->X_add_symbol);
1469 fprintf (stdout, "\n");
1471 if (e->X_op_symbol)
1473 fprintf (stdout, " op_symbol ");
1474 ps (e->X_op_symbol);
1475 fprintf (stdout, "\n");
1479 static void
1480 ps (symbolS *s)
1482 fprintf (stdout, "%s type %s%s",
1483 S_GET_NAME (s),
1484 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
1485 segment_name (S_GET_SEGMENT (s)));
1488 static struct type_name
1490 unsigned int mask;
1491 char *tname;
1493 const type_names[] =
1495 { Reg8, "r8" },
1496 { Reg16, "r16" },
1497 { Reg32, "r32" },
1498 { Reg64, "r64" },
1499 { Imm8, "i8" },
1500 { Imm8S, "i8s" },
1501 { Imm16, "i16" },
1502 { Imm32, "i32" },
1503 { Imm32S, "i32s" },
1504 { Imm64, "i64" },
1505 { Imm1, "i1" },
1506 { BaseIndex, "BaseIndex" },
1507 { Disp8, "d8" },
1508 { Disp16, "d16" },
1509 { Disp32, "d32" },
1510 { Disp32S, "d32s" },
1511 { Disp64, "d64" },
1512 { InOutPortReg, "InOutPortReg" },
1513 { ShiftCount, "ShiftCount" },
1514 { Control, "control reg" },
1515 { Test, "test reg" },
1516 { Debug, "debug reg" },
1517 { FloatReg, "FReg" },
1518 { FloatAcc, "FAcc" },
1519 { SReg2, "SReg2" },
1520 { SReg3, "SReg3" },
1521 { Acc, "Acc" },
1522 { JumpAbsolute, "Jump Absolute" },
1523 { RegMMX, "rMMX" },
1524 { RegXMM, "rXMM" },
1525 { EsSeg, "es" },
1526 { 0, "" }
1529 static void
1530 pt (t)
1531 unsigned int t;
1533 const struct type_name *ty;
1535 for (ty = type_names; ty->mask; ty++)
1536 if (t & ty->mask)
1537 fprintf (stdout, "%s, ", ty->tname);
1538 fflush (stdout);
1541 #endif /* DEBUG386 */
1543 static bfd_reloc_code_real_type
1544 reloc (unsigned int size,
1545 int pcrel,
1546 int sign,
1547 bfd_reloc_code_real_type other)
1549 if (other != NO_RELOC)
1551 reloc_howto_type *reloc;
1553 if (size == 8)
1554 switch (other)
1556 case BFD_RELOC_X86_64_GOT32:
1557 return BFD_RELOC_X86_64_GOT64;
1558 break;
1559 case BFD_RELOC_X86_64_PLTOFF64:
1560 return BFD_RELOC_X86_64_PLTOFF64;
1561 break;
1562 case BFD_RELOC_X86_64_GOTPC32:
1563 other = BFD_RELOC_X86_64_GOTPC64;
1564 break;
1565 case BFD_RELOC_X86_64_GOTPCREL:
1566 other = BFD_RELOC_X86_64_GOTPCREL64;
1567 break;
1568 case BFD_RELOC_X86_64_TPOFF32:
1569 other = BFD_RELOC_X86_64_TPOFF64;
1570 break;
1571 case BFD_RELOC_X86_64_DTPOFF32:
1572 other = BFD_RELOC_X86_64_DTPOFF64;
1573 break;
1574 default:
1575 break;
1578 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
1579 if (size == 4 && flag_code != CODE_64BIT)
1580 sign = -1;
1582 reloc = bfd_reloc_type_lookup (stdoutput, other);
1583 if (!reloc)
1584 as_bad (_("unknown relocation (%u)"), other);
1585 else if (size != bfd_get_reloc_size (reloc))
1586 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
1587 bfd_get_reloc_size (reloc),
1588 size);
1589 else if (pcrel && !reloc->pc_relative)
1590 as_bad (_("non-pc-relative relocation for pc-relative field"));
1591 else if ((reloc->complain_on_overflow == complain_overflow_signed
1592 && !sign)
1593 || (reloc->complain_on_overflow == complain_overflow_unsigned
1594 && sign > 0))
1595 as_bad (_("relocated field and relocation type differ in signedness"));
1596 else
1597 return other;
1598 return NO_RELOC;
1601 if (pcrel)
1603 if (!sign)
1604 as_bad (_("there are no unsigned pc-relative relocations"));
1605 switch (size)
1607 case 1: return BFD_RELOC_8_PCREL;
1608 case 2: return BFD_RELOC_16_PCREL;
1609 case 4: return BFD_RELOC_32_PCREL;
1610 case 8: return BFD_RELOC_64_PCREL;
1612 as_bad (_("cannot do %u byte pc-relative relocation"), size);
1614 else
1616 if (sign > 0)
1617 switch (size)
1619 case 4: return BFD_RELOC_X86_64_32S;
1621 else
1622 switch (size)
1624 case 1: return BFD_RELOC_8;
1625 case 2: return BFD_RELOC_16;
1626 case 4: return BFD_RELOC_32;
1627 case 8: return BFD_RELOC_64;
1629 as_bad (_("cannot do %s %u byte relocation"),
1630 sign > 0 ? "signed" : "unsigned", size);
1633 abort ();
1634 return BFD_RELOC_NONE;
1637 /* Here we decide which fixups can be adjusted to make them relative to
1638 the beginning of the section instead of the symbol. Basically we need
1639 to make sure that the dynamic relocations are done correctly, so in
1640 some cases we force the original symbol to be used. */
1643 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
1645 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1646 if (!IS_ELF)
1647 return 1;
1649 /* Don't adjust pc-relative references to merge sections in 64-bit
1650 mode. */
1651 if (use_rela_relocations
1652 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
1653 && fixP->fx_pcrel)
1654 return 0;
1656 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
1657 and changed later by validate_fix. */
1658 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
1659 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
1660 return 0;
1662 /* adjust_reloc_syms doesn't know about the GOT. */
1663 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1664 || fixP->fx_r_type == BFD_RELOC_386_PLT32
1665 || fixP->fx_r_type == BFD_RELOC_386_GOT32
1666 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
1667 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
1668 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
1669 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
1670 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
1671 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
1672 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
1673 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
1674 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
1675 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
1676 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
1677 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
1678 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
1679 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
1680 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
1681 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
1682 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
1683 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
1684 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
1685 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
1686 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
1687 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
1688 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
1689 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1690 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1691 return 0;
1692 #endif
1693 return 1;
1696 static int
1697 intel_float_operand (const char *mnemonic)
1699 /* Note that the value returned is meaningful only for opcodes with (memory)
1700 operands, hence the code here is free to improperly handle opcodes that
1701 have no operands (for better performance and smaller code). */
1703 if (mnemonic[0] != 'f')
1704 return 0; /* non-math */
1706 switch (mnemonic[1])
1708 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
1709 the fs segment override prefix not currently handled because no
1710 call path can make opcodes without operands get here */
1711 case 'i':
1712 return 2 /* integer op */;
1713 case 'l':
1714 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
1715 return 3; /* fldcw/fldenv */
1716 break;
1717 case 'n':
1718 if (mnemonic[2] != 'o' /* fnop */)
1719 return 3; /* non-waiting control op */
1720 break;
1721 case 'r':
1722 if (mnemonic[2] == 's')
1723 return 3; /* frstor/frstpm */
1724 break;
1725 case 's':
1726 if (mnemonic[2] == 'a')
1727 return 3; /* fsave */
1728 if (mnemonic[2] == 't')
1730 switch (mnemonic[3])
1732 case 'c': /* fstcw */
1733 case 'd': /* fstdw */
1734 case 'e': /* fstenv */
1735 case 's': /* fsts[gw] */
1736 return 3;
1739 break;
1740 case 'x':
1741 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
1742 return 0; /* fxsave/fxrstor are not really math ops */
1743 break;
1746 return 1;
1749 /* This is the guts of the machine-dependent assembler. LINE points to a
1750 machine dependent instruction. This function is supposed to emit
1751 the frags/bytes it assembles to. */
1753 void
1754 md_assemble (line)
1755 char *line;
1757 int j;
1758 char mnemonic[MAX_MNEM_SIZE];
1760 /* Initialize globals. */
1761 memset (&i, '\0', sizeof (i));
1762 for (j = 0; j < MAX_OPERANDS; j++)
1763 i.reloc[j] = NO_RELOC;
1764 memset (disp_expressions, '\0', sizeof (disp_expressions));
1765 memset (im_expressions, '\0', sizeof (im_expressions));
1766 save_stack_p = save_stack;
1768 /* First parse an instruction mnemonic & call i386_operand for the operands.
1769 We assume that the scrubber has arranged it so that line[0] is the valid
1770 start of a (possibly prefixed) mnemonic. */
1772 line = parse_insn (line, mnemonic);
1773 if (line == NULL)
1774 return;
1776 line = parse_operands (line, mnemonic);
1777 if (line == NULL)
1778 return;
1780 /* The order of the immediates should be reversed
1781 for 2 immediates extrq and insertq instructions */
1782 if ((i.imm_operands == 2)
1783 && ((strcmp (mnemonic, "extrq") == 0)
1784 || (strcmp (mnemonic, "insertq") == 0)))
1786 swap_2_operands (0, 1);
1787 /* "extrq" and insertq" are the only two instructions whose operands
1788 have to be reversed even though they have two immediate operands.
1790 if (intel_syntax)
1791 swap_operands ();
1794 /* Now we've parsed the mnemonic into a set of templates, and have the
1795 operands at hand. */
1797 /* All intel opcodes have reversed operands except for "bound" and
1798 "enter". We also don't reverse intersegment "jmp" and "call"
1799 instructions with 2 immediate operands so that the immediate segment
1800 precedes the offset, as it does when in AT&T mode. */
1801 if (intel_syntax
1802 && i.operands > 1
1803 && (strcmp (mnemonic, "bound") != 0)
1804 && (strcmp (mnemonic, "invlpga") != 0)
1805 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1806 swap_operands ();
1808 if (i.imm_operands)
1809 optimize_imm ();
1811 /* Don't optimize displacement for movabs since it only takes 64bit
1812 displacement. */
1813 if (i.disp_operands
1814 && (flag_code != CODE_64BIT
1815 || strcmp (mnemonic, "movabs") != 0))
1816 optimize_disp ();
1818 /* Next, we find a template that matches the given insn,
1819 making sure the overlap of the given operands types is consistent
1820 with the template operand types. */
1822 if (!match_template ())
1823 return;
1825 if (intel_syntax)
1827 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1828 if (SYSV386_COMPAT
1829 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1830 i.tm.base_opcode ^= Opcode_FloatR;
1832 /* Zap movzx and movsx suffix. The suffix may have been set from
1833 "word ptr" or "byte ptr" on the source operand, but we'll use
1834 the suffix later to choose the destination register. */
1835 if ((i.tm.base_opcode & ~9) == 0x0fb6)
1837 if (i.reg_operands < 2
1838 && !i.suffix
1839 && (~i.tm.opcode_modifier
1840 & (No_bSuf
1841 | No_wSuf
1842 | No_lSuf
1843 | No_sSuf
1844 | No_xSuf
1845 | No_qSuf)))
1846 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
1848 i.suffix = 0;
1852 if (i.tm.opcode_modifier & FWait)
1853 if (!add_prefix (FWAIT_OPCODE))
1854 return;
1856 /* Check string instruction segment overrides. */
1857 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1859 if (!check_string ())
1860 return;
1863 if (!process_suffix ())
1864 return;
1866 /* Make still unresolved immediate matches conform to size of immediate
1867 given in i.suffix. */
1868 if (!finalize_imm ())
1869 return;
1871 if (i.types[0] & Imm1)
1872 i.imm_operands = 0; /* kludge for shift insns. */
1873 if (i.types[0] & ImplicitRegister)
1874 i.reg_operands--;
1875 if (i.types[1] & ImplicitRegister)
1876 i.reg_operands--;
1877 if (i.types[2] & ImplicitRegister)
1878 i.reg_operands--;
1880 if (i.tm.opcode_modifier & ImmExt)
1882 expressionS *exp;
1884 if ((i.tm.cpu_flags & CpuSSE3) && i.operands > 0)
1886 /* Streaming SIMD extensions 3 Instructions have the fixed
1887 operands with an opcode suffix which is coded in the same
1888 place as an 8-bit immediate field would be. Here we check
1889 those operands and remove them afterwards. */
1890 unsigned int x;
1892 for (x = 0; x < i.operands; x++)
1893 if (i.op[x].regs->reg_num != x)
1894 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
1895 register_prefix,
1896 i.op[x].regs->reg_name,
1897 x + 1,
1898 i.tm.name);
1899 i.operands = 0;
1902 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1903 opcode suffix which is coded in the same place as an 8-bit
1904 immediate field would be. Here we fake an 8-bit immediate
1905 operand from the opcode suffix stored in tm.extension_opcode. */
1907 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1909 exp = &im_expressions[i.imm_operands++];
1910 i.op[i.operands].imms = exp;
1911 i.types[i.operands++] = Imm8;
1912 exp->X_op = O_constant;
1913 exp->X_add_number = i.tm.extension_opcode;
1914 i.tm.extension_opcode = None;
1917 /* For insns with operands there are more diddles to do to the opcode. */
1918 if (i.operands)
1920 if (!process_operands ())
1921 return;
1923 else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1925 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
1926 as_warn (_("translating to `%sp'"), i.tm.name);
1929 /* Handle conversion of 'int $3' --> special int3 insn. */
1930 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
1932 i.tm.base_opcode = INT3_OPCODE;
1933 i.imm_operands = 0;
1936 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
1937 && i.op[0].disps->X_op == O_constant)
1939 /* Convert "jmp constant" (and "call constant") to a jump (call) to
1940 the absolute address given by the constant. Since ix86 jumps and
1941 calls are pc relative, we need to generate a reloc. */
1942 i.op[0].disps->X_add_symbol = &abs_symbol;
1943 i.op[0].disps->X_op = O_symbol;
1946 if ((i.tm.opcode_modifier & Rex64) != 0)
1947 i.rex |= REX_W;
1949 /* For 8 bit registers we need an empty rex prefix. Also if the
1950 instruction already has a prefix, we need to convert old
1951 registers to new ones. */
1953 if (((i.types[0] & Reg8) != 0
1954 && (i.op[0].regs->reg_flags & RegRex64) != 0)
1955 || ((i.types[1] & Reg8) != 0
1956 && (i.op[1].regs->reg_flags & RegRex64) != 0)
1957 || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
1958 && i.rex != 0))
1960 int x;
1962 i.rex |= REX_OPCODE;
1963 for (x = 0; x < 2; x++)
1965 /* Look for 8 bit operand that uses old registers. */
1966 if ((i.types[x] & Reg8) != 0
1967 && (i.op[x].regs->reg_flags & RegRex64) == 0)
1969 /* In case it is "hi" register, give up. */
1970 if (i.op[x].regs->reg_num > 3)
1971 as_bad (_("can't encode register '%s%s' in an "
1972 "instruction requiring REX prefix."),
1973 register_prefix, i.op[x].regs->reg_name);
1975 /* Otherwise it is equivalent to the extended register.
1976 Since the encoding doesn't change this is merely
1977 cosmetic cleanup for debug output. */
1979 i.op[x].regs = i.op[x].regs + 8;
1984 if (i.rex != 0)
1985 add_prefix (REX_OPCODE | i.rex);
1987 /* We are ready to output the insn. */
1988 output_insn ();
1991 static char *
1992 parse_insn (char *line, char *mnemonic)
1994 char *l = line;
1995 char *token_start = l;
1996 char *mnem_p;
1997 int supported;
1998 const template *t;
2000 /* Non-zero if we found a prefix only acceptable with string insns. */
2001 const char *expecting_string_instruction = NULL;
2003 while (1)
2005 mnem_p = mnemonic;
2006 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
2008 mnem_p++;
2009 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
2011 as_bad (_("no such instruction: `%s'"), token_start);
2012 return NULL;
2014 l++;
2016 if (!is_space_char (*l)
2017 && *l != END_OF_INSN
2018 && (intel_syntax
2019 || (*l != PREFIX_SEPARATOR
2020 && *l != ',')))
2022 as_bad (_("invalid character %s in mnemonic"),
2023 output_invalid (*l));
2024 return NULL;
2026 if (token_start == l)
2028 if (!intel_syntax && *l == PREFIX_SEPARATOR)
2029 as_bad (_("expecting prefix; got nothing"));
2030 else
2031 as_bad (_("expecting mnemonic; got nothing"));
2032 return NULL;
2035 /* Look up instruction (or prefix) via hash table. */
2036 current_templates = hash_find (op_hash, mnemonic);
2038 if (*l != END_OF_INSN
2039 && (!is_space_char (*l) || l[1] != END_OF_INSN)
2040 && current_templates
2041 && (current_templates->start->opcode_modifier & IsPrefix))
2043 if (current_templates->start->cpu_flags
2044 & (flag_code != CODE_64BIT ? Cpu64 : CpuNo64))
2046 as_bad ((flag_code != CODE_64BIT
2047 ? _("`%s' is only supported in 64-bit mode")
2048 : _("`%s' is not supported in 64-bit mode")),
2049 current_templates->start->name);
2050 return NULL;
2052 /* If we are in 16-bit mode, do not allow addr16 or data16.
2053 Similarly, in 32-bit mode, do not allow addr32 or data32. */
2054 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
2055 && flag_code != CODE_64BIT
2056 && (((current_templates->start->opcode_modifier & Size32) != 0)
2057 ^ (flag_code == CODE_16BIT)))
2059 as_bad (_("redundant %s prefix"),
2060 current_templates->start->name);
2061 return NULL;
2063 /* Add prefix, checking for repeated prefixes. */
2064 switch (add_prefix (current_templates->start->base_opcode))
2066 case 0:
2067 return NULL;
2068 case 2:
2069 expecting_string_instruction = current_templates->start->name;
2070 break;
2072 /* Skip past PREFIX_SEPARATOR and reset token_start. */
2073 token_start = ++l;
2075 else
2076 break;
2079 if (!current_templates)
2081 /* See if we can get a match by trimming off a suffix. */
2082 switch (mnem_p[-1])
2084 case WORD_MNEM_SUFFIX:
2085 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
2086 i.suffix = SHORT_MNEM_SUFFIX;
2087 else
2088 case BYTE_MNEM_SUFFIX:
2089 case QWORD_MNEM_SUFFIX:
2090 i.suffix = mnem_p[-1];
2091 mnem_p[-1] = '\0';
2092 current_templates = hash_find (op_hash, mnemonic);
2093 break;
2094 case SHORT_MNEM_SUFFIX:
2095 case LONG_MNEM_SUFFIX:
2096 if (!intel_syntax)
2098 i.suffix = mnem_p[-1];
2099 mnem_p[-1] = '\0';
2100 current_templates = hash_find (op_hash, mnemonic);
2102 break;
2104 /* Intel Syntax. */
2105 case 'd':
2106 if (intel_syntax)
2108 if (intel_float_operand (mnemonic) == 1)
2109 i.suffix = SHORT_MNEM_SUFFIX;
2110 else
2111 i.suffix = LONG_MNEM_SUFFIX;
2112 mnem_p[-1] = '\0';
2113 current_templates = hash_find (op_hash, mnemonic);
2115 break;
2117 if (!current_templates)
2119 as_bad (_("no such instruction: `%s'"), token_start);
2120 return NULL;
2124 if (current_templates->start->opcode_modifier & (Jump | JumpByte))
2126 /* Check for a branch hint. We allow ",pt" and ",pn" for
2127 predict taken and predict not taken respectively.
2128 I'm not sure that branch hints actually do anything on loop
2129 and jcxz insns (JumpByte) for current Pentium4 chips. They
2130 may work in the future and it doesn't hurt to accept them
2131 now. */
2132 if (l[0] == ',' && l[1] == 'p')
2134 if (l[2] == 't')
2136 if (!add_prefix (DS_PREFIX_OPCODE))
2137 return NULL;
2138 l += 3;
2140 else if (l[2] == 'n')
2142 if (!add_prefix (CS_PREFIX_OPCODE))
2143 return NULL;
2144 l += 3;
2148 /* Any other comma loses. */
2149 if (*l == ',')
2151 as_bad (_("invalid character %s in mnemonic"),
2152 output_invalid (*l));
2153 return NULL;
2156 /* Check if instruction is supported on specified architecture. */
2157 supported = 0;
2158 for (t = current_templates->start; t < current_templates->end; ++t)
2160 if (!((t->cpu_flags & ~(Cpu64 | CpuNo64))
2161 & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64))))
2162 supported |= 1;
2163 if (!(t->cpu_flags & (flag_code == CODE_64BIT ? CpuNo64 : Cpu64)))
2164 supported |= 2;
2166 if (!(supported & 2))
2168 as_bad (flag_code == CODE_64BIT
2169 ? _("`%s' is not supported in 64-bit mode")
2170 : _("`%s' is only supported in 64-bit mode"),
2171 current_templates->start->name);
2172 return NULL;
2174 if (!(supported & 1))
2176 as_warn (_("`%s' is not supported on `%s%s'"),
2177 current_templates->start->name,
2178 cpu_arch_name,
2179 cpu_sub_arch_name ? cpu_sub_arch_name : "");
2181 else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
2183 as_warn (_("use .code16 to ensure correct addressing mode"));
2186 /* Check for rep/repne without a string instruction. */
2187 if (expecting_string_instruction)
2189 static templates override;
2191 for (t = current_templates->start; t < current_templates->end; ++t)
2192 if (t->opcode_modifier & IsString)
2193 break;
2194 if (t >= current_templates->end)
2196 as_bad (_("expecting string instruction after `%s'"),
2197 expecting_string_instruction);
2198 return NULL;
2200 for (override.start = t; t < current_templates->end; ++t)
2201 if (!(t->opcode_modifier & IsString))
2202 break;
2203 override.end = t;
2204 current_templates = &override;
2207 return l;
2210 static char *
2211 parse_operands (char *l, const char *mnemonic)
2213 char *token_start;
2215 /* 1 if operand is pending after ','. */
2216 unsigned int expecting_operand = 0;
2218 /* Non-zero if operand parens not balanced. */
2219 unsigned int paren_not_balanced;
2221 while (*l != END_OF_INSN)
2223 /* Skip optional white space before operand. */
2224 if (is_space_char (*l))
2225 ++l;
2226 if (!is_operand_char (*l) && *l != END_OF_INSN)
2228 as_bad (_("invalid character %s before operand %d"),
2229 output_invalid (*l),
2230 i.operands + 1);
2231 return NULL;
2233 token_start = l; /* after white space */
2234 paren_not_balanced = 0;
2235 while (paren_not_balanced || *l != ',')
2237 if (*l == END_OF_INSN)
2239 if (paren_not_balanced)
2241 if (!intel_syntax)
2242 as_bad (_("unbalanced parenthesis in operand %d."),
2243 i.operands + 1);
2244 else
2245 as_bad (_("unbalanced brackets in operand %d."),
2246 i.operands + 1);
2247 return NULL;
2249 else
2250 break; /* we are done */
2252 else if (!is_operand_char (*l) && !is_space_char (*l))
2254 as_bad (_("invalid character %s in operand %d"),
2255 output_invalid (*l),
2256 i.operands + 1);
2257 return NULL;
2259 if (!intel_syntax)
2261 if (*l == '(')
2262 ++paren_not_balanced;
2263 if (*l == ')')
2264 --paren_not_balanced;
2266 else
2268 if (*l == '[')
2269 ++paren_not_balanced;
2270 if (*l == ']')
2271 --paren_not_balanced;
2273 l++;
2275 if (l != token_start)
2276 { /* Yes, we've read in another operand. */
2277 unsigned int operand_ok;
2278 this_operand = i.operands++;
2279 if (i.operands > MAX_OPERANDS)
2281 as_bad (_("spurious operands; (%d operands/instruction max)"),
2282 MAX_OPERANDS);
2283 return NULL;
2285 /* Now parse operand adding info to 'i' as we go along. */
2286 END_STRING_AND_SAVE (l);
2288 if (intel_syntax)
2289 operand_ok =
2290 i386_intel_operand (token_start,
2291 intel_float_operand (mnemonic));
2292 else
2293 operand_ok = i386_operand (token_start);
2295 RESTORE_END_STRING (l);
2296 if (!operand_ok)
2297 return NULL;
2299 else
2301 if (expecting_operand)
2303 expecting_operand_after_comma:
2304 as_bad (_("expecting operand after ','; got nothing"));
2305 return NULL;
2307 if (*l == ',')
2309 as_bad (_("expecting operand before ','; got nothing"));
2310 return NULL;
2314 /* Now *l must be either ',' or END_OF_INSN. */
2315 if (*l == ',')
2317 if (*++l == END_OF_INSN)
2319 /* Just skip it, if it's \n complain. */
2320 goto expecting_operand_after_comma;
2322 expecting_operand = 1;
2325 return l;
2328 static void
2329 swap_2_operands (int xchg1, int xchg2)
2331 union i386_op temp_op;
2332 unsigned int temp_type;
2333 enum bfd_reloc_code_real temp_reloc;
2335 temp_type = i.types[xchg2];
2336 i.types[xchg2] = i.types[xchg1];
2337 i.types[xchg1] = temp_type;
2338 temp_op = i.op[xchg2];
2339 i.op[xchg2] = i.op[xchg1];
2340 i.op[xchg1] = temp_op;
2341 temp_reloc = i.reloc[xchg2];
2342 i.reloc[xchg2] = i.reloc[xchg1];
2343 i.reloc[xchg1] = temp_reloc;
2346 static void
2347 swap_operands (void)
2349 switch (i.operands)
2351 case 4:
2352 swap_2_operands (1, i.operands - 2);
2353 case 3:
2354 case 2:
2355 swap_2_operands (0, i.operands - 1);
2356 break;
2357 default:
2358 abort ();
2361 if (i.mem_operands == 2)
2363 const seg_entry *temp_seg;
2364 temp_seg = i.seg[0];
2365 i.seg[0] = i.seg[1];
2366 i.seg[1] = temp_seg;
2370 /* Try to ensure constant immediates are represented in the smallest
2371 opcode possible. */
2372 static void
2373 optimize_imm (void)
2375 char guess_suffix = 0;
2376 int op;
2378 if (i.suffix)
2379 guess_suffix = i.suffix;
2380 else if (i.reg_operands)
2382 /* Figure out a suffix from the last register operand specified.
2383 We can't do this properly yet, ie. excluding InOutPortReg,
2384 but the following works for instructions with immediates.
2385 In any case, we can't set i.suffix yet. */
2386 for (op = i.operands; --op >= 0;)
2387 if (i.types[op] & Reg)
2389 if (i.types[op] & Reg8)
2390 guess_suffix = BYTE_MNEM_SUFFIX;
2391 else if (i.types[op] & Reg16)
2392 guess_suffix = WORD_MNEM_SUFFIX;
2393 else if (i.types[op] & Reg32)
2394 guess_suffix = LONG_MNEM_SUFFIX;
2395 else if (i.types[op] & Reg64)
2396 guess_suffix = QWORD_MNEM_SUFFIX;
2397 break;
2400 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
2401 guess_suffix = WORD_MNEM_SUFFIX;
2403 for (op = i.operands; --op >= 0;)
2404 if (i.types[op] & Imm)
2406 switch (i.op[op].imms->X_op)
2408 case O_constant:
2409 /* If a suffix is given, this operand may be shortened. */
2410 switch (guess_suffix)
2412 case LONG_MNEM_SUFFIX:
2413 i.types[op] |= Imm32 | Imm64;
2414 break;
2415 case WORD_MNEM_SUFFIX:
2416 i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
2417 break;
2418 case BYTE_MNEM_SUFFIX:
2419 i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
2420 break;
2423 /* If this operand is at most 16 bits, convert it
2424 to a signed 16 bit number before trying to see
2425 whether it will fit in an even smaller size.
2426 This allows a 16-bit operand such as $0xffe0 to
2427 be recognised as within Imm8S range. */
2428 if ((i.types[op] & Imm16)
2429 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
2431 i.op[op].imms->X_add_number =
2432 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
2434 if ((i.types[op] & Imm32)
2435 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
2436 == 0))
2438 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
2439 ^ ((offsetT) 1 << 31))
2440 - ((offsetT) 1 << 31));
2442 i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
2444 /* We must avoid matching of Imm32 templates when 64bit
2445 only immediate is available. */
2446 if (guess_suffix == QWORD_MNEM_SUFFIX)
2447 i.types[op] &= ~Imm32;
2448 break;
2450 case O_absent:
2451 case O_register:
2452 abort ();
2454 /* Symbols and expressions. */
2455 default:
2456 /* Convert symbolic operand to proper sizes for matching, but don't
2457 prevent matching a set of insns that only supports sizes other
2458 than those matching the insn suffix. */
2460 unsigned int mask, allowed = 0;
2461 const template *t;
2463 for (t = current_templates->start;
2464 t < current_templates->end;
2465 ++t)
2466 allowed |= t->operand_types[op];
2467 switch (guess_suffix)
2469 case QWORD_MNEM_SUFFIX:
2470 mask = Imm64 | Imm32S;
2471 break;
2472 case LONG_MNEM_SUFFIX:
2473 mask = Imm32;
2474 break;
2475 case WORD_MNEM_SUFFIX:
2476 mask = Imm16;
2477 break;
2478 case BYTE_MNEM_SUFFIX:
2479 mask = Imm8;
2480 break;
2481 default:
2482 mask = 0;
2483 break;
2485 if (mask & allowed)
2486 i.types[op] &= mask;
2488 break;
2493 /* Try to use the smallest displacement type too. */
2494 static void
2495 optimize_disp (void)
2497 int op;
2499 for (op = i.operands; --op >= 0;)
2500 if (i.types[op] & Disp)
2502 if (i.op[op].disps->X_op == O_constant)
2504 offsetT disp = i.op[op].disps->X_add_number;
2506 if ((i.types[op] & Disp16)
2507 && (disp & ~(offsetT) 0xffff) == 0)
2509 /* If this operand is at most 16 bits, convert
2510 to a signed 16 bit number and don't use 64bit
2511 displacement. */
2512 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
2513 i.types[op] &= ~Disp64;
2515 if ((i.types[op] & Disp32)
2516 && (disp & ~(((offsetT) 2 << 31) - 1)) == 0)
2518 /* If this operand is at most 32 bits, convert
2519 to a signed 32 bit number and don't use 64bit
2520 displacement. */
2521 disp &= (((offsetT) 2 << 31) - 1);
2522 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
2523 i.types[op] &= ~Disp64;
2525 if (!disp && (i.types[op] & BaseIndex))
2527 i.types[op] &= ~Disp;
2528 i.op[op].disps = 0;
2529 i.disp_operands--;
2531 else if (flag_code == CODE_64BIT)
2533 if (fits_in_signed_long (disp))
2535 i.types[op] &= ~Disp64;
2536 i.types[op] |= Disp32S;
2538 if (fits_in_unsigned_long (disp))
2539 i.types[op] |= Disp32;
2541 if ((i.types[op] & (Disp32 | Disp32S | Disp16))
2542 && fits_in_signed_byte (disp))
2543 i.types[op] |= Disp8;
2545 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
2546 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
2548 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
2549 i.op[op].disps, 0, i.reloc[op]);
2550 i.types[op] &= ~Disp;
2552 else
2553 /* We only support 64bit displacement on constants. */
2554 i.types[op] &= ~Disp64;
2558 static int
2559 match_template (void)
2561 /* Points to template once we've found it. */
2562 const template *t;
2563 unsigned int overlap0, overlap1, overlap2, overlap3;
2564 unsigned int found_reverse_match;
2565 int suffix_check;
2566 unsigned int operand_types [MAX_OPERANDS];
2567 int addr_prefix_disp;
2568 unsigned int j;
2570 #if MAX_OPERANDS != 4
2571 # error "MAX_OPERANDS must be 4."
2572 #endif
2574 #define MATCH(overlap, given, template) \
2575 ((overlap & ~JumpAbsolute) \
2576 && (((given) & (BaseIndex | JumpAbsolute)) \
2577 == ((overlap) & (BaseIndex | JumpAbsolute))))
2579 /* If given types r0 and r1 are registers they must be of the same type
2580 unless the expected operand type register overlap is null.
2581 Note that Acc in a template matches every size of reg. */
2582 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
2583 (((g0) & Reg) == 0 || ((g1) & Reg) == 0 \
2584 || ((g0) & Reg) == ((g1) & Reg) \
2585 || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
2587 overlap0 = 0;
2588 overlap1 = 0;
2589 overlap2 = 0;
2590 overlap3 = 0;
2591 found_reverse_match = 0;
2592 for (j = 0; j < MAX_OPERANDS; j++)
2593 operand_types [j] = 0;
2594 addr_prefix_disp = -1;
2595 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
2596 ? No_bSuf
2597 : (i.suffix == WORD_MNEM_SUFFIX
2598 ? No_wSuf
2599 : (i.suffix == SHORT_MNEM_SUFFIX
2600 ? No_sSuf
2601 : (i.suffix == LONG_MNEM_SUFFIX
2602 ? No_lSuf
2603 : (i.suffix == QWORD_MNEM_SUFFIX
2604 ? No_qSuf
2605 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
2606 ? No_xSuf : 0))))));
2608 for (t = current_templates->start; t < current_templates->end; t++)
2610 addr_prefix_disp = -1;
2612 /* Must have right number of operands. */
2613 if (i.operands != t->operands)
2614 continue;
2616 /* Check the suffix, except for some instructions in intel mode. */
2617 if ((t->opcode_modifier & suffix_check)
2618 && !(intel_syntax
2619 && (t->opcode_modifier & IgnoreSize)))
2620 continue;
2622 for (j = 0; j < MAX_OPERANDS; j++)
2623 operand_types [j] = t->operand_types [j];
2625 /* In general, don't allow 64-bit operands in 32-bit mode. */
2626 if (i.suffix == QWORD_MNEM_SUFFIX
2627 && flag_code != CODE_64BIT
2628 && (intel_syntax
2629 ? (!(t->opcode_modifier & IgnoreSize)
2630 && !intel_float_operand (t->name))
2631 : intel_float_operand (t->name) != 2)
2632 && (!(operand_types[0] & (RegMMX | RegXMM))
2633 || !(operand_types[t->operands > 1] & (RegMMX | RegXMM)))
2634 && (t->base_opcode != 0x0fc7
2635 || t->extension_opcode != 1 /* cmpxchg8b */))
2636 continue;
2638 /* Do not verify operands when there are none. */
2639 else if (!t->operands)
2641 if (t->cpu_flags & ~cpu_arch_flags)
2642 continue;
2643 /* We've found a match; break out of loop. */
2644 break;
2647 /* Address size prefix will turn Disp64/Disp32/Disp16 operand
2648 into Disp32/Disp16/Disp32 operand. */
2649 if (i.prefix[ADDR_PREFIX] != 0)
2651 unsigned int DispOn = 0, DispOff = 0;
2653 switch (flag_code)
2655 case CODE_16BIT:
2656 DispOn = Disp32;
2657 DispOff = Disp16;
2658 break;
2659 case CODE_32BIT:
2660 DispOn = Disp16;
2661 DispOff = Disp32;
2662 break;
2663 case CODE_64BIT:
2664 DispOn = Disp32;
2665 DispOff = Disp64;
2666 break;
2669 for (j = 0; j < MAX_OPERANDS; j++)
2671 /* There should be only one Disp operand. */
2672 if ((operand_types[j] & DispOff))
2674 addr_prefix_disp = j;
2675 operand_types[j] |= DispOn;
2676 operand_types[j] &= ~DispOff;
2677 break;
2682 overlap0 = i.types[0] & operand_types[0];
2683 switch (t->operands)
2685 case 1:
2686 if (!MATCH (overlap0, i.types[0], operand_types[0]))
2687 continue;
2688 break;
2689 case 2:
2690 /* xchg %eax, %eax is a special case. It is an aliase for nop
2691 only in 32bit mode and we can use opcode 0x90. In 64bit
2692 mode, we can't use 0x90 for xchg %eax, %eax since it should
2693 zero-extend %eax to %rax. */
2694 if (flag_code == CODE_64BIT
2695 && t->base_opcode == 0x90
2696 && i.types [0] == (Acc | Reg32)
2697 && i.types [1] == (Acc | Reg32))
2698 continue;
2699 case 3:
2700 case 4:
2701 overlap1 = i.types[1] & operand_types[1];
2702 if (!MATCH (overlap0, i.types[0], operand_types[0])
2703 || !MATCH (overlap1, i.types[1], operand_types[1])
2704 /* monitor in SSE3 is a very special case. The first
2705 register and the second register may have different
2706 sizes. The same applies to crc32 in SSE4.2. */
2707 || !((t->base_opcode == 0x0f01
2708 && t->extension_opcode == 0xc8)
2709 || t->base_opcode == 0xf20f38f1
2710 || CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2711 operand_types[0],
2712 overlap1, i.types[1],
2713 operand_types[1])))
2715 /* Check if other direction is valid ... */
2716 if ((t->opcode_modifier & (D | FloatD)) == 0)
2717 continue;
2719 /* Try reversing direction of operands. */
2720 overlap0 = i.types[0] & operand_types[1];
2721 overlap1 = i.types[1] & operand_types[0];
2722 if (!MATCH (overlap0, i.types[0], operand_types[1])
2723 || !MATCH (overlap1, i.types[1], operand_types[0])
2724 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2725 operand_types[1],
2726 overlap1, i.types[1],
2727 operand_types[0]))
2729 /* Does not match either direction. */
2730 continue;
2732 /* found_reverse_match holds which of D or FloatDR
2733 we've found. */
2734 if ((t->opcode_modifier & D))
2735 found_reverse_match = Opcode_D;
2736 else if ((t->opcode_modifier & FloatD))
2737 found_reverse_match = Opcode_FloatD;
2738 else
2739 found_reverse_match = 0;
2740 if ((t->opcode_modifier & FloatR))
2741 found_reverse_match |= Opcode_FloatR;
2743 else
2745 /* Found a forward 2 operand match here. */
2746 switch (t->operands)
2748 case 4:
2749 overlap3 = i.types[3] & operand_types[3];
2750 case 3:
2751 overlap2 = i.types[2] & operand_types[2];
2752 break;
2755 switch (t->operands)
2757 case 4:
2758 if (!MATCH (overlap3, i.types[3], operand_types[3])
2759 || !CONSISTENT_REGISTER_MATCH (overlap2,
2760 i.types[2],
2761 operand_types[2],
2762 overlap3,
2763 i.types[3],
2764 operand_types[3]))
2765 continue;
2766 case 3:
2767 /* Here we make use of the fact that there are no
2768 reverse match 3 operand instructions, and all 3
2769 operand instructions only need to be checked for
2770 register consistency between operands 2 and 3. */
2771 if (!MATCH (overlap2, i.types[2], operand_types[2])
2772 || !CONSISTENT_REGISTER_MATCH (overlap1,
2773 i.types[1],
2774 operand_types[1],
2775 overlap2,
2776 i.types[2],
2777 operand_types[2]))
2778 continue;
2779 break;
2782 /* Found either forward/reverse 2, 3 or 4 operand match here:
2783 slip through to break. */
2785 if (t->cpu_flags & ~cpu_arch_flags)
2787 found_reverse_match = 0;
2788 continue;
2790 /* We've found a match; break out of loop. */
2791 break;
2794 if (t == current_templates->end)
2796 /* We found no match. */
2797 as_bad (_("suffix or operands invalid for `%s'"),
2798 current_templates->start->name);
2799 return 0;
2802 if (!quiet_warnings)
2804 if (!intel_syntax
2805 && ((i.types[0] & JumpAbsolute)
2806 != (operand_types[0] & JumpAbsolute)))
2808 as_warn (_("indirect %s without `*'"), t->name);
2811 if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
2812 == (IsPrefix | IgnoreSize))
2814 /* Warn them that a data or address size prefix doesn't
2815 affect assembly of the next line of code. */
2816 as_warn (_("stand-alone `%s' prefix"), t->name);
2820 /* Copy the template we found. */
2821 i.tm = *t;
2823 if (addr_prefix_disp != -1)
2824 i.tm.operand_types[addr_prefix_disp]
2825 = operand_types[addr_prefix_disp];
2827 if (found_reverse_match)
2829 /* If we found a reverse match we must alter the opcode
2830 direction bit. found_reverse_match holds bits to change
2831 (different for int & float insns). */
2833 i.tm.base_opcode ^= found_reverse_match;
2835 i.tm.operand_types[0] = operand_types[1];
2836 i.tm.operand_types[1] = operand_types[0];
2839 return 1;
2842 static int
2843 check_string (void)
2845 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
2846 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
2848 if (i.seg[0] != NULL && i.seg[0] != &es)
2850 as_bad (_("`%s' operand %d must use `%%es' segment"),
2851 i.tm.name,
2852 mem_op + 1);
2853 return 0;
2855 /* There's only ever one segment override allowed per instruction.
2856 This instruction possibly has a legal segment override on the
2857 second operand, so copy the segment to where non-string
2858 instructions store it, allowing common code. */
2859 i.seg[0] = i.seg[1];
2861 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
2863 if (i.seg[1] != NULL && i.seg[1] != &es)
2865 as_bad (_("`%s' operand %d must use `%%es' segment"),
2866 i.tm.name,
2867 mem_op + 2);
2868 return 0;
2871 return 1;
2874 static int
2875 process_suffix (void)
2877 /* If matched instruction specifies an explicit instruction mnemonic
2878 suffix, use it. */
2879 if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
2881 if (i.tm.opcode_modifier & Size16)
2882 i.suffix = WORD_MNEM_SUFFIX;
2883 else if (i.tm.opcode_modifier & Size64)
2884 i.suffix = QWORD_MNEM_SUFFIX;
2885 else
2886 i.suffix = LONG_MNEM_SUFFIX;
2888 else if (i.reg_operands)
2890 /* If there's no instruction mnemonic suffix we try to invent one
2891 based on register operands. */
2892 if (!i.suffix)
2894 /* We take i.suffix from the last register operand specified,
2895 Destination register type is more significant than source
2896 register type. crc32 in SSE4.2 prefers source register
2897 type. */
2898 if (i.tm.base_opcode == 0xf20f38f1)
2900 if ((i.types[0] & Reg))
2901 i.suffix = ((i.types[0] & Reg16) ? WORD_MNEM_SUFFIX :
2902 LONG_MNEM_SUFFIX);
2904 else if (i.tm.base_opcode == 0xf20f38f0)
2906 if ((i.types[0] & Reg8))
2907 i.suffix = BYTE_MNEM_SUFFIX;
2910 if (!i.suffix)
2912 int op;
2914 if (i.tm.base_opcode == 0xf20f38f1
2915 || i.tm.base_opcode == 0xf20f38f0)
2917 /* We have to know the operand size for crc32. */
2918 as_bad (_("ambiguous memory operand size for `%s`"),
2919 i.tm.name);
2920 return 0;
2923 for (op = i.operands; --op >= 0;)
2924 if ((i.types[op] & Reg)
2925 && !(i.tm.operand_types[op] & InOutPortReg))
2927 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
2928 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
2929 (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
2930 LONG_MNEM_SUFFIX);
2931 break;
2935 else if (i.suffix == BYTE_MNEM_SUFFIX)
2937 if (!check_byte_reg ())
2938 return 0;
2940 else if (i.suffix == LONG_MNEM_SUFFIX)
2942 if (!check_long_reg ())
2943 return 0;
2945 else if (i.suffix == QWORD_MNEM_SUFFIX)
2947 if (!check_qword_reg ())
2948 return 0;
2950 else if (i.suffix == WORD_MNEM_SUFFIX)
2952 if (!check_word_reg ())
2953 return 0;
2955 else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
2956 /* Do nothing if the instruction is going to ignore the prefix. */
2958 else
2959 abort ();
2961 else if ((i.tm.opcode_modifier & DefaultSize)
2962 && !i.suffix
2963 /* exclude fldenv/frstor/fsave/fstenv */
2964 && (i.tm.opcode_modifier & No_sSuf))
2966 i.suffix = stackop_size;
2968 else if (intel_syntax
2969 && !i.suffix
2970 && ((i.tm.operand_types[0] & JumpAbsolute)
2971 || (i.tm.opcode_modifier & (JumpByte|JumpInterSegment))
2972 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
2973 && i.tm.extension_opcode <= 3)))
2975 switch (flag_code)
2977 case CODE_64BIT:
2978 if (!(i.tm.opcode_modifier & No_qSuf))
2980 i.suffix = QWORD_MNEM_SUFFIX;
2981 break;
2983 case CODE_32BIT:
2984 if (!(i.tm.opcode_modifier & No_lSuf))
2985 i.suffix = LONG_MNEM_SUFFIX;
2986 break;
2987 case CODE_16BIT:
2988 if (!(i.tm.opcode_modifier & No_wSuf))
2989 i.suffix = WORD_MNEM_SUFFIX;
2990 break;
2994 if (!i.suffix)
2996 if (!intel_syntax)
2998 if (i.tm.opcode_modifier & W)
3000 as_bad (_("no instruction mnemonic suffix given and "
3001 "no register operands; can't size instruction"));
3002 return 0;
3005 else
3007 unsigned int suffixes = (~i.tm.opcode_modifier
3008 & (No_bSuf
3009 | No_wSuf
3010 | No_lSuf
3011 | No_sSuf
3012 | No_xSuf
3013 | No_qSuf));
3015 if ((i.tm.opcode_modifier & W)
3016 || ((suffixes & (suffixes - 1))
3017 && !(i.tm.opcode_modifier & (DefaultSize | IgnoreSize))))
3019 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
3020 return 0;
3025 /* Change the opcode based on the operand size given by i.suffix;
3026 We don't need to change things for byte insns. */
3028 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
3030 /* It's not a byte, select word/dword operation. */
3031 if (i.tm.opcode_modifier & W)
3033 if (i.tm.opcode_modifier & ShortForm)
3034 i.tm.base_opcode |= 8;
3035 else
3036 i.tm.base_opcode |= 1;
3039 /* Now select between word & dword operations via the operand
3040 size prefix, except for instructions that will ignore this
3041 prefix anyway. */
3042 if (i.tm.base_opcode == 0x0f01 && i.tm.extension_opcode == 0xc8)
3044 /* monitor in SSE3 is a very special case. The default size
3045 of AX is the size of mode. The address size override
3046 prefix will change the size of AX. */
3047 if (i.op->regs[0].reg_type &
3048 (flag_code == CODE_32BIT ? Reg16 : Reg32))
3049 if (!add_prefix (ADDR_PREFIX_OPCODE))
3050 return 0;
3052 else if (i.suffix != QWORD_MNEM_SUFFIX
3053 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
3054 && !(i.tm.opcode_modifier & (IgnoreSize | FloatMF))
3055 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
3056 || (flag_code == CODE_64BIT
3057 && (i.tm.opcode_modifier & JumpByte))))
3059 unsigned int prefix = DATA_PREFIX_OPCODE;
3061 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
3062 prefix = ADDR_PREFIX_OPCODE;
3064 if (!add_prefix (prefix))
3065 return 0;
3068 /* Set mode64 for an operand. */
3069 if (i.suffix == QWORD_MNEM_SUFFIX
3070 && flag_code == CODE_64BIT
3071 && (i.tm.opcode_modifier & NoRex64) == 0)
3073 /* Special case for xchg %rax,%rax. It is NOP and doesn't
3074 need rex64. */
3075 if (i.operands != 2
3076 || i.types [0] != (Acc | Reg64)
3077 || i.types [1] != (Acc | Reg64)
3078 || i.tm.base_opcode != 0x90)
3079 i.rex |= REX_W;
3082 /* Size floating point instruction. */
3083 if (i.suffix == LONG_MNEM_SUFFIX)
3084 if (i.tm.opcode_modifier & FloatMF)
3085 i.tm.base_opcode ^= 4;
3088 return 1;
3091 static int
3092 check_byte_reg (void)
3094 int op;
3096 for (op = i.operands; --op >= 0;)
3098 /* If this is an eight bit register, it's OK. If it's the 16 or
3099 32 bit version of an eight bit register, we will just use the
3100 low portion, and that's OK too. */
3101 if (i.types[op] & Reg8)
3102 continue;
3104 /* movzx, movsx, pextrb and pinsrb should not generate this
3105 warning. */
3106 if (intel_syntax
3107 && (i.tm.base_opcode == 0xfb7
3108 || i.tm.base_opcode == 0xfb6
3109 || i.tm.base_opcode == 0x63
3110 || i.tm.base_opcode == 0xfbe
3111 || i.tm.base_opcode == 0xfbf
3112 || i.tm.base_opcode == 0x660f3a14
3113 || i.tm.base_opcode == 0x660f3a20))
3114 continue;
3116 /* crc32 doesn't generate this warning. */
3117 if (i.tm.base_opcode == 0xf20f38f0)
3118 continue;
3120 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4)
3122 /* Prohibit these changes in the 64bit mode, since the
3123 lowering is more complicated. */
3124 if (flag_code == CODE_64BIT
3125 && (i.tm.operand_types[op] & InOutPortReg) == 0)
3127 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3128 register_prefix, i.op[op].regs->reg_name,
3129 i.suffix);
3130 return 0;
3132 #if REGISTER_WARNINGS
3133 if (!quiet_warnings
3134 && (i.tm.operand_types[op] & InOutPortReg) == 0)
3135 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3136 register_prefix,
3137 (i.op[op].regs + (i.types[op] & Reg16
3138 ? REGNAM_AL - REGNAM_AX
3139 : REGNAM_AL - REGNAM_EAX))->reg_name,
3140 register_prefix,
3141 i.op[op].regs->reg_name,
3142 i.suffix);
3143 #endif
3144 continue;
3146 /* Any other register is bad. */
3147 if (i.types[op] & (Reg | RegMMX | RegXMM
3148 | SReg2 | SReg3
3149 | Control | Debug | Test
3150 | FloatReg | FloatAcc))
3152 as_bad (_("`%s%s' not allowed with `%s%c'"),
3153 register_prefix,
3154 i.op[op].regs->reg_name,
3155 i.tm.name,
3156 i.suffix);
3157 return 0;
3160 return 1;
3163 static int
3164 check_long_reg (void)
3166 int op;
3168 for (op = i.operands; --op >= 0;)
3169 /* Reject eight bit registers, except where the template requires
3170 them. (eg. movzb) */
3171 if ((i.types[op] & Reg8) != 0
3172 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
3174 as_bad (_("`%s%s' not allowed with `%s%c'"),
3175 register_prefix,
3176 i.op[op].regs->reg_name,
3177 i.tm.name,
3178 i.suffix);
3179 return 0;
3181 /* Warn if the e prefix on a general reg is missing. */
3182 else if ((!quiet_warnings || flag_code == CODE_64BIT)
3183 && (i.types[op] & Reg16) != 0
3184 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
3186 /* Prohibit these changes in the 64bit mode, since the
3187 lowering is more complicated. */
3188 if (flag_code == CODE_64BIT)
3190 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3191 register_prefix, i.op[op].regs->reg_name,
3192 i.suffix);
3193 return 0;
3195 #if REGISTER_WARNINGS
3196 else
3197 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3198 register_prefix,
3199 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
3200 register_prefix,
3201 i.op[op].regs->reg_name,
3202 i.suffix);
3203 #endif
3205 /* Warn if the r prefix on a general reg is missing. */
3206 else if ((i.types[op] & Reg64) != 0
3207 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
3209 if (intel_syntax
3210 && i.tm.base_opcode == 0xf30f2d
3211 && (i.types[0] & RegXMM) == 0)
3213 /* cvtss2si converts DWORD memory to Reg64. We want
3214 REX byte. */
3215 i.suffix = QWORD_MNEM_SUFFIX;
3217 else
3219 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3220 register_prefix, i.op[op].regs->reg_name,
3221 i.suffix);
3222 return 0;
3225 return 1;
3228 static int
3229 check_qword_reg (void)
3231 int op;
3233 for (op = i.operands; --op >= 0; )
3234 /* Reject eight bit registers, except where the template requires
3235 them. (eg. movzb) */
3236 if ((i.types[op] & Reg8) != 0
3237 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
3239 as_bad (_("`%s%s' not allowed with `%s%c'"),
3240 register_prefix,
3241 i.op[op].regs->reg_name,
3242 i.tm.name,
3243 i.suffix);
3244 return 0;
3246 /* Warn if the e prefix on a general reg is missing. */
3247 else if ((i.types[op] & (Reg16 | Reg32)) != 0
3248 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
3250 /* Prohibit these changes in the 64bit mode, since the
3251 lowering is more complicated. */
3252 if (intel_syntax
3253 && i.tm.base_opcode == 0xf20f2d
3254 && (i.types[0] & RegXMM) == 0)
3256 /* cvtsd2si converts QWORD memory to Reg32. We don't want
3257 REX byte. */
3258 i.suffix = LONG_MNEM_SUFFIX;
3260 else
3262 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3263 register_prefix, i.op[op].regs->reg_name,
3264 i.suffix);
3265 return 0;
3268 return 1;
3271 static int
3272 check_word_reg (void)
3274 int op;
3275 for (op = i.operands; --op >= 0;)
3276 /* Reject eight bit registers, except where the template requires
3277 them. (eg. movzb) */
3278 if ((i.types[op] & Reg8) != 0
3279 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
3281 as_bad (_("`%s%s' not allowed with `%s%c'"),
3282 register_prefix,
3283 i.op[op].regs->reg_name,
3284 i.tm.name,
3285 i.suffix);
3286 return 0;
3288 /* Warn if the e prefix on a general reg is present. */
3289 else if ((!quiet_warnings || flag_code == CODE_64BIT)
3290 && (i.types[op] & Reg32) != 0
3291 && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
3293 /* Prohibit these changes in the 64bit mode, since the
3294 lowering is more complicated. */
3295 if (flag_code == CODE_64BIT)
3297 as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
3298 register_prefix, i.op[op].regs->reg_name,
3299 i.suffix);
3300 return 0;
3302 else
3303 #if REGISTER_WARNINGS
3304 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
3305 register_prefix,
3306 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
3307 register_prefix,
3308 i.op[op].regs->reg_name,
3309 i.suffix);
3310 #endif
3312 return 1;
3315 static int
3316 finalize_imm (void)
3318 unsigned int overlap0, overlap1, overlap2;
3320 overlap0 = i.types[0] & i.tm.operand_types[0];
3321 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64))
3322 && overlap0 != Imm8 && overlap0 != Imm8S
3323 && overlap0 != Imm16 && overlap0 != Imm32S
3324 && overlap0 != Imm32 && overlap0 != Imm64)
3326 if (i.suffix)
3328 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
3329 ? Imm8 | Imm8S
3330 : (i.suffix == WORD_MNEM_SUFFIX
3331 ? Imm16
3332 : (i.suffix == QWORD_MNEM_SUFFIX
3333 ? Imm64 | Imm32S
3334 : Imm32)));
3336 else if (overlap0 == (Imm16 | Imm32S | Imm32)
3337 || overlap0 == (Imm16 | Imm32)
3338 || overlap0 == (Imm16 | Imm32S))
3340 overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
3341 ? Imm16 : Imm32S);
3343 if (overlap0 != Imm8 && overlap0 != Imm8S
3344 && overlap0 != Imm16 && overlap0 != Imm32S
3345 && overlap0 != Imm32 && overlap0 != Imm64)
3347 as_bad (_("no instruction mnemonic suffix given; "
3348 "can't determine immediate size"));
3349 return 0;
3352 i.types[0] = overlap0;
3354 overlap1 = i.types[1] & i.tm.operand_types[1];
3355 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32 | Imm64))
3356 && overlap1 != Imm8 && overlap1 != Imm8S
3357 && overlap1 != Imm16 && overlap1 != Imm32S
3358 && overlap1 != Imm32 && overlap1 != Imm64)
3360 if (i.suffix)
3362 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
3363 ? Imm8 | Imm8S
3364 : (i.suffix == WORD_MNEM_SUFFIX
3365 ? Imm16
3366 : (i.suffix == QWORD_MNEM_SUFFIX
3367 ? Imm64 | Imm32S
3368 : Imm32)));
3370 else if (overlap1 == (Imm16 | Imm32 | Imm32S)
3371 || overlap1 == (Imm16 | Imm32)
3372 || overlap1 == (Imm16 | Imm32S))
3374 overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
3375 ? Imm16 : Imm32S);
3377 if (overlap1 != Imm8 && overlap1 != Imm8S
3378 && overlap1 != Imm16 && overlap1 != Imm32S
3379 && overlap1 != Imm32 && overlap1 != Imm64)
3381 as_bad (_("no instruction mnemonic suffix given; "
3382 "can't determine immediate size %x %c"),
3383 overlap1, i.suffix);
3384 return 0;
3387 i.types[1] = overlap1;
3389 overlap2 = i.types[2] & i.tm.operand_types[2];
3390 assert ((overlap2 & Imm) == 0);
3391 i.types[2] = overlap2;
3393 return 1;
3396 static int
3397 process_operands (void)
3399 /* Default segment register this instruction will use for memory
3400 accesses. 0 means unknown. This is only for optimizing out
3401 unnecessary segment overrides. */
3402 const seg_entry *default_seg = 0;
3404 /* The imul $imm, %reg instruction is converted into
3405 imul $imm, %reg, %reg, and the clr %reg instruction
3406 is converted into xor %reg, %reg. */
3407 if (i.tm.opcode_modifier & RegKludge)
3409 if ((i.tm.cpu_flags & CpuSSE4_1))
3411 /* The first operand in instruction blendvpd, blendvps and
3412 pblendvb in SSE4.1 is implicit and must be xmm0. */
3413 assert (i.operands == 3
3414 && i.reg_operands >= 2
3415 && i.types[0] == RegXMM);
3416 if (i.op[0].regs->reg_num != 0)
3418 if (intel_syntax)
3419 as_bad (_("the last operand of `%s' must be `%sxmm0'"),
3420 i.tm.name, register_prefix);
3421 else
3422 as_bad (_("the first operand of `%s' must be `%sxmm0'"),
3423 i.tm.name, register_prefix);
3424 return 0;
3426 i.op[0] = i.op[1];
3427 i.op[1] = i.op[2];
3428 i.types[0] = i.types[1];
3429 i.types[1] = i.types[2];
3430 i.operands--;
3431 i.reg_operands--;
3433 /* We need to adjust fields in i.tm since they are used by
3434 build_modrm_byte. */
3435 i.tm.operand_types [0] = i.tm.operand_types [1];
3436 i.tm.operand_types [1] = i.tm.operand_types [2];
3437 i.tm.operands--;
3439 else
3441 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
3442 /* Pretend we saw the extra register operand. */
3443 assert (i.reg_operands == 1
3444 && i.op[first_reg_op + 1].regs == 0);
3445 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
3446 i.types[first_reg_op + 1] = i.types[first_reg_op];
3447 i.operands++;
3448 i.reg_operands++;
3452 if (i.tm.opcode_modifier & ShortForm)
3454 if (i.types[0] & (SReg2 | SReg3))
3456 if (i.tm.base_opcode == POP_SEG_SHORT
3457 && i.op[0].regs->reg_num == 1)
3459 as_bad (_("you can't `pop %%cs'"));
3460 return 0;
3462 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
3463 if ((i.op[0].regs->reg_flags & RegRex) != 0)
3464 i.rex |= REX_B;
3466 else
3468 /* The register or float register operand is in operand 0 or 1. */
3469 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
3470 /* Register goes in low 3 bits of opcode. */
3471 i.tm.base_opcode |= i.op[op].regs->reg_num;
3472 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3473 i.rex |= REX_B;
3474 if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
3476 /* Warn about some common errors, but press on regardless.
3477 The first case can be generated by gcc (<= 2.8.1). */
3478 if (i.operands == 2)
3480 /* Reversed arguments on faddp, fsubp, etc. */
3481 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
3482 register_prefix, i.op[1].regs->reg_name,
3483 register_prefix, i.op[0].regs->reg_name);
3485 else
3487 /* Extraneous `l' suffix on fp insn. */
3488 as_warn (_("translating to `%s %s%s'"), i.tm.name,
3489 register_prefix, i.op[0].regs->reg_name);
3494 else if (i.tm.opcode_modifier & Modrm)
3496 /* The opcode is completed (modulo i.tm.extension_opcode which
3497 must be put into the modrm byte). Now, we make the modrm and
3498 index base bytes based on all the info we've collected. */
3500 default_seg = build_modrm_byte ();
3502 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
3504 default_seg = &ds;
3506 else if ((i.tm.opcode_modifier & IsString) != 0)
3508 /* For the string instructions that allow a segment override
3509 on one of their operands, the default segment is ds. */
3510 default_seg = &ds;
3513 if ((i.tm.base_opcode == 0x8d /* lea */
3514 || (i.tm.cpu_flags & CpuSVME))
3515 && i.seg[0] && !quiet_warnings)
3516 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
3518 /* If a segment was explicitly specified, and the specified segment
3519 is not the default, use an opcode prefix to select it. If we
3520 never figured out what the default segment is, then default_seg
3521 will be zero at this point, and the specified segment prefix will
3522 always be used. */
3523 if ((i.seg[0]) && (i.seg[0] != default_seg))
3525 if (!add_prefix (i.seg[0]->seg_prefix))
3526 return 0;
3528 return 1;
3531 static const seg_entry *
3532 build_modrm_byte (void)
3534 const seg_entry *default_seg = 0;
3536 /* i.reg_operands MUST be the number of real register operands;
3537 implicit registers do not count. */
3538 if (i.reg_operands == 2)
3540 unsigned int source, dest;
3542 switch (i.operands)
3544 case 2:
3545 source = 0;
3546 break;
3547 case 3:
3548 /* When there are 3 operands, one of them may be immediate,
3549 which may be the first or the last operand. Otherwise,
3550 the first operand must be shift count register (cl). */
3551 assert (i.imm_operands == 1
3552 || (i.imm_operands == 0
3553 && (i.types[0] & ShiftCount)));
3554 source = (i.types[0] & (Imm | ShiftCount)) ? 1 : 0;
3555 break;
3556 case 4:
3557 /* When there are 4 operands, the first two must be immediate
3558 operands. The source operand will be the 3rd one. */
3559 assert (i.imm_operands == 2
3560 && (i.types[0] & Imm)
3561 && (i.types[1] & Imm));
3562 source = 2;
3563 break;
3564 default:
3565 abort ();
3568 dest = source + 1;
3570 i.rm.mode = 3;
3571 /* One of the register operands will be encoded in the i.tm.reg
3572 field, the other in the combined i.tm.mode and i.tm.regmem
3573 fields. If no form of this instruction supports a memory
3574 destination operand, then we assume the source operand may
3575 sometimes be a memory operand and so we need to store the
3576 destination in the i.rm.reg field. */
3577 if ((i.tm.operand_types[dest] & (AnyMem | RegMem)) == 0)
3579 i.rm.reg = i.op[dest].regs->reg_num;
3580 i.rm.regmem = i.op[source].regs->reg_num;
3581 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
3582 i.rex |= REX_R;
3583 if ((i.op[source].regs->reg_flags & RegRex) != 0)
3584 i.rex |= REX_B;
3586 else
3588 i.rm.reg = i.op[source].regs->reg_num;
3589 i.rm.regmem = i.op[dest].regs->reg_num;
3590 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
3591 i.rex |= REX_B;
3592 if ((i.op[source].regs->reg_flags & RegRex) != 0)
3593 i.rex |= REX_R;
3595 if (flag_code != CODE_64BIT && (i.rex & (REX_R | REX_B)))
3597 if (!((i.types[0] | i.types[1]) & Control))
3598 abort ();
3599 i.rex &= ~(REX_R | REX_B);
3600 add_prefix (LOCK_PREFIX_OPCODE);
3603 else
3604 { /* If it's not 2 reg operands... */
3605 if (i.mem_operands)
3607 unsigned int fake_zero_displacement = 0;
3608 unsigned int op;
3610 for (op = 0; op < i.operands; op++)
3611 if ((i.types[op] & AnyMem))
3612 break;
3613 assert (op < i.operands);
3615 default_seg = &ds;
3617 if (i.base_reg == 0)
3619 i.rm.mode = 0;
3620 if (!i.disp_operands)
3621 fake_zero_displacement = 1;
3622 if (i.index_reg == 0)
3624 /* Operand is just <disp> */
3625 if (flag_code == CODE_64BIT)
3627 /* 64bit mode overwrites the 32bit absolute
3628 addressing by RIP relative addressing and
3629 absolute addressing is encoded by one of the
3630 redundant SIB forms. */
3631 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3632 i.sib.base = NO_BASE_REGISTER;
3633 i.sib.index = NO_INDEX_REGISTER;
3634 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0)
3635 ? Disp32S : Disp32);
3637 else if ((flag_code == CODE_16BIT)
3638 ^ (i.prefix[ADDR_PREFIX] != 0))
3640 i.rm.regmem = NO_BASE_REGISTER_16;
3641 i.types[op] = Disp16;
3643 else
3645 i.rm.regmem = NO_BASE_REGISTER;
3646 i.types[op] = Disp32;
3649 else /* !i.base_reg && i.index_reg */
3651 i.sib.index = i.index_reg->reg_num;
3652 i.sib.base = NO_BASE_REGISTER;
3653 i.sib.scale = i.log2_scale_factor;
3654 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3655 i.types[op] &= ~Disp;
3656 if (flag_code != CODE_64BIT)
3657 i.types[op] |= Disp32; /* Must be 32 bit */
3658 else
3659 i.types[op] |= Disp32S;
3660 if ((i.index_reg->reg_flags & RegRex) != 0)
3661 i.rex |= REX_X;
3664 /* RIP addressing for 64bit mode. */
3665 else if (i.base_reg->reg_type == BaseIndex)
3667 i.rm.regmem = NO_BASE_REGISTER;
3668 i.types[op] &= ~ Disp;
3669 i.types[op] |= Disp32S;
3670 i.flags[op] |= Operand_PCrel;
3671 if (! i.disp_operands)
3672 fake_zero_displacement = 1;
3674 else if (i.base_reg->reg_type & Reg16)
3676 switch (i.base_reg->reg_num)
3678 case 3: /* (%bx) */
3679 if (i.index_reg == 0)
3680 i.rm.regmem = 7;
3681 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
3682 i.rm.regmem = i.index_reg->reg_num - 6;
3683 break;
3684 case 5: /* (%bp) */
3685 default_seg = &ss;
3686 if (i.index_reg == 0)
3688 i.rm.regmem = 6;
3689 if ((i.types[op] & Disp) == 0)
3691 /* fake (%bp) into 0(%bp) */
3692 i.types[op] |= Disp8;
3693 fake_zero_displacement = 1;
3696 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
3697 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
3698 break;
3699 default: /* (%si) -> 4 or (%di) -> 5 */
3700 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
3702 i.rm.mode = mode_from_disp_size (i.types[op]);
3704 else /* i.base_reg and 32/64 bit mode */
3706 if (flag_code == CODE_64BIT
3707 && (i.types[op] & Disp))
3708 i.types[op] = ((i.types[op] & Disp8)
3709 | (i.prefix[ADDR_PREFIX] == 0
3710 ? Disp32S : Disp32));
3712 i.rm.regmem = i.base_reg->reg_num;
3713 if ((i.base_reg->reg_flags & RegRex) != 0)
3714 i.rex |= REX_B;
3715 i.sib.base = i.base_reg->reg_num;
3716 /* x86-64 ignores REX prefix bit here to avoid decoder
3717 complications. */
3718 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
3720 default_seg = &ss;
3721 if (i.disp_operands == 0)
3723 fake_zero_displacement = 1;
3724 i.types[op] |= Disp8;
3727 else if (i.base_reg->reg_num == ESP_REG_NUM)
3729 default_seg = &ss;
3731 i.sib.scale = i.log2_scale_factor;
3732 if (i.index_reg == 0)
3734 /* <disp>(%esp) becomes two byte modrm with no index
3735 register. We've already stored the code for esp
3736 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
3737 Any base register besides %esp will not use the
3738 extra modrm byte. */
3739 i.sib.index = NO_INDEX_REGISTER;
3740 #if !SCALE1_WHEN_NO_INDEX
3741 /* Another case where we force the second modrm byte. */
3742 if (i.log2_scale_factor)
3743 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3744 #endif
3746 else
3748 i.sib.index = i.index_reg->reg_num;
3749 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3750 if ((i.index_reg->reg_flags & RegRex) != 0)
3751 i.rex |= REX_X;
3754 if (i.disp_operands
3755 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
3756 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
3757 i.rm.mode = 0;
3758 else
3759 i.rm.mode = mode_from_disp_size (i.types[op]);
3762 if (fake_zero_displacement)
3764 /* Fakes a zero displacement assuming that i.types[op]
3765 holds the correct displacement size. */
3766 expressionS *exp;
3768 assert (i.op[op].disps == 0);
3769 exp = &disp_expressions[i.disp_operands++];
3770 i.op[op].disps = exp;
3771 exp->X_op = O_constant;
3772 exp->X_add_number = 0;
3773 exp->X_add_symbol = (symbolS *) 0;
3774 exp->X_op_symbol = (symbolS *) 0;
3778 /* Fill in i.rm.reg or i.rm.regmem field with register operand
3779 (if any) based on i.tm.extension_opcode. Again, we must be
3780 careful to make sure that segment/control/debug/test/MMX
3781 registers are coded into the i.rm.reg field. */
3782 if (i.reg_operands)
3784 unsigned int op;
3786 for (op = 0; op < i.operands; op++)
3787 if ((i.types[op] & (Reg | RegMMX | RegXMM
3788 | SReg2 | SReg3
3789 | Control | Debug | Test)))
3790 break;
3791 assert (op < i.operands);
3793 /* If there is an extension opcode to put here, the register
3794 number must be put into the regmem field. */
3795 if (i.tm.extension_opcode != None)
3797 i.rm.regmem = i.op[op].regs->reg_num;
3798 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3799 i.rex |= REX_B;
3801 else
3803 i.rm.reg = i.op[op].regs->reg_num;
3804 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3805 i.rex |= REX_R;
3808 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
3809 must set it to 3 to indicate this is a register operand
3810 in the regmem field. */
3811 if (!i.mem_operands)
3812 i.rm.mode = 3;
3815 /* Fill in i.rm.reg field with extension opcode (if any). */
3816 if (i.tm.extension_opcode != None)
3817 i.rm.reg = i.tm.extension_opcode;
3819 return default_seg;
3822 static void
3823 output_branch (void)
3825 char *p;
3826 int code16;
3827 int prefix;
3828 relax_substateT subtype;
3829 symbolS *sym;
3830 offsetT off;
3832 code16 = 0;
3833 if (flag_code == CODE_16BIT)
3834 code16 = CODE16;
3836 prefix = 0;
3837 if (i.prefix[DATA_PREFIX] != 0)
3839 prefix = 1;
3840 i.prefixes -= 1;
3841 code16 ^= CODE16;
3843 /* Pentium4 branch hints. */
3844 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3845 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3847 prefix++;
3848 i.prefixes--;
3850 if (i.prefix[REX_PREFIX] != 0)
3852 prefix++;
3853 i.prefixes--;
3856 if (i.prefixes != 0 && !intel_syntax)
3857 as_warn (_("skipping prefixes on this instruction"));
3859 /* It's always a symbol; End frag & setup for relax.
3860 Make sure there is enough room in this frag for the largest
3861 instruction we may generate in md_convert_frag. This is 2
3862 bytes for the opcode and room for the prefix and largest
3863 displacement. */
3864 frag_grow (prefix + 2 + 4);
3865 /* Prefix and 1 opcode byte go in fr_fix. */
3866 p = frag_more (prefix + 1);
3867 if (i.prefix[DATA_PREFIX] != 0)
3868 *p++ = DATA_PREFIX_OPCODE;
3869 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
3870 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
3871 *p++ = i.prefix[SEG_PREFIX];
3872 if (i.prefix[REX_PREFIX] != 0)
3873 *p++ = i.prefix[REX_PREFIX];
3874 *p = i.tm.base_opcode;
3876 if ((unsigned char) *p == JUMP_PC_RELATIVE)
3877 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
3878 else if ((cpu_arch_flags & Cpu386) != 0)
3879 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
3880 else
3881 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
3882 subtype |= code16;
3884 sym = i.op[0].disps->X_add_symbol;
3885 off = i.op[0].disps->X_add_number;
3887 if (i.op[0].disps->X_op != O_constant
3888 && i.op[0].disps->X_op != O_symbol)
3890 /* Handle complex expressions. */
3891 sym = make_expr_symbol (i.op[0].disps);
3892 off = 0;
3895 /* 1 possible extra opcode + 4 byte displacement go in var part.
3896 Pass reloc in fr_var. */
3897 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
3900 static void
3901 output_jump (void)
3903 char *p;
3904 int size;
3905 fixS *fixP;
3907 if (i.tm.opcode_modifier & JumpByte)
3909 /* This is a loop or jecxz type instruction. */
3910 size = 1;
3911 if (i.prefix[ADDR_PREFIX] != 0)
3913 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
3914 i.prefixes -= 1;
3916 /* Pentium4 branch hints. */
3917 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3918 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3920 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
3921 i.prefixes--;
3924 else
3926 int code16;
3928 code16 = 0;
3929 if (flag_code == CODE_16BIT)
3930 code16 = CODE16;
3932 if (i.prefix[DATA_PREFIX] != 0)
3934 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
3935 i.prefixes -= 1;
3936 code16 ^= CODE16;
3939 size = 4;
3940 if (code16)
3941 size = 2;
3944 if (i.prefix[REX_PREFIX] != 0)
3946 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
3947 i.prefixes -= 1;
3950 if (i.prefixes != 0 && !intel_syntax)
3951 as_warn (_("skipping prefixes on this instruction"));
3953 p = frag_more (1 + size);
3954 *p++ = i.tm.base_opcode;
3956 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3957 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
3959 /* All jumps handled here are signed, but don't use a signed limit
3960 check for 32 and 16 bit jumps as we want to allow wrap around at
3961 4G and 64k respectively. */
3962 if (size == 1)
3963 fixP->fx_signed = 1;
3966 static void
3967 output_interseg_jump (void)
3969 char *p;
3970 int size;
3971 int prefix;
3972 int code16;
3974 code16 = 0;
3975 if (flag_code == CODE_16BIT)
3976 code16 = CODE16;
3978 prefix = 0;
3979 if (i.prefix[DATA_PREFIX] != 0)
3981 prefix = 1;
3982 i.prefixes -= 1;
3983 code16 ^= CODE16;
3985 if (i.prefix[REX_PREFIX] != 0)
3987 prefix++;
3988 i.prefixes -= 1;
3991 size = 4;
3992 if (code16)
3993 size = 2;
3995 if (i.prefixes != 0 && !intel_syntax)
3996 as_warn (_("skipping prefixes on this instruction"));
3998 /* 1 opcode; 2 segment; offset */
3999 p = frag_more (prefix + 1 + 2 + size);
4001 if (i.prefix[DATA_PREFIX] != 0)
4002 *p++ = DATA_PREFIX_OPCODE;
4004 if (i.prefix[REX_PREFIX] != 0)
4005 *p++ = i.prefix[REX_PREFIX];
4007 *p++ = i.tm.base_opcode;
4008 if (i.op[1].imms->X_op == O_constant)
4010 offsetT n = i.op[1].imms->X_add_number;
4012 if (size == 2
4013 && !fits_in_unsigned_word (n)
4014 && !fits_in_signed_word (n))
4016 as_bad (_("16-bit jump out of range"));
4017 return;
4019 md_number_to_chars (p, n, size);
4021 else
4022 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4023 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
4024 if (i.op[0].imms->X_op != O_constant)
4025 as_bad (_("can't handle non absolute segment in `%s'"),
4026 i.tm.name);
4027 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
4030 static void
4031 insert_sandbox_code (void)
4033 char* p;
4034 int align_mask = (1 << nacl_alignment) - 1;
4036 if (getenv("NACL_DEBUG_ALIGN")) {
4037 p = frag_more (12);
4038 p[0] = 0xF7; p[1] = 0xC0 + i.rm.regmem;
4039 p[2] = align_mask; p[3] = p[4] = p[5] = 0x00; // TEST reg, align_mask
4040 p[6] = 0x74; p[7] = 0x04; // JZ +4
4041 p[8] = 0xCC; // INT3
4042 p[9] = 0x0F; p[10] = 0x1F; p[11] = 0x00; // NOP
4044 else {
4045 p = frag_more (3);
4046 p[0] = 0x83; // AND instruction.
4047 p[1] = (0xe0 + i.rm.regmem); // mod = 11, reg = 100, rm = i.rm.regmem;
4048 // p[2] = 0xff & ~align_mask;
4049 p[2] = 0xff;
4054 static int
4055 frag_is_a_call (void)
4057 if (i.tm.base_opcode == 0xe8) {
4058 // direct calls
4059 return 1;
4061 else if (i.tm.base_opcode == 0xff) {
4062 // possibly indirect calls
4063 return (i.rm.mode == 3) && (i.rm.reg == 2);
4065 else {
4066 return 0;
4071 static void
4072 output_insn (void)
4074 fragS *insn_start_frag;
4075 offsetT insn_start_off;
4077 // DCS -- New code for NativeClient.
4078 // Frag_align_code sets the alignment on the current fragment and may
4079 // create a new one. Because of this we remember the current fragment
4080 // before calling frag_align_code.
4081 insn_start_frag = frag_now;
4083 if (nacl_alignment > 0) {
4084 frag_align_code(0, 0);
4085 #ifdef DCS
4086 // TODO: this code seems unnecessary. Remove it.
4087 // Really ugly stuff to avoid splitting call and its subsequent alignment
4088 // across two fragments.
4089 if (frag_is_a_call() &&
4090 obstack_room(&frchain_now->frch_obstack) < (unsigned)((1<<(nacl_alignment+1))-1)) {
4091 fragS *temp_frag = frag_now;
4092 frag_align_code(nacl_alignment + 1, 0);
4093 temp_frag->fr_offset = 0;
4094 temp_frag->fr_subtype = 0;
4096 #endif /* DCS */
4099 /* Tie dwarf2 debug info to the address at the start of the insn.
4100 We can't do this after the insn has been output as the current
4101 frag may have been closed off. eg. by frag_var. */
4102 dwarf2_emit_insn (0);
4104 insn_start_off = frag_now_fix ();
4106 if (nacl_alignment > 0) {
4107 if (!strcmp(i.tm.name, "naclcall")) {
4108 insert_sandbox_code();
4110 else if (!strcmp(i.tm.name, "nacljmp")) {
4111 insert_sandbox_code();
4113 else if (!strcmp(i.tm.name, "naclret")) {
4114 insert_sandbox_code();
4118 /* Output jumps. */
4119 if (i.tm.opcode_modifier & Jump)
4120 output_branch ();
4121 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
4122 output_jump ();
4123 else if (i.tm.opcode_modifier & JumpInterSegment)
4124 output_interseg_jump ();
4125 else
4127 /* Output normal instructions here. */
4128 char *p;
4129 unsigned char *q;
4130 unsigned int prefix;
4132 /* All opcodes on i386 have either 1 or 2 bytes. SSSE3 and
4133 SSE4 instructions have 3 bytes. We may use one more higher
4134 byte to specify a prefix the instruction requires. Exclude
4135 instructions which are in both SSE4 and ABM. */
4136 if ((i.tm.cpu_flags & (CpuSSSE3 | CpuSSE4)) != 0
4137 && (i.tm.cpu_flags & CpuABM) == 0)
4139 if (i.tm.base_opcode & 0xff000000)
4141 prefix = (i.tm.base_opcode >> 24) & 0xff;
4142 goto check_prefix;
4145 else if ((i.tm.base_opcode & 0xff0000) != 0)
4147 prefix = (i.tm.base_opcode >> 16) & 0xff;
4148 if ((i.tm.cpu_flags & CpuPadLock) != 0)
4150 check_prefix:
4151 if (prefix != REPE_PREFIX_OPCODE
4152 || i.prefix[LOCKREP_PREFIX] != REPE_PREFIX_OPCODE)
4153 add_prefix (prefix);
4155 else
4156 add_prefix (prefix);
4159 /* The prefix bytes. */
4160 for (q = i.prefix;
4161 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
4162 q++)
4164 if (*q)
4166 p = frag_more (1);
4167 md_number_to_chars (p, (valueT) *q, 1);
4171 /* Now the opcode; be careful about word order here! */
4172 if (fits_in_unsigned_byte (i.tm.base_opcode))
4174 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
4176 else
4178 if ((i.tm.cpu_flags & (CpuSSSE3 | CpuSSE4)) != 0
4179 && (i.tm.cpu_flags & CpuABM) == 0)
4181 p = frag_more (3);
4182 *p++ = (i.tm.base_opcode >> 16) & 0xff;
4184 else
4185 p = frag_more (2);
4187 /* Put out high byte first: can't use md_number_to_chars! */
4188 *p++ = (i.tm.base_opcode >> 8) & 0xff;
4189 *p = i.tm.base_opcode & 0xff;
4192 /* Now the modrm byte and sib byte (if present). */
4193 if (i.tm.opcode_modifier & Modrm)
4195 p = frag_more (1);
4196 md_number_to_chars (p,
4197 (valueT) (i.rm.regmem << 0
4198 | i.rm.reg << 3
4199 | i.rm.mode << 6),
4201 /* If i.rm.regmem == ESP (4)
4202 && i.rm.mode != (Register mode)
4203 && not 16 bit
4204 ==> need second modrm byte. */
4205 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
4206 && i.rm.mode != 3
4207 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
4209 p = frag_more (1);
4210 md_number_to_chars (p,
4211 (valueT) (i.sib.base << 0
4212 | i.sib.index << 3
4213 | i.sib.scale << 6),
4218 if (i.disp_operands)
4219 output_disp (insn_start_frag, insn_start_off);
4221 if (i.imm_operands)
4222 output_imm (insn_start_frag, insn_start_off);
4225 #ifdef DEBUG386
4226 if (flag_debug)
4228 pi ("" /*line*/, &i);
4230 #endif /* DEBUG386 */
4232 * We want to make sure no instruction straddles a (1 << nacl_alignment)
4233 * boundary. We do this by setting the fragment alignment to
4234 * (1 << nacl_alignment), but allowing no more than the size of the
4235 * instruction as fill.
4237 if (nacl_alignment > 0) {
4238 int align_base;
4239 int call_align;
4240 int instrsize = (int) frag_now_fix();
4243 * "library mode" enables compatible library builds for either 16 or
4244 * 32 byte alignment. Using the strictest alignment requirement for
4245 * instructions makes them 0mod16 aligned. Calls need to end a 32 byte
4246 * region.
4248 if (nacl_library_mode) {
4249 align_base = 4;
4250 call_align = 5;
4252 else {
4253 align_base = nacl_alignment;
4254 call_align = nacl_alignment;
4257 switch (instrsize) {
4258 case 0:
4259 // We get zero size for jump instructions. Go to their biggest.
4260 insn_start_frag->fr_offset = align_base;
4261 insn_start_frag->fr_subtype = 5;
4262 break;
4264 case 1:
4265 if ((i.tm.base_opcode == 0xf3) || (i.tm.base_opcode == 0xf2) ||
4266 (i.tm.base_opcode == 0xf0)) {
4267 // rep and lock refixes are treated as separate instructions.
4268 // I don't know any other patch but to force an alignment to 0,
4269 // i.e., waste as many bytes as it takes.
4270 insn_start_frag->fr_offset = align_base;
4271 insn_start_frag->fr_subtype = 0;
4273 else {
4274 // Don't align other one-byte instructions.
4275 insn_start_frag->fr_offset = 0;
4276 insn_start_frag->fr_subtype = 0;
4278 break;
4280 default:
4281 // Don't use more than size-1 bytes to pad.
4282 insn_start_frag->fr_offset = align_base;
4283 insn_start_frag->fr_subtype = instrsize-1;
4284 break;
4289 * Calls need to fall at the end of a (1 << call_align) region. We
4290 * make sure there are no instructions after the call until the next
4291 * alignment. During writing of the object we swap the nops before the
4292 * instruction.
4294 if (frag_is_a_call()) {
4295 frag_now->is_call = 1;
4296 frag_align_code(call_align,0);
4301 /* Return the size of the displacement operand N. */
4303 static int
4304 disp_size (unsigned int n)
4306 int size = 4;
4307 if (i.types[n] & (Disp8 | Disp16 | Disp64))
4309 size = 2;
4310 if (i.types[n] & Disp8)
4311 size = 1;
4312 if (i.types[n] & Disp64)
4313 size = 8;
4315 return size;
4318 /* Return the size of the immediate operand N. */
4320 static int
4321 imm_size (unsigned int n)
4323 int size = 4;
4324 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
4326 size = 2;
4327 if (i.types[n] & (Imm8 | Imm8S))
4328 size = 1;
4329 if (i.types[n] & Imm64)
4330 size = 8;
4332 return size;
4335 static void
4336 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
4338 char *p;
4339 unsigned int n;
4341 for (n = 0; n < i.operands; n++)
4343 if (i.types[n] & Disp)
4345 if (i.op[n].disps->X_op == O_constant)
4347 int size = disp_size (n);
4348 offsetT val;
4350 val = offset_in_range (i.op[n].disps->X_add_number,
4351 size);
4352 p = frag_more (size);
4353 md_number_to_chars (p, val, size);
4355 else
4357 enum bfd_reloc_code_real reloc_type;
4358 int size = disp_size (n);
4359 int sign = (i.types[n] & Disp32S) != 0;
4360 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
4362 /* We can't have 8 bit displacement here. */
4363 assert ((i.types[n] & Disp8) == 0);
4365 /* The PC relative address is computed relative
4366 to the instruction boundary, so in case immediate
4367 fields follows, we need to adjust the value. */
4368 if (pcrel && i.imm_operands)
4370 unsigned int n1;
4371 int sz = 0;
4373 for (n1 = 0; n1 < i.operands; n1++)
4374 if (i.types[n1] & Imm)
4376 /* Only one immediate is allowed for PC
4377 relative address. */
4378 assert (sz == 0);
4379 sz = imm_size (n1);
4380 i.op[n].disps->X_add_number -= sz;
4382 /* We should find the immediate. */
4383 assert (sz != 0);
4386 p = frag_more (size);
4387 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
4388 if (GOT_symbol
4389 && GOT_symbol == i.op[n].disps->X_add_symbol
4390 && (((reloc_type == BFD_RELOC_32
4391 || reloc_type == BFD_RELOC_X86_64_32S
4392 || (reloc_type == BFD_RELOC_64
4393 && object_64bit))
4394 && (i.op[n].disps->X_op == O_symbol
4395 || (i.op[n].disps->X_op == O_add
4396 && ((symbol_get_value_expression
4397 (i.op[n].disps->X_op_symbol)->X_op)
4398 == O_subtract))))
4399 || reloc_type == BFD_RELOC_32_PCREL))
4401 offsetT add;
4403 if (insn_start_frag == frag_now)
4404 add = (p - frag_now->fr_literal) - insn_start_off;
4405 else
4407 fragS *fr;
4409 add = insn_start_frag->fr_fix - insn_start_off;
4410 for (fr = insn_start_frag->fr_next;
4411 fr && fr != frag_now; fr = fr->fr_next)
4412 add += fr->fr_fix;
4413 add += p - frag_now->fr_literal;
4416 if (!object_64bit)
4418 reloc_type = BFD_RELOC_386_GOTPC;
4419 i.op[n].imms->X_add_number += add;
4421 else if (reloc_type == BFD_RELOC_64)
4422 reloc_type = BFD_RELOC_X86_64_GOTPC64;
4423 else
4424 /* Don't do the adjustment for x86-64, as there
4425 the pcrel addressing is relative to the _next_
4426 insn, and that is taken care of in other code. */
4427 reloc_type = BFD_RELOC_X86_64_GOTPC32;
4429 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4430 i.op[n].disps, pcrel, reloc_type);
4436 static void
4437 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
4439 char *p;
4440 unsigned int n;
4442 for (n = 0; n < i.operands; n++)
4444 if (i.types[n] & Imm)
4446 if (i.op[n].imms->X_op == O_constant)
4448 int size = imm_size (n);
4449 offsetT val;
4451 val = offset_in_range (i.op[n].imms->X_add_number,
4452 size);
4453 p = frag_more (size);
4454 md_number_to_chars (p, val, size);
4456 else
4458 /* Not absolute_section.
4459 Need a 32-bit fixup (don't support 8bit
4460 non-absolute imms). Try to support other
4461 sizes ... */
4462 enum bfd_reloc_code_real reloc_type;
4463 int size = imm_size (n);
4464 int sign;
4466 if ((i.types[n] & (Imm32S))
4467 && (i.suffix == QWORD_MNEM_SUFFIX
4468 || (!i.suffix && (i.tm.opcode_modifier & No_lSuf))))
4469 sign = 1;
4470 else
4471 sign = 0;
4473 p = frag_more (size);
4474 reloc_type = reloc (size, 0, sign, i.reloc[n]);
4476 /* This is tough to explain. We end up with this one if we
4477 * have operands that look like
4478 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
4479 * obtain the absolute address of the GOT, and it is strongly
4480 * preferable from a performance point of view to avoid using
4481 * a runtime relocation for this. The actual sequence of
4482 * instructions often look something like:
4484 * call .L66
4485 * .L66:
4486 * popl %ebx
4487 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
4489 * The call and pop essentially return the absolute address
4490 * of the label .L66 and store it in %ebx. The linker itself
4491 * will ultimately change the first operand of the addl so
4492 * that %ebx points to the GOT, but to keep things simple, the
4493 * .o file must have this operand set so that it generates not
4494 * the absolute address of .L66, but the absolute address of
4495 * itself. This allows the linker itself simply treat a GOTPC
4496 * relocation as asking for a pcrel offset to the GOT to be
4497 * added in, and the addend of the relocation is stored in the
4498 * operand field for the instruction itself.
4500 * Our job here is to fix the operand so that it would add
4501 * the correct offset so that %ebx would point to itself. The
4502 * thing that is tricky is that .-.L66 will point to the
4503 * beginning of the instruction, so we need to further modify
4504 * the operand so that it will point to itself. There are
4505 * other cases where you have something like:
4507 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4509 * and here no correction would be required. Internally in
4510 * the assembler we treat operands of this form as not being
4511 * pcrel since the '.' is explicitly mentioned, and I wonder
4512 * whether it would simplify matters to do it this way. Who
4513 * knows. In earlier versions of the PIC patches, the
4514 * pcrel_adjust field was used to store the correction, but
4515 * since the expression is not pcrel, I felt it would be
4516 * confusing to do it this way. */
4518 if ((reloc_type == BFD_RELOC_32
4519 || reloc_type == BFD_RELOC_X86_64_32S
4520 || reloc_type == BFD_RELOC_64)
4521 && GOT_symbol
4522 && GOT_symbol == i.op[n].imms->X_add_symbol
4523 && (i.op[n].imms->X_op == O_symbol
4524 || (i.op[n].imms->X_op == O_add
4525 && ((symbol_get_value_expression
4526 (i.op[n].imms->X_op_symbol)->X_op)
4527 == O_subtract))))
4529 offsetT add;
4531 if (insn_start_frag == frag_now)
4532 add = (p - frag_now->fr_literal) - insn_start_off;
4533 else
4535 fragS *fr;
4537 add = insn_start_frag->fr_fix - insn_start_off;
4538 for (fr = insn_start_frag->fr_next;
4539 fr && fr != frag_now; fr = fr->fr_next)
4540 add += fr->fr_fix;
4541 add += p - frag_now->fr_literal;
4544 if (!object_64bit)
4545 reloc_type = BFD_RELOC_386_GOTPC;
4546 else if (size == 4)
4547 reloc_type = BFD_RELOC_X86_64_GOTPC32;
4548 else if (size == 8)
4549 reloc_type = BFD_RELOC_X86_64_GOTPC64;
4550 i.op[n].imms->X_add_number += add;
4552 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
4553 i.op[n].imms, 0, reloc_type);
4559 /* x86_cons_fix_new is called via the expression parsing code when a
4560 reloc is needed. We use this hook to get the correct .got reloc. */
4561 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
4562 static int cons_sign = -1;
4564 void
4565 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
4566 expressionS *exp)
4568 enum bfd_reloc_code_real r = reloc (len, 0, cons_sign, got_reloc);
4570 got_reloc = NO_RELOC;
4572 #ifdef TE_PE
4573 if (exp->X_op == O_secrel)
4575 exp->X_op = O_symbol;
4576 r = BFD_RELOC_32_SECREL;
4578 #endif
4580 fix_new_exp (frag, off, len, exp, 0, r);
4583 #if (!defined (OBJ_ELF) && !defined (OBJ_MAYBE_ELF)) || defined (LEX_AT)
4584 # define lex_got(reloc, adjust, types) NULL
4585 #else
4586 /* Parse operands of the form
4587 <symbol>@GOTOFF+<nnn>
4588 and similar .plt or .got references.
4590 If we find one, set up the correct relocation in RELOC and copy the
4591 input string, minus the `@GOTOFF' into a malloc'd buffer for
4592 parsing by the calling routine. Return this buffer, and if ADJUST
4593 is non-null set it to the length of the string we removed from the
4594 input line. Otherwise return NULL. */
4595 static char *
4596 lex_got (enum bfd_reloc_code_real *reloc,
4597 int *adjust,
4598 unsigned int *types)
4600 /* Some of the relocations depend on the size of what field is to
4601 be relocated. But in our callers i386_immediate and i386_displacement
4602 we don't yet know the operand size (this will be set by insn
4603 matching). Hence we record the word32 relocation here,
4604 and adjust the reloc according to the real size in reloc(). */
4605 static const struct {
4606 const char *str;
4607 const enum bfd_reloc_code_real rel[2];
4608 const unsigned int types64;
4609 } gotrel[] = {
4610 { "PLTOFF", { 0,
4611 BFD_RELOC_X86_64_PLTOFF64 },
4612 Imm64 },
4613 { "PLT", { BFD_RELOC_386_PLT32,
4614 BFD_RELOC_X86_64_PLT32 },
4615 Imm32 | Imm32S | Disp32 },
4616 { "GOTPLT", { 0,
4617 BFD_RELOC_X86_64_GOTPLT64 },
4618 Imm64 | Disp64 },
4619 { "GOTOFF", { BFD_RELOC_386_GOTOFF,
4620 BFD_RELOC_X86_64_GOTOFF64 },
4621 Imm64 | Disp64 },
4622 { "GOTPCREL", { 0,
4623 BFD_RELOC_X86_64_GOTPCREL },
4624 Imm32 | Imm32S | Disp32 },
4625 { "TLSGD", { BFD_RELOC_386_TLS_GD,
4626 BFD_RELOC_X86_64_TLSGD },
4627 Imm32 | Imm32S | Disp32 },
4628 { "TLSLDM", { BFD_RELOC_386_TLS_LDM,
4629 0 },
4630 0 },
4631 { "TLSLD", { 0,
4632 BFD_RELOC_X86_64_TLSLD },
4633 Imm32 | Imm32S | Disp32 },
4634 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32,
4635 BFD_RELOC_X86_64_GOTTPOFF },
4636 Imm32 | Imm32S | Disp32 },
4637 { "TPOFF", { BFD_RELOC_386_TLS_LE_32,
4638 BFD_RELOC_X86_64_TPOFF32 },
4639 Imm32 | Imm32S | Imm64 | Disp32 | Disp64 },
4640 { "NTPOFF", { BFD_RELOC_386_TLS_LE,
4641 0 },
4642 0 },
4643 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32,
4644 BFD_RELOC_X86_64_DTPOFF32 },
4645 Imm32 | Imm32S | Imm64 | Disp32 | Disp64 },
4646 { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE,
4647 0 },
4648 0 },
4649 { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE,
4650 0 },
4651 0 },
4652 { "GOT", { BFD_RELOC_386_GOT32,
4653 BFD_RELOC_X86_64_GOT32 },
4654 Imm32 | Imm32S | Disp32 | Imm64 },
4655 { "TLSDESC", { BFD_RELOC_386_TLS_GOTDESC,
4656 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
4657 Imm32 | Imm32S | Disp32 },
4658 { "TLSCALL", { BFD_RELOC_386_TLS_DESC_CALL,
4659 BFD_RELOC_X86_64_TLSDESC_CALL },
4660 Imm32 | Imm32S | Disp32 }
4662 char *cp;
4663 unsigned int j;
4665 if (!IS_ELF)
4666 return NULL;
4668 for (cp = input_line_pointer; *cp != '@'; cp++)
4669 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
4670 return NULL;
4672 for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
4674 int len;
4676 len = strlen (gotrel[j].str);
4677 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
4679 if (gotrel[j].rel[object_64bit] != 0)
4681 int first, second;
4682 char *tmpbuf, *past_reloc;
4684 *reloc = gotrel[j].rel[object_64bit];
4685 if (adjust)
4686 *adjust = len;
4688 if (types)
4690 if (flag_code != CODE_64BIT)
4691 *types = Imm32 | Disp32;
4692 else
4693 *types = gotrel[j].types64;
4696 if (GOT_symbol == NULL)
4697 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
4699 /* The length of the first part of our input line. */
4700 first = cp - input_line_pointer;
4702 /* The second part goes from after the reloc token until
4703 (and including) an end_of_line char or comma. */
4704 past_reloc = cp + 1 + len;
4705 cp = past_reloc;
4706 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
4707 ++cp;
4708 second = cp + 1 - past_reloc;
4710 /* Allocate and copy string. The trailing NUL shouldn't
4711 be necessary, but be safe. */
4712 tmpbuf = xmalloc (first + second + 2);
4713 memcpy (tmpbuf, input_line_pointer, first);
4714 if (second != 0 && *past_reloc != ' ')
4715 /* Replace the relocation token with ' ', so that
4716 errors like foo@GOTOFF1 will be detected. */
4717 tmpbuf[first++] = ' ';
4718 memcpy (tmpbuf + first, past_reloc, second);
4719 tmpbuf[first + second] = '\0';
4720 return tmpbuf;
4723 as_bad (_("@%s reloc is not supported with %d-bit output format"),
4724 gotrel[j].str, 1 << (5 + object_64bit));
4725 return NULL;
4729 /* Might be a symbol version string. Don't as_bad here. */
4730 return NULL;
4733 void
4734 x86_cons (expressionS *exp, int size)
4736 if (size == 4 || (object_64bit && size == 8))
4738 /* Handle @GOTOFF and the like in an expression. */
4739 char *save;
4740 char *gotfree_input_line;
4741 int adjust;
4743 save = input_line_pointer;
4744 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
4745 if (gotfree_input_line)
4746 input_line_pointer = gotfree_input_line;
4748 expression (exp);
4750 if (gotfree_input_line)
4752 /* expression () has merrily parsed up to the end of line,
4753 or a comma - in the wrong buffer. Transfer how far
4754 input_line_pointer has moved to the right buffer. */
4755 input_line_pointer = (save
4756 + (input_line_pointer - gotfree_input_line)
4757 + adjust);
4758 free (gotfree_input_line);
4761 else
4762 expression (exp);
4764 #endif
4766 static void signed_cons (int size)
4768 if (flag_code == CODE_64BIT)
4769 cons_sign = 1;
4770 cons (size);
4771 cons_sign = -1;
4774 #ifdef TE_PE
4775 static void
4776 pe_directive_secrel (dummy)
4777 int dummy ATTRIBUTE_UNUSED;
4779 expressionS exp;
4783 expression (&exp);
4784 if (exp.X_op == O_symbol)
4785 exp.X_op = O_secrel;
4787 emit_expr (&exp, 4);
4789 while (*input_line_pointer++ == ',');
4791 input_line_pointer--;
4792 demand_empty_rest_of_line ();
4794 #endif
4796 static int
4797 i386_immediate (char *imm_start)
4799 char *save_input_line_pointer;
4800 char *gotfree_input_line;
4801 segT exp_seg = 0;
4802 expressionS *exp;
4803 unsigned int types = ~0U;
4805 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
4807 as_bad (_("at most %d immediate operands are allowed"),
4808 MAX_IMMEDIATE_OPERANDS);
4809 return 0;
4812 exp = &im_expressions[i.imm_operands++];
4813 i.op[this_operand].imms = exp;
4815 if (is_space_char (*imm_start))
4816 ++imm_start;
4818 save_input_line_pointer = input_line_pointer;
4819 input_line_pointer = imm_start;
4821 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
4822 if (gotfree_input_line)
4823 input_line_pointer = gotfree_input_line;
4825 exp_seg = expression (exp);
4827 SKIP_WHITESPACE ();
4828 if (*input_line_pointer)
4829 as_bad (_("junk `%s' after expression"), input_line_pointer);
4831 input_line_pointer = save_input_line_pointer;
4832 if (gotfree_input_line)
4833 free (gotfree_input_line);
4835 if (exp->X_op == O_absent || exp->X_op == O_big)
4837 /* Missing or bad expr becomes absolute 0. */
4838 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
4839 imm_start);
4840 exp->X_op = O_constant;
4841 exp->X_add_number = 0;
4842 exp->X_add_symbol = (symbolS *) 0;
4843 exp->X_op_symbol = (symbolS *) 0;
4845 else if (exp->X_op == O_constant)
4847 /* Size it properly later. */
4848 i.types[this_operand] |= Imm64;
4849 /* If BFD64, sign extend val. */
4850 if (!use_rela_relocations
4851 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
4852 exp->X_add_number
4853 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
4855 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4856 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
4857 && exp_seg != absolute_section
4858 && exp_seg != text_section
4859 && exp_seg != data_section
4860 && exp_seg != bss_section
4861 && exp_seg != undefined_section
4862 && !bfd_is_com_section (exp_seg))
4864 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
4865 return 0;
4867 #endif
4868 else if (!intel_syntax && exp->X_op == O_register)
4870 as_bad (_("illegal immediate register operand %s"), imm_start);
4871 return 0;
4873 else
4875 /* This is an address. The size of the address will be
4876 determined later, depending on destination register,
4877 suffix, or the default for the section. */
4878 i.types[this_operand] |= Imm8 | Imm16 | Imm32 | Imm32S | Imm64;
4879 i.types[this_operand] &= types;
4882 return 1;
4885 static char *
4886 i386_scale (char *scale)
4888 offsetT val;
4889 char *save = input_line_pointer;
4891 input_line_pointer = scale;
4892 val = get_absolute_expression ();
4894 switch (val)
4896 case 1:
4897 i.log2_scale_factor = 0;
4898 break;
4899 case 2:
4900 i.log2_scale_factor = 1;
4901 break;
4902 case 4:
4903 i.log2_scale_factor = 2;
4904 break;
4905 case 8:
4906 i.log2_scale_factor = 3;
4907 break;
4908 default:
4910 char sep = *input_line_pointer;
4912 *input_line_pointer = '\0';
4913 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
4914 scale);
4915 *input_line_pointer = sep;
4916 input_line_pointer = save;
4917 return NULL;
4920 if (i.log2_scale_factor != 0 && i.index_reg == 0)
4922 as_warn (_("scale factor of %d without an index register"),
4923 1 << i.log2_scale_factor);
4924 #if SCALE1_WHEN_NO_INDEX
4925 i.log2_scale_factor = 0;
4926 #endif
4928 scale = input_line_pointer;
4929 input_line_pointer = save;
4930 return scale;
4933 static int
4934 i386_displacement (char *disp_start, char *disp_end)
4936 expressionS *exp;
4937 segT exp_seg = 0;
4938 char *save_input_line_pointer;
4939 char *gotfree_input_line;
4940 int bigdisp, override;
4941 unsigned int types = Disp;
4943 if (i.disp_operands == MAX_MEMORY_OPERANDS)
4945 as_bad (_("at most %d displacement operands are allowed"),
4946 MAX_MEMORY_OPERANDS);
4947 return 0;
4950 if ((i.types[this_operand] & JumpAbsolute)
4951 || !(current_templates->start->opcode_modifier & (Jump | JumpDword)))
4953 bigdisp = Disp32;
4954 override = (i.prefix[ADDR_PREFIX] != 0);
4956 else
4958 /* For PC-relative branches, the width of the displacement
4959 is dependent upon data size, not address size. */
4960 bigdisp = 0;
4961 override = (i.prefix[DATA_PREFIX] != 0);
4963 if (flag_code == CODE_64BIT)
4965 if (!bigdisp)
4966 bigdisp = ((override || i.suffix == WORD_MNEM_SUFFIX)
4967 ? Disp16
4968 : Disp32S | Disp32);
4969 else if (!override)
4970 bigdisp = Disp64 | Disp32S | Disp32;
4972 else
4974 if (!bigdisp)
4976 if (!override)
4977 override = (i.suffix == (flag_code != CODE_16BIT
4978 ? WORD_MNEM_SUFFIX
4979 : LONG_MNEM_SUFFIX));
4980 bigdisp = Disp32;
4982 if ((flag_code == CODE_16BIT) ^ override)
4983 bigdisp = Disp16;
4985 i.types[this_operand] |= bigdisp;
4987 exp = &disp_expressions[i.disp_operands];
4988 i.op[this_operand].disps = exp;
4989 i.disp_operands++;
4990 save_input_line_pointer = input_line_pointer;
4991 input_line_pointer = disp_start;
4992 END_STRING_AND_SAVE (disp_end);
4994 #ifndef GCC_ASM_O_HACK
4995 #define GCC_ASM_O_HACK 0
4996 #endif
4997 #if GCC_ASM_O_HACK
4998 END_STRING_AND_SAVE (disp_end + 1);
4999 if ((i.types[this_operand] & BaseIndex) != 0
5000 && displacement_string_end[-1] == '+')
5002 /* This hack is to avoid a warning when using the "o"
5003 constraint within gcc asm statements.
5004 For instance:
5006 #define _set_tssldt_desc(n,addr,limit,type) \
5007 __asm__ __volatile__ ( \
5008 "movw %w2,%0\n\t" \
5009 "movw %w1,2+%0\n\t" \
5010 "rorl $16,%1\n\t" \
5011 "movb %b1,4+%0\n\t" \
5012 "movb %4,5+%0\n\t" \
5013 "movb $0,6+%0\n\t" \
5014 "movb %h1,7+%0\n\t" \
5015 "rorl $16,%1" \
5016 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
5018 This works great except that the output assembler ends
5019 up looking a bit weird if it turns out that there is
5020 no offset. You end up producing code that looks like:
5022 #APP
5023 movw $235,(%eax)
5024 movw %dx,2+(%eax)
5025 rorl $16,%edx
5026 movb %dl,4+(%eax)
5027 movb $137,5+(%eax)
5028 movb $0,6+(%eax)
5029 movb %dh,7+(%eax)
5030 rorl $16,%edx
5031 #NO_APP
5033 So here we provide the missing zero. */
5035 *displacement_string_end = '0';
5037 #endif
5038 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
5039 if (gotfree_input_line)
5040 input_line_pointer = gotfree_input_line;
5042 exp_seg = expression (exp);
5044 SKIP_WHITESPACE ();
5045 if (*input_line_pointer)
5046 as_bad (_("junk `%s' after expression"), input_line_pointer);
5047 #if GCC_ASM_O_HACK
5048 RESTORE_END_STRING (disp_end + 1);
5049 #endif
5050 RESTORE_END_STRING (disp_end);
5051 input_line_pointer = save_input_line_pointer;
5052 if (gotfree_input_line)
5053 free (gotfree_input_line);
5055 /* We do this to make sure that the section symbol is in
5056 the symbol table. We will ultimately change the relocation
5057 to be relative to the beginning of the section. */
5058 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
5059 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
5060 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
5062 if (exp->X_op != O_symbol)
5064 as_bad (_("bad expression used with @%s"),
5065 (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
5066 ? "GOTPCREL"
5067 : "GOTOFF"));
5068 return 0;
5071 if (S_IS_LOCAL (exp->X_add_symbol)
5072 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
5073 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
5074 exp->X_op = O_subtract;
5075 exp->X_op_symbol = GOT_symbol;
5076 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
5077 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
5078 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
5079 i.reloc[this_operand] = BFD_RELOC_64;
5080 else
5081 i.reloc[this_operand] = BFD_RELOC_32;
5084 if (exp->X_op == O_absent || exp->X_op == O_big)
5086 /* Missing or bad expr becomes absolute 0. */
5087 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
5088 disp_start);
5089 exp->X_op = O_constant;
5090 exp->X_add_number = 0;
5091 exp->X_add_symbol = (symbolS *) 0;
5092 exp->X_op_symbol = (symbolS *) 0;
5095 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5096 if (exp->X_op != O_constant
5097 && OUTPUT_FLAVOR == bfd_target_aout_flavour
5098 && exp_seg != absolute_section
5099 && exp_seg != text_section
5100 && exp_seg != data_section
5101 && exp_seg != bss_section
5102 && exp_seg != undefined_section
5103 && !bfd_is_com_section (exp_seg))
5105 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
5106 return 0;
5108 #endif
5110 if (!(i.types[this_operand] & ~Disp))
5111 i.types[this_operand] &= types;
5113 return 1;
5116 /* Make sure the memory operand we've been dealt is valid.
5117 Return 1 on success, 0 on a failure. */
5119 static int
5120 i386_index_check (const char *operand_string)
5122 int ok;
5123 #if INFER_ADDR_PREFIX
5124 int fudged = 0;
5126 tryprefix:
5127 #endif
5128 ok = 1;
5129 if ((current_templates->start->cpu_flags & CpuSVME)
5130 && current_templates->end[-1].operand_types[0] == AnyMem)
5132 /* Memory operands of SVME insns are special in that they only allow
5133 rAX as their memory address and ignore any segment override. */
5134 unsigned RegXX;
5136 /* SKINIT is even more restrictive: it always requires EAX. */
5137 if (strcmp (current_templates->start->name, "skinit") == 0)
5138 RegXX = Reg32;
5139 else if (flag_code == CODE_64BIT)
5140 RegXX = i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32;
5141 else
5142 RegXX = ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0)
5143 ? Reg16
5144 : Reg32);
5145 if (!i.base_reg
5146 || !(i.base_reg->reg_type & Acc)
5147 || !(i.base_reg->reg_type & RegXX)
5148 || i.index_reg
5149 || (i.types[0] & Disp))
5150 ok = 0;
5152 else if (flag_code == CODE_64BIT)
5154 unsigned RegXX = (i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32);
5156 if ((i.base_reg
5157 && ((i.base_reg->reg_type & RegXX) == 0)
5158 && (i.base_reg->reg_type != BaseIndex
5159 || i.index_reg))
5160 || (i.index_reg
5161 && ((i.index_reg->reg_type & (RegXX | BaseIndex))
5162 != (RegXX | BaseIndex))))
5163 ok = 0;
5165 else
5167 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
5169 /* 16bit checks. */
5170 if ((i.base_reg
5171 && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
5172 != (Reg16 | BaseIndex)))
5173 || (i.index_reg
5174 && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
5175 != (Reg16 | BaseIndex))
5176 || !(i.base_reg
5177 && i.base_reg->reg_num < 6
5178 && i.index_reg->reg_num >= 6
5179 && i.log2_scale_factor == 0))))
5180 ok = 0;
5182 else
5184 /* 32bit checks. */
5185 if ((i.base_reg
5186 && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
5187 || (i.index_reg
5188 && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
5189 != (Reg32 | BaseIndex))))
5190 ok = 0;
5193 if (!ok)
5195 #if INFER_ADDR_PREFIX
5196 if (i.prefix[ADDR_PREFIX] == 0)
5198 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
5199 i.prefixes += 1;
5200 /* Change the size of any displacement too. At most one of
5201 Disp16 or Disp32 is set.
5202 FIXME. There doesn't seem to be any real need for separate
5203 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
5204 Removing them would probably clean up the code quite a lot. */
5205 if (flag_code != CODE_64BIT
5206 && (i.types[this_operand] & (Disp16 | Disp32)))
5207 i.types[this_operand] ^= (Disp16 | Disp32);
5208 fudged = 1;
5209 goto tryprefix;
5211 if (fudged)
5212 as_bad (_("`%s' is not a valid base/index expression"),
5213 operand_string);
5214 else
5215 #endif
5216 as_bad (_("`%s' is not a valid %s bit base/index expression"),
5217 operand_string,
5218 flag_code_names[flag_code]);
5220 return ok;
5223 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
5224 on error. */
5226 static int
5227 i386_operand (char *operand_string)
5229 const reg_entry *r;
5230 char *end_op;
5231 char *op_string = operand_string;
5233 if (is_space_char (*op_string))
5234 ++op_string;
5236 /* We check for an absolute prefix (differentiating,
5237 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
5238 if (*op_string == ABSOLUTE_PREFIX)
5240 ++op_string;
5241 if (is_space_char (*op_string))
5242 ++op_string;
5243 i.types[this_operand] |= JumpAbsolute;
5246 /* Check if operand is a register. */
5247 if ((r = parse_register (op_string, &end_op)) != NULL)
5249 /* Check for a segment override by searching for ':' after a
5250 segment register. */
5251 op_string = end_op;
5252 if (is_space_char (*op_string))
5253 ++op_string;
5254 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
5256 switch (r->reg_num)
5258 case 0:
5259 i.seg[i.mem_operands] = &es;
5260 break;
5261 case 1:
5262 i.seg[i.mem_operands] = &cs;
5263 break;
5264 case 2:
5265 i.seg[i.mem_operands] = &ss;
5266 break;
5267 case 3:
5268 i.seg[i.mem_operands] = &ds;
5269 break;
5270 case 4:
5271 i.seg[i.mem_operands] = &fs;
5272 break;
5273 case 5:
5274 i.seg[i.mem_operands] = &gs;
5275 break;
5278 /* Skip the ':' and whitespace. */
5279 ++op_string;
5280 if (is_space_char (*op_string))
5281 ++op_string;
5283 if (!is_digit_char (*op_string)
5284 && !is_identifier_char (*op_string)
5285 && *op_string != '('
5286 && *op_string != ABSOLUTE_PREFIX)
5288 as_bad (_("bad memory operand `%s'"), op_string);
5289 return 0;
5291 /* Handle case of %es:*foo. */
5292 if (*op_string == ABSOLUTE_PREFIX)
5294 ++op_string;
5295 if (is_space_char (*op_string))
5296 ++op_string;
5297 i.types[this_operand] |= JumpAbsolute;
5299 goto do_memory_reference;
5301 if (*op_string)
5303 as_bad (_("junk `%s' after register"), op_string);
5304 return 0;
5306 i.types[this_operand] |= r->reg_type & ~BaseIndex;
5307 i.op[this_operand].regs = r;
5308 i.reg_operands++;
5310 else if (*op_string == REGISTER_PREFIX)
5312 as_bad (_("bad register name `%s'"), op_string);
5313 return 0;
5315 else if (*op_string == IMMEDIATE_PREFIX)
5317 ++op_string;
5318 if (i.types[this_operand] & JumpAbsolute)
5320 as_bad (_("immediate operand illegal with absolute jump"));
5321 return 0;
5323 if (!i386_immediate (op_string))
5324 return 0;
5326 else if (is_digit_char (*op_string)
5327 || is_identifier_char (*op_string)
5328 || *op_string == '(')
5330 /* This is a memory reference of some sort. */
5331 char *base_string;
5333 /* Start and end of displacement string expression (if found). */
5334 char *displacement_string_start;
5335 char *displacement_string_end;
5337 do_memory_reference:
5338 if ((i.mem_operands == 1
5339 && (current_templates->start->opcode_modifier & IsString) == 0)
5340 || i.mem_operands == 2)
5342 as_bad (_("too many memory references for `%s'"),
5343 current_templates->start->name);
5344 return 0;
5347 /* Check for base index form. We detect the base index form by
5348 looking for an ')' at the end of the operand, searching
5349 for the '(' matching it, and finding a REGISTER_PREFIX or ','
5350 after the '('. */
5351 base_string = op_string + strlen (op_string);
5353 --base_string;
5354 if (is_space_char (*base_string))
5355 --base_string;
5357 /* If we only have a displacement, set-up for it to be parsed later. */
5358 displacement_string_start = op_string;
5359 displacement_string_end = base_string + 1;
5361 if (*base_string == ')')
5363 char *temp_string;
5364 unsigned int parens_balanced = 1;
5365 /* We've already checked that the number of left & right ()'s are
5366 equal, so this loop will not be infinite. */
5369 base_string--;
5370 if (*base_string == ')')
5371 parens_balanced++;
5372 if (*base_string == '(')
5373 parens_balanced--;
5375 while (parens_balanced);
5377 temp_string = base_string;
5379 /* Skip past '(' and whitespace. */
5380 ++base_string;
5381 if (is_space_char (*base_string))
5382 ++base_string;
5384 if (*base_string == ','
5385 || ((i.base_reg = parse_register (base_string, &end_op))
5386 != NULL))
5388 displacement_string_end = temp_string;
5390 i.types[this_operand] |= BaseIndex;
5392 if (i.base_reg)
5394 base_string = end_op;
5395 if (is_space_char (*base_string))
5396 ++base_string;
5399 /* There may be an index reg or scale factor here. */
5400 if (*base_string == ',')
5402 ++base_string;
5403 if (is_space_char (*base_string))
5404 ++base_string;
5406 if ((i.index_reg = parse_register (base_string, &end_op))
5407 != NULL)
5409 base_string = end_op;
5410 if (is_space_char (*base_string))
5411 ++base_string;
5412 if (*base_string == ',')
5414 ++base_string;
5415 if (is_space_char (*base_string))
5416 ++base_string;
5418 else if (*base_string != ')')
5420 as_bad (_("expecting `,' or `)' "
5421 "after index register in `%s'"),
5422 operand_string);
5423 return 0;
5426 else if (*base_string == REGISTER_PREFIX)
5428 as_bad (_("bad register name `%s'"), base_string);
5429 return 0;
5432 /* Check for scale factor. */
5433 if (*base_string != ')')
5435 char *end_scale = i386_scale (base_string);
5437 if (!end_scale)
5438 return 0;
5440 base_string = end_scale;
5441 if (is_space_char (*base_string))
5442 ++base_string;
5443 if (*base_string != ')')
5445 as_bad (_("expecting `)' "
5446 "after scale factor in `%s'"),
5447 operand_string);
5448 return 0;
5451 else if (!i.index_reg)
5453 as_bad (_("expecting index register or scale factor "
5454 "after `,'; got '%c'"),
5455 *base_string);
5456 return 0;
5459 else if (*base_string != ')')
5461 as_bad (_("expecting `,' or `)' "
5462 "after base register in `%s'"),
5463 operand_string);
5464 return 0;
5467 else if (*base_string == REGISTER_PREFIX)
5469 as_bad (_("bad register name `%s'"), base_string);
5470 return 0;
5474 /* If there's an expression beginning the operand, parse it,
5475 assuming displacement_string_start and
5476 displacement_string_end are meaningful. */
5477 if (displacement_string_start != displacement_string_end)
5479 if (!i386_displacement (displacement_string_start,
5480 displacement_string_end))
5481 return 0;
5484 /* Special case for (%dx) while doing input/output op. */
5485 if (i.base_reg
5486 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
5487 && i.index_reg == 0
5488 && i.log2_scale_factor == 0
5489 && i.seg[i.mem_operands] == 0
5490 && (i.types[this_operand] & Disp) == 0)
5492 i.types[this_operand] = InOutPortReg;
5493 return 1;
5496 if (i386_index_check (operand_string) == 0)
5497 return 0;
5498 i.mem_operands++;
5500 else
5502 /* It's not a memory operand; argh! */
5503 as_bad (_("invalid char %s beginning operand %d `%s'"),
5504 output_invalid (*op_string),
5505 this_operand + 1,
5506 op_string);
5507 return 0;
5509 return 1; /* Normal return. */
5512 /* md_estimate_size_before_relax()
5514 Called just before relax() for rs_machine_dependent frags. The x86
5515 assembler uses these frags to handle variable size jump
5516 instructions.
5518 Any symbol that is now undefined will not become defined.
5519 Return the correct fr_subtype in the frag.
5520 Return the initial "guess for variable size of frag" to caller.
5521 The guess is actually the growth beyond the fixed part. Whatever
5522 we do to grow the fixed or variable part contributes to our
5523 returned value. */
5526 md_estimate_size_before_relax (fragP, segment)
5527 fragS *fragP;
5528 segT segment;
5530 /* We've already got fragP->fr_subtype right; all we have to do is
5531 check for un-relaxable symbols. On an ELF system, we can't relax
5532 an externally visible symbol, because it may be overridden by a
5533 shared library. */
5534 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
5535 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5536 || (IS_ELF
5537 && (S_IS_EXTERNAL (fragP->fr_symbol)
5538 || S_IS_WEAK (fragP->fr_symbol)))
5539 #endif
5542 /* Symbol is undefined in this segment, or we need to keep a
5543 reloc so that weak symbols can be overridden. */
5544 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
5545 enum bfd_reloc_code_real reloc_type;
5546 unsigned char *opcode;
5547 int old_fr_fix;
5549 if (fragP->fr_var != NO_RELOC)
5550 reloc_type = fragP->fr_var;
5551 else if (size == 2)
5552 reloc_type = BFD_RELOC_16_PCREL;
5553 else
5554 reloc_type = BFD_RELOC_32_PCREL;
5556 old_fr_fix = fragP->fr_fix;
5557 opcode = (unsigned char *) fragP->fr_opcode;
5559 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
5561 case UNCOND_JUMP:
5562 /* Make jmp (0xeb) a (d)word displacement jump. */
5563 opcode[0] = 0xe9;
5564 fragP->fr_fix += size;
5565 fix_new (fragP, old_fr_fix, size,
5566 fragP->fr_symbol,
5567 fragP->fr_offset, 1,
5568 reloc_type);
5569 break;
5571 case COND_JUMP86:
5572 if (size == 2
5573 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
5575 /* Negate the condition, and branch past an
5576 unconditional jump. */
5577 opcode[0] ^= 1;
5578 opcode[1] = 3;
5579 /* Insert an unconditional jump. */
5580 opcode[2] = 0xe9;
5581 /* We added two extra opcode bytes, and have a two byte
5582 offset. */
5583 fragP->fr_fix += 2 + 2;
5584 fix_new (fragP, old_fr_fix + 2, 2,
5585 fragP->fr_symbol,
5586 fragP->fr_offset, 1,
5587 reloc_type);
5588 break;
5590 /* Fall through. */
5592 case COND_JUMP:
5593 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
5595 fixS *fixP;
5597 fragP->fr_fix += 1;
5598 fixP = fix_new (fragP, old_fr_fix, 1,
5599 fragP->fr_symbol,
5600 fragP->fr_offset, 1,
5601 BFD_RELOC_8_PCREL);
5602 fixP->fx_signed = 1;
5603 break;
5606 /* This changes the byte-displacement jump 0x7N
5607 to the (d)word-displacement jump 0x0f,0x8N. */
5608 opcode[1] = opcode[0] + 0x10;
5609 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
5610 /* We've added an opcode byte. */
5611 fragP->fr_fix += 1 + size;
5612 fix_new (fragP, old_fr_fix + 1, size,
5613 fragP->fr_symbol,
5614 fragP->fr_offset, 1,
5615 reloc_type);
5616 break;
5618 default:
5619 BAD_CASE (fragP->fr_subtype);
5620 break;
5622 frag_wane (fragP);
5623 return fragP->fr_fix - old_fr_fix;
5626 /* Guess size depending on current relax state. Initially the relax
5627 state will correspond to a short jump and we return 1, because
5628 the variable part of the frag (the branch offset) is one byte
5629 long. However, we can relax a section more than once and in that
5630 case we must either set fr_subtype back to the unrelaxed state,
5631 or return the value for the appropriate branch. */
5632 return md_relax_table[fragP->fr_subtype].rlx_length;
5635 /* Called after relax() is finished.
5637 In: Address of frag.
5638 fr_type == rs_machine_dependent.
5639 fr_subtype is what the address relaxed to.
5641 Out: Any fixSs and constants are set up.
5642 Caller will turn frag into a ".space 0". */
5644 void
5645 md_convert_frag (abfd, sec, fragP)
5646 bfd *abfd ATTRIBUTE_UNUSED;
5647 segT sec ATTRIBUTE_UNUSED;
5648 fragS *fragP;
5650 unsigned char *opcode;
5651 unsigned char *where_to_put_displacement = NULL;
5652 offsetT target_address;
5653 offsetT opcode_address;
5654 unsigned int extension = 0;
5655 offsetT displacement_from_opcode_start;
5657 opcode = (unsigned char *) fragP->fr_opcode;
5659 /* Address we want to reach in file space. */
5660 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
5662 /* Address opcode resides at in file space. */
5663 opcode_address = fragP->fr_address + fragP->fr_fix;
5665 /* Displacement from opcode start to fill into instruction. */
5666 displacement_from_opcode_start = target_address - opcode_address;
5668 if ((fragP->fr_subtype & BIG) == 0)
5670 /* Don't have to change opcode. */
5671 extension = 1; /* 1 opcode + 1 displacement */
5672 where_to_put_displacement = &opcode[1];
5674 else
5676 if (no_cond_jump_promotion
5677 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
5678 as_warn_where (fragP->fr_file, fragP->fr_line,
5679 _("long jump required"));
5681 switch (fragP->fr_subtype)
5683 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
5684 extension = 4; /* 1 opcode + 4 displacement */
5685 opcode[0] = 0xe9;
5686 where_to_put_displacement = &opcode[1];
5687 break;
5689 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
5690 extension = 2; /* 1 opcode + 2 displacement */
5691 opcode[0] = 0xe9;
5692 where_to_put_displacement = &opcode[1];
5693 break;
5695 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
5696 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
5697 extension = 5; /* 2 opcode + 4 displacement */
5698 opcode[1] = opcode[0] + 0x10;
5699 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
5700 where_to_put_displacement = &opcode[2];
5701 break;
5703 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
5704 extension = 3; /* 2 opcode + 2 displacement */
5705 opcode[1] = opcode[0] + 0x10;
5706 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
5707 where_to_put_displacement = &opcode[2];
5708 break;
5710 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
5711 extension = 4;
5712 opcode[0] ^= 1;
5713 opcode[1] = 3;
5714 opcode[2] = 0xe9;
5715 where_to_put_displacement = &opcode[3];
5716 break;
5718 default:
5719 BAD_CASE (fragP->fr_subtype);
5720 break;
5724 /* If size if less then four we are sure that the operand fits,
5725 but if it's 4, then it could be that the displacement is larger
5726 then -/+ 2GB. */
5727 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
5728 && object_64bit
5729 && ((addressT) (displacement_from_opcode_start - extension
5730 + ((addressT) 1 << 31))
5731 > (((addressT) 2 << 31) - 1)))
5733 as_bad_where (fragP->fr_file, fragP->fr_line,
5734 _("jump target out of range"));
5735 /* Make us emit 0. */
5736 displacement_from_opcode_start = extension;
5738 /* Now put displacement after opcode. */
5739 md_number_to_chars ((char *) where_to_put_displacement,
5740 (valueT) (displacement_from_opcode_start - extension),
5741 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
5742 fragP->fr_fix += extension;
5745 /* Size of byte displacement jmp. */
5746 int md_short_jump_size = 2;
5748 /* Size of dword displacement jmp. */
5749 int md_long_jump_size = 5;
5751 void
5752 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
5753 char *ptr;
5754 addressT from_addr, to_addr;
5755 fragS *frag ATTRIBUTE_UNUSED;
5756 symbolS *to_symbol ATTRIBUTE_UNUSED;
5758 offsetT offset;
5760 offset = to_addr - (from_addr + 2);
5761 /* Opcode for byte-disp jump. */
5762 md_number_to_chars (ptr, (valueT) 0xeb, 1);
5763 md_number_to_chars (ptr + 1, (valueT) offset, 1);
5766 void
5767 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
5768 char *ptr;
5769 addressT from_addr, to_addr;
5770 fragS *frag ATTRIBUTE_UNUSED;
5771 symbolS *to_symbol ATTRIBUTE_UNUSED;
5773 offsetT offset;
5775 offset = to_addr - (from_addr + 5);
5776 md_number_to_chars (ptr, (valueT) 0xe9, 1);
5777 md_number_to_chars (ptr + 1, (valueT) offset, 4);
5780 /* Apply a fixup (fixS) to segment data, once it has been determined
5781 by our caller that we have all the info we need to fix it up.
5783 On the 386, immediates, displacements, and data pointers are all in
5784 the same (little-endian) format, so we don't need to care about which
5785 we are handling. */
5787 void
5788 md_apply_fix (fixP, valP, seg)
5789 /* The fix we're to put in. */
5790 fixS *fixP;
5791 /* Pointer to the value of the bits. */
5792 valueT *valP;
5793 /* Segment fix is from. */
5794 segT seg ATTRIBUTE_UNUSED;
5796 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
5797 valueT value = *valP;
5799 #if !defined (TE_Mach)
5800 if (fixP->fx_pcrel)
5802 switch (fixP->fx_r_type)
5804 default:
5805 break;
5807 case BFD_RELOC_64:
5808 fixP->fx_r_type = BFD_RELOC_64_PCREL;
5809 break;
5810 case BFD_RELOC_32:
5811 case BFD_RELOC_X86_64_32S:
5812 fixP->fx_r_type = BFD_RELOC_32_PCREL;
5813 break;
5814 case BFD_RELOC_16:
5815 fixP->fx_r_type = BFD_RELOC_16_PCREL;
5816 break;
5817 case BFD_RELOC_8:
5818 fixP->fx_r_type = BFD_RELOC_8_PCREL;
5819 break;
5823 if (fixP->fx_addsy != NULL
5824 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
5825 || fixP->fx_r_type == BFD_RELOC_64_PCREL
5826 || fixP->fx_r_type == BFD_RELOC_16_PCREL
5827 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
5828 && !use_rela_relocations)
5830 /* This is a hack. There should be a better way to handle this.
5831 This covers for the fact that bfd_install_relocation will
5832 subtract the current location (for partial_inplace, PC relative
5833 relocations); see more below. */
5834 #ifndef OBJ_AOUT
5835 if (IS_ELF
5836 #ifdef TE_PE
5837 || OUTPUT_FLAVOR == bfd_target_coff_flavour
5838 #endif
5840 value += fixP->fx_where + fixP->fx_frag->fr_address;
5841 #endif
5842 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5843 if (IS_ELF)
5845 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
5847 if ((sym_seg == seg
5848 || (symbol_section_p (fixP->fx_addsy)
5849 && sym_seg != absolute_section))
5850 && !generic_force_reloc (fixP))
5852 /* Yes, we add the values in twice. This is because
5853 bfd_install_relocation subtracts them out again. I think
5854 bfd_install_relocation is broken, but I don't dare change
5855 it. FIXME. */
5856 value += fixP->fx_where + fixP->fx_frag->fr_address;
5859 #endif
5860 #if defined (OBJ_COFF) && defined (TE_PE)
5861 /* For some reason, the PE format does not store a
5862 section address offset for a PC relative symbol. */
5863 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
5864 || S_IS_WEAK (fixP->fx_addsy))
5865 value += md_pcrel_from (fixP);
5866 #endif
5869 /* Fix a few things - the dynamic linker expects certain values here,
5870 and we must not disappoint it. */
5871 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5872 if (IS_ELF && fixP->fx_addsy)
5873 switch (fixP->fx_r_type)
5875 case BFD_RELOC_386_PLT32:
5876 case BFD_RELOC_X86_64_PLT32:
5877 /* Make the jump instruction point to the address of the operand. At
5878 runtime we merely add the offset to the actual PLT entry. */
5879 value = -4;
5880 break;
5882 case BFD_RELOC_386_TLS_GD:
5883 case BFD_RELOC_386_TLS_LDM:
5884 case BFD_RELOC_386_TLS_IE_32:
5885 case BFD_RELOC_386_TLS_IE:
5886 case BFD_RELOC_386_TLS_GOTIE:
5887 case BFD_RELOC_386_TLS_GOTDESC:
5888 case BFD_RELOC_X86_64_TLSGD:
5889 case BFD_RELOC_X86_64_TLSLD:
5890 case BFD_RELOC_X86_64_GOTTPOFF:
5891 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
5892 value = 0; /* Fully resolved at runtime. No addend. */
5893 /* Fallthrough */
5894 case BFD_RELOC_386_TLS_LE:
5895 case BFD_RELOC_386_TLS_LDO_32:
5896 case BFD_RELOC_386_TLS_LE_32:
5897 case BFD_RELOC_X86_64_DTPOFF32:
5898 case BFD_RELOC_X86_64_DTPOFF64:
5899 case BFD_RELOC_X86_64_TPOFF32:
5900 case BFD_RELOC_X86_64_TPOFF64:
5901 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5902 break;
5904 case BFD_RELOC_386_TLS_DESC_CALL:
5905 case BFD_RELOC_X86_64_TLSDESC_CALL:
5906 value = 0; /* Fully resolved at runtime. No addend. */
5907 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5908 fixP->fx_done = 0;
5909 return;
5911 case BFD_RELOC_386_GOT32:
5912 case BFD_RELOC_X86_64_GOT32:
5913 value = 0; /* Fully resolved at runtime. No addend. */
5914 break;
5916 case BFD_RELOC_VTABLE_INHERIT:
5917 case BFD_RELOC_VTABLE_ENTRY:
5918 fixP->fx_done = 0;
5919 return;
5921 default:
5922 break;
5924 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
5925 *valP = value;
5926 #endif /* !defined (TE_Mach) */
5928 /* Are we finished with this relocation now? */
5929 if (fixP->fx_addsy == NULL)
5930 fixP->fx_done = 1;
5931 else if (use_rela_relocations)
5933 fixP->fx_no_overflow = 1;
5934 /* Remember value for tc_gen_reloc. */
5935 fixP->fx_addnumber = value;
5936 value = 0;
5939 md_number_to_chars (p, value, fixP->fx_size);
5942 #define MAX_LITTLENUMS 6
5944 /* Turn the string pointed to by litP into a floating point constant
5945 of type TYPE, and emit the appropriate bytes. The number of
5946 LITTLENUMS emitted is stored in *SIZEP. An error message is
5947 returned, or NULL on OK. */
5949 char *
5950 md_atof (type, litP, sizeP)
5951 int type;
5952 char *litP;
5953 int *sizeP;
5955 int prec;
5956 LITTLENUM_TYPE words[MAX_LITTLENUMS];
5957 LITTLENUM_TYPE *wordP;
5958 char *t;
5960 switch (type)
5962 case 'f':
5963 case 'F':
5964 prec = 2;
5965 break;
5967 case 'd':
5968 case 'D':
5969 prec = 4;
5970 break;
5972 case 'x':
5973 case 'X':
5974 prec = 5;
5975 break;
5977 default:
5978 *sizeP = 0;
5979 return _("Bad call to md_atof ()");
5981 t = atof_ieee (input_line_pointer, type, words);
5982 if (t)
5983 input_line_pointer = t;
5985 *sizeP = prec * sizeof (LITTLENUM_TYPE);
5986 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
5987 the bigendian 386. */
5988 for (wordP = words + prec - 1; prec--;)
5990 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
5991 litP += sizeof (LITTLENUM_TYPE);
5993 return 0;
5996 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
5998 static char *
5999 output_invalid (int c)
6001 if (ISPRINT (c))
6002 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
6003 "'%c'", c);
6004 else
6005 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
6006 "(0x%x)", (unsigned char) c);
6007 return output_invalid_buf;
6010 /* REG_STRING starts *before* REGISTER_PREFIX. */
6012 static const reg_entry *
6013 parse_real_register (char *reg_string, char **end_op)
6015 char *s = reg_string;
6016 char *p;
6017 char reg_name_given[MAX_REG_NAME_SIZE + 1];
6018 const reg_entry *r;
6020 /* Skip possible REGISTER_PREFIX and possible whitespace. */
6021 if (*s == REGISTER_PREFIX)
6022 ++s;
6024 if (is_space_char (*s))
6025 ++s;
6027 p = reg_name_given;
6028 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
6030 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
6031 return (const reg_entry *) NULL;
6032 s++;
6035 /* For naked regs, make sure that we are not dealing with an identifier.
6036 This prevents confusing an identifier like `eax_var' with register
6037 `eax'. */
6038 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
6039 return (const reg_entry *) NULL;
6041 *end_op = s;
6043 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
6045 /* Handle floating point regs, allowing spaces in the (i) part. */
6046 if (r == i386_regtab /* %st is first entry of table */)
6048 if (is_space_char (*s))
6049 ++s;
6050 if (*s == '(')
6052 ++s;
6053 if (is_space_char (*s))
6054 ++s;
6055 if (*s >= '0' && *s <= '7')
6057 int fpr = *s - '0';
6058 ++s;
6059 if (is_space_char (*s))
6060 ++s;
6061 if (*s == ')')
6063 *end_op = s + 1;
6064 r = hash_find (reg_hash, "st(0)");
6065 know (r);
6066 return r + fpr;
6069 /* We have "%st(" then garbage. */
6070 return (const reg_entry *) NULL;
6074 if (r != NULL
6075 && ((r->reg_flags & (RegRex64 | RegRex)) | (r->reg_type & Reg64)) != 0
6076 && (r->reg_type != Control || !(cpu_arch_flags & CpuSledgehammer))
6077 && flag_code != CODE_64BIT)
6078 return (const reg_entry *) NULL;
6080 return r;
6083 /* REG_STRING starts *before* REGISTER_PREFIX. */
6085 static const reg_entry *
6086 parse_register (char *reg_string, char **end_op)
6088 const reg_entry *r;
6090 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
6091 r = parse_real_register (reg_string, end_op);
6092 else
6093 r = NULL;
6094 if (!r)
6096 char *save = input_line_pointer;
6097 char c;
6098 symbolS *symbolP;
6100 input_line_pointer = reg_string;
6101 c = get_symbol_end ();
6102 symbolP = symbol_find (reg_string);
6103 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
6105 const expressionS *e = symbol_get_value_expression (symbolP);
6107 know (e->X_op == O_register);
6108 know (e->X_add_number >= 0
6109 && (valueT) e->X_add_number < i386_regtab_size);
6110 r = i386_regtab + e->X_add_number;
6111 *end_op = input_line_pointer;
6113 *input_line_pointer = c;
6114 input_line_pointer = save;
6116 return r;
6120 i386_parse_name (char *name, expressionS *e, char *nextcharP)
6122 const reg_entry *r;
6123 char *end = input_line_pointer;
6125 *end = *nextcharP;
6126 r = parse_register (name, &input_line_pointer);
6127 if (r && end <= input_line_pointer)
6129 *nextcharP = *input_line_pointer;
6130 *input_line_pointer = 0;
6131 e->X_op = O_register;
6132 e->X_add_number = r - i386_regtab;
6133 return 1;
6135 input_line_pointer = end;
6136 *end = 0;
6137 return 0;
6140 void
6141 md_operand (expressionS *e)
6143 if (*input_line_pointer == REGISTER_PREFIX)
6145 char *end;
6146 const reg_entry *r = parse_real_register (input_line_pointer, &end);
6148 if (r)
6150 e->X_op = O_register;
6151 e->X_add_number = r - i386_regtab;
6152 input_line_pointer = end;
6158 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6159 const char *md_shortopts = "kVQ:sqn";
6160 #else
6161 const char *md_shortopts = "qn";
6162 #endif
6164 #define OPTION_32 (OPTION_MD_BASE + 0)
6165 #define OPTION_64 (OPTION_MD_BASE + 1)
6166 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
6167 #define OPTION_MARCH (OPTION_MD_BASE + 3)
6168 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
6169 #define OPTION_NACL_ALIGN (OPTION_MD_BASE + 5)
6170 #define OPTION_NACL_LIBRARY_MODE (OPTION_MD_BASE + 6)
6173 struct option md_longopts[] =
6175 {"32", no_argument, NULL, OPTION_32},
6176 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6177 {"64", no_argument, NULL, OPTION_64},
6178 #endif
6179 {"divide", no_argument, NULL, OPTION_DIVIDE},
6180 {"march", required_argument, NULL, OPTION_MARCH},
6181 {"mtune", required_argument, NULL, OPTION_MTUNE},
6182 {"nacl-align", required_argument, NULL, OPTION_NACL_ALIGN},
6183 {"nacl-library-mode", no_argument, NULL, OPTION_NACL_LIBRARY_MODE},
6184 {NULL, no_argument, NULL, 0}
6186 size_t md_longopts_size = sizeof (md_longopts);
6189 md_parse_option (int c, char *arg)
6191 unsigned int i;
6193 switch (c)
6195 case 'n':
6196 optimize_align_code = 0;
6197 break;
6199 case 'q':
6200 quiet_warnings = 1;
6201 break;
6203 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6204 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
6205 should be emitted or not. FIXME: Not implemented. */
6206 case 'Q':
6207 break;
6209 /* -V: SVR4 argument to print version ID. */
6210 case 'V':
6211 print_version_id ();
6212 break;
6214 /* -k: Ignore for FreeBSD compatibility. */
6215 case 'k':
6216 break;
6218 case 's':
6219 /* -s: On i386 Solaris, this tells the native assembler to use
6220 .stab instead of .stab.excl. We always use .stab anyhow. */
6221 break;
6222 #endif
6223 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6224 case OPTION_64:
6226 const char **list, **l;
6228 list = bfd_target_list ();
6229 for (l = list; *l != NULL; l++)
6230 if (CONST_STRNEQ (*l, "elf64-x86-64")
6231 || strcmp (*l, "coff-x86-64") == 0
6232 || strcmp (*l, "pe-x86-64") == 0
6233 || strcmp (*l, "pei-x86-64") == 0)
6235 default_arch = "x86_64";
6236 break;
6238 if (*l == NULL)
6239 as_fatal (_("No compiled in support for x86_64"));
6240 free (list);
6242 break;
6243 #endif
6245 case OPTION_32:
6246 default_arch = "i386";
6247 break;
6249 case OPTION_DIVIDE:
6250 #ifdef SVR4_COMMENT_CHARS
6252 char *n, *t;
6253 const char *s;
6255 n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
6256 t = n;
6257 for (s = i386_comment_chars; *s != '\0'; s++)
6258 if (*s != '/')
6259 *t++ = *s;
6260 *t = '\0';
6261 i386_comment_chars = n;
6263 #endif
6264 break;
6266 case OPTION_MARCH:
6267 if (*arg == '.')
6268 as_fatal (_("Invalid -march= option: `%s'"), arg);
6269 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
6271 if (strcmp (arg, cpu_arch [i].name) == 0)
6273 cpu_arch_isa = cpu_arch[i].type;
6274 cpu_arch_isa_flags = cpu_arch[i].flags;
6275 if (!cpu_arch_tune_set)
6277 cpu_arch_tune = cpu_arch_isa;
6278 cpu_arch_tune_flags = cpu_arch_isa_flags;
6280 break;
6283 if (i >= ARRAY_SIZE (cpu_arch))
6284 as_fatal (_("Invalid -march= option: `%s'"), arg);
6285 break;
6287 case OPTION_MTUNE:
6288 if (*arg == '.')
6289 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
6290 for (i = 0; i < ARRAY_SIZE (cpu_arch); i++)
6292 if (strcmp (arg, cpu_arch [i].name) == 0)
6294 cpu_arch_tune_set = 1;
6295 cpu_arch_tune = cpu_arch [i].type;
6296 cpu_arch_tune_flags = cpu_arch[i].flags;
6297 break;
6300 if (i >= ARRAY_SIZE (cpu_arch))
6301 as_fatal (_("Invalid -mtune= option: `%s'"), arg);
6302 break;
6304 case OPTION_NACL_ALIGN:
6306 nacl_alignment = atoi (optarg);
6307 if (nacl_alignment < 0)
6308 as_fatal (_("--nacl-align needs a non-negative argument"));
6309 break;
6312 case OPTION_NACL_LIBRARY_MODE:
6314 nacl_library_mode = 1;
6315 break;
6318 default:
6319 return 0;
6321 return 1;
6324 void
6325 md_show_usage (stream)
6326 FILE *stream;
6328 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6329 fprintf (stream, _("\
6330 -Q ignored\n\
6331 -V print assembler version number\n\
6332 -k ignored\n"));
6333 #endif
6334 fprintf (stream, _("\
6335 -n Do not optimize code alignment\n\
6336 -q quieten some warnings\n"));
6337 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6338 fprintf (stream, _("\
6339 -s ignored\n"));
6340 #endif
6341 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined(TE_PEP)
6342 fprintf (stream, _("\
6343 --32/--64 generate 32bit/64bit code\n"));
6344 #endif
6345 #ifdef SVR4_COMMENT_CHARS
6346 fprintf (stream, _("\
6347 --divide do not treat `/' as a comment character\n"));
6348 #else
6349 fprintf (stream, _("\
6350 --divide ignored\n"));
6351 #endif
6352 fprintf (stream, _("\
6353 -march=CPU/-mtune=CPU generate code/optimize for CPU, where CPU is one of:\n\
6354 i386, i486, pentium, pentiumpro, pentium4, nocona,\n\
6355 core, core2, k6, athlon, k8, generic32, generic64\n"));
6359 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
6360 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (TE_PEP))
6362 /* Pick the target format to use. */
6364 const char *
6365 i386_target_format (void)
6367 if (!strcmp (default_arch, "x86_64"))
6369 set_code_flag (CODE_64BIT);
6370 if (cpu_arch_isa_flags == 0)
6371 cpu_arch_isa_flags = Cpu186|Cpu286|Cpu386|Cpu486
6372 |Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2
6373 |CpuSSE|CpuSSE2;
6374 if (cpu_arch_tune_flags == 0)
6375 cpu_arch_tune_flags = Cpu186|Cpu286|Cpu386|Cpu486
6376 |Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2
6377 |CpuSSE|CpuSSE2;
6379 else if (!strcmp (default_arch, "i386"))
6381 set_code_flag (CODE_32BIT);
6382 if (cpu_arch_isa_flags == 0)
6383 cpu_arch_isa_flags = Cpu186|Cpu286|Cpu386;
6384 if (cpu_arch_tune_flags == 0)
6385 cpu_arch_tune_flags = Cpu186|Cpu286|Cpu386;
6387 else
6388 as_fatal (_("Unknown architecture"));
6389 switch (OUTPUT_FLAVOR)
6391 #ifdef TE_PEP
6392 case bfd_target_coff_flavour:
6393 return flag_code == CODE_64BIT ? COFF_TARGET_FORMAT : "coff-i386";
6394 break;
6395 #endif
6396 #ifdef OBJ_MAYBE_AOUT
6397 case bfd_target_aout_flavour:
6398 return AOUT_TARGET_FORMAT;
6399 #endif
6400 #ifdef OBJ_MAYBE_COFF
6401 case bfd_target_coff_flavour:
6402 return "coff-i386";
6403 #endif
6404 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
6405 case bfd_target_elf_flavour:
6407 if (flag_code == CODE_64BIT)
6409 object_64bit = 1;
6410 use_rela_relocations = 1;
6412 return flag_code == CODE_64BIT ? ELF_TARGET_FORMAT64 : ELF_TARGET_FORMAT;
6414 #endif
6415 default:
6416 abort ();
6417 return NULL;
6421 #endif /* OBJ_MAYBE_ more than one */
6423 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
6424 void
6425 i386_elf_emit_arch_note (void)
6427 if (IS_ELF && cpu_arch_name != NULL)
6429 char *p;
6430 asection *seg = now_seg;
6431 subsegT subseg = now_subseg;
6432 Elf_Internal_Note i_note;
6433 Elf_External_Note e_note;
6434 asection *note_secp;
6435 int len;
6437 /* Create the .note section. */
6438 note_secp = subseg_new (".note", 0);
6439 bfd_set_section_flags (stdoutput,
6440 note_secp,
6441 SEC_HAS_CONTENTS | SEC_READONLY);
6443 /* Process the arch string. */
6444 len = strlen (cpu_arch_name);
6446 i_note.namesz = len + 1;
6447 i_note.descsz = 0;
6448 i_note.type = NT_ARCH;
6449 p = frag_more (sizeof (e_note.namesz));
6450 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
6451 p = frag_more (sizeof (e_note.descsz));
6452 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
6453 p = frag_more (sizeof (e_note.type));
6454 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
6455 p = frag_more (len + 1);
6456 strcpy (p, cpu_arch_name);
6458 frag_align (2, 0, 0);
6460 subseg_set (seg, subseg);
6463 #endif
6465 symbolS *
6466 md_undefined_symbol (name)
6467 char *name;
6469 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
6470 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
6471 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
6472 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
6474 if (!GOT_symbol)
6476 if (symbol_find (name))
6477 as_bad (_("GOT already in symbol table"));
6478 GOT_symbol = symbol_new (name, undefined_section,
6479 (valueT) 0, &zero_address_frag);
6481 return GOT_symbol;
6483 return 0;
6486 /* Round up a section size to the appropriate boundary. */
6488 valueT
6489 md_section_align (segment, size)
6490 segT segment ATTRIBUTE_UNUSED;
6491 valueT size;
6493 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6494 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
6496 /* For a.out, force the section size to be aligned. If we don't do
6497 this, BFD will align it for us, but it will not write out the
6498 final bytes of the section. This may be a bug in BFD, but it is
6499 easier to fix it here since that is how the other a.out targets
6500 work. */
6501 int align;
6503 align = bfd_get_section_alignment (stdoutput, segment);
6504 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
6506 #endif
6508 return size;
6511 /* On the i386, PC-relative offsets are relative to the start of the
6512 next instruction. That is, the address of the offset, plus its
6513 size, since the offset is always the last part of the insn. */
6515 long
6516 md_pcrel_from (fixS *fixP)
6518 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
6521 #ifndef I386COFF
6523 static void
6524 s_bss (int ignore ATTRIBUTE_UNUSED)
6526 int temp;
6528 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
6529 if (IS_ELF)
6530 obj_elf_section_change_hook ();
6531 #endif
6532 temp = get_absolute_expression ();
6533 subseg_set (bss_section, (subsegT) temp);
6534 demand_empty_rest_of_line ();
6537 #endif
6539 void
6540 i386_validate_fix (fixS *fixp)
6542 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
6544 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
6546 if (!object_64bit)
6547 abort ();
6548 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
6550 else
6552 if (!object_64bit)
6553 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
6554 else
6555 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
6557 fixp->fx_subsy = 0;
6561 arelent *
6562 tc_gen_reloc (section, fixp)
6563 asection *section ATTRIBUTE_UNUSED;
6564 fixS *fixp;
6566 arelent *rel;
6567 bfd_reloc_code_real_type code;
6569 switch (fixp->fx_r_type)
6571 case BFD_RELOC_X86_64_PLT32:
6572 case BFD_RELOC_X86_64_GOT32:
6573 case BFD_RELOC_X86_64_GOTPCREL:
6574 case BFD_RELOC_386_PLT32:
6575 case BFD_RELOC_386_GOT32:
6576 case BFD_RELOC_386_GOTOFF:
6577 case BFD_RELOC_386_GOTPC:
6578 case BFD_RELOC_386_TLS_GD:
6579 case BFD_RELOC_386_TLS_LDM:
6580 case BFD_RELOC_386_TLS_LDO_32:
6581 case BFD_RELOC_386_TLS_IE_32:
6582 case BFD_RELOC_386_TLS_IE:
6583 case BFD_RELOC_386_TLS_GOTIE:
6584 case BFD_RELOC_386_TLS_LE_32:
6585 case BFD_RELOC_386_TLS_LE:
6586 case BFD_RELOC_386_TLS_GOTDESC:
6587 case BFD_RELOC_386_TLS_DESC_CALL:
6588 case BFD_RELOC_X86_64_TLSGD:
6589 case BFD_RELOC_X86_64_TLSLD:
6590 case BFD_RELOC_X86_64_DTPOFF32:
6591 case BFD_RELOC_X86_64_DTPOFF64:
6592 case BFD_RELOC_X86_64_GOTTPOFF:
6593 case BFD_RELOC_X86_64_TPOFF32:
6594 case BFD_RELOC_X86_64_TPOFF64:
6595 case BFD_RELOC_X86_64_GOTOFF64:
6596 case BFD_RELOC_X86_64_GOTPC32:
6597 case BFD_RELOC_X86_64_GOT64:
6598 case BFD_RELOC_X86_64_GOTPCREL64:
6599 case BFD_RELOC_X86_64_GOTPC64:
6600 case BFD_RELOC_X86_64_GOTPLT64:
6601 case BFD_RELOC_X86_64_PLTOFF64:
6602 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
6603 case BFD_RELOC_X86_64_TLSDESC_CALL:
6604 case BFD_RELOC_RVA:
6605 case BFD_RELOC_VTABLE_ENTRY:
6606 case BFD_RELOC_VTABLE_INHERIT:
6607 #ifdef TE_PE
6608 case BFD_RELOC_32_SECREL:
6609 #endif
6610 code = fixp->fx_r_type;
6611 break;
6612 case BFD_RELOC_X86_64_32S:
6613 if (!fixp->fx_pcrel)
6615 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
6616 code = fixp->fx_r_type;
6617 break;
6619 default:
6620 if (fixp->fx_pcrel)
6622 switch (fixp->fx_size)
6624 default:
6625 as_bad_where (fixp->fx_file, fixp->fx_line,
6626 _("can not do %d byte pc-relative relocation"),
6627 fixp->fx_size);
6628 code = BFD_RELOC_32_PCREL;
6629 break;
6630 case 1: code = BFD_RELOC_8_PCREL; break;
6631 case 2: code = BFD_RELOC_16_PCREL; break;
6632 case 4: code = BFD_RELOC_32_PCREL; break;
6633 #ifdef BFD64
6634 case 8: code = BFD_RELOC_64_PCREL; break;
6635 #endif
6638 else
6640 switch (fixp->fx_size)
6642 default:
6643 as_bad_where (fixp->fx_file, fixp->fx_line,
6644 _("can not do %d byte relocation"),
6645 fixp->fx_size);
6646 code = BFD_RELOC_32;
6647 break;
6648 case 1: code = BFD_RELOC_8; break;
6649 case 2: code = BFD_RELOC_16; break;
6650 case 4: code = BFD_RELOC_32; break;
6651 #ifdef BFD64
6652 case 8: code = BFD_RELOC_64; break;
6653 #endif
6656 break;
6659 if ((code == BFD_RELOC_32
6660 || code == BFD_RELOC_32_PCREL
6661 || code == BFD_RELOC_X86_64_32S)
6662 && GOT_symbol
6663 && fixp->fx_addsy == GOT_symbol)
6665 if (!object_64bit)
6666 code = BFD_RELOC_386_GOTPC;
6667 else
6668 code = BFD_RELOC_X86_64_GOTPC32;
6670 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
6671 && GOT_symbol
6672 && fixp->fx_addsy == GOT_symbol)
6674 code = BFD_RELOC_X86_64_GOTPC64;
6677 rel = (arelent *) xmalloc (sizeof (arelent));
6678 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
6679 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
6681 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
6683 if (!use_rela_relocations)
6685 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
6686 vtable entry to be used in the relocation's section offset. */
6687 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
6688 rel->address = fixp->fx_offset;
6690 rel->addend = 0;
6692 /* Use the rela in 64bit mode. */
6693 else
6695 if (!fixp->fx_pcrel)
6696 rel->addend = fixp->fx_offset;
6697 else
6698 switch (code)
6700 case BFD_RELOC_X86_64_PLT32:
6701 case BFD_RELOC_X86_64_GOT32:
6702 case BFD_RELOC_X86_64_GOTPCREL:
6703 case BFD_RELOC_X86_64_TLSGD:
6704 case BFD_RELOC_X86_64_TLSLD:
6705 case BFD_RELOC_X86_64_GOTTPOFF:
6706 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
6707 case BFD_RELOC_X86_64_TLSDESC_CALL:
6708 rel->addend = fixp->fx_offset - fixp->fx_size;
6709 break;
6710 default:
6711 rel->addend = (section->vma
6712 - fixp->fx_size
6713 + fixp->fx_addnumber
6714 + md_pcrel_from (fixp));
6715 break;
6719 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
6720 if (rel->howto == NULL)
6722 as_bad_where (fixp->fx_file, fixp->fx_line,
6723 _("cannot represent relocation type %s"),
6724 bfd_get_reloc_code_name (code));
6725 /* Set howto to a garbage value so that we can keep going. */
6726 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
6727 assert (rel->howto != NULL);
6730 return rel;
6734 /* Parse operands using Intel syntax. This implements a recursive descent
6735 parser based on the BNF grammar published in Appendix B of the MASM 6.1
6736 Programmer's Guide.
6738 FIXME: We do not recognize the full operand grammar defined in the MASM
6739 documentation. In particular, all the structure/union and
6740 high-level macro operands are missing.
6742 Uppercase words are terminals, lower case words are non-terminals.
6743 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
6744 bars '|' denote choices. Most grammar productions are implemented in
6745 functions called 'intel_<production>'.
6747 Initial production is 'expr'.
6749 addOp + | -
6751 alpha [a-zA-Z]
6753 binOp & | AND | \| | OR | ^ | XOR
6755 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
6757 constant digits [[ radixOverride ]]
6759 dataType BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD
6761 digits decdigit
6762 | digits decdigit
6763 | digits hexdigit
6765 decdigit [0-9]
6767 e04 e04 addOp e05
6768 | e05
6770 e05 e05 binOp e06
6771 | e06
6773 e06 e06 mulOp e09
6774 | e09
6776 e09 OFFSET e10
6777 | SHORT e10
6778 | + e10
6779 | - e10
6780 | ~ e10
6781 | NOT e10
6782 | e09 PTR e10
6783 | e09 : e10
6784 | e10
6786 e10 e10 [ expr ]
6787 | e11
6789 e11 ( expr )
6790 | [ expr ]
6791 | constant
6792 | dataType
6793 | id
6795 | register
6797 => expr expr cmpOp e04
6798 | e04
6800 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
6801 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
6803 hexdigit a | b | c | d | e | f
6804 | A | B | C | D | E | F
6806 id alpha
6807 | id alpha
6808 | id decdigit
6810 mulOp * | / | % | MOD | << | SHL | >> | SHR
6812 quote " | '
6814 register specialRegister
6815 | gpRegister
6816 | byteRegister
6818 segmentRegister CS | DS | ES | FS | GS | SS
6820 specialRegister CR0 | CR2 | CR3 | CR4
6821 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
6822 | TR3 | TR4 | TR5 | TR6 | TR7
6824 We simplify the grammar in obvious places (e.g., register parsing is
6825 done by calling parse_register) and eliminate immediate left recursion
6826 to implement a recursive-descent parser.
6828 expr e04 expr'
6830 expr' cmpOp e04 expr'
6831 | Empty
6833 e04 e05 e04'
6835 e04' addOp e05 e04'
6836 | Empty
6838 e05 e06 e05'
6840 e05' binOp e06 e05'
6841 | Empty
6843 e06 e09 e06'
6845 e06' mulOp e09 e06'
6846 | Empty
6848 e09 OFFSET e10 e09'
6849 | SHORT e10'
6850 | + e10'
6851 | - e10'
6852 | ~ e10'
6853 | NOT e10'
6854 | e10 e09'
6856 e09' PTR e10 e09'
6857 | : e10 e09'
6858 | Empty
6860 e10 e11 e10'
6862 e10' [ expr ] e10'
6863 | Empty
6865 e11 ( expr )
6866 | [ expr ]
6867 | BYTE
6868 | WORD
6869 | DWORD
6870 | FWORD
6871 | QWORD
6872 | TBYTE
6873 | OWORD
6874 | XMMWORD
6877 | register
6878 | id
6879 | constant */
6881 /* Parsing structure for the intel syntax parser. Used to implement the
6882 semantic actions for the operand grammar. */
6883 struct intel_parser_s
6885 char *op_string; /* The string being parsed. */
6886 int got_a_float; /* Whether the operand is a float. */
6887 int op_modifier; /* Operand modifier. */
6888 int is_mem; /* 1 if operand is memory reference. */
6889 int in_offset; /* >=1 if parsing operand of offset. */
6890 int in_bracket; /* >=1 if parsing operand in brackets. */
6891 const reg_entry *reg; /* Last register reference found. */
6892 char *disp; /* Displacement string being built. */
6893 char *next_operand; /* Resume point when splitting operands. */
6896 static struct intel_parser_s intel_parser;
6898 /* Token structure for parsing intel syntax. */
6899 struct intel_token
6901 int code; /* Token code. */
6902 const reg_entry *reg; /* Register entry for register tokens. */
6903 char *str; /* String representation. */
6906 static struct intel_token cur_token, prev_token;
6908 /* Token codes for the intel parser. Since T_SHORT is already used
6909 by COFF, undefine it first to prevent a warning. */
6910 #define T_NIL -1
6911 #define T_CONST 1
6912 #define T_REG 2
6913 #define T_BYTE 3
6914 #define T_WORD 4
6915 #define T_DWORD 5
6916 #define T_FWORD 6
6917 #define T_QWORD 7
6918 #define T_TBYTE 8
6919 #define T_XMMWORD 9
6920 #undef T_SHORT
6921 #define T_SHORT 10
6922 #define T_OFFSET 11
6923 #define T_PTR 12
6924 #define T_ID 13
6925 #define T_SHL 14
6926 #define T_SHR 15
6928 /* Prototypes for intel parser functions. */
6929 static int intel_match_token (int);
6930 static void intel_putback_token (void);
6931 static void intel_get_token (void);
6932 static int intel_expr (void);
6933 static int intel_e04 (void);
6934 static int intel_e05 (void);
6935 static int intel_e06 (void);
6936 static int intel_e09 (void);
6937 static int intel_e10 (void);
6938 static int intel_e11 (void);
6940 static int
6941 i386_intel_operand (char *operand_string, int got_a_float)
6943 int ret;
6944 char *p;
6946 p = intel_parser.op_string = xstrdup (operand_string);
6947 intel_parser.disp = (char *) xmalloc (strlen (operand_string) + 1);
6949 for (;;)
6951 /* Initialize token holders. */
6952 cur_token.code = prev_token.code = T_NIL;
6953 cur_token.reg = prev_token.reg = NULL;
6954 cur_token.str = prev_token.str = NULL;
6956 /* Initialize parser structure. */
6957 intel_parser.got_a_float = got_a_float;
6958 intel_parser.op_modifier = 0;
6959 intel_parser.is_mem = 0;
6960 intel_parser.in_offset = 0;
6961 intel_parser.in_bracket = 0;
6962 intel_parser.reg = NULL;
6963 intel_parser.disp[0] = '\0';
6964 intel_parser.next_operand = NULL;
6966 /* Read the first token and start the parser. */
6967 intel_get_token ();
6968 ret = intel_expr ();
6970 if (!ret)
6971 break;
6973 if (cur_token.code != T_NIL)
6975 as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
6976 current_templates->start->name, cur_token.str);
6977 ret = 0;
6979 /* If we found a memory reference, hand it over to i386_displacement
6980 to fill in the rest of the operand fields. */
6981 else if (intel_parser.is_mem)
6983 if ((i.mem_operands == 1
6984 && (current_templates->start->opcode_modifier & IsString) == 0)
6985 || i.mem_operands == 2)
6987 as_bad (_("too many memory references for '%s'"),
6988 current_templates->start->name);
6989 ret = 0;
6991 else
6993 char *s = intel_parser.disp;
6994 i.mem_operands++;
6996 if (!quiet_warnings && intel_parser.is_mem < 0)
6997 /* See the comments in intel_bracket_expr. */
6998 as_warn (_("Treating `%s' as memory reference"), operand_string);
7000 /* Add the displacement expression. */
7001 if (*s != '\0')
7002 ret = i386_displacement (s, s + strlen (s));
7003 if (ret)
7005 /* Swap base and index in 16-bit memory operands like
7006 [si+bx]. Since i386_index_check is also used in AT&T
7007 mode we have to do that here. */
7008 if (i.base_reg
7009 && i.index_reg
7010 && (i.base_reg->reg_type & Reg16)
7011 && (i.index_reg->reg_type & Reg16)
7012 && i.base_reg->reg_num >= 6
7013 && i.index_reg->reg_num < 6)
7015 const reg_entry *base = i.index_reg;
7017 i.index_reg = i.base_reg;
7018 i.base_reg = base;
7020 ret = i386_index_check (operand_string);
7025 /* Constant and OFFSET expressions are handled by i386_immediate. */
7026 else if ((intel_parser.op_modifier & (1 << T_OFFSET))
7027 || intel_parser.reg == NULL)
7028 ret = i386_immediate (intel_parser.disp);
7030 if (intel_parser.next_operand && this_operand >= MAX_OPERANDS - 1)
7031 ret = 0;
7032 if (!ret || !intel_parser.next_operand)
7033 break;
7034 intel_parser.op_string = intel_parser.next_operand;
7035 this_operand = i.operands++;
7038 free (p);
7039 free (intel_parser.disp);
7041 return ret;
7044 #define NUM_ADDRESS_REGS (!!i.base_reg + !!i.index_reg)
7046 /* expr e04 expr'
7048 expr' cmpOp e04 expr'
7049 | Empty */
7050 static int
7051 intel_expr (void)
7053 /* XXX Implement the comparison operators. */
7054 return intel_e04 ();
7057 /* e04 e05 e04'
7059 e04' addOp e05 e04'
7060 | Empty */
7061 static int
7062 intel_e04 (void)
7064 int nregs = -1;
7066 for (;;)
7068 if (!intel_e05())
7069 return 0;
7071 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7072 i.base_reg = i386_regtab + REGNAM_AL; /* al is invalid as base */
7074 if (cur_token.code == '+')
7075 nregs = -1;
7076 else if (cur_token.code == '-')
7077 nregs = NUM_ADDRESS_REGS;
7078 else
7079 return 1;
7081 strcat (intel_parser.disp, cur_token.str);
7082 intel_match_token (cur_token.code);
7086 /* e05 e06 e05'
7088 e05' binOp e06 e05'
7089 | Empty */
7090 static int
7091 intel_e05 (void)
7093 int nregs = ~NUM_ADDRESS_REGS;
7095 for (;;)
7097 if (!intel_e06())
7098 return 0;
7100 if (cur_token.code == '&'
7101 || cur_token.code == '|'
7102 || cur_token.code == '^')
7104 char str[2];
7106 str[0] = cur_token.code;
7107 str[1] = 0;
7108 strcat (intel_parser.disp, str);
7110 else
7111 break;
7113 intel_match_token (cur_token.code);
7115 if (nregs < 0)
7116 nregs = ~nregs;
7118 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7119 i.base_reg = i386_regtab + REGNAM_AL + 1; /* cl is invalid as base */
7120 return 1;
7123 /* e06 e09 e06'
7125 e06' mulOp e09 e06'
7126 | Empty */
7127 static int
7128 intel_e06 (void)
7130 int nregs = ~NUM_ADDRESS_REGS;
7132 for (;;)
7134 if (!intel_e09())
7135 return 0;
7137 if (cur_token.code == '*'
7138 || cur_token.code == '/'
7139 || cur_token.code == '%')
7141 char str[2];
7143 str[0] = cur_token.code;
7144 str[1] = 0;
7145 strcat (intel_parser.disp, str);
7147 else if (cur_token.code == T_SHL)
7148 strcat (intel_parser.disp, "<<");
7149 else if (cur_token.code == T_SHR)
7150 strcat (intel_parser.disp, ">>");
7151 else
7152 break;
7154 intel_match_token (cur_token.code);
7156 if (nregs < 0)
7157 nregs = ~nregs;
7159 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7160 i.base_reg = i386_regtab + REGNAM_AL + 2; /* dl is invalid as base */
7161 return 1;
7164 /* e09 OFFSET e09
7165 | SHORT e09
7166 | + e09
7167 | - e09
7168 | ~ e09
7169 | NOT e09
7170 | e10 e09'
7172 e09' PTR e10 e09'
7173 | : e10 e09'
7174 | Empty */
7175 static int
7176 intel_e09 (void)
7178 int nregs = ~NUM_ADDRESS_REGS;
7179 int in_offset = 0;
7181 for (;;)
7183 /* Don't consume constants here. */
7184 if (cur_token.code == '+' || cur_token.code == '-')
7186 /* Need to look one token ahead - if the next token
7187 is a constant, the current token is its sign. */
7188 int next_code;
7190 intel_match_token (cur_token.code);
7191 next_code = cur_token.code;
7192 intel_putback_token ();
7193 if (next_code == T_CONST)
7194 break;
7197 /* e09 OFFSET e09 */
7198 if (cur_token.code == T_OFFSET)
7200 if (!in_offset++)
7201 ++intel_parser.in_offset;
7204 /* e09 SHORT e09 */
7205 else if (cur_token.code == T_SHORT)
7206 intel_parser.op_modifier |= 1 << T_SHORT;
7208 /* e09 + e09 */
7209 else if (cur_token.code == '+')
7210 strcat (intel_parser.disp, "+");
7212 /* e09 - e09
7213 | ~ e09
7214 | NOT e09 */
7215 else if (cur_token.code == '-' || cur_token.code == '~')
7217 char str[2];
7219 if (nregs < 0)
7220 nregs = ~nregs;
7221 str[0] = cur_token.code;
7222 str[1] = 0;
7223 strcat (intel_parser.disp, str);
7226 /* e09 e10 e09' */
7227 else
7228 break;
7230 intel_match_token (cur_token.code);
7233 for (;;)
7235 if (!intel_e10 ())
7236 return 0;
7238 /* e09' PTR e10 e09' */
7239 if (cur_token.code == T_PTR)
7241 char suffix;
7243 if (prev_token.code == T_BYTE)
7244 suffix = BYTE_MNEM_SUFFIX;
7246 else if (prev_token.code == T_WORD)
7248 if (current_templates->start->name[0] == 'l'
7249 && current_templates->start->name[2] == 's'
7250 && current_templates->start->name[3] == 0)
7251 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7252 else if (intel_parser.got_a_float == 2) /* "fi..." */
7253 suffix = SHORT_MNEM_SUFFIX;
7254 else
7255 suffix = WORD_MNEM_SUFFIX;
7258 else if (prev_token.code == T_DWORD)
7260 if (current_templates->start->name[0] == 'l'
7261 && current_templates->start->name[2] == 's'
7262 && current_templates->start->name[3] == 0)
7263 suffix = WORD_MNEM_SUFFIX;
7264 else if (flag_code == CODE_16BIT
7265 && (current_templates->start->opcode_modifier
7266 & (Jump | JumpDword)))
7267 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7268 else if (intel_parser.got_a_float == 1) /* "f..." */
7269 suffix = SHORT_MNEM_SUFFIX;
7270 else
7271 suffix = LONG_MNEM_SUFFIX;
7274 else if (prev_token.code == T_FWORD)
7276 if (current_templates->start->name[0] == 'l'
7277 && current_templates->start->name[2] == 's'
7278 && current_templates->start->name[3] == 0)
7279 suffix = LONG_MNEM_SUFFIX;
7280 else if (!intel_parser.got_a_float)
7282 if (flag_code == CODE_16BIT)
7283 add_prefix (DATA_PREFIX_OPCODE);
7284 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7286 else
7287 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7290 else if (prev_token.code == T_QWORD)
7292 if (intel_parser.got_a_float == 1) /* "f..." */
7293 suffix = LONG_MNEM_SUFFIX;
7294 else
7295 suffix = QWORD_MNEM_SUFFIX;
7298 else if (prev_token.code == T_TBYTE)
7300 if (intel_parser.got_a_float == 1)
7301 suffix = LONG_DOUBLE_MNEM_SUFFIX;
7302 else
7303 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
7306 else if (prev_token.code == T_XMMWORD)
7308 /* XXX ignored for now, but accepted since gcc uses it */
7309 suffix = 0;
7312 else
7314 as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
7315 return 0;
7318 /* Operands for jump/call using 'ptr' notation denote absolute
7319 addresses. */
7320 if (current_templates->start->opcode_modifier & (Jump | JumpDword))
7321 i.types[this_operand] |= JumpAbsolute;
7323 if (current_templates->start->base_opcode == 0x8d /* lea */)
7325 else if (!i.suffix)
7326 i.suffix = suffix;
7327 else if (i.suffix != suffix)
7329 as_bad (_("Conflicting operand modifiers"));
7330 return 0;
7335 /* e09' : e10 e09' */
7336 else if (cur_token.code == ':')
7338 if (prev_token.code != T_REG)
7340 /* While {call,jmp} SSSS:OOOO is MASM syntax only when SSSS is a
7341 segment/group identifier (which we don't have), using comma
7342 as the operand separator there is even less consistent, since
7343 there all branches only have a single operand. */
7344 if (this_operand != 0
7345 || intel_parser.in_offset
7346 || intel_parser.in_bracket
7347 || (!(current_templates->start->opcode_modifier
7348 & (Jump|JumpDword|JumpInterSegment))
7349 && !(current_templates->start->operand_types[0]
7350 & JumpAbsolute)))
7351 return intel_match_token (T_NIL);
7352 /* Remember the start of the 2nd operand and terminate 1st
7353 operand here.
7354 XXX This isn't right, yet (when SSSS:OOOO is right operand of
7355 another expression), but it gets at least the simplest case
7356 (a plain number or symbol on the left side) right. */
7357 intel_parser.next_operand = intel_parser.op_string;
7358 *--intel_parser.op_string = '\0';
7359 return intel_match_token (':');
7363 /* e09' Empty */
7364 else
7365 break;
7367 intel_match_token (cur_token.code);
7371 if (in_offset)
7373 --intel_parser.in_offset;
7374 if (nregs < 0)
7375 nregs = ~nregs;
7376 if (NUM_ADDRESS_REGS > nregs)
7378 as_bad (_("Invalid operand to `OFFSET'"));
7379 return 0;
7381 intel_parser.op_modifier |= 1 << T_OFFSET;
7384 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
7385 i.base_reg = i386_regtab + REGNAM_AL + 3; /* bl is invalid as base */
7386 return 1;
7389 static int
7390 intel_bracket_expr (void)
7392 int was_offset = intel_parser.op_modifier & (1 << T_OFFSET);
7393 const char *start = intel_parser.op_string;
7394 int len;
7396 if (i.op[this_operand].regs)
7397 return intel_match_token (T_NIL);
7399 intel_match_token ('[');
7401 /* Mark as a memory operand only if it's not already known to be an
7402 offset expression. If it's an offset expression, we need to keep
7403 the brace in. */
7404 if (!intel_parser.in_offset)
7406 ++intel_parser.in_bracket;
7408 /* Operands for jump/call inside brackets denote absolute addresses. */
7409 if (current_templates->start->opcode_modifier & (Jump | JumpDword))
7410 i.types[this_operand] |= JumpAbsolute;
7412 /* Unfortunately gas always diverged from MASM in a respect that can't
7413 be easily fixed without risking to break code sequences likely to be
7414 encountered (the testsuite even check for this): MASM doesn't consider
7415 an expression inside brackets unconditionally as a memory reference.
7416 When that is e.g. a constant, an offset expression, or the sum of the
7417 two, this is still taken as a constant load. gas, however, always
7418 treated these as memory references. As a compromise, we'll try to make
7419 offset expressions inside brackets work the MASM way (since that's
7420 less likely to be found in real world code), but make constants alone
7421 continue to work the traditional gas way. In either case, issue a
7422 warning. */
7423 intel_parser.op_modifier &= ~was_offset;
7425 else
7426 strcat (intel_parser.disp, "[");
7428 /* Add a '+' to the displacement string if necessary. */
7429 if (*intel_parser.disp != '\0'
7430 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
7431 strcat (intel_parser.disp, "+");
7433 if (intel_expr ()
7434 && (len = intel_parser.op_string - start - 1,
7435 intel_match_token (']')))
7437 /* Preserve brackets when the operand is an offset expression. */
7438 if (intel_parser.in_offset)
7439 strcat (intel_parser.disp, "]");
7440 else
7442 --intel_parser.in_bracket;
7443 if (i.base_reg || i.index_reg)
7444 intel_parser.is_mem = 1;
7445 if (!intel_parser.is_mem)
7447 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
7448 /* Defer the warning until all of the operand was parsed. */
7449 intel_parser.is_mem = -1;
7450 else if (!quiet_warnings)
7451 as_warn (_("`[%.*s]' taken to mean just `%.*s'"),
7452 len, start, len, start);
7455 intel_parser.op_modifier |= was_offset;
7457 return 1;
7459 return 0;
7462 /* e10 e11 e10'
7464 e10' [ expr ] e10'
7465 | Empty */
7466 static int
7467 intel_e10 (void)
7469 if (!intel_e11 ())
7470 return 0;
7472 while (cur_token.code == '[')
7474 if (!intel_bracket_expr ())
7475 return 0;
7478 return 1;
7481 /* e11 ( expr )
7482 | [ expr ]
7483 | BYTE
7484 | WORD
7485 | DWORD
7486 | FWORD
7487 | QWORD
7488 | TBYTE
7489 | OWORD
7490 | XMMWORD
7493 | register
7494 | id
7495 | constant */
7496 static int
7497 intel_e11 (void)
7499 switch (cur_token.code)
7501 /* e11 ( expr ) */
7502 case '(':
7503 intel_match_token ('(');
7504 strcat (intel_parser.disp, "(");
7506 if (intel_expr () && intel_match_token (')'))
7508 strcat (intel_parser.disp, ")");
7509 return 1;
7511 return 0;
7513 /* e11 [ expr ] */
7514 case '[':
7515 return intel_bracket_expr ();
7517 /* e11 $
7518 | . */
7519 case '.':
7520 strcat (intel_parser.disp, cur_token.str);
7521 intel_match_token (cur_token.code);
7523 /* Mark as a memory operand only if it's not already known to be an
7524 offset expression. */
7525 if (!intel_parser.in_offset)
7526 intel_parser.is_mem = 1;
7528 return 1;
7530 /* e11 register */
7531 case T_REG:
7533 const reg_entry *reg = intel_parser.reg = cur_token.reg;
7535 intel_match_token (T_REG);
7537 /* Check for segment change. */
7538 if (cur_token.code == ':')
7540 if (!(reg->reg_type & (SReg2 | SReg3)))
7542 as_bad (_("`%s' is not a valid segment register"),
7543 reg->reg_name);
7544 return 0;
7546 else if (i.seg[i.mem_operands])
7547 as_warn (_("Extra segment override ignored"));
7548 else
7550 if (!intel_parser.in_offset)
7551 intel_parser.is_mem = 1;
7552 switch (reg->reg_num)
7554 case 0:
7555 i.seg[i.mem_operands] = &es;
7556 break;
7557 case 1:
7558 i.seg[i.mem_operands] = &cs;
7559 break;
7560 case 2:
7561 i.seg[i.mem_operands] = &ss;
7562 break;
7563 case 3:
7564 i.seg[i.mem_operands] = &ds;
7565 break;
7566 case 4:
7567 i.seg[i.mem_operands] = &fs;
7568 break;
7569 case 5:
7570 i.seg[i.mem_operands] = &gs;
7571 break;
7576 /* Not a segment register. Check for register scaling. */
7577 else if (cur_token.code == '*')
7579 if (!intel_parser.in_bracket)
7581 as_bad (_("Register scaling only allowed in memory operands"));
7582 return 0;
7585 if (reg->reg_type & Reg16) /* Disallow things like [si*1]. */
7586 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
7587 else if (i.index_reg)
7588 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
7590 /* What follows must be a valid scale. */
7591 intel_match_token ('*');
7592 i.index_reg = reg;
7593 i.types[this_operand] |= BaseIndex;
7595 /* Set the scale after setting the register (otherwise,
7596 i386_scale will complain) */
7597 if (cur_token.code == '+' || cur_token.code == '-')
7599 char *str, sign = cur_token.code;
7600 intel_match_token (cur_token.code);
7601 if (cur_token.code != T_CONST)
7603 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
7604 cur_token.str);
7605 return 0;
7607 str = (char *) xmalloc (strlen (cur_token.str) + 2);
7608 strcpy (str + 1, cur_token.str);
7609 *str = sign;
7610 if (!i386_scale (str))
7611 return 0;
7612 free (str);
7614 else if (!i386_scale (cur_token.str))
7615 return 0;
7616 intel_match_token (cur_token.code);
7619 /* No scaling. If this is a memory operand, the register is either a
7620 base register (first occurrence) or an index register (second
7621 occurrence). */
7622 else if (intel_parser.in_bracket)
7625 if (!i.base_reg)
7626 i.base_reg = reg;
7627 else if (!i.index_reg)
7628 i.index_reg = reg;
7629 else
7631 as_bad (_("Too many register references in memory operand"));
7632 return 0;
7635 i.types[this_operand] |= BaseIndex;
7638 /* It's neither base nor index. */
7639 else if (!intel_parser.in_offset && !intel_parser.is_mem)
7641 i.types[this_operand] |= reg->reg_type & ~BaseIndex;
7642 i.op[this_operand].regs = reg;
7643 i.reg_operands++;
7645 else
7647 as_bad (_("Invalid use of register"));
7648 return 0;
7651 /* Since registers are not part of the displacement string (except
7652 when we're parsing offset operands), we may need to remove any
7653 preceding '+' from the displacement string. */
7654 if (*intel_parser.disp != '\0'
7655 && !intel_parser.in_offset)
7657 char *s = intel_parser.disp;
7658 s += strlen (s) - 1;
7659 if (*s == '+')
7660 *s = '\0';
7663 return 1;
7666 /* e11 BYTE
7667 | WORD
7668 | DWORD
7669 | FWORD
7670 | QWORD
7671 | TBYTE
7672 | OWORD
7673 | XMMWORD */
7674 case T_BYTE:
7675 case T_WORD:
7676 case T_DWORD:
7677 case T_FWORD:
7678 case T_QWORD:
7679 case T_TBYTE:
7680 case T_XMMWORD:
7681 intel_match_token (cur_token.code);
7683 if (cur_token.code == T_PTR)
7684 return 1;
7686 /* It must have been an identifier. */
7687 intel_putback_token ();
7688 cur_token.code = T_ID;
7689 /* FALLTHRU */
7691 /* e11 id
7692 | constant */
7693 case T_ID:
7694 if (!intel_parser.in_offset && intel_parser.is_mem <= 0)
7696 symbolS *symbolP;
7698 /* The identifier represents a memory reference only if it's not
7699 preceded by an offset modifier and if it's not an equate. */
7700 symbolP = symbol_find(cur_token.str);
7701 if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
7702 intel_parser.is_mem = 1;
7704 /* FALLTHRU */
7706 case T_CONST:
7707 case '-':
7708 case '+':
7710 char *save_str, sign = 0;
7712 /* Allow constants that start with `+' or `-'. */
7713 if (cur_token.code == '-' || cur_token.code == '+')
7715 sign = cur_token.code;
7716 intel_match_token (cur_token.code);
7717 if (cur_token.code != T_CONST)
7719 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
7720 cur_token.str);
7721 return 0;
7725 save_str = (char *) xmalloc (strlen (cur_token.str) + 2);
7726 strcpy (save_str + !!sign, cur_token.str);
7727 if (sign)
7728 *save_str = sign;
7730 /* Get the next token to check for register scaling. */
7731 intel_match_token (cur_token.code);
7733 /* Check if this constant is a scaling factor for an
7734 index register. */
7735 if (cur_token.code == '*')
7737 if (intel_match_token ('*') && cur_token.code == T_REG)
7739 const reg_entry *reg = cur_token.reg;
7741 if (!intel_parser.in_bracket)
7743 as_bad (_("Register scaling only allowed "
7744 "in memory operands"));
7745 return 0;
7748 /* Disallow things like [1*si].
7749 sp and esp are invalid as index. */
7750 if (reg->reg_type & Reg16)
7751 reg = i386_regtab + REGNAM_AX + 4;
7752 else if (i.index_reg)
7753 reg = i386_regtab + REGNAM_EAX + 4;
7755 /* The constant is followed by `* reg', so it must be
7756 a valid scale. */
7757 i.index_reg = reg;
7758 i.types[this_operand] |= BaseIndex;
7760 /* Set the scale after setting the register (otherwise,
7761 i386_scale will complain) */
7762 if (!i386_scale (save_str))
7763 return 0;
7764 intel_match_token (T_REG);
7766 /* Since registers are not part of the displacement
7767 string, we may need to remove any preceding '+' from
7768 the displacement string. */
7769 if (*intel_parser.disp != '\0')
7771 char *s = intel_parser.disp;
7772 s += strlen (s) - 1;
7773 if (*s == '+')
7774 *s = '\0';
7777 free (save_str);
7779 return 1;
7782 /* The constant was not used for register scaling. Since we have
7783 already consumed the token following `*' we now need to put it
7784 back in the stream. */
7785 intel_putback_token ();
7788 /* Add the constant to the displacement string. */
7789 strcat (intel_parser.disp, save_str);
7790 free (save_str);
7792 return 1;
7796 as_bad (_("Unrecognized token '%s'"), cur_token.str);
7797 return 0;
7800 /* Match the given token against cur_token. If they match, read the next
7801 token from the operand string. */
7802 static int
7803 intel_match_token (int code)
7805 if (cur_token.code == code)
7807 intel_get_token ();
7808 return 1;
7810 else
7812 as_bad (_("Unexpected token `%s'"), cur_token.str);
7813 return 0;
7817 /* Read a new token from intel_parser.op_string and store it in cur_token. */
7818 static void
7819 intel_get_token (void)
7821 char *end_op;
7822 const reg_entry *reg;
7823 struct intel_token new_token;
7825 new_token.code = T_NIL;
7826 new_token.reg = NULL;
7827 new_token.str = NULL;
7829 /* Free the memory allocated to the previous token and move
7830 cur_token to prev_token. */
7831 if (prev_token.str)
7832 free (prev_token.str);
7834 prev_token = cur_token;
7836 /* Skip whitespace. */
7837 while (is_space_char (*intel_parser.op_string))
7838 intel_parser.op_string++;
7840 /* Return an empty token if we find nothing else on the line. */
7841 if (*intel_parser.op_string == '\0')
7843 cur_token = new_token;
7844 return;
7847 /* The new token cannot be larger than the remainder of the operand
7848 string. */
7849 new_token.str = (char *) xmalloc (strlen (intel_parser.op_string) + 1);
7850 new_token.str[0] = '\0';
7852 if (strchr ("0123456789", *intel_parser.op_string))
7854 char *p = new_token.str;
7855 char *q = intel_parser.op_string;
7856 new_token.code = T_CONST;
7858 /* Allow any kind of identifier char to encompass floating point and
7859 hexadecimal numbers. */
7860 while (is_identifier_char (*q))
7861 *p++ = *q++;
7862 *p = '\0';
7864 /* Recognize special symbol names [0-9][bf]. */
7865 if (strlen (intel_parser.op_string) == 2
7866 && (intel_parser.op_string[1] == 'b'
7867 || intel_parser.op_string[1] == 'f'))
7868 new_token.code = T_ID;
7871 else if ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL)
7873 size_t len = end_op - intel_parser.op_string;
7875 new_token.code = T_REG;
7876 new_token.reg = reg;
7878 memcpy (new_token.str, intel_parser.op_string, len);
7879 new_token.str[len] = '\0';
7882 else if (is_identifier_char (*intel_parser.op_string))
7884 char *p = new_token.str;
7885 char *q = intel_parser.op_string;
7887 /* A '.' or '$' followed by an identifier char is an identifier.
7888 Otherwise, it's operator '.' followed by an expression. */
7889 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
7891 new_token.code = '.';
7892 new_token.str[0] = '.';
7893 new_token.str[1] = '\0';
7895 else
7897 while (is_identifier_char (*q) || *q == '@')
7898 *p++ = *q++;
7899 *p = '\0';
7901 if (strcasecmp (new_token.str, "NOT") == 0)
7902 new_token.code = '~';
7904 else if (strcasecmp (new_token.str, "MOD") == 0)
7905 new_token.code = '%';
7907 else if (strcasecmp (new_token.str, "AND") == 0)
7908 new_token.code = '&';
7910 else if (strcasecmp (new_token.str, "OR") == 0)
7911 new_token.code = '|';
7913 else if (strcasecmp (new_token.str, "XOR") == 0)
7914 new_token.code = '^';
7916 else if (strcasecmp (new_token.str, "SHL") == 0)
7917 new_token.code = T_SHL;
7919 else if (strcasecmp (new_token.str, "SHR") == 0)
7920 new_token.code = T_SHR;
7922 else if (strcasecmp (new_token.str, "BYTE") == 0)
7923 new_token.code = T_BYTE;
7925 else if (strcasecmp (new_token.str, "WORD") == 0)
7926 new_token.code = T_WORD;
7928 else if (strcasecmp (new_token.str, "DWORD") == 0)
7929 new_token.code = T_DWORD;
7931 else if (strcasecmp (new_token.str, "FWORD") == 0)
7932 new_token.code = T_FWORD;
7934 else if (strcasecmp (new_token.str, "QWORD") == 0)
7935 new_token.code = T_QWORD;
7937 else if (strcasecmp (new_token.str, "TBYTE") == 0
7938 /* XXX remove (gcc still uses it) */
7939 || strcasecmp (new_token.str, "XWORD") == 0)
7940 new_token.code = T_TBYTE;
7942 else if (strcasecmp (new_token.str, "XMMWORD") == 0
7943 || strcasecmp (new_token.str, "OWORD") == 0)
7944 new_token.code = T_XMMWORD;
7946 else if (strcasecmp (new_token.str, "PTR") == 0)
7947 new_token.code = T_PTR;
7949 else if (strcasecmp (new_token.str, "SHORT") == 0)
7950 new_token.code = T_SHORT;
7952 else if (strcasecmp (new_token.str, "OFFSET") == 0)
7954 new_token.code = T_OFFSET;
7956 /* ??? This is not mentioned in the MASM grammar but gcc
7957 makes use of it with -mintel-syntax. OFFSET may be
7958 followed by FLAT: */
7959 if (strncasecmp (q, " FLAT:", 6) == 0)
7960 strcat (new_token.str, " FLAT:");
7963 /* ??? This is not mentioned in the MASM grammar. */
7964 else if (strcasecmp (new_token.str, "FLAT") == 0)
7966 new_token.code = T_OFFSET;
7967 if (*q == ':')
7968 strcat (new_token.str, ":");
7969 else
7970 as_bad (_("`:' expected"));
7973 else
7974 new_token.code = T_ID;
7978 else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
7980 new_token.code = *intel_parser.op_string;
7981 new_token.str[0] = *intel_parser.op_string;
7982 new_token.str[1] = '\0';
7985 else if (strchr ("<>", *intel_parser.op_string)
7986 && *intel_parser.op_string == *(intel_parser.op_string + 1))
7988 new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
7989 new_token.str[0] = *intel_parser.op_string;
7990 new_token.str[1] = *intel_parser.op_string;
7991 new_token.str[2] = '\0';
7994 else
7995 as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
7997 intel_parser.op_string += strlen (new_token.str);
7998 cur_token = new_token;
8001 /* Put cur_token back into the token stream and make cur_token point to
8002 prev_token. */
8003 static void
8004 intel_putback_token (void)
8006 if (cur_token.code != T_NIL)
8008 intel_parser.op_string -= strlen (cur_token.str);
8009 free (cur_token.str);
8011 cur_token = prev_token;
8013 /* Forget prev_token. */
8014 prev_token.code = T_NIL;
8015 prev_token.reg = NULL;
8016 prev_token.str = NULL;
8020 tc_x86_regname_to_dw2regnum (char *regname)
8022 unsigned int regnum;
8023 unsigned int regnames_count;
8024 static const char *const regnames_32[] =
8026 "eax", "ecx", "edx", "ebx",
8027 "esp", "ebp", "esi", "edi",
8028 "eip", "eflags", NULL,
8029 "st0", "st1", "st2", "st3",
8030 "st4", "st5", "st6", "st7",
8031 NULL, NULL,
8032 "xmm0", "xmm1", "xmm2", "xmm3",
8033 "xmm4", "xmm5", "xmm6", "xmm7",
8034 "mm0", "mm1", "mm2", "mm3",
8035 "mm4", "mm5", "mm6", "mm7",
8036 "fcw", "fsw", "mxcsr",
8037 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8038 "tr", "ldtr"
8040 static const char *const regnames_64[] =
8042 "rax", "rdx", "rcx", "rbx",
8043 "rsi", "rdi", "rbp", "rsp",
8044 "r8", "r9", "r10", "r11",
8045 "r12", "r13", "r14", "r15",
8046 "rip",
8047 "xmm0", "xmm1", "xmm2", "xmm3",
8048 "xmm4", "xmm5", "xmm6", "xmm7",
8049 "xmm8", "xmm9", "xmm10", "xmm11",
8050 "xmm12", "xmm13", "xmm14", "xmm15",
8051 "st0", "st1", "st2", "st3",
8052 "st4", "st5", "st6", "st7",
8053 "mm0", "mm1", "mm2", "mm3",
8054 "mm4", "mm5", "mm6", "mm7",
8055 "rflags",
8056 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8057 "fs.base", "gs.base", NULL, NULL,
8058 "tr", "ldtr",
8059 "mxcsr", "fcw", "fsw"
8061 const char *const *regnames;
8063 if (flag_code == CODE_64BIT)
8065 regnames = regnames_64;
8066 regnames_count = ARRAY_SIZE (regnames_64);
8068 else
8070 regnames = regnames_32;
8071 regnames_count = ARRAY_SIZE (regnames_32);
8074 for (regnum = 0; regnum < regnames_count; regnum++)
8075 if (regnames[regnum] != NULL
8076 && strcmp (regname, regnames[regnum]) == 0)
8077 return regnum;
8079 return -1;
8082 void
8083 tc_x86_frame_initial_instructions (void)
8085 static unsigned int sp_regno;
8087 if (!sp_regno)
8088 sp_regno = tc_x86_regname_to_dw2regnum (flag_code == CODE_64BIT
8089 ? "rsp" : "esp");
8091 cfi_add_CFA_def_cfa (sp_regno, -x86_cie_data_alignment);
8092 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
8096 i386_elf_section_type (const char *str, size_t len)
8098 if (flag_code == CODE_64BIT
8099 && len == sizeof ("unwind") - 1
8100 && strncmp (str, "unwind", 6) == 0)
8101 return SHT_X86_64_UNWIND;
8103 return -1;
8106 #ifdef TE_PE
8107 void
8108 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
8110 expressionS expr;
8112 expr.X_op = O_secrel;
8113 expr.X_add_symbol = symbol;
8114 expr.X_add_number = 0;
8115 emit_expr (&expr, size);
8117 #endif
8119 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8120 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
8123 x86_64_section_letter (int letter, char **ptr_msg)
8125 if (flag_code == CODE_64BIT)
8127 if (letter == 'l')
8128 return SHF_X86_64_LARGE;
8130 *ptr_msg = _("Bad .section directive: want a,l,w,x,M,S,G,T in string");
8132 else
8133 *ptr_msg = _("Bad .section directive: want a,w,x,M,S,G,T in string");
8134 return -1;
8138 x86_64_section_word (char *str, size_t len)
8140 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
8141 return SHF_X86_64_LARGE;
8143 return -1;
8146 static void
8147 handle_large_common (int small ATTRIBUTE_UNUSED)
8149 if (flag_code != CODE_64BIT)
8151 s_comm_internal (0, elf_common_parse);
8152 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
8154 else
8156 static segT lbss_section;
8157 asection *saved_com_section_ptr = elf_com_section_ptr;
8158 asection *saved_bss_section = bss_section;
8160 if (lbss_section == NULL)
8162 flagword applicable;
8163 segT seg = now_seg;
8164 subsegT subseg = now_subseg;
8166 /* The .lbss section is for local .largecomm symbols. */
8167 lbss_section = subseg_new (".lbss", 0);
8168 applicable = bfd_applicable_section_flags (stdoutput);
8169 bfd_set_section_flags (stdoutput, lbss_section,
8170 applicable & SEC_ALLOC);
8171 seg_info (lbss_section)->bss = 1;
8173 subseg_set (seg, subseg);
8176 elf_com_section_ptr = &_bfd_elf_large_com_section;
8177 bss_section = lbss_section;
8179 s_comm_internal (0, elf_common_parse);
8181 elf_com_section_ptr = saved_com_section_ptr;
8182 bss_section = saved_bss_section;
8185 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */