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