xref: /netbsd-src/external/gpl3/binutils.old/dist/gas/config/tc-i386-intel.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
2    Copyright (C) 2009-2018 Free Software Foundation, Inc.
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 3, 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 the Free
18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19    02110-1301, USA.  */
20 
21 static struct
22   {
23     operatorT op_modifier;	/* Operand modifier.  */
24     int is_mem;			/* 1 if operand is memory reference.  */
25     int is_indirect;		/* 1 if operand is indirect reference.  */
26     int has_offset;		/* 1 if operand has offset.  */
27     unsigned int in_offset;	/* >=1 if processing operand of offset.  */
28     unsigned int in_bracket;	/* >=1 if processing operand in brackets.  */
29     unsigned int in_scale;	/* >=1 if processing multiplication operand
30 				 * in brackets.  */
31     i386_operand_type reloc_types;	/* Value obtained from lex_got().  */
32     const reg_entry *base;	/* Base register (if any).  */
33     const reg_entry *index;	/* Index register (if any).  */
34     offsetT scale_factor;	/* Accumulated scale factor.  */
35     symbolS *seg;
36   }
37 intel_state;
38 
39 /* offset X_add_symbol */
40 #define O_offset O_md32
41 /* offset X_add_symbol */
42 #define O_short O_md31
43 /* near ptr X_add_symbol */
44 #define O_near_ptr O_md30
45 /* far ptr X_add_symbol */
46 #define O_far_ptr O_md29
47 /* byte ptr X_add_symbol */
48 #define O_byte_ptr O_md28
49 /* word ptr X_add_symbol */
50 #define O_word_ptr O_md27
51 /* dword ptr X_add_symbol */
52 #define O_dword_ptr O_md26
53 /* qword ptr X_add_symbol */
54 #define O_qword_ptr O_md25
55 /* oword ptr X_add_symbol */
56 #define O_oword_ptr O_md24
57 /* fword ptr X_add_symbol */
58 #define O_fword_ptr O_md23
59 /* tbyte ptr X_add_symbol */
60 #define O_tbyte_ptr O_md22
61 /* xmmword ptr X_add_symbol */
62 #define O_xmmword_ptr O_md21
63 /* ymmword ptr X_add_symbol */
64 #define O_ymmword_ptr O_md20
65 /* zmmword ptr X_add_symbol */
66 #define O_zmmword_ptr O_md19
67 
68 static struct
69   {
70     const char *name;
71     operatorT op;
72     unsigned int operands;
73   }
74 const i386_operators[] =
75   {
76     { "and", O_bit_and, 2 },
77     { "eq", O_eq, 2 },
78     { "ge", O_ge, 2 },
79     { "gt", O_gt, 2 },
80     { "le", O_le, 2 },
81     { "lt", O_lt, 2 },
82     { "mod", O_modulus, 2 },
83     { "ne", O_ne, 2 },
84     { "not", O_bit_not, 1 },
85     { "offset", O_offset, 1 },
86     { "or", O_bit_inclusive_or, 2 },
87     { "shl", O_left_shift, 2 },
88     { "short", O_short, 1 },
89     { "shr", O_right_shift, 2 },
90     { "xor", O_bit_exclusive_or, 2 },
91     { NULL, O_illegal, 0 }
92   };
93 
94 static struct
95   {
96     const char *name;
97     operatorT op;
98     unsigned short sz[3];
99   }
100 const i386_types[] =
101   {
102 #define I386_TYPE(t, n) { #t, O_##t##_ptr, { n, n, n } }
103     I386_TYPE(byte, 1),
104     I386_TYPE(word, 2),
105     I386_TYPE(dword, 4),
106     I386_TYPE(fword, 6),
107     I386_TYPE(qword, 8),
108     I386_TYPE(tbyte, 10),
109     I386_TYPE(oword, 16),
110     I386_TYPE(xmmword, 16),
111     I386_TYPE(ymmword, 32),
112     I386_TYPE(zmmword, 64),
113 #undef I386_TYPE
114     { "near", O_near_ptr, { 0xff04, 0xff02, 0xff08 } },
115     { "far", O_far_ptr, { 0xff06, 0xff05, 0xff06 } },
116     { NULL, O_illegal, { 0, 0, 0 } }
117   };
118 
119 operatorT i386_operator (const char *name, unsigned int operands, char *pc)
120 {
121   unsigned int j;
122 
123   if (!intel_syntax)
124     return O_absent;
125 
126   if (!name)
127     {
128       if (operands != 2)
129 	return O_illegal;
130       switch (*input_line_pointer)
131 	{
132 	case ':':
133 	  ++input_line_pointer;
134 	  return O_full_ptr;
135 	case '[':
136 	  ++input_line_pointer;
137 	  return O_index;
138 	case '@':
139 	  if (this_operand >= 0 && i.reloc[this_operand] == NO_RELOC)
140 	    {
141 	      int adjust = 0;
142 	      char *gotfree_input_line = lex_got (&i.reloc[this_operand],
143 						  &adjust,
144 						  &intel_state.reloc_types);
145 
146 	      if (!gotfree_input_line)
147 		break;
148 	      free (gotfree_input_line);
149 	      *input_line_pointer++ = '+';
150 	      memset (input_line_pointer, '0', adjust - 1);
151 	      input_line_pointer[adjust - 1] = ' ';
152 	      return O_add;
153 	    }
154 	  break;
155 	}
156       return O_illegal;
157     }
158 
159   for (j = 0; i386_operators[j].name; ++j)
160     if (strcasecmp (i386_operators[j].name, name) == 0)
161       {
162 	if (i386_operators[j].operands
163 	    && i386_operators[j].operands != operands)
164 	  return O_illegal;
165 	return i386_operators[j].op;
166       }
167 
168   for (j = 0; i386_types[j].name; ++j)
169     if (strcasecmp (i386_types[j].name, name) == 0)
170       break;
171 
172   if (i386_types[j].name && *pc == ' ')
173     {
174       char *pname;
175       char c;
176 
177       ++input_line_pointer;
178       c = get_symbol_name (&pname);
179 
180       if (strcasecmp (pname, "ptr") == 0)
181 	{
182 	  /* FIXME: What if c == '"' ?  */
183 	  pname[-1] = *pc;
184 	  *pc = c;
185 	  if (intel_syntax > 0 || operands != 1)
186 	    return O_illegal;
187 	  return i386_types[j].op;
188 	}
189 
190       (void) restore_line_pointer (c);
191       input_line_pointer = pname - 1;
192     }
193 
194   return O_absent;
195 }
196 
197 static int i386_intel_parse_name (const char *name, expressionS *e)
198 {
199   unsigned int j;
200 
201   if (! strcmp (name, "$"))
202     {
203       current_location (e);
204       return 1;
205     }
206 
207   for (j = 0; i386_types[j].name; ++j)
208     if (strcasecmp(i386_types[j].name, name) == 0)
209       {
210 	e->X_op = O_constant;
211 	e->X_add_number = i386_types[j].sz[flag_code];
212 	e->X_add_symbol = NULL;
213 	e->X_op_symbol = NULL;
214 	return 1;
215       }
216 
217   return 0;
218 }
219 
220 static INLINE int i386_intel_check (const reg_entry *rreg,
221 				    const reg_entry *base,
222 				    const reg_entry *iindex)
223 {
224   if ((this_operand >= 0
225        && rreg != i.op[this_operand].regs)
226       || base != intel_state.base
227       || iindex != intel_state.index)
228     {
229       as_bad (_("invalid use of register"));
230       return 0;
231     }
232   return 1;
233 }
234 
235 static INLINE void i386_intel_fold (expressionS *e, symbolS *sym)
236 {
237   expressionS *exp = symbol_get_value_expression (sym);
238   if (S_GET_SEGMENT (sym) == absolute_section)
239     {
240       offsetT val = e->X_add_number;
241 
242       *e = *exp;
243       e->X_add_number += val;
244     }
245   else
246     {
247       if (exp->X_op == O_symbol
248 	  && strcmp (S_GET_NAME (exp->X_add_symbol),
249 		     GLOBAL_OFFSET_TABLE_NAME) == 0)
250 	sym = exp->X_add_symbol;
251       e->X_add_symbol = sym;
252       e->X_op_symbol = NULL;
253       e->X_op = O_symbol;
254     }
255 }
256 
257 static int
258 i386_intel_simplify_register (expressionS *e)
259 {
260   int reg_num;
261 
262   if (this_operand < 0 || intel_state.in_offset)
263     {
264       as_bad (_("invalid use of register"));
265       return 0;
266     }
267 
268   if (e->X_op == O_register)
269     reg_num = e->X_add_number;
270   else
271     reg_num = e->X_md - 1;
272 
273   if (!intel_state.in_bracket)
274     {
275       if (i.op[this_operand].regs)
276 	{
277 	  as_bad (_("invalid use of register"));
278 	  return 0;
279 	}
280       if (i386_regtab[reg_num].reg_type.bitfield.sreg3
281 	  && i386_regtab[reg_num].reg_num == RegFlat)
282 	{
283 	  as_bad (_("invalid use of pseudo-register"));
284 	  return 0;
285 	}
286       i.op[this_operand].regs = i386_regtab + reg_num;
287     }
288   else if (!intel_state.index
289 	   && (i386_regtab[reg_num].reg_type.bitfield.xmmword
290 	       || i386_regtab[reg_num].reg_type.bitfield.ymmword
291 	       || i386_regtab[reg_num].reg_type.bitfield.zmmword
292 	       || i386_regtab[reg_num].reg_num == RegRiz
293 	       || i386_regtab[reg_num].reg_num == RegEiz))
294     intel_state.index = i386_regtab + reg_num;
295   else if (!intel_state.base && !intel_state.in_scale)
296     intel_state.base = i386_regtab + reg_num;
297   else if (!intel_state.index)
298     {
299       if (intel_state.in_scale
300 	  || current_templates->start->base_opcode == 0xf30f1b /* bndmk */
301 	  || (current_templates->start->base_opcode & ~1) == 0x0f1a /* bnd{ld,st}x */
302 	  || i386_regtab[reg_num].reg_type.bitfield.baseindex)
303 	intel_state.index = i386_regtab + reg_num;
304       else
305 	{
306 	  /* Convert base to index and make ESP/RSP the base.  */
307 	  intel_state.index = intel_state.base;
308 	  intel_state.base = i386_regtab + reg_num;
309 	}
310     }
311   else
312     {
313       /* esp is invalid as index */
314       intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM;
315     }
316   return 2;
317 }
318 
319 static int i386_intel_simplify (expressionS *);
320 
321 static INLINE int i386_intel_simplify_symbol(symbolS *sym)
322 {
323   int ret = i386_intel_simplify (symbol_get_value_expression (sym));
324 
325   if (ret == 2)
326   {
327     S_SET_SEGMENT(sym, absolute_section);
328     ret = 1;
329   }
330   return ret;
331 }
332 
333 static int i386_intel_simplify (expressionS *e)
334 {
335   const reg_entry *the_reg = (this_operand >= 0
336 			      ? i.op[this_operand].regs : NULL);
337   const reg_entry *base = intel_state.base;
338   const reg_entry *state_index = intel_state.index;
339   int ret;
340 
341   if (!intel_syntax)
342     return 1;
343 
344   switch (e->X_op)
345     {
346     case O_index:
347       if (e->X_add_symbol)
348 	{
349 	  if (!i386_intel_simplify_symbol (e->X_add_symbol)
350 	      || !i386_intel_check(the_reg, intel_state.base,
351 				   intel_state.index))
352 	    return 0;
353 	}
354       if (!intel_state.in_offset)
355 	++intel_state.in_bracket;
356       ret = i386_intel_simplify_symbol (e->X_op_symbol);
357       if (!intel_state.in_offset)
358 	--intel_state.in_bracket;
359       if (!ret)
360 	return 0;
361       if (e->X_add_symbol)
362 	e->X_op = O_add;
363       else
364 	i386_intel_fold (e, e->X_op_symbol);
365       break;
366 
367     case O_offset:
368       intel_state.has_offset = 1;
369       ++intel_state.in_offset;
370       ret = i386_intel_simplify_symbol (e->X_add_symbol);
371       --intel_state.in_offset;
372       if (!ret || !i386_intel_check(the_reg, base, state_index))
373 	return 0;
374       i386_intel_fold (e, e->X_add_symbol);
375       return ret;
376 
377     case O_byte_ptr:
378     case O_word_ptr:
379     case O_dword_ptr:
380     case O_fword_ptr:
381     case O_qword_ptr:
382     case O_tbyte_ptr:
383     case O_oword_ptr:
384     case O_xmmword_ptr:
385     case O_ymmword_ptr:
386     case O_zmmword_ptr:
387     case O_near_ptr:
388     case O_far_ptr:
389       if (intel_state.op_modifier == O_absent)
390 	intel_state.op_modifier = e->X_op;
391       /* FALLTHROUGH */
392     case O_short:
393       if (symbol_get_value_expression (e->X_add_symbol)->X_op
394 	  == O_register)
395 	{
396 	  as_bad (_("invalid use of register"));
397 	  return 0;
398 	}
399       if (!i386_intel_simplify_symbol (e->X_add_symbol))
400 	return 0;
401       i386_intel_fold (e, e->X_add_symbol);
402       break;
403 
404     case O_full_ptr:
405       if (symbol_get_value_expression (e->X_op_symbol)->X_op
406 	  == O_register)
407 	{
408 	  as_bad (_("invalid use of register"));
409 	  return 0;
410 	}
411       if (!i386_intel_simplify_symbol (e->X_op_symbol)
412 	  || !i386_intel_check(the_reg, intel_state.base,
413 			       intel_state.index))
414 	return 0;
415       if (!intel_state.in_offset)
416 	{
417 	  if (!intel_state.seg)
418 	    intel_state.seg = e->X_add_symbol;
419 	  else
420 	    {
421 	      expressionS exp;
422 
423 	      exp.X_op = O_full_ptr;
424 	      exp.X_add_symbol = e->X_add_symbol;
425 	      exp.X_op_symbol = intel_state.seg;
426 	      intel_state.seg = make_expr_symbol (&exp);
427 	    }
428 	}
429       i386_intel_fold (e, e->X_op_symbol);
430       break;
431 
432     case O_multiply:
433       if (this_operand >= 0 && intel_state.in_bracket)
434 	{
435 	  expressionS *scale = NULL;
436 	  int has_index = (intel_state.index != NULL);
437 
438 	  if (!intel_state.in_scale++)
439 	    intel_state.scale_factor = 1;
440 
441 	  ret = i386_intel_simplify_symbol (e->X_add_symbol);
442 	  if (ret && !has_index && intel_state.index)
443 	    scale = symbol_get_value_expression (e->X_op_symbol);
444 
445 	  if (ret)
446 	    ret = i386_intel_simplify_symbol (e->X_op_symbol);
447 	  if (ret && !scale && !has_index && intel_state.index)
448 	    scale = symbol_get_value_expression (e->X_add_symbol);
449 
450 	  if (ret && scale)
451 	    {
452 	      resolve_expression (scale);
453 	      if (scale->X_op != O_constant
454 		  || intel_state.index->reg_type.bitfield.word)
455 		scale->X_add_number = 0;
456 	      intel_state.scale_factor *= scale->X_add_number;
457 	    }
458 
459 	  --intel_state.in_scale;
460 	  if (!ret)
461 	    return 0;
462 
463 	  if (!intel_state.in_scale)
464 	    switch (intel_state.scale_factor)
465 	      {
466 	      case 1:
467 		i.log2_scale_factor = 0;
468 		break;
469 	      case 2:
470 		i.log2_scale_factor = 1;
471 		break;
472 	      case 4:
473 		i.log2_scale_factor = 2;
474 		break;
475 	      case 8:
476 		i.log2_scale_factor = 3;
477 		break;
478 	      default:
479 		/* esp is invalid as index */
480 		intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM;
481 		break;
482 	      }
483 
484 	  break;
485 	}
486       goto fallthrough;
487 
488     case O_register:
489       ret = i386_intel_simplify_register (e);
490       if (ret == 2)
491 	{
492 	  gas_assert (e->X_add_number < (unsigned short) -1);
493 	  e->X_md = (unsigned short) e->X_add_number + 1;
494 	  e->X_op = O_constant;
495 	  e->X_add_number = 0;
496 	}
497       return ret;
498 
499     case O_constant:
500       if (e->X_md)
501 	return i386_intel_simplify_register (e);
502 
503       /* FALLTHROUGH */
504     default:
505 fallthrough:
506       if (e->X_add_symbol
507 	  && !i386_intel_simplify_symbol (e->X_add_symbol))
508 	return 0;
509       if (e->X_op == O_add || e->X_op == O_subtract)
510 	{
511 	  base = intel_state.base;
512 	  state_index = intel_state.index;
513 	}
514       if (!i386_intel_check (the_reg, base, state_index)
515 	  || (e->X_op_symbol
516 	      && !i386_intel_simplify_symbol (e->X_op_symbol))
517 	  || !i386_intel_check (the_reg,
518 				(e->X_op != O_add
519 				 ? base : intel_state.base),
520 				(e->X_op != O_add
521 				 ? state_index : intel_state.index)))
522 	return 0;
523       break;
524     }
525 
526   if (this_operand >= 0
527       && e->X_op == O_symbol
528       && !intel_state.in_offset)
529     {
530       segT seg = S_GET_SEGMENT (e->X_add_symbol);
531 
532       if (seg != absolute_section
533 	  && seg != reg_section
534 	  && seg != expr_section)
535 	intel_state.is_mem |= 2 - !intel_state.in_bracket;
536     }
537 
538   return 1;
539 }
540 
541 int i386_need_index_operator (void)
542 {
543   return intel_syntax < 0;
544 }
545 
546 static int
547 i386_intel_operand (char *operand_string, int got_a_float)
548 {
549   char *saved_input_line_pointer, *buf;
550   segT exp_seg;
551   expressionS exp, *expP;
552   char suffix = 0;
553   int ret;
554 
555   /* Handle vector immediates.  */
556   if (RC_SAE_immediate (operand_string))
557     return 1;
558 
559   /* Initialize state structure.  */
560   intel_state.op_modifier = O_absent;
561   intel_state.is_mem = 0;
562   intel_state.is_indirect = 0;
563   intel_state.has_offset = 0;
564   intel_state.base = NULL;
565   intel_state.index = NULL;
566   intel_state.seg = NULL;
567   operand_type_set (&intel_state.reloc_types, ~0);
568   gas_assert (!intel_state.in_offset);
569   gas_assert (!intel_state.in_bracket);
570   gas_assert (!intel_state.in_scale);
571 
572   saved_input_line_pointer = input_line_pointer;
573   input_line_pointer = buf = xstrdup (operand_string);
574 
575   intel_syntax = -1;
576   memset (&exp, 0, sizeof(exp));
577   exp_seg = expression (&exp);
578   ret = i386_intel_simplify (&exp);
579   intel_syntax = 1;
580 
581   SKIP_WHITESPACE ();
582 
583   /* Handle vector operations.  */
584   if (*input_line_pointer == '{')
585     {
586       char *end = check_VecOperations (input_line_pointer, NULL);
587       if (end)
588 	input_line_pointer = end;
589       else
590 	ret = 0;
591     }
592 
593   if (!is_end_of_line[(unsigned char) *input_line_pointer])
594     {
595       if (ret)
596 	as_bad (_("junk `%s' after expression"), input_line_pointer);
597       ret = 0;
598     }
599   else if (exp.X_op == O_illegal || exp.X_op == O_absent)
600     {
601       if (ret)
602 	as_bad (_("invalid expression"));
603       ret = 0;
604     }
605   else if (!intel_state.has_offset
606 	   && input_line_pointer > buf
607 	   && *(input_line_pointer - 1) == ']')
608     {
609       intel_state.is_mem |= 1;
610       intel_state.is_indirect = 1;
611     }
612 
613   input_line_pointer = saved_input_line_pointer;
614   free (buf);
615 
616   gas_assert (!intel_state.in_offset);
617   gas_assert (!intel_state.in_bracket);
618   gas_assert (!intel_state.in_scale);
619 
620   if (!ret)
621     return 0;
622 
623   if (intel_state.op_modifier != O_absent
624       && current_templates->start->base_opcode != 0x8d /* lea */)
625     {
626       i.types[this_operand].bitfield.unspecified = 0;
627 
628       switch (intel_state.op_modifier)
629 	{
630 	case O_byte_ptr:
631 	  i.types[this_operand].bitfield.byte = 1;
632 	  suffix = BYTE_MNEM_SUFFIX;
633 	  break;
634 
635 	case O_word_ptr:
636 	  i.types[this_operand].bitfield.word = 1;
637 	  if ((current_templates->start->name[0] == 'l'
638 	       && current_templates->start->name[2] == 's'
639 	       && current_templates->start->name[3] == 0)
640 	      || current_templates->start->base_opcode == 0x62 /* bound */)
641 	    suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
642 	  else if (got_a_float == 2)	/* "fi..." */
643 	    suffix = SHORT_MNEM_SUFFIX;
644 	  else
645 	    suffix = WORD_MNEM_SUFFIX;
646 	  break;
647 
648 	case O_dword_ptr:
649 	  i.types[this_operand].bitfield.dword = 1;
650 	  if ((current_templates->start->name[0] == 'l'
651 	       && current_templates->start->name[2] == 's'
652 	       && current_templates->start->name[3] == 0)
653 	      || current_templates->start->base_opcode == 0x62 /* bound */)
654 	    suffix = WORD_MNEM_SUFFIX;
655 	  else if (flag_code == CODE_16BIT
656 		   && (current_templates->start->opcode_modifier.jump
657 		       || current_templates->start->opcode_modifier.jumpdword))
658 	    suffix = LONG_DOUBLE_MNEM_SUFFIX;
659 	  else if (got_a_float == 1)	/* "f..." */
660 	    suffix = SHORT_MNEM_SUFFIX;
661 	  else
662 	    suffix = LONG_MNEM_SUFFIX;
663 	  break;
664 
665 	case O_fword_ptr:
666 	  i.types[this_operand].bitfield.fword = 1;
667 	  if (current_templates->start->name[0] == 'l'
668 	      && current_templates->start->name[2] == 's'
669 	      && current_templates->start->name[3] == 0)
670 	    suffix = LONG_MNEM_SUFFIX;
671 	  else if (!got_a_float)
672 	    {
673 	      if (flag_code == CODE_16BIT)
674 		add_prefix (DATA_PREFIX_OPCODE);
675 	      suffix = LONG_DOUBLE_MNEM_SUFFIX;
676 	    }
677 	  else
678 	    suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
679 	  break;
680 
681 	case O_qword_ptr:
682 	  i.types[this_operand].bitfield.qword = 1;
683 	  if (current_templates->start->base_opcode == 0x62 /* bound */
684 	      || got_a_float == 1)	/* "f..." */
685 	    suffix = LONG_MNEM_SUFFIX;
686 	  else
687 	    suffix = QWORD_MNEM_SUFFIX;
688 	  break;
689 
690 	case O_tbyte_ptr:
691 	  i.types[this_operand].bitfield.tbyte = 1;
692 	  if (got_a_float == 1)
693 	    suffix = LONG_DOUBLE_MNEM_SUFFIX;
694 	  else
695 	    suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
696 	  break;
697 
698 	case O_oword_ptr:
699 	case O_xmmword_ptr:
700 	  i.types[this_operand].bitfield.xmmword = 1;
701 	  break;
702 
703 	case O_ymmword_ptr:
704 	  i.types[this_operand].bitfield.ymmword = 1;
705 	  break;
706 
707 	case O_zmmword_ptr:
708 	  i.types[this_operand].bitfield.zmmword = 1;
709 	  break;
710 
711 	case O_far_ptr:
712 	  suffix = LONG_DOUBLE_MNEM_SUFFIX;
713 	  /* FALLTHROUGH */
714 	case O_near_ptr:
715 	  if (!current_templates->start->opcode_modifier.jump
716 	      && !current_templates->start->opcode_modifier.jumpdword)
717 	    suffix = got_a_float /* so it will cause an error */
718 		     ? BYTE_MNEM_SUFFIX
719 		     : LONG_DOUBLE_MNEM_SUFFIX;
720 	  break;
721 
722 	default:
723 	  BAD_CASE (intel_state.op_modifier);
724 	  break;
725 	}
726 
727       if (!i.suffix)
728 	i.suffix = suffix;
729       else if (i.suffix != suffix)
730 	{
731 	  as_bad (_("conflicting operand size modifiers"));
732 	  return 0;
733 	}
734     }
735 
736   /* Operands for jump/call need special consideration.  */
737   if (current_templates->start->opcode_modifier.jump
738       || current_templates->start->opcode_modifier.jumpdword
739       || current_templates->start->opcode_modifier.jumpintersegment)
740     {
741       if (i.op[this_operand].regs
742 	  || intel_state.base
743 	  || intel_state.index
744 	  || intel_state.is_mem > 1)
745 	i.types[this_operand].bitfield.jumpabsolute = 1;
746       else
747 	switch (intel_state.op_modifier)
748 	  {
749 	  case O_near_ptr:
750 	    if (intel_state.seg)
751 	      i.types[this_operand].bitfield.jumpabsolute = 1;
752 	    else
753 	      intel_state.is_mem = 1;
754 	    break;
755 	  case O_far_ptr:
756 	  case O_absent:
757 	    if (!intel_state.seg)
758 	      {
759 		intel_state.is_mem = 1;
760 		if (intel_state.op_modifier == O_absent)
761 		  {
762 		    if (intel_state.is_indirect == 1)
763 		      i.types[this_operand].bitfield.jumpabsolute = 1;
764 		    break;
765 		  }
766 		as_bad (_("cannot infer the segment part of the operand"));
767 		return 0;
768 	      }
769 	    else if (S_GET_SEGMENT (intel_state.seg) == reg_section)
770 	      i.types[this_operand].bitfield.jumpabsolute = 1;
771 	    else
772 	      {
773 		i386_operand_type types;
774 
775 		if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
776 		  {
777 		    as_bad (_("at most %d immediate operands are allowed"),
778 			    MAX_IMMEDIATE_OPERANDS);
779 		    return 0;
780 		  }
781 		expP = &im_expressions[i.imm_operands++];
782 		memset (expP, 0, sizeof(*expP));
783 		expP->X_op = O_symbol;
784 		expP->X_add_symbol = intel_state.seg;
785 		i.op[this_operand].imms = expP;
786 
787 		resolve_expression (expP);
788 		operand_type_set (&types, ~0);
789 		if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg),
790 					      expP, types, operand_string))
791 		  return 0;
792 		if (i.operands < MAX_OPERANDS)
793 		  {
794 		    this_operand = i.operands++;
795 		    i.types[this_operand].bitfield.unspecified = 1;
796 		  }
797 		if (suffix == LONG_DOUBLE_MNEM_SUFFIX)
798 		  i.suffix = 0;
799 		intel_state.seg = NULL;
800 		intel_state.is_mem = 0;
801 	      }
802 	    break;
803 	  default:
804 	    i.types[this_operand].bitfield.jumpabsolute = 1;
805 	    break;
806 	  }
807       if (i.types[this_operand].bitfield.jumpabsolute)
808 	intel_state.is_mem |= 1;
809     }
810   else if (intel_state.seg)
811     intel_state.is_mem |= 1;
812 
813   if (i.op[this_operand].regs)
814     {
815       i386_operand_type temp;
816 
817       /* Register operand.  */
818       if (intel_state.base || intel_state.index || intel_state.seg)
819 	{
820 	  as_bad (_("invalid operand"));
821 	  return 0;
822 	}
823 
824       temp = i.op[this_operand].regs->reg_type;
825       temp.bitfield.baseindex = 0;
826       i.types[this_operand] = operand_type_or (i.types[this_operand],
827 					       temp);
828       i.types[this_operand].bitfield.unspecified = 0;
829       ++i.reg_operands;
830     }
831   else if (intel_state.base
832 	   || intel_state.index
833 	   || intel_state.seg
834 	   || intel_state.is_mem)
835     {
836       /* Memory operand.  */
837       if (i.mem_operands == 1 && !maybe_adjust_templates ())
838 	return 0;
839       if ((int) i.mem_operands
840 	  >= 2 - !current_templates->start->opcode_modifier.isstring)
841 	{
842 	  /* Handle
843 
844 	     call	0x9090,0x90909090
845 	     lcall	0x9090,0x90909090
846 	     jmp	0x9090,0x90909090
847 	     ljmp	0x9090,0x90909090
848 	   */
849 
850 	  if ((current_templates->start->opcode_modifier.jumpintersegment
851 	       || current_templates->start->opcode_modifier.jumpdword
852 	       || current_templates->start->opcode_modifier.jump)
853 	      && this_operand == 1
854 	      && intel_state.seg == NULL
855 	      && i.mem_operands == 1
856 	      && i.disp_operands == 1
857 	      && intel_state.op_modifier == O_absent)
858 	    {
859 	      /* Try to process the first operand as immediate,  */
860 	      this_operand = 0;
861 	      if (i386_finalize_immediate (exp_seg, i.op[0].imms,
862 					   intel_state.reloc_types,
863 					   NULL))
864 		{
865 		  this_operand = 1;
866 		  expP = &im_expressions[0];
867 		  i.op[this_operand].imms = expP;
868 		  *expP = exp;
869 
870 		  /* Try to process the second operand as immediate,  */
871 		  if (i386_finalize_immediate (exp_seg, expP,
872 					       intel_state.reloc_types,
873 					       NULL))
874 		    {
875 		      i.mem_operands = 0;
876 		      i.disp_operands = 0;
877 		      i.imm_operands = 2;
878 		      i.types[0].bitfield.mem = 0;
879 		      i.types[0].bitfield.disp16 = 0;
880 		      i.types[0].bitfield.disp32 = 0;
881 		      i.types[0].bitfield.disp32s = 0;
882 		      return 1;
883 		    }
884 		}
885 	    }
886 
887 	  as_bad (_("too many memory references for `%s'"),
888 		  current_templates->start->name);
889 	  return 0;
890 	}
891 
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 this here.  */
895       if (intel_state.base
896 	  && intel_state.index
897 	  && intel_state.base->reg_type.bitfield.word
898 	  && intel_state.index->reg_type.bitfield.word
899 	  && intel_state.base->reg_num >= 6
900 	  && intel_state.index->reg_num < 6)
901 	{
902 	  i.base_reg = intel_state.index;
903 	  i.index_reg = intel_state.base;
904 	}
905       else
906 	{
907 	  i.base_reg = intel_state.base;
908 	  i.index_reg = intel_state.index;
909 	}
910 
911       if (i.base_reg || i.index_reg)
912 	i.types[this_operand].bitfield.baseindex = 1;
913 
914       expP = &disp_expressions[i.disp_operands];
915       memcpy (expP, &exp, sizeof(exp));
916       resolve_expression (expP);
917 
918       if (expP->X_op != O_constant
919 	  || expP->X_add_number
920 	  || !i.types[this_operand].bitfield.baseindex)
921 	{
922 	  i.op[this_operand].disps = expP;
923 	  i.disp_operands++;
924 
925 	  i386_addressing_mode ();
926 
927 	  if (flag_code == CODE_64BIT)
928 	    {
929 	      i.types[this_operand].bitfield.disp32 = 1;
930 	      if (!i.prefix[ADDR_PREFIX])
931 		{
932 		  i.types[this_operand].bitfield.disp64 = 1;
933 		  i.types[this_operand].bitfield.disp32s = 1;
934 		}
935 	    }
936 	  else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
937 	    i.types[this_operand].bitfield.disp32 = 1;
938 	  else
939 	    i.types[this_operand].bitfield.disp16 = 1;
940 
941 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
942 	  /*
943 	   * exp_seg is used only for verification in
944 	   * i386_finalize_displacement, and we can end up seeing reg_section
945 	   * here - but we know we removed all registers from the expression
946 	   * (or error-ed on any remaining ones) in i386_intel_simplify.  I
947 	   * consider the check in i386_finalize_displacement bogus anyway, in
948 	   * particular because it doesn't allow for expr_section, so I'd
949 	   * rather see that check (and the similar one in
950 	   * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
951 	   * expert I can't really say whether that would have other bad side
952 	   * effects.
953 	   */
954 	  if (OUTPUT_FLAVOR == bfd_target_aout_flavour
955 	      && exp_seg == reg_section)
956 	    exp_seg = expP->X_op != O_constant ? undefined_section
957 					       : absolute_section;
958 #endif
959 
960 	  if (!i386_finalize_displacement (exp_seg, expP,
961 					   intel_state.reloc_types,
962 					   operand_string))
963 	    return 0;
964 	}
965 
966       if (intel_state.seg)
967 	{
968 	  for (ret = check_none; ; ret = operand_check)
969 	    {
970 	      expP = symbol_get_value_expression (intel_state.seg);
971 	      if (expP->X_op != O_full_ptr
972 		  || symbol_get_value_expression (expP->X_op_symbol)->X_op
973 		     != O_register)
974 		break;
975 	      intel_state.seg = expP->X_add_symbol;
976 	    }
977 	  if (expP->X_op != O_register)
978 	    {
979 	      as_bad (_("segment register name expected"));
980 	      return 0;
981 	    }
982 	  if (!i386_regtab[expP->X_add_number].reg_type.bitfield.sreg2
983 	      && !i386_regtab[expP->X_add_number].reg_type.bitfield.sreg3)
984 	    {
985 	      as_bad (_("invalid use of register"));
986 	      return 0;
987 	    }
988 	  switch (ret)
989 	    {
990 	    case check_error:
991 	      as_bad (_("redundant segment overrides"));
992 	      return 0;
993 	    case check_warning:
994 	      as_warn (_("redundant segment overrides"));
995 	      break;
996 	    }
997 	  switch (i386_regtab[expP->X_add_number].reg_num)
998 	    {
999 	    case 0: i.seg[i.mem_operands] = &es; break;
1000 	    case 1: i.seg[i.mem_operands] = &cs; break;
1001 	    case 2: i.seg[i.mem_operands] = &ss; break;
1002 	    case 3: i.seg[i.mem_operands] = &ds; break;
1003 	    case 4: i.seg[i.mem_operands] = &fs; break;
1004 	    case 5: i.seg[i.mem_operands] = &gs; break;
1005 	    case RegFlat: i.seg[i.mem_operands] = NULL; break;
1006 	    }
1007 	}
1008 
1009       if (!i386_index_check (operand_string))
1010 	return 0;
1011 
1012       i.types[this_operand].bitfield.mem = 1;
1013       if (i.mem_operands == 0)
1014 	i.memop1_string = xstrdup (operand_string);
1015       ++i.mem_operands;
1016     }
1017   else
1018     {
1019       /* Immediate.  */
1020       if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
1021 	{
1022 	  as_bad (_("at most %d immediate operands are allowed"),
1023 		  MAX_IMMEDIATE_OPERANDS);
1024 	  return 0;
1025 	}
1026 
1027       expP = &im_expressions[i.imm_operands++];
1028       i.op[this_operand].imms = expP;
1029       *expP = exp;
1030 
1031       return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
1032 				      operand_string);
1033     }
1034 
1035   return 1;
1036 }
1037