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