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