* tc-i386.c (md_assemble): Skip suffix check if the opcode modifier
[binutils-gdb.git] / gas / config / tc-i386.c
1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* Intel 80386 machine specific gas.
23 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
24 Bugs & suggestions are completely welcome. This is free software.
25 Please help us make it better. */
26
27 #include <ctype.h>
28
29 #include "as.h"
30 #include "subsegs.h"
31 #include "opcode/i386.h"
32
33 #ifndef REGISTER_WARNINGS
34 #define REGISTER_WARNINGS 1
35 #endif
36
37 #ifndef INFER_ADDR_PREFIX
38 #define INFER_ADDR_PREFIX 1
39 #endif
40
41 #ifndef SCALE1_WHEN_NO_INDEX
42 /* Specifying a scale factor besides 1 when there is no index is
43 futile. eg. `mov (%ebx,2),%al' does exactly the same as
44 `mov (%ebx),%al'. To slavishly follow what the programmer
45 specified, set SCALE1_WHEN_NO_INDEX to 0. */
46 #define SCALE1_WHEN_NO_INDEX 1
47 #endif
48
49 #define true 1
50 #define false 0
51
52 static unsigned int mode_from_disp_size PARAMS ((unsigned int));
53 static int fits_in_signed_byte PARAMS ((offsetT));
54 static int fits_in_unsigned_byte PARAMS ((offsetT));
55 static int fits_in_unsigned_word PARAMS ((offsetT));
56 static int fits_in_signed_word PARAMS ((offsetT));
57 static int smallest_imm_type PARAMS ((offsetT));
58 static offsetT offset_in_range PARAMS ((offsetT, int));
59 static int add_prefix PARAMS ((unsigned int));
60 static void set_16bit_code_flag PARAMS ((int));
61 static void set_16bit_gcc_code_flag PARAMS ((int));
62 static void set_intel_syntax PARAMS ((int));
63 static void set_cpu_arch PARAMS ((int));
64
65 #ifdef BFD_ASSEMBLER
66 static bfd_reloc_code_real_type reloc
67 PARAMS ((int, int, bfd_reloc_code_real_type));
68 #endif
69
70 /* 'md_assemble ()' gathers together information and puts it into a
71 i386_insn. */
72
73 union i386_op
74 {
75 expressionS *disps;
76 expressionS *imms;
77 const reg_entry *regs;
78 };
79
80 struct _i386_insn
81 {
82 /* TM holds the template for the insn were currently assembling. */
83 template tm;
84
85 /* SUFFIX holds the instruction mnemonic suffix if given.
86 (e.g. 'l' for 'movl') */
87 char suffix;
88
89 /* OPERANDS gives the number of given operands. */
90 unsigned int operands;
91
92 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
93 of given register, displacement, memory operands and immediate
94 operands. */
95 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
96
97 /* TYPES [i] is the type (see above #defines) which tells us how to
98 use OP[i] for the corresponding operand. */
99 unsigned int types[MAX_OPERANDS];
100
101 /* Displacement expression, immediate expression, or register for each
102 operand. */
103 union i386_op op[MAX_OPERANDS];
104
105 /* Relocation type for operand */
106 #ifdef BFD_ASSEMBLER
107 enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
108 #else
109 int disp_reloc[MAX_OPERANDS];
110 #endif
111
112 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
113 the base index byte below. */
114 const reg_entry *base_reg;
115 const reg_entry *index_reg;
116 unsigned int log2_scale_factor;
117
118 /* SEG gives the seg_entries of this insn. They are zero unless
119 explicit segment overrides are given. */
120 const seg_entry *seg[2]; /* segments for memory operands (if given) */
121
122 /* PREFIX holds all the given prefix opcodes (usually null).
123 PREFIXES is the number of prefix opcodes. */
124 unsigned int prefixes;
125 unsigned char prefix[MAX_PREFIXES];
126
127 /* RM and SIB are the modrm byte and the sib byte where the
128 addressing modes of this insn are encoded. */
129
130 modrm_byte rm;
131 sib_byte sib;
132 };
133
134 typedef struct _i386_insn i386_insn;
135
136 /* List of chars besides those in app.c:symbol_chars that can start an
137 operand. Used to prevent the scrubber eating vital white-space. */
138 #ifdef LEX_AT
139 const char extra_symbol_chars[] = "*%-(@";
140 #else
141 const char extra_symbol_chars[] = "*%-(";
142 #endif
143
144 /* This array holds the chars that always start a comment. If the
145 pre-processor is disabled, these aren't very useful */
146 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD))
147 /* Putting '/' here makes it impossible to use the divide operator.
148 However, we need it for compatibility with SVR4 systems. */
149 const char comment_chars[] = "#/";
150 #define PREFIX_SEPARATOR '\\'
151 #else
152 const char comment_chars[] = "#";
153 #define PREFIX_SEPARATOR '/'
154 #endif
155
156 /* This array holds the chars that only start a comment at the beginning of
157 a line. If the line seems to have the form '# 123 filename'
158 .line and .file directives will appear in the pre-processed output */
159 /* Note that input_file.c hand checks for '#' at the beginning of the
160 first line of the input file. This is because the compiler outputs
161 #NO_APP at the beginning of its output. */
162 /* Also note that comments started like this one will always work if
163 '/' isn't otherwise defined. */
164 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD))
165 const char line_comment_chars[] = "";
166 #else
167 const char line_comment_chars[] = "/";
168 #endif
169
170 const char line_separator_chars[] = ";";
171
172 /* Chars that can be used to separate mant from exp in floating point nums */
173 const char EXP_CHARS[] = "eE";
174
175 /* Chars that mean this number is a floating point constant */
176 /* As in 0f12.456 */
177 /* or 0d1.2345e12 */
178 const char FLT_CHARS[] = "fFdDxX";
179
180 /* tables for lexical analysis */
181 static char mnemonic_chars[256];
182 static char register_chars[256];
183 static char operand_chars[256];
184 static char identifier_chars[256];
185 static char digit_chars[256];
186
187 /* lexical macros */
188 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
189 #define is_operand_char(x) (operand_chars[(unsigned char) x])
190 #define is_register_char(x) (register_chars[(unsigned char) x])
191 #define is_space_char(x) ((x) == ' ')
192 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
193 #define is_digit_char(x) (digit_chars[(unsigned char) x])
194
195 /* put here all non-digit non-letter charcters that may occur in an operand */
196 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
197
198 /* md_assemble() always leaves the strings it's passed unaltered. To
199 effect this we maintain a stack of saved characters that we've smashed
200 with '\0's (indicating end of strings for various sub-fields of the
201 assembler instruction). */
202 static char save_stack[32];
203 static char *save_stack_p; /* stack pointer */
204 #define END_STRING_AND_SAVE(s) \
205 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
206 #define RESTORE_END_STRING(s) \
207 do { *(s) = *--save_stack_p; } while (0)
208
209 /* The instruction we're assembling. */
210 static i386_insn i;
211
212 /* Possible templates for current insn. */
213 static const templates *current_templates;
214
215 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
216 static expressionS disp_expressions[2], im_expressions[2];
217
218 /* Current operand we are working on. */
219 static int this_operand;
220
221 /* 1 if we're writing 16-bit code,
222 0 if 32-bit. */
223 static int flag_16bit_code;
224
225 /* 1 for intel syntax,
226 0 if att syntax. */
227 static int intel_syntax = 0;
228
229 /* 1 if register prefix % not required. */
230 static int allow_naked_reg = 0;
231
232 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
233 leave, push, and pop instructions so that gcc has the same stack
234 frame as in 32 bit mode. */
235 static char stackop_size = '\0';
236
237 /* Non-zero to quieten some warnings. */
238 static int quiet_warnings = 0;
239
240 /* CPU name. */
241 static const char *cpu_arch_name = NULL;
242
243 /* CPU feature flags. */
244 static unsigned int cpu_arch_flags = 0;
245
246 /* Interface to relax_segment.
247 There are 2 relax states for 386 jump insns: one for conditional &
248 one for unconditional jumps. This is because these two types of
249 jumps add different sizes to frags when we're figuring out what
250 sort of jump to choose to reach a given label. */
251
252 /* Types. */
253 #define COND_JUMP 1 /* Conditional jump. */
254 #define UNCOND_JUMP 2 /* Unconditional jump. */
255 /* Sizes. */
256 #define CODE16 1
257 #define SMALL 0
258 #define SMALL16 (SMALL|CODE16)
259 #define BIG 2
260 #define BIG16 (BIG|CODE16)
261
262 #ifndef INLINE
263 #ifdef __GNUC__
264 #define INLINE __inline__
265 #else
266 #define INLINE
267 #endif
268 #endif
269
270 #define ENCODE_RELAX_STATE(type,size) \
271 ((relax_substateT)((type<<2) | (size)))
272 #define SIZE_FROM_RELAX_STATE(s) \
273 ( (((s) & 0x3) == BIG ? 4 : (((s) & 0x3) == BIG16 ? 2 : 1)) )
274
275 /* This table is used by relax_frag to promote short jumps to long
276 ones where necessary. SMALL (short) jumps may be promoted to BIG
277 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
278 don't allow a short jump in a 32 bit code segment to be promoted to
279 a 16 bit offset jump because it's slower (requires data size
280 prefix), and doesn't work, unless the destination is in the bottom
281 64k of the code segment (The top 16 bits of eip are zeroed). */
282
283 const relax_typeS md_relax_table[] =
284 {
285 /* The fields are:
286 1) most positive reach of this state,
287 2) most negative reach of this state,
288 3) how many bytes this mode will add to the size of the current frag
289 4) which index into the table to try if we can't fit into this one.
290 */
291 {1, 1, 0, 0},
292 {1, 1, 0, 0},
293 {1, 1, 0, 0},
294 {1, 1, 0, 0},
295
296 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
297 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
298 /* dword conditionals adds 4 bytes to frag:
299 1 extra opcode byte, 3 extra displacement bytes. */
300 {0, 0, 4, 0},
301 /* word conditionals add 2 bytes to frag:
302 1 extra opcode byte, 1 extra displacement byte. */
303 {0, 0, 2, 0},
304
305 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
306 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
307 /* dword jmp adds 3 bytes to frag:
308 0 extra opcode bytes, 3 extra displacement bytes. */
309 {0, 0, 3, 0},
310 /* word jmp adds 1 byte to frag:
311 0 extra opcode bytes, 1 extra displacement byte. */
312 {0, 0, 1, 0}
313
314 };
315
316 static const arch_entry cpu_arch[] = {
317 {"i8086", Cpu086 },
318 {"i186", Cpu086|Cpu186 },
319 {"i286", Cpu086|Cpu186|Cpu286 },
320 {"i386", Cpu086|Cpu186|Cpu286|Cpu386 },
321 {"i486", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486 },
322 {"i586", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuMMX },
323 {"i686", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuSSE },
324 {"pentium", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuMMX },
325 {"pentiumpro",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuSSE },
326 {"k6", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuMMX|Cpu3dnow },
327 {"athlon", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|Cpu3dnow },
328 {NULL, 0 }
329 };
330
331 void
332 i386_align_code (fragP, count)
333 fragS *fragP;
334 int count;
335 {
336 /* Various efficient no-op patterns for aligning code labels. */
337 /* Note: Don't try to assemble the instructions in the comments. */
338 /* 0L and 0w are not legal */
339 static const char f32_1[] =
340 {0x90}; /* nop */
341 static const char f32_2[] =
342 {0x89,0xf6}; /* movl %esi,%esi */
343 static const char f32_3[] =
344 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
345 static const char f32_4[] =
346 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
347 static const char f32_5[] =
348 {0x90, /* nop */
349 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
350 static const char f32_6[] =
351 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
352 static const char f32_7[] =
353 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
354 static const char f32_8[] =
355 {0x90, /* nop */
356 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
357 static const char f32_9[] =
358 {0x89,0xf6, /* movl %esi,%esi */
359 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
360 static const char f32_10[] =
361 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
362 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
363 static const char f32_11[] =
364 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
365 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
366 static const char f32_12[] =
367 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
368 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
369 static const char f32_13[] =
370 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
371 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
372 static const char f32_14[] =
373 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
374 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
375 static const char f32_15[] =
376 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
377 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
378 static const char f16_3[] =
379 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
380 static const char f16_4[] =
381 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
382 static const char f16_5[] =
383 {0x90, /* nop */
384 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
385 static const char f16_6[] =
386 {0x89,0xf6, /* mov %si,%si */
387 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
388 static const char f16_7[] =
389 {0x8d,0x74,0x00, /* lea 0(%si),%si */
390 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
391 static const char f16_8[] =
392 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
393 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
394 static const char *const f32_patt[] = {
395 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
396 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
397 };
398 static const char *const f16_patt[] = {
399 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
400 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
401 };
402
403 if (count > 0 && count <= 15)
404 {
405 if (flag_16bit_code)
406 {
407 memcpy (fragP->fr_literal + fragP->fr_fix,
408 f16_patt[count - 1], count);
409 if (count > 8)
410 /* Adjust jump offset. */
411 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
412 }
413 else
414 memcpy (fragP->fr_literal + fragP->fr_fix,
415 f32_patt[count - 1], count);
416 fragP->fr_var = count;
417 }
418 }
419
420 static char *output_invalid PARAMS ((int c));
421 static int i386_operand PARAMS ((char *operand_string));
422 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
423 static const reg_entry *parse_register PARAMS ((char *reg_string,
424 char **end_op));
425
426 #ifndef I386COFF
427 static void s_bss PARAMS ((int));
428 #endif
429
430 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
431
432 static INLINE unsigned int
433 mode_from_disp_size (t)
434 unsigned int t;
435 {
436 return (t & Disp8) ? 1 : (t & (Disp16 | Disp32)) ? 2 : 0;
437 }
438
439 static INLINE int
440 fits_in_signed_byte (num)
441 offsetT num;
442 {
443 return (num >= -128) && (num <= 127);
444 }
445
446 static INLINE int
447 fits_in_unsigned_byte (num)
448 offsetT num;
449 {
450 return (num & 0xff) == num;
451 }
452
453 static INLINE int
454 fits_in_unsigned_word (num)
455 offsetT num;
456 {
457 return (num & 0xffff) == num;
458 }
459
460 static INLINE int
461 fits_in_signed_word (num)
462 offsetT num;
463 {
464 return (-32768 <= num) && (num <= 32767);
465 }
466
467 static int
468 smallest_imm_type (num)
469 offsetT num;
470 {
471 if (cpu_arch_flags != 0
472 && cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486))
473 {
474 /* This code is disabled on the 486 because all the Imm1 forms
475 in the opcode table are slower on the i486. They're the
476 versions with the implicitly specified single-position
477 displacement, which has another syntax if you really want to
478 use that form. */
479 if (num == 1)
480 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
481 }
482 return (fits_in_signed_byte (num)
483 ? (Imm8S | Imm8 | Imm16 | Imm32)
484 : fits_in_unsigned_byte (num)
485 ? (Imm8 | Imm16 | Imm32)
486 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
487 ? (Imm16 | Imm32)
488 : (Imm32));
489 }
490
491 static offsetT
492 offset_in_range (val, size)
493 offsetT val;
494 int size;
495 {
496 addressT mask;
497
498 switch (size)
499 {
500 case 1: mask = ((addressT) 1 << 8) - 1; break;
501 case 2: mask = ((addressT) 1 << 16) - 1; break;
502 case 4: mask = ((addressT) 2 << 31) - 1; break;
503 default: abort ();
504 }
505
506 /* If BFD64, sign extend val. */
507 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
508 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
509
510 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
511 {
512 char buf1[40], buf2[40];
513
514 sprint_value (buf1, val);
515 sprint_value (buf2, val & mask);
516 as_warn (_("%s shortened to %s"), buf1, buf2);
517 }
518 return val & mask;
519 }
520
521 /* Returns 0 if attempting to add a prefix where one from the same
522 class already exists, 1 if non rep/repne added, 2 if rep/repne
523 added. */
524 static int
525 add_prefix (prefix)
526 unsigned int prefix;
527 {
528 int ret = 1;
529 int q;
530
531 switch (prefix)
532 {
533 default:
534 abort ();
535
536 case CS_PREFIX_OPCODE:
537 case DS_PREFIX_OPCODE:
538 case ES_PREFIX_OPCODE:
539 case FS_PREFIX_OPCODE:
540 case GS_PREFIX_OPCODE:
541 case SS_PREFIX_OPCODE:
542 q = SEG_PREFIX;
543 break;
544
545 case REPNE_PREFIX_OPCODE:
546 case REPE_PREFIX_OPCODE:
547 ret = 2;
548 /* fall thru */
549 case LOCK_PREFIX_OPCODE:
550 q = LOCKREP_PREFIX;
551 break;
552
553 case FWAIT_OPCODE:
554 q = WAIT_PREFIX;
555 break;
556
557 case ADDR_PREFIX_OPCODE:
558 q = ADDR_PREFIX;
559 break;
560
561 case DATA_PREFIX_OPCODE:
562 q = DATA_PREFIX;
563 break;
564 }
565
566 if (i.prefix[q])
567 {
568 as_bad (_("same type of prefix used twice"));
569 return 0;
570 }
571
572 i.prefixes += 1;
573 i.prefix[q] = prefix;
574 return ret;
575 }
576
577 static void
578 set_16bit_code_flag (new_16bit_code_flag)
579 int new_16bit_code_flag;
580 {
581 flag_16bit_code = new_16bit_code_flag;
582 stackop_size = '\0';
583 }
584
585 static void
586 set_16bit_gcc_code_flag (new_16bit_code_flag)
587 int new_16bit_code_flag;
588 {
589 flag_16bit_code = new_16bit_code_flag;
590 stackop_size = new_16bit_code_flag ? 'l' : '\0';
591 }
592
593 static void
594 set_intel_syntax (syntax_flag)
595 int syntax_flag;
596 {
597 /* Find out if register prefixing is specified. */
598 int ask_naked_reg = 0;
599
600 SKIP_WHITESPACE ();
601 if (! is_end_of_line[(unsigned char) *input_line_pointer])
602 {
603 char *string = input_line_pointer;
604 int e = get_symbol_end ();
605
606 if (strcmp (string, "prefix") == 0)
607 ask_naked_reg = 1;
608 else if (strcmp (string, "noprefix") == 0)
609 ask_naked_reg = -1;
610 else
611 as_bad (_("bad argument to syntax directive."));
612 *input_line_pointer = e;
613 }
614 demand_empty_rest_of_line ();
615
616 intel_syntax = syntax_flag;
617
618 if (ask_naked_reg == 0)
619 {
620 #ifdef BFD_ASSEMBLER
621 allow_naked_reg = (intel_syntax
622 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
623 #else
624 /* Conservative default. */
625 allow_naked_reg = 0;
626 #endif
627 }
628 else
629 allow_naked_reg = (ask_naked_reg < 0);
630 }
631
632 static void
633 set_cpu_arch (dummy)
634 int dummy ATTRIBUTE_UNUSED;
635 {
636 SKIP_WHITESPACE ();
637
638 if (! is_end_of_line[(unsigned char) *input_line_pointer])
639 {
640 char *string = input_line_pointer;
641 int e = get_symbol_end ();
642 int i;
643
644 for (i = 0; cpu_arch[i].name; i++)
645 {
646 if (strcmp (string, cpu_arch[i].name) == 0)
647 {
648 cpu_arch_name = cpu_arch[i].name;
649 cpu_arch_flags = cpu_arch[i].flags;
650 break;
651 }
652 }
653 if (!cpu_arch[i].name)
654 as_bad (_("no such architecture: `%s'"), string);
655
656 *input_line_pointer = e;
657 }
658 else
659 as_bad (_("missing cpu architecture"));
660
661 demand_empty_rest_of_line ();
662 }
663
664 const pseudo_typeS md_pseudo_table[] =
665 {
666 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
667 {"align", s_align_bytes, 0},
668 #else
669 {"align", s_align_ptwo, 0},
670 #endif
671 {"arch", set_cpu_arch, 0},
672 #ifndef I386COFF
673 {"bss", s_bss, 0},
674 #endif
675 {"ffloat", float_cons, 'f'},
676 {"dfloat", float_cons, 'd'},
677 {"tfloat", float_cons, 'x'},
678 {"value", cons, 2},
679 {"noopt", s_ignore, 0},
680 {"optim", s_ignore, 0},
681 {"code16gcc", set_16bit_gcc_code_flag, 1},
682 {"code16", set_16bit_code_flag, 1},
683 {"code32", set_16bit_code_flag, 0},
684 {"intel_syntax", set_intel_syntax, 1},
685 {"att_syntax", set_intel_syntax, 0},
686 {0, 0, 0}
687 };
688
689 /* For interface with expression (). */
690 extern char *input_line_pointer;
691
692 /* Hash table for instruction mnemonic lookup. */
693 static struct hash_control *op_hash;
694
695 /* Hash table for register lookup. */
696 static struct hash_control *reg_hash;
697 \f
698 void
699 md_begin ()
700 {
701 const char *hash_err;
702
703 /* Initialize op_hash hash table. */
704 op_hash = hash_new ();
705
706 {
707 register const template *optab;
708 register templates *core_optab;
709
710 /* Setup for loop. */
711 optab = i386_optab;
712 core_optab = (templates *) xmalloc (sizeof (templates));
713 core_optab->start = optab;
714
715 while (1)
716 {
717 ++optab;
718 if (optab->name == NULL
719 || strcmp (optab->name, (optab - 1)->name) != 0)
720 {
721 /* different name --> ship out current template list;
722 add to hash table; & begin anew. */
723 core_optab->end = optab;
724 hash_err = hash_insert (op_hash,
725 (optab - 1)->name,
726 (PTR) core_optab);
727 if (hash_err)
728 {
729 hash_error:
730 as_fatal (_("Internal Error: Can't hash %s: %s"),
731 (optab - 1)->name,
732 hash_err);
733 }
734 if (optab->name == NULL)
735 break;
736 core_optab = (templates *) xmalloc (sizeof (templates));
737 core_optab->start = optab;
738 }
739 }
740 }
741
742 /* Initialize reg_hash hash table. */
743 reg_hash = hash_new ();
744 {
745 register const reg_entry *regtab;
746
747 for (regtab = i386_regtab;
748 regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
749 regtab++)
750 {
751 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
752 if (hash_err)
753 goto hash_error;
754 }
755 }
756
757 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
758 {
759 register int c;
760 register char *p;
761
762 for (c = 0; c < 256; c++)
763 {
764 if (isdigit (c))
765 {
766 digit_chars[c] = c;
767 mnemonic_chars[c] = c;
768 register_chars[c] = c;
769 operand_chars[c] = c;
770 }
771 else if (islower (c))
772 {
773 mnemonic_chars[c] = c;
774 register_chars[c] = c;
775 operand_chars[c] = c;
776 }
777 else if (isupper (c))
778 {
779 mnemonic_chars[c] = tolower (c);
780 register_chars[c] = mnemonic_chars[c];
781 operand_chars[c] = c;
782 }
783
784 if (isalpha (c) || isdigit (c))
785 identifier_chars[c] = c;
786 else if (c >= 128)
787 {
788 identifier_chars[c] = c;
789 operand_chars[c] = c;
790 }
791 }
792
793 #ifdef LEX_AT
794 identifier_chars['@'] = '@';
795 #endif
796 digit_chars['-'] = '-';
797 identifier_chars['_'] = '_';
798 identifier_chars['.'] = '.';
799
800 for (p = operand_special_chars; *p != '\0'; p++)
801 operand_chars[(unsigned char) *p] = *p;
802 }
803
804 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
805 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
806 {
807 record_alignment (text_section, 2);
808 record_alignment (data_section, 2);
809 record_alignment (bss_section, 2);
810 }
811 #endif
812 }
813
814 void
815 i386_print_statistics (file)
816 FILE *file;
817 {
818 hash_print_statistics (file, "i386 opcode", op_hash);
819 hash_print_statistics (file, "i386 register", reg_hash);
820 }
821 \f
822 #ifdef DEBUG386
823
824 /* debugging routines for md_assemble */
825 static void pi PARAMS ((char *, i386_insn *));
826 static void pte PARAMS ((template *));
827 static void pt PARAMS ((unsigned int));
828 static void pe PARAMS ((expressionS *));
829 static void ps PARAMS ((symbolS *));
830
831 static void
832 pi (line, x)
833 char *line;
834 i386_insn *x;
835 {
836 register template *p;
837 int i;
838
839 fprintf (stdout, "%s: template ", line);
840 pte (&x->tm);
841 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x",
842 x->rm.mode, x->rm.reg, x->rm.regmem);
843 fprintf (stdout, " base %x index %x scale %x\n",
844 x->bi.base, x->bi.index, x->bi.scale);
845 for (i = 0; i < x->operands; i++)
846 {
847 fprintf (stdout, " #%d: ", i + 1);
848 pt (x->types[i]);
849 fprintf (stdout, "\n");
850 if (x->types[i]
851 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
852 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
853 if (x->types[i] & Imm)
854 pe (x->op[i].imms);
855 if (x->types[i] & Disp)
856 pe (x->op[i].disps);
857 }
858 }
859
860 static void
861 pte (t)
862 template *t;
863 {
864 int i;
865 fprintf (stdout, " %d operands ", t->operands);
866 fprintf (stdout, "opcode %x ", t->base_opcode);
867 if (t->extension_opcode != None)
868 fprintf (stdout, "ext %x ", t->extension_opcode);
869 if (t->opcode_modifier & D)
870 fprintf (stdout, "D");
871 if (t->opcode_modifier & W)
872 fprintf (stdout, "W");
873 fprintf (stdout, "\n");
874 for (i = 0; i < t->operands; i++)
875 {
876 fprintf (stdout, " #%d type ", i + 1);
877 pt (t->operand_types[i]);
878 fprintf (stdout, "\n");
879 }
880 }
881
882 static void
883 pe (e)
884 expressionS *e;
885 {
886 fprintf (stdout, " operation %d\n", e->X_op);
887 fprintf (stdout, " add_number %ld (%lx)\n",
888 (long) e->X_add_number, (long) e->X_add_number);
889 if (e->X_add_symbol)
890 {
891 fprintf (stdout, " add_symbol ");
892 ps (e->X_add_symbol);
893 fprintf (stdout, "\n");
894 }
895 if (e->X_op_symbol)
896 {
897 fprintf (stdout, " op_symbol ");
898 ps (e->X_op_symbol);
899 fprintf (stdout, "\n");
900 }
901 }
902
903 static void
904 ps (s)
905 symbolS *s;
906 {
907 fprintf (stdout, "%s type %s%s",
908 S_GET_NAME (s),
909 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
910 segment_name (S_GET_SEGMENT (s)));
911 }
912
913 struct type_name
914 {
915 unsigned int mask;
916 char *tname;
917 }
918
919 type_names[] =
920 {
921 { Reg8, "r8" },
922 { Reg16, "r16" },
923 { Reg32, "r32" },
924 { Imm8, "i8" },
925 { Imm8S, "i8s" },
926 { Imm16, "i16" },
927 { Imm32, "i32" },
928 { Imm1, "i1" },
929 { BaseIndex, "BaseIndex" },
930 { Disp8, "d8" },
931 { Disp16, "d16" },
932 { Disp32, "d32" },
933 { InOutPortReg, "InOutPortReg" },
934 { ShiftCount, "ShiftCount" },
935 { Control, "control reg" },
936 { Test, "test reg" },
937 { Debug, "debug reg" },
938 { FloatReg, "FReg" },
939 { FloatAcc, "FAcc" },
940 { SReg2, "SReg2" },
941 { SReg3, "SReg3" },
942 { Acc, "Acc" },
943 { JumpAbsolute, "Jump Absolute" },
944 { RegMMX, "rMMX" },
945 { RegXMM, "rXMM" },
946 { EsSeg, "es" },
947 { 0, "" }
948 };
949
950 static void
951 pt (t)
952 unsigned int t;
953 {
954 register struct type_name *ty;
955
956 if (t == Unknown)
957 {
958 fprintf (stdout, _("Unknown"));
959 }
960 else
961 {
962 for (ty = type_names; ty->mask; ty++)
963 if (t & ty->mask)
964 fprintf (stdout, "%s, ", ty->tname);
965 }
966 fflush (stdout);
967 }
968
969 #endif /* DEBUG386 */
970 \f
971 int
972 tc_i386_force_relocation (fixp)
973 struct fix *fixp;
974 {
975 #ifdef BFD_ASSEMBLER
976 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
977 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
978 return 1;
979 return 0;
980 #else
981 /* For COFF */
982 return fixp->fx_r_type == 7;
983 #endif
984 }
985
986 #ifdef BFD_ASSEMBLER
987 static bfd_reloc_code_real_type reloc
988 PARAMS ((int, int, bfd_reloc_code_real_type));
989
990 static bfd_reloc_code_real_type
991 reloc (size, pcrel, other)
992 int size;
993 int pcrel;
994 bfd_reloc_code_real_type other;
995 {
996 if (other != NO_RELOC)
997 return other;
998
999 if (pcrel)
1000 {
1001 switch (size)
1002 {
1003 case 1: return BFD_RELOC_8_PCREL;
1004 case 2: return BFD_RELOC_16_PCREL;
1005 case 4: return BFD_RELOC_32_PCREL;
1006 }
1007 as_bad (_("can not do %d byte pc-relative relocation"), size);
1008 }
1009 else
1010 {
1011 switch (size)
1012 {
1013 case 1: return BFD_RELOC_8;
1014 case 2: return BFD_RELOC_16;
1015 case 4: return BFD_RELOC_32;
1016 }
1017 as_bad (_("can not do %d byte relocation"), size);
1018 }
1019
1020 return BFD_RELOC_NONE;
1021 }
1022
1023 /* Here we decide which fixups can be adjusted to make them relative to
1024 the beginning of the section instead of the symbol. Basically we need
1025 to make sure that the dynamic relocations are done correctly, so in
1026 some cases we force the original symbol to be used. */
1027
1028 int
1029 tc_i386_fix_adjustable (fixP)
1030 fixS *fixP;
1031 {
1032 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1033 /* Prevent all adjustments to global symbols, or else dynamic
1034 linking will not work correctly. */
1035 if (S_IS_EXTERNAL (fixP->fx_addsy)
1036 || S_IS_WEAK (fixP->fx_addsy))
1037 return 0;
1038 #endif
1039 /* adjust_reloc_syms doesn't know about the GOT */
1040 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1041 || fixP->fx_r_type == BFD_RELOC_386_PLT32
1042 || fixP->fx_r_type == BFD_RELOC_386_GOT32
1043 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1044 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1045 return 0;
1046 return 1;
1047 }
1048 #else
1049 #define reloc(SIZE,PCREL,OTHER) 0
1050 #define BFD_RELOC_16 0
1051 #define BFD_RELOC_32 0
1052 #define BFD_RELOC_16_PCREL 0
1053 #define BFD_RELOC_32_PCREL 0
1054 #define BFD_RELOC_386_PLT32 0
1055 #define BFD_RELOC_386_GOT32 0
1056 #define BFD_RELOC_386_GOTOFF 0
1057 #endif
1058
1059 static int intel_float_operand PARAMS ((char *mnemonic));
1060
1061 static int
1062 intel_float_operand (mnemonic)
1063 char *mnemonic;
1064 {
1065 if (mnemonic[0] == 'f' && mnemonic[1] == 'i')
1066 return 2;
1067
1068 if (mnemonic[0] == 'f')
1069 return 1;
1070
1071 return 0;
1072 }
1073
1074 /* This is the guts of the machine-dependent assembler. LINE points to a
1075 machine dependent instruction. This function is supposed to emit
1076 the frags/bytes it assembles to. */
1077
1078 void
1079 md_assemble (line)
1080 char *line;
1081 {
1082 /* Points to template once we've found it. */
1083 const template *t;
1084
1085 /* Count the size of the instruction generated. */
1086 int insn_size = 0;
1087
1088 int j;
1089
1090 char mnemonic[MAX_MNEM_SIZE];
1091
1092 /* Initialize globals. */
1093 memset (&i, '\0', sizeof (i));
1094 for (j = 0; j < MAX_OPERANDS; j++)
1095 i.disp_reloc[j] = NO_RELOC;
1096 memset (disp_expressions, '\0', sizeof (disp_expressions));
1097 memset (im_expressions, '\0', sizeof (im_expressions));
1098 save_stack_p = save_stack; /* reset stack pointer */
1099
1100 /* First parse an instruction mnemonic & call i386_operand for the operands.
1101 We assume that the scrubber has arranged it so that line[0] is the valid
1102 start of a (possibly prefixed) mnemonic. */
1103 {
1104 char *l = line;
1105 char *token_start = l;
1106 char *mnem_p;
1107
1108 /* Non-zero if we found a prefix only acceptable with string insns. */
1109 const char *expecting_string_instruction = NULL;
1110
1111 while (1)
1112 {
1113 mnem_p = mnemonic;
1114 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1115 {
1116 mnem_p++;
1117 if (mnem_p >= mnemonic + sizeof (mnemonic))
1118 {
1119 as_bad (_("no such instruction: `%s'"), token_start);
1120 return;
1121 }
1122 l++;
1123 }
1124 if (!is_space_char (*l)
1125 && *l != END_OF_INSN
1126 && *l != PREFIX_SEPARATOR)
1127 {
1128 as_bad (_("invalid character %s in mnemonic"),
1129 output_invalid (*l));
1130 return;
1131 }
1132 if (token_start == l)
1133 {
1134 if (*l == PREFIX_SEPARATOR)
1135 as_bad (_("expecting prefix; got nothing"));
1136 else
1137 as_bad (_("expecting mnemonic; got nothing"));
1138 return;
1139 }
1140
1141 /* Look up instruction (or prefix) via hash table. */
1142 current_templates = hash_find (op_hash, mnemonic);
1143
1144 if (*l != END_OF_INSN
1145 && (! is_space_char (*l) || l[1] != END_OF_INSN)
1146 && current_templates
1147 && (current_templates->start->opcode_modifier & IsPrefix))
1148 {
1149 /* If we are in 16-bit mode, do not allow addr16 or data16.
1150 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1151 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1152 && (((current_templates->start->opcode_modifier & Size32) != 0)
1153 ^ flag_16bit_code))
1154 {
1155 as_bad (_("redundant %s prefix"),
1156 current_templates->start->name);
1157 return;
1158 }
1159 /* Add prefix, checking for repeated prefixes. */
1160 switch (add_prefix (current_templates->start->base_opcode))
1161 {
1162 case 0:
1163 return;
1164 case 2:
1165 expecting_string_instruction = current_templates->start->name;
1166 break;
1167 }
1168 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1169 token_start = ++l;
1170 }
1171 else
1172 break;
1173 }
1174
1175 if (!current_templates)
1176 {
1177 /* See if we can get a match by trimming off a suffix. */
1178 switch (mnem_p[-1])
1179 {
1180 case WORD_MNEM_SUFFIX:
1181 case BYTE_MNEM_SUFFIX:
1182 case SHORT_MNEM_SUFFIX:
1183 case LONG_MNEM_SUFFIX:
1184 i.suffix = mnem_p[-1];
1185 mnem_p[-1] = '\0';
1186 current_templates = hash_find (op_hash, mnemonic);
1187 break;
1188
1189 /* Intel Syntax */
1190 case DWORD_MNEM_SUFFIX:
1191 if (intel_syntax)
1192 {
1193 i.suffix = mnem_p[-1];
1194 mnem_p[-1] = '\0';
1195 current_templates = hash_find (op_hash, mnemonic);
1196 break;
1197 }
1198 }
1199 if (!current_templates)
1200 {
1201 as_bad (_("no such instruction: `%s'"), token_start);
1202 return;
1203 }
1204 }
1205
1206 /* Check if instruction is supported on specified architecture. */
1207 if (cpu_arch_flags != 0)
1208 {
1209 if (current_templates->start->cpu_flags & ~cpu_arch_flags)
1210 {
1211 as_warn (_("`%s' is not supported on `%s'"),
1212 current_templates->start->name, cpu_arch_name);
1213 }
1214 else if ((Cpu386 & ~cpu_arch_flags) && !flag_16bit_code)
1215 {
1216 as_warn (_("use .code16 to ensure correct addressing mode"));
1217 }
1218 }
1219
1220 /* check for rep/repne without a string instruction */
1221 if (expecting_string_instruction
1222 && !(current_templates->start->opcode_modifier & IsString))
1223 {
1224 as_bad (_("expecting string instruction after `%s'"),
1225 expecting_string_instruction);
1226 return;
1227 }
1228
1229 /* There may be operands to parse. */
1230 if (*l != END_OF_INSN)
1231 {
1232 /* parse operands */
1233
1234 /* 1 if operand is pending after ','. */
1235 unsigned int expecting_operand = 0;
1236
1237 /* Non-zero if operand parens not balanced. */
1238 unsigned int paren_not_balanced;
1239
1240 do
1241 {
1242 /* skip optional white space before operand */
1243 if (is_space_char (*l))
1244 ++l;
1245 if (!is_operand_char (*l) && *l != END_OF_INSN)
1246 {
1247 as_bad (_("invalid character %s before operand %d"),
1248 output_invalid (*l),
1249 i.operands + 1);
1250 return;
1251 }
1252 token_start = l; /* after white space */
1253 paren_not_balanced = 0;
1254 while (paren_not_balanced || *l != ',')
1255 {
1256 if (*l == END_OF_INSN)
1257 {
1258 if (paren_not_balanced)
1259 {
1260 if (!intel_syntax)
1261 as_bad (_("unbalanced parenthesis in operand %d."),
1262 i.operands + 1);
1263 else
1264 as_bad (_("unbalanced brackets in operand %d."),
1265 i.operands + 1);
1266 return;
1267 }
1268 else
1269 break; /* we are done */
1270 }
1271 else if (!is_operand_char (*l) && !is_space_char (*l))
1272 {
1273 as_bad (_("invalid character %s in operand %d"),
1274 output_invalid (*l),
1275 i.operands + 1);
1276 return;
1277 }
1278 if (!intel_syntax)
1279 {
1280 if (*l == '(')
1281 ++paren_not_balanced;
1282 if (*l == ')')
1283 --paren_not_balanced;
1284 }
1285 else
1286 {
1287 if (*l == '[')
1288 ++paren_not_balanced;
1289 if (*l == ']')
1290 --paren_not_balanced;
1291 }
1292 l++;
1293 }
1294 if (l != token_start)
1295 { /* Yes, we've read in another operand. */
1296 unsigned int operand_ok;
1297 this_operand = i.operands++;
1298 if (i.operands > MAX_OPERANDS)
1299 {
1300 as_bad (_("spurious operands; (%d operands/instruction max)"),
1301 MAX_OPERANDS);
1302 return;
1303 }
1304 /* Now parse operand adding info to 'i' as we go along. */
1305 END_STRING_AND_SAVE (l);
1306
1307 if (intel_syntax)
1308 operand_ok =
1309 i386_intel_operand (token_start,
1310 intel_float_operand (mnemonic));
1311 else
1312 operand_ok = i386_operand (token_start);
1313
1314 RESTORE_END_STRING (l); /* restore old contents */
1315 if (!operand_ok)
1316 return;
1317 }
1318 else
1319 {
1320 if (expecting_operand)
1321 {
1322 expecting_operand_after_comma:
1323 as_bad (_("expecting operand after ','; got nothing"));
1324 return;
1325 }
1326 if (*l == ',')
1327 {
1328 as_bad (_("expecting operand before ','; got nothing"));
1329 return;
1330 }
1331 }
1332
1333 /* now *l must be either ',' or END_OF_INSN */
1334 if (*l == ',')
1335 {
1336 if (*++l == END_OF_INSN)
1337 { /* just skip it, if it's \n complain */
1338 goto expecting_operand_after_comma;
1339 }
1340 expecting_operand = 1;
1341 }
1342 }
1343 while (*l != END_OF_INSN); /* until we get end of insn */
1344 }
1345 }
1346
1347 /* Now we've parsed the mnemonic into a set of templates, and have the
1348 operands at hand.
1349
1350 Next, we find a template that matches the given insn,
1351 making sure the overlap of the given operands types is consistent
1352 with the template operand types. */
1353
1354 #define MATCH(overlap, given, template) \
1355 ((overlap & ~JumpAbsolute) \
1356 && ((given) & (BaseIndex|JumpAbsolute)) == ((overlap) & (BaseIndex|JumpAbsolute)))
1357
1358 /* If given types r0 and r1 are registers they must be of the same type
1359 unless the expected operand type register overlap is null.
1360 Note that Acc in a template matches every size of reg. */
1361 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
1362 ( ((g0) & Reg) == 0 || ((g1) & Reg) == 0 || \
1363 ((g0) & Reg) == ((g1) & Reg) || \
1364 ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
1365
1366 {
1367 register unsigned int overlap0, overlap1;
1368 unsigned int overlap2;
1369 unsigned int found_reverse_match;
1370 int suffix_check;
1371
1372 /* All intel opcodes have reversed operands except for "bound" and
1373 "enter". We also don't reverse intersegment "jmp" and "call"
1374 instructions with 2 immediate operands so that the immediate segment
1375 precedes the offset, as it does when in AT&T mode. "enter" and the
1376 intersegment "jmp" and "call" instructions are the only ones that
1377 have two immediate operands. */
1378 if (intel_syntax && i.operands > 1
1379 && (strcmp (mnemonic, "bound") != 0)
1380 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1381 {
1382 union i386_op temp_op;
1383 unsigned int temp_type;
1384 int xchg1 = 0;
1385 int xchg2 = 0;
1386
1387 if (i.operands == 2)
1388 {
1389 xchg1 = 0;
1390 xchg2 = 1;
1391 }
1392 else if (i.operands == 3)
1393 {
1394 xchg1 = 0;
1395 xchg2 = 2;
1396 }
1397 temp_type = i.types[xchg2];
1398 i.types[xchg2] = i.types[xchg1];
1399 i.types[xchg1] = temp_type;
1400 temp_op = i.op[xchg2];
1401 i.op[xchg2] = i.op[xchg1];
1402 i.op[xchg1] = temp_op;
1403
1404 if (i.mem_operands == 2)
1405 {
1406 const seg_entry *temp_seg;
1407 temp_seg = i.seg[0];
1408 i.seg[0] = i.seg[1];
1409 i.seg[1] = temp_seg;
1410 }
1411 }
1412
1413 if (i.imm_operands)
1414 {
1415 /* Try to ensure constant immediates are represented in the smallest
1416 opcode possible. */
1417 char guess_suffix = 0;
1418 int op;
1419
1420 if (i.suffix)
1421 guess_suffix = i.suffix;
1422 else if (i.reg_operands)
1423 {
1424 /* Figure out a suffix from the last register operand specified.
1425 We can't do this properly yet, ie. excluding InOutPortReg,
1426 but the following works for instructions with immediates.
1427 In any case, we can't set i.suffix yet. */
1428 for (op = i.operands; --op >= 0;)
1429 if (i.types[op] & Reg)
1430 {
1431 if (i.types[op] & Reg8)
1432 guess_suffix = BYTE_MNEM_SUFFIX;
1433 else if (i.types[op] & Reg16)
1434 guess_suffix = WORD_MNEM_SUFFIX;
1435 break;
1436 }
1437 }
1438 else if (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0))
1439 guess_suffix = WORD_MNEM_SUFFIX;
1440
1441 for (op = i.operands; --op >= 0;)
1442 if ((i.types[op] & Imm)
1443 && i.op[op].imms->X_op == O_constant)
1444 {
1445 /* If a suffix is given, this operand may be shortened. */
1446 switch (guess_suffix)
1447 {
1448 case WORD_MNEM_SUFFIX:
1449 i.types[op] |= Imm16;
1450 break;
1451 case BYTE_MNEM_SUFFIX:
1452 i.types[op] |= Imm16 | Imm8 | Imm8S;
1453 break;
1454 }
1455
1456 /* If this operand is at most 16 bits, convert it to a
1457 signed 16 bit number before trying to see whether it will
1458 fit in an even smaller size. This allows a 16-bit operand
1459 such as $0xffe0 to be recognised as within Imm8S range. */
1460 if ((i.types[op] & Imm16)
1461 && (i.op[op].imms->X_add_number & ~(offsetT)0xffff) == 0)
1462 {
1463 i.op[op].imms->X_add_number =
1464 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
1465 }
1466 i.types[op] |= smallest_imm_type ((long) i.op[op].imms->X_add_number);
1467 }
1468 }
1469
1470 if (i.disp_operands)
1471 {
1472 /* Try to use the smallest displacement type too. */
1473 int op;
1474
1475 for (op = i.operands; --op >= 0;)
1476 if ((i.types[op] & Disp)
1477 && i.op[op].imms->X_op == O_constant)
1478 {
1479 offsetT disp = i.op[op].disps->X_add_number;
1480
1481 if (i.types[op] & Disp16)
1482 {
1483 /* We know this operand is at most 16 bits, so
1484 convert to a signed 16 bit number before trying
1485 to see whether it will fit in an even smaller
1486 size. */
1487
1488 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
1489 }
1490 if (fits_in_signed_byte (disp))
1491 i.types[op] |= Disp8;
1492 }
1493 }
1494
1495 overlap0 = 0;
1496 overlap1 = 0;
1497 overlap2 = 0;
1498 found_reverse_match = 0;
1499 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
1500 ? No_bSuf
1501 : (i.suffix == WORD_MNEM_SUFFIX
1502 ? No_wSuf
1503 : (i.suffix == SHORT_MNEM_SUFFIX
1504 ? No_sSuf
1505 : (i.suffix == LONG_MNEM_SUFFIX
1506 ? No_lSuf
1507 : (i.suffix == DWORD_MNEM_SUFFIX
1508 ? No_dSuf
1509 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX ? No_xSuf : 0))))));
1510
1511 for (t = current_templates->start;
1512 t < current_templates->end;
1513 t++)
1514 {
1515 /* Must have right number of operands. */
1516 if (i.operands != t->operands)
1517 continue;
1518
1519 /* Check the suffix, except for some instructions in intel mode. */
1520 if ((t->opcode_modifier & suffix_check)
1521 && !(intel_syntax
1522 && (t->opcode_modifier & IgnoreSize))
1523 && !(intel_syntax
1524 && t->base_opcode == 0xd9
1525 && (t->extension_opcode == 5 /* 0xd9,5 "fldcw" */
1526 || t->extension_opcode == 7))) /* 0xd9,7 "f{n}stcw" */
1527 continue;
1528
1529 else if (!t->operands)
1530 /* 0 operands always matches. */
1531 break;
1532
1533 overlap0 = i.types[0] & t->operand_types[0];
1534 switch (t->operands)
1535 {
1536 case 1:
1537 if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
1538 continue;
1539 break;
1540 case 2:
1541 case 3:
1542 overlap1 = i.types[1] & t->operand_types[1];
1543 if (!MATCH (overlap0, i.types[0], t->operand_types[0])
1544 || !MATCH (overlap1, i.types[1], t->operand_types[1])
1545 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
1546 t->operand_types[0],
1547 overlap1, i.types[1],
1548 t->operand_types[1]))
1549 {
1550 /* Check if other direction is valid ... */
1551 if ((t->opcode_modifier & (D|FloatD)) == 0)
1552 continue;
1553
1554 /* Try reversing direction of operands. */
1555 overlap0 = i.types[0] & t->operand_types[1];
1556 overlap1 = i.types[1] & t->operand_types[0];
1557 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
1558 || !MATCH (overlap1, i.types[1], t->operand_types[0])
1559 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
1560 t->operand_types[1],
1561 overlap1, i.types[1],
1562 t->operand_types[0]))
1563 {
1564 /* Does not match either direction. */
1565 continue;
1566 }
1567 /* found_reverse_match holds which of D or FloatDR
1568 we've found. */
1569 found_reverse_match = t->opcode_modifier & (D|FloatDR);
1570 break;
1571 }
1572 /* Found a forward 2 operand match here. */
1573 if (t->operands == 3)
1574 {
1575 /* Here we make use of the fact that there are no
1576 reverse match 3 operand instructions, and all 3
1577 operand instructions only need to be checked for
1578 register consistency between operands 2 and 3. */
1579 overlap2 = i.types[2] & t->operand_types[2];
1580 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
1581 || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
1582 t->operand_types[1],
1583 overlap2, i.types[2],
1584 t->operand_types[2]))
1585
1586 continue;
1587 }
1588 /* Found either forward/reverse 2 or 3 operand match here:
1589 slip through to break */
1590 }
1591 /* We've found a match; break out of loop. */
1592 break;
1593 } /* for (t = ... */
1594 if (t == current_templates->end)
1595 {
1596 /* We found no match. */
1597 as_bad (_("suffix or operands invalid for `%s'"),
1598 current_templates->start->name);
1599 return;
1600 }
1601
1602 if (!quiet_warnings)
1603 {
1604 if (!intel_syntax
1605 && ((i.types[0] & JumpAbsolute)
1606 != (t->operand_types[0] & JumpAbsolute)))
1607 {
1608 as_warn (_("indirect %s without `*'"), t->name);
1609 }
1610
1611 if ((t->opcode_modifier & (IsPrefix|IgnoreSize))
1612 == (IsPrefix|IgnoreSize))
1613 {
1614 /* Warn them that a data or address size prefix doesn't
1615 affect assembly of the next line of code. */
1616 as_warn (_("stand-alone `%s' prefix"), t->name);
1617 }
1618 }
1619
1620 /* Copy the template we found. */
1621 i.tm = *t;
1622 if (found_reverse_match)
1623 {
1624 /* If we found a reverse match we must alter the opcode
1625 direction bit. found_reverse_match holds bits to change
1626 (different for int & float insns). */
1627
1628 i.tm.base_opcode ^= found_reverse_match;
1629
1630 i.tm.operand_types[0] = t->operand_types[1];
1631 i.tm.operand_types[1] = t->operand_types[0];
1632 }
1633
1634 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1635 if (SYSV386_COMPAT
1636 && intel_syntax
1637 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1638 i.tm.base_opcode ^= FloatR;
1639
1640 if (i.tm.opcode_modifier & FWait)
1641 if (! add_prefix (FWAIT_OPCODE))
1642 return;
1643
1644 /* Check string instruction segment overrides */
1645 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1646 {
1647 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
1648 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
1649 {
1650 if (i.seg[0] != NULL && i.seg[0] != &es)
1651 {
1652 as_bad (_("`%s' operand %d must use `%%es' segment"),
1653 i.tm.name,
1654 mem_op + 1);
1655 return;
1656 }
1657 /* There's only ever one segment override allowed per instruction.
1658 This instruction possibly has a legal segment override on the
1659 second operand, so copy the segment to where non-string
1660 instructions store it, allowing common code. */
1661 i.seg[0] = i.seg[1];
1662 }
1663 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
1664 {
1665 if (i.seg[1] != NULL && i.seg[1] != &es)
1666 {
1667 as_bad (_("`%s' operand %d must use `%%es' segment"),
1668 i.tm.name,
1669 mem_op + 2);
1670 return;
1671 }
1672 }
1673 }
1674
1675 /* If matched instruction specifies an explicit instruction mnemonic
1676 suffix, use it. */
1677 if (i.tm.opcode_modifier & (Size16 | Size32))
1678 {
1679 if (i.tm.opcode_modifier & Size16)
1680 i.suffix = WORD_MNEM_SUFFIX;
1681 else
1682 i.suffix = LONG_MNEM_SUFFIX;
1683 }
1684 else if (i.reg_operands)
1685 {
1686 /* If there's no instruction mnemonic suffix we try to invent one
1687 based on register operands. */
1688 if (!i.suffix)
1689 {
1690 /* We take i.suffix from the last register operand specified,
1691 Destination register type is more significant than source
1692 register type. */
1693 int op;
1694 for (op = i.operands; --op >= 0;)
1695 if ((i.types[op] & Reg)
1696 && !(i.tm.operand_types[op] & InOutPortReg))
1697 {
1698 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
1699 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
1700 LONG_MNEM_SUFFIX);
1701 break;
1702 }
1703 }
1704 else if (i.suffix == BYTE_MNEM_SUFFIX)
1705 {
1706 int op;
1707 for (op = i.operands; --op >= 0;)
1708 {
1709 /* If this is an eight bit register, it's OK. If it's
1710 the 16 or 32 bit version of an eight bit register,
1711 we will just use the low portion, and that's OK too. */
1712 if (i.types[op] & Reg8)
1713 continue;
1714
1715 /* movzx and movsx should not generate this warning. */
1716 if (intel_syntax
1717 && (i.tm.base_opcode == 0xfb7
1718 || i.tm.base_opcode == 0xfb6
1719 || i.tm.base_opcode == 0xfbe
1720 || i.tm.base_opcode == 0xfbf))
1721 continue;
1722
1723 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4
1724 #if 0
1725 /* Check that the template allows eight bit regs
1726 This kills insns such as `orb $1,%edx', which
1727 maybe should be allowed. */
1728 && (i.tm.operand_types[op] & (Reg8|InOutPortReg))
1729 #endif
1730 )
1731 {
1732 #if REGISTER_WARNINGS
1733 if (!quiet_warnings
1734 && (i.tm.operand_types[op] & InOutPortReg) == 0)
1735 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1736 (i.op[op].regs - (i.types[op] & Reg16 ? 8 : 16))->reg_name,
1737 i.op[op].regs->reg_name,
1738 i.suffix);
1739 #endif
1740 continue;
1741 }
1742 /* Any other register is bad */
1743 if (i.types[op] & (Reg | RegMMX | RegXMM
1744 | SReg2 | SReg3
1745 | Control | Debug | Test
1746 | FloatReg | FloatAcc))
1747 {
1748 as_bad (_("`%%%s' not allowed with `%s%c'"),
1749 i.op[op].regs->reg_name,
1750 i.tm.name,
1751 i.suffix);
1752 return;
1753 }
1754 }
1755 }
1756 else if (i.suffix == LONG_MNEM_SUFFIX)
1757 {
1758 int op;
1759
1760 for (op = i.operands; --op >= 0;)
1761 /* Reject eight bit registers, except where the template
1762 requires them. (eg. movzb) */
1763 if ((i.types[op] & Reg8) != 0
1764 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
1765 {
1766 as_bad (_("`%%%s' not allowed with `%s%c'"),
1767 i.op[op].regs->reg_name,
1768 i.tm.name,
1769 i.suffix);
1770 return;
1771 }
1772 #if REGISTER_WARNINGS
1773 /* Warn if the e prefix on a general reg is missing. */
1774 else if (!quiet_warnings
1775 && (i.types[op] & Reg16) != 0
1776 && (i.tm.operand_types[op] & (Reg32|Acc)) != 0)
1777 {
1778 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1779 (i.op[op].regs + 8)->reg_name,
1780 i.op[op].regs->reg_name,
1781 i.suffix);
1782 }
1783 #endif
1784 }
1785 else if (i.suffix == WORD_MNEM_SUFFIX)
1786 {
1787 int op;
1788 for (op = i.operands; --op >= 0;)
1789 /* Reject eight bit registers, except where the template
1790 requires them. (eg. movzb) */
1791 if ((i.types[op] & Reg8) != 0
1792 && (i.tm.operand_types[op] & (Reg16|Reg32|Acc)) != 0)
1793 {
1794 as_bad (_("`%%%s' not allowed with `%s%c'"),
1795 i.op[op].regs->reg_name,
1796 i.tm.name,
1797 i.suffix);
1798 return;
1799 }
1800 #if REGISTER_WARNINGS
1801 /* Warn if the e prefix on a general reg is present. */
1802 else if (!quiet_warnings
1803 && (i.types[op] & Reg32) != 0
1804 && (i.tm.operand_types[op] & (Reg16|Acc)) != 0)
1805 {
1806 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1807 (i.op[op].regs - 8)->reg_name,
1808 i.op[op].regs->reg_name,
1809 i.suffix);
1810 }
1811 #endif
1812 }
1813 else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
1814 /* Do nothing if the instruction is going to ignore the prefix. */
1815 ;
1816 else
1817 abort ();
1818 }
1819 else if ((i.tm.opcode_modifier & DefaultSize) && !i.suffix)
1820 {
1821 i.suffix = stackop_size;
1822 }
1823
1824 /* Make still unresolved immediate matches conform to size of immediate
1825 given in i.suffix. Note: overlap2 cannot be an immediate! */
1826 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
1827 && overlap0 != Imm8 && overlap0 != Imm8S
1828 && overlap0 != Imm16 && overlap0 != Imm32)
1829 {
1830 if (i.suffix)
1831 {
1832 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
1833 (i.suffix == WORD_MNEM_SUFFIX ? Imm16 : Imm32));
1834 }
1835 else if (overlap0 == (Imm16 | Imm32))
1836 {
1837 overlap0 =
1838 (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32;
1839 }
1840 else
1841 {
1842 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
1843 return;
1844 }
1845 }
1846 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
1847 && overlap1 != Imm8 && overlap1 != Imm8S
1848 && overlap1 != Imm16 && overlap1 != Imm32)
1849 {
1850 if (i.suffix)
1851 {
1852 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
1853 (i.suffix == WORD_MNEM_SUFFIX ? Imm16 : Imm32));
1854 }
1855 else if (overlap1 == (Imm16 | Imm32))
1856 {
1857 overlap1 =
1858 (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32;
1859 }
1860 else
1861 {
1862 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
1863 return;
1864 }
1865 }
1866 assert ((overlap2 & Imm) == 0);
1867
1868 i.types[0] = overlap0;
1869 if (overlap0 & ImplicitRegister)
1870 i.reg_operands--;
1871 if (overlap0 & Imm1)
1872 i.imm_operands = 0; /* kludge for shift insns */
1873
1874 i.types[1] = overlap1;
1875 if (overlap1 & ImplicitRegister)
1876 i.reg_operands--;
1877
1878 i.types[2] = overlap2;
1879 if (overlap2 & ImplicitRegister)
1880 i.reg_operands--;
1881
1882 /* Finalize opcode. First, we change the opcode based on the operand
1883 size given by i.suffix: We need not change things for byte insns. */
1884
1885 if (!i.suffix && (i.tm.opcode_modifier & W))
1886 {
1887 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
1888 return;
1889 }
1890
1891 /* For movzx and movsx, need to check the register type */
1892 if (intel_syntax
1893 && (i.tm.base_opcode == 0xfb6 || i.tm.base_opcode == 0xfbe))
1894 if (i.suffix && i.suffix == BYTE_MNEM_SUFFIX)
1895 {
1896 unsigned int prefix = DATA_PREFIX_OPCODE;
1897
1898 if ((i.op[1].regs->reg_type & Reg16) != 0)
1899 if (!add_prefix (prefix))
1900 return;
1901 }
1902
1903 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
1904 {
1905 /* It's not a byte, select word/dword operation. */
1906 if (i.tm.opcode_modifier & W)
1907 {
1908 if (i.tm.opcode_modifier & ShortForm)
1909 i.tm.base_opcode |= 8;
1910 else
1911 i.tm.base_opcode |= 1;
1912 }
1913 /* Now select between word & dword operations via the operand
1914 size prefix, except for instructions that will ignore this
1915 prefix anyway. */
1916 if (((intel_syntax && (i.suffix == DWORD_MNEM_SUFFIX))
1917 || i.suffix == LONG_MNEM_SUFFIX) == flag_16bit_code
1918 && !(i.tm.opcode_modifier & IgnoreSize))
1919 {
1920 unsigned int prefix = DATA_PREFIX_OPCODE;
1921 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
1922 prefix = ADDR_PREFIX_OPCODE;
1923
1924 if (! add_prefix (prefix))
1925 return;
1926 }
1927 /* Size floating point instruction. */
1928 if (i.suffix == LONG_MNEM_SUFFIX
1929 || (intel_syntax && i.suffix == DWORD_MNEM_SUFFIX))
1930 {
1931 if (i.tm.opcode_modifier & FloatMF)
1932 i.tm.base_opcode ^= 4;
1933 }
1934 }
1935
1936 if (i.tm.opcode_modifier & ImmExt)
1937 {
1938 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1939 opcode suffix which is coded in the same place as an 8-bit
1940 immediate field would be. Here we fake an 8-bit immediate
1941 operand from the opcode suffix stored in tm.extension_opcode. */
1942
1943 expressionS *exp;
1944
1945 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1946
1947 exp = &im_expressions[i.imm_operands++];
1948 i.op[i.operands].imms = exp;
1949 i.types[i.operands++] = Imm8;
1950 exp->X_op = O_constant;
1951 exp->X_add_number = i.tm.extension_opcode;
1952 i.tm.extension_opcode = None;
1953 }
1954
1955 /* For insns with operands there are more diddles to do to the opcode. */
1956 if (i.operands)
1957 {
1958 /* Default segment register this instruction will use
1959 for memory accesses. 0 means unknown.
1960 This is only for optimizing out unnecessary segment overrides. */
1961 const seg_entry *default_seg = 0;
1962
1963 /* The imul $imm, %reg instruction is converted into
1964 imul $imm, %reg, %reg, and the clr %reg instruction
1965 is converted into xor %reg, %reg. */
1966 if (i.tm.opcode_modifier & regKludge)
1967 {
1968 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
1969 /* Pretend we saw the extra register operand. */
1970 assert (i.op[first_reg_op + 1].regs == 0);
1971 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
1972 i.types[first_reg_op + 1] = i.types[first_reg_op];
1973 i.reg_operands = 2;
1974 }
1975
1976 if (i.tm.opcode_modifier & ShortForm)
1977 {
1978 /* The register or float register operand is in operand 0 or 1. */
1979 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
1980 /* Register goes in low 3 bits of opcode. */
1981 i.tm.base_opcode |= i.op[op].regs->reg_num;
1982 if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1983 {
1984 /* Warn about some common errors, but press on regardless.
1985 The first case can be generated by gcc (<= 2.8.1). */
1986 if (i.operands == 2)
1987 {
1988 /* Reversed arguments on faddp, fsubp, etc. */
1989 as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
1990 i.op[1].regs->reg_name,
1991 i.op[0].regs->reg_name);
1992 }
1993 else
1994 {
1995 /* Extraneous `l' suffix on fp insn. */
1996 as_warn (_("translating to `%s %%%s'"), i.tm.name,
1997 i.op[0].regs->reg_name);
1998 }
1999 }
2000 }
2001 else if (i.tm.opcode_modifier & Modrm)
2002 {
2003 /* The opcode is completed (modulo i.tm.extension_opcode which
2004 must be put into the modrm byte).
2005 Now, we make the modrm & index base bytes based on all the
2006 info we've collected. */
2007
2008 /* i.reg_operands MUST be the number of real register operands;
2009 implicit registers do not count. */
2010 if (i.reg_operands == 2)
2011 {
2012 unsigned int source, dest;
2013 source = ((i.types[0]
2014 & (Reg | RegMMX | RegXMM
2015 | SReg2 | SReg3
2016 | Control | Debug | Test))
2017 ? 0 : 1);
2018 dest = source + 1;
2019
2020 i.rm.mode = 3;
2021 /* One of the register operands will be encoded in the
2022 i.tm.reg field, the other in the combined i.tm.mode
2023 and i.tm.regmem fields. If no form of this
2024 instruction supports a memory destination operand,
2025 then we assume the source operand may sometimes be
2026 a memory operand and so we need to store the
2027 destination in the i.rm.reg field. */
2028 if ((i.tm.operand_types[dest] & AnyMem) == 0)
2029 {
2030 i.rm.reg = i.op[dest].regs->reg_num;
2031 i.rm.regmem = i.op[source].regs->reg_num;
2032 }
2033 else
2034 {
2035 i.rm.reg = i.op[source].regs->reg_num;
2036 i.rm.regmem = i.op[dest].regs->reg_num;
2037 }
2038 }
2039 else
2040 { /* If it's not 2 reg operands... */
2041 if (i.mem_operands)
2042 {
2043 unsigned int fake_zero_displacement = 0;
2044 unsigned int op = ((i.types[0] & AnyMem)
2045 ? 0
2046 : (i.types[1] & AnyMem) ? 1 : 2);
2047
2048 default_seg = &ds;
2049
2050 if (! i.base_reg)
2051 {
2052 i.rm.mode = 0;
2053 if (! i.disp_operands)
2054 fake_zero_displacement = 1;
2055 if (! i.index_reg)
2056 {
2057 /* Operand is just <disp> */
2058 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0))
2059 {
2060 i.rm.regmem = NO_BASE_REGISTER_16;
2061 i.types[op] &= ~Disp;
2062 i.types[op] |= Disp16;
2063 }
2064 else
2065 {
2066 i.rm.regmem = NO_BASE_REGISTER;
2067 i.types[op] &= ~Disp;
2068 i.types[op] |= Disp32;
2069 }
2070 }
2071 else /* ! i.base_reg && i.index_reg */
2072 {
2073 i.sib.index = i.index_reg->reg_num;
2074 i.sib.base = NO_BASE_REGISTER;
2075 i.sib.scale = i.log2_scale_factor;
2076 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2077 i.types[op] &= ~Disp;
2078 i.types[op] |= Disp32; /* Must be 32 bit. */
2079 }
2080 }
2081 else if (i.base_reg->reg_type & Reg16)
2082 {
2083 switch (i.base_reg->reg_num)
2084 {
2085 case 3: /* (%bx) */
2086 if (! i.index_reg)
2087 i.rm.regmem = 7;
2088 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
2089 i.rm.regmem = i.index_reg->reg_num - 6;
2090 break;
2091 case 5: /* (%bp) */
2092 default_seg = &ss;
2093 if (! i.index_reg)
2094 {
2095 i.rm.regmem = 6;
2096 if ((i.types[op] & Disp) == 0)
2097 {
2098 /* fake (%bp) into 0(%bp) */
2099 i.types[op] |= Disp8;
2100 fake_zero_displacement = 1;
2101 }
2102 }
2103 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
2104 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
2105 break;
2106 default: /* (%si) -> 4 or (%di) -> 5 */
2107 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
2108 }
2109 i.rm.mode = mode_from_disp_size (i.types[op]);
2110 }
2111 else /* i.base_reg and 32 bit mode */
2112 {
2113 i.rm.regmem = i.base_reg->reg_num;
2114 i.sib.base = i.base_reg->reg_num;
2115 if (i.base_reg->reg_num == EBP_REG_NUM)
2116 {
2117 default_seg = &ss;
2118 if (i.disp_operands == 0)
2119 {
2120 fake_zero_displacement = 1;
2121 i.types[op] |= Disp8;
2122 }
2123 }
2124 else if (i.base_reg->reg_num == ESP_REG_NUM)
2125 {
2126 default_seg = &ss;
2127 }
2128 i.sib.scale = i.log2_scale_factor;
2129 if (! i.index_reg)
2130 {
2131 /* <disp>(%esp) becomes two byte modrm
2132 with no index register. We've already
2133 stored the code for esp in i.rm.regmem
2134 ie. ESCAPE_TO_TWO_BYTE_ADDRESSING. Any
2135 base register besides %esp will not use
2136 the extra modrm byte. */
2137 i.sib.index = NO_INDEX_REGISTER;
2138 #if ! SCALE1_WHEN_NO_INDEX
2139 /* Another case where we force the second
2140 modrm byte. */
2141 if (i.log2_scale_factor)
2142 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2143 #endif
2144 }
2145 else
2146 {
2147 i.sib.index = i.index_reg->reg_num;
2148 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2149 }
2150 i.rm.mode = mode_from_disp_size (i.types[op]);
2151 }
2152
2153 if (fake_zero_displacement)
2154 {
2155 /* Fakes a zero displacement assuming that i.types[op]
2156 holds the correct displacement size. */
2157 expressionS *exp;
2158
2159 assert (i.op[op].disps == 0);
2160 exp = &disp_expressions[i.disp_operands++];
2161 i.op[op].disps = exp;
2162 exp->X_op = O_constant;
2163 exp->X_add_number = 0;
2164 exp->X_add_symbol = (symbolS *) 0;
2165 exp->X_op_symbol = (symbolS *) 0;
2166 }
2167 }
2168
2169 /* Fill in i.rm.reg or i.rm.regmem field with register
2170 operand (if any) based on i.tm.extension_opcode.
2171 Again, we must be careful to make sure that
2172 segment/control/debug/test/MMX registers are coded
2173 into the i.rm.reg field. */
2174 if (i.reg_operands)
2175 {
2176 unsigned int op =
2177 ((i.types[0]
2178 & (Reg | RegMMX | RegXMM
2179 | SReg2 | SReg3
2180 | Control | Debug | Test))
2181 ? 0
2182 : ((i.types[1]
2183 & (Reg | RegMMX | RegXMM
2184 | SReg2 | SReg3
2185 | Control | Debug | Test))
2186 ? 1
2187 : 2));
2188 /* If there is an extension opcode to put here, the
2189 register number must be put into the regmem field. */
2190 if (i.tm.extension_opcode != None)
2191 i.rm.regmem = i.op[op].regs->reg_num;
2192 else
2193 i.rm.reg = i.op[op].regs->reg_num;
2194
2195 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
2196 we must set it to 3 to indicate this is a register
2197 operand in the regmem field. */
2198 if (!i.mem_operands)
2199 i.rm.mode = 3;
2200 }
2201
2202 /* Fill in i.rm.reg field with extension opcode (if any). */
2203 if (i.tm.extension_opcode != None)
2204 i.rm.reg = i.tm.extension_opcode;
2205 }
2206 }
2207 else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2208 {
2209 if (i.tm.base_opcode == POP_SEG_SHORT
2210 && i.op[0].regs->reg_num == 1)
2211 {
2212 as_bad (_("you can't `pop %%cs'"));
2213 return;
2214 }
2215 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2216 }
2217 else if ((i.tm.base_opcode & ~(D|W)) == MOV_AX_DISP32)
2218 {
2219 default_seg = &ds;
2220 }
2221 else if ((i.tm.opcode_modifier & IsString) != 0)
2222 {
2223 /* For the string instructions that allow a segment override
2224 on one of their operands, the default segment is ds. */
2225 default_seg = &ds;
2226 }
2227
2228 /* If a segment was explicitly specified,
2229 and the specified segment is not the default,
2230 use an opcode prefix to select it.
2231 If we never figured out what the default segment is,
2232 then default_seg will be zero at this point,
2233 and the specified segment prefix will always be used. */
2234 if ((i.seg[0]) && (i.seg[0] != default_seg))
2235 {
2236 if (! add_prefix (i.seg[0]->seg_prefix))
2237 return;
2238 }
2239 }
2240 else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
2241 {
2242 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
2243 as_warn (_("translating to `%sp'"), i.tm.name);
2244 }
2245 }
2246
2247 /* Handle conversion of 'int $3' --> special int3 insn. */
2248 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
2249 {
2250 i.tm.base_opcode = INT3_OPCODE;
2251 i.imm_operands = 0;
2252 }
2253
2254 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
2255 && i.op[0].disps->X_op == O_constant)
2256 {
2257 /* Convert "jmp constant" (and "call constant") to a jump (call) to
2258 the absolute address given by the constant. Since ix86 jumps and
2259 calls are pc relative, we need to generate a reloc. */
2260 i.op[0].disps->X_add_symbol = &abs_symbol;
2261 i.op[0].disps->X_op = O_symbol;
2262 }
2263
2264 /* We are ready to output the insn. */
2265 {
2266 register char *p;
2267
2268 /* Output jumps. */
2269 if (i.tm.opcode_modifier & Jump)
2270 {
2271 int size;
2272 int code16;
2273 int prefix;
2274
2275 code16 = 0;
2276 if (flag_16bit_code)
2277 code16 = CODE16;
2278
2279 prefix = 0;
2280 if (i.prefix[DATA_PREFIX])
2281 {
2282 prefix = 1;
2283 i.prefixes -= 1;
2284 code16 ^= CODE16;
2285 }
2286
2287 size = 4;
2288 if (code16)
2289 size = 2;
2290
2291 if (i.prefixes != 0 && !intel_syntax)
2292 as_warn (_("skipping prefixes on this instruction"));
2293
2294 /* It's always a symbol; End frag & setup for relax.
2295 Make sure there is enough room in this frag for the largest
2296 instruction we may generate in md_convert_frag. This is 2
2297 bytes for the opcode and room for the prefix and largest
2298 displacement. */
2299 frag_grow (prefix + 2 + size);
2300 insn_size += prefix + 1;
2301 /* Prefix and 1 opcode byte go in fr_fix. */
2302 p = frag_more (prefix + 1);
2303 if (prefix)
2304 *p++ = DATA_PREFIX_OPCODE;
2305 *p = i.tm.base_opcode;
2306 /* 1 possible extra opcode + displacement go in var part.
2307 Pass reloc in fr_var. */
2308 frag_var (rs_machine_dependent,
2309 1 + size,
2310 i.disp_reloc[0],
2311 ((unsigned char) *p == JUMP_PC_RELATIVE
2312 ? ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL) | code16
2313 : ENCODE_RELAX_STATE (COND_JUMP, SMALL) | code16),
2314 i.op[0].disps->X_add_symbol,
2315 i.op[0].disps->X_add_number,
2316 p);
2317 }
2318 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
2319 {
2320 int size;
2321
2322 if (i.tm.opcode_modifier & JumpByte)
2323 {
2324 /* This is a loop or jecxz type instruction. */
2325 size = 1;
2326 if (i.prefix[ADDR_PREFIX])
2327 {
2328 insn_size += 1;
2329 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
2330 i.prefixes -= 1;
2331 }
2332 }
2333 else
2334 {
2335 int code16;
2336
2337 code16 = 0;
2338 if (flag_16bit_code)
2339 code16 = CODE16;
2340
2341 if (i.prefix[DATA_PREFIX])
2342 {
2343 insn_size += 1;
2344 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
2345 i.prefixes -= 1;
2346 code16 ^= CODE16;
2347 }
2348
2349 size = 4;
2350 if (code16)
2351 size = 2;
2352 }
2353
2354 if (i.prefixes != 0 && !intel_syntax)
2355 as_warn (_("skipping prefixes on this instruction"));
2356
2357 if (fits_in_unsigned_byte (i.tm.base_opcode))
2358 {
2359 insn_size += 1 + size;
2360 p = frag_more (1 + size);
2361 }
2362 else
2363 {
2364 /* Opcode can be at most two bytes. */
2365 insn_size += 2 + size;
2366 p = frag_more (2 + size);
2367 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2368 }
2369 *p++ = i.tm.base_opcode & 0xff;
2370
2371 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2372 i.op[0].disps, 1, reloc (size, 1, i.disp_reloc[0]));
2373 }
2374 else if (i.tm.opcode_modifier & JumpInterSegment)
2375 {
2376 int size;
2377 int prefix;
2378 int code16;
2379
2380 code16 = 0;
2381 if (flag_16bit_code)
2382 code16 = CODE16;
2383
2384 prefix = 0;
2385 if (i.prefix[DATA_PREFIX])
2386 {
2387 prefix = 1;
2388 i.prefixes -= 1;
2389 code16 ^= CODE16;
2390 }
2391
2392 size = 4;
2393 if (code16)
2394 size = 2;
2395
2396 if (i.prefixes != 0 && !intel_syntax)
2397 as_warn (_("skipping prefixes on this instruction"));
2398
2399 /* 1 opcode; 2 segment; offset */
2400 insn_size += prefix + 1 + 2 + size;
2401 p = frag_more (prefix + 1 + 2 + size);
2402 if (prefix)
2403 *p++ = DATA_PREFIX_OPCODE;
2404 *p++ = i.tm.base_opcode;
2405 if (i.op[1].imms->X_op == O_constant)
2406 {
2407 offsetT n = i.op[1].imms->X_add_number;
2408
2409 if (size == 2
2410 && !fits_in_unsigned_word (n)
2411 && !fits_in_signed_word (n))
2412 {
2413 as_bad (_("16-bit jump out of range"));
2414 return;
2415 }
2416 md_number_to_chars (p, n, size);
2417 }
2418 else
2419 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2420 i.op[1].imms, 0, reloc (size, 0, i.disp_reloc[0]));
2421 if (i.op[0].imms->X_op != O_constant)
2422 as_bad (_("can't handle non absolute segment in `%s'"),
2423 i.tm.name);
2424 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
2425 }
2426 else
2427 {
2428 /* Output normal instructions here. */
2429 unsigned char *q;
2430
2431 /* The prefix bytes. */
2432 for (q = i.prefix;
2433 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
2434 q++)
2435 {
2436 if (*q)
2437 {
2438 insn_size += 1;
2439 p = frag_more (1);
2440 md_number_to_chars (p, (valueT) *q, 1);
2441 }
2442 }
2443
2444 /* Now the opcode; be careful about word order here! */
2445 if (fits_in_unsigned_byte (i.tm.base_opcode))
2446 {
2447 insn_size += 1;
2448 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
2449 }
2450 else if (fits_in_unsigned_word (i.tm.base_opcode))
2451 {
2452 insn_size += 2;
2453 p = frag_more (2);
2454 /* Put out high byte first: can't use md_number_to_chars! */
2455 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2456 *p = i.tm.base_opcode & 0xff;
2457 }
2458 else
2459 { /* Opcode is either 3 or 4 bytes. */
2460 if (i.tm.base_opcode & 0xff000000)
2461 {
2462 insn_size += 4;
2463 p = frag_more (4);
2464 *p++ = (i.tm.base_opcode >> 24) & 0xff;
2465 }
2466 else
2467 {
2468 insn_size += 3;
2469 p = frag_more (3);
2470 }
2471 *p++ = (i.tm.base_opcode >> 16) & 0xff;
2472 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2473 *p = (i.tm.base_opcode) & 0xff;
2474 }
2475
2476 /* Now the modrm byte and sib byte (if present). */
2477 if (i.tm.opcode_modifier & Modrm)
2478 {
2479 insn_size += 1;
2480 p = frag_more (1);
2481 md_number_to_chars (p,
2482 (valueT) (i.rm.regmem << 0
2483 | i.rm.reg << 3
2484 | i.rm.mode << 6),
2485 1);
2486 /* If i.rm.regmem == ESP (4)
2487 && i.rm.mode != (Register mode)
2488 && not 16 bit
2489 ==> need second modrm byte. */
2490 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
2491 && i.rm.mode != 3
2492 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
2493 {
2494 insn_size += 1;
2495 p = frag_more (1);
2496 md_number_to_chars (p,
2497 (valueT) (i.sib.base << 0
2498 | i.sib.index << 3
2499 | i.sib.scale << 6),
2500 1);
2501 }
2502 }
2503
2504 if (i.disp_operands)
2505 {
2506 register unsigned int n;
2507
2508 for (n = 0; n < i.operands; n++)
2509 {
2510 if (i.types[n] & Disp)
2511 {
2512 if (i.op[n].disps->X_op == O_constant)
2513 {
2514 int size;
2515 offsetT val;
2516
2517 size = 4;
2518 if (i.types[n] & (Disp8 | Disp16))
2519 {
2520 size = 2;
2521 if (i.types[n] & Disp8)
2522 size = 1;
2523 }
2524 val = offset_in_range (i.op[n].disps->X_add_number,
2525 size);
2526 insn_size += size;
2527 p = frag_more (size);
2528 md_number_to_chars (p, val, size);
2529 }
2530 else
2531 {
2532 int size = 4;
2533
2534 if (i.types[n] & Disp16)
2535 size = 2;
2536
2537 insn_size += size;
2538 p = frag_more (size);
2539 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2540 i.op[n].disps, 0,
2541 reloc (size, 0, i.disp_reloc[n]));
2542 }
2543 }
2544 }
2545 } /* End displacement output. */
2546
2547 /* Output immediate. */
2548 if (i.imm_operands)
2549 {
2550 register unsigned int n;
2551
2552 for (n = 0; n < i.operands; n++)
2553 {
2554 if (i.types[n] & Imm)
2555 {
2556 if (i.op[n].imms->X_op == O_constant)
2557 {
2558 int size;
2559 offsetT val;
2560
2561 size = 4;
2562 if (i.types[n] & (Imm8 | Imm8S | Imm16))
2563 {
2564 size = 2;
2565 if (i.types[n] & (Imm8 | Imm8S))
2566 size = 1;
2567 }
2568 val = offset_in_range (i.op[n].imms->X_add_number,
2569 size);
2570 insn_size += size;
2571 p = frag_more (size);
2572 md_number_to_chars (p, val, size);
2573 }
2574 else
2575 { /* Not absolute_section. */
2576 /* Need a 32-bit fixup (don't support 8bit
2577 non-absolute imms). Try to support other
2578 sizes ... */
2579 #ifdef BFD_ASSEMBLER
2580 enum bfd_reloc_code_real reloc_type;
2581 #else
2582 int reloc_type;
2583 #endif
2584 int size = 4;
2585
2586 if (i.types[n] & Imm16)
2587 size = 2;
2588 else if (i.types[n] & (Imm8 | Imm8S))
2589 size = 1;
2590
2591 insn_size += size;
2592 p = frag_more (size);
2593 reloc_type = reloc (size, 0, i.disp_reloc[0]);
2594 #ifdef BFD_ASSEMBLER
2595 if (reloc_type == BFD_RELOC_32
2596 && GOT_symbol
2597 && GOT_symbol == i.op[n].imms->X_add_symbol
2598 && (i.op[n].imms->X_op == O_symbol
2599 || (i.op[n].imms->X_op == O_add
2600 && ((symbol_get_value_expression
2601 (i.op[n].imms->X_op_symbol)->X_op)
2602 == O_subtract))))
2603 {
2604 reloc_type = BFD_RELOC_386_GOTPC;
2605 i.op[n].imms->X_add_number += 3;
2606 }
2607 #endif
2608 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2609 i.op[n].imms, 0, reloc_type);
2610 }
2611 }
2612 }
2613 } /* end immediate output */
2614 }
2615
2616 #ifdef DEBUG386
2617 if (flag_debug)
2618 {
2619 pi (line, &i);
2620 }
2621 #endif /* DEBUG386 */
2622 }
2623 }
2624 \f
2625 static int i386_immediate PARAMS ((char *));
2626
2627 static int
2628 i386_immediate (imm_start)
2629 char *imm_start;
2630 {
2631 char *save_input_line_pointer;
2632 segT exp_seg = 0;
2633 expressionS *exp;
2634
2635 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
2636 {
2637 as_bad (_("only 1 or 2 immediate operands are allowed"));
2638 return 0;
2639 }
2640
2641 exp = &im_expressions[i.imm_operands++];
2642 i.op[this_operand].imms = exp;
2643
2644 if (is_space_char (*imm_start))
2645 ++imm_start;
2646
2647 save_input_line_pointer = input_line_pointer;
2648 input_line_pointer = imm_start;
2649
2650 #ifndef LEX_AT
2651 {
2652 /* We can have operands of the form
2653 <symbol>@GOTOFF+<nnn>
2654 Take the easy way out here and copy everything
2655 into a temporary buffer... */
2656 register char *cp;
2657
2658 cp = strchr (input_line_pointer, '@');
2659 if (cp != NULL)
2660 {
2661 char *tmpbuf;
2662 int len = 0;
2663 int first;
2664
2665 /* GOT relocations are not supported in 16 bit mode. */
2666 if (flag_16bit_code)
2667 as_bad (_("GOT relocations not supported in 16 bit mode"));
2668
2669 if (GOT_symbol == NULL)
2670 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
2671
2672 if (strncmp (cp + 1, "PLT", 3) == 0)
2673 {
2674 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2675 len = 3;
2676 }
2677 else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
2678 {
2679 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2680 len = 6;
2681 }
2682 else if (strncmp (cp + 1, "GOT", 3) == 0)
2683 {
2684 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2685 len = 3;
2686 }
2687 else
2688 as_bad (_("bad reloc specifier in expression"));
2689
2690 /* Replace the relocation token with ' ', so that errors like
2691 foo@GOTOFF1 will be detected. */
2692 first = cp - input_line_pointer;
2693 tmpbuf = (char *) alloca (strlen (input_line_pointer));
2694 memcpy (tmpbuf, input_line_pointer, first);
2695 tmpbuf[first] = ' ';
2696 strcpy (tmpbuf + first + 1, cp + 1 + len);
2697 input_line_pointer = tmpbuf;
2698 }
2699 }
2700 #endif
2701
2702 exp_seg = expression (exp);
2703
2704 SKIP_WHITESPACE ();
2705 if (*input_line_pointer)
2706 as_bad (_("ignoring junk `%s' after expression"), input_line_pointer);
2707
2708 input_line_pointer = save_input_line_pointer;
2709
2710 if (exp->X_op == O_absent || exp->X_op == O_big)
2711 {
2712 /* Missing or bad expr becomes absolute 0. */
2713 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
2714 imm_start);
2715 exp->X_op = O_constant;
2716 exp->X_add_number = 0;
2717 exp->X_add_symbol = (symbolS *) 0;
2718 exp->X_op_symbol = (symbolS *) 0;
2719 }
2720
2721 if (exp->X_op == O_constant)
2722 {
2723 /* Size it properly later. */
2724 i.types[this_operand] |= Imm32;
2725 }
2726 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
2727 else if (1
2728 #ifdef BFD_ASSEMBLER
2729 && OUTPUT_FLAVOR == bfd_target_aout_flavour
2730 #endif
2731 && exp_seg != text_section
2732 && exp_seg != data_section
2733 && exp_seg != bss_section
2734 && exp_seg != undefined_section
2735 #ifdef BFD_ASSEMBLER
2736 && !bfd_is_com_section (exp_seg)
2737 #endif
2738 )
2739 {
2740 #ifdef BFD_ASSEMBLER
2741 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
2742 #else
2743 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
2744 #endif
2745 return 0;
2746 }
2747 #endif
2748 else
2749 {
2750 /* This is an address. The size of the address will be
2751 determined later, depending on destination register,
2752 suffix, or the default for the section. We exclude
2753 Imm8S here so that `push $foo' and other instructions
2754 with an Imm8S form will use Imm16 or Imm32. */
2755 i.types[this_operand] |= (Imm8 | Imm16 | Imm32);
2756 }
2757
2758 return 1;
2759 }
2760
2761 static int i386_scale PARAMS ((char *));
2762
2763 static int
2764 i386_scale (scale)
2765 char *scale;
2766 {
2767 if (!isdigit (*scale))
2768 goto bad_scale;
2769
2770 switch (*scale)
2771 {
2772 case '0':
2773 case '1':
2774 i.log2_scale_factor = 0;
2775 break;
2776 case '2':
2777 i.log2_scale_factor = 1;
2778 break;
2779 case '4':
2780 i.log2_scale_factor = 2;
2781 break;
2782 case '8':
2783 i.log2_scale_factor = 3;
2784 break;
2785 default:
2786 bad_scale:
2787 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
2788 scale);
2789 return 0;
2790 }
2791 if (i.log2_scale_factor != 0 && ! i.index_reg)
2792 {
2793 as_warn (_("scale factor of %d without an index register"),
2794 1 << i.log2_scale_factor);
2795 #if SCALE1_WHEN_NO_INDEX
2796 i.log2_scale_factor = 0;
2797 #endif
2798 }
2799 return 1;
2800 }
2801
2802 static int i386_displacement PARAMS ((char *, char *));
2803
2804 static int
2805 i386_displacement (disp_start, disp_end)
2806 char *disp_start;
2807 char *disp_end;
2808 {
2809 register expressionS *exp;
2810 segT exp_seg = 0;
2811 char *save_input_line_pointer;
2812 int bigdisp = Disp32;
2813
2814 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0))
2815 bigdisp = Disp16;
2816 i.types[this_operand] |= bigdisp;
2817
2818 exp = &disp_expressions[i.disp_operands];
2819 i.op[this_operand].disps = exp;
2820 i.disp_operands++;
2821 save_input_line_pointer = input_line_pointer;
2822 input_line_pointer = disp_start;
2823 END_STRING_AND_SAVE (disp_end);
2824
2825 #ifndef GCC_ASM_O_HACK
2826 #define GCC_ASM_O_HACK 0
2827 #endif
2828 #if GCC_ASM_O_HACK
2829 END_STRING_AND_SAVE (disp_end + 1);
2830 if ((i.types[this_operand] & BaseIndex) != 0
2831 && displacement_string_end[-1] == '+')
2832 {
2833 /* This hack is to avoid a warning when using the "o"
2834 constraint within gcc asm statements.
2835 For instance:
2836
2837 #define _set_tssldt_desc(n,addr,limit,type) \
2838 __asm__ __volatile__ ( \
2839 "movw %w2,%0\n\t" \
2840 "movw %w1,2+%0\n\t" \
2841 "rorl $16,%1\n\t" \
2842 "movb %b1,4+%0\n\t" \
2843 "movb %4,5+%0\n\t" \
2844 "movb $0,6+%0\n\t" \
2845 "movb %h1,7+%0\n\t" \
2846 "rorl $16,%1" \
2847 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
2848
2849 This works great except that the output assembler ends
2850 up looking a bit weird if it turns out that there is
2851 no offset. You end up producing code that looks like:
2852
2853 #APP
2854 movw $235,(%eax)
2855 movw %dx,2+(%eax)
2856 rorl $16,%edx
2857 movb %dl,4+(%eax)
2858 movb $137,5+(%eax)
2859 movb $0,6+(%eax)
2860 movb %dh,7+(%eax)
2861 rorl $16,%edx
2862 #NO_APP
2863
2864 So here we provide the missing zero. */
2865
2866 *displacement_string_end = '0';
2867 }
2868 #endif
2869 #ifndef LEX_AT
2870 {
2871 /* We can have operands of the form
2872 <symbol>@GOTOFF+<nnn>
2873 Take the easy way out here and copy everything
2874 into a temporary buffer... */
2875 register char *cp;
2876
2877 cp = strchr (input_line_pointer, '@');
2878 if (cp != NULL)
2879 {
2880 char *tmpbuf;
2881 int len = 0;
2882 int first;
2883
2884 /* GOT relocations are not supported in 16 bit mode. */
2885 if (flag_16bit_code)
2886 as_bad (_("GOT relocations not supported in 16 bit mode"));
2887
2888 if (GOT_symbol == NULL)
2889 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
2890
2891 if (strncmp (cp + 1, "PLT", 3) == 0)
2892 {
2893 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2894 len = 3;
2895 }
2896 else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
2897 {
2898 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2899 len = 6;
2900 }
2901 else if (strncmp (cp + 1, "GOT", 3) == 0)
2902 {
2903 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2904 len = 3;
2905 }
2906 else
2907 as_bad (_("bad reloc specifier in expression"));
2908
2909 /* Replace the relocation token with ' ', so that errors like
2910 foo@GOTOFF1 will be detected. */
2911 first = cp - input_line_pointer;
2912 tmpbuf = (char *) alloca (strlen (input_line_pointer));
2913 memcpy (tmpbuf, input_line_pointer, first);
2914 tmpbuf[first] = ' ';
2915 strcpy (tmpbuf + first + 1, cp + 1 + len);
2916 input_line_pointer = tmpbuf;
2917 }
2918 }
2919 #endif
2920
2921 exp_seg = expression (exp);
2922
2923 #ifdef BFD_ASSEMBLER
2924 /* We do this to make sure that the section symbol is in
2925 the symbol table. We will ultimately change the relocation
2926 to be relative to the beginning of the section. */
2927 if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
2928 {
2929 if (S_IS_LOCAL(exp->X_add_symbol)
2930 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
2931 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
2932 assert (exp->X_op == O_symbol);
2933 exp->X_op = O_subtract;
2934 exp->X_op_symbol = GOT_symbol;
2935 i.disp_reloc[this_operand] = BFD_RELOC_32;
2936 }
2937 #endif
2938
2939 SKIP_WHITESPACE ();
2940 if (*input_line_pointer)
2941 as_bad (_("ignoring junk `%s' after expression"),
2942 input_line_pointer);
2943 #if GCC_ASM_O_HACK
2944 RESTORE_END_STRING (disp_end + 1);
2945 #endif
2946 RESTORE_END_STRING (disp_end);
2947 input_line_pointer = save_input_line_pointer;
2948
2949 if (exp->X_op == O_absent || exp->X_op == O_big)
2950 {
2951 /* Missing or bad expr becomes absolute 0. */
2952 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
2953 disp_start);
2954 exp->X_op = O_constant;
2955 exp->X_add_number = 0;
2956 exp->X_add_symbol = (symbolS *) 0;
2957 exp->X_op_symbol = (symbolS *) 0;
2958 }
2959
2960 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
2961 if (exp->X_op != O_constant
2962 #ifdef BFD_ASSEMBLER
2963 && OUTPUT_FLAVOR == bfd_target_aout_flavour
2964 #endif
2965 && exp_seg != text_section
2966 && exp_seg != data_section
2967 && exp_seg != bss_section
2968 && exp_seg != undefined_section)
2969 {
2970 #ifdef BFD_ASSEMBLER
2971 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
2972 #else
2973 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
2974 #endif
2975 return 0;
2976 }
2977 #endif
2978 return 1;
2979 }
2980
2981 static int i386_operand_modifier PARAMS ((char **, int));
2982
2983 static int
2984 i386_operand_modifier (op_string, got_a_float)
2985 char **op_string;
2986 int got_a_float;
2987 {
2988 if (!strncasecmp (*op_string, "BYTE PTR", 8))
2989 {
2990 i.suffix = BYTE_MNEM_SUFFIX;
2991 *op_string += 8;
2992 return BYTE_PTR;
2993
2994 }
2995 else if (!strncasecmp (*op_string, "WORD PTR", 8))
2996 {
2997 if (got_a_float == 2) /* "fi..." */
2998 i.suffix = SHORT_MNEM_SUFFIX;
2999 else
3000 i.suffix = WORD_MNEM_SUFFIX;
3001 *op_string += 8;
3002 return WORD_PTR;
3003 }
3004
3005 else if (!strncasecmp (*op_string, "DWORD PTR", 9))
3006 {
3007 if (got_a_float == 1) /* "f..." */
3008 i.suffix = SHORT_MNEM_SUFFIX;
3009 else
3010 i.suffix = LONG_MNEM_SUFFIX;
3011 *op_string += 9;
3012 return DWORD_PTR;
3013 }
3014
3015 else if (!strncasecmp (*op_string, "QWORD PTR", 9))
3016 {
3017 i.suffix = DWORD_MNEM_SUFFIX;
3018 *op_string += 9;
3019 return QWORD_PTR;
3020 }
3021
3022 else if (!strncasecmp (*op_string, "XWORD PTR", 9))
3023 {
3024 i.suffix = LONG_DOUBLE_MNEM_SUFFIX;
3025 *op_string += 9;
3026 return XWORD_PTR;
3027 }
3028
3029 else if (!strncasecmp (*op_string, "SHORT", 5))
3030 {
3031 *op_string += 5;
3032 return SHORT;
3033 }
3034
3035 else if (!strncasecmp (*op_string, "OFFSET FLAT:", 12))
3036 {
3037 *op_string += 12;
3038 return OFFSET_FLAT;
3039 }
3040
3041 else if (!strncasecmp (*op_string, "FLAT", 4))
3042 {
3043 *op_string += 4;
3044 return FLAT;
3045 }
3046
3047 else return NONE_FOUND;
3048 }
3049
3050 static char * build_displacement_string PARAMS ((int, char *));
3051
3052 static char *
3053 build_displacement_string (initial_disp, op_string)
3054 int initial_disp;
3055 char *op_string;
3056 {
3057 char *temp_string = (char *) malloc (strlen (op_string) + 1);
3058 char *end_of_operand_string;
3059 char *tc;
3060 char *temp_disp;
3061
3062 temp_string[0] = '\0';
3063 tc = end_of_operand_string = strchr (op_string, '[');
3064 if (initial_disp && !end_of_operand_string)
3065 {
3066 strcpy (temp_string, op_string);
3067 return temp_string;
3068 }
3069
3070 /* Build the whole displacement string. */
3071 if (initial_disp)
3072 {
3073 strncpy (temp_string, op_string, end_of_operand_string - op_string);
3074 temp_string[end_of_operand_string - op_string] = '\0';
3075 temp_disp = tc;
3076 }
3077 else
3078 temp_disp = op_string;
3079
3080 while (*temp_disp != '\0')
3081 {
3082 char *end_op;
3083 int add_minus = (*temp_disp == '-');
3084
3085 if (*temp_disp == '+' || *temp_disp == '-' || *temp_disp == '[')
3086 temp_disp++;
3087
3088 if (is_space_char (*temp_disp))
3089 temp_disp++;
3090
3091 /* Don't consider registers. */
3092 if ( !((*temp_disp == REGISTER_PREFIX || allow_naked_reg)
3093 && parse_register (temp_disp, &end_op)) )
3094 {
3095 char *string_start = temp_disp;
3096
3097 while (*temp_disp != ']'
3098 && *temp_disp != '+'
3099 && *temp_disp != '-'
3100 && *temp_disp != '*')
3101 ++temp_disp;
3102
3103 if (add_minus)
3104 strcat (temp_string, "-");
3105 else
3106 strcat (temp_string, "+");
3107
3108 strncat (temp_string, string_start, temp_disp - string_start);
3109 if (*temp_disp == '+' || *temp_disp == '-')
3110 --temp_disp;
3111 }
3112
3113 while (*temp_disp != '\0'
3114 && *temp_disp != '+'
3115 && *temp_disp != '-')
3116 ++temp_disp;
3117 }
3118
3119 return temp_string;
3120 }
3121
3122 static int i386_parse_seg PARAMS ((char *));
3123
3124 static int
3125 i386_parse_seg (op_string)
3126 char *op_string;
3127 {
3128 if (is_space_char (*op_string))
3129 ++op_string;
3130
3131 /* Should be one of es, cs, ss, ds fs or gs. */
3132 switch (*op_string++)
3133 {
3134 case 'e':
3135 i.seg[i.mem_operands] = &es;
3136 break;
3137 case 'c':
3138 i.seg[i.mem_operands] = &cs;
3139 break;
3140 case 's':
3141 i.seg[i.mem_operands] = &ss;
3142 break;
3143 case 'd':
3144 i.seg[i.mem_operands] = &ds;
3145 break;
3146 case 'f':
3147 i.seg[i.mem_operands] = &fs;
3148 break;
3149 case 'g':
3150 i.seg[i.mem_operands] = &gs;
3151 break;
3152 default:
3153 as_bad (_("bad segment name `%s'"), op_string);
3154 return 0;
3155 }
3156
3157 if (*op_string++ != 's')
3158 {
3159 as_bad (_("bad segment name `%s'"), op_string);
3160 return 0;
3161 }
3162
3163 if (is_space_char (*op_string))
3164 ++op_string;
3165
3166 if (*op_string != ':')
3167 {
3168 as_bad (_("bad segment name `%s'"), op_string);
3169 return 0;
3170 }
3171
3172 return 1;
3173
3174 }
3175
3176 static int i386_index_check PARAMS((const char *));
3177
3178 /* Make sure the memory operand we've been dealt is valid.
3179 Return 1 on success, 0 on a failure. */
3180
3181 static int
3182 i386_index_check (operand_string)
3183 const char *operand_string;
3184 {
3185 #if INFER_ADDR_PREFIX
3186 int fudged = 0;
3187
3188 tryprefix:
3189 #endif
3190 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0)
3191 /* 16 bit mode checks. */
3192 ? ((i.base_reg
3193 && ((i.base_reg->reg_type & (Reg16|BaseIndex))
3194 != (Reg16|BaseIndex)))
3195 || (i.index_reg
3196 && (((i.index_reg->reg_type & (Reg16|BaseIndex))
3197 != (Reg16|BaseIndex))
3198 || ! (i.base_reg
3199 && i.base_reg->reg_num < 6
3200 && i.index_reg->reg_num >= 6
3201 && i.log2_scale_factor == 0))))
3202 /* 32 bit mode checks. */
3203 : ((i.base_reg
3204 && (i.base_reg->reg_type & Reg32) == 0)
3205 || (i.index_reg
3206 && ((i.index_reg->reg_type & (Reg32|BaseIndex))
3207 != (Reg32|BaseIndex)))))
3208 {
3209 #if INFER_ADDR_PREFIX
3210 if (i.prefix[ADDR_PREFIX] == 0 && stackop_size != '\0')
3211 {
3212 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
3213 i.prefixes += 1;
3214 /* Change the size of any displacement too. At most one of
3215 Disp16 or Disp32 is set.
3216 FIXME. There doesn't seem to be any real need for separate
3217 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
3218 Removing them would probably clean up the code quite a lot. */
3219 if (i.types[this_operand] & (Disp16|Disp32))
3220 i.types[this_operand] ^= (Disp16|Disp32);
3221 fudged = 1;
3222 goto tryprefix;
3223 }
3224 if (fudged)
3225 as_bad (_("`%s' is not a valid base/index expression"),
3226 operand_string);
3227 else
3228 #endif
3229 as_bad (_("`%s' is not a valid %s bit base/index expression"),
3230 operand_string,
3231 flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0) ? "16" : "32");
3232 return 0;
3233 }
3234 return 1;
3235 }
3236
3237 static int i386_intel_memory_operand PARAMS ((char *));
3238
3239 static int
3240 i386_intel_memory_operand (operand_string)
3241 char *operand_string;
3242 {
3243 char *op_string = operand_string;
3244 char *end_of_operand_string;
3245
3246 if ((i.mem_operands == 1
3247 && (current_templates->start->opcode_modifier & IsString) == 0)
3248 || i.mem_operands == 2)
3249 {
3250 as_bad (_("too many memory references for `%s'"),
3251 current_templates->start->name);
3252 return 0;
3253 }
3254
3255 /* First check for a segment override. */
3256 if (*op_string != '[')
3257 {
3258 char *end_seg;
3259
3260 end_seg = strchr (op_string, ':');
3261 if (end_seg)
3262 {
3263 if (!i386_parse_seg (op_string))
3264 return 0;
3265 op_string = end_seg + 1;
3266 }
3267 }
3268
3269 /* Look for displacement preceding open bracket. */
3270 if (*op_string != '[')
3271 {
3272 char *temp_string;
3273
3274 if (i.disp_operands)
3275 return 0;
3276
3277 temp_string = build_displacement_string (true, op_string);
3278
3279 if (!i386_displacement (temp_string, temp_string + strlen (temp_string)))
3280 {
3281 free (temp_string);
3282 return 0;
3283 }
3284 free (temp_string);
3285
3286 end_of_operand_string = strchr (op_string, '[');
3287 if (!end_of_operand_string)
3288 end_of_operand_string = op_string + strlen (op_string);
3289
3290 if (is_space_char (*end_of_operand_string))
3291 --end_of_operand_string;
3292
3293 op_string = end_of_operand_string;
3294 }
3295
3296 if (*op_string == '[')
3297 {
3298 ++op_string;
3299
3300 /* Pick off each component and figure out where it belongs */
3301
3302 end_of_operand_string = op_string;
3303
3304 while (*op_string != ']')
3305 {
3306 const reg_entry *temp_reg;
3307 char *end_op;
3308 char *temp_string;
3309
3310 while (*end_of_operand_string != '+'
3311 && *end_of_operand_string != '-'
3312 && *end_of_operand_string != '*'
3313 && *end_of_operand_string != ']')
3314 end_of_operand_string++;
3315
3316 temp_string = op_string;
3317 if (*temp_string == '+')
3318 {
3319 ++temp_string;
3320 if (is_space_char (*temp_string))
3321 ++temp_string;
3322 }
3323
3324 if ((*temp_string == REGISTER_PREFIX || allow_naked_reg)
3325 && (temp_reg = parse_register (temp_string, &end_op)) != NULL)
3326 {
3327 if (i.base_reg == NULL)
3328 i.base_reg = temp_reg;
3329 else
3330 i.index_reg = temp_reg;
3331
3332 i.types[this_operand] |= BaseIndex;
3333 }
3334 else if (*temp_string == REGISTER_PREFIX)
3335 {
3336 as_bad (_("bad register name `%s'"), temp_string);
3337 return 0;
3338 }
3339 else if (is_digit_char (*op_string)
3340 || *op_string == '+' || *op_string == '-')
3341 {
3342 char *temp_str;
3343
3344 if (i.disp_operands != 0)
3345 return 0;
3346
3347 temp_string = build_displacement_string (false, op_string);
3348
3349 temp_str = temp_string;
3350 if (*temp_str == '+')
3351 ++temp_str;
3352
3353 if (!i386_displacement (temp_str, temp_str + strlen (temp_str)))
3354 {
3355 free (temp_string);
3356 return 0;
3357 }
3358 free (temp_string);
3359
3360 ++op_string;
3361 end_of_operand_string = op_string;
3362 while (*end_of_operand_string != ']'
3363 && *end_of_operand_string != '+'
3364 && *end_of_operand_string != '-'
3365 && *end_of_operand_string != '*')
3366 ++end_of_operand_string;
3367 }
3368 else if (*op_string == '*')
3369 {
3370 ++op_string;
3371
3372 if (i.base_reg && !i.index_reg)
3373 {
3374 i.index_reg = i.base_reg;
3375 i.base_reg = 0;
3376 }
3377
3378 if (!i386_scale (op_string))
3379 return 0;
3380 }
3381 op_string = end_of_operand_string;
3382 ++end_of_operand_string;
3383 }
3384 }
3385
3386 if (i386_index_check (operand_string) == 0)
3387 return 0;
3388
3389 i.mem_operands++;
3390 return 1;
3391 }
3392
3393 static int
3394 i386_intel_operand (operand_string, got_a_float)
3395 char *operand_string;
3396 int got_a_float;
3397 {
3398 const reg_entry *r;
3399 char *end_op;
3400 char *op_string = operand_string;
3401
3402 int operand_modifier = i386_operand_modifier (&op_string, got_a_float);
3403 if (is_space_char (*op_string))
3404 ++op_string;
3405
3406 switch (operand_modifier)
3407 {
3408 case BYTE_PTR:
3409 case WORD_PTR:
3410 case DWORD_PTR:
3411 case QWORD_PTR:
3412 case XWORD_PTR:
3413 if (!i386_intel_memory_operand (op_string))
3414 return 0;
3415 break;
3416
3417 case FLAT:
3418 case OFFSET_FLAT:
3419 if (!i386_immediate (op_string))
3420 return 0;
3421 break;
3422
3423 case SHORT:
3424 case NONE_FOUND:
3425 /* Should be register or immediate */
3426 if (is_digit_char (*op_string)
3427 && strchr (op_string, '[') == 0)
3428 {
3429 if (!i386_immediate (op_string))
3430 return 0;
3431 }
3432 else if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
3433 && (r = parse_register (op_string, &end_op)) != NULL)
3434 {
3435 /* Check for a segment override by searching for ':' after a
3436 segment register. */
3437 op_string = end_op;
3438 if (is_space_char (*op_string))
3439 ++op_string;
3440 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
3441 {
3442 switch (r->reg_num)
3443 {
3444 case 0:
3445 i.seg[i.mem_operands] = &es;
3446 break;
3447 case 1:
3448 i.seg[i.mem_operands] = &cs;
3449 break;
3450 case 2:
3451 i.seg[i.mem_operands] = &ss;
3452 break;
3453 case 3:
3454 i.seg[i.mem_operands] = &ds;
3455 break;
3456 case 4:
3457 i.seg[i.mem_operands] = &fs;
3458 break;
3459 case 5:
3460 i.seg[i.mem_operands] = &gs;
3461 break;
3462 }
3463
3464 }
3465 i.types[this_operand] |= r->reg_type & ~BaseIndex;
3466 i.op[this_operand].regs = r;
3467 i.reg_operands++;
3468 }
3469 else if (*op_string == REGISTER_PREFIX)
3470 {
3471 as_bad (_("bad register name `%s'"), op_string);
3472 return 0;
3473 }
3474 else if (!i386_intel_memory_operand (op_string))
3475 return 0;
3476
3477 break;
3478 } /* end switch */
3479
3480 return 1;
3481 }
3482
3483 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
3484 on error. */
3485
3486 static int
3487 i386_operand (operand_string)
3488 char *operand_string;
3489 {
3490 const reg_entry *r;
3491 char *end_op;
3492 char *op_string = operand_string;
3493
3494 if (is_space_char (*op_string))
3495 ++op_string;
3496
3497 /* We check for an absolute prefix (differentiating,
3498 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
3499 if (*op_string == ABSOLUTE_PREFIX)
3500 {
3501 ++op_string;
3502 if (is_space_char (*op_string))
3503 ++op_string;
3504 i.types[this_operand] |= JumpAbsolute;
3505 }
3506
3507 /* Check if operand is a register. */
3508 if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
3509 && (r = parse_register (op_string, &end_op)) != NULL)
3510 {
3511 /* Check for a segment override by searching for ':' after a
3512 segment register. */
3513 op_string = end_op;
3514 if (is_space_char (*op_string))
3515 ++op_string;
3516 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
3517 {
3518 switch (r->reg_num)
3519 {
3520 case 0:
3521 i.seg[i.mem_operands] = &es;
3522 break;
3523 case 1:
3524 i.seg[i.mem_operands] = &cs;
3525 break;
3526 case 2:
3527 i.seg[i.mem_operands] = &ss;
3528 break;
3529 case 3:
3530 i.seg[i.mem_operands] = &ds;
3531 break;
3532 case 4:
3533 i.seg[i.mem_operands] = &fs;
3534 break;
3535 case 5:
3536 i.seg[i.mem_operands] = &gs;
3537 break;
3538 }
3539
3540 /* Skip the ':' and whitespace. */
3541 ++op_string;
3542 if (is_space_char (*op_string))
3543 ++op_string;
3544
3545 if (!is_digit_char (*op_string)
3546 && !is_identifier_char (*op_string)
3547 && *op_string != '('
3548 && *op_string != ABSOLUTE_PREFIX)
3549 {
3550 as_bad (_("bad memory operand `%s'"), op_string);
3551 return 0;
3552 }
3553 /* Handle case of %es:*foo. */
3554 if (*op_string == ABSOLUTE_PREFIX)
3555 {
3556 ++op_string;
3557 if (is_space_char (*op_string))
3558 ++op_string;
3559 i.types[this_operand] |= JumpAbsolute;
3560 }
3561 goto do_memory_reference;
3562 }
3563 if (*op_string)
3564 {
3565 as_bad (_("junk `%s' after register"), op_string);
3566 return 0;
3567 }
3568 i.types[this_operand] |= r->reg_type & ~BaseIndex;
3569 i.op[this_operand].regs = r;
3570 i.reg_operands++;
3571 }
3572 else if (*op_string == REGISTER_PREFIX)
3573 {
3574 as_bad (_("bad register name `%s'"), op_string);
3575 return 0;
3576 }
3577 else if (*op_string == IMMEDIATE_PREFIX)
3578 { /* ... or an immediate */
3579 ++op_string;
3580 if (i.types[this_operand] & JumpAbsolute)
3581 {
3582 as_bad (_("immediate operand illegal with absolute jump"));
3583 return 0;
3584 }
3585 if (!i386_immediate (op_string))
3586 return 0;
3587 }
3588 else if (is_digit_char (*op_string)
3589 || is_identifier_char (*op_string)
3590 || *op_string == '(' )
3591 {
3592 /* This is a memory reference of some sort. */
3593 char *base_string;
3594
3595 /* Start and end of displacement string expression (if found). */
3596 char *displacement_string_start;
3597 char *displacement_string_end;
3598
3599 do_memory_reference:
3600 if ((i.mem_operands == 1
3601 && (current_templates->start->opcode_modifier & IsString) == 0)
3602 || i.mem_operands == 2)
3603 {
3604 as_bad (_("too many memory references for `%s'"),
3605 current_templates->start->name);
3606 return 0;
3607 }
3608
3609 /* Check for base index form. We detect the base index form by
3610 looking for an ')' at the end of the operand, searching
3611 for the '(' matching it, and finding a REGISTER_PREFIX or ','
3612 after the '('. */
3613 base_string = op_string + strlen (op_string);
3614
3615 --base_string;
3616 if (is_space_char (*base_string))
3617 --base_string;
3618
3619 /* If we only have a displacement, set-up for it to be parsed later. */
3620 displacement_string_start = op_string;
3621 displacement_string_end = base_string + 1;
3622
3623 if (*base_string == ')')
3624 {
3625 char *temp_string;
3626 unsigned int parens_balanced = 1;
3627 /* We've already checked that the number of left & right ()'s are
3628 equal, so this loop will not be infinite. */
3629 do
3630 {
3631 base_string--;
3632 if (*base_string == ')')
3633 parens_balanced++;
3634 if (*base_string == '(')
3635 parens_balanced--;
3636 }
3637 while (parens_balanced);
3638
3639 temp_string = base_string;
3640
3641 /* Skip past '(' and whitespace. */
3642 ++base_string;
3643 if (is_space_char (*base_string))
3644 ++base_string;
3645
3646 if (*base_string == ','
3647 || ((*base_string == REGISTER_PREFIX || allow_naked_reg)
3648 && (i.base_reg = parse_register (base_string, &end_op)) != NULL))
3649 {
3650 displacement_string_end = temp_string;
3651
3652 i.types[this_operand] |= BaseIndex;
3653
3654 if (i.base_reg)
3655 {
3656 base_string = end_op;
3657 if (is_space_char (*base_string))
3658 ++base_string;
3659 }
3660
3661 /* There may be an index reg or scale factor here. */
3662 if (*base_string == ',')
3663 {
3664 ++base_string;
3665 if (is_space_char (*base_string))
3666 ++base_string;
3667
3668 if ((*base_string == REGISTER_PREFIX || allow_naked_reg)
3669 && (i.index_reg = parse_register (base_string, &end_op)) != NULL)
3670 {
3671 base_string = end_op;
3672 if (is_space_char (*base_string))
3673 ++base_string;
3674 if (*base_string == ',')
3675 {
3676 ++base_string;
3677 if (is_space_char (*base_string))
3678 ++base_string;
3679 }
3680 else if (*base_string != ')' )
3681 {
3682 as_bad (_("expecting `,' or `)' after index register in `%s'"),
3683 operand_string);
3684 return 0;
3685 }
3686 }
3687 else if (*base_string == REGISTER_PREFIX)
3688 {
3689 as_bad (_("bad register name `%s'"), base_string);
3690 return 0;
3691 }
3692
3693 /* Check for scale factor. */
3694 if (isdigit ((unsigned char) *base_string))
3695 {
3696 if (!i386_scale (base_string))
3697 return 0;
3698
3699 ++base_string;
3700 if (is_space_char (*base_string))
3701 ++base_string;
3702 if (*base_string != ')')
3703 {
3704 as_bad (_("expecting `)' after scale factor in `%s'"),
3705 operand_string);
3706 return 0;
3707 }
3708 }
3709 else if (!i.index_reg)
3710 {
3711 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
3712 *base_string);
3713 return 0;
3714 }
3715 }
3716 else if (*base_string != ')')
3717 {
3718 as_bad (_("expecting `,' or `)' after base register in `%s'"),
3719 operand_string);
3720 return 0;
3721 }
3722 }
3723 else if (*base_string == REGISTER_PREFIX)
3724 {
3725 as_bad (_("bad register name `%s'"), base_string);
3726 return 0;
3727 }
3728 }
3729
3730 /* If there's an expression beginning the operand, parse it,
3731 assuming displacement_string_start and
3732 displacement_string_end are meaningful. */
3733 if (displacement_string_start != displacement_string_end)
3734 {
3735 if (!i386_displacement (displacement_string_start,
3736 displacement_string_end))
3737 return 0;
3738 }
3739
3740 /* Special case for (%dx) while doing input/output op. */
3741 if (i.base_reg
3742 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
3743 && i.index_reg == 0
3744 && i.log2_scale_factor == 0
3745 && i.seg[i.mem_operands] == 0
3746 && (i.types[this_operand] & Disp) == 0)
3747 {
3748 i.types[this_operand] = InOutPortReg;
3749 return 1;
3750 }
3751
3752 if (i386_index_check (operand_string) == 0)
3753 return 0;
3754 i.mem_operands++;
3755 }
3756 else
3757 { /* It's not a memory operand; argh! */
3758 as_bad (_("invalid char %s beginning operand %d `%s'"),
3759 output_invalid (*op_string),
3760 this_operand + 1,
3761 op_string);
3762 return 0;
3763 }
3764 return 1; /* Normal return. */
3765 }
3766 \f
3767 /* md_estimate_size_before_relax()
3768
3769 Called just before relax() for rs_machine_dependent frags. The x86
3770 assembler uses these frags to handle variable size jump
3771 instructions.
3772
3773 Any symbol that is now undefined will not become defined.
3774 Return the correct fr_subtype in the frag.
3775 Return the initial "guess for variable size of frag" to caller.
3776 The guess is actually the growth beyond the fixed part. Whatever
3777 we do to grow the fixed or variable part contributes to our
3778 returned value. */
3779
3780 int
3781 md_estimate_size_before_relax (fragP, segment)
3782 register fragS *fragP;
3783 register segT segment;
3784 {
3785 /* We've already got fragP->fr_subtype right; all we have to do is
3786 check for un-relaxable symbols. On an ELF system, we can't relax
3787 an externally visible symbol, because it may be overridden by a
3788 shared library. */
3789 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
3790 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3791 || S_IS_EXTERNAL (fragP->fr_symbol)
3792 || S_IS_WEAK (fragP->fr_symbol)
3793 #endif
3794 )
3795 {
3796 /* Symbol is undefined in this segment, or we need to keep a
3797 reloc so that weak symbols can be overridden. */
3798 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
3799 #ifdef BFD_ASSEMBLER
3800 enum bfd_reloc_code_real reloc_type;
3801 #else
3802 int reloc_type;
3803 #endif
3804 unsigned char *opcode;
3805 int old_fr_fix;
3806
3807 if (fragP->fr_var != NO_RELOC)
3808 reloc_type = fragP->fr_var;
3809 else if (size == 2)
3810 reloc_type = BFD_RELOC_16_PCREL;
3811 else
3812 reloc_type = BFD_RELOC_32_PCREL;
3813
3814 old_fr_fix = fragP->fr_fix;
3815 opcode = (unsigned char *) fragP->fr_opcode;
3816
3817 switch (opcode[0])
3818 {
3819 case JUMP_PC_RELATIVE:
3820 /* Make jmp (0xeb) a dword displacement jump. */
3821 /* dword disp jmp */
3822 opcode[0] = 0xe9;
3823 fragP->fr_fix += size;
3824 fix_new (fragP, old_fr_fix, size,
3825 fragP->fr_symbol,
3826 fragP->fr_offset, 1,
3827 reloc_type);
3828 break;
3829
3830 default:
3831 /* This changes the byte-displacement jump 0x7N
3832 to the dword-displacement jump 0x0f,0x8N. */
3833 opcode[1] = opcode[0] + 0x10;
3834 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3835 /* We've added an opcode byte. */
3836 fragP->fr_fix += 1 + size;
3837 fix_new (fragP, old_fr_fix + 1, size,
3838 fragP->fr_symbol,
3839 fragP->fr_offset, 1,
3840 reloc_type);
3841 break;
3842 }
3843 frag_wane (fragP);
3844 return fragP->fr_fix - old_fr_fix;
3845 }
3846 /* Guess a short jump. */
3847 return 1;
3848 }
3849
3850 /* Called after relax() is finished.
3851
3852 In: Address of frag.
3853 fr_type == rs_machine_dependent.
3854 fr_subtype is what the address relaxed to.
3855
3856 Out: Any fixSs and constants are set up.
3857 Caller will turn frag into a ".space 0". */
3858
3859 #ifndef BFD_ASSEMBLER
3860 void
3861 md_convert_frag (headers, sec, fragP)
3862 object_headers *headers ATTRIBUTE_UNUSED;
3863 segT sec ATTRIBUTE_UNUSED;
3864 register fragS *fragP;
3865 #else
3866 void
3867 md_convert_frag (abfd, sec, fragP)
3868 bfd *abfd ATTRIBUTE_UNUSED;
3869 segT sec ATTRIBUTE_UNUSED;
3870 register fragS *fragP;
3871 #endif
3872 {
3873 register unsigned char *opcode;
3874 unsigned char *where_to_put_displacement = NULL;
3875 offsetT target_address;
3876 offsetT opcode_address;
3877 unsigned int extension = 0;
3878 offsetT displacement_from_opcode_start;
3879
3880 opcode = (unsigned char *) fragP->fr_opcode;
3881
3882 /* Address we want to reach in file space. */
3883 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
3884 #ifdef BFD_ASSEMBLER
3885 /* Not needed otherwise? */
3886 target_address += symbol_get_frag (fragP->fr_symbol)->fr_address;
3887 #endif
3888
3889 /* Address opcode resides at in file space. */
3890 opcode_address = fragP->fr_address + fragP->fr_fix;
3891
3892 /* Displacement from opcode start to fill into instruction. */
3893 displacement_from_opcode_start = target_address - opcode_address;
3894
3895 switch (fragP->fr_subtype)
3896 {
3897 case ENCODE_RELAX_STATE (COND_JUMP, SMALL):
3898 case ENCODE_RELAX_STATE (COND_JUMP, SMALL16):
3899 case ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL):
3900 case ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL16):
3901 /* Don't have to change opcode. */
3902 extension = 1; /* 1 opcode + 1 displacement */
3903 where_to_put_displacement = &opcode[1];
3904 break;
3905
3906 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
3907 extension = 5; /* 2 opcode + 4 displacement */
3908 opcode[1] = opcode[0] + 0x10;
3909 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3910 where_to_put_displacement = &opcode[2];
3911 break;
3912
3913 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
3914 extension = 4; /* 1 opcode + 4 displacement */
3915 opcode[0] = 0xe9;
3916 where_to_put_displacement = &opcode[1];
3917 break;
3918
3919 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
3920 extension = 3; /* 2 opcode + 2 displacement */
3921 opcode[1] = opcode[0] + 0x10;
3922 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3923 where_to_put_displacement = &opcode[2];
3924 break;
3925
3926 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
3927 extension = 2; /* 1 opcode + 2 displacement */
3928 opcode[0] = 0xe9;
3929 where_to_put_displacement = &opcode[1];
3930 break;
3931
3932 default:
3933 BAD_CASE (fragP->fr_subtype);
3934 break;
3935 }
3936 /* Now put displacement after opcode. */
3937 md_number_to_chars ((char *) where_to_put_displacement,
3938 (valueT) (displacement_from_opcode_start - extension),
3939 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
3940 fragP->fr_fix += extension;
3941 }
3942 \f
3943 /* Size of byte displacement jmp. */
3944 int md_short_jump_size = 2;
3945
3946 /* Size of dword displacement jmp. */
3947 int md_long_jump_size = 5;
3948
3949 /* Size of relocation record. */
3950 const int md_reloc_size = 8;
3951
3952 void
3953 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
3954 char *ptr;
3955 addressT from_addr, to_addr;
3956 fragS *frag ATTRIBUTE_UNUSED;
3957 symbolS *to_symbol ATTRIBUTE_UNUSED;
3958 {
3959 offsetT offset;
3960
3961 offset = to_addr - (from_addr + 2);
3962 /* Opcode for byte-disp jump. */
3963 md_number_to_chars (ptr, (valueT) 0xeb, 1);
3964 md_number_to_chars (ptr + 1, (valueT) offset, 1);
3965 }
3966
3967 void
3968 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
3969 char *ptr;
3970 addressT from_addr, to_addr;
3971 fragS *frag ATTRIBUTE_UNUSED;
3972 symbolS *to_symbol ATTRIBUTE_UNUSED;
3973 {
3974 offsetT offset;
3975
3976 offset = to_addr - (from_addr + 5);
3977 md_number_to_chars (ptr, (valueT) 0xe9, 1);
3978 md_number_to_chars (ptr + 1, (valueT) offset, 4);
3979 }
3980 \f
3981 /* Apply a fixup (fixS) to segment data, once it has been determined
3982 by our caller that we have all the info we need to fix it up.
3983
3984 On the 386, immediates, displacements, and data pointers are all in
3985 the same (little-endian) format, so we don't need to care about which
3986 we are handling. */
3987
3988 int
3989 md_apply_fix3 (fixP, valp, seg)
3990 /* The fix we're to put in. */
3991 fixS *fixP;
3992
3993 /* Pointer to the value of the bits. */
3994 valueT *valp;
3995
3996 /* Segment fix is from. */
3997 segT seg ATTRIBUTE_UNUSED;
3998 {
3999 register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
4000 valueT value = *valp;
4001
4002 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
4003 if (fixP->fx_pcrel)
4004 {
4005 switch (fixP->fx_r_type)
4006 {
4007 default:
4008 break;
4009
4010 case BFD_RELOC_32:
4011 fixP->fx_r_type = BFD_RELOC_32_PCREL;
4012 break;
4013 case BFD_RELOC_16:
4014 fixP->fx_r_type = BFD_RELOC_16_PCREL;
4015 break;
4016 case BFD_RELOC_8:
4017 fixP->fx_r_type = BFD_RELOC_8_PCREL;
4018 break;
4019 }
4020 }
4021
4022 /* This is a hack. There should be a better way to handle this.
4023 This covers for the fact that bfd_install_relocation will
4024 subtract the current location (for partial_inplace, PC relative
4025 relocations); see more below. */
4026 if ((fixP->fx_r_type == BFD_RELOC_32_PCREL
4027 || fixP->fx_r_type == BFD_RELOC_16_PCREL
4028 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
4029 && fixP->fx_addsy)
4030 {
4031 #ifndef OBJ_AOUT
4032 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4033 #ifdef TE_PE
4034 || OUTPUT_FLAVOR == bfd_target_coff_flavour
4035 #endif
4036 )
4037 value += fixP->fx_where + fixP->fx_frag->fr_address;
4038 #endif
4039 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4040 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
4041 {
4042 segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
4043
4044 if ((fseg == seg
4045 || (symbol_section_p (fixP->fx_addsy)
4046 && fseg != absolute_section))
4047 && ! S_IS_EXTERNAL (fixP->fx_addsy)
4048 && ! S_IS_WEAK (fixP->fx_addsy)
4049 && S_IS_DEFINED (fixP->fx_addsy)
4050 && ! S_IS_COMMON (fixP->fx_addsy))
4051 {
4052 /* Yes, we add the values in twice. This is because
4053 bfd_perform_relocation subtracts them out again. I think
4054 bfd_perform_relocation is broken, but I don't dare change
4055 it. FIXME. */
4056 value += fixP->fx_where + fixP->fx_frag->fr_address;
4057 }
4058 }
4059 #endif
4060 #if defined (OBJ_COFF) && defined (TE_PE)
4061 /* For some reason, the PE format does not store a section
4062 address offset for a PC relative symbol. */
4063 if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
4064 value += md_pcrel_from (fixP);
4065 #endif
4066 }
4067
4068 /* Fix a few things - the dynamic linker expects certain values here,
4069 and we must not dissappoint it. */
4070 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4071 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4072 && fixP->fx_addsy)
4073 switch (fixP->fx_r_type)
4074 {
4075 case BFD_RELOC_386_PLT32:
4076 /* Make the jump instruction point to the address of the operand. At
4077 runtime we merely add the offset to the actual PLT entry. */
4078 value = -4;
4079 break;
4080 case BFD_RELOC_386_GOTPC:
4081
4082 /* This is tough to explain. We end up with this one if we have
4083 * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal
4084 * here is to obtain the absolute address of the GOT, and it is strongly
4085 * preferable from a performance point of view to avoid using a runtime
4086 * relocation for this. The actual sequence of instructions often look
4087 * something like:
4088 *
4089 * call .L66
4090 * .L66:
4091 * popl %ebx
4092 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
4093 *
4094 * The call and pop essentially return the absolute address of
4095 * the label .L66 and store it in %ebx. The linker itself will
4096 * ultimately change the first operand of the addl so that %ebx points to
4097 * the GOT, but to keep things simple, the .o file must have this operand
4098 * set so that it generates not the absolute address of .L66, but the
4099 * absolute address of itself. This allows the linker itself simply
4100 * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
4101 * added in, and the addend of the relocation is stored in the operand
4102 * field for the instruction itself.
4103 *
4104 * Our job here is to fix the operand so that it would add the correct
4105 * offset so that %ebx would point to itself. The thing that is tricky is
4106 * that .-.L66 will point to the beginning of the instruction, so we need
4107 * to further modify the operand so that it will point to itself.
4108 * There are other cases where you have something like:
4109 *
4110 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4111 *
4112 * and here no correction would be required. Internally in the assembler
4113 * we treat operands of this form as not being pcrel since the '.' is
4114 * explicitly mentioned, and I wonder whether it would simplify matters
4115 * to do it this way. Who knows. In earlier versions of the PIC patches,
4116 * the pcrel_adjust field was used to store the correction, but since the
4117 * expression is not pcrel, I felt it would be confusing to do it this
4118 * way. */
4119
4120 value -= 1;
4121 break;
4122 case BFD_RELOC_386_GOT32:
4123 value = 0; /* Fully resolved at runtime. No addend. */
4124 break;
4125 case BFD_RELOC_386_GOTOFF:
4126 break;
4127
4128 case BFD_RELOC_VTABLE_INHERIT:
4129 case BFD_RELOC_VTABLE_ENTRY:
4130 fixP->fx_done = 0;
4131 return 1;
4132
4133 default:
4134 break;
4135 }
4136 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
4137 *valp = value;
4138 #endif /* defined (BFD_ASSEMBLER) && !defined (TE_Mach) */
4139 md_number_to_chars (p, value, fixP->fx_size);
4140
4141 return 1;
4142 }
4143 \f
4144 #define MAX_LITTLENUMS 6
4145
4146 /* Turn the string pointed to by litP into a floating point constant
4147 of type TYPE, and emit the appropriate bytes. The number of
4148 LITTLENUMS emitted is stored in *SIZEP. An error message is
4149 returned, or NULL on OK. */
4150
4151 char *
4152 md_atof (type, litP, sizeP)
4153 int type;
4154 char *litP;
4155 int *sizeP;
4156 {
4157 int prec;
4158 LITTLENUM_TYPE words[MAX_LITTLENUMS];
4159 LITTLENUM_TYPE *wordP;
4160 char *t;
4161
4162 switch (type)
4163 {
4164 case 'f':
4165 case 'F':
4166 prec = 2;
4167 break;
4168
4169 case 'd':
4170 case 'D':
4171 prec = 4;
4172 break;
4173
4174 case 'x':
4175 case 'X':
4176 prec = 5;
4177 break;
4178
4179 default:
4180 *sizeP = 0;
4181 return _("Bad call to md_atof ()");
4182 }
4183 t = atof_ieee (input_line_pointer, type, words);
4184 if (t)
4185 input_line_pointer = t;
4186
4187 *sizeP = prec * sizeof (LITTLENUM_TYPE);
4188 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
4189 the bigendian 386. */
4190 for (wordP = words + prec - 1; prec--;)
4191 {
4192 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
4193 litP += sizeof (LITTLENUM_TYPE);
4194 }
4195 return 0;
4196 }
4197 \f
4198 char output_invalid_buf[8];
4199
4200 static char *
4201 output_invalid (c)
4202 int c;
4203 {
4204 if (isprint (c))
4205 sprintf (output_invalid_buf, "'%c'", c);
4206 else
4207 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
4208 return output_invalid_buf;
4209 }
4210
4211 /* REG_STRING starts *before* REGISTER_PREFIX. */
4212
4213 static const reg_entry *
4214 parse_register (reg_string, end_op)
4215 char *reg_string;
4216 char **end_op;
4217 {
4218 char *s = reg_string;
4219 char *p;
4220 char reg_name_given[MAX_REG_NAME_SIZE + 1];
4221 const reg_entry *r;
4222
4223 /* Skip possible REGISTER_PREFIX and possible whitespace. */
4224 if (*s == REGISTER_PREFIX)
4225 ++s;
4226
4227 if (is_space_char (*s))
4228 ++s;
4229
4230 p = reg_name_given;
4231 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
4232 {
4233 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
4234 return (const reg_entry *) NULL;
4235 s++;
4236 }
4237
4238 *end_op = s;
4239
4240 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
4241
4242 /* Handle floating point regs, allowing spaces in the (i) part. */
4243 if (r == i386_regtab /* %st is first entry of table */)
4244 {
4245 if (is_space_char (*s))
4246 ++s;
4247 if (*s == '(')
4248 {
4249 ++s;
4250 if (is_space_char (*s))
4251 ++s;
4252 if (*s >= '0' && *s <= '7')
4253 {
4254 r = &i386_float_regtab[*s - '0'];
4255 ++s;
4256 if (is_space_char (*s))
4257 ++s;
4258 if (*s == ')')
4259 {
4260 *end_op = s + 1;
4261 return r;
4262 }
4263 }
4264 /* We have "%st(" then garbage. */
4265 return (const reg_entry *) NULL;
4266 }
4267 }
4268
4269 return r;
4270 }
4271 \f
4272 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4273 const char *md_shortopts = "kVQ:sq";
4274 #else
4275 const char *md_shortopts = "q";
4276 #endif
4277 struct option md_longopts[] = {
4278 {NULL, no_argument, NULL, 0}
4279 };
4280 size_t md_longopts_size = sizeof (md_longopts);
4281
4282 int
4283 md_parse_option (c, arg)
4284 int c;
4285 char *arg ATTRIBUTE_UNUSED;
4286 {
4287 switch (c)
4288 {
4289 case 'q':
4290 quiet_warnings = 1;
4291 break;
4292
4293 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4294 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
4295 should be emitted or not. FIXME: Not implemented. */
4296 case 'Q':
4297 break;
4298
4299 /* -V: SVR4 argument to print version ID. */
4300 case 'V':
4301 print_version_id ();
4302 break;
4303
4304 /* -k: Ignore for FreeBSD compatibility. */
4305 case 'k':
4306 break;
4307
4308 case 's':
4309 /* -s: On i386 Solaris, this tells the native assembler to use
4310 .stab instead of .stab.excl. We always use .stab anyhow. */
4311 break;
4312 #endif
4313
4314 default:
4315 return 0;
4316 }
4317 return 1;
4318 }
4319
4320 void
4321 md_show_usage (stream)
4322 FILE *stream;
4323 {
4324 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4325 fprintf (stream, _("\
4326 -Q ignored\n\
4327 -V print assembler version number\n\
4328 -k ignored\n\
4329 -q quieten some warnings\n\
4330 -s ignored\n"));
4331 #else
4332 fprintf (stream, _("\
4333 -q quieten some warnings\n"));
4334 #endif
4335 }
4336
4337 #ifdef BFD_ASSEMBLER
4338 #if ((defined (OBJ_MAYBE_ELF) && defined (OBJ_MAYBE_COFF)) \
4339 || (defined (OBJ_MAYBE_ELF) && defined (OBJ_MAYBE_AOUT)) \
4340 || (defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)))
4341
4342 /* Pick the target format to use. */
4343
4344 const char *
4345 i386_target_format ()
4346 {
4347 switch (OUTPUT_FLAVOR)
4348 {
4349 #ifdef OBJ_MAYBE_AOUT
4350 case bfd_target_aout_flavour:
4351 return AOUT_TARGET_FORMAT;
4352 #endif
4353 #ifdef OBJ_MAYBE_COFF
4354 case bfd_target_coff_flavour:
4355 return "coff-i386";
4356 #endif
4357 #ifdef OBJ_MAYBE_ELF
4358 case bfd_target_elf_flavour:
4359 return "elf32-i386";
4360 #endif
4361 default:
4362 abort ();
4363 return NULL;
4364 }
4365 }
4366
4367 #endif /* OBJ_MAYBE_ more than one */
4368 #endif /* BFD_ASSEMBLER */
4369 \f
4370 symbolS *
4371 md_undefined_symbol (name)
4372 char *name;
4373 {
4374 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
4375 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
4376 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
4377 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4378 {
4379 if (!GOT_symbol)
4380 {
4381 if (symbol_find (name))
4382 as_bad (_("GOT already in symbol table"));
4383 GOT_symbol = symbol_new (name, undefined_section,
4384 (valueT) 0, &zero_address_frag);
4385 };
4386 return GOT_symbol;
4387 }
4388 return 0;
4389 }
4390
4391 /* Round up a section size to the appropriate boundary. */
4392
4393 valueT
4394 md_section_align (segment, size)
4395 segT segment ATTRIBUTE_UNUSED;
4396 valueT size;
4397 {
4398 #ifdef BFD_ASSEMBLER
4399 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4400 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
4401 {
4402 /* For a.out, force the section size to be aligned. If we don't do
4403 this, BFD will align it for us, but it will not write out the
4404 final bytes of the section. This may be a bug in BFD, but it is
4405 easier to fix it here since that is how the other a.out targets
4406 work. */
4407 int align;
4408
4409 align = bfd_get_section_alignment (stdoutput, segment);
4410 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
4411 }
4412 #endif
4413 #endif
4414
4415 return size;
4416 }
4417
4418 /* On the i386, PC-relative offsets are relative to the start of the
4419 next instruction. That is, the address of the offset, plus its
4420 size, since the offset is always the last part of the insn. */
4421
4422 long
4423 md_pcrel_from (fixP)
4424 fixS *fixP;
4425 {
4426 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
4427 }
4428
4429 #ifndef I386COFF
4430
4431 static void
4432 s_bss (ignore)
4433 int ignore ATTRIBUTE_UNUSED;
4434 {
4435 register int temp;
4436
4437 temp = get_absolute_expression ();
4438 subseg_set (bss_section, (subsegT) temp);
4439 demand_empty_rest_of_line ();
4440 }
4441
4442 #endif
4443
4444 #ifdef BFD_ASSEMBLER
4445
4446 void
4447 i386_validate_fix (fixp)
4448 fixS *fixp;
4449 {
4450 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
4451 {
4452 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
4453 fixp->fx_subsy = 0;
4454 }
4455 }
4456
4457 arelent *
4458 tc_gen_reloc (section, fixp)
4459 asection *section ATTRIBUTE_UNUSED;
4460 fixS *fixp;
4461 {
4462 arelent *rel;
4463 bfd_reloc_code_real_type code;
4464
4465 switch (fixp->fx_r_type)
4466 {
4467 case BFD_RELOC_386_PLT32:
4468 case BFD_RELOC_386_GOT32:
4469 case BFD_RELOC_386_GOTOFF:
4470 case BFD_RELOC_386_GOTPC:
4471 case BFD_RELOC_RVA:
4472 case BFD_RELOC_VTABLE_ENTRY:
4473 case BFD_RELOC_VTABLE_INHERIT:
4474 code = fixp->fx_r_type;
4475 break;
4476 default:
4477 if (fixp->fx_pcrel)
4478 {
4479 switch (fixp->fx_size)
4480 {
4481 default:
4482 as_bad (_("can not do %d byte pc-relative relocation"),
4483 fixp->fx_size);
4484 code = BFD_RELOC_32_PCREL;
4485 break;
4486 case 1: code = BFD_RELOC_8_PCREL; break;
4487 case 2: code = BFD_RELOC_16_PCREL; break;
4488 case 4: code = BFD_RELOC_32_PCREL; break;
4489 }
4490 }
4491 else
4492 {
4493 switch (fixp->fx_size)
4494 {
4495 default:
4496 as_bad (_("can not do %d byte relocation"), fixp->fx_size);
4497 code = BFD_RELOC_32;
4498 break;
4499 case 1: code = BFD_RELOC_8; break;
4500 case 2: code = BFD_RELOC_16; break;
4501 case 4: code = BFD_RELOC_32; break;
4502 }
4503 }
4504 break;
4505 }
4506
4507 if (code == BFD_RELOC_32
4508 && GOT_symbol
4509 && fixp->fx_addsy == GOT_symbol)
4510 code = BFD_RELOC_386_GOTPC;
4511
4512 rel = (arelent *) xmalloc (sizeof (arelent));
4513 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4514 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4515
4516 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4517 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
4518 vtable entry to be used in the relocation's section offset. */
4519 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
4520 rel->address = fixp->fx_offset;
4521
4522 if (fixp->fx_pcrel)
4523 rel->addend = fixp->fx_addnumber;
4524 else
4525 rel->addend = 0;
4526
4527 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
4528 if (rel->howto == NULL)
4529 {
4530 as_bad_where (fixp->fx_file, fixp->fx_line,
4531 _("cannot represent relocation type %s"),
4532 bfd_get_reloc_code_name (code));
4533 /* Set howto to a garbage value so that we can keep going. */
4534 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4535 assert (rel->howto != NULL);
4536 }
4537
4538 return rel;
4539 }
4540
4541 #else /* ! BFD_ASSEMBLER */
4542
4543 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
4544 void
4545 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
4546 char *where;
4547 fixS *fixP;
4548 relax_addressT segment_address_in_file;
4549 {
4550 /* In: length of relocation (or of address) in chars: 1, 2 or 4.
4551 Out: GNU LD relocation length code: 0, 1, or 2. */
4552
4553 static const unsigned char nbytes_r_length[] = { 42, 0, 1, 42, 2 };
4554 long r_symbolnum;
4555
4556 know (fixP->fx_addsy != NULL);
4557
4558 md_number_to_chars (where,
4559 (valueT) (fixP->fx_frag->fr_address
4560 + fixP->fx_where - segment_address_in_file),
4561 4);
4562
4563 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
4564 ? S_GET_TYPE (fixP->fx_addsy)
4565 : fixP->fx_addsy->sy_number);
4566
4567 where[6] = (r_symbolnum >> 16) & 0x0ff;
4568 where[5] = (r_symbolnum >> 8) & 0x0ff;
4569 where[4] = r_symbolnum & 0x0ff;
4570 where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
4571 | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
4572 | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
4573 }
4574
4575 #endif /* OBJ_AOUT or OBJ_BOUT. */
4576
4577 #if defined (I386COFF)
4578
4579 short
4580 tc_coff_fix2rtype (fixP)
4581 fixS *fixP;
4582 {
4583 if (fixP->fx_r_type == R_IMAGEBASE)
4584 return R_IMAGEBASE;
4585
4586 return (fixP->fx_pcrel ?
4587 (fixP->fx_size == 1 ? R_PCRBYTE :
4588 fixP->fx_size == 2 ? R_PCRWORD :
4589 R_PCRLONG) :
4590 (fixP->fx_size == 1 ? R_RELBYTE :
4591 fixP->fx_size == 2 ? R_RELWORD :
4592 R_DIR32));
4593 }
4594
4595 int
4596 tc_coff_sizemachdep (frag)
4597 fragS *frag;
4598 {
4599 if (frag->fr_next)
4600 return (frag->fr_next->fr_address - frag->fr_address);
4601 else
4602 return 0;
4603 }
4604
4605 #endif /* I386COFF */
4606
4607 #endif /* ! BFD_ASSEMBLER */