[PATCH, BINUTILS, AARCH64, 1/9] Add -march=armv8.5-a and related internal feature...
[external/binutils.git] / gas / config / tc-i386-intel.c
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 == RegIZ))
293     intel_state.index = i386_regtab + reg_num;
294   else if (!intel_state.base && !intel_state.in_scale)
295     intel_state.base = i386_regtab + reg_num;
296   else if (!intel_state.index)
297     {
298       if (intel_state.in_scale
299           || current_templates->start->base_opcode == 0xf30f1b /* bndmk */
300           || (current_templates->start->base_opcode & ~1) == 0x0f1a /* bnd{ld,st}x */
301           || i386_regtab[reg_num].reg_type.bitfield.baseindex)
302         intel_state.index = i386_regtab + reg_num;
303       else
304         {
305           /* Convert base to index and make ESP/RSP the base.  */
306           intel_state.index = intel_state.base;
307           intel_state.base = i386_regtab + reg_num;
308         }
309     }
310   else
311     {
312       /* esp is invalid as index */
313       intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM;
314     }
315   return 2;
316 }
317
318 static int i386_intel_simplify (expressionS *);
319
320 static INLINE int i386_intel_simplify_symbol(symbolS *sym)
321 {
322   int ret = i386_intel_simplify (symbol_get_value_expression (sym));
323
324   if (ret == 2)
325   {
326     S_SET_SEGMENT(sym, absolute_section);
327     ret = 1;
328   }
329   return ret;
330 }
331
332 static int i386_intel_simplify (expressionS *e)
333 {
334   const reg_entry *the_reg = (this_operand >= 0
335                               ? i.op[this_operand].regs : NULL);
336   const reg_entry *base = intel_state.base;
337   const reg_entry *state_index = intel_state.index;
338   int ret;
339
340   if (!intel_syntax)
341     return 1;
342
343   switch (e->X_op)
344     {
345     case O_index:
346       if (e->X_add_symbol)
347         {
348           if (!i386_intel_simplify_symbol (e->X_add_symbol)
349               || !i386_intel_check(the_reg, intel_state.base,
350                                    intel_state.index))
351             return 0;
352         }
353       if (!intel_state.in_offset)
354         ++intel_state.in_bracket;
355       ret = i386_intel_simplify_symbol (e->X_op_symbol);
356       if (!intel_state.in_offset)
357         --intel_state.in_bracket;
358       if (!ret)
359         return 0;
360       if (e->X_add_symbol)
361         e->X_op = O_add;
362       else
363         i386_intel_fold (e, e->X_op_symbol);
364       break;
365
366     case O_offset:
367       intel_state.has_offset = 1;
368       ++intel_state.in_offset;
369       ret = i386_intel_simplify_symbol (e->X_add_symbol);
370       --intel_state.in_offset;
371       if (!ret || !i386_intel_check(the_reg, base, state_index))
372         return 0;
373       i386_intel_fold (e, e->X_add_symbol);
374       return ret;
375
376     case O_byte_ptr:
377     case O_word_ptr:
378     case O_dword_ptr:
379     case O_fword_ptr:
380     case O_qword_ptr:
381     case O_tbyte_ptr:
382     case O_oword_ptr:
383     case O_xmmword_ptr:
384     case O_ymmword_ptr:
385     case O_zmmword_ptr:
386     case O_near_ptr:
387     case O_far_ptr:
388       if (intel_state.op_modifier == O_absent)
389         intel_state.op_modifier = e->X_op;
390       /* FALLTHROUGH */
391     case O_short:
392       if (symbol_get_value_expression (e->X_add_symbol)->X_op
393           == O_register)
394         {
395           as_bad (_("invalid use of register"));
396           return 0;
397         }
398       if (!i386_intel_simplify_symbol (e->X_add_symbol))
399         return 0;
400       i386_intel_fold (e, e->X_add_symbol);
401       break;
402
403     case O_full_ptr:
404       if (symbol_get_value_expression (e->X_op_symbol)->X_op
405           == O_register)
406         {
407           as_bad (_("invalid use of register"));
408           return 0;
409         }
410       if (!i386_intel_simplify_symbol (e->X_op_symbol)
411           || !i386_intel_check(the_reg, intel_state.base,
412                                intel_state.index))
413         return 0;
414       if (!intel_state.in_offset)
415         {
416           if (!intel_state.seg)
417             intel_state.seg = e->X_add_symbol;
418           else
419             {
420               expressionS exp;
421
422               exp.X_op = O_full_ptr;
423               exp.X_add_symbol = e->X_add_symbol;
424               exp.X_op_symbol = intel_state.seg;
425               intel_state.seg = make_expr_symbol (&exp);
426             }
427         }
428       i386_intel_fold (e, e->X_op_symbol);
429       break;
430
431     case O_multiply:
432       if (this_operand >= 0 && intel_state.in_bracket)
433         {
434           expressionS *scale = NULL;
435           int has_index = (intel_state.index != NULL);
436
437           if (!intel_state.in_scale++)
438             intel_state.scale_factor = 1;
439
440           ret = i386_intel_simplify_symbol (e->X_add_symbol);
441           if (ret && !has_index && intel_state.index)
442             scale = symbol_get_value_expression (e->X_op_symbol);
443
444           if (ret)
445             ret = i386_intel_simplify_symbol (e->X_op_symbol);
446           if (ret && !scale && !has_index && intel_state.index)
447             scale = symbol_get_value_expression (e->X_add_symbol);
448
449           if (ret && scale)
450             {
451               resolve_expression (scale);
452               if (scale->X_op != O_constant
453                   || intel_state.index->reg_type.bitfield.word)
454                 scale->X_add_number = 0;
455               intel_state.scale_factor *= scale->X_add_number;
456             }
457
458           --intel_state.in_scale;
459           if (!ret)
460             return 0;
461
462           if (!intel_state.in_scale)
463             switch (intel_state.scale_factor)
464               {
465               case 1:
466                 i.log2_scale_factor = 0;
467                 break;
468               case 2:
469                 i.log2_scale_factor = 1;
470                 break;
471               case 4:
472                 i.log2_scale_factor = 2;
473                 break;
474               case 8:
475                 i.log2_scale_factor = 3;
476                 break;
477               default:
478                 /* esp is invalid as index */
479                 intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM;
480                 break;
481               }
482
483           break;
484         }
485       goto fallthrough;
486
487     case O_register:
488       ret = i386_intel_simplify_register (e);
489       if (ret == 2)
490         {
491           gas_assert (e->X_add_number < (unsigned short) -1);
492           e->X_md = (unsigned short) e->X_add_number + 1;
493           e->X_op = O_constant;
494           e->X_add_number = 0;
495         }
496       return ret;
497
498     case O_constant:
499       if (e->X_md)
500         return i386_intel_simplify_register (e);
501
502       /* FALLTHROUGH */
503     default:
504 fallthrough:
505       if (e->X_add_symbol
506           && !i386_intel_simplify_symbol (e->X_add_symbol))
507         return 0;
508       if (e->X_op == O_add || e->X_op == O_subtract)
509         {
510           base = intel_state.base;
511           state_index = intel_state.index;
512         }
513       if (!i386_intel_check (the_reg, base, state_index)
514           || (e->X_op_symbol
515               && !i386_intel_simplify_symbol (e->X_op_symbol))
516           || !i386_intel_check (the_reg,
517                                 (e->X_op != O_add
518                                  ? base : intel_state.base),
519                                 (e->X_op != O_add
520                                  ? state_index : intel_state.index)))
521         return 0;
522       break;
523     }
524
525   if (this_operand >= 0
526       && e->X_op == O_symbol
527       && !intel_state.in_offset)
528     {
529       segT seg = S_GET_SEGMENT (e->X_add_symbol);
530
531       if (seg != absolute_section
532           && seg != reg_section
533           && seg != expr_section)
534         intel_state.is_mem |= 2 - !intel_state.in_bracket;
535     }
536
537   return 1;
538 }
539
540 int i386_need_index_operator (void)
541 {
542   return intel_syntax < 0;
543 }
544
545 static int
546 i386_intel_operand (char *operand_string, int got_a_float)
547 {
548   char *saved_input_line_pointer, *buf;
549   segT exp_seg;
550   expressionS exp, *expP;
551   char suffix = 0;
552   int ret;
553
554   /* Handle vector immediates.  */
555   if (RC_SAE_immediate (operand_string))
556     return 1;
557
558   /* Initialize state structure.  */
559   intel_state.op_modifier = O_absent;
560   intel_state.is_mem = 0;
561   intel_state.is_indirect = 0;
562   intel_state.has_offset = 0;
563   intel_state.base = NULL;
564   intel_state.index = NULL;
565   intel_state.seg = NULL;
566   operand_type_set (&intel_state.reloc_types, ~0);
567   gas_assert (!intel_state.in_offset);
568   gas_assert (!intel_state.in_bracket);
569   gas_assert (!intel_state.in_scale);
570
571   saved_input_line_pointer = input_line_pointer;
572   input_line_pointer = buf = xstrdup (operand_string);
573
574   intel_syntax = -1;
575   memset (&exp, 0, sizeof(exp));
576   exp_seg = expression (&exp);
577   ret = i386_intel_simplify (&exp);
578   intel_syntax = 1;
579
580   SKIP_WHITESPACE ();
581
582   /* Handle vector operations.  */
583   if (*input_line_pointer == '{')
584     {
585       char *end = check_VecOperations (input_line_pointer, NULL);
586       if (end)
587         input_line_pointer = end;
588       else
589         ret = 0;
590     }
591
592   if (!is_end_of_line[(unsigned char) *input_line_pointer])
593     {
594       if (ret)
595         as_bad (_("junk `%s' after expression"), input_line_pointer);
596       ret = 0;
597     }
598   else if (exp.X_op == O_illegal || exp.X_op == O_absent)
599     {
600       if (ret)
601         as_bad (_("invalid expression"));
602       ret = 0;
603     }
604   else if (!intel_state.has_offset
605            && input_line_pointer > buf
606            && *(input_line_pointer - 1) == ']')
607     {
608       intel_state.is_mem |= 1;
609       intel_state.is_indirect = 1;
610     }
611
612   input_line_pointer = saved_input_line_pointer;
613   free (buf);
614
615   gas_assert (!intel_state.in_offset);
616   gas_assert (!intel_state.in_bracket);
617   gas_assert (!intel_state.in_scale);
618
619   if (!ret)
620     return 0;
621
622   if (intel_state.op_modifier != O_absent
623       && current_templates->start->base_opcode != 0x8d /* lea */)
624     {
625       i.types[this_operand].bitfield.unspecified = 0;
626
627       switch (intel_state.op_modifier)
628         {
629         case O_byte_ptr:
630           i.types[this_operand].bitfield.byte = 1;
631           suffix = BYTE_MNEM_SUFFIX;
632           break;
633
634         case O_word_ptr:
635           i.types[this_operand].bitfield.word = 1;
636           if ((current_templates->start->name[0] == 'l'
637                && current_templates->start->name[2] == 's'
638                && current_templates->start->name[3] == 0)
639               || current_templates->start->base_opcode == 0x62 /* bound */)
640             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
641           else if (got_a_float == 2)    /* "fi..." */
642             suffix = SHORT_MNEM_SUFFIX;
643           else
644             suffix = WORD_MNEM_SUFFIX;
645           break;
646
647         case O_dword_ptr:
648           i.types[this_operand].bitfield.dword = 1;
649           if ((current_templates->start->name[0] == 'l'
650                && current_templates->start->name[2] == 's'
651                && current_templates->start->name[3] == 0)
652               || current_templates->start->base_opcode == 0x62 /* bound */)
653             suffix = WORD_MNEM_SUFFIX;
654           else if (flag_code == CODE_16BIT
655                    && (current_templates->start->opcode_modifier.jump
656                        || current_templates->start->opcode_modifier.jumpdword))
657             suffix = LONG_DOUBLE_MNEM_SUFFIX;
658           else if (got_a_float == 1)    /* "f..." */
659             suffix = SHORT_MNEM_SUFFIX;
660           else
661             suffix = LONG_MNEM_SUFFIX;
662           break;
663
664         case O_fword_ptr:
665           i.types[this_operand].bitfield.fword = 1;
666           if (current_templates->start->name[0] == 'l'
667               && current_templates->start->name[2] == 's'
668               && current_templates->start->name[3] == 0)
669             suffix = LONG_MNEM_SUFFIX;
670           else if (!got_a_float)
671             {
672               if (flag_code == CODE_16BIT)
673                 add_prefix (DATA_PREFIX_OPCODE);
674               suffix = LONG_DOUBLE_MNEM_SUFFIX;
675             }
676           else
677             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
678           break;
679
680         case O_qword_ptr:
681           i.types[this_operand].bitfield.qword = 1;
682           if (current_templates->start->base_opcode == 0x62 /* bound */
683               || got_a_float == 1)      /* "f..." */
684             suffix = LONG_MNEM_SUFFIX;
685           else
686             suffix = QWORD_MNEM_SUFFIX;
687           break;
688
689         case O_tbyte_ptr:
690           i.types[this_operand].bitfield.tbyte = 1;
691           if (got_a_float == 1)
692             suffix = LONG_DOUBLE_MNEM_SUFFIX;
693           else
694             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
695           break;
696
697         case O_oword_ptr:
698         case O_xmmword_ptr:
699           i.types[this_operand].bitfield.xmmword = 1;
700           break;
701
702         case O_ymmword_ptr:
703           i.types[this_operand].bitfield.ymmword = 1;
704           break;
705
706         case O_zmmword_ptr:
707           i.types[this_operand].bitfield.zmmword = 1;
708           break;
709
710         case O_far_ptr:
711           suffix = LONG_DOUBLE_MNEM_SUFFIX;
712           /* FALLTHROUGH */
713         case O_near_ptr:
714           if (!current_templates->start->opcode_modifier.jump
715               && !current_templates->start->opcode_modifier.jumpdword)
716             suffix = got_a_float /* so it will cause an error */
717                      ? BYTE_MNEM_SUFFIX
718                      : LONG_DOUBLE_MNEM_SUFFIX;
719           break;
720
721         default:
722           BAD_CASE (intel_state.op_modifier);
723           break;
724         }
725
726       if (!i.suffix)
727         i.suffix = suffix;
728       else if (i.suffix != suffix)
729         {
730           as_bad (_("conflicting operand size modifiers"));
731           return 0;
732         }
733     }
734
735   /* Operands for jump/call need special consideration.  */
736   if (current_templates->start->opcode_modifier.jump
737       || current_templates->start->opcode_modifier.jumpdword
738       || current_templates->start->opcode_modifier.jumpintersegment)
739     {
740       if (i.op[this_operand].regs
741           || intel_state.base
742           || intel_state.index
743           || intel_state.is_mem > 1)
744         i.types[this_operand].bitfield.jumpabsolute = 1;
745       else
746         switch (intel_state.op_modifier)
747           {
748           case O_near_ptr:
749             if (intel_state.seg)
750               i.types[this_operand].bitfield.jumpabsolute = 1;
751             else
752               intel_state.is_mem = 1;
753             break;
754           case O_far_ptr:
755           case O_absent:
756             if (!intel_state.seg)
757               {
758                 intel_state.is_mem = 1;
759                 if (intel_state.op_modifier == O_absent)
760                   {
761                     if (intel_state.is_indirect == 1)
762                       i.types[this_operand].bitfield.jumpabsolute = 1;
763                     break;
764                   }
765                 as_bad (_("cannot infer the segment part of the operand"));
766                 return 0;
767               }
768             else if (S_GET_SEGMENT (intel_state.seg) == reg_section)
769               i.types[this_operand].bitfield.jumpabsolute = 1;
770             else
771               {
772                 i386_operand_type types;
773
774                 if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
775                   {
776                     as_bad (_("at most %d immediate operands are allowed"),
777                             MAX_IMMEDIATE_OPERANDS);
778                     return 0;
779                   }
780                 expP = &im_expressions[i.imm_operands++];
781                 memset (expP, 0, sizeof(*expP));
782                 expP->X_op = O_symbol;
783                 expP->X_add_symbol = intel_state.seg;
784                 i.op[this_operand].imms = expP;
785
786                 resolve_expression (expP);
787                 operand_type_set (&types, ~0);
788                 if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg),
789                                               expP, types, operand_string))
790                   return 0;
791                 if (i.operands < MAX_OPERANDS)
792                   {
793                     this_operand = i.operands++;
794                     i.types[this_operand].bitfield.unspecified = 1;
795                   }
796                 if (suffix == LONG_DOUBLE_MNEM_SUFFIX)
797                   i.suffix = 0;
798                 intel_state.seg = NULL;
799                 intel_state.is_mem = 0;
800               }
801             break;
802           default:
803             i.types[this_operand].bitfield.jumpabsolute = 1;
804             break;
805           }
806       if (i.types[this_operand].bitfield.jumpabsolute)
807         intel_state.is_mem |= 1;
808     }
809   else if (intel_state.seg)
810     intel_state.is_mem |= 1;
811
812   if (i.op[this_operand].regs)
813     {
814       i386_operand_type temp;
815
816       /* Register operand.  */
817       if (intel_state.base || intel_state.index || intel_state.seg)
818         {
819           as_bad (_("invalid operand"));
820           return 0;
821         }
822
823       temp = i.op[this_operand].regs->reg_type;
824       temp.bitfield.baseindex = 0;
825       i.types[this_operand] = operand_type_or (i.types[this_operand],
826                                                temp);
827       i.types[this_operand].bitfield.unspecified = 0;
828       ++i.reg_operands;
829     }
830   else if (intel_state.base
831            || intel_state.index
832            || intel_state.seg
833            || intel_state.is_mem)
834     {
835       /* Memory operand.  */
836       if (i.mem_operands == 1 && !maybe_adjust_templates ())
837         return 0;
838       if ((int) i.mem_operands
839           >= 2 - !current_templates->start->opcode_modifier.isstring)
840         {
841           /* Handle
842
843              call       0x9090,0x90909090
844              lcall      0x9090,0x90909090
845              jmp        0x9090,0x90909090
846              ljmp       0x9090,0x90909090
847            */
848
849           if ((current_templates->start->opcode_modifier.jumpintersegment
850                || current_templates->start->opcode_modifier.jumpdword
851                || current_templates->start->opcode_modifier.jump)
852               && this_operand == 1
853               && intel_state.seg == NULL
854               && i.mem_operands == 1
855               && i.disp_operands == 1
856               && intel_state.op_modifier == O_absent)
857             {
858               /* Try to process the first operand as immediate,  */
859               this_operand = 0;
860               if (i386_finalize_immediate (exp_seg, i.op[0].imms,
861                                            intel_state.reloc_types,
862                                            NULL))
863                 {
864                   this_operand = 1;
865                   expP = &im_expressions[0];
866                   i.op[this_operand].imms = expP;
867                   *expP = exp;
868
869                   /* Try to process the second operand as immediate,  */
870                   if (i386_finalize_immediate (exp_seg, expP,
871                                                intel_state.reloc_types,
872                                                NULL))
873                     {
874                       i.mem_operands = 0;
875                       i.disp_operands = 0;
876                       i.imm_operands = 2;
877                       i.flags[0] &= ~Operand_Mem;
878                       i.types[0].bitfield.disp16 = 0;
879                       i.types[0].bitfield.disp32 = 0;
880                       i.types[0].bitfield.disp32s = 0;
881                       return 1;
882                     }
883                 }
884             }
885
886           as_bad (_("too many memory references for `%s'"),
887                   current_templates->start->name);
888           return 0;
889         }
890
891       /* Swap base and index in 16-bit memory operands like
892          [si+bx]. Since i386_index_check is also used in AT&T
893          mode we have to do this here.  */
894       if (intel_state.base
895           && intel_state.index
896           && intel_state.base->reg_type.bitfield.word
897           && intel_state.index->reg_type.bitfield.word
898           && intel_state.base->reg_num >= 6
899           && intel_state.index->reg_num < 6)
900         {
901           i.base_reg = intel_state.index;
902           i.index_reg = intel_state.base;
903         }
904       else
905         {
906           i.base_reg = intel_state.base;
907           i.index_reg = intel_state.index;
908         }
909
910       if (i.base_reg || i.index_reg)
911         i.types[this_operand].bitfield.baseindex = 1;
912
913       expP = &disp_expressions[i.disp_operands];
914       memcpy (expP, &exp, sizeof(exp));
915       resolve_expression (expP);
916
917       if (expP->X_op != O_constant
918           || expP->X_add_number
919           || !i.types[this_operand].bitfield.baseindex)
920         {
921           i.op[this_operand].disps = expP;
922           i.disp_operands++;
923
924           i386_addressing_mode ();
925
926           if (flag_code == CODE_64BIT)
927             {
928               i.types[this_operand].bitfield.disp32 = 1;
929               if (!i.prefix[ADDR_PREFIX])
930                 {
931                   i.types[this_operand].bitfield.disp64 = 1;
932                   i.types[this_operand].bitfield.disp32s = 1;
933                 }
934             }
935           else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
936             i.types[this_operand].bitfield.disp32 = 1;
937           else
938             i.types[this_operand].bitfield.disp16 = 1;
939
940 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
941           /*
942            * exp_seg is used only for verification in
943            * i386_finalize_displacement, and we can end up seeing reg_section
944            * here - but we know we removed all registers from the expression
945            * (or error-ed on any remaining ones) in i386_intel_simplify.  I
946            * consider the check in i386_finalize_displacement bogus anyway, in
947            * particular because it doesn't allow for expr_section, so I'd
948            * rather see that check (and the similar one in
949            * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
950            * expert I can't really say whether that would have other bad side
951            * effects.
952            */
953           if (OUTPUT_FLAVOR == bfd_target_aout_flavour
954               && exp_seg == reg_section)
955             exp_seg = expP->X_op != O_constant ? undefined_section
956                                                : absolute_section;
957 #endif
958
959           if (!i386_finalize_displacement (exp_seg, expP,
960                                            intel_state.reloc_types,
961                                            operand_string))
962             return 0;
963         }
964
965       if (intel_state.seg)
966         {
967           for (ret = check_none; ; ret = operand_check)
968             {
969               expP = symbol_get_value_expression (intel_state.seg);
970               if (expP->X_op != O_full_ptr 
971                   || symbol_get_value_expression (expP->X_op_symbol)->X_op
972                      != O_register)
973                 break;
974               intel_state.seg = expP->X_add_symbol;
975             }
976           if (expP->X_op != O_register)
977             {
978               as_bad (_("segment register name expected"));
979               return 0;
980             }
981           if (!i386_regtab[expP->X_add_number].reg_type.bitfield.sreg2
982               && !i386_regtab[expP->X_add_number].reg_type.bitfield.sreg3)
983             {
984               as_bad (_("invalid use of register"));
985               return 0;
986             }
987           switch (ret)
988             {
989             case check_error:
990               as_bad (_("redundant segment overrides"));
991               return 0;
992             case check_warning:
993               as_warn (_("redundant segment overrides"));
994               break;
995             }
996           switch (i386_regtab[expP->X_add_number].reg_num)
997             {
998             case 0: i.seg[i.mem_operands] = &es; break;
999             case 1: i.seg[i.mem_operands] = &cs; break;
1000             case 2: i.seg[i.mem_operands] = &ss; break;
1001             case 3: i.seg[i.mem_operands] = &ds; break;
1002             case 4: i.seg[i.mem_operands] = &fs; break;
1003             case 5: i.seg[i.mem_operands] = &gs; break;
1004             case RegFlat: i.seg[i.mem_operands] = NULL; break;
1005             }
1006         }
1007
1008       if (!i386_index_check (operand_string))
1009         return 0;
1010
1011       i.flags[this_operand] |= Operand_Mem;
1012       if (i.mem_operands == 0)
1013         i.memop1_string = xstrdup (operand_string);
1014       ++i.mem_operands;
1015     }
1016   else
1017     {
1018       /* Immediate.  */
1019       if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
1020         {
1021           as_bad (_("at most %d immediate operands are allowed"),
1022                   MAX_IMMEDIATE_OPERANDS);
1023           return 0;
1024         }
1025
1026       expP = &im_expressions[i.imm_operands++];
1027       i.op[this_operand].imms = expP;
1028       *expP = exp;
1029
1030       return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
1031                                       operand_string);
1032     }
1033
1034   return 1;
1035 }