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