More gcc lint with harsher warning options.
[binutils-gdb.git] / gas / config / tc-i386.c
1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23 Bugs & suggestions are completely welcome. This is free software.
24 Please help us make it better.
25 */
26
27 #include <ctype.h>
28
29 #include "as.h"
30
31 #include "obstack.h"
32 #include "opcode/i386.h"
33
34 /* 'md_assemble ()' gathers together information and puts it into a
35 i386_insn. */
36
37 struct _i386_insn
38 {
39 /* TM holds the template for the insn were currently assembling. */
40 template tm;
41 /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
42 char suffix;
43 /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
44
45 /* OPERANDS gives the number of given operands. */
46 unsigned int operands;
47
48 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
49 of given register, displacement, memory operands and immediate
50 operands. */
51 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
52
53 /* TYPES [i] is the type (see above #defines) which tells us how to
54 search through DISPS [i] & IMMS [i] & REGS [i] for the required
55 operand. */
56 unsigned int types[MAX_OPERANDS];
57
58 /* Displacements (if given) for each operand. */
59 expressionS *disps[MAX_OPERANDS];
60
61 /* Immediate operands (if given) for each operand. */
62 expressionS *imms[MAX_OPERANDS];
63
64 /* Register operands (if given) for each operand. */
65 reg_entry *regs[MAX_OPERANDS];
66
67 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
68 the base index byte below. */
69 reg_entry *base_reg;
70 reg_entry *index_reg;
71 unsigned int log2_scale_factor;
72
73 /* SEG gives the seg_entry of this insn. It is equal to zero unless
74 an explicit segment override is given. */
75 const seg_entry *seg; /* segment for memory operands (if given) */
76
77 /* PREFIX holds all the given prefix opcodes (usually null).
78 PREFIXES is the size of PREFIX. */
79 /* richfix: really unsigned? */
80 unsigned char prefix[MAX_PREFIXES];
81 unsigned int prefixes;
82
83 /* RM and IB are the modrm byte and the base index byte where the
84 addressing modes of this insn are encoded. */
85
86 modrm_byte rm;
87 base_index_byte bi;
88 };
89
90 typedef struct _i386_insn i386_insn;
91
92 /* This array holds the chars that always start a comment. If the
93 pre-processor is disabled, these aren't very useful */
94 #if defined (TE_I386AIX) || defined (OBJ_ELF)
95 const char comment_chars[] = "#/";
96 #else
97 const char comment_chars[] = "#";
98 #endif
99
100 /* This array holds the chars that only start a comment at the beginning of
101 a line. If the line seems to have the form '# 123 filename'
102 .line and .file directives will appear in the pre-processed output */
103 /* Note that input_file.c hand checks for '#' at the beginning of the
104 first line of the input file. This is because the compiler outputs
105 #NO_APP at the beginning of its output. */
106 /* Also note that comments started like this one will always work if
107 '/' isn't otherwise defined. */
108 #if defined (TE_I386AIX) || defined (OBJ_ELF)
109 const char line_comment_chars[];
110 #else
111 const char line_comment_chars[] = "/";
112 #endif
113 const char line_separator_chars[] = "";
114
115 /* Chars that can be used to separate mant from exp in floating point nums */
116 const char EXP_CHARS[] = "eE";
117
118 /* Chars that mean this number is a floating point constant */
119 /* As in 0f12.456 */
120 /* or 0d1.2345e12 */
121 const char FLT_CHARS[] = "fFdDxX";
122
123 /* tables for lexical analysis */
124 static char opcode_chars[256];
125 static char register_chars[256];
126 static char operand_chars[256];
127 static char space_chars[256];
128 static char identifier_chars[256];
129 static char digit_chars[256];
130
131 /* lexical macros */
132 #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
133 #define is_operand_char(x) (operand_chars[(unsigned char) x])
134 #define is_register_char(x) (register_chars[(unsigned char) x])
135 #define is_space_char(x) (space_chars[(unsigned char) x])
136 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
137 #define is_digit_char(x) (digit_chars[(unsigned char) x])
138
139 /* put here all non-digit non-letter charcters that may occur in an operand */
140 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:";
141
142 static char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
143
144 /* md_assemble() always leaves the strings it's passed unaltered. To
145 effect this we maintain a stack of saved characters that we've smashed
146 with '\0's (indicating end of strings for various sub-fields of the
147 assembler instruction). */
148 static char save_stack[32];
149 static char *save_stack_p; /* stack pointer */
150 #define END_STRING_AND_SAVE(s) *save_stack_p++ = *s; *s = '\0'
151 #define RESTORE_END_STRING(s) *s = *--save_stack_p
152
153 /* The instruction we're assembling. */
154 static i386_insn i;
155
156 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
157 static expressionS disp_expressions[2], im_expressions[2];
158
159 /* pointers to ebp & esp entries in reg_hash hash table */
160 static reg_entry *ebp, *esp;
161
162 static int this_operand; /* current operand we are working on */
163
164 /* Interface to relax_segment.
165 There are 2 relax states for 386 jump insns: one for conditional &
166 one for unconditional jumps. This is because the these two types
167 of jumps add different sizes to frags when we're figuring out what
168 sort of jump to choose to reach a given label. */
169
170 /* types */
171 #define COND_JUMP 1 /* conditional jump */
172 #define UNCOND_JUMP 2 /* unconditional jump */
173 /* sizes */
174 #define BYTE 0
175 #define WORD 1
176 #define DWORD 2
177 #define UNKNOWN_SIZE 3
178
179 #ifndef INLINE
180 #ifdef __GNUC__
181 #define INLINE __inline__
182 #else
183 #define INLINE
184 #endif
185 #endif
186
187 #define ENCODE_RELAX_STATE(type,size) ((type<<2) | (size))
188 #define SIZE_FROM_RELAX_STATE(s) \
189 ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
190
191 const relax_typeS md_relax_table[] =
192 {
193 /* The fields are:
194 1) most positive reach of this state,
195 2) most negative reach of this state,
196 3) how many bytes this mode will add to the size of the current frag
197 4) which index into the table to try if we can't fit into this one.
198 */
199 {1, 1, 0, 0},
200 {1, 1, 0, 0},
201 {1, 1, 0, 0},
202 {1, 1, 0, 0},
203
204 /* For now we don't use word displacement jumps; they may be
205 untrustworthy. */
206 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
207 /* word conditionals add 3 bytes to frag:
208 2 opcode prefix; 1 displacement bytes */
209 {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
210 /* dword conditionals adds 4 bytes to frag:
211 1 opcode prefix; 3 displacement bytes */
212 {0, 0, 4, 0},
213 {1, 1, 0, 0},
214
215 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
216 /* word jmp adds 2 bytes to frag:
217 1 opcode prefix; 1 displacement bytes */
218 {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
219 /* dword jmp adds 3 bytes to frag:
220 0 opcode prefix; 3 displacement bytes */
221 {0, 0, 3, 0},
222 {1, 1, 0, 0},
223
224 };
225
226 static char *output_invalid PARAMS ((int c));
227 static int i386_operand PARAMS ((char *operand_string));
228 static reg_entry *parse_register PARAMS ((char *reg_string));
229 #ifndef I386COFF
230 static void s_bss PARAMS ((void));
231 #endif
232
233 static INLINE unsigned long
234 mode_from_disp_size (t)
235 unsigned long t;
236 {
237 return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
238 }
239
240 /* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
241
242 static INLINE unsigned long
243 opcode_suffix_to_type (s)
244 unsigned long s;
245 {
246 return (s == BYTE_OPCODE_SUFFIX
247 ? Byte : (s == WORD_OPCODE_SUFFIX
248 ? Word : DWord));
249 } /* opcode_suffix_to_type() */
250
251 static INLINE int
252 fits_in_signed_byte (num)
253 long num;
254 {
255 return (num >= -128) && (num <= 127);
256 } /* fits_in_signed_byte() */
257
258 static INLINE int
259 fits_in_unsigned_byte (num)
260 long num;
261 {
262 return (num & 0xff) == num;
263 } /* fits_in_unsigned_byte() */
264
265 static INLINE int
266 fits_in_unsigned_word (num)
267 long num;
268 {
269 return (num & 0xffff) == num;
270 } /* fits_in_unsigned_word() */
271
272 static INLINE int
273 fits_in_signed_word (num)
274 long num;
275 {
276 return (-32768 <= num) && (num <= 32767);
277 } /* fits_in_signed_word() */
278
279 static int
280 smallest_imm_type (num)
281 long num;
282 {
283 return ((num == 1)
284 ? (Imm1 | Imm8 | Imm8S | Imm16 | Imm32)
285 : fits_in_signed_byte (num)
286 ? (Imm8S | Imm8 | Imm16 | Imm32)
287 : fits_in_unsigned_byte (num)
288 ? (Imm8 | Imm16 | Imm32)
289 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
290 ? (Imm16 | Imm32)
291 : (Imm32));
292 } /* smallest_imm_type() */
293
294 /* Ignore certain directives generated by gcc. This probably should
295 not be here. */
296 void
297 dummy ()
298 {
299 while (*input_line_pointer && *input_line_pointer != '\n')
300 input_line_pointer++;
301 }
302
303 const pseudo_typeS md_pseudo_table[] =
304 {
305 #ifndef I386COFF
306 {"bss", s_bss, 0},
307 #endif
308 #ifndef OBJ_AOUT
309 {"align", s_align_bytes, 0},
310 #else
311 {"align", s_align_ptwo, 0},
312 #endif
313 {"ffloat", float_cons, 'f'},
314 {"dfloat", float_cons, 'd'},
315 {"tfloat", float_cons, 'x'},
316 {"value", cons, 2},
317 {"noopt", s_ignore, 0},
318 {"optim", s_ignore, 0},
319 #ifdef OBJ_ELF
320 {"zero", s_space, 0},
321 #endif
322 {0, 0, 0}
323 };
324
325 /* for interface with expression () */
326 extern char *input_line_pointer;
327
328 /* obstack for constructing various things in md_begin */
329 struct obstack o;
330
331 /* hash table for opcode lookup */
332 static struct hash_control *op_hash = (struct hash_control *) 0;
333 /* hash table for register lookup */
334 static struct hash_control *reg_hash = (struct hash_control *) 0;
335 /* hash table for prefix lookup */
336 static struct hash_control *prefix_hash = (struct hash_control *) 0;
337 \f
338
339 void
340 md_begin ()
341 {
342 char *hash_err;
343
344 obstack_begin (&o, 4096);
345
346 /* initialize op_hash hash table */
347 op_hash = hash_new (); /* xmalloc handles error */
348
349 {
350 register const template *optab;
351 register templates *core_optab;
352 char *prev_name;
353
354 optab = i386_optab; /* setup for loop */
355 prev_name = optab->name;
356 obstack_grow (&o, optab, sizeof (template));
357 core_optab = (templates *) xmalloc (sizeof (templates));
358
359 for (optab++; optab < i386_optab_end; optab++)
360 {
361 if (!strcmp (optab->name, prev_name))
362 {
363 /* same name as before --> append to current template list */
364 obstack_grow (&o, optab, sizeof (template));
365 }
366 else
367 {
368 /* different name --> ship out current template list;
369 add to hash table; & begin anew */
370 /* Note: end must be set before start! since obstack_next_free
371 changes upon opstack_finish */
372 core_optab->end = (template *) obstack_next_free (&o);
373 core_optab->start = (template *) obstack_finish (&o);
374 hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
375 if (hash_err && *hash_err)
376 {
377 hash_error:
378 as_fatal ("Internal Error: Can't hash %s: %s", prev_name,
379 hash_err);
380 }
381 prev_name = optab->name;
382 core_optab = (templates *) xmalloc (sizeof (templates));
383 obstack_grow (&o, optab, sizeof (template));
384 }
385 }
386 }
387
388 /* initialize reg_hash hash table */
389 reg_hash = hash_new ();
390 {
391 register const reg_entry *regtab;
392
393 for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
394 {
395 hash_err = hash_insert (reg_hash, regtab->reg_name, regtab);
396 if (hash_err && *hash_err)
397 goto hash_error;
398 }
399 }
400
401 esp = (reg_entry *) hash_find (reg_hash, "esp");
402 ebp = (reg_entry *) hash_find (reg_hash, "ebp");
403
404 /* initialize reg_hash hash table */
405 prefix_hash = hash_new ();
406 {
407 register const prefix_entry *prefixtab;
408
409 for (prefixtab = i386_prefixtab;
410 prefixtab < i386_prefixtab_end; prefixtab++)
411 {
412 hash_err = hash_insert (prefix_hash, prefixtab->prefix_name, prefixtab);
413 if (hash_err && *hash_err)
414 goto hash_error;
415 }
416 }
417
418 /* fill in lexical tables: opcode_chars, operand_chars, space_chars */
419 {
420 register unsigned int c;
421
422 memset (opcode_chars, '\0', sizeof (opcode_chars));
423 memset (operand_chars, '\0', sizeof (operand_chars));
424 memset (space_chars, '\0', sizeof (space_chars));
425 memset (identifier_chars, '\0', sizeof (identifier_chars));
426 memset (digit_chars, '\0', sizeof (digit_chars));
427
428 for (c = 0; c < 256; c++)
429 {
430 if (islower (c) || isdigit (c))
431 {
432 opcode_chars[c] = c;
433 register_chars[c] = c;
434 }
435 else if (isupper (c))
436 {
437 opcode_chars[c] = tolower (c);
438 register_chars[c] = opcode_chars[c];
439 }
440 else if (c == PREFIX_SEPERATOR)
441 {
442 opcode_chars[c] = c;
443 }
444 else if (c == ')' || c == '(')
445 {
446 register_chars[c] = c;
447 }
448
449 if (isupper (c) || islower (c) || isdigit (c))
450 operand_chars[c] = c;
451 else if (c && strchr (operand_special_chars, c))
452 operand_chars[c] = c;
453
454 if (isdigit (c) || c == '-')
455 digit_chars[c] = c;
456
457 if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
458 identifier_chars[c] = c;
459
460 if (c == ' ' || c == '\t')
461 space_chars[c] = c;
462 }
463 }
464 }
465
466 void
467 md_end ()
468 {
469 } /* not much to do here. */
470 \f
471
472 #ifdef DEBUG386
473
474 /* debugging routines for md_assemble */
475 /* static void pi (), pte (), pt (), pe (), ps (); */
476
477 static void
478 pi (line, x)
479 char *line;
480 i386_insn *x;
481 {
482 register template *p;
483 int i;
484
485 fprintf (stdout, "%s: template ", line);
486 pte (&x->tm);
487 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x",
488 x->rm.mode, x->rm.reg, x->rm.regmem);
489 fprintf (stdout, " base %x index %x scale %x\n",
490 x->bi.base, x->bi.index, x->bi.scale);
491 for (i = 0; i < x->operands; i++)
492 {
493 fprintf (stdout, " #%d: ", i + 1);
494 pt (x->types[i]);
495 fprintf (stdout, "\n");
496 if (x->types[i] & Reg)
497 fprintf (stdout, "%s\n", x->regs[i]->reg_name);
498 if (x->types[i] & Imm)
499 pe (x->imms[i]);
500 if (x->types[i] & (Disp | Abs))
501 pe (x->disps[i]);
502 }
503 }
504
505 static void
506 pte (t)
507 template *t;
508 {
509 int i;
510 fprintf (stdout, " %d operands ", t->operands);
511 fprintf (stdout, "opcode %x ",
512 t->base_opcode);
513 if (t->extension_opcode != None)
514 fprintf (stdout, "ext %x ", t->extension_opcode);
515 if (t->opcode_modifier & D)
516 fprintf (stdout, "D");
517 if (t->opcode_modifier & W)
518 fprintf (stdout, "W");
519 fprintf (stdout, "\n");
520 for (i = 0; i < t->operands; i++)
521 {
522 fprintf (stdout, " #%d type ", i + 1);
523 pt (t->operand_types[i]);
524 fprintf (stdout, "\n");
525 }
526 }
527
528 static void
529 pe (e)
530 expressionS *e;
531 {
532 fprintf (stdout, " operation %d\n", e->X_op);
533 fprintf (stdout, " add_number %d (%x)\n",
534 e->X_add_number, e->X_add_number);
535 if (e->X_add_symbol)
536 {
537 fprintf (stdout, " add_symbol ");
538 ps (e->X_add_symbol);
539 fprintf (stdout, "\n");
540 }
541 if (e->X_op_symbol)
542 {
543 fprintf (stdout, " op_symbol ");
544 ps (e->X_op_symbol);
545 fprintf (stdout, "\n");
546 }
547 }
548
549 static void
550 ps (s)
551 symbolS *s;
552 {
553 fprintf (stdout, "%s type %s%s",
554 S_GET_NAME (s),
555 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
556 segment_name (S_GET_SEGMENT (s)));
557 }
558
559 struct type_name
560 {
561 unsigned int mask;
562 char *tname;
563 }
564
565 type_names[] =
566 {
567 { Reg8, "r8" },
568 { Reg16, "r16" },
569 { Reg32, "r32" },
570 { Imm8, "i8" },
571 { Imm8S, "i8s" },
572 { Imm16, "i16" },
573 { Imm32, "i32" },
574 { Mem8, "Mem8" },
575 { Mem16, "Mem16" },
576 { Mem32, "Mem32" },
577 { BaseIndex, "BaseIndex" },
578 { Abs8, "Abs8" },
579 { Abs16, "Abs16" },
580 { Abs32, "Abs32" },
581 { Disp8, "d8" },
582 { Disp16, "d16" },
583 { Disp32, "d32" },
584 { SReg2, "SReg2" },
585 { SReg3, "SReg3" },
586 { Acc, "Acc" },
587 { InOutPortReg, "InOutPortReg" },
588 { ShiftCount, "ShiftCount" },
589 { Imm1, "i1" },
590 { Control, "control reg" },
591 { Test, "test reg" },
592 { FloatReg, "FReg" },
593 { FloatAcc, "FAcc" },
594 { JumpAbsolute, "Jump Absolute" },
595 { 0, "" }
596 };
597
598 static void
599 pt (t)
600 unsigned int t;
601 {
602 register struct type_name *ty;
603
604 if (t == Unknown)
605 {
606 fprintf (stdout, "Unknown");
607 }
608 else
609 {
610 for (ty = type_names; ty->mask; ty++)
611 if (t & ty->mask)
612 fprintf (stdout, "%s, ", ty->tname);
613 }
614 fflush (stdout);
615 }
616
617 #endif /* DEBUG386 */
618 \f
619 #ifdef BFD_ASSEMBLER
620 static bfd_reloc_code_real_type
621 reloc (size, pcrel)
622 int size;
623 int pcrel;
624 {
625 if (pcrel)
626 switch (size)
627 {
628 case 1: return BFD_RELOC_8_PCREL;
629 case 2: return BFD_RELOC_16_PCREL;
630 case 4: return BFD_RELOC_32_PCREL;
631 }
632 else
633 switch (size)
634 {
635 case 1: return BFD_RELOC_8;
636 case 2: return BFD_RELOC_16;
637 case 4: return BFD_RELOC_32;
638 }
639 abort ();
640 }
641 #else
642 #define reloc(SIZE,PCREL) 0
643 #define BFD_RELOC_32 0
644 #define BFD_RELOC_32_PCREL 0
645 #endif
646
647 /* This is the guts of the machine-dependent assembler. LINE points to a
648 machine dependent instruction. This function is supposed to emit
649 the frags/bytes it assembles to. */
650 void
651 md_assemble (line)
652 char *line;
653 {
654 /* Holds temlate once we've found it. */
655 register template *t;
656
657 /* Possible templates for current insn */
658 templates *current_templates = (templates *) 0;
659
660 /* Initialize globals. */
661 memset (&i, '\0', sizeof (i));
662 memset (disp_expressions, '\0', sizeof (disp_expressions));
663 memset (im_expressions, '\0', sizeof (im_expressions));
664 save_stack_p = save_stack; /* reset stack pointer */
665
666 /* Fist parse an opcode & call i386_operand for the operands.
667 We assume that the scrubber has arranged it so that line[0] is the valid
668 start of a (possibly prefixed) opcode. */
669 {
670 register char *l = line; /* Fast place to put LINE. */
671
672 /* 1 if operand is pending after ','. */
673 unsigned int expecting_operand = 0;
674 /* 1 if we found a prefix only acceptable with string insns. */
675 unsigned int expecting_string_instruction = 0;
676 /* Non-zero if operand parens not balenced. */
677 unsigned int paren_not_balenced;
678 char *token_start = l;
679
680 while (!is_space_char (*l) && *l != END_OF_INSN)
681 {
682 if (!is_opcode_char (*l))
683 {
684 as_bad ("invalid character %s in opcode", output_invalid (*l));
685 return;
686 }
687 else if (*l != PREFIX_SEPERATOR)
688 {
689 *l = opcode_chars[(unsigned char) *l]; /* fold case of opcodes */
690 l++;
691 }
692 else
693 { /* this opcode's got a prefix */
694 register unsigned int q;
695 register prefix_entry *prefix;
696
697 if (l == token_start)
698 {
699 as_bad ("expecting prefix; got nothing");
700 return;
701 }
702 END_STRING_AND_SAVE (l);
703 prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
704 if (!prefix)
705 {
706 as_bad ("no such opcode prefix ('%s')", token_start);
707 return;
708 }
709 RESTORE_END_STRING (l);
710 /* check for repeated prefix */
711 for (q = 0; q < i.prefixes; q++)
712 if (i.prefix[q] == prefix->prefix_code)
713 {
714 as_bad ("same prefix used twice; you don't really want this!");
715 return;
716 }
717 if (i.prefixes == MAX_PREFIXES)
718 {
719 as_bad ("too many opcode prefixes");
720 return;
721 }
722 i.prefix[i.prefixes++] = prefix->prefix_code;
723 if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
724 expecting_string_instruction = 1;
725 /* skip past PREFIX_SEPERATOR and reset token_start */
726 token_start = ++l;
727 }
728 }
729 END_STRING_AND_SAVE (l);
730 if (token_start == l)
731 {
732 as_bad ("expecting opcode; got nothing");
733 return;
734 }
735
736 /* Lookup insn in hash; try intel & att naming conventions if appropriate;
737 that is: we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
738 current_templates = (templates *) hash_find (op_hash, token_start);
739 if (!current_templates)
740 {
741 int last_index = strlen (token_start) - 1;
742 char last_char = token_start[last_index];
743 switch (last_char)
744 {
745 case DWORD_OPCODE_SUFFIX:
746 case WORD_OPCODE_SUFFIX:
747 case BYTE_OPCODE_SUFFIX:
748 token_start[last_index] = '\0';
749 current_templates = (templates *) hash_find (op_hash, token_start);
750 token_start[last_index] = last_char;
751 i.suffix = last_char;
752 }
753 if (!current_templates)
754 {
755 as_bad ("no such 386 instruction: `%s'", token_start);
756 return;
757 }
758 }
759 RESTORE_END_STRING (l);
760
761 /* check for rep/repne without a string instruction */
762 if (expecting_string_instruction &&
763 !IS_STRING_INSTRUCTION (current_templates->
764 start->base_opcode))
765 {
766 as_bad ("expecting string instruction after rep/repne");
767 return;
768 }
769
770 /* There may be operands to parse. */
771 if (*l != END_OF_INSN &&
772 /* For string instructions, we ignore any operands if given. This
773 kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
774 the operands are always going to be the same, and are not really
775 encoded in machine code. */
776 !IS_STRING_INSTRUCTION (current_templates->
777 start->base_opcode))
778 {
779 /* parse operands */
780 do
781 {
782 /* skip optional white space before operand */
783 while (!is_operand_char (*l) && *l != END_OF_INSN)
784 {
785 if (!is_space_char (*l))
786 {
787 as_bad ("invalid character %s before %s operand",
788 output_invalid (*l),
789 ordinal_names[i.operands]);
790 return;
791 }
792 l++;
793 }
794 token_start = l; /* after white space */
795 paren_not_balenced = 0;
796 while (paren_not_balenced || *l != ',')
797 {
798 if (*l == END_OF_INSN)
799 {
800 if (paren_not_balenced)
801 {
802 as_bad ("unbalenced parenthesis in %s operand.",
803 ordinal_names[i.operands]);
804 return;
805 }
806 else
807 break; /* we are done */
808 }
809 else if (!is_operand_char (*l))
810 {
811 as_bad ("invalid character %s in %s operand",
812 output_invalid (*l),
813 ordinal_names[i.operands]);
814 return;
815 }
816 if (*l == '(')
817 ++paren_not_balenced;
818 if (*l == ')')
819 --paren_not_balenced;
820 l++;
821 }
822 if (l != token_start)
823 { /* yes, we've read in another operand */
824 unsigned int operand_ok;
825 this_operand = i.operands++;
826 if (i.operands > MAX_OPERANDS)
827 {
828 as_bad ("spurious operands; (%d operands/instruction max)",
829 MAX_OPERANDS);
830 return;
831 }
832 /* now parse operand adding info to 'i' as we go along */
833 END_STRING_AND_SAVE (l);
834 operand_ok = i386_operand (token_start);
835 RESTORE_END_STRING (l); /* restore old contents */
836 if (!operand_ok)
837 return;
838 }
839 else
840 {
841 if (expecting_operand)
842 {
843 expecting_operand_after_comma:
844 as_bad ("expecting operand after ','; got nothing");
845 return;
846 }
847 if (*l == ',')
848 {
849 as_bad ("expecting operand before ','; got nothing");
850 return;
851 }
852 }
853
854 /* now *l must be either ',' or END_OF_INSN */
855 if (*l == ',')
856 {
857 if (*++l == END_OF_INSN)
858 { /* just skip it, if it's \n complain */
859 goto expecting_operand_after_comma;
860 }
861 expecting_operand = 1;
862 }
863 }
864 while (*l != END_OF_INSN); /* until we get end of insn */
865 }
866 }
867
868 /* Now we've parsed the opcode into a set of templates, and have the
869 operands at hand.
870
871 Next, we find a template that matches the given insn,
872 making sure the overlap of the given operands types is consistent
873 with the template operand types. */
874
875 #define MATCH(overlap,given_type) \
876 (overlap && \
877 (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
878 == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
879 || (overlap == InOutPortReg)))
880
881
882 /* If m0 and m1 are register matches they must be consistent
883 with the expected operand types t0 and t1.
884 That is, if both m0 & m1 are register matches
885 i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
886 then, either 1. or 2. must be true:
887 1. the expected operand type register overlap is null:
888 (t0 & t1 & Reg) == 0
889 AND
890 the given register overlap is null:
891 (m0 & m1 & Reg) == 0
892 2. the expected operand type register overlap == the given
893 operand type overlap: (t0 & t1 & m0 & m1 & Reg).
894 */
895 #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
896 ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
897 ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
898 ((t0 & t1) & (m0 & m1) & (Reg)) \
899 ) : 1)
900 {
901 register unsigned int overlap0, overlap1;
902 expressionS *exp;
903 unsigned int overlap2;
904 unsigned int found_reverse_match;
905
906 overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
907 for (t = current_templates->start;
908 t < current_templates->end;
909 t++)
910 {
911
912 /* must have right number of operands */
913 if (i.operands != t->operands)
914 continue;
915 else if (!t->operands)
916 break; /* 0 operands always matches */
917
918 overlap0 = i.types[0] & t->operand_types[0];
919 switch (t->operands)
920 {
921 case 1:
922 if (!MATCH (overlap0, i.types[0]))
923 continue;
924 break;
925 case 2:
926 case 3:
927 overlap1 = i.types[1] & t->operand_types[1];
928 if (!MATCH (overlap0, i.types[0]) ||
929 !MATCH (overlap1, i.types[1]) ||
930 !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
931 t->operand_types[0],
932 t->operand_types[1]))
933 {
934
935 /* check if other direction is valid ... */
936 if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
937 continue;
938
939 /* try reversing direction of operands */
940 overlap0 = i.types[0] & t->operand_types[1];
941 overlap1 = i.types[1] & t->operand_types[0];
942 if (!MATCH (overlap0, i.types[0]) ||
943 !MATCH (overlap1, i.types[1]) ||
944 !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
945 t->operand_types[0],
946 t->operand_types[1]))
947 {
948 /* does not match either direction */
949 continue;
950 }
951 /* found a reverse match here -- slip through */
952 /* found_reverse_match holds which of D or FloatD we've found */
953 found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
954 } /* endif: not forward match */
955 /* found either forward/reverse 2 operand match here */
956 if (t->operands == 3)
957 {
958 overlap2 = i.types[2] & t->operand_types[2];
959 if (!MATCH (overlap2, i.types[2]) ||
960 !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
961 t->operand_types[0],
962 t->operand_types[2]) ||
963 !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
964 t->operand_types[1],
965 t->operand_types[2]))
966 continue;
967 }
968 /* found either forward/reverse 2 or 3 operand match here:
969 slip through to break */
970 }
971 break; /* we've found a match; break out of loop */
972 } /* for (t = ... */
973 if (t == current_templates->end)
974 { /* we found no match */
975 as_bad ("operands given don't match any known 386 instruction");
976 return;
977 }
978
979 /* Copy the template we found (we may change it!). */
980 memcpy (&i.tm, t, sizeof (template));
981 t = &i.tm; /* alter new copy of template */
982
983 /* If there's no opcode suffix we try to invent one based on register
984 operands. */
985 if (!i.suffix && i.reg_operands)
986 {
987 /* We take i.suffix from the LAST register operand specified. This
988 assumes that the last register operands is the destination register
989 operand. */
990 int o;
991 for (o = 0; o < MAX_OPERANDS; o++)
992 if (i.types[o] & Reg)
993 {
994 i.suffix = (i.types[o] == Reg8) ? BYTE_OPCODE_SUFFIX :
995 (i.types[o] == Reg16) ? WORD_OPCODE_SUFFIX :
996 DWORD_OPCODE_SUFFIX;
997 }
998 }
999
1000 /* Make still unresolved immediate matches conform to size of immediate
1001 given in i.suffix. Note: overlap2 cannot be an immediate!
1002 We assume this. */
1003 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
1004 && overlap0 != Imm8 && overlap0 != Imm8S
1005 && overlap0 != Imm16 && overlap0 != Imm32)
1006 {
1007 if (!i.suffix)
1008 {
1009 as_bad ("no opcode suffix given; can't determine immediate size");
1010 return;
1011 }
1012 overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1013 (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1014 }
1015 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
1016 && overlap1 != Imm8 && overlap1 != Imm8S
1017 && overlap1 != Imm16 && overlap1 != Imm32)
1018 {
1019 if (!i.suffix)
1020 {
1021 as_bad ("no opcode suffix given; can't determine immediate size");
1022 return;
1023 }
1024 overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1025 (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1026 }
1027
1028 i.types[0] = overlap0;
1029 i.types[1] = overlap1;
1030 i.types[2] = overlap2;
1031
1032 if (overlap0 & ImplicitRegister)
1033 i.reg_operands--;
1034 if (overlap1 & ImplicitRegister)
1035 i.reg_operands--;
1036 if (overlap2 & ImplicitRegister)
1037 i.reg_operands--;
1038 if (overlap0 & Imm1)
1039 i.imm_operands = 0; /* kludge for shift insns */
1040
1041 if (found_reverse_match)
1042 {
1043 unsigned int save;
1044 save = t->operand_types[0];
1045 t->operand_types[0] = t->operand_types[1];
1046 t->operand_types[1] = save;
1047 }
1048
1049 /* Finalize opcode. First, we change the opcode based on the operand
1050 size given by i.suffix: we never have to change things for byte insns,
1051 or when no opcode suffix is need to size the operands. */
1052
1053 if (!i.suffix && (t->opcode_modifier & W))
1054 {
1055 as_bad ("no opcode suffix given and no register operands; can't size instruction");
1056 return;
1057 }
1058
1059 if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
1060 {
1061 /* Select between byte and word/dword operations. */
1062 if (t->opcode_modifier & W)
1063 t->base_opcode |= W;
1064 /* Now select between word & dword operations via the
1065 operand size prefix. */
1066 if (i.suffix == WORD_OPCODE_SUFFIX)
1067 {
1068 if (i.prefixes == MAX_PREFIXES)
1069 {
1070 as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
1071 MAX_PREFIXES);
1072 return;
1073 }
1074 i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
1075 }
1076 }
1077
1078 /* For insns with operands there are more diddles to do to the opcode. */
1079 if (i.operands)
1080 {
1081 /* If we found a reverse match we must alter the opcode direction bit
1082 found_reverse_match holds bit to set (different for int &
1083 float insns). */
1084
1085 if (found_reverse_match)
1086 {
1087 t->base_opcode |= found_reverse_match;
1088 }
1089
1090 /* The imul $imm, %reg instruction is converted into
1091 imul $imm, %reg, %reg. */
1092 if (t->opcode_modifier & imulKludge)
1093 {
1094 /* Pretend we saw the 3 operand case. */
1095 i.regs[2] = i.regs[1];
1096 i.reg_operands = 2;
1097 }
1098
1099 /* Certain instructions expect the destination to be in the i.rm.reg
1100 field. This is by far the exceptional case. For these
1101 instructions, if the source operand is a register, we must reverse
1102 the i.rm.reg and i.rm.regmem fields. We accomplish this by faking
1103 that the two register operands were given in the reverse order. */
1104 if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
1105 {
1106 unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
1107 unsigned int second_reg_operand = first_reg_operand + 1;
1108 reg_entry *tmp = i.regs[first_reg_operand];
1109 i.regs[first_reg_operand] = i.regs[second_reg_operand];
1110 i.regs[second_reg_operand] = tmp;
1111 }
1112
1113 if (t->opcode_modifier & ShortForm)
1114 {
1115 /* The register or float register operand is in operand 0 or 1. */
1116 unsigned int o = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
1117 /* Register goes in low 3 bits of opcode. */
1118 t->base_opcode |= i.regs[o]->reg_num;
1119 }
1120 else if (t->opcode_modifier & ShortFormW)
1121 {
1122 /* Short form with 0x8 width bit. Register is always dest. operand */
1123 t->base_opcode |= i.regs[1]->reg_num;
1124 if (i.suffix == WORD_OPCODE_SUFFIX ||
1125 i.suffix == DWORD_OPCODE_SUFFIX)
1126 t->base_opcode |= 0x8;
1127 }
1128 else if (t->opcode_modifier & Seg2ShortForm)
1129 {
1130 if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
1131 {
1132 as_bad ("you can't 'pop cs' on the 386.");
1133 return;
1134 }
1135 t->base_opcode |= (i.regs[0]->reg_num << 3);
1136 }
1137 else if (t->opcode_modifier & Seg3ShortForm)
1138 {
1139 /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
1140 'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
1141 So, only if i.regs[0]->reg_num == 5 (%gs) do we need
1142 to change the opcode. */
1143 if (i.regs[0]->reg_num == 5)
1144 t->base_opcode |= 0x08;
1145 }
1146 else if (t->opcode_modifier & Modrm)
1147 {
1148 /* The opcode is completed (modulo t->extension_opcode which must
1149 be put into the modrm byte.
1150 Now, we make the modrm & index base bytes based on all the info
1151 we've collected. */
1152
1153 /* i.reg_operands MUST be the number of real register operands;
1154 implicit registers do not count. */
1155 if (i.reg_operands == 2)
1156 {
1157 unsigned int source, dest;
1158 source = (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 : 1;
1159 dest = source + 1;
1160 i.rm.mode = 3;
1161 /* We must be careful to make sure that all
1162 segment/control/test/debug registers go into the i.rm.reg
1163 field (despite the whether they are source or destination
1164 operands). */
1165 if (i.regs[dest]->reg_type & (SReg2 | SReg3 | Control | Debug | Test))
1166 {
1167 i.rm.reg = i.regs[dest]->reg_num;
1168 i.rm.regmem = i.regs[source]->reg_num;
1169 }
1170 else
1171 {
1172 i.rm.reg = i.regs[source]->reg_num;
1173 i.rm.regmem = i.regs[dest]->reg_num;
1174 }
1175 }
1176 else
1177 { /* if it's not 2 reg operands... */
1178 if (i.mem_operands)
1179 {
1180 unsigned int fake_zero_displacement = 0;
1181 unsigned int o = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
1182
1183 /* Encode memory operand into modrm byte and base index
1184 byte. */
1185
1186 if (i.base_reg == esp && !i.index_reg)
1187 {
1188 /* <disp>(%esp) becomes two byte modrm with no index
1189 register. */
1190 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1191 i.rm.mode = mode_from_disp_size (i.types[o]);
1192 i.bi.base = ESP_REG_NUM;
1193 i.bi.index = NO_INDEX_REGISTER;
1194 i.bi.scale = 0; /* Must be zero! */
1195 }
1196 else if (i.base_reg == ebp && !i.index_reg)
1197 {
1198 if (!(i.types[o] & Disp))
1199 {
1200 /* Must fake a zero byte displacement. There is
1201 no direct way to code '(%ebp)' directly. */
1202 fake_zero_displacement = 1;
1203 /* fake_zero_displacement code does not set this. */
1204 i.types[o] |= Disp8;
1205 }
1206 i.rm.mode = mode_from_disp_size (i.types[o]);
1207 i.rm.regmem = EBP_REG_NUM;
1208 }
1209 else if (!i.base_reg && (i.types[o] & BaseIndex))
1210 {
1211 /* There are three cases here.
1212 Case 1: '<32bit disp>(,1)' -- indirect absolute.
1213 (Same as cases 2 & 3 with NO index register)
1214 Case 2: <32bit disp> (,<index>) -- no base register with disp
1215 Case 3: (, <index>) --- no base register;
1216 no disp (must add 32bit 0 disp). */
1217 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1218 i.rm.mode = 0; /* 32bit mode */
1219 i.bi.base = NO_BASE_REGISTER;
1220 i.types[o] &= ~Disp;
1221 i.types[o] |= Disp32; /* Must be 32bit! */
1222 if (i.index_reg)
1223 { /* case 2 or case 3 */
1224 i.bi.index = i.index_reg->reg_num;
1225 i.bi.scale = i.log2_scale_factor;
1226 if (i.disp_operands == 0)
1227 fake_zero_displacement = 1; /* case 3 */
1228 }
1229 else
1230 {
1231 i.bi.index = NO_INDEX_REGISTER;
1232 i.bi.scale = 0;
1233 }
1234 }
1235 else if (i.disp_operands && !i.base_reg && !i.index_reg)
1236 {
1237 /* Operand is just <32bit disp> */
1238 i.rm.regmem = EBP_REG_NUM;
1239 i.rm.mode = 0;
1240 i.types[o] &= ~Disp;
1241 i.types[o] |= Disp32;
1242 }
1243 else
1244 {
1245 /* It's not a special case; rev'em up. */
1246 i.rm.regmem = i.base_reg->reg_num;
1247 i.rm.mode = mode_from_disp_size (i.types[o]);
1248 if (i.index_reg)
1249 {
1250 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1251 i.bi.base = i.base_reg->reg_num;
1252 i.bi.index = i.index_reg->reg_num;
1253 i.bi.scale = i.log2_scale_factor;
1254 if (i.base_reg == ebp && i.disp_operands == 0)
1255 { /* pace */
1256 fake_zero_displacement = 1;
1257 i.types[o] |= Disp8;
1258 i.rm.mode = mode_from_disp_size (i.types[o]);
1259 }
1260 }
1261 }
1262 if (fake_zero_displacement)
1263 {
1264 /* Fakes a zero displacement assuming that i.types[o]
1265 holds the correct displacement size. */
1266 exp = &disp_expressions[i.disp_operands++];
1267 i.disps[o] = exp;
1268 exp->X_op = O_constant;
1269 exp->X_add_number = 0;
1270 exp->X_add_symbol = (symbolS *) 0;
1271 exp->X_op_symbol = (symbolS *) 0;
1272 }
1273
1274 /* Select the correct segment for the memory operand. */
1275 if (i.seg)
1276 {
1277 unsigned int seg_index;
1278 const seg_entry *default_seg;
1279
1280 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
1281 {
1282 seg_index = (i.rm.mode << 3) | i.bi.base;
1283 default_seg = two_byte_segment_defaults[seg_index];
1284 }
1285 else
1286 {
1287 seg_index = (i.rm.mode << 3) | i.rm.regmem;
1288 default_seg = one_byte_segment_defaults[seg_index];
1289 }
1290 /* If the specified segment is not the default, use an
1291 opcode prefix to select it */
1292 if (i.seg != default_seg)
1293 {
1294 if (i.prefixes == MAX_PREFIXES)
1295 {
1296 as_bad ("%d prefixes given and %s segment override gives too many prefixes",
1297 MAX_PREFIXES, i.seg->seg_name);
1298 return;
1299 }
1300 i.prefix[i.prefixes++] = i.seg->seg_prefix;
1301 }
1302 }
1303 }
1304
1305 /* Fill in i.rm.reg or i.rm.regmem field with register operand
1306 (if any) based on t->extension_opcode. Again, we must be
1307 careful to make sure that segment/control/debug/test
1308 registers are coded into the i.rm.reg field. */
1309 if (i.reg_operands)
1310 {
1311 unsigned int o =
1312 (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 :
1313 (i.types[1] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 1 : 2;
1314 /* If there is an extension opcode to put here, the
1315 register number must be put into the regmem field. */
1316 if (t->extension_opcode != None)
1317 i.rm.regmem = i.regs[o]->reg_num;
1318 else
1319 i.rm.reg = i.regs[o]->reg_num;
1320
1321 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
1322 we must set it to 3 to indicate this is a register
1323 operand int the regmem field */
1324 if (!i.mem_operands)
1325 i.rm.mode = 3;
1326 }
1327
1328 /* Fill in i.rm.reg field with extension opcode (if any). */
1329 if (t->extension_opcode != None)
1330 i.rm.reg = t->extension_opcode;
1331 }
1332 }
1333 }
1334 }
1335
1336 /* Handle conversion of 'int $3' --> special int3 insn. */
1337 if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
1338 {
1339 t->base_opcode = INT3_OPCODE;
1340 i.imm_operands = 0;
1341 }
1342
1343 /* We are ready to output the insn. */
1344 {
1345 register char *p;
1346
1347 /* Output jumps. */
1348 if (t->opcode_modifier & Jump)
1349 {
1350 int n = i.disps[0]->X_add_number;
1351
1352 if (i.disps[0]->X_op == O_constant)
1353 {
1354 if (fits_in_signed_byte (n))
1355 {
1356 p = frag_more (2);
1357 p[0] = t->base_opcode;
1358 p[1] = n;
1359 }
1360 #if 0 /* leave out 16 bit jumps - pace */
1361 else if (fits_in_signed_word (n))
1362 {
1363 p = frag_more (4);
1364 p[0] = WORD_PREFIX_OPCODE;
1365 p[1] = t->base_opcode;
1366 md_number_to_chars (&p[2], n, 2);
1367 }
1368 #endif
1369 else
1370 { /* It's an absolute dword displacement. */
1371 if (t->base_opcode == JUMP_PC_RELATIVE)
1372 { /* pace */
1373 /* unconditional jump */
1374 p = frag_more (5);
1375 p[0] = (char) 0xe9;
1376 md_number_to_chars (&p[1], n, 4);
1377 }
1378 else
1379 {
1380 /* conditional jump */
1381 p = frag_more (6);
1382 p[0] = TWO_BYTE_OPCODE_ESCAPE;
1383 p[1] = t->base_opcode + 0x10;
1384 md_number_to_chars (&p[2], n, 4);
1385 }
1386 }
1387 }
1388 else
1389 {
1390 /* It's a symbol; end frag & setup for relax.
1391 Make sure there are more than 6 chars left in the current frag;
1392 if not we'll have to start a new one. */
1393 if (obstack_room (&frags) <= 6)
1394 {
1395 frag_wane (frag_now);
1396 frag_new (0);
1397 }
1398 p = frag_more (1);
1399 p[0] = t->base_opcode;
1400 frag_var (rs_machine_dependent,
1401 6, /* 2 opcode/prefix + 4 displacement */
1402 1,
1403 ((unsigned char) *p == JUMP_PC_RELATIVE
1404 ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
1405 : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
1406 i.disps[0]->X_add_symbol,
1407 n, p);
1408 }
1409 }
1410 else if (t->opcode_modifier & (JumpByte | JumpDword))
1411 {
1412 int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
1413 int n = i.disps[0]->X_add_number;
1414
1415 if (fits_in_unsigned_byte (t->base_opcode))
1416 {
1417 FRAG_APPEND_1_CHAR (t->base_opcode);
1418 }
1419 else
1420 {
1421 p = frag_more (2); /* opcode can be at most two bytes */
1422 /* put out high byte first: can't use md_number_to_chars! */
1423 *p++ = (t->base_opcode >> 8) & 0xff;
1424 *p = t->base_opcode & 0xff;
1425 }
1426
1427 p = frag_more (size);
1428 if (i.disps[0]->X_op == O_constant)
1429 {
1430 md_number_to_chars (p, n, size);
1431 if (size == 1 && !fits_in_signed_byte (n))
1432 {
1433 as_bad ("loop/jecx only takes byte displacement; %d shortened to %d",
1434 n, *p);
1435 }
1436 }
1437 else
1438 {
1439 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
1440 i.disps[0], 1, reloc (size, 1));
1441 }
1442 }
1443 else if (t->opcode_modifier & JumpInterSegment)
1444 {
1445 p = frag_more (1 + 2 + 4); /* 1 opcode; 2 segment; 4 offset */
1446 p[0] = t->base_opcode;
1447 if (i.imms[1]->X_op == O_constant)
1448 md_number_to_chars (p + 1, i.imms[1]->X_add_number, 4);
1449 else
1450 fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
1451 i.imms[1], 0, BFD_RELOC_32);
1452 if (i.imms[0]->X_op != O_constant)
1453 as_bad ("can't handle non absolute segment in long call/jmp");
1454 md_number_to_chars (p + 5, i.imms[0]->X_add_number, 2);
1455 }
1456 else
1457 {
1458 /* Output normal instructions here. */
1459 unsigned char *q;
1460
1461 /* First the prefix bytes. */
1462 for (q = i.prefix; q < i.prefix + i.prefixes; q++)
1463 {
1464 p = frag_more (1);
1465 md_number_to_chars (p, (unsigned int) *q, 1);
1466 }
1467
1468 /* Now the opcode; be careful about word order here! */
1469 if (fits_in_unsigned_byte (t->base_opcode))
1470 {
1471 FRAG_APPEND_1_CHAR (t->base_opcode);
1472 }
1473 else if (fits_in_unsigned_word (t->base_opcode))
1474 {
1475 p = frag_more (2);
1476 /* put out high byte first: can't use md_number_to_chars! */
1477 *p++ = (t->base_opcode >> 8) & 0xff;
1478 *p = t->base_opcode & 0xff;
1479 }
1480 else
1481 { /* opcode is either 3 or 4 bytes */
1482 if (t->base_opcode & 0xff000000)
1483 {
1484 p = frag_more (4);
1485 *p++ = (t->base_opcode >> 24) & 0xff;
1486 }
1487 else
1488 p = frag_more (3);
1489 *p++ = (t->base_opcode >> 16) & 0xff;
1490 *p++ = (t->base_opcode >> 8) & 0xff;
1491 *p = (t->base_opcode) & 0xff;
1492 }
1493
1494 /* Now the modrm byte and base index byte (if present). */
1495 if (t->opcode_modifier & Modrm)
1496 {
1497 p = frag_more (1);
1498 /* md_number_to_chars (p, i.rm, 1); */
1499 md_number_to_chars (p, (i.rm.regmem << 0 | i.rm.reg << 3 | i.rm.mode << 6), 1);
1500 /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
1501 ==> need second modrm byte. */
1502 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
1503 {
1504 p = frag_more (1);
1505 /* md_number_to_chars (p, i.bi, 1); */
1506 md_number_to_chars (p, (i.bi.base << 0 | i.bi.index << 3 | i.bi.scale << 6), 1);
1507 }
1508 }
1509
1510 if (i.disp_operands)
1511 {
1512 register unsigned int n;
1513
1514 for (n = 0; n < i.operands; n++)
1515 {
1516 if (i.disps[n])
1517 {
1518 if (i.disps[n]->X_op == O_constant)
1519 {
1520 if (i.types[n] & (Disp8 | Abs8))
1521 {
1522 p = frag_more (1);
1523 md_number_to_chars (p, i.disps[n]->X_add_number, 1);
1524 }
1525 else if (i.types[n] & (Disp16 | Abs16))
1526 {
1527 p = frag_more (2);
1528 md_number_to_chars (p, i.disps[n]->X_add_number, 2);
1529 }
1530 else
1531 { /* Disp32|Abs32 */
1532 p = frag_more (4);
1533 md_number_to_chars (p, i.disps[n]->X_add_number, 4);
1534 }
1535 }
1536 else
1537 { /* not absolute_section */
1538 /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
1539 p = frag_more (4);
1540 fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
1541 i.disps[n], 0, BFD_RELOC_32);
1542 }
1543 }
1544 }
1545 } /* end displacement output */
1546
1547 /* output immediate */
1548 if (i.imm_operands)
1549 {
1550 register unsigned int n;
1551
1552 for (n = 0; n < i.operands; n++)
1553 {
1554 if (i.imms[n])
1555 {
1556 if (i.imms[n]->X_op == O_constant)
1557 {
1558 if (i.types[n] & (Imm8 | Imm8S))
1559 {
1560 p = frag_more (1);
1561 md_number_to_chars (p, i.imms[n]->X_add_number, 1);
1562 }
1563 else if (i.types[n] & Imm16)
1564 {
1565 p = frag_more (2);
1566 md_number_to_chars (p, i.imms[n]->X_add_number, 2);
1567 }
1568 else
1569 {
1570 p = frag_more (4);
1571 md_number_to_chars (p, i.imms[n]->X_add_number, 4);
1572 }
1573 }
1574 else
1575 { /* not absolute_section */
1576 /* need a 32-bit fixup (don't support 8bit non-absolute ims) */
1577 /* try to support other sizes ... */
1578 int size;
1579 if (i.types[n] & (Imm8 | Imm8S))
1580 size = 1;
1581 else if (i.types[n] & Imm16)
1582 size = 2;
1583 else
1584 size = 4;
1585 p = frag_more (size);
1586 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
1587 i.imms[n], 0, reloc (size, 0));
1588 }
1589 }
1590 }
1591 } /* end immediate output */
1592 }
1593
1594 #ifdef DEBUG386
1595 if (flagseen['D'])
1596 {
1597 pi (line, &i);
1598 }
1599 #endif /* DEBUG386 */
1600
1601 }
1602 return;
1603 }
1604 \f
1605 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
1606 on error. */
1607
1608 static int
1609 i386_operand (operand_string)
1610 char *operand_string;
1611 {
1612 register char *op_string = operand_string;
1613
1614 /* Address of '\0' at end of operand_string. */
1615 char *end_of_operand_string = operand_string + strlen (operand_string);
1616
1617 /* Start and end of displacement string expression (if found). */
1618 char *displacement_string_start = NULL;
1619 char *displacement_string_end = NULL;
1620
1621 /* We check for an absolute prefix (differentiating,
1622 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
1623 if (*op_string == ABSOLUTE_PREFIX)
1624 {
1625 op_string++;
1626 i.types[this_operand] |= JumpAbsolute;
1627 }
1628
1629 /* Check if operand is a register. */
1630 if (*op_string == REGISTER_PREFIX)
1631 {
1632 register reg_entry *r;
1633 if (!(r = parse_register (op_string)))
1634 {
1635 as_bad ("bad register name ('%s')", op_string);
1636 return 0;
1637 }
1638 /* Check for segment override, rather than segment register by
1639 searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
1640 if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
1641 {
1642 switch (r->reg_num)
1643 {
1644 case 0:
1645 i.seg = (seg_entry *) & es;
1646 break;
1647 case 1:
1648 i.seg = (seg_entry *) & cs;
1649 break;
1650 case 2:
1651 i.seg = (seg_entry *) & ss;
1652 break;
1653 case 3:
1654 i.seg = (seg_entry *) & ds;
1655 break;
1656 case 4:
1657 i.seg = (seg_entry *) & fs;
1658 break;
1659 case 5:
1660 i.seg = (seg_entry *) & gs;
1661 break;
1662 }
1663 op_string += 4; /* skip % <x> s : */
1664 operand_string = op_string; /* Pretend given string starts here. */
1665 if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
1666 && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
1667 {
1668 as_bad ("bad memory operand after segment override");
1669 return 0;
1670 }
1671 /* Handle case of %es:*foo. */
1672 if (*op_string == ABSOLUTE_PREFIX)
1673 {
1674 op_string++;
1675 i.types[this_operand] |= JumpAbsolute;
1676 }
1677 goto do_memory_reference;
1678 }
1679 i.types[this_operand] |= r->reg_type;
1680 i.regs[this_operand] = r;
1681 i.reg_operands++;
1682 }
1683 else if (*op_string == IMMEDIATE_PREFIX)
1684 { /* ... or an immediate */
1685 char *save_input_line_pointer;
1686 segT exp_seg = 0;
1687 expressionS *exp;
1688
1689 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
1690 {
1691 as_bad ("only 1 or 2 immediate operands are allowed");
1692 return 0;
1693 }
1694
1695 exp = &im_expressions[i.imm_operands++];
1696 i.imms[this_operand] = exp;
1697 save_input_line_pointer = input_line_pointer;
1698 input_line_pointer = ++op_string; /* must advance op_string! */
1699 exp_seg = expression (exp);
1700 input_line_pointer = save_input_line_pointer;
1701
1702 if (exp->X_op == O_absent)
1703 {
1704 /* missing or bad expr becomes absolute 0 */
1705 as_bad ("missing or invalid immediate expression '%s' taken as 0",
1706 operand_string);
1707 exp->X_op = O_constant;
1708 exp->X_add_number = 0;
1709 exp->X_add_symbol = (symbolS *) 0;
1710 exp->X_op_symbol = (symbolS *) 0;
1711 i.types[this_operand] |= Imm;
1712 }
1713 else if (exp->X_op == O_constant)
1714 {
1715 i.types[this_operand] |= smallest_imm_type (exp->X_add_number);
1716 }
1717 #ifdef OBJ_AOUT
1718 else if (exp_seg != text_section
1719 && exp_seg != data_section
1720 && exp_seg != bss_section
1721 && exp_seg != undefined_section
1722 #ifdef BFD_ASSEMBLER
1723 && ! bfd_is_com_section (exp_seg)
1724 #endif
1725 )
1726 {
1727 seg_unimplemented:
1728 as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
1729 return 0;
1730 }
1731 #endif
1732 else
1733 {
1734 /* this is an address ==> 32bit */
1735 i.types[this_operand] |= Imm32;
1736 }
1737 /* shorten this type of this operand if the instruction wants
1738 * fewer bits than are present in the immediate. The bit field
1739 * code can put out 'andb $0xffffff, %al', for example. pace
1740 * also 'movw $foo,(%eax)'
1741 */
1742 switch (i.suffix)
1743 {
1744 case WORD_OPCODE_SUFFIX:
1745 i.types[this_operand] |= Imm16;
1746 break;
1747 case BYTE_OPCODE_SUFFIX:
1748 i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
1749 break;
1750 }
1751 }
1752 else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
1753 || *op_string == '(')
1754 {
1755 /* This is a memory reference of some sort. */
1756 register char *base_string;
1757 unsigned int found_base_index_form;
1758
1759 do_memory_reference:
1760 if (i.mem_operands == MAX_MEMORY_OPERANDS)
1761 {
1762 as_bad ("more than 1 memory reference in instruction");
1763 return 0;
1764 }
1765 i.mem_operands++;
1766
1767 /* Determine type of memory operand from opcode_suffix;
1768 no opcode suffix implies general memory references. */
1769 switch (i.suffix)
1770 {
1771 case BYTE_OPCODE_SUFFIX:
1772 i.types[this_operand] |= Mem8;
1773 break;
1774 case WORD_OPCODE_SUFFIX:
1775 i.types[this_operand] |= Mem16;
1776 break;
1777 case DWORD_OPCODE_SUFFIX:
1778 default:
1779 i.types[this_operand] |= Mem32;
1780 }
1781
1782 /* Check for base index form. We detect the base index form by
1783 looking for an ')' at the end of the operand, searching
1784 for the '(' matching it, and finding a REGISTER_PREFIX or ','
1785 after it. */
1786 base_string = end_of_operand_string - 1;
1787 found_base_index_form = 0;
1788 if (*base_string == ')')
1789 {
1790 unsigned int parens_balenced = 1;
1791 /* We've already checked that the number of left & right ()'s are
1792 equal, so this loop will not be infinite. */
1793 do
1794 {
1795 base_string--;
1796 if (*base_string == ')')
1797 parens_balenced++;
1798 if (*base_string == '(')
1799 parens_balenced--;
1800 }
1801 while (parens_balenced);
1802 base_string++; /* Skip past '('. */
1803 if (*base_string == REGISTER_PREFIX || *base_string == ',')
1804 found_base_index_form = 1;
1805 }
1806
1807 /* If we can't parse a base index register expression, we've found
1808 a pure displacement expression. We set up displacement_string_start
1809 and displacement_string_end for the code below. */
1810 if (!found_base_index_form)
1811 {
1812 displacement_string_start = op_string;
1813 displacement_string_end = end_of_operand_string;
1814 }
1815 else
1816 {
1817 char *base_reg_name, *index_reg_name, *num_string;
1818 int num;
1819
1820 i.types[this_operand] |= BaseIndex;
1821
1822 /* If there is a displacement set-up for it to be parsed later. */
1823 if (base_string != op_string + 1)
1824 {
1825 displacement_string_start = op_string;
1826 displacement_string_end = base_string - 1;
1827 }
1828
1829 /* Find base register (if any). */
1830 if (*base_string != ',')
1831 {
1832 base_reg_name = base_string++;
1833 /* skip past register name & parse it */
1834 while (isalpha (*base_string))
1835 base_string++;
1836 if (base_string == base_reg_name + 1)
1837 {
1838 as_bad ("can't find base register name after '(%c'",
1839 REGISTER_PREFIX);
1840 return 0;
1841 }
1842 END_STRING_AND_SAVE (base_string);
1843 if (!(i.base_reg = parse_register (base_reg_name)))
1844 {
1845 as_bad ("bad base register name ('%s')", base_reg_name);
1846 return 0;
1847 }
1848 RESTORE_END_STRING (base_string);
1849 }
1850
1851 /* Now check seperator; must be ',' ==> index reg
1852 OR num ==> no index reg. just scale factor
1853 OR ')' ==> end. (scale factor = 1) */
1854 if (*base_string != ',' && *base_string != ')')
1855 {
1856 as_bad ("expecting ',' or ')' after base register in `%s'",
1857 operand_string);
1858 return 0;
1859 }
1860
1861 /* There may index reg here; and there may be a scale factor. */
1862 if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
1863 {
1864 index_reg_name = ++base_string;
1865 while (isalpha (*++base_string));
1866 END_STRING_AND_SAVE (base_string);
1867 if (!(i.index_reg = parse_register (index_reg_name)))
1868 {
1869 as_bad ("bad index register name ('%s')", index_reg_name);
1870 return 0;
1871 }
1872 RESTORE_END_STRING (base_string);
1873 }
1874
1875 /* Check for scale factor. */
1876 if (*base_string == ',' && isdigit (*(base_string + 1)))
1877 {
1878 num_string = ++base_string;
1879 while (is_digit_char (*base_string))
1880 base_string++;
1881 if (base_string == num_string)
1882 {
1883 as_bad ("can't find a scale factor after ','");
1884 return 0;
1885 }
1886 END_STRING_AND_SAVE (base_string);
1887 /* We've got a scale factor. */
1888 if (!sscanf (num_string, "%d", &num))
1889 {
1890 as_bad ("can't parse scale factor from '%s'", num_string);
1891 return 0;
1892 }
1893 RESTORE_END_STRING (base_string);
1894 switch (num)
1895 { /* must be 1 digit scale */
1896 case 1:
1897 i.log2_scale_factor = 0;
1898 break;
1899 case 2:
1900 i.log2_scale_factor = 1;
1901 break;
1902 case 4:
1903 i.log2_scale_factor = 2;
1904 break;
1905 case 8:
1906 i.log2_scale_factor = 3;
1907 break;
1908 default:
1909 as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
1910 return 0;
1911 }
1912 }
1913 else
1914 {
1915 if (!i.index_reg && *base_string == ',')
1916 {
1917 as_bad ("expecting index register or scale factor after ','; got '%c'",
1918 *(base_string + 1));
1919 return 0;
1920 }
1921 }
1922 }
1923
1924 /* If there's an expression begining the operand, parse it,
1925 assuming displacement_string_start and displacement_string_end
1926 are meaningful. */
1927 if (displacement_string_start)
1928 {
1929 register expressionS *exp;
1930 segT exp_seg = 0;
1931 char *save_input_line_pointer;
1932 exp = &disp_expressions[i.disp_operands];
1933 i.disps[this_operand] = exp;
1934 i.disp_operands++;
1935 save_input_line_pointer = input_line_pointer;
1936 input_line_pointer = displacement_string_start;
1937 END_STRING_AND_SAVE (displacement_string_end);
1938 exp_seg = expression (exp);
1939 if (*input_line_pointer)
1940 as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
1941 RESTORE_END_STRING (displacement_string_end);
1942 input_line_pointer = save_input_line_pointer;
1943 if (exp->X_op == O_absent)
1944 {
1945 /* missing expr becomes absolute 0 */
1946 as_bad ("missing or invalid displacement '%s' taken as 0",
1947 operand_string);
1948 i.types[this_operand] |= (Disp | Abs);
1949 exp->X_op = O_constant;
1950 exp->X_add_number = 0;
1951 exp->X_add_symbol = (symbolS *) 0;
1952 exp->X_op_symbol = (symbolS *) 0;
1953 }
1954 else if (exp->X_op == O_constant)
1955 {
1956 i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
1957 }
1958 else if (exp_seg == text_section
1959 || exp_seg == data_section
1960 || exp_seg == bss_section
1961 || exp_seg == undefined_section)
1962 {
1963 i.types[this_operand] |= Disp32;
1964 }
1965 else
1966 {
1967 #ifndef OBJ_AOUT
1968 i.types[this_operand] |= Disp32;
1969 #else
1970 goto seg_unimplemented;
1971 #endif
1972 }
1973 }
1974
1975 /* Make sure the memory operand we've been dealt is valid. */
1976 if (i.base_reg && i.index_reg &&
1977 !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
1978 {
1979 as_bad ("register size mismatch in (base,index,scale) expression");
1980 return 0;
1981 }
1982 /*
1983 * special case for (%dx) while doing input/output op
1984 */
1985 if ((i.base_reg &&
1986 (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
1987 (i.index_reg == 0)))
1988 {
1989 i.types[this_operand] |= InOutPortReg;
1990 return 1;
1991 }
1992 if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
1993 (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
1994 {
1995 as_bad ("base/index register must be 32 bit register");
1996 return 0;
1997 }
1998 if (i.index_reg && i.index_reg == esp)
1999 {
2000 as_bad ("%s may not be used as an index register", esp->reg_name);
2001 return 0;
2002 }
2003 }
2004 else
2005 { /* it's not a memory operand; argh! */
2006 as_bad ("invalid char %s begining %s operand '%s'",
2007 output_invalid (*op_string), ordinal_names[this_operand],
2008 op_string);
2009 return 0;
2010 }
2011 return 1; /* normal return */
2012 }
2013 \f
2014 /*
2015 * md_estimate_size_before_relax()
2016 *
2017 * Called just before relax().
2018 * Any symbol that is now undefined will not become defined.
2019 * Return the correct fr_subtype in the frag.
2020 * Return the initial "guess for fr_var" to caller.
2021 * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
2022 * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
2023 * Although it may not be explicit in the frag, pretend fr_var starts with a
2024 * 0 value.
2025 */
2026 int
2027 md_estimate_size_before_relax (fragP, segment)
2028 register fragS *fragP;
2029 register segT segment;
2030 {
2031 register unsigned char *opcode;
2032 register int old_fr_fix;
2033
2034 old_fr_fix = fragP->fr_fix;
2035 opcode = (unsigned char *) fragP->fr_opcode;
2036 /* We've already got fragP->fr_subtype right; all we have to do is check
2037 for un-relaxable symbols. */
2038 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
2039 {
2040 /* symbol is undefined in this segment */
2041 switch (opcode[0])
2042 {
2043 case JUMP_PC_RELATIVE: /* make jmp (0xeb) a dword displacement jump */
2044 opcode[0] = 0xe9; /* dword disp jmp */
2045 fragP->fr_fix += 4;
2046 fix_new (fragP, old_fr_fix, 4,
2047 fragP->fr_symbol,
2048 fragP->fr_offset, 1, BFD_RELOC_32_PCREL);
2049 break;
2050
2051 default:
2052 /* This changes the byte-displacement jump 0x7N -->
2053 the dword-displacement jump 0x0f8N */
2054 opcode[1] = opcode[0] + 0x10;
2055 opcode[0] = TWO_BYTE_OPCODE_ESCAPE; /* two-byte escape */
2056 fragP->fr_fix += 1 + 4; /* we've added an opcode byte */
2057 fix_new (fragP, old_fr_fix + 1, 4,
2058 fragP->fr_symbol,
2059 fragP->fr_offset, 1, BFD_RELOC_32_PCREL);
2060 break;
2061 }
2062 frag_wane (fragP);
2063 }
2064 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
2065 } /* md_estimate_size_before_relax() */
2066 \f
2067 /*
2068 * md_convert_frag();
2069 *
2070 * Called after relax() is finished.
2071 * In: Address of frag.
2072 * fr_type == rs_machine_dependent.
2073 * fr_subtype is what the address relaxed to.
2074 *
2075 * Out: Any fixSs and constants are set up.
2076 * Caller will turn frag into a ".space 0".
2077 */
2078 #ifndef BFD_ASSEMBLER
2079 void
2080 md_convert_frag (headers, fragP)
2081 object_headers *headers;
2082 register fragS *fragP;
2083 #else
2084 void
2085 md_convert_frag (abfd, sec, fragP)
2086 bfd *abfd;
2087 segT sec;
2088 register fragS *fragP;
2089 #endif
2090 {
2091 register unsigned char *opcode;
2092 unsigned char *where_to_put_displacement = NULL;
2093 unsigned int target_address;
2094 unsigned int opcode_address;
2095 unsigned int extension = 0;
2096 int displacement_from_opcode_start;
2097
2098 opcode = (unsigned char *) fragP->fr_opcode;
2099
2100 /* Address we want to reach in file space. */
2101 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
2102 #ifdef BFD_ASSEMBLER /* not needed otherwise? */
2103 target_address += fragP->fr_symbol->sy_frag->fr_address;
2104 #endif
2105
2106 /* Address opcode resides at in file space. */
2107 opcode_address = fragP->fr_address + fragP->fr_fix;
2108
2109 /* Displacement from opcode start to fill into instruction. */
2110 displacement_from_opcode_start = target_address - opcode_address;
2111
2112 switch (fragP->fr_subtype)
2113 {
2114 case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
2115 case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
2116 /* don't have to change opcode */
2117 extension = 1; /* 1 opcode + 1 displacement */
2118 where_to_put_displacement = &opcode[1];
2119 break;
2120
2121 case ENCODE_RELAX_STATE (COND_JUMP, WORD):
2122 opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
2123 opcode[2] = opcode[0] + 0x10;
2124 opcode[0] = WORD_PREFIX_OPCODE;
2125 extension = 4; /* 3 opcode + 2 displacement */
2126 where_to_put_displacement = &opcode[3];
2127 break;
2128
2129 case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
2130 opcode[1] = 0xe9;
2131 opcode[0] = WORD_PREFIX_OPCODE;
2132 extension = 3; /* 2 opcode + 2 displacement */
2133 where_to_put_displacement = &opcode[2];
2134 break;
2135
2136 case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
2137 opcode[1] = opcode[0] + 0x10;
2138 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
2139 extension = 5; /* 2 opcode + 4 displacement */
2140 where_to_put_displacement = &opcode[2];
2141 break;
2142
2143 case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
2144 opcode[0] = 0xe9;
2145 extension = 4; /* 1 opcode + 4 displacement */
2146 where_to_put_displacement = &opcode[1];
2147 break;
2148
2149 default:
2150 BAD_CASE (fragP->fr_subtype);
2151 break;
2152 }
2153 /* now put displacement after opcode */
2154 md_number_to_chars ((char *) where_to_put_displacement,
2155 displacement_from_opcode_start - extension,
2156 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
2157 fragP->fr_fix += extension;
2158 }
2159 \f
2160
2161 int md_short_jump_size = 2; /* size of byte displacement jmp */
2162 int md_long_jump_size = 5; /* size of dword displacement jmp */
2163 const int md_reloc_size = 8; /* Size of relocation record */
2164
2165 void
2166 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
2167 char *ptr;
2168 addressT from_addr, to_addr;
2169 fragS *frag;
2170 symbolS *to_symbol;
2171 {
2172 long offset;
2173
2174 offset = to_addr - (from_addr + 2);
2175 md_number_to_chars (ptr, (long) 0xeb, 1); /* opcode for byte-disp jump */
2176 md_number_to_chars (ptr + 1, offset, 1);
2177 }
2178
2179 void
2180 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
2181 char *ptr;
2182 addressT from_addr, to_addr;
2183 fragS *frag;
2184 symbolS *to_symbol;
2185 {
2186 long offset;
2187
2188 if (flagseen['m'])
2189 {
2190 offset = to_addr - S_GET_VALUE (to_symbol);
2191 md_number_to_chars (ptr, 0xe9, 1); /* opcode for long jmp */
2192 md_number_to_chars (ptr + 1, offset, 4);
2193 fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
2194 to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
2195 }
2196 else
2197 {
2198 offset = to_addr - (from_addr + 5);
2199 md_number_to_chars (ptr, (long) 0xe9, 1);
2200 md_number_to_chars (ptr + 1, offset, 4);
2201 }
2202 }
2203 \f
2204 void /* Knows about order of bytes in address. */
2205 md_number_to_chars (con, value, nbytes)
2206 char con[]; /* Return 'nbytes' of chars here. */
2207 valueT value; /* The value of the bits. */
2208 int nbytes; /* Number of bytes in the output. */
2209 {
2210 register char *p = con;
2211
2212 switch (nbytes)
2213 {
2214 case 1:
2215 p[0] = value & 0xff;
2216 break;
2217 case 2:
2218 p[0] = value & 0xff;
2219 p[1] = (value >> 8) & 0xff;
2220 break;
2221 case 4:
2222 p[0] = value & 0xff;
2223 p[1] = (value >> 8) & 0xff;
2224 p[2] = (value >> 16) & 0xff;
2225 p[3] = (value >> 24) & 0xff;
2226 break;
2227 default:
2228 BAD_CASE (nbytes);
2229 }
2230 }
2231
2232
2233 /* Apply a fixup (fixS) to segment data, once it has been determined
2234 by our caller that we have all the info we need to fix it up.
2235
2236 On the 386, immediates, displacements, and data pointers are all in
2237 the same (little-endian) format, so we don't need to care about which
2238 we are handling. */
2239
2240 static void
2241 md_apply_fix_1 (fixP, value)
2242 fixS *fixP; /* The fix we're to put in */
2243 long value; /* The value of the bits. */
2244 {
2245 register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
2246
2247 #ifdef BFD_ASSEMBLER
2248 /*
2249 * This is a hack. There should be a better way to
2250 * handle this.
2251 */
2252 if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
2253 {
2254 value += fixP->fx_where + fixP->fx_frag->fr_address;
2255 }
2256 #endif
2257
2258 switch (fixP->fx_size)
2259 {
2260 case 1:
2261 *p = value;
2262 break;
2263 case 2:
2264 *p++ = value;
2265 *p = (value >> 8);
2266 break;
2267 case 4:
2268 *p++ = value;
2269 *p++ = (value >> 8);
2270 *p++ = (value >> 16);
2271 *p = (value >> 24);
2272 break;
2273 default:
2274 BAD_CASE (fixP->fx_size);
2275 }
2276 }
2277
2278 #ifdef BFD_ASSEMBLER
2279 int
2280 md_apply_fix (fixP, valp)
2281 fixS *fixP;
2282 valueT *valp;
2283 {
2284 md_apply_fix_1 (fixP, *valp);
2285 return 1;
2286 }
2287 #else
2288 void
2289 md_apply_fix (fixP, val)
2290 fixS *fixP;
2291 long val;
2292 {
2293 md_apply_fix_1 (fixP, val);
2294 }
2295 #endif
2296
2297 long /* Knows about the byte order in a word. */
2298 md_chars_to_number (con, nbytes)
2299 unsigned char con[]; /* Low order byte 1st. */
2300 int nbytes; /* Number of bytes in the input. */
2301 {
2302 long retval;
2303 for (retval = 0, con += nbytes - 1; nbytes--; con--)
2304 {
2305 retval <<= BITS_PER_CHAR;
2306 retval |= *con;
2307 }
2308 return retval;
2309 }
2310
2311 \f
2312
2313 #define MAX_LITTLENUMS 6
2314
2315 /* Turn the string pointed to by litP into a floating point constant of type
2316 type, and emit the appropriate bytes. The number of LITTLENUMS emitted
2317 is stored in *sizeP . An error message is returned, or NULL on OK. */
2318 char *
2319 md_atof (type, litP, sizeP)
2320 char type;
2321 char *litP;
2322 int *sizeP;
2323 {
2324 int prec;
2325 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2326 LITTLENUM_TYPE *wordP;
2327 char *t;
2328
2329 switch (type)
2330 {
2331 case 'f':
2332 case 'F':
2333 prec = 2;
2334 break;
2335
2336 case 'd':
2337 case 'D':
2338 prec = 4;
2339 break;
2340
2341 case 'x':
2342 case 'X':
2343 prec = 5;
2344 break;
2345
2346 default:
2347 *sizeP = 0;
2348 return "Bad call to md_atof ()";
2349 }
2350 t = atof_ieee (input_line_pointer, type, words);
2351 if (t)
2352 input_line_pointer = t;
2353
2354 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2355 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
2356 the bigendian 386. */
2357 for (wordP = words + prec - 1; prec--;)
2358 {
2359 md_number_to_chars (litP, (long) (*wordP--), sizeof (LITTLENUM_TYPE));
2360 litP += sizeof (LITTLENUM_TYPE);
2361 }
2362 return 0;
2363 }
2364 \f
2365 char output_invalid_buf[8];
2366
2367 static char *
2368 output_invalid (c)
2369 char c;
2370 {
2371 if (isprint (c))
2372 sprintf (output_invalid_buf, "'%c'", c);
2373 else
2374 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
2375 return output_invalid_buf;
2376 }
2377
2378 /* reg_string starts *before* REGISTER_PREFIX */
2379 static reg_entry *
2380 parse_register (reg_string)
2381 char *reg_string;
2382 {
2383 register char *s = reg_string;
2384 register char *p;
2385 char reg_name_given[MAX_REG_NAME_SIZE];
2386
2387 s++; /* skip REGISTER_PREFIX */
2388 for (p = reg_name_given; is_register_char (*s); p++, s++)
2389 {
2390 *p = register_chars[(unsigned char) *s];
2391 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
2392 return (reg_entry *) 0;
2393 }
2394 *p = '\0';
2395 return (reg_entry *) hash_find (reg_hash, reg_name_given);
2396 }
2397
2398
2399 /* We have no need to default values of symbols. */
2400
2401 /* ARGSUSED */
2402 symbolS *
2403 md_undefined_symbol (name)
2404 char *name;
2405 {
2406 return 0;
2407 }
2408
2409 /* Parse an operand that is machine-specific.
2410 We just return without modifying the expression if we have nothing
2411 to do. */
2412
2413 /* ARGSUSED */
2414 void
2415 md_operand (expressionP)
2416 expressionS *expressionP;
2417 {
2418 }
2419
2420 /* Round up a section size to the appropriate boundary. */
2421 valueT
2422 md_section_align (segment, size)
2423 segT segment;
2424 valueT size;
2425 {
2426 return size; /* Byte alignment is fine */
2427 }
2428
2429 /* Exactly what point is a PC-relative offset relative TO? On the
2430 i386, they're relative to the address of the offset, plus its
2431 size. (??? Is this right? FIXME-SOON!) */
2432 long
2433 md_pcrel_from (fixP)
2434 fixS *fixP;
2435 {
2436 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2437 }
2438
2439 #ifndef I386COFF
2440
2441 static void
2442 s_bss ()
2443 {
2444 register int temp;
2445
2446 temp = get_absolute_expression ();
2447 #ifdef BFD_ASSEMBLER
2448 subseg_set (bss_section, (subsegT) temp);
2449 #else
2450 subseg_new (bss_section, (subsegT) temp);
2451 #endif
2452 demand_empty_rest_of_line ();
2453 }
2454
2455 #endif
2456
2457
2458 #ifdef BFD_ASSEMBLER
2459
2460 arelent *
2461 tc_gen_reloc (section, fixp)
2462 asection *section;
2463 fixS *fixp;
2464 {
2465 arelent *reloc;
2466 bfd_reloc_code_real_type code;
2467
2468 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
2469 switch (F (fixp->fx_size, fixp->fx_pcrel))
2470 {
2471 #define MAP(SZ,PCREL,TYPE) case F(SZ,PCREL): code = (TYPE); break
2472 MAP (1, 0, BFD_RELOC_8);
2473 MAP (2, 0, BFD_RELOC_16);
2474 MAP (4, 0, BFD_RELOC_32);
2475 MAP (1, 1, BFD_RELOC_8_PCREL);
2476 MAP (2, 1, BFD_RELOC_16_PCREL);
2477 MAP (4, 1, BFD_RELOC_32_PCREL);
2478 default:
2479 abort ();
2480 }
2481 #undef MAP
2482 #undef F
2483
2484 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
2485 assert (reloc != 0);
2486 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2487 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2488 if (fixp->fx_pcrel)
2489 reloc->addend = fixp->fx_addnumber;
2490 else
2491 reloc->addend = 0;
2492
2493 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2494 assert (reloc->howto != 0);
2495
2496 return reloc;
2497 }
2498
2499 #else /* ! BFD_ASSEMBLER */
2500
2501 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
2502 void
2503 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
2504 char *where;
2505 fixS *fixP;
2506 relax_addressT segment_address_in_file;
2507 {
2508 /*
2509 * In: length of relocation (or of address) in chars: 1, 2 or 4.
2510 * Out: GNU LD relocation length code: 0, 1, or 2.
2511 */
2512
2513 static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
2514 long r_symbolnum;
2515
2516 know (fixP->fx_addsy != NULL);
2517
2518 md_number_to_chars (where,
2519 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
2520 4);
2521
2522 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
2523 ? S_GET_TYPE (fixP->fx_addsy)
2524 : fixP->fx_addsy->sy_number);
2525
2526 where[6] = (r_symbolnum >> 16) & 0x0ff;
2527 where[5] = (r_symbolnum >> 8) & 0x0ff;
2528 where[4] = r_symbolnum & 0x0ff;
2529 where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
2530 | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
2531 | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
2532 }
2533
2534 #endif /* OBJ_AOUT or OBJ_BOUT */
2535
2536 #if defined (I386COFF)
2537
2538 short
2539 tc_coff_fix2rtype (fixP)
2540 fixS *fixP;
2541 {
2542 return (fixP->fx_pcrel ?
2543 (fixP->fx_size == 1 ? R_PCRBYTE :
2544 fixP->fx_size == 2 ? R_PCRWORD :
2545 R_PCRLONG) :
2546 (fixP->fx_size == 1 ? R_RELBYTE :
2547 fixP->fx_size == 2 ? R_RELWORD :
2548 R_DIR32));
2549 }
2550
2551 int
2552 tc_coff_sizemachdep (frag)
2553 fragS *frag;
2554 {
2555 if (frag->fr_next)
2556 return (frag->fr_next->fr_address - frag->fr_address);
2557 else
2558 return 0;
2559 }
2560
2561 #endif /* I386COFF */
2562
2563 #endif /* BFD_ASSEMBLER? */
2564
2565 /* end of tc-i386.c */