1e563b2d81c59a18282c95ae79ac9b0dbf7261e4
[external/binutils.git] / gas / config / tc-i386-intel.c
1 /* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
2    Copyright 2009, 2010
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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.  */
21
22 static struct
23   {
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;
37   }
38 intel_state;
39
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
66
67 static struct
68   {
69     const char *name;
70     operatorT op;
71     unsigned int operands;
72   }
73 const i386_operators[] =
74   {
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 }
91   };
92
93 static struct
94   {
95     const char *name;
96     operatorT op;
97     unsigned short sz[3];
98   }
99 const i386_types[] =
100   {
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 } }
115   };
116
117 operatorT i386_operator (const char *name, unsigned int operands, char *pc)
118 {
119   unsigned int j;
120
121   if (!intel_syntax)
122     return O_absent;
123
124   if (!name)
125     {
126       if (operands != 2)
127         return O_illegal;
128       switch (*input_line_pointer)
129         {
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)
138             {
139               int adjust = 0;
140               char *gotfree_input_line = lex_got (&i.reloc[this_operand],
141                                                   &adjust,
142                                                   &intel_state.reloc_types);
143
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;
151             }
152           break;
153         }
154       return O_illegal;
155     }
156
157   for (j = 0; i386_operators[j].name; ++j)
158     if (strcasecmp (i386_operators[j].name, name) == 0)
159       {
160         if (i386_operators[j].operands
161             && i386_operators[j].operands != operands)
162           return O_illegal;
163         return i386_operators[j].op;
164       }
165
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 == ' ')
170     {
171       char *pname = ++input_line_pointer;
172       char c = get_symbol_end ();
173
174       if (strcasecmp (pname, "ptr") == 0)
175         {
176           pname[-1] = *pc;
177           *pc = c;
178           if (intel_syntax > 0 || operands != 1)
179             return O_illegal;
180           return i386_types[j].op;
181         }
182
183       *input_line_pointer = c;
184       input_line_pointer = pname - 1;
185     }
186
187   return O_absent;
188 }
189
190 static int i386_intel_parse_name (const char *name, expressionS *e)
191 {
192   unsigned int j;
193
194   if (! strcmp (name, "$"))
195     {
196       current_location (e);
197       return 1;
198     }
199
200   for (j = 0; i386_types[j].name; ++j)
201     if (strcasecmp(i386_types[j].name, name) == 0)
202       {
203         e->X_op = O_constant;
204         e->X_add_number = i386_types[j].sz[flag_code];
205         e->X_add_symbol = NULL;
206         e->X_op_symbol = NULL;
207         return 1;
208       }
209
210   return 0;
211 }
212
213 static INLINE int i386_intel_check (const reg_entry *rreg,
214                                     const reg_entry *base,
215                                     const reg_entry *iindex)
216 {
217   if ((this_operand >= 0
218        && rreg != i.op[this_operand].regs)
219       || base != intel_state.base
220       || iindex != intel_state.index)
221     {
222       as_bad (_("invalid use of register"));
223       return 0;
224     }
225   return 1;
226 }
227
228 static INLINE void i386_intel_fold (expressionS *e, symbolS *sym)
229 {
230   expressionS *exp = symbol_get_value_expression (sym);
231   if (S_GET_SEGMENT (sym) == absolute_section)
232     {
233       offsetT val = e->X_add_number;
234
235       *e = *exp;
236       e->X_add_number += val;
237     }
238   else
239     {
240       if (exp->X_op == O_symbol
241           && strcmp (S_GET_NAME (exp->X_add_symbol),
242                      GLOBAL_OFFSET_TABLE_NAME) == 0)
243         sym = exp->X_add_symbol;
244       e->X_add_symbol = sym;
245       e->X_op_symbol = NULL;
246       e->X_op = O_symbol;
247     }
248 }
249
250 static int
251 i386_intel_simplify_register (expressionS *e)
252 {
253   int reg_num;
254
255   if (this_operand < 0 || intel_state.in_offset)
256     {
257       as_bad (_("invalid use of register"));
258       return 0;
259     }
260
261   if (e->X_op == O_register)
262     reg_num = e->X_add_number;
263   else
264     reg_num = e->X_md - 1;
265
266   if (!intel_state.in_bracket)
267     {
268       if (i.op[this_operand].regs)
269         {
270           as_bad (_("invalid use of register"));
271           return 0;
272         }
273       if (i386_regtab[reg_num].reg_type.bitfield.sreg3
274           && i386_regtab[reg_num].reg_num == RegFlat)
275         {
276           as_bad (_("invalid use of pseudo-register"));
277           return 0;
278         }
279       i.op[this_operand].regs = i386_regtab + reg_num;
280     }
281   else if (!intel_state.base && !intel_state.in_scale)
282     intel_state.base = i386_regtab + reg_num;
283   else if (!intel_state.index)
284     intel_state.index = i386_regtab + reg_num;
285   else
286     {
287       /* esp is invalid as index */
288       intel_state.index = i386_regtab + REGNAM_EAX + 4;
289     }
290   return 2;
291 }
292
293 static int i386_intel_simplify (expressionS *);
294
295 static INLINE int i386_intel_simplify_symbol(symbolS *sym)
296 {
297   int ret = i386_intel_simplify (symbol_get_value_expression (sym));
298
299   if (ret == 2)
300   {
301     S_SET_SEGMENT(sym, absolute_section);
302     ret = 1;
303   }
304   return ret;
305 }
306
307 static int i386_intel_simplify (expressionS *e)
308 {
309   const reg_entry *the_reg = (this_operand >= 0
310                               ? i.op[this_operand].regs : NULL);
311   const reg_entry *base = intel_state.base;
312   const reg_entry *state_index = intel_state.index;
313   int ret;
314
315   if (!intel_syntax)
316     return 1;
317
318   switch (e->X_op)
319     {
320     case O_index:
321       if (e->X_add_symbol)
322         {
323           if (!i386_intel_simplify_symbol (e->X_add_symbol)
324               || !i386_intel_check(the_reg, intel_state.base,
325                                    intel_state.index))
326             return 0;;
327         }
328       if (!intel_state.in_offset)
329         ++intel_state.in_bracket;
330       ret = i386_intel_simplify_symbol (e->X_op_symbol);
331       if (!intel_state.in_offset)
332         --intel_state.in_bracket;
333       if (!ret)
334         return 0;
335       if (e->X_add_symbol)
336         e->X_op = O_add;
337       else
338         i386_intel_fold (e, e->X_op_symbol);
339       break;
340
341     case O_offset:
342       intel_state.has_offset = 1;
343       ++intel_state.in_offset;
344       ret = i386_intel_simplify_symbol (e->X_add_symbol);
345       --intel_state.in_offset;
346       if (!ret || !i386_intel_check(the_reg, base, state_index))
347         return 0;
348       i386_intel_fold (e, e->X_add_symbol);
349       return ret;
350
351     case O_byte_ptr:
352     case O_word_ptr:
353     case O_dword_ptr:
354     case O_fword_ptr:
355     case O_qword_ptr:
356     case O_tbyte_ptr:
357     case O_oword_ptr:
358     case O_xmmword_ptr:
359     case O_ymmword_ptr:
360     case O_near_ptr:
361     case O_far_ptr:
362       if (intel_state.op_modifier == O_absent)
363         intel_state.op_modifier = e->X_op;
364       /* FALLTHROUGH */
365     case O_short:
366       if (symbol_get_value_expression (e->X_add_symbol)->X_op
367           == O_register)
368         {
369           as_bad (_("invalid use of register"));
370           return 0;
371         }
372       if (!i386_intel_simplify_symbol (e->X_add_symbol))
373         return 0;
374       i386_intel_fold (e, e->X_add_symbol);
375       break;
376
377     case O_full_ptr:
378       if (symbol_get_value_expression (e->X_op_symbol)->X_op
379           == O_register)
380         {
381           as_bad (_("invalid use of register"));
382           return 0;
383         }
384       if (!i386_intel_simplify_symbol (e->X_op_symbol)
385           || !i386_intel_check(the_reg, intel_state.base,
386                                intel_state.index))
387         return 0;
388       if (!intel_state.in_offset)
389         intel_state.seg = e->X_add_symbol;
390       i386_intel_fold (e, e->X_op_symbol);
391       break;
392
393     case O_multiply:
394       if (this_operand >= 0 && intel_state.in_bracket)
395         {
396           expressionS *scale = NULL;
397
398           if (intel_state.index)
399             --scale;
400
401           if (!intel_state.in_scale++)
402             intel_state.scale_factor = 1;
403
404           ret = i386_intel_simplify_symbol (e->X_add_symbol);
405           if (ret && !scale && intel_state.index)
406             scale = symbol_get_value_expression (e->X_op_symbol);
407
408           if (ret)
409             ret = i386_intel_simplify_symbol (e->X_op_symbol);
410           if (ret && !scale && intel_state.index)
411             scale = symbol_get_value_expression (e->X_add_symbol);
412
413           if (ret && scale && (scale + 1))
414             {
415               resolve_expression (scale);
416               if (scale->X_op != O_constant
417                   || intel_state.index->reg_type.bitfield.reg16)
418                 scale->X_add_number = 0;
419               intel_state.scale_factor *= scale->X_add_number;
420             }
421
422           --intel_state.in_scale;
423           if (!ret)
424             return 0;
425
426           if (!intel_state.in_scale)
427             switch (intel_state.scale_factor)
428               {
429               case 1:
430                 i.log2_scale_factor = 0;
431                 break;
432               case 2:
433                 i.log2_scale_factor = 1;
434                 break;
435               case 4:
436                 i.log2_scale_factor = 2;
437                 break;
438               case 8:
439                 i.log2_scale_factor = 3;
440                 break;
441               default:
442                 /* esp is invalid as index */
443                 intel_state.index = i386_regtab + REGNAM_EAX + 4;
444                 break;
445               }
446
447           break;
448         }
449       goto fallthrough;
450
451     case O_register:
452       ret = i386_intel_simplify_register (e);
453       if (ret == 2)
454         {
455           gas_assert (e->X_add_number < (unsigned short) -1);
456           e->X_md = (unsigned short) e->X_add_number + 1;
457           e->X_op = O_constant;
458           e->X_add_number = 0;
459         }
460       return ret;
461
462     case O_constant:
463       if (e->X_md)
464         return i386_intel_simplify_register (e);
465
466       /* FALLTHROUGH */
467     default:
468 fallthrough:
469       if (e->X_add_symbol
470           && !i386_intel_simplify_symbol (e->X_add_symbol))
471         return 0;
472       if (e->X_op == O_add || e->X_op == O_subtract)
473         {
474           base = intel_state.base;
475           state_index = intel_state.index;
476         }
477       if (!i386_intel_check (the_reg, base, state_index)
478           || (e->X_op_symbol
479               && !i386_intel_simplify_symbol (e->X_op_symbol))
480           || !i386_intel_check (the_reg,
481                                 (e->X_op != O_add
482                                  ? base : intel_state.base),
483                                 (e->X_op != O_add
484                                  ? state_index : intel_state.index)))
485         return 0;
486       break;
487     }
488
489   if (this_operand >= 0
490       && e->X_op == O_symbol
491       && !intel_state.in_offset)
492     {
493       segT seg = S_GET_SEGMENT (e->X_add_symbol);
494
495       if (seg != absolute_section
496           && seg != reg_section
497           && seg != expr_section)
498         intel_state.is_mem |= 2 - !intel_state.in_bracket;
499     }
500
501   return 1;
502 }
503
504 int i386_need_index_operator (void)
505 {
506   return intel_syntax < 0;
507 }
508
509 static int
510 i386_intel_operand (char *operand_string, int got_a_float)
511 {
512   char *saved_input_line_pointer, *buf;
513   segT exp_seg;
514   expressionS exp, *expP;
515   char suffix = 0;
516   int ret;
517
518   /* Initialize state structure.  */
519   intel_state.op_modifier = O_absent;
520   intel_state.is_mem = 0;
521   intel_state.is_indirect = 0;
522   intel_state.has_offset = 0;
523   intel_state.base = NULL;
524   intel_state.index = NULL;
525   intel_state.seg = NULL;
526   operand_type_set (&intel_state.reloc_types, ~0);
527   gas_assert (!intel_state.in_offset);
528   gas_assert (!intel_state.in_bracket);
529   gas_assert (!intel_state.in_scale);
530
531   saved_input_line_pointer = input_line_pointer;
532   input_line_pointer = buf = xstrdup (operand_string);
533
534   intel_syntax = -1;
535   memset (&exp, 0, sizeof(exp));
536   exp_seg = expression (&exp);
537   ret = i386_intel_simplify (&exp);
538   intel_syntax = 1;
539
540   SKIP_WHITESPACE ();
541   if (!is_end_of_line[(unsigned char) *input_line_pointer])
542     {
543       as_bad (_("junk `%s' after expression"), input_line_pointer);
544       ret = 0;
545     }
546   else if (exp.X_op == O_illegal || exp.X_op == O_absent)
547     {
548       as_bad (_("invalid expression"));
549       ret = 0;
550     }
551   else if (!intel_state.has_offset
552            && input_line_pointer > buf
553            && *(input_line_pointer - 1) == ']')
554     {
555       intel_state.is_mem |= 1;
556       intel_state.is_indirect = 1;
557     }
558
559   input_line_pointer = saved_input_line_pointer;
560   free (buf);
561
562   gas_assert (!intel_state.in_offset);
563   gas_assert (!intel_state.in_bracket);
564   gas_assert (!intel_state.in_scale);
565
566   if (!ret)
567     return 0;
568
569   if (intel_state.op_modifier != O_absent
570       && current_templates->start->base_opcode != 0x8d /* lea */)
571     {
572       i.types[this_operand].bitfield.unspecified = 0;
573
574       switch (intel_state.op_modifier)
575         {
576         case O_byte_ptr:
577           i.types[this_operand].bitfield.byte = 1;
578           suffix = BYTE_MNEM_SUFFIX;
579           break;
580
581         case O_word_ptr:
582           i.types[this_operand].bitfield.word = 1;
583           if ((current_templates->start->name[0] == 'l'
584                && current_templates->start->name[2] == 's'
585                && current_templates->start->name[3] == 0)
586               || current_templates->start->base_opcode == 0x62 /* bound */)
587             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
588           else if (got_a_float == 2)    /* "fi..." */
589             suffix = SHORT_MNEM_SUFFIX;
590           else
591             suffix = WORD_MNEM_SUFFIX;
592           break;
593
594         case O_dword_ptr:
595           i.types[this_operand].bitfield.dword = 1;
596           if ((current_templates->start->name[0] == 'l'
597                && current_templates->start->name[2] == 's'
598                && current_templates->start->name[3] == 0)
599               || current_templates->start->base_opcode == 0x62 /* bound */)
600             suffix = WORD_MNEM_SUFFIX;
601           else if (flag_code == CODE_16BIT
602                    && (current_templates->start->opcode_modifier.jump
603                        || current_templates->start->opcode_modifier.jumpdword))
604             suffix = LONG_DOUBLE_MNEM_SUFFIX;
605           else if (got_a_float == 1)    /* "f..." */
606             suffix = SHORT_MNEM_SUFFIX;
607           else
608             suffix = LONG_MNEM_SUFFIX;
609           break;
610
611         case O_fword_ptr:
612           i.types[this_operand].bitfield.fword = 1;
613           if (current_templates->start->name[0] == 'l'
614               && current_templates->start->name[2] == 's'
615               && current_templates->start->name[3] == 0)
616             suffix = LONG_MNEM_SUFFIX;
617           else if (!got_a_float)
618             {
619               if (flag_code == CODE_16BIT)
620                 add_prefix (DATA_PREFIX_OPCODE);
621               suffix = LONG_DOUBLE_MNEM_SUFFIX;
622             }
623           else
624             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
625           break;
626
627         case O_qword_ptr:
628           i.types[this_operand].bitfield.qword = 1;
629           if (current_templates->start->base_opcode == 0x62 /* bound */
630               || got_a_float == 1)      /* "f..." */
631             suffix = LONG_MNEM_SUFFIX;
632           else
633             suffix = QWORD_MNEM_SUFFIX;
634           break;
635
636         case O_tbyte_ptr:
637           i.types[this_operand].bitfield.tbyte = 1;
638           if (got_a_float == 1)
639             suffix = LONG_DOUBLE_MNEM_SUFFIX;
640           else
641             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
642           break;
643
644         case O_oword_ptr:
645         case O_xmmword_ptr:
646           i.types[this_operand].bitfield.xmmword = 1;
647           suffix = XMMWORD_MNEM_SUFFIX;
648           break;
649
650         case O_ymmword_ptr:
651           i.types[this_operand].bitfield.ymmword = 1;
652           suffix = YMMWORD_MNEM_SUFFIX;
653           break;
654
655         case O_far_ptr:
656           suffix = LONG_DOUBLE_MNEM_SUFFIX;
657           /* FALLTHROUGH */
658         case O_near_ptr:
659           if (!current_templates->start->opcode_modifier.jump
660               && !current_templates->start->opcode_modifier.jumpdword)
661             suffix = got_a_float /* so it will cause an error */
662                      ? BYTE_MNEM_SUFFIX
663                      : LONG_DOUBLE_MNEM_SUFFIX;
664           break;
665
666         default:
667           BAD_CASE (intel_state.op_modifier);
668           break;
669         }
670
671       if (!i.suffix)
672         i.suffix = suffix;
673       else if (i.suffix != suffix)
674         {
675           as_bad (_("conflicting operand size modifiers"));
676           return 0;
677         }
678     }
679
680   /* Operands for jump/call need special consideration.  */
681   if (current_templates->start->opcode_modifier.jump
682       || current_templates->start->opcode_modifier.jumpdword
683       || current_templates->start->opcode_modifier.jumpintersegment)
684     {
685       if (i.op[this_operand].regs
686           || intel_state.base
687           || intel_state.index
688           || intel_state.is_mem > 1)
689         i.types[this_operand].bitfield.jumpabsolute = 1;
690       else
691         switch (intel_state.op_modifier)
692           {
693           case O_near_ptr:
694             if (intel_state.seg)
695               i.types[this_operand].bitfield.jumpabsolute = 1;
696             else
697               intel_state.is_mem = 1;
698             break;
699           case O_far_ptr:
700           case O_absent:
701             if (!intel_state.seg)
702               {
703                 intel_state.is_mem = 1;
704                 if (intel_state.op_modifier == O_absent)
705                   {
706                     if (intel_state.is_indirect == 1)
707                       i.types[this_operand].bitfield.jumpabsolute = 1;
708                     break;
709                   }
710                 as_bad (_("cannot infer the segment part of the operand"));
711                 return 0;
712               }
713             else if (S_GET_SEGMENT (intel_state.seg) == reg_section)
714               i.types[this_operand].bitfield.jumpabsolute = 1;
715             else
716               {
717                 i386_operand_type types;
718
719                 if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
720                   {
721                     as_bad (_("at most %d immediate operands are allowed"),
722                             MAX_IMMEDIATE_OPERANDS);
723                     return 0;
724                   }
725                 expP = &im_expressions[i.imm_operands++];
726                 memset (expP, 0, sizeof(*expP));
727                 expP->X_op = O_symbol;
728                 expP->X_add_symbol = intel_state.seg;
729                 i.op[this_operand].imms = expP;
730
731                 resolve_expression (expP);
732                 operand_type_set (&types, ~0);
733                 if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg),
734                                               expP, types, operand_string))
735                   return 0;
736                 if (i.operands < MAX_OPERANDS)
737                   {
738                     this_operand = i.operands++;
739                     i.types[this_operand].bitfield.unspecified = 1;
740                   }
741                 if (suffix == LONG_DOUBLE_MNEM_SUFFIX)
742                   i.suffix = 0;
743                 intel_state.seg = NULL;
744                 intel_state.is_mem = 0;
745               }
746             break;
747           default:
748             i.types[this_operand].bitfield.jumpabsolute = 1;
749             break;
750           }
751       if (i.types[this_operand].bitfield.jumpabsolute)
752         intel_state.is_mem |= 1;
753     }
754   else if (intel_state.seg)
755     intel_state.is_mem |= 1;
756
757   if (i.op[this_operand].regs)
758     {
759       i386_operand_type temp;
760
761       /* Register operand.  */
762       if (intel_state.base || intel_state.index || intel_state.seg)
763         {
764           as_bad (_("invalid operand"));
765           return 0;
766         }
767
768       temp = i.op[this_operand].regs->reg_type;
769       temp.bitfield.baseindex = 0;
770       i.types[this_operand] = operand_type_or (i.types[this_operand],
771                                                temp);
772       i.types[this_operand].bitfield.unspecified = 0;
773       ++i.reg_operands;
774     }
775   else if (intel_state.base
776            || intel_state.index
777            || intel_state.seg
778            || intel_state.is_mem)
779     {
780       /* Memory operand.  */
781       if (i.mem_operands
782           >= 2 - !current_templates->start->opcode_modifier.isstring)
783         {
784           /* Handle
785
786              call       0x9090,0x90909090
787              lcall      0x9090,0x90909090
788              jmp        0x9090,0x90909090
789              ljmp       0x9090,0x90909090
790            */
791
792           if ((current_templates->start->opcode_modifier.jumpintersegment
793                || current_templates->start->opcode_modifier.jumpdword
794                || current_templates->start->opcode_modifier.jump)
795               && this_operand == 1
796               && intel_state.seg == NULL
797               && i.mem_operands == 1
798               && i.disp_operands == 1
799               && intel_state.op_modifier == O_absent)
800             {
801               /* Try to process the first operand as immediate,  */
802               this_operand = 0;
803               if (i386_finalize_immediate (exp_seg, i.op[0].imms,
804                                            intel_state.reloc_types,
805                                            NULL))
806                 {
807                   this_operand = 1;
808                   expP = &im_expressions[0];
809                   i.op[this_operand].imms = expP;
810                   *expP = exp;
811
812                   /* Try to process the second operand as immediate,  */
813                   if (i386_finalize_immediate (exp_seg, expP,
814                                                intel_state.reloc_types,
815                                                NULL))
816                     {
817                       i.mem_operands = 0;
818                       i.disp_operands = 0;
819                       i.imm_operands = 2;
820                       i.types[0].bitfield.mem = 0;
821                       i.types[0].bitfield.disp16 = 0;
822                       i.types[0].bitfield.disp32 = 0;
823                       i.types[0].bitfield.disp32s = 0;
824                       return 1;
825                     }
826                 }
827             }
828
829           as_bad (_("too many memory references for `%s'"),
830                   current_templates->start->name);
831           return 0;
832         }
833
834       expP = &disp_expressions[i.disp_operands];
835       memcpy (expP, &exp, sizeof(exp));
836       resolve_expression (expP);
837
838       if (expP->X_op != O_constant
839           || expP->X_add_number
840           || (!intel_state.base
841               && !intel_state.index))
842         {
843           i.op[this_operand].disps = expP;
844           i.disp_operands++;
845
846           if (flag_code == CODE_64BIT)
847             {
848               i.types[this_operand].bitfield.disp32 = 1;
849               if (!i.prefix[ADDR_PREFIX])
850                 {
851                   i.types[this_operand].bitfield.disp64 = 1;
852                   i.types[this_operand].bitfield.disp32s = 1;
853                 }
854             }
855           else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
856             i.types[this_operand].bitfield.disp32 = 1;
857           else
858             i.types[this_operand].bitfield.disp16 = 1;
859
860 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
861           /*
862            * exp_seg is used only for verification in
863            * i386_finalize_displacement, and we can end up seeing reg_section
864            * here - but we know we removed all registers from the expression
865            * (or error-ed on any remaining ones) in i386_intel_simplify.  I
866            * consider the check in i386_finalize_displacement bogus anyway, in
867            * particular because it doesn't allow for expr_section, so I'd
868            * rather see that check (and the similar one in
869            * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
870            * expert I can't really say whether that would have other bad side
871            * effects.
872            */
873           if (OUTPUT_FLAVOR == bfd_target_aout_flavour
874               && exp_seg == reg_section)
875             exp_seg = expP->X_op != O_constant ? undefined_section
876                                                : absolute_section;
877 #endif
878
879           if (!i386_finalize_displacement (exp_seg, expP,
880                                            intel_state.reloc_types,
881                                            operand_string))
882             return 0;
883         }
884
885       if (intel_state.base || intel_state.index)
886         i.types[this_operand].bitfield.baseindex = 1;
887
888       if (intel_state.seg)
889         {
890           for (;;)
891             {
892               expP = symbol_get_value_expression (intel_state.seg);
893               if (expP->X_op != O_full_ptr)
894                 break;
895               intel_state.seg = expP->X_add_symbol;
896             }
897           if (expP->X_op != O_register)
898             {
899               as_bad (_("segment register name expected"));
900               return 0;
901             }
902           if (!i386_regtab[expP->X_add_number].reg_type.bitfield.sreg2
903               && !i386_regtab[expP->X_add_number].reg_type.bitfield.sreg3)
904             {
905               as_bad (_("invalid use of register"));
906               return 0;
907             }
908           switch (i386_regtab[expP->X_add_number].reg_num)
909             {
910             case 0: i.seg[i.mem_operands] = &es; break;
911             case 1: i.seg[i.mem_operands] = &cs; break;
912             case 2: i.seg[i.mem_operands] = &ss; break;
913             case 3: i.seg[i.mem_operands] = &ds; break;
914             case 4: i.seg[i.mem_operands] = &fs; break;
915             case 5: i.seg[i.mem_operands] = &gs; break;
916             case RegFlat: i.seg[i.mem_operands] = NULL; break;
917             }
918         }
919
920       /* Swap base and index in 16-bit memory operands like
921          [si+bx]. Since i386_index_check is also used in AT&T
922          mode we have to do that here.  */
923       if (intel_state.base
924           && intel_state.index
925           && intel_state.base->reg_type.bitfield.reg16
926           && intel_state.index->reg_type.bitfield.reg16
927           && intel_state.base->reg_num >= 6
928           && intel_state.index->reg_num < 6)
929         {
930           i.base_reg = intel_state.index;
931           i.index_reg = intel_state.base;
932         }
933       else
934         {
935           i.base_reg = intel_state.base;
936           i.index_reg = intel_state.index;
937         }
938
939       if (!i386_index_check (operand_string))
940         return 0;
941
942       i.types[this_operand].bitfield.mem = 1;
943       ++i.mem_operands;
944     }
945   else
946     {
947       /* Immediate.  */
948       if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
949         {
950           as_bad (_("at most %d immediate operands are allowed"),
951                   MAX_IMMEDIATE_OPERANDS);
952           return 0;
953         }
954
955       expP = &im_expressions[i.imm_operands++];
956       i.op[this_operand].imms = expP;
957       *expP = exp;
958
959       return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
960                                       operand_string);
961     }
962
963   return 1;
964 }