Mostly cosmetic. Fixes to comments. Don't start as_bad and as_warn
[binutils.git] / gas / config / tc-i386.c
blob3a01bab565badc05983877557967196153005a4d
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.
5 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
23 Intel 80386 machine specific gas.
24 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better.
29 #include <ctype.h>
31 #include "as.h"
32 #include "subsegs.h"
33 #include "opcode/i386.h"
35 #ifndef REGISTER_WARNINGS
36 #define REGISTER_WARNINGS 1
37 #endif
39 #ifndef INFER_ADDR_PREFIX
40 #define INFER_ADDR_PREFIX 1
41 #endif
43 #ifndef SCALE1_WHEN_NO_INDEX
44 /* Specifying a scale factor besides 1 when there is no index is
45 futile. eg. `mov (%ebx,2),%al' does exactly the same as
46 `mov (%ebx),%al'. To slavishly follow what the programmer
47 specified, set SCALE1_WHEN_NO_INDEX to 0. */
48 #define SCALE1_WHEN_NO_INDEX 1
49 #endif
51 #define true 1
52 #define false 0
54 static unsigned int mode_from_disp_size PARAMS ((unsigned int));
55 static int fits_in_signed_byte PARAMS ((long));
56 static int fits_in_unsigned_byte PARAMS ((long));
57 static int fits_in_unsigned_word PARAMS ((long));
58 static int fits_in_signed_word PARAMS ((long));
59 static int smallest_imm_type PARAMS ((long));
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));
65 #ifdef BFD_ASSEMBLER
66 static bfd_reloc_code_real_type reloc
67 PARAMS ((int, int, bfd_reloc_code_real_type));
68 #endif
70 /* 'md_assemble ()' gathers together information and puts it into a
71 i386_insn. */
73 union i386_op
75 expressionS *disps;
76 expressionS *imms;
77 const reg_entry *regs;
80 struct _i386_insn
82 /* TM holds the template for the insn were currently assembling. */
83 template tm;
85 /* SUFFIX holds the instruction mnemonic suffix if given.
86 (e.g. 'l' for 'movl') */
87 char suffix;
89 /* OPERANDS gives the number of given operands. */
90 unsigned int operands;
92 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
93 of given register, displacement, memory operands and immediate
94 operands. */
95 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
97 /* TYPES [i] is the type (see above #defines) which tells us how to
98 use OP[i] for the corresponding operand. */
99 unsigned int types[MAX_OPERANDS];
101 /* Displacement expression, immediate expression, or register for each
102 operand. */
103 union i386_op op[MAX_OPERANDS];
105 /* Relocation type for operand */
106 #ifdef BFD_ASSEMBLER
107 enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
108 #else
109 int disp_reloc[MAX_OPERANDS];
110 #endif
112 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
113 the base index byte below. */
114 const reg_entry *base_reg;
115 const reg_entry *index_reg;
116 unsigned int log2_scale_factor;
118 /* SEG gives the seg_entries of this insn. They are zero unless
119 explicit segment overrides are given. */
120 const seg_entry *seg[2]; /* segments for memory operands (if given) */
122 /* PREFIX holds all the given prefix opcodes (usually null).
123 PREFIXES is the number of prefix opcodes. */
124 unsigned int prefixes;
125 unsigned char prefix[MAX_PREFIXES];
127 /* RM and SIB are the modrm byte and the sib byte where the
128 addressing modes of this insn are encoded. */
130 modrm_byte rm;
131 sib_byte sib;
134 typedef struct _i386_insn i386_insn;
136 /* List of chars besides those in app.c:symbol_chars that can start an
137 operand. Used to prevent the scrubber eating vital white-space. */
138 #ifdef LEX_AT
139 const char extra_symbol_chars[] = "*%-(@";
140 #else
141 const char extra_symbol_chars[] = "*%-(";
142 #endif
144 /* This array holds the chars that always start a comment. If the
145 pre-processor is disabled, these aren't very useful */
146 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX))
147 /* Putting '/' here makes it impossible to use the divide operator.
148 However, we need it for compatibility with SVR4 systems. */
149 const char comment_chars[] = "#/";
150 #define PREFIX_SEPARATOR '\\'
151 #else
152 const char comment_chars[] = "#";
153 #define PREFIX_SEPARATOR '/'
154 #endif
156 /* This array holds the chars that only start a comment at the beginning of
157 a line. If the line seems to have the form '# 123 filename'
158 .line and .file directives will appear in the pre-processed output */
159 /* Note that input_file.c hand checks for '#' at the beginning of the
160 first line of the input file. This is because the compiler outputs
161 #NO_APP at the beginning of its output. */
162 /* Also note that comments started like this one will always work if
163 '/' isn't otherwise defined. */
164 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX))
165 const char line_comment_chars[] = "";
166 #else
167 const char line_comment_chars[] = "/";
168 #endif
170 const char line_separator_chars[] = "";
172 /* Chars that can be used to separate mant from exp in floating point nums */
173 const char EXP_CHARS[] = "eE";
175 /* Chars that mean this number is a floating point constant */
176 /* As in 0f12.456 */
177 /* or 0d1.2345e12 */
178 const char FLT_CHARS[] = "fFdDxX";
180 /* tables for lexical analysis */
181 static char mnemonic_chars[256];
182 static char register_chars[256];
183 static char operand_chars[256];
184 static char identifier_chars[256];
185 static char digit_chars[256];
187 /* lexical macros */
188 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
189 #define is_operand_char(x) (operand_chars[(unsigned char) x])
190 #define is_register_char(x) (register_chars[(unsigned char) x])
191 #define is_space_char(x) ((x) == ' ')
192 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
193 #define is_digit_char(x) (digit_chars[(unsigned char) x])
195 /* put here all non-digit non-letter charcters that may occur in an operand */
196 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
198 /* md_assemble() always leaves the strings it's passed unaltered. To
199 effect this we maintain a stack of saved characters that we've smashed
200 with '\0's (indicating end of strings for various sub-fields of the
201 assembler instruction). */
202 static char save_stack[32];
203 static char *save_stack_p; /* stack pointer */
204 #define END_STRING_AND_SAVE(s) \
205 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
206 #define RESTORE_END_STRING(s) \
207 do { *(s) = *--save_stack_p; } while (0)
209 /* The instruction we're assembling. */
210 static i386_insn i;
212 /* Possible templates for current insn. */
213 static const templates *current_templates;
215 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
216 static expressionS disp_expressions[2], im_expressions[2];
218 static int this_operand; /* current operand we are working on */
220 static int flag_do_long_jump; /* FIXME what does this do? */
222 static int flag_16bit_code; /* 1 if we're writing 16-bit code, 0 if 32-bit */
224 static int intel_syntax = 0; /* 1 for intel syntax, 0 if att syntax */
226 static int allow_naked_reg = 0; /* 1 if register prefix % not required */
228 static char stackop_size = '\0'; /* Used in 16 bit gcc mode to add an l
229 suffix to call, ret, enter, leave, push,
230 and pop instructions so that gcc has the
231 same stack frame as in 32 bit mode. */
233 /* Interface to relax_segment.
234 There are 2 relax states for 386 jump insns: one for conditional &
235 one for unconditional jumps. This is because these two types of
236 jumps add different sizes to frags when we're figuring out what
237 sort of jump to choose to reach a given label. */
239 /* types */
240 #define COND_JUMP 1 /* conditional jump */
241 #define UNCOND_JUMP 2 /* unconditional jump */
242 /* sizes */
243 #define CODE16 1
244 #define SMALL 0
245 #define SMALL16 (SMALL|CODE16)
246 #define BIG 2
247 #define BIG16 (BIG|CODE16)
249 #ifndef INLINE
250 #ifdef __GNUC__
251 #define INLINE __inline__
252 #else
253 #define INLINE
254 #endif
255 #endif
257 #define ENCODE_RELAX_STATE(type,size) \
258 ((relax_substateT)((type<<2) | (size)))
259 #define SIZE_FROM_RELAX_STATE(s) \
260 ( (((s) & 0x3) == BIG ? 4 : (((s) & 0x3) == BIG16 ? 2 : 1)) )
262 /* This table is used by relax_frag to promote short jumps to long
263 ones where necessary. SMALL (short) jumps may be promoted to BIG
264 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
265 don't allow a short jump in a 32 bit code segment to be promoted to
266 a 16 bit offset jump because it's slower (requires data size
267 prefix), and doesn't work, unless the destination is in the bottom
268 64k of the code segment (The top 16 bits of eip are zeroed). */
270 const relax_typeS md_relax_table[] =
272 /* The fields are:
273 1) most positive reach of this state,
274 2) most negative reach of this state,
275 3) how many bytes this mode will add to the size of the current frag
276 4) which index into the table to try if we can't fit into this one.
278 {1, 1, 0, 0},
279 {1, 1, 0, 0},
280 {1, 1, 0, 0},
281 {1, 1, 0, 0},
283 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
284 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
285 /* dword conditionals adds 4 bytes to frag:
286 1 extra opcode byte, 3 extra displacement bytes. */
287 {0, 0, 4, 0},
288 /* word conditionals add 2 bytes to frag:
289 1 extra opcode byte, 1 extra displacement byte. */
290 {0, 0, 2, 0},
292 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
293 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
294 /* dword jmp adds 3 bytes to frag:
295 0 extra opcode bytes, 3 extra displacement bytes. */
296 {0, 0, 3, 0},
297 /* word jmp adds 1 byte to frag:
298 0 extra opcode bytes, 1 extra displacement byte. */
299 {0, 0, 1, 0}
304 void
305 i386_align_code (fragP, count)
306 fragS *fragP;
307 int count;
309 /* Various efficient no-op patterns for aligning code labels. */
310 /* Note: Don't try to assemble the instructions in the comments. */
311 /* 0L and 0w are not legal */
312 static const char f32_1[] =
313 {0x90}; /* nop */
314 static const char f32_2[] =
315 {0x89,0xf6}; /* movl %esi,%esi */
316 static const char f32_3[] =
317 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
318 static const char f32_4[] =
319 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
320 static const char f32_5[] =
321 {0x90, /* nop */
322 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
323 static const char f32_6[] =
324 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
325 static const char f32_7[] =
326 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
327 static const char f32_8[] =
328 {0x90, /* nop */
329 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
330 static const char f32_9[] =
331 {0x89,0xf6, /* movl %esi,%esi */
332 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
333 static const char f32_10[] =
334 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
335 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
336 static const char f32_11[] =
337 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
338 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
339 static const char f32_12[] =
340 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
341 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
342 static const char f32_13[] =
343 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
344 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
345 static const char f32_14[] =
346 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
347 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
348 static const char f32_15[] =
349 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
350 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
351 static const char f16_3[] =
352 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
353 static const char f16_4[] =
354 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
355 static const char f16_5[] =
356 {0x90, /* nop */
357 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
358 static const char f16_6[] =
359 {0x89,0xf6, /* mov %si,%si */
360 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
361 static const char f16_7[] =
362 {0x8d,0x74,0x00, /* lea 0(%si),%si */
363 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
364 static const char f16_8[] =
365 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
366 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
367 static const char *const f32_patt[] = {
368 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
369 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
371 static const char *const f16_patt[] = {
372 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
373 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
376 if (count > 0 && count <= 15)
378 if (flag_16bit_code)
380 memcpy(fragP->fr_literal + fragP->fr_fix,
381 f16_patt[count - 1], count);
382 if (count > 8) /* adjust jump offset */
383 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
385 else
386 memcpy(fragP->fr_literal + fragP->fr_fix,
387 f32_patt[count - 1], count);
388 fragP->fr_var = count;
392 static char *output_invalid PARAMS ((int c));
393 static int i386_operand PARAMS ((char *operand_string));
394 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
395 static const reg_entry *parse_register PARAMS ((char *reg_string,
396 char **end_op));
398 #ifndef I386COFF
399 static void s_bss PARAMS ((int));
400 #endif
402 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
404 static INLINE unsigned int
405 mode_from_disp_size (t)
406 unsigned int t;
408 return (t & Disp8) ? 1 : (t & (Disp16|Disp32)) ? 2 : 0;
411 static INLINE int
412 fits_in_signed_byte (num)
413 long num;
415 return (num >= -128) && (num <= 127);
416 } /* fits_in_signed_byte() */
418 static INLINE int
419 fits_in_unsigned_byte (num)
420 long num;
422 return (num & 0xff) == num;
423 } /* fits_in_unsigned_byte() */
425 static INLINE int
426 fits_in_unsigned_word (num)
427 long num;
429 return (num & 0xffff) == num;
430 } /* fits_in_unsigned_word() */
432 static INLINE int
433 fits_in_signed_word (num)
434 long num;
436 return (-32768 <= num) && (num <= 32767);
437 } /* fits_in_signed_word() */
439 static int
440 smallest_imm_type (num)
441 long num;
443 #if 0
444 /* This code is disabled because all the Imm1 forms in the opcode table
445 are slower on the i486, and they're the versions with the implicitly
446 specified single-position displacement, which has another syntax if
447 you really want to use that form. If you really prefer to have the
448 one-byte-shorter Imm1 form despite these problems, re-enable this
449 code. */
450 if (num == 1)
451 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
452 #endif
453 return (fits_in_signed_byte (num)
454 ? (Imm8S | Imm8 | Imm16 | Imm32)
455 : fits_in_unsigned_byte (num)
456 ? (Imm8 | Imm16 | Imm32)
457 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
458 ? (Imm16 | Imm32)
459 : (Imm32));
460 } /* smallest_imm_type() */
462 /* Returns 0 if attempting to add a prefix where one from the same
463 class already exists, 1 if non rep/repne added, 2 if rep/repne
464 added. */
465 static int
466 add_prefix (prefix)
467 unsigned int prefix;
469 int ret = 1;
470 int q;
472 switch (prefix)
474 default:
475 abort ();
477 case CS_PREFIX_OPCODE:
478 case DS_PREFIX_OPCODE:
479 case ES_PREFIX_OPCODE:
480 case FS_PREFIX_OPCODE:
481 case GS_PREFIX_OPCODE:
482 case SS_PREFIX_OPCODE:
483 q = SEG_PREFIX;
484 break;
486 case REPNE_PREFIX_OPCODE:
487 case REPE_PREFIX_OPCODE:
488 ret = 2;
489 /* fall thru */
490 case LOCK_PREFIX_OPCODE:
491 q = LOCKREP_PREFIX;
492 break;
494 case FWAIT_OPCODE:
495 q = WAIT_PREFIX;
496 break;
498 case ADDR_PREFIX_OPCODE:
499 q = ADDR_PREFIX;
500 break;
502 case DATA_PREFIX_OPCODE:
503 q = DATA_PREFIX;
504 break;
507 if (i.prefix[q])
509 as_bad (_("same type of prefix used twice"));
510 return 0;
513 i.prefixes += 1;
514 i.prefix[q] = prefix;
515 return ret;
518 static void
519 set_16bit_code_flag (new_16bit_code_flag)
520 int new_16bit_code_flag;
522 flag_16bit_code = new_16bit_code_flag;
523 stackop_size = '\0';
526 static void
527 set_16bit_gcc_code_flag (new_16bit_code_flag)
528 int new_16bit_code_flag;
530 flag_16bit_code = new_16bit_code_flag;
531 stackop_size = new_16bit_code_flag ? 'l' : '\0';
534 static void
535 set_intel_syntax (syntax_flag)
536 int syntax_flag;
538 /* Find out if register prefixing is specified. */
539 int ask_naked_reg = 0;
541 SKIP_WHITESPACE ();
542 if (! is_end_of_line[(unsigned char) *input_line_pointer])
544 char *string = input_line_pointer;
545 int e = get_symbol_end ();
547 if (strcmp(string, "prefix") == 0)
548 ask_naked_reg = 1;
549 else if (strcmp(string, "noprefix") == 0)
550 ask_naked_reg = -1;
551 else
552 as_bad (_("bad argument to syntax directive."));
553 *input_line_pointer = e;
555 demand_empty_rest_of_line ();
557 intel_syntax = syntax_flag;
559 if (ask_naked_reg == 0)
561 #ifdef BFD_ASSEMBLER
562 allow_naked_reg = (intel_syntax
563 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
564 #else
565 allow_naked_reg = 0; /* conservative default */
566 #endif
568 else
569 allow_naked_reg = (ask_naked_reg < 0);
572 const pseudo_typeS md_pseudo_table[] =
574 #ifndef I386COFF
575 {"bss", s_bss, 0},
576 #endif
577 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
578 {"align", s_align_bytes, 0},
579 #else
580 {"align", s_align_ptwo, 0},
581 #endif
582 {"ffloat", float_cons, 'f'},
583 {"dfloat", float_cons, 'd'},
584 {"tfloat", float_cons, 'x'},
585 {"value", cons, 2},
586 {"noopt", s_ignore, 0},
587 {"optim", s_ignore, 0},
588 {"code16gcc", set_16bit_gcc_code_flag, 1},
589 {"code16", set_16bit_code_flag, 1},
590 {"code32", set_16bit_code_flag, 0},
591 {"intel_syntax", set_intel_syntax, 1},
592 {"att_syntax", set_intel_syntax, 0},
593 {0, 0, 0}
596 /* for interface with expression () */
597 extern char *input_line_pointer;
599 /* hash table for instruction mnemonic lookup */
600 static struct hash_control *op_hash;
601 /* hash table for register lookup */
602 static struct hash_control *reg_hash;
605 void
606 md_begin ()
608 const char *hash_err;
610 /* initialize op_hash hash table */
611 op_hash = hash_new ();
614 register const template *optab;
615 register templates *core_optab;
617 optab = i386_optab; /* setup for loop */
618 core_optab = (templates *) xmalloc (sizeof (templates));
619 core_optab->start = optab;
621 while (1)
623 ++optab;
624 if (optab->name == NULL
625 || strcmp (optab->name, (optab - 1)->name) != 0)
627 /* different name --> ship out current template list;
628 add to hash table; & begin anew */
629 core_optab->end = optab;
630 hash_err = hash_insert (op_hash,
631 (optab - 1)->name,
632 (PTR) core_optab);
633 if (hash_err)
635 hash_error:
636 as_fatal (_("Internal Error: Can't hash %s: %s"),
637 (optab - 1)->name,
638 hash_err);
640 if (optab->name == NULL)
641 break;
642 core_optab = (templates *) xmalloc (sizeof (templates));
643 core_optab->start = optab;
648 /* initialize reg_hash hash table */
649 reg_hash = hash_new ();
651 register const reg_entry *regtab;
653 for (regtab = i386_regtab;
654 regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
655 regtab++)
657 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
658 if (hash_err)
659 goto hash_error;
663 /* fill in lexical tables: mnemonic_chars, operand_chars. */
665 register int c;
666 register char *p;
668 for (c = 0; c < 256; c++)
670 if (isdigit (c))
672 digit_chars[c] = c;
673 mnemonic_chars[c] = c;
674 register_chars[c] = c;
675 operand_chars[c] = c;
677 else if (islower (c))
679 mnemonic_chars[c] = c;
680 register_chars[c] = c;
681 operand_chars[c] = c;
683 else if (isupper (c))
685 mnemonic_chars[c] = tolower (c);
686 register_chars[c] = mnemonic_chars[c];
687 operand_chars[c] = c;
690 if (isalpha (c) || isdigit (c))
691 identifier_chars[c] = c;
692 else if (c >= 128)
694 identifier_chars[c] = c;
695 operand_chars[c] = c;
699 #ifdef LEX_AT
700 identifier_chars['@'] = '@';
701 #endif
702 digit_chars['-'] = '-';
703 identifier_chars['_'] = '_';
704 identifier_chars['.'] = '.';
706 for (p = operand_special_chars; *p != '\0'; p++)
707 operand_chars[(unsigned char) *p] = *p;
710 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
711 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
713 record_alignment (text_section, 2);
714 record_alignment (data_section, 2);
715 record_alignment (bss_section, 2);
717 #endif
720 void
721 i386_print_statistics (file)
722 FILE *file;
724 hash_print_statistics (file, "i386 opcode", op_hash);
725 hash_print_statistics (file, "i386 register", reg_hash);
729 #ifdef DEBUG386
731 /* debugging routines for md_assemble */
732 static void pi PARAMS ((char *, i386_insn *));
733 static void pte PARAMS ((template *));
734 static void pt PARAMS ((unsigned int));
735 static void pe PARAMS ((expressionS *));
736 static void ps PARAMS ((symbolS *));
738 static void
739 pi (line, x)
740 char *line;
741 i386_insn *x;
743 register template *p;
744 int i;
746 fprintf (stdout, "%s: template ", line);
747 pte (&x->tm);
748 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x",
749 x->rm.mode, x->rm.reg, x->rm.regmem);
750 fprintf (stdout, " base %x index %x scale %x\n",
751 x->bi.base, x->bi.index, x->bi.scale);
752 for (i = 0; i < x->operands; i++)
754 fprintf (stdout, " #%d: ", i + 1);
755 pt (x->types[i]);
756 fprintf (stdout, "\n");
757 if (x->types[i]
758 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
759 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
760 if (x->types[i] & Imm)
761 pe (x->op[i].imms);
762 if (x->types[i] & Disp)
763 pe (x->op[i].disps);
767 static void
768 pte (t)
769 template *t;
771 int i;
772 fprintf (stdout, " %d operands ", t->operands);
773 fprintf (stdout, "opcode %x ",
774 t->base_opcode);
775 if (t->extension_opcode != None)
776 fprintf (stdout, "ext %x ", t->extension_opcode);
777 if (t->opcode_modifier & D)
778 fprintf (stdout, "D");
779 if (t->opcode_modifier & W)
780 fprintf (stdout, "W");
781 fprintf (stdout, "\n");
782 for (i = 0; i < t->operands; i++)
784 fprintf (stdout, " #%d type ", i + 1);
785 pt (t->operand_types[i]);
786 fprintf (stdout, "\n");
790 static void
791 pe (e)
792 expressionS *e;
794 fprintf (stdout, " operation %d\n", e->X_op);
795 fprintf (stdout, " add_number %ld (%lx)\n",
796 (long) e->X_add_number, (long) e->X_add_number);
797 if (e->X_add_symbol)
799 fprintf (stdout, " add_symbol ");
800 ps (e->X_add_symbol);
801 fprintf (stdout, "\n");
803 if (e->X_op_symbol)
805 fprintf (stdout, " op_symbol ");
806 ps (e->X_op_symbol);
807 fprintf (stdout, "\n");
811 static void
812 ps (s)
813 symbolS *s;
815 fprintf (stdout, "%s type %s%s",
816 S_GET_NAME (s),
817 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
818 segment_name (S_GET_SEGMENT (s)));
821 struct type_name
823 unsigned int mask;
824 char *tname;
827 type_names[] =
829 { Reg8, "r8" },
830 { Reg16, "r16" },
831 { Reg32, "r32" },
832 { Imm8, "i8" },
833 { Imm8S, "i8s" },
834 { Imm16, "i16" },
835 { Imm32, "i32" },
836 { Imm1, "i1" },
837 { BaseIndex, "BaseIndex" },
838 { Disp8, "d8" },
839 { Disp16, "d16" },
840 { Disp32, "d32" },
841 { InOutPortReg, "InOutPortReg" },
842 { ShiftCount, "ShiftCount" },
843 { Control, "control reg" },
844 { Test, "test reg" },
845 { Debug, "debug reg" },
846 { FloatReg, "FReg" },
847 { FloatAcc, "FAcc" },
848 { SReg2, "SReg2" },
849 { SReg3, "SReg3" },
850 { Acc, "Acc" },
851 { JumpAbsolute, "Jump Absolute" },
852 { RegMMX, "rMMX" },
853 { RegXMM, "rXMM" },
854 { EsSeg, "es" },
855 { 0, "" }
858 static void
859 pt (t)
860 unsigned int t;
862 register struct type_name *ty;
864 if (t == Unknown)
866 fprintf (stdout, _("Unknown"));
868 else
870 for (ty = type_names; ty->mask; ty++)
871 if (t & ty->mask)
872 fprintf (stdout, "%s, ", ty->tname);
874 fflush (stdout);
877 #endif /* DEBUG386 */
880 tc_i386_force_relocation (fixp)
881 struct fix *fixp;
883 #ifdef BFD_ASSEMBLER
884 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
885 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
886 return 1;
887 return 0;
888 #else
889 /* For COFF */
890 return fixp->fx_r_type == 7;
891 #endif
894 #ifdef BFD_ASSEMBLER
895 static bfd_reloc_code_real_type reloc
896 PARAMS ((int, int, bfd_reloc_code_real_type));
898 static bfd_reloc_code_real_type
899 reloc (size, pcrel, other)
900 int size;
901 int pcrel;
902 bfd_reloc_code_real_type other;
904 if (other != NO_RELOC) return other;
906 if (pcrel)
908 switch (size)
910 case 1: return BFD_RELOC_8_PCREL;
911 case 2: return BFD_RELOC_16_PCREL;
912 case 4: return BFD_RELOC_32_PCREL;
914 as_bad (_("can not do %d byte pc-relative relocation"), size);
916 else
918 switch (size)
920 case 1: return BFD_RELOC_8;
921 case 2: return BFD_RELOC_16;
922 case 4: return BFD_RELOC_32;
924 as_bad (_("can not do %d byte relocation"), size);
927 return BFD_RELOC_NONE;
931 * Here we decide which fixups can be adjusted to make them relative to
932 * the beginning of the section instead of the symbol. Basically we need
933 * to make sure that the dynamic relocations are done correctly, so in
934 * some cases we force the original symbol to be used.
937 tc_i386_fix_adjustable (fixP)
938 fixS *fixP;
940 #if defined (OBJ_ELF) || defined (TE_PE)
941 /* Prevent all adjustments to global symbols, or else dynamic
942 linking will not work correctly. */
943 if (S_IS_EXTERN (fixP->fx_addsy))
944 return 0;
945 if (S_IS_WEAK (fixP->fx_addsy))
946 return 0;
947 #endif
948 /* adjust_reloc_syms doesn't know about the GOT */
949 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
950 || fixP->fx_r_type == BFD_RELOC_386_PLT32
951 || fixP->fx_r_type == BFD_RELOC_386_GOT32
952 || fixP->fx_r_type == BFD_RELOC_RVA
953 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
954 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
955 return 0;
956 return 1;
958 #else
959 #define reloc(SIZE,PCREL,OTHER) 0
960 #define BFD_RELOC_16 0
961 #define BFD_RELOC_32 0
962 #define BFD_RELOC_16_PCREL 0
963 #define BFD_RELOC_32_PCREL 0
964 #define BFD_RELOC_386_PLT32 0
965 #define BFD_RELOC_386_GOT32 0
966 #define BFD_RELOC_386_GOTOFF 0
967 #endif
969 static int
970 intel_float_operand PARAMS ((char *mnemonic));
972 static int
973 intel_float_operand (mnemonic)
974 char *mnemonic;
976 if (mnemonic[0] == 'f' && mnemonic[1] =='i')
977 return 2;
979 if (mnemonic[0] == 'f')
980 return 1;
982 return 0;
985 /* This is the guts of the machine-dependent assembler. LINE points to a
986 machine dependent instruction. This function is supposed to emit
987 the frags/bytes it assembles to. */
989 void
990 md_assemble (line)
991 char *line;
993 /* Points to template once we've found it. */
994 const template *t;
996 /* Count the size of the instruction generated. */
997 int insn_size = 0;
999 int j;
1001 char mnemonic[MAX_MNEM_SIZE];
1003 /* Initialize globals. */
1004 memset (&i, '\0', sizeof (i));
1005 for (j = 0; j < MAX_OPERANDS; j++)
1006 i.disp_reloc[j] = NO_RELOC;
1007 memset (disp_expressions, '\0', sizeof (disp_expressions));
1008 memset (im_expressions, '\0', sizeof (im_expressions));
1009 save_stack_p = save_stack; /* reset stack pointer */
1011 /* First parse an instruction mnemonic & call i386_operand for the operands.
1012 We assume that the scrubber has arranged it so that line[0] is the valid
1013 start of a (possibly prefixed) mnemonic. */
1015 char *l = line;
1016 char *token_start = l;
1017 char *mnem_p;
1019 /* Non-zero if we found a prefix only acceptable with string insns. */
1020 const char *expecting_string_instruction = NULL;
1022 while (1)
1024 mnem_p = mnemonic;
1025 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1027 mnem_p++;
1028 if (mnem_p >= mnemonic + sizeof (mnemonic))
1030 as_bad (_("no such 386 instruction: `%s'"), token_start);
1031 return;
1033 l++;
1035 if (!is_space_char (*l)
1036 && *l != END_OF_INSN
1037 && *l != PREFIX_SEPARATOR)
1039 as_bad (_("invalid character %s in mnemonic"),
1040 output_invalid (*l));
1041 return;
1043 if (token_start == l)
1045 if (*l == PREFIX_SEPARATOR)
1046 as_bad (_("expecting prefix; got nothing"));
1047 else
1048 as_bad (_("expecting mnemonic; got nothing"));
1049 return;
1052 /* Look up instruction (or prefix) via hash table. */
1053 current_templates = hash_find (op_hash, mnemonic);
1055 if (*l != END_OF_INSN
1056 && (! is_space_char (*l) || l[1] != END_OF_INSN)
1057 && current_templates
1058 && (current_templates->start->opcode_modifier & IsPrefix))
1060 /* If we are in 16-bit mode, do not allow addr16 or data16.
1061 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1062 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1063 && (((current_templates->start->opcode_modifier & Size32) != 0)
1064 ^ flag_16bit_code))
1066 as_bad (_("redundant %s prefix"),
1067 current_templates->start->name);
1068 return;
1070 /* Add prefix, checking for repeated prefixes. */
1071 switch (add_prefix (current_templates->start->base_opcode))
1073 case 0:
1074 return;
1075 case 2:
1076 expecting_string_instruction =
1077 current_templates->start->name;
1078 break;
1080 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1081 token_start = ++l;
1083 else
1084 break;
1087 if (!current_templates)
1089 /* See if we can get a match by trimming off a suffix. */
1090 switch (mnem_p[-1])
1092 case WORD_MNEM_SUFFIX:
1093 case BYTE_MNEM_SUFFIX:
1094 case SHORT_MNEM_SUFFIX:
1095 case LONG_MNEM_SUFFIX:
1096 i.suffix = mnem_p[-1];
1097 mnem_p[-1] = '\0';
1098 current_templates = hash_find (op_hash, mnemonic);
1099 break;
1101 /* Intel Syntax */
1102 case DWORD_MNEM_SUFFIX:
1103 if (intel_syntax)
1105 i.suffix = mnem_p[-1];
1106 mnem_p[-1] = '\0';
1107 current_templates = hash_find (op_hash, mnemonic);
1108 break;
1111 if (!current_templates)
1113 as_bad (_("no such 386 instruction: `%s'"), token_start);
1114 return;
1118 /* check for rep/repne without a string instruction */
1119 if (expecting_string_instruction
1120 && !(current_templates->start->opcode_modifier & IsString))
1122 as_bad (_("expecting string instruction after `%s'"),
1123 expecting_string_instruction);
1124 return;
1127 /* There may be operands to parse. */
1128 if (*l != END_OF_INSN)
1130 /* parse operands */
1132 /* 1 if operand is pending after ','. */
1133 unsigned int expecting_operand = 0;
1135 /* Non-zero if operand parens not balanced. */
1136 unsigned int paren_not_balanced;
1140 /* skip optional white space before operand */
1141 if (is_space_char (*l))
1142 ++l;
1143 if (!is_operand_char (*l) && *l != END_OF_INSN)
1145 as_bad (_("invalid character %s before operand %d"),
1146 output_invalid (*l),
1147 i.operands + 1);
1148 return;
1150 token_start = l; /* after white space */
1151 paren_not_balanced = 0;
1152 while (paren_not_balanced || *l != ',')
1154 if (*l == END_OF_INSN)
1156 if (paren_not_balanced)
1158 if (!intel_syntax)
1159 as_bad (_("unbalanced parenthesis in operand %d."),
1160 i.operands + 1);
1161 else
1162 as_bad (_("unbalanced brackets in operand %d."),
1163 i.operands + 1);
1164 return;
1166 else
1167 break; /* we are done */
1169 else if (!is_operand_char (*l) && !is_space_char (*l))
1171 as_bad (_("invalid character %s in operand %d"),
1172 output_invalid (*l),
1173 i.operands + 1);
1174 return;
1176 if (!intel_syntax)
1178 if (*l == '(')
1179 ++paren_not_balanced;
1180 if (*l == ')')
1181 --paren_not_balanced;
1183 else
1185 if (*l == '[')
1186 ++paren_not_balanced;
1187 if (*l == ']')
1188 --paren_not_balanced;
1190 l++;
1192 if (l != token_start)
1193 { /* yes, we've read in another operand */
1194 unsigned int operand_ok;
1195 this_operand = i.operands++;
1196 if (i.operands > MAX_OPERANDS)
1198 as_bad (_("spurious operands; (%d operands/instruction max)"),
1199 MAX_OPERANDS);
1200 return;
1202 /* now parse operand adding info to 'i' as we go along */
1203 END_STRING_AND_SAVE (l);
1205 if (intel_syntax)
1206 operand_ok = i386_intel_operand (token_start, intel_float_operand (mnemonic));
1207 else
1208 operand_ok = i386_operand (token_start);
1210 RESTORE_END_STRING (l); /* restore old contents */
1211 if (!operand_ok)
1212 return;
1214 else
1216 if (expecting_operand)
1218 expecting_operand_after_comma:
1219 as_bad (_("expecting operand after ','; got nothing"));
1220 return;
1222 if (*l == ',')
1224 as_bad (_("expecting operand before ','; got nothing"));
1225 return;
1229 /* now *l must be either ',' or END_OF_INSN */
1230 if (*l == ',')
1232 if (*++l == END_OF_INSN)
1233 { /* just skip it, if it's \n complain */
1234 goto expecting_operand_after_comma;
1236 expecting_operand = 1;
1239 while (*l != END_OF_INSN); /* until we get end of insn */
1243 /* Now we've parsed the mnemonic into a set of templates, and have the
1244 operands at hand.
1246 Next, we find a template that matches the given insn,
1247 making sure the overlap of the given operands types is consistent
1248 with the template operand types. */
1250 #define MATCH(overlap, given, template) \
1251 ((overlap & ~JumpAbsolute) \
1252 && ((given) & (BaseIndex|JumpAbsolute)) == ((overlap) & (BaseIndex|JumpAbsolute)))
1254 /* If given types r0 and r1 are registers they must be of the same type
1255 unless the expected operand type register overlap is null.
1256 Note that Acc in a template matches every size of reg. */
1257 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
1258 ( ((g0) & Reg) == 0 || ((g1) & Reg) == 0 || \
1259 ((g0) & Reg) == ((g1) & Reg) || \
1260 ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
1263 register unsigned int overlap0, overlap1;
1264 unsigned int overlap2;
1265 unsigned int found_reverse_match;
1266 int suffix_check;
1268 /* All intel opcodes have reversed operands except for "bound" and
1269 "enter". We also don't reverse intersegment "jmp" and "call"
1270 instructions with 2 immediate operands so that the immediate segment
1271 precedes the offset, as it does when in AT&T mode. "enter" and the
1272 intersegment "jmp" and "call" instructions are the only ones that
1273 have two immediate operands. */
1274 if (intel_syntax && i.operands > 1
1275 && (strcmp (mnemonic, "bound") != 0)
1276 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1278 union i386_op temp_op;
1279 unsigned int temp_type;
1280 int xchg1 = 0;
1281 int xchg2 = 0;
1283 if (i.operands == 2)
1285 xchg1 = 0;
1286 xchg2 = 1;
1288 else if (i.operands == 3)
1290 xchg1 = 0;
1291 xchg2 = 2;
1293 temp_type = i.types[xchg2];
1294 i.types[xchg2] = i.types[xchg1];
1295 i.types[xchg1] = temp_type;
1296 temp_op = i.op[xchg2];
1297 i.op[xchg2] = i.op[xchg1];
1298 i.op[xchg1] = temp_op;
1300 if (i.mem_operands == 2)
1302 const seg_entry *temp_seg;
1303 temp_seg = i.seg[0];
1304 i.seg[0] = i.seg[1];
1305 i.seg[1] = temp_seg;
1309 if (i.imm_operands)
1311 /* Try to ensure constant immediates are represented in the smallest
1312 opcode possible. */
1313 char guess_suffix = 0;
1314 int op;
1316 if (i.suffix)
1317 guess_suffix = i.suffix;
1318 else if (i.reg_operands)
1320 /* Figure out a suffix from the last register operand specified.
1321 We can't do this properly yet, ie. excluding InOutPortReg,
1322 but the following works for instructions with immediates.
1323 In any case, we can't set i.suffix yet. */
1324 for (op = i.operands; --op >= 0; )
1325 if (i.types[op] & Reg)
1327 if (i.types[op] & Reg8)
1328 guess_suffix = BYTE_MNEM_SUFFIX;
1329 else if (i.types[op] & Reg16)
1330 guess_suffix = WORD_MNEM_SUFFIX;
1331 break;
1334 for (op = i.operands; --op >= 0; )
1335 if ((i.types[op] & Imm)
1336 && i.op[op].imms->X_op == O_constant)
1338 /* If a suffix is given, this operand may be shortened. */
1339 switch (guess_suffix)
1341 case WORD_MNEM_SUFFIX:
1342 i.types[op] |= Imm16;
1343 break;
1344 case BYTE_MNEM_SUFFIX:
1345 i.types[op] |= Imm16 | Imm8 | Imm8S;
1346 break;
1349 /* If this operand is at most 16 bits, convert it to a
1350 signed 16 bit number before trying to see whether it will
1351 fit in an even smaller size. This allows a 16-bit operand
1352 such as $0xffe0 to be recognised as within Imm8S range. */
1353 if ((i.types[op] & Imm16)
1354 && (i.op[op].imms->X_add_number & ~(offsetT)0xffff) == 0)
1356 i.op[op].imms->X_add_number =
1357 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
1359 i.types[op] |= smallest_imm_type ((long) i.op[op].imms->X_add_number);
1363 overlap0 = 0;
1364 overlap1 = 0;
1365 overlap2 = 0;
1366 found_reverse_match = 0;
1367 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
1368 ? No_bSuf
1369 : (i.suffix == WORD_MNEM_SUFFIX
1370 ? No_wSuf
1371 : (i.suffix == SHORT_MNEM_SUFFIX
1372 ? No_sSuf
1373 : (i.suffix == LONG_MNEM_SUFFIX
1374 ? No_lSuf
1375 : (i.suffix == DWORD_MNEM_SUFFIX
1376 ? No_dSuf
1377 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX ? No_xSuf : 0))))));
1379 for (t = current_templates->start;
1380 t < current_templates->end;
1381 t++)
1383 /* Must have right number of operands. */
1384 if (i.operands != t->operands)
1385 continue;
1387 /* Check the suffix, except for some instructions in intel mode. */
1388 if ((t->opcode_modifier & suffix_check)
1389 && !(intel_syntax
1390 && t->base_opcode == 0xd9
1391 && (t->extension_opcode == 5 /* 0xd9,5 "fldcw" */
1392 || t->extension_opcode == 7))) /* 0xd9,7 "f{n}stcw" */
1393 continue;
1395 else if (!t->operands)
1396 break; /* 0 operands always matches */
1398 overlap0 = i.types[0] & t->operand_types[0];
1399 switch (t->operands)
1401 case 1:
1402 if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
1403 continue;
1404 break;
1405 case 2:
1406 case 3:
1407 overlap1 = i.types[1] & t->operand_types[1];
1408 if (!MATCH (overlap0, i.types[0], t->operand_types[0])
1409 || !MATCH (overlap1, i.types[1], t->operand_types[1])
1410 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
1411 t->operand_types[0],
1412 overlap1, i.types[1],
1413 t->operand_types[1]))
1416 /* check if other direction is valid ... */
1417 if ((t->opcode_modifier & (D|FloatD)) == 0)
1418 continue;
1420 /* try reversing direction of operands */
1421 overlap0 = i.types[0] & t->operand_types[1];
1422 overlap1 = i.types[1] & t->operand_types[0];
1423 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
1424 || !MATCH (overlap1, i.types[1], t->operand_types[0])
1425 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
1426 t->operand_types[1],
1427 overlap1, i.types[1],
1428 t->operand_types[0]))
1430 /* does not match either direction */
1431 continue;
1433 /* found_reverse_match holds which of D or FloatDR
1434 we've found. */
1435 found_reverse_match = t->opcode_modifier & (D|FloatDR);
1436 break;
1438 /* found a forward 2 operand match here */
1439 if (t->operands == 3)
1441 /* Here we make use of the fact that there are no
1442 reverse match 3 operand instructions, and all 3
1443 operand instructions only need to be checked for
1444 register consistency between operands 2 and 3. */
1445 overlap2 = i.types[2] & t->operand_types[2];
1446 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
1447 || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
1448 t->operand_types[1],
1449 overlap2, i.types[2],
1450 t->operand_types[2]))
1452 continue;
1454 /* found either forward/reverse 2 or 3 operand match here:
1455 slip through to break */
1457 break; /* we've found a match; break out of loop */
1458 } /* for (t = ... */
1459 if (t == current_templates->end)
1460 { /* we found no match */
1461 as_bad (_("suffix or operands invalid for `%s'"),
1462 current_templates->start->name);
1463 return;
1466 if (!intel_syntax
1467 && (i.types[0] & JumpAbsolute) != (t->operand_types[0] & JumpAbsolute))
1469 as_warn (_("indirect %s without `*'"), t->name);
1472 if ((t->opcode_modifier & (IsPrefix|IgnoreSize)) == (IsPrefix|IgnoreSize))
1474 /* Warn them that a data or address size prefix doesn't affect
1475 assembly of the next line of code. */
1476 as_warn (_("stand-alone `%s' prefix"), t->name);
1479 /* Copy the template we found. */
1480 i.tm = *t;
1481 if (found_reverse_match)
1483 /* If we found a reverse match we must alter the opcode
1484 direction bit. found_reverse_match holds bits to change
1485 (different for int & float insns). */
1487 i.tm.base_opcode ^= found_reverse_match;
1489 i.tm.operand_types[0] = t->operand_types[1];
1490 i.tm.operand_types[1] = t->operand_types[0];
1493 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1494 if (SYSV386_COMPAT
1495 && intel_syntax
1496 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1497 i.tm.base_opcode ^= FloatR;
1499 if (i.tm.opcode_modifier & FWait)
1500 if (! add_prefix (FWAIT_OPCODE))
1501 return;
1503 /* Check string instruction segment overrides */
1504 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1506 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
1507 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
1509 if (i.seg[0] != NULL && i.seg[0] != &es)
1511 as_bad (_("`%s' operand %d must use `%%es' segment"),
1512 i.tm.name,
1513 mem_op + 1);
1514 return;
1516 /* There's only ever one segment override allowed per instruction.
1517 This instruction possibly has a legal segment override on the
1518 second operand, so copy the segment to where non-string
1519 instructions store it, allowing common code. */
1520 i.seg[0] = i.seg[1];
1522 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
1524 if (i.seg[1] != NULL && i.seg[1] != &es)
1526 as_bad (_("`%s' operand %d must use `%%es' segment"),
1527 i.tm.name,
1528 mem_op + 2);
1529 return;
1534 /* If matched instruction specifies an explicit instruction mnemonic
1535 suffix, use it. */
1536 if (i.tm.opcode_modifier & (Size16 | Size32))
1538 if (i.tm.opcode_modifier & Size16)
1539 i.suffix = WORD_MNEM_SUFFIX;
1540 else
1541 i.suffix = LONG_MNEM_SUFFIX;
1543 else if (i.reg_operands)
1545 /* If there's no instruction mnemonic suffix we try to invent one
1546 based on register operands. */
1547 if (!i.suffix)
1549 /* We take i.suffix from the last register operand specified,
1550 Destination register type is more significant than source
1551 register type. */
1552 int op;
1553 for (op = i.operands; --op >= 0; )
1554 if ((i.types[op] & Reg)
1555 && !(i.tm.operand_types[op] & InOutPortReg))
1557 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
1558 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
1559 LONG_MNEM_SUFFIX);
1560 break;
1563 else if (i.suffix == BYTE_MNEM_SUFFIX)
1565 int op;
1566 for (op = i.operands; --op >= 0; )
1568 /* If this is an eight bit register, it's OK. If it's
1569 the 16 or 32 bit version of an eight bit register,
1570 we will just use the low portion, and that's OK too. */
1571 if (i.types[op] & Reg8)
1572 continue;
1574 /* movzx and movsx should not generate this warning. */
1575 if (intel_syntax
1576 && (i.tm.base_opcode == 0xfb7
1577 || i.tm.base_opcode == 0xfb6
1578 || i.tm.base_opcode == 0xfbe
1579 || i.tm.base_opcode == 0xfbf))
1580 continue;
1582 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4
1583 #if 0
1584 /* Check that the template allows eight bit regs
1585 This kills insns such as `orb $1,%edx', which
1586 maybe should be allowed. */
1587 && (i.tm.operand_types[op] & (Reg8|InOutPortReg))
1588 #endif
1591 #if REGISTER_WARNINGS
1592 if ((i.tm.operand_types[op] & InOutPortReg) == 0)
1593 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1594 (i.op[op].regs - (i.types[op] & Reg16 ? 8 : 16))->reg_name,
1595 i.op[op].regs->reg_name,
1596 i.suffix);
1597 #endif
1598 continue;
1600 /* Any other register is bad */
1601 if (i.types[op] & (Reg | RegMMX | RegXMM
1602 | SReg2 | SReg3
1603 | Control | Debug | Test
1604 | FloatReg | FloatAcc))
1606 as_bad (_("`%%%s' not allowed with `%s%c'"),
1607 i.op[op].regs->reg_name,
1608 i.tm.name,
1609 i.suffix);
1610 return;
1614 else if (i.suffix == LONG_MNEM_SUFFIX)
1616 int op;
1617 for (op = i.operands; --op >= 0; )
1618 /* Reject eight bit registers, except where the template
1619 requires them. (eg. movzb) */
1620 if ((i.types[op] & Reg8) != 0
1621 && (i.tm.operand_types[op] & (Reg16|Reg32|Acc)) != 0)
1623 as_bad (_("`%%%s' not allowed with `%s%c'"),
1624 i.op[op].regs->reg_name,
1625 i.tm.name,
1626 i.suffix);
1627 return;
1629 #if REGISTER_WARNINGS
1630 /* Warn if the e prefix on a general reg is missing. */
1631 else if ((i.types[op] & Reg16) != 0
1632 && (i.tm.operand_types[op] & (Reg32|Acc)) != 0)
1634 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1635 (i.op[op].regs + 8)->reg_name,
1636 i.op[op].regs->reg_name,
1637 i.suffix);
1639 #endif
1641 else if (i.suffix == WORD_MNEM_SUFFIX)
1643 int op;
1644 for (op = i.operands; --op >= 0; )
1645 /* Reject eight bit registers, except where the template
1646 requires them. (eg. movzb) */
1647 if ((i.types[op] & Reg8) != 0
1648 && (i.tm.operand_types[op] & (Reg16|Reg32|Acc)) != 0)
1650 as_bad (_("`%%%s' not allowed with `%s%c'"),
1651 i.op[op].regs->reg_name,
1652 i.tm.name,
1653 i.suffix);
1654 return;
1656 #if REGISTER_WARNINGS
1657 /* Warn if the e prefix on a general reg is present. */
1658 else if ((i.types[op] & Reg32) != 0
1659 && (i.tm.operand_types[op] & (Reg16|Acc)) != 0)
1661 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
1662 (i.op[op].regs - 8)->reg_name,
1663 i.op[op].regs->reg_name,
1664 i.suffix);
1666 #endif
1668 else
1669 abort();
1671 else if ((i.tm.opcode_modifier & DefaultSize) && !i.suffix)
1673 i.suffix = stackop_size;
1676 /* Make still unresolved immediate matches conform to size of immediate
1677 given in i.suffix. Note: overlap2 cannot be an immediate! */
1678 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
1679 && overlap0 != Imm8 && overlap0 != Imm8S
1680 && overlap0 != Imm16 && overlap0 != Imm32)
1682 if (i.suffix)
1684 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
1685 (i.suffix == WORD_MNEM_SUFFIX ? Imm16 : Imm32));
1687 else if (overlap0 == (Imm16 | Imm32))
1689 overlap0 =
1690 (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32;
1692 else
1694 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
1695 return;
1698 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
1699 && overlap1 != Imm8 && overlap1 != Imm8S
1700 && overlap1 != Imm16 && overlap1 != Imm32)
1702 if (i.suffix)
1704 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
1705 (i.suffix == WORD_MNEM_SUFFIX ? Imm16 : Imm32));
1707 else if (overlap1 == (Imm16 | Imm32))
1709 overlap1 =
1710 (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32;
1712 else
1714 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
1715 return;
1718 assert ((overlap2 & Imm) == 0);
1720 i.types[0] = overlap0;
1721 if (overlap0 & ImplicitRegister)
1722 i.reg_operands--;
1723 if (overlap0 & Imm1)
1724 i.imm_operands = 0; /* kludge for shift insns */
1726 i.types[1] = overlap1;
1727 if (overlap1 & ImplicitRegister)
1728 i.reg_operands--;
1730 i.types[2] = overlap2;
1731 if (overlap2 & ImplicitRegister)
1732 i.reg_operands--;
1734 /* Finalize opcode. First, we change the opcode based on the operand
1735 size given by i.suffix: We need not change things for byte insns. */
1737 if (!i.suffix && (i.tm.opcode_modifier & W))
1739 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
1740 return;
1743 /* For movzx and movsx, need to check the register type */
1744 if (intel_syntax
1745 && (i.tm.base_opcode == 0xfb6 || i.tm.base_opcode == 0xfbe))
1746 if (i.suffix && i.suffix == BYTE_MNEM_SUFFIX)
1748 unsigned int prefix = DATA_PREFIX_OPCODE;
1750 if ((i.op[1].regs->reg_type & Reg16) != 0)
1751 if (!add_prefix (prefix))
1752 return;
1755 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
1757 /* It's not a byte, select word/dword operation. */
1758 if (i.tm.opcode_modifier & W)
1760 if (i.tm.opcode_modifier & ShortForm)
1761 i.tm.base_opcode |= 8;
1762 else
1763 i.tm.base_opcode |= 1;
1765 /* Now select between word & dword operations via the operand
1766 size prefix, except for instructions that will ignore this
1767 prefix anyway. */
1768 if (((intel_syntax && (i.suffix == DWORD_MNEM_SUFFIX))
1769 || i.suffix == LONG_MNEM_SUFFIX) == flag_16bit_code
1770 && !(i.tm.opcode_modifier & IgnoreSize))
1772 unsigned int prefix = DATA_PREFIX_OPCODE;
1773 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
1774 prefix = ADDR_PREFIX_OPCODE;
1776 if (! add_prefix (prefix))
1777 return;
1779 /* Size floating point instruction. */
1780 if (i.suffix == LONG_MNEM_SUFFIX
1781 || (intel_syntax && i.suffix == DWORD_MNEM_SUFFIX))
1783 if (i.tm.opcode_modifier & FloatMF)
1784 i.tm.base_opcode ^= 4;
1788 if (i.tm.opcode_modifier & ImmExt)
1790 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1791 opcode suffix which is coded in the same place as an 8-bit
1792 immediate field would be. Here we fake an 8-bit immediate
1793 operand from the opcode suffix stored in tm.extension_opcode. */
1795 expressionS *exp;
1797 assert(i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1799 exp = &im_expressions[i.imm_operands++];
1800 i.op[i.operands].imms = exp;
1801 i.types[i.operands++] = Imm8;
1802 exp->X_op = O_constant;
1803 exp->X_add_number = i.tm.extension_opcode;
1804 i.tm.extension_opcode = None;
1807 /* For insns with operands there are more diddles to do to the opcode. */
1808 if (i.operands)
1810 /* Default segment register this instruction will use
1811 for memory accesses. 0 means unknown.
1812 This is only for optimizing out unnecessary segment overrides. */
1813 const seg_entry *default_seg = 0;
1815 /* The imul $imm, %reg instruction is converted into
1816 imul $imm, %reg, %reg, and the clr %reg instruction
1817 is converted into xor %reg, %reg. */
1818 if (i.tm.opcode_modifier & regKludge)
1820 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
1821 /* Pretend we saw the extra register operand. */
1822 assert (i.op[first_reg_op+1].regs == 0);
1823 i.op[first_reg_op+1].regs = i.op[first_reg_op].regs;
1824 i.types[first_reg_op+1] = i.types[first_reg_op];
1825 i.reg_operands = 2;
1828 if (i.tm.opcode_modifier & ShortForm)
1830 /* The register or float register operand is in operand 0 or 1. */
1831 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
1832 /* Register goes in low 3 bits of opcode. */
1833 i.tm.base_opcode |= i.op[op].regs->reg_num;
1834 if ((i.tm.opcode_modifier & Ugh) != 0)
1836 /* Warn about some common errors, but press on regardless.
1837 The first case can be generated by gcc (<= 2.8.1). */
1838 if (i.operands == 2)
1840 /* reversed arguments on faddp, fsubp, etc. */
1841 as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
1842 i.op[1].regs->reg_name,
1843 i.op[0].regs->reg_name);
1845 else
1847 /* extraneous `l' suffix on fp insn */
1848 as_warn (_("translating to `%s %%%s'"), i.tm.name,
1849 i.op[0].regs->reg_name);
1853 else if (i.tm.opcode_modifier & Modrm)
1855 /* The opcode is completed (modulo i.tm.extension_opcode which
1856 must be put into the modrm byte).
1857 Now, we make the modrm & index base bytes based on all the
1858 info we've collected. */
1860 /* i.reg_operands MUST be the number of real register operands;
1861 implicit registers do not count. */
1862 if (i.reg_operands == 2)
1864 unsigned int source, dest;
1865 source = ((i.types[0]
1866 & (Reg | RegMMX | RegXMM
1867 | SReg2 | SReg3
1868 | Control | Debug | Test))
1869 ? 0 : 1);
1870 dest = source + 1;
1872 i.rm.mode = 3;
1873 /* One of the register operands will be encoded in the
1874 i.tm.reg field, the other in the combined i.tm.mode
1875 and i.tm.regmem fields. If no form of this
1876 instruction supports a memory destination operand,
1877 then we assume the source operand may sometimes be
1878 a memory operand and so we need to store the
1879 destination in the i.rm.reg field. */
1880 if ((i.tm.operand_types[dest] & AnyMem) == 0)
1882 i.rm.reg = i.op[dest].regs->reg_num;
1883 i.rm.regmem = i.op[source].regs->reg_num;
1885 else
1887 i.rm.reg = i.op[source].regs->reg_num;
1888 i.rm.regmem = i.op[dest].regs->reg_num;
1891 else
1892 { /* if it's not 2 reg operands... */
1893 if (i.mem_operands)
1895 unsigned int fake_zero_displacement = 0;
1896 unsigned int op = ((i.types[0] & AnyMem)
1898 : (i.types[1] & AnyMem) ? 1 : 2);
1900 default_seg = &ds;
1902 if (! i.base_reg)
1904 i.rm.mode = 0;
1905 if (! i.disp_operands)
1906 fake_zero_displacement = 1;
1907 if (! i.index_reg)
1909 /* Operand is just <disp> */
1910 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0))
1912 i.rm.regmem = NO_BASE_REGISTER_16;
1913 i.types[op] &= ~Disp;
1914 i.types[op] |= Disp16;
1916 else
1918 i.rm.regmem = NO_BASE_REGISTER;
1919 i.types[op] &= ~Disp;
1920 i.types[op] |= Disp32;
1923 else /* ! i.base_reg && i.index_reg */
1925 i.sib.index = i.index_reg->reg_num;
1926 i.sib.base = NO_BASE_REGISTER;
1927 i.sib.scale = i.log2_scale_factor;
1928 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1929 i.types[op] &= ~Disp;
1930 i.types[op] |= Disp32; /* Must be 32 bit */
1933 else if (i.base_reg->reg_type & Reg16)
1935 switch (i.base_reg->reg_num)
1937 case 3: /* (%bx) */
1938 if (! i.index_reg)
1939 i.rm.regmem = 7;
1940 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
1941 i.rm.regmem = i.index_reg->reg_num - 6;
1942 break;
1943 case 5: /* (%bp) */
1944 default_seg = &ss;
1945 if (! i.index_reg)
1947 i.rm.regmem = 6;
1948 if ((i.types[op] & Disp) == 0)
1950 /* fake (%bp) into 0(%bp) */
1951 i.types[op] |= Disp8;
1952 fake_zero_displacement = 1;
1955 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
1956 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
1957 break;
1958 default: /* (%si) -> 4 or (%di) -> 5 */
1959 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
1961 i.rm.mode = mode_from_disp_size (i.types[op]);
1963 else /* i.base_reg and 32 bit mode */
1965 i.rm.regmem = i.base_reg->reg_num;
1966 i.sib.base = i.base_reg->reg_num;
1967 if (i.base_reg->reg_num == EBP_REG_NUM)
1969 default_seg = &ss;
1970 if (i.disp_operands == 0)
1972 fake_zero_displacement = 1;
1973 i.types[op] |= Disp8;
1976 else if (i.base_reg->reg_num == ESP_REG_NUM)
1978 default_seg = &ss;
1980 i.sib.scale = i.log2_scale_factor;
1981 if (! i.index_reg)
1983 /* <disp>(%esp) becomes two byte modrm
1984 with no index register. We've already
1985 stored the code for esp in i.rm.regmem
1986 ie. ESCAPE_TO_TWO_BYTE_ADDRESSING. Any
1987 base register besides %esp will not use
1988 the extra modrm byte. */
1989 i.sib.index = NO_INDEX_REGISTER;
1990 #if ! SCALE1_WHEN_NO_INDEX
1991 /* Another case where we force the second
1992 modrm byte. */
1993 if (i.log2_scale_factor)
1994 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1995 #endif
1997 else
1999 i.sib.index = i.index_reg->reg_num;
2000 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2002 i.rm.mode = mode_from_disp_size (i.types[op]);
2005 if (fake_zero_displacement)
2007 /* Fakes a zero displacement assuming that i.types[op]
2008 holds the correct displacement size. */
2009 expressionS *exp;
2011 assert (i.op[op].disps == 0);
2012 exp = &disp_expressions[i.disp_operands++];
2013 i.op[op].disps = exp;
2014 exp->X_op = O_constant;
2015 exp->X_add_number = 0;
2016 exp->X_add_symbol = (symbolS *) 0;
2017 exp->X_op_symbol = (symbolS *) 0;
2021 /* Fill in i.rm.reg or i.rm.regmem field with register
2022 operand (if any) based on i.tm.extension_opcode.
2023 Again, we must be careful to make sure that
2024 segment/control/debug/test/MMX registers are coded
2025 into the i.rm.reg field. */
2026 if (i.reg_operands)
2028 unsigned int op =
2029 ((i.types[0]
2030 & (Reg | RegMMX | RegXMM
2031 | SReg2 | SReg3
2032 | Control | Debug | Test))
2034 : ((i.types[1]
2035 & (Reg | RegMMX | RegXMM
2036 | SReg2 | SReg3
2037 | Control | Debug | Test))
2039 : 2));
2040 /* If there is an extension opcode to put here, the
2041 register number must be put into the regmem field. */
2042 if (i.tm.extension_opcode != None)
2043 i.rm.regmem = i.op[op].regs->reg_num;
2044 else
2045 i.rm.reg = i.op[op].regs->reg_num;
2047 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
2048 we must set it to 3 to indicate this is a register
2049 operand in the regmem field. */
2050 if (!i.mem_operands)
2051 i.rm.mode = 3;
2054 /* Fill in i.rm.reg field with extension opcode (if any). */
2055 if (i.tm.extension_opcode != None)
2056 i.rm.reg = i.tm.extension_opcode;
2059 else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2061 if (i.tm.base_opcode == POP_SEG_SHORT && i.op[0].regs->reg_num == 1)
2063 as_bad (_("you can't `pop %%cs'"));
2064 return;
2066 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2068 else if ((i.tm.base_opcode & ~(D|W)) == MOV_AX_DISP32)
2070 default_seg = &ds;
2072 else if ((i.tm.opcode_modifier & IsString) != 0)
2074 /* For the string instructions that allow a segment override
2075 on one of their operands, the default segment is ds. */
2076 default_seg = &ds;
2079 /* If a segment was explicitly specified,
2080 and the specified segment is not the default,
2081 use an opcode prefix to select it.
2082 If we never figured out what the default segment is,
2083 then default_seg will be zero at this point,
2084 and the specified segment prefix will always be used. */
2085 if ((i.seg[0]) && (i.seg[0] != default_seg))
2087 if (! add_prefix (i.seg[0]->seg_prefix))
2088 return;
2091 else if ((i.tm.opcode_modifier & Ugh) != 0)
2093 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
2094 as_warn (_("translating to `%sp'"), i.tm.name);
2098 /* Handle conversion of 'int $3' --> special int3 insn. */
2099 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
2101 i.tm.base_opcode = INT3_OPCODE;
2102 i.imm_operands = 0;
2105 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
2106 && i.op[0].disps->X_op == O_constant)
2108 /* Convert "jmp constant" (and "call constant") to a jump (call) to
2109 the absolute address given by the constant. Since ix86 jumps and
2110 calls are pc relative, we need to generate a reloc. */
2111 i.op[0].disps->X_add_symbol = &abs_symbol;
2112 i.op[0].disps->X_op = O_symbol;
2115 /* We are ready to output the insn. */
2117 register char *p;
2119 /* Output jumps. */
2120 if (i.tm.opcode_modifier & Jump)
2122 int size;
2123 int code16;
2124 int prefix;
2126 code16 = 0;
2127 if (flag_16bit_code)
2128 code16 = CODE16;
2130 prefix = 0;
2131 if (i.prefix[DATA_PREFIX])
2133 prefix = 1;
2134 i.prefixes -= 1;
2135 code16 ^= CODE16;
2138 size = 4;
2139 if (code16)
2140 size = 2;
2142 if (i.prefixes != 0 && !intel_syntax)
2143 as_warn (_("skipping prefixes on this instruction"));
2145 /* It's always a symbol; End frag & setup for relax.
2146 Make sure there is enough room in this frag for the largest
2147 instruction we may generate in md_convert_frag. This is 2
2148 bytes for the opcode and room for the prefix and largest
2149 displacement. */
2150 frag_grow (prefix + 2 + size);
2151 insn_size += prefix + 1;
2152 /* Prefix and 1 opcode byte go in fr_fix. */
2153 p = frag_more (prefix + 1);
2154 if (prefix)
2155 *p++ = DATA_PREFIX_OPCODE;
2156 *p = i.tm.base_opcode;
2157 /* 1 possible extra opcode + displacement go in fr_var. */
2158 frag_var (rs_machine_dependent,
2159 1 + size,
2161 ((unsigned char) *p == JUMP_PC_RELATIVE
2162 ? ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL) | code16
2163 : ENCODE_RELAX_STATE (COND_JUMP, SMALL) | code16),
2164 i.op[0].disps->X_add_symbol,
2165 i.op[0].disps->X_add_number,
2168 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
2170 int size;
2172 if (i.tm.opcode_modifier & JumpByte)
2174 /* This is a loop or jecxz type instruction. */
2175 size = 1;
2176 if (i.prefix[ADDR_PREFIX])
2178 insn_size += 1;
2179 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
2180 i.prefixes -= 1;
2183 else
2185 int code16;
2187 code16 = 0;
2188 if (flag_16bit_code)
2189 code16 = CODE16;
2191 if (i.prefix[DATA_PREFIX])
2193 insn_size += 1;
2194 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
2195 i.prefixes -= 1;
2196 code16 ^= CODE16;
2199 size = 4;
2200 if (code16)
2201 size = 2;
2204 if (i.prefixes != 0 && !intel_syntax)
2205 as_warn (_("skipping prefixes on this instruction"));
2207 if (fits_in_unsigned_byte (i.tm.base_opcode))
2209 insn_size += 1 + size;
2210 p = frag_more (1 + size);
2212 else
2214 /* opcode can be at most two bytes */
2215 insn_size += 2 + size;
2216 p = frag_more (2 + size);
2217 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2219 *p++ = i.tm.base_opcode & 0xff;
2221 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2222 i.op[0].disps, 1, reloc (size, 1, i.disp_reloc[0]));
2224 else if (i.tm.opcode_modifier & JumpInterSegment)
2226 int size;
2227 int prefix;
2228 int code16;
2230 code16 = 0;
2231 if (flag_16bit_code)
2232 code16 = CODE16;
2234 prefix = 0;
2235 if (i.prefix[DATA_PREFIX])
2237 prefix = 1;
2238 i.prefixes -= 1;
2239 code16 ^= CODE16;
2242 size = 4;
2243 if (code16)
2244 size = 2;
2246 if (i.prefixes != 0 && !intel_syntax)
2247 as_warn (_("skipping prefixes on this instruction"));
2249 insn_size += prefix + 1 + 2 + size; /* 1 opcode; 2 segment; offset */
2250 p = frag_more (prefix + 1 + 2 + size);
2251 if (prefix)
2252 *p++ = DATA_PREFIX_OPCODE;
2253 *p++ = i.tm.base_opcode;
2254 if (i.op[1].imms->X_op == O_constant)
2256 long n = (long) i.op[1].imms->X_add_number;
2258 if (size == 2
2259 && !fits_in_unsigned_word (n)
2260 && !fits_in_signed_word (n))
2262 as_bad (_("16-bit jump out of range"));
2263 return;
2265 md_number_to_chars (p, (valueT) n, size);
2267 else
2268 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2269 i.op[1].imms, 0, reloc (size, 0, i.disp_reloc[0]));
2270 if (i.op[0].imms->X_op != O_constant)
2271 as_bad (_("can't handle non absolute segment in `%s'"),
2272 i.tm.name);
2273 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
2275 else
2277 /* Output normal instructions here. */
2278 unsigned char *q;
2280 /* The prefix bytes. */
2281 for (q = i.prefix;
2282 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
2283 q++)
2285 if (*q)
2287 insn_size += 1;
2288 p = frag_more (1);
2289 md_number_to_chars (p, (valueT) *q, 1);
2293 /* Now the opcode; be careful about word order here! */
2294 if (fits_in_unsigned_byte (i.tm.base_opcode))
2296 insn_size += 1;
2297 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
2299 else if (fits_in_unsigned_word (i.tm.base_opcode))
2301 insn_size += 2;
2302 p = frag_more (2);
2303 /* put out high byte first: can't use md_number_to_chars! */
2304 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2305 *p = i.tm.base_opcode & 0xff;
2307 else
2308 { /* opcode is either 3 or 4 bytes */
2309 if (i.tm.base_opcode & 0xff000000)
2311 insn_size += 4;
2312 p = frag_more (4);
2313 *p++ = (i.tm.base_opcode >> 24) & 0xff;
2315 else
2317 insn_size += 3;
2318 p = frag_more (3);
2320 *p++ = (i.tm.base_opcode >> 16) & 0xff;
2321 *p++ = (i.tm.base_opcode >> 8) & 0xff;
2322 *p = (i.tm.base_opcode) & 0xff;
2325 /* Now the modrm byte and sib byte (if present). */
2326 if (i.tm.opcode_modifier & Modrm)
2328 insn_size += 1;
2329 p = frag_more (1);
2330 md_number_to_chars (p,
2331 (valueT) (i.rm.regmem << 0
2332 | i.rm.reg << 3
2333 | i.rm.mode << 6),
2335 /* If i.rm.regmem == ESP (4)
2336 && i.rm.mode != (Register mode)
2337 && not 16 bit
2338 ==> need second modrm byte. */
2339 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
2340 && i.rm.mode != 3
2341 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
2343 insn_size += 1;
2344 p = frag_more (1);
2345 md_number_to_chars (p,
2346 (valueT) (i.sib.base << 0
2347 | i.sib.index << 3
2348 | i.sib.scale << 6),
2353 if (i.disp_operands)
2355 register unsigned int n;
2357 for (n = 0; n < i.operands; n++)
2359 if (i.types[n] & Disp)
2361 if (i.op[n].disps->X_op == O_constant)
2363 int size = 4;
2364 long val = (long) i.op[n].disps->X_add_number;
2366 if (i.types[n] & (Disp8 | Disp16))
2368 long mask;
2370 size = 2;
2371 mask = ~ (long) 0xffff;
2372 if (i.types[n] & Disp8)
2374 size = 1;
2375 mask = ~ (long) 0xff;
2378 if ((val & mask) != 0 && (val & mask) != mask)
2379 as_warn (_("%ld shortened to %ld"),
2380 val, val & ~mask);
2382 insn_size += size;
2383 p = frag_more (size);
2384 md_number_to_chars (p, (valueT) val, size);
2386 else
2388 int size = 4;
2390 if (i.types[n] & Disp16)
2391 size = 2;
2393 insn_size += size;
2394 p = frag_more (size);
2395 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2396 i.op[n].disps, 0,
2397 reloc (size, 0, i.disp_reloc[n]));
2401 } /* end displacement output */
2403 /* output immediate */
2404 if (i.imm_operands)
2406 register unsigned int n;
2408 for (n = 0; n < i.operands; n++)
2410 if (i.types[n] & Imm)
2412 if (i.op[n].imms->X_op == O_constant)
2414 int size = 4;
2415 long val = (long) i.op[n].imms->X_add_number;
2417 if (i.types[n] & (Imm8 | Imm8S | Imm16))
2419 long mask;
2421 size = 2;
2422 mask = ~ (long) 0xffff;
2423 if (i.types[n] & (Imm8 | Imm8S))
2425 size = 1;
2426 mask = ~ (long) 0xff;
2428 if ((val & mask) != 0 && (val & mask) != mask)
2429 as_warn (_("%ld shortened to %ld"),
2430 val, val & ~mask);
2432 insn_size += size;
2433 p = frag_more (size);
2434 md_number_to_chars (p, (valueT) val, size);
2436 else
2437 { /* not absolute_section */
2438 /* Need a 32-bit fixup (don't support 8bit
2439 non-absolute imms). Try to support other
2440 sizes ... */
2441 #ifdef BFD_ASSEMBLER
2442 enum bfd_reloc_code_real reloc_type;
2443 #else
2444 int reloc_type;
2445 #endif
2446 int size = 4;
2448 if (i.types[n] & Imm16)
2449 size = 2;
2450 else if (i.types[n] & (Imm8 | Imm8S))
2451 size = 1;
2453 insn_size += size;
2454 p = frag_more (size);
2455 reloc_type = reloc (size, 0, i.disp_reloc[0]);
2456 #ifdef BFD_ASSEMBLER
2457 if (reloc_type == BFD_RELOC_32
2458 && GOT_symbol
2459 && GOT_symbol == i.op[n].imms->X_add_symbol
2460 && (i.op[n].imms->X_op == O_symbol
2461 || (i.op[n].imms->X_op == O_add
2462 && ((symbol_get_value_expression
2463 (i.op[n].imms->X_op_symbol)->X_op)
2464 == O_subtract))))
2466 reloc_type = BFD_RELOC_386_GOTPC;
2467 i.op[n].imms->X_add_number += 3;
2469 #endif
2470 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2471 i.op[n].imms, 0, reloc_type);
2475 } /* end immediate output */
2478 #ifdef DEBUG386
2479 if (flag_debug)
2481 pi (line, &i);
2483 #endif /* DEBUG386 */
2487 static int i386_immediate PARAMS ((char *));
2489 static int
2490 i386_immediate (imm_start)
2491 char *imm_start;
2493 char *save_input_line_pointer;
2494 segT exp_seg = 0;
2495 expressionS * exp;
2497 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
2499 as_bad (_("only 1 or 2 immediate operands are allowed"));
2500 return 0;
2503 exp = &im_expressions[i.imm_operands++];
2504 i.op[this_operand].imms = exp;
2506 if (is_space_char (*imm_start))
2507 ++imm_start;
2509 save_input_line_pointer = input_line_pointer;
2510 input_line_pointer = imm_start;
2512 #ifndef LEX_AT
2515 * We can have operands of the form
2516 * <symbol>@GOTOFF+<nnn>
2517 * Take the easy way out here and copy everything
2518 * into a temporary buffer...
2520 register char *cp;
2522 cp = strchr (input_line_pointer, '@');
2523 if (cp != NULL)
2525 char *tmpbuf;
2526 int len = 0;
2527 int first;
2529 /* GOT relocations are not supported in 16 bit mode */
2530 if (flag_16bit_code)
2531 as_bad (_("GOT relocations not supported in 16 bit mode"));
2533 if (GOT_symbol == NULL)
2534 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
2536 if (strncmp (cp + 1, "PLT", 3) == 0)
2538 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2539 len = 3;
2541 else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
2543 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2544 len = 6;
2546 else if (strncmp (cp + 1, "GOT", 3) == 0)
2548 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2549 len = 3;
2551 else
2552 as_bad (_("bad reloc specifier in expression"));
2554 /* Replace the relocation token with ' ', so that errors like
2555 foo@GOTOFF1 will be detected. */
2556 first = cp - input_line_pointer;
2557 tmpbuf = (char *) alloca (strlen(input_line_pointer));
2558 memcpy (tmpbuf, input_line_pointer, first);
2559 tmpbuf[first] = ' ';
2560 strcpy (tmpbuf + first + 1, cp + 1 + len);
2561 input_line_pointer = tmpbuf;
2564 #endif
2566 exp_seg = expression (exp);
2568 SKIP_WHITESPACE ();
2569 if (*input_line_pointer)
2570 as_bad (_("ignoring junk `%s' after expression"), input_line_pointer);
2572 input_line_pointer = save_input_line_pointer;
2574 if (exp->X_op == O_absent || exp->X_op == O_big)
2576 /* missing or bad expr becomes absolute 0 */
2577 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
2578 imm_start);
2579 exp->X_op = O_constant;
2580 exp->X_add_number = 0;
2581 exp->X_add_symbol = (symbolS *) 0;
2582 exp->X_op_symbol = (symbolS *) 0;
2585 if (exp->X_op == O_constant)
2587 int bigimm = Imm32;
2588 if (flag_16bit_code ^ (i.prefix[DATA_PREFIX] != 0))
2589 bigimm = Imm16;
2590 i.types[this_operand] |= bigimm;
2592 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
2593 else if (
2594 #ifdef BFD_ASSEMBLER
2595 OUTPUT_FLAVOR == bfd_target_aout_flavour &&
2596 #endif
2597 exp_seg != text_section
2598 && exp_seg != data_section
2599 && exp_seg != bss_section
2600 && exp_seg != undefined_section
2601 #ifdef BFD_ASSEMBLER
2602 && !bfd_is_com_section (exp_seg)
2603 #endif
2606 #ifdef BFD_ASSEMBLER
2607 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
2608 #else
2609 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
2610 #endif
2611 return 0;
2613 #endif
2614 else
2616 /* This is an address. The size of the address will be
2617 determined later, depending on destination register,
2618 suffix, or the default for the section. We exclude
2619 Imm8S here so that `push $foo' and other instructions
2620 with an Imm8S form will use Imm16 or Imm32. */
2621 i.types[this_operand] |= (Imm8 | Imm16 | Imm32);
2624 return 1;
2627 static int i386_scale PARAMS ((char *));
2629 static int
2630 i386_scale (scale)
2631 char *scale;
2633 if (!isdigit (*scale))
2634 goto bad_scale;
2636 switch (*scale)
2638 case '0':
2639 case '1':
2640 i.log2_scale_factor = 0;
2641 break;
2642 case '2':
2643 i.log2_scale_factor = 1;
2644 break;
2645 case '4':
2646 i.log2_scale_factor = 2;
2647 break;
2648 case '8':
2649 i.log2_scale_factor = 3;
2650 break;
2651 default:
2652 bad_scale:
2653 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
2654 scale);
2655 return 0;
2657 if (i.log2_scale_factor != 0 && ! i.index_reg)
2659 as_warn (_("scale factor of %d without an index register"),
2660 1 << i.log2_scale_factor);
2661 #if SCALE1_WHEN_NO_INDEX
2662 i.log2_scale_factor = 0;
2663 #endif
2665 return 1;
2668 static int i386_displacement PARAMS ((char *, char *));
2670 static int
2671 i386_displacement (disp_start, disp_end)
2672 char *disp_start;
2673 char *disp_end;
2675 register expressionS *exp;
2676 segT exp_seg = 0;
2677 char *save_input_line_pointer;
2678 int bigdisp = Disp32;
2680 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0))
2681 bigdisp = Disp16;
2682 i.types[this_operand] |= bigdisp;
2684 exp = &disp_expressions[i.disp_operands];
2685 i.op[this_operand].disps = exp;
2686 i.disp_operands++;
2687 save_input_line_pointer = input_line_pointer;
2688 input_line_pointer = disp_start;
2689 END_STRING_AND_SAVE (disp_end);
2691 #ifndef GCC_ASM_O_HACK
2692 #define GCC_ASM_O_HACK 0
2693 #endif
2694 #if GCC_ASM_O_HACK
2695 END_STRING_AND_SAVE (disp_end + 1);
2696 if ((i.types[this_operand] & BaseIndex) != 0
2697 && displacement_string_end[-1] == '+')
2699 /* This hack is to avoid a warning when using the "o"
2700 constraint within gcc asm statements.
2701 For instance:
2703 #define _set_tssldt_desc(n,addr,limit,type) \
2704 __asm__ __volatile__ ( \
2705 "movw %w2,%0\n\t" \
2706 "movw %w1,2+%0\n\t" \
2707 "rorl $16,%1\n\t" \
2708 "movb %b1,4+%0\n\t" \
2709 "movb %4,5+%0\n\t" \
2710 "movb $0,6+%0\n\t" \
2711 "movb %h1,7+%0\n\t" \
2712 "rorl $16,%1" \
2713 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
2715 This works great except that the output assembler ends
2716 up looking a bit weird if it turns out that there is
2717 no offset. You end up producing code that looks like:
2719 #APP
2720 movw $235,(%eax)
2721 movw %dx,2+(%eax)
2722 rorl $16,%edx
2723 movb %dl,4+(%eax)
2724 movb $137,5+(%eax)
2725 movb $0,6+(%eax)
2726 movb %dh,7+(%eax)
2727 rorl $16,%edx
2728 #NO_APP
2730 So here we provide the missing zero.
2733 *displacement_string_end = '0';
2735 #endif
2736 #ifndef LEX_AT
2739 * We can have operands of the form
2740 * <symbol>@GOTOFF+<nnn>
2741 * Take the easy way out here and copy everything
2742 * into a temporary buffer...
2744 register char *cp;
2746 cp = strchr (input_line_pointer, '@');
2747 if (cp != NULL)
2749 char *tmpbuf;
2750 int len = 0;
2751 int first;
2753 /* GOT relocations are not supported in 16 bit mode */
2754 if (flag_16bit_code)
2755 as_bad (_("GOT relocations not supported in 16 bit mode"));
2757 if (GOT_symbol == NULL)
2758 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
2760 if (strncmp (cp + 1, "PLT", 3) == 0)
2762 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2763 len = 3;
2765 else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
2767 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2768 len = 6;
2770 else if (strncmp (cp + 1, "GOT", 3) == 0)
2772 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2773 len = 3;
2775 else
2776 as_bad (_("bad reloc specifier in expression"));
2778 /* Replace the relocation token with ' ', so that errors like
2779 foo@GOTOFF1 will be detected. */
2780 first = cp - input_line_pointer;
2781 tmpbuf = (char *) alloca (strlen(input_line_pointer));
2782 memcpy (tmpbuf, input_line_pointer, first);
2783 tmpbuf[first] = ' ';
2784 strcpy (tmpbuf + first + 1, cp + 1 + len);
2785 input_line_pointer = tmpbuf;
2788 #endif
2790 exp_seg = expression (exp);
2792 #ifdef BFD_ASSEMBLER
2793 /* We do this to make sure that the section symbol is in
2794 the symbol table. We will ultimately change the relocation
2795 to be relative to the beginning of the section */
2796 if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
2798 if (S_IS_LOCAL(exp->X_add_symbol)
2799 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
2800 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
2801 assert (exp->X_op == O_symbol);
2802 exp->X_op = O_subtract;
2803 exp->X_op_symbol = GOT_symbol;
2804 i.disp_reloc[this_operand] = BFD_RELOC_32;
2806 #endif
2808 SKIP_WHITESPACE ();
2809 if (*input_line_pointer)
2810 as_bad (_("ignoring junk `%s' after expression"),
2811 input_line_pointer);
2812 #if GCC_ASM_O_HACK
2813 RESTORE_END_STRING (disp_end + 1);
2814 #endif
2815 RESTORE_END_STRING (disp_end);
2816 input_line_pointer = save_input_line_pointer;
2818 if (exp->X_op == O_absent || exp->X_op == O_big)
2820 /* missing or bad expr becomes absolute 0 */
2821 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
2822 disp_start);
2823 exp->X_op = O_constant;
2824 exp->X_add_number = 0;
2825 exp->X_add_symbol = (symbolS *) 0;
2826 exp->X_op_symbol = (symbolS *) 0;
2829 if (exp->X_op == O_constant)
2831 if (i.types[this_operand] & Disp16)
2833 /* We know this operand is at most 16 bits, so convert to a
2834 signed 16 bit number before trying to see whether it will
2835 fit in an even smaller size. */
2836 exp->X_add_number =
2837 (((exp->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
2839 if (fits_in_signed_byte (exp->X_add_number))
2840 i.types[this_operand] |= Disp8;
2842 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
2843 else if (
2844 #ifdef BFD_ASSEMBLER
2845 OUTPUT_FLAVOR == bfd_target_aout_flavour &&
2846 #endif
2847 exp_seg != text_section
2848 && exp_seg != data_section
2849 && exp_seg != bss_section
2850 && exp_seg != undefined_section)
2852 #ifdef BFD_ASSEMBLER
2853 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
2854 #else
2855 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
2856 #endif
2857 return 0;
2859 #endif
2860 return 1;
2863 static int i386_operand_modifier PARAMS ((char **, int));
2865 static int
2866 i386_operand_modifier (op_string, got_a_float)
2867 char **op_string;
2868 int got_a_float;
2870 if (!strncasecmp (*op_string, "BYTE PTR", 8))
2872 i.suffix = BYTE_MNEM_SUFFIX;
2873 *op_string += 8;
2874 return BYTE_PTR;
2877 else if (!strncasecmp (*op_string, "WORD PTR", 8))
2879 if (got_a_float == 2) /* "fi..." */
2880 i.suffix = SHORT_MNEM_SUFFIX;
2881 else
2882 i.suffix = WORD_MNEM_SUFFIX;
2883 *op_string += 8;
2884 return WORD_PTR;
2887 else if (!strncasecmp (*op_string, "DWORD PTR", 9))
2889 if (got_a_float == 1) /* "f..." */
2890 i.suffix = SHORT_MNEM_SUFFIX;
2891 else
2892 i.suffix = LONG_MNEM_SUFFIX;
2893 *op_string += 9;
2894 return DWORD_PTR;
2897 else if (!strncasecmp (*op_string, "QWORD PTR", 9))
2899 i.suffix = DWORD_MNEM_SUFFIX;
2900 *op_string += 9;
2901 return QWORD_PTR;
2904 else if (!strncasecmp (*op_string, "XWORD PTR", 9))
2906 i.suffix = LONG_DOUBLE_MNEM_SUFFIX;
2907 *op_string += 9;
2908 return XWORD_PTR;
2911 else if (!strncasecmp (*op_string, "SHORT", 5))
2913 *op_string += 5;
2914 return SHORT;
2917 else if (!strncasecmp (*op_string, "OFFSET FLAT:", 12))
2919 *op_string += 12;
2920 return OFFSET_FLAT;
2923 else if (!strncasecmp (*op_string, "FLAT", 4))
2925 *op_string += 4;
2926 return FLAT;
2929 else return NONE_FOUND;
2932 static char * build_displacement_string PARAMS ((int, char *));
2934 static char *
2935 build_displacement_string (initial_disp, op_string)
2936 int initial_disp;
2937 char *op_string;
2939 char *temp_string = (char *) malloc (strlen (op_string) + 1);
2940 char *end_of_operand_string;
2941 char *tc;
2942 char *temp_disp;
2944 temp_string[0] = '\0';
2945 tc = end_of_operand_string = strchr (op_string, '[');
2946 if (initial_disp && !end_of_operand_string)
2948 strcpy (temp_string, op_string);
2949 return temp_string;
2952 /* Build the whole displacement string */
2953 if (initial_disp)
2955 strncpy (temp_string, op_string, end_of_operand_string - op_string);
2956 temp_string[end_of_operand_string - op_string] = '\0';
2957 temp_disp = tc;
2959 else
2960 temp_disp = op_string;
2962 while (*temp_disp != '\0')
2964 char *end_op;
2965 int add_minus = (*temp_disp == '-');
2967 if (*temp_disp == '+' || *temp_disp == '-' || *temp_disp == '[')
2968 temp_disp++;
2970 if (is_space_char (*temp_disp))
2971 temp_disp++;
2973 /* Don't consider registers */
2974 if ( !((*temp_disp == REGISTER_PREFIX || allow_naked_reg)
2975 && parse_register (temp_disp, &end_op)) )
2977 char *string_start = temp_disp;
2979 while (*temp_disp != ']'
2980 && *temp_disp != '+'
2981 && *temp_disp != '-'
2982 && *temp_disp != '*')
2983 ++temp_disp;
2985 if (add_minus)
2986 strcat (temp_string, "-");
2987 else
2988 strcat (temp_string, "+");
2990 strncat (temp_string, string_start, temp_disp - string_start);
2991 if (*temp_disp == '+' || *temp_disp == '-')
2992 --temp_disp;
2995 while (*temp_disp != '\0'
2996 && *temp_disp != '+'
2997 && *temp_disp != '-')
2998 ++temp_disp;
3001 return temp_string;
3004 static int i386_parse_seg PARAMS ((char *));
3006 static int
3007 i386_parse_seg (op_string)
3008 char *op_string;
3010 if (is_space_char (*op_string))
3011 ++op_string;
3013 /* Should be one of es, cs, ss, ds fs or gs */
3014 switch (*op_string++)
3016 case 'e':
3017 i.seg[i.mem_operands] = &es;
3018 break;
3019 case 'c':
3020 i.seg[i.mem_operands] = &cs;
3021 break;
3022 case 's':
3023 i.seg[i.mem_operands] = &ss;
3024 break;
3025 case 'd':
3026 i.seg[i.mem_operands] = &ds;
3027 break;
3028 case 'f':
3029 i.seg[i.mem_operands] = &fs;
3030 break;
3031 case 'g':
3032 i.seg[i.mem_operands] = &gs;
3033 break;
3034 default:
3035 as_bad (_("bad segment name `%s'"), op_string);
3036 return 0;
3039 if (*op_string++ != 's')
3041 as_bad (_("bad segment name `%s'"), op_string);
3042 return 0;
3045 if (is_space_char (*op_string))
3046 ++op_string;
3048 if (*op_string != ':')
3050 as_bad (_("bad segment name `%s'"), op_string);
3051 return 0;
3054 return 1;
3058 static int i386_index_check PARAMS((const char *));
3060 /* Make sure the memory operand we've been dealt is valid.
3061 Returns 1 on success, 0 on a failure.
3063 static int
3064 i386_index_check (operand_string)
3065 const char *operand_string;
3067 #if INFER_ADDR_PREFIX
3068 int fudged = 0;
3070 tryprefix:
3071 #endif
3072 if (flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0)
3073 /* 16 bit mode checks */
3074 ? ((i.base_reg
3075 && ((i.base_reg->reg_type & (Reg16|BaseIndex))
3076 != (Reg16|BaseIndex)))
3077 || (i.index_reg
3078 && (((i.index_reg->reg_type & (Reg16|BaseIndex))
3079 != (Reg16|BaseIndex))
3080 || ! (i.base_reg
3081 && i.base_reg->reg_num < 6
3082 && i.index_reg->reg_num >= 6
3083 && i.log2_scale_factor == 0))))
3084 /* 32 bit mode checks */
3085 : ((i.base_reg
3086 && (i.base_reg->reg_type & Reg32) == 0)
3087 || (i.index_reg
3088 && ((i.index_reg->reg_type & (Reg32|BaseIndex))
3089 != (Reg32|BaseIndex)))))
3091 #if INFER_ADDR_PREFIX
3092 if (i.prefix[ADDR_PREFIX] == 0 && stackop_size != '\0')
3094 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
3095 i.prefixes += 1;
3096 /* Change the size of any displacement too. At most one of
3097 Disp16 or Disp32 is set.
3098 FIXME. There doesn't seem to be any real need for separate
3099 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
3100 Removing them would probably clean up the code quite a lot.
3102 if (i.types[this_operand] & (Disp16|Disp32))
3103 i.types[this_operand] ^= (Disp16|Disp32);
3104 fudged = 1;
3105 goto tryprefix;
3107 if (fudged)
3108 as_bad (_("`%s' is not a valid base/index expression"),
3109 operand_string);
3110 else
3111 #endif
3112 as_bad (_("`%s' is not a valid %s bit base/index expression"),
3113 operand_string,
3114 flag_16bit_code ^ (i.prefix[ADDR_PREFIX] != 0) ? "16" : "32");
3115 return 0;
3117 return 1;
3120 static int i386_intel_memory_operand PARAMS ((char *));
3122 static int
3123 i386_intel_memory_operand (operand_string)
3124 char *operand_string;
3126 char *op_string = operand_string;
3127 char *end_of_operand_string;
3129 if ((i.mem_operands == 1
3130 && (current_templates->start->opcode_modifier & IsString) == 0)
3131 || i.mem_operands == 2)
3133 as_bad (_("too many memory references for `%s'"),
3134 current_templates->start->name);
3135 return 0;
3138 /* First check for a segment override. */
3139 if (*op_string != '[')
3141 char *end_seg;
3143 end_seg = strchr (op_string, ':');
3144 if (end_seg)
3146 if (!i386_parse_seg (op_string))
3147 return 0;
3148 op_string = end_seg + 1;
3152 /* Look for displacement preceding open bracket */
3153 if (*op_string != '[')
3155 char *temp_string;
3157 if (i.disp_operands)
3158 return 0;
3160 temp_string = build_displacement_string (true, op_string);
3162 if (!i386_displacement (temp_string, temp_string + strlen (temp_string)))
3164 free (temp_string);
3165 return 0;
3167 free (temp_string);
3169 end_of_operand_string = strchr (op_string, '[');
3170 if (!end_of_operand_string)
3171 end_of_operand_string = op_string + strlen (op_string);
3173 if (is_space_char (*end_of_operand_string))
3174 --end_of_operand_string;
3176 op_string = end_of_operand_string;
3179 if (*op_string == '[')
3181 ++op_string;
3183 /* Pick off each component and figure out where it belongs */
3185 end_of_operand_string = op_string;
3187 while (*op_string != ']')
3189 const reg_entry *temp_reg;
3190 char *end_op;
3191 char *temp_string;
3193 while (*end_of_operand_string != '+'
3194 && *end_of_operand_string != '-'
3195 && *end_of_operand_string != '*'
3196 && *end_of_operand_string != ']')
3197 end_of_operand_string++;
3199 temp_string = op_string;
3200 if (*temp_string == '+')
3202 ++temp_string;
3203 if (is_space_char (*temp_string))
3204 ++temp_string;
3207 if ((*temp_string == REGISTER_PREFIX || allow_naked_reg)
3208 && (temp_reg = parse_register (temp_string, &end_op)) != NULL)
3210 if (i.base_reg == NULL)
3211 i.base_reg = temp_reg;
3212 else
3213 i.index_reg = temp_reg;
3215 i.types[this_operand] |= BaseIndex;
3217 else if (*temp_string == REGISTER_PREFIX)
3219 as_bad (_("bad register name `%s'"), temp_string);
3220 return 0;
3222 else if (is_digit_char (*op_string)
3223 || *op_string == '+' || *op_string == '-')
3225 char *temp_str;
3227 if (i.disp_operands != 0)
3228 return 0;
3230 temp_string = build_displacement_string (false, op_string);
3232 temp_str = temp_string;
3233 if (*temp_str == '+')
3234 ++temp_str;
3236 if (!i386_displacement (temp_str, temp_str + strlen (temp_str)))
3238 free (temp_string);
3239 return 0;
3241 free (temp_string);
3243 ++op_string;
3244 end_of_operand_string = op_string;
3245 while (*end_of_operand_string != ']'
3246 && *end_of_operand_string != '+'
3247 && *end_of_operand_string != '-'
3248 && *end_of_operand_string != '*')
3249 ++end_of_operand_string;
3251 else if (*op_string == '*')
3253 ++op_string;
3255 if (i.base_reg && !i.index_reg)
3257 i.index_reg = i.base_reg;
3258 i.base_reg = 0;
3261 if (!i386_scale (op_string))
3262 return 0;
3264 op_string = end_of_operand_string;
3265 ++end_of_operand_string;
3269 if (i386_index_check (operand_string) == 0)
3270 return 0;
3272 i.mem_operands++;
3273 return 1;
3276 static int
3277 i386_intel_operand (operand_string, got_a_float)
3278 char *operand_string;
3279 int got_a_float;
3281 const reg_entry * r;
3282 char *end_op;
3283 char *op_string = operand_string;
3285 int operand_modifier = i386_operand_modifier (&op_string, got_a_float);
3286 if (is_space_char (*op_string))
3287 ++op_string;
3289 switch (operand_modifier)
3291 case BYTE_PTR:
3292 case WORD_PTR:
3293 case DWORD_PTR:
3294 case QWORD_PTR:
3295 case XWORD_PTR:
3296 if (!i386_intel_memory_operand (op_string))
3297 return 0;
3298 break;
3300 case FLAT:
3301 case OFFSET_FLAT:
3302 if (!i386_immediate (op_string))
3303 return 0;
3304 break;
3306 case SHORT:
3307 case NONE_FOUND:
3308 /* Should be register or immediate */
3309 if (is_digit_char (*op_string)
3310 && strchr (op_string, '[') == 0)
3312 if (!i386_immediate (op_string))
3313 return 0;
3315 else if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
3316 && (r = parse_register (op_string, &end_op)) != NULL)
3318 /* Check for a segment override by searching for ':' after a
3319 segment register. */
3320 op_string = end_op;
3321 if (is_space_char (*op_string))
3322 ++op_string;
3323 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
3325 switch (r->reg_num)
3327 case 0:
3328 i.seg[i.mem_operands] = &es;
3329 break;
3330 case 1:
3331 i.seg[i.mem_operands] = &cs;
3332 break;
3333 case 2:
3334 i.seg[i.mem_operands] = &ss;
3335 break;
3336 case 3:
3337 i.seg[i.mem_operands] = &ds;
3338 break;
3339 case 4:
3340 i.seg[i.mem_operands] = &fs;
3341 break;
3342 case 5:
3343 i.seg[i.mem_operands] = &gs;
3344 break;
3348 i.types[this_operand] |= r->reg_type & ~BaseIndex;
3349 i.op[this_operand].regs = r;
3350 i.reg_operands++;
3352 else if (*op_string == REGISTER_PREFIX)
3354 as_bad (_("bad register name `%s'"), op_string);
3355 return 0;
3357 else if (!i386_intel_memory_operand (op_string))
3358 return 0;
3360 break;
3361 } /* end switch */
3363 return 1;
3366 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
3367 on error. */
3369 static int
3370 i386_operand (operand_string)
3371 char *operand_string;
3373 const reg_entry *r;
3374 char *end_op;
3375 char *op_string = operand_string;
3377 if (is_space_char (*op_string))
3378 ++op_string;
3380 /* We check for an absolute prefix (differentiating,
3381 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
3382 if (*op_string == ABSOLUTE_PREFIX)
3384 ++op_string;
3385 if (is_space_char (*op_string))
3386 ++op_string;
3387 i.types[this_operand] |= JumpAbsolute;
3390 /* Check if operand is a register. */
3391 if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
3392 && (r = parse_register (op_string, &end_op)) != NULL)
3394 /* Check for a segment override by searching for ':' after a
3395 segment register. */
3396 op_string = end_op;
3397 if (is_space_char (*op_string))
3398 ++op_string;
3399 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
3401 switch (r->reg_num)
3403 case 0:
3404 i.seg[i.mem_operands] = &es;
3405 break;
3406 case 1:
3407 i.seg[i.mem_operands] = &cs;
3408 break;
3409 case 2:
3410 i.seg[i.mem_operands] = &ss;
3411 break;
3412 case 3:
3413 i.seg[i.mem_operands] = &ds;
3414 break;
3415 case 4:
3416 i.seg[i.mem_operands] = &fs;
3417 break;
3418 case 5:
3419 i.seg[i.mem_operands] = &gs;
3420 break;
3423 /* Skip the ':' and whitespace. */
3424 ++op_string;
3425 if (is_space_char (*op_string))
3426 ++op_string;
3428 if (!is_digit_char (*op_string)
3429 && !is_identifier_char (*op_string)
3430 && *op_string != '('
3431 && *op_string != ABSOLUTE_PREFIX)
3433 as_bad (_("bad memory operand `%s'"), op_string);
3434 return 0;
3436 /* Handle case of %es:*foo. */
3437 if (*op_string == ABSOLUTE_PREFIX)
3439 ++op_string;
3440 if (is_space_char (*op_string))
3441 ++op_string;
3442 i.types[this_operand] |= JumpAbsolute;
3444 goto do_memory_reference;
3446 if (*op_string)
3448 as_bad (_("junk `%s' after register"), op_string);
3449 return 0;
3451 i.types[this_operand] |= r->reg_type & ~BaseIndex;
3452 i.op[this_operand].regs = r;
3453 i.reg_operands++;
3455 else if (*op_string == REGISTER_PREFIX)
3457 as_bad (_("bad register name `%s'"), op_string);
3458 return 0;
3460 else if (*op_string == IMMEDIATE_PREFIX)
3461 { /* ... or an immediate */
3462 ++op_string;
3463 if (i.types[this_operand] & JumpAbsolute)
3465 as_bad (_("immediate operand illegal with absolute jump"));
3466 return 0;
3468 if (!i386_immediate (op_string))
3469 return 0;
3471 else if (is_digit_char (*op_string)
3472 || is_identifier_char (*op_string)
3473 || *op_string == '(' )
3475 /* This is a memory reference of some sort. */
3476 char *base_string;
3478 /* Start and end of displacement string expression (if found). */
3479 char *displacement_string_start;
3480 char *displacement_string_end;
3482 do_memory_reference:
3483 if ((i.mem_operands == 1
3484 && (current_templates->start->opcode_modifier & IsString) == 0)
3485 || i.mem_operands == 2)
3487 as_bad (_("too many memory references for `%s'"),
3488 current_templates->start->name);
3489 return 0;
3492 /* Check for base index form. We detect the base index form by
3493 looking for an ')' at the end of the operand, searching
3494 for the '(' matching it, and finding a REGISTER_PREFIX or ','
3495 after the '('. */
3496 base_string = op_string + strlen (op_string);
3498 --base_string;
3499 if (is_space_char (*base_string))
3500 --base_string;
3502 /* If we only have a displacement, set-up for it to be parsed later. */
3503 displacement_string_start = op_string;
3504 displacement_string_end = base_string + 1;
3506 if (*base_string == ')')
3508 char *temp_string;
3509 unsigned int parens_balanced = 1;
3510 /* We've already checked that the number of left & right ()'s are
3511 equal, so this loop will not be infinite. */
3514 base_string--;
3515 if (*base_string == ')')
3516 parens_balanced++;
3517 if (*base_string == '(')
3518 parens_balanced--;
3520 while (parens_balanced);
3522 temp_string = base_string;
3524 /* Skip past '(' and whitespace. */
3525 ++base_string;
3526 if (is_space_char (*base_string))
3527 ++base_string;
3529 if (*base_string == ','
3530 || ((*base_string == REGISTER_PREFIX || allow_naked_reg)
3531 && (i.base_reg = parse_register (base_string, &end_op)) != NULL))
3533 displacement_string_end = temp_string;
3535 i.types[this_operand] |= BaseIndex;
3537 if (i.base_reg)
3539 base_string = end_op;
3540 if (is_space_char (*base_string))
3541 ++base_string;
3544 /* There may be an index reg or scale factor here. */
3545 if (*base_string == ',')
3547 ++base_string;
3548 if (is_space_char (*base_string))
3549 ++base_string;
3551 if ((*base_string == REGISTER_PREFIX || allow_naked_reg)
3552 && (i.index_reg = parse_register (base_string, &end_op)) != NULL)
3554 base_string = end_op;
3555 if (is_space_char (*base_string))
3556 ++base_string;
3557 if (*base_string == ',')
3559 ++base_string;
3560 if (is_space_char (*base_string))
3561 ++base_string;
3563 else if (*base_string != ')' )
3565 as_bad (_("expecting `,' or `)' after index register in `%s'"),
3566 operand_string);
3567 return 0;
3570 else if (*base_string == REGISTER_PREFIX)
3572 as_bad (_("bad register name `%s'"), base_string);
3573 return 0;
3576 /* Check for scale factor. */
3577 if (isdigit ((unsigned char) *base_string))
3579 if (!i386_scale (base_string))
3580 return 0;
3582 ++base_string;
3583 if (is_space_char (*base_string))
3584 ++base_string;
3585 if (*base_string != ')')
3587 as_bad (_("expecting `)' after scale factor in `%s'"),
3588 operand_string);
3589 return 0;
3592 else if (!i.index_reg)
3594 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
3595 *base_string);
3596 return 0;
3599 else if (*base_string != ')')
3601 as_bad (_("expecting `,' or `)' after base register in `%s'"),
3602 operand_string);
3603 return 0;
3606 else if (*base_string == REGISTER_PREFIX)
3608 as_bad (_("bad register name `%s'"), base_string);
3609 return 0;
3613 /* If there's an expression beginning the operand, parse it,
3614 assuming displacement_string_start and
3615 displacement_string_end are meaningful. */
3616 if (displacement_string_start != displacement_string_end)
3618 if (!i386_displacement (displacement_string_start,
3619 displacement_string_end))
3620 return 0;
3623 /* Special case for (%dx) while doing input/output op. */
3624 if (i.base_reg
3625 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
3626 && i.index_reg == 0
3627 && i.log2_scale_factor == 0
3628 && i.seg[i.mem_operands] == 0
3629 && (i.types[this_operand] & Disp) == 0)
3631 i.types[this_operand] = InOutPortReg;
3632 return 1;
3635 if (i386_index_check (operand_string) == 0)
3636 return 0;
3637 i.mem_operands++;
3639 else
3640 { /* it's not a memory operand; argh! */
3641 as_bad (_("invalid char %s beginning operand %d `%s'"),
3642 output_invalid (*op_string),
3643 this_operand + 1,
3644 op_string);
3645 return 0;
3647 return 1; /* normal return */
3651 * md_estimate_size_before_relax()
3653 * Called just before relax().
3654 * Any symbol that is now undefined will not become defined.
3655 * Return the correct fr_subtype in the frag.
3656 * Return the initial "guess for fr_var" to caller.
3657 * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
3658 * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
3659 * Although it may not be explicit in the frag, pretend fr_var starts with a
3660 * 0 value.
3663 md_estimate_size_before_relax (fragP, segment)
3664 register fragS *fragP;
3665 register segT segment;
3667 register unsigned char *opcode;
3668 register int old_fr_fix;
3670 old_fr_fix = fragP->fr_fix;
3671 opcode = (unsigned char *) fragP->fr_opcode;
3672 /* We've already got fragP->fr_subtype right; all we have to do is
3673 check for un-relaxable symbols. */
3674 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
3676 /* symbol is undefined in this segment */
3677 int code16 = fragP->fr_subtype & CODE16;
3678 int size = code16 ? 2 : 4;
3679 #ifdef BFD_ASSEMBLER
3680 enum bfd_reloc_code_real reloc_type;
3681 #else
3682 int reloc_type;
3683 #endif
3685 if (GOT_symbol /* Not quite right - we should switch on presence of
3686 @PLT, but I cannot see how to get to that from
3687 here. We should have done this in md_assemble to
3688 really get it right all of the time, but I think it
3689 does not matter that much, as this will be right
3690 most of the time. ERY */
3691 && S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)
3692 reloc_type = BFD_RELOC_386_PLT32;
3693 else if (code16)
3694 reloc_type = BFD_RELOC_16_PCREL;
3695 else
3696 reloc_type = BFD_RELOC_32_PCREL;
3698 switch (opcode[0])
3700 case JUMP_PC_RELATIVE: /* make jmp (0xeb) a dword displacement jump */
3701 opcode[0] = 0xe9; /* dword disp jmp */
3702 fragP->fr_fix += size;
3703 fix_new (fragP, old_fr_fix, size,
3704 fragP->fr_symbol,
3705 fragP->fr_offset, 1,
3706 reloc_type);
3707 break;
3709 default:
3710 /* This changes the byte-displacement jump 0x7N
3711 to the dword-displacement jump 0x0f,0x8N. */
3712 opcode[1] = opcode[0] + 0x10;
3713 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3714 fragP->fr_fix += 1 + size; /* we've added an opcode byte */
3715 fix_new (fragP, old_fr_fix + 1, size,
3716 fragP->fr_symbol,
3717 fragP->fr_offset, 1,
3718 reloc_type);
3719 break;
3721 frag_wane (fragP);
3723 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
3724 } /* md_estimate_size_before_relax() */
3727 * md_convert_frag();
3729 * Called after relax() is finished.
3730 * In: Address of frag.
3731 * fr_type == rs_machine_dependent.
3732 * fr_subtype is what the address relaxed to.
3734 * Out: Any fixSs and constants are set up.
3735 * Caller will turn frag into a ".space 0".
3737 #ifndef BFD_ASSEMBLER
3738 void
3739 md_convert_frag (headers, sec, fragP)
3740 object_headers *headers ATTRIBUTE_UNUSED;
3741 segT sec ATTRIBUTE_UNUSED;
3742 register fragS *fragP;
3743 #else
3744 void
3745 md_convert_frag (abfd, sec, fragP)
3746 bfd *abfd ATTRIBUTE_UNUSED;
3747 segT sec ATTRIBUTE_UNUSED;
3748 register fragS *fragP;
3749 #endif
3751 register unsigned char *opcode;
3752 unsigned char *where_to_put_displacement = NULL;
3753 unsigned int target_address;
3754 unsigned int opcode_address;
3755 unsigned int extension = 0;
3756 int displacement_from_opcode_start;
3758 opcode = (unsigned char *) fragP->fr_opcode;
3760 /* Address we want to reach in file space. */
3761 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
3762 #ifdef BFD_ASSEMBLER /* not needed otherwise? */
3763 target_address += symbol_get_frag (fragP->fr_symbol)->fr_address;
3764 #endif
3766 /* Address opcode resides at in file space. */
3767 opcode_address = fragP->fr_address + fragP->fr_fix;
3769 /* Displacement from opcode start to fill into instruction. */
3770 displacement_from_opcode_start = target_address - opcode_address;
3772 switch (fragP->fr_subtype)
3774 case ENCODE_RELAX_STATE (COND_JUMP, SMALL):
3775 case ENCODE_RELAX_STATE (COND_JUMP, SMALL16):
3776 case ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL):
3777 case ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL16):
3778 /* don't have to change opcode */
3779 extension = 1; /* 1 opcode + 1 displacement */
3780 where_to_put_displacement = &opcode[1];
3781 break;
3783 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
3784 extension = 5; /* 2 opcode + 4 displacement */
3785 opcode[1] = opcode[0] + 0x10;
3786 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3787 where_to_put_displacement = &opcode[2];
3788 break;
3790 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
3791 extension = 4; /* 1 opcode + 4 displacement */
3792 opcode[0] = 0xe9;
3793 where_to_put_displacement = &opcode[1];
3794 break;
3796 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
3797 extension = 3; /* 2 opcode + 2 displacement */
3798 opcode[1] = opcode[0] + 0x10;
3799 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
3800 where_to_put_displacement = &opcode[2];
3801 break;
3803 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
3804 extension = 2; /* 1 opcode + 2 displacement */
3805 opcode[0] = 0xe9;
3806 where_to_put_displacement = &opcode[1];
3807 break;
3809 default:
3810 BAD_CASE (fragP->fr_subtype);
3811 break;
3813 /* now put displacement after opcode */
3814 md_number_to_chars ((char *) where_to_put_displacement,
3815 (valueT) (displacement_from_opcode_start - extension),
3816 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
3817 fragP->fr_fix += extension;
3821 int md_short_jump_size = 2; /* size of byte displacement jmp */
3822 int md_long_jump_size = 5; /* size of dword displacement jmp */
3823 const int md_reloc_size = 8; /* Size of relocation record */
3825 void
3826 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
3827 char *ptr;
3828 addressT from_addr, to_addr;
3829 fragS *frag ATTRIBUTE_UNUSED;
3830 symbolS *to_symbol ATTRIBUTE_UNUSED;
3832 long offset;
3834 offset = to_addr - (from_addr + 2);
3835 md_number_to_chars (ptr, (valueT) 0xeb, 1); /* opcode for byte-disp jump */
3836 md_number_to_chars (ptr + 1, (valueT) offset, 1);
3839 void
3840 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
3841 char *ptr;
3842 addressT from_addr, to_addr;
3843 fragS *frag;
3844 symbolS *to_symbol;
3846 long offset;
3848 if (flag_do_long_jump)
3850 offset = to_addr - S_GET_VALUE (to_symbol);
3851 md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
3852 md_number_to_chars (ptr + 1, (valueT) offset, 4);
3853 fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
3854 to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
3856 else
3858 offset = to_addr - (from_addr + 5);
3859 md_number_to_chars (ptr, (valueT) 0xe9, 1);
3860 md_number_to_chars (ptr + 1, (valueT) offset, 4);
3864 /* Apply a fixup (fixS) to segment data, once it has been determined
3865 by our caller that we have all the info we need to fix it up.
3867 On the 386, immediates, displacements, and data pointers are all in
3868 the same (little-endian) format, so we don't need to care about which
3869 we are handling. */
3872 md_apply_fix3 (fixP, valp, seg)
3873 fixS *fixP; /* The fix we're to put in. */
3874 valueT *valp; /* Pointer to the value of the bits. */
3875 segT seg ATTRIBUTE_UNUSED; /* Segment fix is from. */
3877 register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
3878 valueT value = *valp;
3880 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
3881 if (fixP->fx_pcrel)
3883 switch (fixP->fx_r_type)
3885 default:
3886 break;
3888 case BFD_RELOC_32:
3889 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3890 break;
3891 case BFD_RELOC_16:
3892 fixP->fx_r_type = BFD_RELOC_16_PCREL;
3893 break;
3894 case BFD_RELOC_8:
3895 fixP->fx_r_type = BFD_RELOC_8_PCREL;
3896 break;
3900 /* This is a hack. There should be a better way to handle this.
3901 This covers for the fact that bfd_install_relocation will
3902 subtract the current location (for partial_inplace, PC relative
3903 relocations); see more below. */
3904 if ((fixP->fx_r_type == BFD_RELOC_32_PCREL
3905 || fixP->fx_r_type == BFD_RELOC_16_PCREL
3906 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
3907 && fixP->fx_addsy)
3909 #ifndef OBJ_AOUT
3910 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
3911 #ifdef TE_PE
3912 || OUTPUT_FLAVOR == bfd_target_coff_flavour
3913 #endif
3915 value += fixP->fx_where + fixP->fx_frag->fr_address;
3916 #endif
3917 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3918 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
3920 segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
3922 if ((fseg == seg
3923 || (symbol_section_p (fixP->fx_addsy)
3924 && fseg != absolute_section))
3925 && ! S_IS_EXTERNAL (fixP->fx_addsy)
3926 && ! S_IS_WEAK (fixP->fx_addsy)
3927 && S_IS_DEFINED (fixP->fx_addsy)
3928 && ! S_IS_COMMON (fixP->fx_addsy))
3930 /* Yes, we add the values in twice. This is because
3931 bfd_perform_relocation subtracts them out again. I think
3932 bfd_perform_relocation is broken, but I don't dare change
3933 it. FIXME. */
3934 value += fixP->fx_where + fixP->fx_frag->fr_address;
3937 #endif
3938 #if defined (OBJ_COFF) && defined (TE_PE)
3939 /* For some reason, the PE format does not store a section
3940 address offset for a PC relative symbol. */
3941 if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
3942 value += md_pcrel_from (fixP);
3943 else if (S_IS_EXTERNAL (fixP->fx_addsy)
3944 || S_IS_WEAK (fixP->fx_addsy))
3946 /* We are generating an external relocation for this defined
3947 symbol. We add the address, because
3948 bfd_install_relocation will subtract it. VALUE already
3949 holds the symbol value, because fixup_segment added it
3950 in. We subtract it out, and then we subtract it out
3951 again because bfd_install_relocation will add it in
3952 again. */
3953 value += md_pcrel_from (fixP);
3954 value -= 2 * S_GET_VALUE (fixP->fx_addsy);
3956 #endif
3958 #ifdef TE_PE
3959 else if (fixP->fx_addsy != NULL
3960 && S_IS_DEFINED (fixP->fx_addsy)
3961 && (S_IS_EXTERNAL (fixP->fx_addsy)
3962 || S_IS_WEAK (fixP->fx_addsy)))
3964 /* We are generating an external relocation for this defined
3965 symbol. VALUE already holds the symbol value, and
3966 bfd_install_relocation will add it in again. We don't want
3967 either addition. */
3968 value -= 2 * S_GET_VALUE (fixP->fx_addsy);
3970 #endif
3972 /* Fix a few things - the dynamic linker expects certain values here,
3973 and we must not dissappoint it. */
3974 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3975 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
3976 && fixP->fx_addsy)
3977 switch (fixP->fx_r_type) {
3978 case BFD_RELOC_386_PLT32:
3979 /* Make the jump instruction point to the address of the operand. At
3980 runtime we merely add the offset to the actual PLT entry. */
3981 value = 0xfffffffc;
3982 break;
3983 case BFD_RELOC_386_GOTPC:
3985 * This is tough to explain. We end up with this one if we have
3986 * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal
3987 * here is to obtain the absolute address of the GOT, and it is strongly
3988 * preferable from a performance point of view to avoid using a runtime
3989 * relocation for this. The actual sequence of instructions often look
3990 * something like:
3992 * call .L66
3993 * .L66:
3994 * popl %ebx
3995 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
3997 * The call and pop essentially return the absolute address of
3998 * the label .L66 and store it in %ebx. The linker itself will
3999 * ultimately change the first operand of the addl so that %ebx points to
4000 * the GOT, but to keep things simple, the .o file must have this operand
4001 * set so that it generates not the absolute address of .L66, but the
4002 * absolute address of itself. This allows the linker itself simply
4003 * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
4004 * added in, and the addend of the relocation is stored in the operand
4005 * field for the instruction itself.
4007 * Our job here is to fix the operand so that it would add the correct
4008 * offset so that %ebx would point to itself. The thing that is tricky is
4009 * that .-.L66 will point to the beginning of the instruction, so we need
4010 * to further modify the operand so that it will point to itself.
4011 * There are other cases where you have something like:
4013 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4015 * and here no correction would be required. Internally in the assembler
4016 * we treat operands of this form as not being pcrel since the '.' is
4017 * explicitly mentioned, and I wonder whether it would simplify matters
4018 * to do it this way. Who knows. In earlier versions of the PIC patches,
4019 * the pcrel_adjust field was used to store the correction, but since the
4020 * expression is not pcrel, I felt it would be confusing to do it this way.
4022 value -= 1;
4023 break;
4024 case BFD_RELOC_386_GOT32:
4025 value = 0; /* Fully resolved at runtime. No addend. */
4026 break;
4027 case BFD_RELOC_386_GOTOFF:
4028 break;
4030 case BFD_RELOC_VTABLE_INHERIT:
4031 case BFD_RELOC_VTABLE_ENTRY:
4032 fixP->fx_done = 0;
4033 return 1;
4035 default:
4036 break;
4038 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
4039 *valp = value;
4040 #endif /* defined (BFD_ASSEMBLER) && !defined (TE_Mach) */
4041 md_number_to_chars (p, value, fixP->fx_size);
4043 return 1;
4046 #if 0
4047 /* This is never used. */
4048 long /* Knows about the byte order in a word. */
4049 md_chars_to_number (con, nbytes)
4050 unsigned char con[]; /* Low order byte 1st. */
4051 int nbytes; /* Number of bytes in the input. */
4053 long retval;
4054 for (retval = 0, con += nbytes - 1; nbytes--; con--)
4056 retval <<= BITS_PER_CHAR;
4057 retval |= *con;
4059 return retval;
4061 #endif /* 0 */
4064 #define MAX_LITTLENUMS 6
4066 /* Turn the string pointed to by litP into a floating point constant of type
4067 type, and emit the appropriate bytes. The number of LITTLENUMS emitted
4068 is stored in *sizeP . An error message is returned, or NULL on OK. */
4069 char *
4070 md_atof (type, litP, sizeP)
4071 int type;
4072 char *litP;
4073 int *sizeP;
4075 int prec;
4076 LITTLENUM_TYPE words[MAX_LITTLENUMS];
4077 LITTLENUM_TYPE *wordP;
4078 char *t;
4080 switch (type)
4082 case 'f':
4083 case 'F':
4084 prec = 2;
4085 break;
4087 case 'd':
4088 case 'D':
4089 prec = 4;
4090 break;
4092 case 'x':
4093 case 'X':
4094 prec = 5;
4095 break;
4097 default:
4098 *sizeP = 0;
4099 return _("Bad call to md_atof ()");
4101 t = atof_ieee (input_line_pointer, type, words);
4102 if (t)
4103 input_line_pointer = t;
4105 *sizeP = prec * sizeof (LITTLENUM_TYPE);
4106 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
4107 the bigendian 386. */
4108 for (wordP = words + prec - 1; prec--;)
4110 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
4111 litP += sizeof (LITTLENUM_TYPE);
4113 return 0;
4116 char output_invalid_buf[8];
4118 static char * output_invalid PARAMS ((int));
4120 static char *
4121 output_invalid (c)
4122 int c;
4124 if (isprint (c))
4125 sprintf (output_invalid_buf, "'%c'", c);
4126 else
4127 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
4128 return output_invalid_buf;
4132 /* REG_STRING starts *before* REGISTER_PREFIX. */
4134 static const reg_entry *
4135 parse_register (reg_string, end_op)
4136 char *reg_string;
4137 char **end_op;
4139 char *s = reg_string;
4140 char *p;
4141 char reg_name_given[MAX_REG_NAME_SIZE + 1];
4142 const reg_entry *r;
4144 /* Skip possible REGISTER_PREFIX and possible whitespace. */
4145 if (*s == REGISTER_PREFIX)
4146 ++s;
4148 if (is_space_char (*s))
4149 ++s;
4151 p = reg_name_given;
4152 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
4154 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
4155 return (const reg_entry *) NULL;
4156 s++;
4159 *end_op = s;
4161 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
4163 /* Handle floating point regs, allowing spaces in the (i) part. */
4164 if (r == i386_regtab /* %st is first entry of table */)
4166 if (is_space_char (*s))
4167 ++s;
4168 if (*s == '(')
4170 ++s;
4171 if (is_space_char (*s))
4172 ++s;
4173 if (*s >= '0' && *s <= '7')
4175 r = &i386_float_regtab[*s - '0'];
4176 ++s;
4177 if (is_space_char (*s))
4178 ++s;
4179 if (*s == ')')
4181 *end_op = s + 1;
4182 return r;
4185 /* We have "%st(" then garbage */
4186 return (const reg_entry *) NULL;
4190 return r;
4193 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4194 CONST char *md_shortopts = "kmVQ:sq";
4195 #else
4196 CONST char *md_shortopts = "m";
4197 #endif
4198 struct option md_longopts[] = {
4199 {NULL, no_argument, NULL, 0}
4201 size_t md_longopts_size = sizeof (md_longopts);
4204 md_parse_option (c, arg)
4205 int c;
4206 char *arg ATTRIBUTE_UNUSED;
4208 switch (c)
4210 case 'm':
4211 flag_do_long_jump = 1;
4212 break;
4214 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4215 /* -k: Ignore for FreeBSD compatibility. */
4216 case 'k':
4217 break;
4219 /* -V: SVR4 argument to print version ID. */
4220 case 'V':
4221 print_version_id ();
4222 break;
4224 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
4225 should be emitted or not. FIXME: Not implemented. */
4226 case 'Q':
4227 break;
4229 case 's':
4230 /* -s: On i386 Solaris, this tells the native assembler to use
4231 .stab instead of .stab.excl. We always use .stab anyhow. */
4232 break;
4234 case 'q':
4235 /* -q: On i386 Solaris, this tells the native assembler does
4236 fewer checks. */
4237 break;
4238 #endif
4240 default:
4241 return 0;
4243 return 1;
4246 void
4247 md_show_usage (stream)
4248 FILE *stream;
4250 fprintf (stream, _("\
4251 -m do long jump\n"));
4252 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4253 fprintf (stream, _("\
4254 -V print assembler version number\n\
4255 -k ignored\n\
4256 -Qy, -Qn ignored\n\
4257 -q ignored\n\
4258 -s ignored\n"));
4259 #endif
4262 #ifdef BFD_ASSEMBLER
4263 #if ((defined (OBJ_MAYBE_ELF) && defined (OBJ_MAYBE_COFF)) \
4264 || (defined (OBJ_MAYBE_ELF) && defined (OBJ_MAYBE_AOUT)) \
4265 || (defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)))
4267 /* Pick the target format to use. */
4269 const char *
4270 i386_target_format ()
4272 switch (OUTPUT_FLAVOR)
4274 #ifdef OBJ_MAYBE_AOUT
4275 case bfd_target_aout_flavour:
4276 return AOUT_TARGET_FORMAT;
4277 #endif
4278 #ifdef OBJ_MAYBE_COFF
4279 case bfd_target_coff_flavour:
4280 return "coff-i386";
4281 #endif
4282 #ifdef OBJ_MAYBE_ELF
4283 case bfd_target_elf_flavour:
4284 return "elf32-i386";
4285 #endif
4286 default:
4287 abort ();
4288 return NULL;
4292 #endif /* OBJ_MAYBE_ more than one */
4293 #endif /* BFD_ASSEMBLER */
4295 symbolS *
4296 md_undefined_symbol (name)
4297 char *name;
4299 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
4300 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
4301 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
4302 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4304 if (!GOT_symbol)
4306 if (symbol_find (name))
4307 as_bad (_("GOT already in symbol table"));
4308 GOT_symbol = symbol_new (name, undefined_section,
4309 (valueT) 0, &zero_address_frag);
4311 return GOT_symbol;
4313 return 0;
4316 /* Round up a section size to the appropriate boundary. */
4317 valueT
4318 md_section_align (segment, size)
4319 segT segment ATTRIBUTE_UNUSED;
4320 valueT size;
4322 #ifdef BFD_ASSEMBLER
4323 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4324 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
4326 /* For a.out, force the section size to be aligned. If we don't do
4327 this, BFD will align it for us, but it will not write out the
4328 final bytes of the section. This may be a bug in BFD, but it is
4329 easier to fix it here since that is how the other a.out targets
4330 work. */
4331 int align;
4333 align = bfd_get_section_alignment (stdoutput, segment);
4334 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
4336 #endif
4337 #endif
4339 return size;
4342 /* On the i386, PC-relative offsets are relative to the start of the
4343 next instruction. That is, the address of the offset, plus its
4344 size, since the offset is always the last part of the insn. */
4346 long
4347 md_pcrel_from (fixP)
4348 fixS *fixP;
4350 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
4353 #ifndef I386COFF
4355 static void
4356 s_bss (ignore)
4357 int ignore ATTRIBUTE_UNUSED;
4359 register int temp;
4361 temp = get_absolute_expression ();
4362 subseg_set (bss_section, (subsegT) temp);
4363 demand_empty_rest_of_line ();
4366 #endif
4369 #ifdef BFD_ASSEMBLER
4371 void
4372 i386_validate_fix (fixp)
4373 fixS *fixp;
4375 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
4377 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
4378 fixp->fx_subsy = 0;
4382 arelent *
4383 tc_gen_reloc (section, fixp)
4384 asection *section ATTRIBUTE_UNUSED;
4385 fixS *fixp;
4387 arelent *rel;
4388 bfd_reloc_code_real_type code;
4390 switch (fixp->fx_r_type)
4392 case BFD_RELOC_386_PLT32:
4393 case BFD_RELOC_386_GOT32:
4394 case BFD_RELOC_386_GOTOFF:
4395 case BFD_RELOC_386_GOTPC:
4396 case BFD_RELOC_RVA:
4397 case BFD_RELOC_VTABLE_ENTRY:
4398 case BFD_RELOC_VTABLE_INHERIT:
4399 code = fixp->fx_r_type;
4400 break;
4401 default:
4402 if (fixp->fx_pcrel)
4404 switch (fixp->fx_size)
4406 default:
4407 as_bad (_("can not do %d byte pc-relative relocation"),
4408 fixp->fx_size);
4409 code = BFD_RELOC_32_PCREL;
4410 break;
4411 case 1: code = BFD_RELOC_8_PCREL; break;
4412 case 2: code = BFD_RELOC_16_PCREL; break;
4413 case 4: code = BFD_RELOC_32_PCREL; break;
4416 else
4418 switch (fixp->fx_size)
4420 default:
4421 as_bad (_("can not do %d byte relocation"), fixp->fx_size);
4422 code = BFD_RELOC_32;
4423 break;
4424 case 1: code = BFD_RELOC_8; break;
4425 case 2: code = BFD_RELOC_16; break;
4426 case 4: code = BFD_RELOC_32; break;
4429 break;
4432 if (code == BFD_RELOC_32
4433 && GOT_symbol
4434 && fixp->fx_addsy == GOT_symbol)
4435 code = BFD_RELOC_386_GOTPC;
4437 rel = (arelent *) xmalloc (sizeof (arelent));
4438 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4439 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4441 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4442 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
4443 vtable entry to be used in the relocation's section offset. */
4444 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
4445 rel->address = fixp->fx_offset;
4447 if (fixp->fx_pcrel)
4448 rel->addend = fixp->fx_addnumber;
4449 else
4450 rel->addend = 0;
4452 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
4453 if (rel->howto == NULL)
4455 as_bad_where (fixp->fx_file, fixp->fx_line,
4456 _("cannot represent relocation type %s"),
4457 bfd_get_reloc_code_name (code));
4458 /* Set howto to a garbage value so that we can keep going. */
4459 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4460 assert (rel->howto != NULL);
4463 return rel;
4466 #else /* ! BFD_ASSEMBLER */
4468 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
4469 void
4470 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
4471 char *where;
4472 fixS *fixP;
4473 relax_addressT segment_address_in_file;
4476 * In: length of relocation (or of address) in chars: 1, 2 or 4.
4477 * Out: GNU LD relocation length code: 0, 1, or 2.
4480 static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
4481 long r_symbolnum;
4483 know (fixP->fx_addsy != NULL);
4485 md_number_to_chars (where,
4486 (valueT) (fixP->fx_frag->fr_address
4487 + fixP->fx_where - segment_address_in_file),
4490 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
4491 ? S_GET_TYPE (fixP->fx_addsy)
4492 : fixP->fx_addsy->sy_number);
4494 where[6] = (r_symbolnum >> 16) & 0x0ff;
4495 where[5] = (r_symbolnum >> 8) & 0x0ff;
4496 where[4] = r_symbolnum & 0x0ff;
4497 where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
4498 | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
4499 | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
4502 #endif /* OBJ_AOUT or OBJ_BOUT */
4504 #if defined (I386COFF)
4506 short
4507 tc_coff_fix2rtype (fixP)
4508 fixS *fixP;
4510 if (fixP->fx_r_type == R_IMAGEBASE)
4511 return R_IMAGEBASE;
4513 return (fixP->fx_pcrel ?
4514 (fixP->fx_size == 1 ? R_PCRBYTE :
4515 fixP->fx_size == 2 ? R_PCRWORD :
4516 R_PCRLONG) :
4517 (fixP->fx_size == 1 ? R_RELBYTE :
4518 fixP->fx_size == 2 ? R_RELWORD :
4519 R_DIR32));
4523 tc_coff_sizemachdep (frag)
4524 fragS *frag;
4526 if (frag->fr_next)
4527 return (frag->fr_next->fr_address - frag->fr_address);
4528 else
4529 return 0;
4532 #endif /* I386COFF */
4534 #endif /* ! BFD_ASSEMBLER */
4536 /* end of tc-i386.c */