Restore "call|jmp [xtrn]" in x86 assembler.
[binutils.git] / gas / config / tc-i386-intel.c
blob163634405307e0cd77c45d121ffe34c6056bab2a
1 /* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
2 Copyright 2009
3 Free Software Foundation, Inc.
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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 static struct
24 operatorT op_modifier; /* Operand modifier. */
25 int is_mem; /* 1 if operand is memory reference. */
26 int is_indirect; /* 1 if operand is indirect reference. */
27 int has_offset; /* 1 if operand has offset. */
28 unsigned int in_offset; /* >=1 if processing operand of offset. */
29 unsigned int in_bracket; /* >=1 if processing operand in brackets. */
30 unsigned int in_scale; /* >=1 if processing multipication operand
31 * in brackets. */
32 i386_operand_type reloc_types; /* Value obtained from lex_got(). */
33 const reg_entry *base; /* Base register (if any). */
34 const reg_entry *index; /* Index register (if any). */
35 offsetT scale_factor; /* Accumulated scale factor. */
36 symbolS *seg;
38 intel_state;
40 /* offset X_add_symbol */
41 #define O_offset O_md32
42 /* offset X_add_symbol */
43 #define O_short O_md31
44 /* near ptr X_add_symbol */
45 #define O_near_ptr O_md30
46 /* far ptr X_add_symbol */
47 #define O_far_ptr O_md29
48 /* byte ptr X_add_symbol */
49 #define O_byte_ptr O_md28
50 /* word ptr X_add_symbol */
51 #define O_word_ptr O_md27
52 /* dword ptr X_add_symbol */
53 #define O_dword_ptr O_md26
54 /* qword ptr X_add_symbol */
55 #define O_qword_ptr O_md25
56 /* oword ptr X_add_symbol */
57 #define O_oword_ptr O_md24
58 /* fword ptr X_add_symbol */
59 #define O_fword_ptr O_md23
60 /* tbyte ptr X_add_symbol */
61 #define O_tbyte_ptr O_md22
62 /* xmmword ptr X_add_symbol */
63 #define O_xmmword_ptr O_md21
64 /* ymmword ptr X_add_symbol */
65 #define O_ymmword_ptr O_md20
67 static struct
69 const char *name;
70 operatorT op;
71 unsigned int operands;
73 const i386_operators[] =
75 { "and", O_bit_and, 2 },
76 { "eq", O_eq, 2 },
77 { "ge", O_ge, 2 },
78 { "gt", O_gt, 2 },
79 { "le", O_le, 2 },
80 { "lt", O_lt, 2 },
81 { "mod", O_modulus, 2 },
82 { "ne", O_ne, 2 },
83 { "not", O_bit_not, 1 },
84 { "offset", O_offset, 1 },
85 { "or", O_bit_inclusive_or, 2 },
86 { "shl", O_left_shift, 2 },
87 { "short", O_short, 1 },
88 { "shr", O_right_shift, 2 },
89 { "xor", O_bit_exclusive_or, 2 },
90 { NULL, O_illegal, 0 }
93 static struct
95 const char *name;
96 operatorT op;
97 unsigned short sz[3];
99 const i386_types[] =
101 #define I386_TYPE(t, n) { #t, O_##t##_ptr, { n, n, n } }
102 I386_TYPE(byte, 1),
103 I386_TYPE(word, 2),
104 I386_TYPE(dword, 4),
105 I386_TYPE(fword, 6),
106 I386_TYPE(qword, 8),
107 I386_TYPE(tbyte, 10),
108 I386_TYPE(oword, 16),
109 I386_TYPE(xmmword, 16),
110 I386_TYPE(ymmword, 32),
111 #undef I386_TYPE
112 { "near", O_near_ptr, { 0xff04, 0xff02, 0xff08 } },
113 { "far", O_far_ptr, { 0xff06, 0xff05, 0xff06 } },
114 { NULL, O_illegal, { 0, 0, 0 } }
117 operatorT i386_operator (const char *name, unsigned int operands, char *pc)
119 unsigned int j;
121 if (!intel_syntax)
122 return O_absent;
124 if (!name)
126 if (operands != 2)
127 return O_illegal;
128 switch (*input_line_pointer)
130 case ':':
131 ++input_line_pointer;
132 return O_full_ptr;
133 case '[':
134 ++input_line_pointer;
135 return O_index;
136 case '@':
137 if (this_operand >= 0 && i.reloc[this_operand] == NO_RELOC)
139 int adjust = 0;
140 char *gotfree_input_line = lex_got (&i.reloc[this_operand],
141 &adjust,
142 &intel_state.reloc_types);
144 if (!gotfree_input_line)
145 break;
146 free (gotfree_input_line);
147 *input_line_pointer++ = '+';
148 memset (input_line_pointer, '0', adjust - 1);
149 input_line_pointer[adjust - 1] = ' ';
150 return O_add;
152 break;
154 return O_illegal;
157 for (j = 0; i386_operators[j].name; ++j)
158 if (strcasecmp (i386_operators[j].name, name) == 0)
160 if (i386_operators[j].operands
161 && i386_operators[j].operands != operands)
162 return O_illegal;
163 return i386_operators[j].op;
166 for (j = 0; i386_types[j].name; ++j)
167 if (strcasecmp (i386_types[j].name, name) == 0)
168 break;
169 if (i386_types[j].name && *pc == ' ')
171 char *pname = ++input_line_pointer;
172 char c = get_symbol_end ();
174 if (strcasecmp (pname, "ptr") == 0)
176 pname[-1] = *pc;
177 *pc = c;
178 if (intel_syntax > 0 || operands != 1)
179 return O_illegal;
180 return i386_types[j].op;
183 *input_line_pointer = c;
184 input_line_pointer = pname - 1;
187 return O_absent;
190 static int i386_intel_parse_name (const char *name, expressionS *e)
192 unsigned int j;
194 for (j = 0; i386_types[j].name; ++j)
195 if (strcasecmp(i386_types[j].name, name) == 0)
197 e->X_op = O_constant;
198 e->X_add_number = i386_types[j].sz[flag_code];
199 e->X_add_symbol = NULL;
200 e->X_op_symbol = NULL;
201 return 1;
204 return 0;
207 static INLINE int i386_intel_check (const reg_entry *rreg,
208 const reg_entry *base,
209 const reg_entry *iindex)
211 if ((this_operand >= 0 && rreg != i.op[this_operand].regs)
212 || base != intel_state.base || iindex != intel_state.index)
214 as_bad (_("invalid use of register"));
215 return 0;
217 return 1;
220 static INLINE void i386_intel_fold (expressionS *e, symbolS *sym)
222 if (S_GET_SEGMENT (sym) == absolute_section)
224 offsetT val = e->X_add_number;
226 *e = *symbol_get_value_expression (sym);
227 e->X_add_number += val;
229 else
231 e->X_add_symbol = sym;
232 e->X_op_symbol = NULL;
233 e->X_op = O_symbol;
237 static int
238 i386_intel_simplify_register (expressionS *e)
240 int reg_num;
242 if (this_operand < 0 || intel_state.in_offset)
244 as_bad (_("invalid use of register"));
245 return 0;
248 if (e->X_op == O_register)
249 reg_num = e->X_add_number;
250 else
251 reg_num = e->X_md - 1;
253 if (!intel_state.in_bracket)
255 if (i.op[this_operand].regs)
257 as_bad (_("invalid use of register"));
258 return 0;
260 if (i386_regtab[reg_num].reg_type.bitfield.sreg3
261 && i386_regtab[reg_num].reg_num == RegFlat)
263 as_bad (_("invalid use of pseudo-register"));
264 return 0;
266 i.op[this_operand].regs = i386_regtab + reg_num;
268 else if (!intel_state.base && !intel_state.in_scale)
269 intel_state.base = i386_regtab + reg_num;
270 else if (!intel_state.index)
271 intel_state.index = i386_regtab + reg_num;
272 else
274 /* esp is invalid as index */
275 intel_state.index = i386_regtab + REGNAM_EAX + 4;
277 return 2;
280 static int i386_intel_simplify (expressionS *);
282 static INLINE int i386_intel_simplify_symbol(symbolS *sym)
284 int ret = i386_intel_simplify (symbol_get_value_expression (sym));
286 if (ret == 2)
288 S_SET_SEGMENT(sym, absolute_section);
289 ret = 1;
291 return ret;
294 static int i386_intel_simplify (expressionS *e)
296 const reg_entry *the_reg = this_operand >= 0 ? i.op[this_operand].regs : NULL;
297 const reg_entry *base = intel_state.base;
298 const reg_entry *state_index = intel_state.index;
299 int ret;
301 if (!intel_syntax)
302 return 1;
304 switch (e->X_op)
306 case O_index:
307 if (e->X_add_symbol)
309 if (!i386_intel_simplify_symbol (e->X_add_symbol)
310 || !i386_intel_check(the_reg, intel_state.base, intel_state.index))
311 return 0;;
313 if (!intel_state.in_offset)
314 ++intel_state.in_bracket;
315 ret = i386_intel_simplify_symbol (e->X_op_symbol);
316 if (!intel_state.in_offset)
317 --intel_state.in_bracket;
318 if (!ret)
319 return 0;
320 if (e->X_add_symbol)
321 e->X_op = O_add;
322 else
323 i386_intel_fold (e, e->X_op_symbol);
324 break;
326 case O_offset:
327 intel_state.has_offset = 1;
328 ++intel_state.in_offset;
329 ret = i386_intel_simplify_symbol (e->X_add_symbol);
330 --intel_state.in_offset;
331 if (!ret || !i386_intel_check(the_reg, base, state_index))
332 return 0;
333 i386_intel_fold (e, e->X_add_symbol);
334 return ret;
336 case O_byte_ptr:
337 case O_word_ptr:
338 case O_dword_ptr:
339 case O_fword_ptr:
340 case O_qword_ptr:
341 case O_tbyte_ptr:
342 case O_oword_ptr:
343 case O_xmmword_ptr:
344 case O_ymmword_ptr:
345 case O_near_ptr:
346 case O_far_ptr:
347 if (intel_state.op_modifier == O_absent)
348 intel_state.op_modifier = e->X_op;
349 /* FALLTHROUGH */
350 case O_short:
351 if (symbol_get_value_expression (e->X_add_symbol)->X_op == O_register)
353 as_bad (_("invalid use of register"));
354 return 0;
356 if (!i386_intel_simplify_symbol (e->X_add_symbol))
357 return 0;
358 i386_intel_fold (e, e->X_add_symbol);
359 break;
361 case O_full_ptr:
362 if (symbol_get_value_expression (e->X_op_symbol)->X_op == O_register)
364 as_bad (_("invalid use of register"));
365 return 0;
367 if (!i386_intel_simplify_symbol (e->X_op_symbol)
368 || !i386_intel_check(the_reg, intel_state.base, intel_state.index))
369 return 0;
370 if (!intel_state.in_offset)
371 intel_state.seg = e->X_add_symbol;
372 i386_intel_fold (e, e->X_op_symbol);
373 break;
375 case O_multiply:
376 if (this_operand >= 0 && intel_state.in_bracket)
378 expressionS *scale = NULL;
380 if (intel_state.index)
381 --scale;
383 if (!intel_state.in_scale++)
384 intel_state.scale_factor = 1;
386 ret = i386_intel_simplify_symbol (e->X_add_symbol);
387 if (ret && !scale && intel_state.index)
388 scale = symbol_get_value_expression (e->X_op_symbol);
390 if (ret)
391 ret = i386_intel_simplify_symbol (e->X_op_symbol);
392 if (ret && !scale && intel_state.index)
393 scale = symbol_get_value_expression (e->X_add_symbol);
395 if (ret && scale && (scale + 1))
397 resolve_expression (scale);
398 if (scale->X_op != O_constant
399 || intel_state.index->reg_type.bitfield.reg16)
400 scale->X_add_number = 0;
401 intel_state.scale_factor *= scale->X_add_number;
404 --intel_state.in_scale;
405 if (!ret)
406 return 0;
408 if (!intel_state.in_scale)
409 switch (intel_state.scale_factor)
411 case 1:
412 i.log2_scale_factor = 0;
413 break;
414 case 2:
415 i.log2_scale_factor = 1;
416 break;
417 case 4:
418 i.log2_scale_factor = 2;
419 break;
420 case 8:
421 i.log2_scale_factor = 3;
422 break;
423 default:
424 /* esp is invalid as index */
425 intel_state.index = i386_regtab + REGNAM_EAX + 4;
426 break;
429 break;
432 case O_register:
433 ret = i386_intel_simplify_register (e);
434 if (ret == 2)
436 gas_assert (e->X_add_number < (unsigned short) -1);
437 e->X_md = (unsigned short) e->X_add_number + 1;
438 e->X_op = O_constant;
439 e->X_add_number = 0;
441 return ret;
443 case O_constant:
444 if (e->X_md)
445 return i386_intel_simplify_register (e);
447 /* FALLTHROUGH */
448 default:
449 if (e->X_add_symbol && !i386_intel_simplify_symbol (e->X_add_symbol))
450 return 0;
451 if (e->X_op == O_add || e->X_op == O_subtract)
453 base = intel_state.base;
454 state_index = intel_state.index;
456 if (!i386_intel_check (the_reg, base, state_index)
457 || (e->X_op_symbol && !i386_intel_simplify_symbol (e->X_op_symbol))
458 || !i386_intel_check (the_reg,
459 e->X_op != O_add ? base : intel_state.base,
460 e->X_op != O_add ? state_index : intel_state.index))
461 return 0;
462 break;
465 if (this_operand >= 0 && e->X_op == O_symbol && !intel_state.in_offset)
467 segT seg = S_GET_SEGMENT (e->X_add_symbol);
469 if (seg != absolute_section
470 && seg != reg_section
471 && seg != expr_section)
472 intel_state.is_mem |= 2 - !intel_state.in_bracket;
475 return 1;
478 int i386_need_index_operator (void)
480 return intel_syntax < 0;
483 static int
484 i386_intel_operand (char *operand_string, int got_a_float)
486 char *saved_input_line_pointer, *buf;
487 segT exp_seg;
488 expressionS exp, *expP;
489 char suffix = 0;
490 int ret;
492 /* Initialize state structure. */
493 intel_state.op_modifier = O_absent;
494 intel_state.is_mem = 0;
495 intel_state.is_indirect = 0;
496 intel_state.has_offset = 0;
497 intel_state.base = NULL;
498 intel_state.index = NULL;
499 intel_state.seg = NULL;
500 operand_type_set (&intel_state.reloc_types, ~0);
501 gas_assert (!intel_state.in_offset);
502 gas_assert (!intel_state.in_bracket);
503 gas_assert (!intel_state.in_scale);
505 saved_input_line_pointer = input_line_pointer;
506 input_line_pointer = buf = xstrdup (operand_string);
508 /* A '$' followed by an identifier char is an identifier. Otherwise,
509 it's operator '.' followed by an expression. */
510 if (*buf == '$' && !is_identifier_char (buf[1]))
511 *buf = '.';
513 intel_syntax = -1;
514 memset (&exp, 0, sizeof(exp));
515 exp_seg = expression (&exp);
516 ret = i386_intel_simplify (&exp);
517 intel_syntax = 1;
519 SKIP_WHITESPACE ();
520 if (!is_end_of_line[(unsigned char) *input_line_pointer])
522 as_bad (_("junk `%s' after expression"), input_line_pointer);
523 ret = 0;
525 else if (exp.X_op == O_illegal || exp.X_op == O_absent)
527 as_bad (_("invalid expression"));
528 ret = 0;
530 else if (!intel_state.has_offset
531 && input_line_pointer > buf
532 && *(input_line_pointer - 1) == ']')
534 intel_state.is_mem |= 1;
535 intel_state.is_indirect = 1;
538 input_line_pointer = saved_input_line_pointer;
539 free (buf);
541 gas_assert (!intel_state.in_offset);
542 gas_assert (!intel_state.in_bracket);
543 gas_assert (!intel_state.in_scale);
545 if (!ret)
546 return 0;
548 if (intel_state.op_modifier != O_absent
549 && current_templates->start->base_opcode != 0x8d /* lea */)
551 i.types[this_operand].bitfield.unspecified = 0;
553 switch (intel_state.op_modifier)
555 case O_byte_ptr:
556 i.types[this_operand].bitfield.byte = 1;
557 suffix = BYTE_MNEM_SUFFIX;
558 break;
560 case O_word_ptr:
561 i.types[this_operand].bitfield.word = 1;
562 if ((current_templates->start->name[0] == 'l'
563 && current_templates->start->name[2] == 's'
564 && current_templates->start->name[3] == 0)
565 || current_templates->start->base_opcode == 0x62 /* bound */)
566 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
567 else if (got_a_float == 2) /* "fi..." */
568 suffix = SHORT_MNEM_SUFFIX;
569 else
570 suffix = WORD_MNEM_SUFFIX;
571 break;
573 case O_dword_ptr:
574 i.types[this_operand].bitfield.dword = 1;
575 if ((current_templates->start->name[0] == 'l'
576 && current_templates->start->name[2] == 's'
577 && current_templates->start->name[3] == 0)
578 || current_templates->start->base_opcode == 0x62 /* bound */)
579 suffix = WORD_MNEM_SUFFIX;
580 else if (flag_code == CODE_16BIT
581 && (current_templates->start->opcode_modifier.jump
582 || current_templates->start->opcode_modifier.jumpdword))
583 suffix = LONG_DOUBLE_MNEM_SUFFIX;
584 else if (got_a_float == 1) /* "f..." */
585 suffix = SHORT_MNEM_SUFFIX;
586 else
587 suffix = LONG_MNEM_SUFFIX;
588 break;
590 case O_fword_ptr:
591 i.types[this_operand].bitfield.fword = 1;
592 if (current_templates->start->name[0] == 'l'
593 && current_templates->start->name[2] == 's'
594 && current_templates->start->name[3] == 0)
595 suffix = LONG_MNEM_SUFFIX;
596 else if (!got_a_float)
598 if (flag_code == CODE_16BIT)
599 add_prefix (DATA_PREFIX_OPCODE);
600 suffix = LONG_DOUBLE_MNEM_SUFFIX;
602 else
603 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
604 break;
606 case O_qword_ptr:
607 i.types[this_operand].bitfield.qword = 1;
608 if (current_templates->start->base_opcode == 0x62 /* bound */
609 || got_a_float == 1) /* "f..." */
610 suffix = LONG_MNEM_SUFFIX;
611 else
612 suffix = QWORD_MNEM_SUFFIX;
613 break;
615 case O_tbyte_ptr:
616 i.types[this_operand].bitfield.tbyte = 1;
617 if (got_a_float == 1)
618 suffix = LONG_DOUBLE_MNEM_SUFFIX;
619 else
620 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
621 break;
623 case O_oword_ptr:
624 case O_xmmword_ptr:
625 i.types[this_operand].bitfield.xmmword = 1;
626 suffix = XMMWORD_MNEM_SUFFIX;
627 break;
629 case O_ymmword_ptr:
630 i.types[this_operand].bitfield.ymmword = 1;
631 suffix = YMMWORD_MNEM_SUFFIX;
632 break;
634 case O_far_ptr:
635 suffix = LONG_DOUBLE_MNEM_SUFFIX;
636 /* FALLTHROUGH */
637 case O_near_ptr:
638 if (!current_templates->start->opcode_modifier.jump
639 && !current_templates->start->opcode_modifier.jumpdword)
640 suffix = got_a_float /* so it will cause an error */
641 ? BYTE_MNEM_SUFFIX
642 : LONG_DOUBLE_MNEM_SUFFIX;
643 break;
645 default:
646 BAD_CASE (intel_state.op_modifier);
647 break;
650 if (!i.suffix)
651 i.suffix = suffix;
652 else if (i.suffix != suffix)
654 as_bad (_("conflicting operand size modifiers"));
655 return 0;
659 /* Operands for jump/call need special consideration. */
660 if (current_templates->start->opcode_modifier.jump
661 || current_templates->start->opcode_modifier.jumpdword
662 || current_templates->start->opcode_modifier.jumpintersegment)
664 if (i.op[this_operand].regs || intel_state.base || intel_state.index
665 || intel_state.is_mem > 1)
666 i.types[this_operand].bitfield.jumpabsolute = 1;
667 else
668 switch (intel_state.op_modifier)
670 case O_near_ptr:
671 if (intel_state.seg)
672 i.types[this_operand].bitfield.jumpabsolute = 1;
673 else
674 intel_state.is_mem = 1;
675 break;
676 case O_far_ptr:
677 case O_absent:
678 if (!intel_state.seg)
680 intel_state.is_mem = 1;
681 if (intel_state.op_modifier == O_absent)
683 if (intel_state.is_indirect == 1)
684 i.types[this_operand].bitfield.jumpabsolute = 1;
685 break;
687 as_bad (_("cannot infer the segment part of the operand"));
688 return 0;
690 else if (S_GET_SEGMENT (intel_state.seg) == reg_section)
691 i.types[this_operand].bitfield.jumpabsolute = 1;
692 else
694 i386_operand_type types;
696 if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
698 as_bad (_("at most %d immediate operands are allowed"),
699 MAX_IMMEDIATE_OPERANDS);
700 return 0;
702 expP = &im_expressions[i.imm_operands++];
703 memset (expP, 0, sizeof(*expP));
704 expP->X_op = O_symbol;
705 expP->X_add_symbol = intel_state.seg;
706 i.op[this_operand].imms = expP;
708 resolve_expression (expP);
709 operand_type_set (&types, ~0);
710 if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg),
711 expP, types, operand_string))
712 return 0;
713 if (i.operands < MAX_OPERANDS)
715 this_operand = i.operands++;
716 i.types[this_operand].bitfield.unspecified = 1;
718 if (suffix == LONG_DOUBLE_MNEM_SUFFIX)
719 i.suffix = 0;
720 intel_state.seg = NULL;
721 intel_state.is_mem = 0;
723 break;
724 default:
725 i.types[this_operand].bitfield.jumpabsolute = 1;
726 break;
728 if (i.types[this_operand].bitfield.jumpabsolute)
729 intel_state.is_mem |= 1;
731 else if (intel_state.seg)
732 intel_state.is_mem |= 1;
734 if (i.op[this_operand].regs)
736 i386_operand_type temp;
738 /* Register operand. */
739 if (intel_state.base || intel_state.index || intel_state.seg)
741 as_bad (_("invalid operand"));
742 return 0;
745 temp = i.op[this_operand].regs->reg_type;
746 temp.bitfield.baseindex = 0;
747 i.types[this_operand] = operand_type_or (i.types[this_operand], temp);
748 i.types[this_operand].bitfield.unspecified = 0;
749 ++i.reg_operands;
751 else if (intel_state.base || intel_state.index || intel_state.seg
752 || intel_state.is_mem)
754 /* Memory operand. */
755 if (i.mem_operands
756 >= 2 - !current_templates->start->opcode_modifier.isstring)
758 /* Handle
760 call 0x9090,0x90909090
761 lcall 0x9090,0x90909090
762 jmp 0x9090,0x90909090
763 ljmp 0x9090,0x90909090
766 if ((current_templates->start->opcode_modifier.jumpintersegment
767 || current_templates->start->opcode_modifier.jumpdword
768 || current_templates->start->opcode_modifier.jump)
769 && this_operand == 1
770 && intel_state.seg == NULL
771 && i.mem_operands == 1
772 && i.disp_operands == 1
773 && intel_state.op_modifier == O_absent)
775 /* Try to process the first operand as immediate, */
776 this_operand = 0;
777 if (i386_finalize_immediate (exp_seg, i.op[0].imms,
778 intel_state.reloc_types,
779 NULL))
781 this_operand = 1;
782 expP = &im_expressions[0];
783 i.op[this_operand].imms = expP;
784 *expP = exp;
786 /* Try to process the second operand as immediate, */
787 if (i386_finalize_immediate (exp_seg, expP,
788 intel_state.reloc_types,
789 NULL))
791 i.mem_operands = 0;
792 i.disp_operands = 0;
793 i.imm_operands = 2;
794 i.types[0].bitfield.mem = 0;
795 i.types[0].bitfield.disp16 = 0;
796 i.types[0].bitfield.disp32 = 0;
797 i.types[0].bitfield.disp32s = 0;
798 return 1;
803 as_bad (_("too many memory references for `%s'"),
804 current_templates->start->name);
805 return 0;
808 expP = &disp_expressions[i.disp_operands];
809 memcpy (expP, &exp, sizeof(exp));
810 resolve_expression (expP);
812 if (expP->X_op != O_constant || expP->X_add_number
813 || (!intel_state.base && !intel_state.index))
815 i.op[this_operand].disps = expP;
816 i.disp_operands++;
818 if (flag_code == CODE_64BIT)
820 i.types[this_operand].bitfield.disp32 = 1;
821 if (!i.prefix[ADDR_PREFIX])
823 i.types[this_operand].bitfield.disp64 = 1;
824 i.types[this_operand].bitfield.disp32s = 1;
827 else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
828 i.types[this_operand].bitfield.disp32 = 1;
829 else
830 i.types[this_operand].bitfield.disp16 = 1;
832 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
834 * exp_seg is used only for verification in
835 * i386_finalize_displacement, and we can end up seeing reg_section
836 * here - but we know we removed all registers from the expression
837 * (or error-ed on any remaining ones) in i386_intel_simplify. I
838 * consider the check in i386_finalize_displacement bogus anyway, in
839 * particular because it doesn't allow for expr_section, so I'd
840 * rather see that check (and the similar one in
841 * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
842 * expert I can't really say whether that would have other bad side
843 * effects.
845 if (OUTPUT_FLAVOR == bfd_target_aout_flavour
846 && exp_seg == reg_section)
847 exp_seg = expP->X_op != O_constant ? undefined_section
848 : absolute_section;
849 #endif
851 if (!i386_finalize_displacement (exp_seg, expP,
852 intel_state.reloc_types,
853 operand_string))
854 return 0;
857 if (intel_state.base || intel_state.index)
858 i.types[this_operand].bitfield.baseindex = 1;
860 if (intel_state.seg)
862 for (;;)
864 expP = symbol_get_value_expression (intel_state.seg);
865 if (expP->X_op != O_full_ptr)
866 break;
867 intel_state.seg = expP->X_add_symbol;
869 if (expP->X_op != O_register)
871 as_bad (_("segment register name expected"));
872 return 0;
874 if (!i386_regtab[expP->X_add_number].reg_type.bitfield.sreg2
875 && !i386_regtab[expP->X_add_number].reg_type.bitfield.sreg3)
877 as_bad (_("invalid use of register"));
878 return 0;
880 switch (i386_regtab[expP->X_add_number].reg_num)
882 case 0: i.seg[i.mem_operands] = &es; break;
883 case 1: i.seg[i.mem_operands] = &cs; break;
884 case 2: i.seg[i.mem_operands] = &ss; break;
885 case 3: i.seg[i.mem_operands] = &ds; break;
886 case 4: i.seg[i.mem_operands] = &fs; break;
887 case 5: i.seg[i.mem_operands] = &gs; break;
888 case RegFlat: i.seg[i.mem_operands] = NULL; break;
892 /* Swap base and index in 16-bit memory operands like
893 [si+bx]. Since i386_index_check is also used in AT&T
894 mode we have to do that here. */
895 if (intel_state.base
896 && intel_state.index
897 && intel_state.base->reg_type.bitfield.reg16
898 && intel_state.index->reg_type.bitfield.reg16
899 && intel_state.base->reg_num >= 6
900 && intel_state.index->reg_num < 6)
902 i.base_reg = intel_state.index;
903 i.index_reg = intel_state.base;
905 else
907 i.base_reg = intel_state.base;
908 i.index_reg = intel_state.index;
911 if (!i386_index_check (operand_string))
912 return 0;
914 i.types[this_operand].bitfield.mem = 1;
915 ++i.mem_operands;
917 else
919 /* Immediate. */
920 if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
922 as_bad (_("at most %d immediate operands are allowed"),
923 MAX_IMMEDIATE_OPERANDS);
924 return 0;
927 expP = &im_expressions[i.imm_operands++];
928 i.op[this_operand].imms = expP;
929 *expP = exp;
931 return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
932 operand_string);
935 return 1;