Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/gas/config/tc-i386-intel.c
Line
Count
Source (jump to first uncovered line)
1
/* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
2
   Copyright (C) 2009-2025 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
0
#define O_offset O_md32
41
/* offset X_add_symbol */
42
0
#define O_short O_md31
43
/* near ptr X_add_symbol */
44
0
#define O_near_ptr O_md30
45
/* far ptr X_add_symbol */
46
0
#define O_far_ptr O_md29
47
/* byte ptr X_add_symbol */
48
0
#define O_byte_ptr O_md28
49
/* word ptr X_add_symbol */
50
0
#define O_word_ptr O_md27
51
/* dword ptr X_add_symbol */
52
0
#define O_dword_ptr O_md26
53
/* qword ptr X_add_symbol */
54
0
#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
0
#define O_fword_ptr O_md24
59
/* tbyte ptr X_add_symbol */
60
0
#define O_tbyte_ptr O_md23
61
/* oword ptr X_add_symbol */
62
0
#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
0
#define O_ymmword_ptr O_md21
67
/* zmmword ptr X_add_symbol */
68
0
#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
337k
{
124
337k
  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
337k
  if (!intel_syntax)
137
337k
    return O_absent;
138
139
0
  if (!name)
140
0
    {
141
0
      if (operands != 2)
142
0
  return O_illegal;
143
0
      switch (*input_line_pointer)
144
0
  {
145
0
  case ':':
146
0
    ++input_line_pointer;
147
0
    return O_full_ptr;
148
0
  case '[':
149
0
    ++input_line_pointer;
150
0
    return O_index;
151
0
  case '@':
152
0
    if (this_operand >= 0 && i.reloc[this_operand] == NO_RELOC)
153
0
      {
154
0
        int adjust = 0;
155
0
        char *gotfree_input_line = lex_got (&i.reloc[this_operand],
156
0
              &adjust,
157
0
              &intel_state.reloc_types);
158
159
0
        if (!gotfree_input_line)
160
0
    break;
161
0
        free (gotfree_input_line);
162
0
        *input_line_pointer++ = '+';
163
0
        memset (input_line_pointer, '0', adjust - 1);
164
0
        input_line_pointer[adjust - 1] = ' ';
165
0
        return O_add;
166
0
      }
167
0
    break;
168
0
  }
169
0
      return O_illegal;
170
0
    }
171
172
  /* See the quotation related comment in i386_parse_name().  */
173
0
  if (*pc == '"')
174
0
    return O_absent;
175
176
0
  for (j = 0; i386_operators[j].name; ++j)
177
0
    if (strcasecmp (i386_operators[j].name, name) == 0)
178
0
      {
179
0
  if (i386_operators[j].operands
180
0
      && i386_operators[j].operands != operands)
181
0
    return O_illegal;
182
0
  return i386_operators[j].op;
183
0
      }
184
185
0
  for (j = 0; i386_types[j].name; ++j)
186
0
    if (strcasecmp (i386_types[j].name, name) == 0)
187
0
      break;
188
189
0
  if (i386_types[j].name && is_whitespace (*pc))
190
0
    {
191
0
      const char *start = ++input_line_pointer;
192
0
      char *pname;
193
0
      char c = get_symbol_name (&pname);
194
195
0
      if (strcasecmp (pname, "ptr") == 0 && (c != '"' || pname == start))
196
0
  {
197
0
    pname[-1] = *pc;
198
0
    *pc = c;
199
0
    if (intel_syntax > 0 || operands != 1)
200
0
      return O_illegal;
201
0
    return i386_types[j].op;
202
0
  }
203
204
0
      if (strcasecmp (pname, "bcst") == 0 && (c != '"' || pname == start))
205
0
  {
206
0
    pname[-1] = *pc;
207
0
    *pc = c;
208
0
    if (intel_syntax > 0 || operands != 1
209
0
        || i386_types[j].sz[0] > 8
210
0
        || (i386_types[j].sz[0] & (i386_types[j].sz[0] - 1)))
211
0
      return O_illegal;
212
0
    switch (pp.encoding)
213
0
      {
214
0
      case encoding_default:
215
0
      case encoding_egpr:
216
0
        pp.encoding = encoding_evex;
217
0
        break;
218
0
      case encoding_evex:
219
0
      case encoding_evex512:
220
0
        break;
221
0
      default:
222
0
        return O_illegal;
223
0
      }
224
0
    if (!i.broadcast.bytes && !i.broadcast.type)
225
0
      {
226
0
        i.broadcast.bytes = i386_types[j].sz[0];
227
0
        i.broadcast.operand = this_operand;
228
0
      }
229
0
    return i386_types[j].op;
230
0
  }
231
232
0
      (void) restore_line_pointer (c);
233
0
      input_line_pointer = pname - 1;
234
0
    }
235
236
0
  return O_absent;
237
0
}
238
239
static int i386_intel_parse_name (const char *name,
240
          expressionS *e,
241
          enum expr_mode mode)
242
0
{
243
0
  unsigned int j;
244
245
0
  if (! strcmp (name, "$"))
246
0
    {
247
0
      current_location (e, mode);
248
0
      return 1;
249
0
    }
250
251
0
  for (j = 0; i386_types[j].name; ++j)
252
0
    if (strcasecmp(i386_types[j].name, name) == 0)
253
0
      {
254
0
  e->X_op = O_constant;
255
0
  e->X_add_number = i386_types[j].sz[flag_code];
256
0
  e->X_add_symbol = NULL;
257
0
  e->X_op_symbol = NULL;
258
0
  return 1;
259
0
      }
260
261
0
  return 0;
262
0
}
263
264
static INLINE int i386_intel_check (const reg_entry *rreg,
265
            const reg_entry *base,
266
            const reg_entry *iindex)
267
0
{
268
0
  if ((this_operand >= 0
269
0
       && rreg != i.op[this_operand].regs)
270
0
      || base != intel_state.base
271
0
      || iindex != intel_state.index)
272
0
    {
273
0
      as_bad (_("invalid use of register"));
274
0
      return 0;
275
0
    }
276
0
  return 1;
277
0
}
278
279
static INLINE void i386_intel_fold (expressionS *e, symbolS *sym)
280
0
{
281
0
  expressionS *exp = symbol_get_value_expression (sym);
282
0
  if (S_GET_SEGMENT (sym) == absolute_section)
283
0
    {
284
0
      offsetT val = e->X_add_number;
285
286
0
      *e = *exp;
287
0
      e->X_add_number += val;
288
0
    }
289
0
  else
290
0
    {
291
0
      if (exp->X_op == O_symbol
292
0
    && strcmp (S_GET_NAME (exp->X_add_symbol),
293
0
         GLOBAL_OFFSET_TABLE_NAME) == 0)
294
0
  sym = exp->X_add_symbol;
295
0
      e->X_add_symbol = sym;
296
0
      e->X_op_symbol = NULL;
297
0
      e->X_op = O_symbol;
298
0
    }
299
0
}
300
301
static int
302
i386_intel_simplify_register (expressionS *e)
303
0
{
304
0
  int reg_num;
305
306
0
  if (this_operand < 0 || intel_state.in_offset)
307
0
    {
308
0
      as_bad (_("invalid use of register"));
309
0
      return 0;
310
0
    }
311
312
0
  if (e->X_op == O_register)
313
0
    reg_num = e->X_add_number;
314
0
  else
315
0
    reg_num = e->X_md - 1;
316
317
0
  if (reg_num < 0 || reg_num >= (int) i386_regtab_size)
318
0
    {
319
0
      as_bad (_("invalid register number"));
320
0
      return 0;
321
0
    }
322
323
0
  if (!check_register (&i386_regtab[reg_num]))
324
0
    {
325
0
      as_bad (_("register '%s%s' cannot be used here"),
326
0
        register_prefix, i386_regtab[reg_num].reg_name);
327
0
      return 0;
328
0
    }
329
330
0
  if (!intel_state.in_bracket)
331
0
    {
332
0
      if (i.op[this_operand].regs)
333
0
  {
334
0
    as_bad (_("invalid use of register"));
335
0
    return 0;
336
0
  }
337
0
      if ((i386_regtab[reg_num].reg_type.bitfield.class == SReg
338
0
     && i386_regtab[reg_num].reg_num == RegFlat)
339
0
    || (dot_insn ()
340
0
        && i386_regtab[reg_num].reg_type.bitfield.class == ClassNone))
341
0
  {
342
0
    as_bad (_("invalid use of pseudo-register"));
343
0
    return 0;
344
0
  }
345
0
      i.op[this_operand].regs = i386_regtab + reg_num;
346
0
    }
347
0
  else if (!intel_state.index
348
0
     && (i386_regtab[reg_num].reg_type.bitfield.xmmword
349
0
         || i386_regtab[reg_num].reg_type.bitfield.ymmword
350
0
         || i386_regtab[reg_num].reg_type.bitfield.zmmword
351
0
         || i386_regtab[reg_num].reg_num == RegIZ))
352
0
    intel_state.index = i386_regtab + reg_num;
353
0
  else if (!intel_state.base && !intel_state.in_scale)
354
0
    intel_state.base = i386_regtab + reg_num;
355
0
  else if (!intel_state.index)
356
0
    {
357
0
      const insn_template *t = current_templates.start;
358
359
0
      if (intel_state.in_scale
360
0
    || i386_regtab[reg_num].reg_type.bitfield.baseindex
361
0
    || dot_insn ()
362
0
    || t->mnem_off == MN_bndmk
363
0
    || t->mnem_off == MN_bndldx
364
0
    || t->mnem_off == MN_bndstx)
365
0
  intel_state.index = i386_regtab + reg_num;
366
0
      else
367
0
  {
368
    /* Convert base to index and make ESP/RSP the base.  */
369
0
    intel_state.index = intel_state.base;
370
0
    intel_state.base = i386_regtab + reg_num;
371
0
  }
372
0
    }
373
0
  else
374
0
    {
375
      /* esp is invalid as index */
376
0
      intel_state.index = reg_eax + ESP_REG_NUM;
377
0
    }
378
0
  return 2;
379
0
}
380
381
static int
382
i386_intel_simplify_symbol (symbolS *sym)
383
0
{
384
0
  if (symbol_resolving_p (sym))
385
0
    return 1;
386
387
0
  symbol_mark_resolving (sym);
388
0
  int ret = i386_intel_simplify (symbol_get_value_expression (sym));
389
0
  if (ret == 2)
390
0
    {
391
0
      S_SET_SEGMENT (sym, absolute_section);
392
0
      ret = 1;
393
0
    }
394
0
  symbol_clear_resolving (sym);
395
0
  return ret;
396
0
}
397
398
static int
399
i386_intel_simplify (expressionS *e)
400
0
{
401
0
  const reg_entry *the_reg = (this_operand >= 0
402
0
            ? i.op[this_operand].regs : NULL);
403
0
  const reg_entry *base = intel_state.base;
404
0
  const reg_entry *state_index = intel_state.index;
405
0
  int ret;
406
407
0
  if (!intel_syntax)
408
0
    return 1;
409
410
0
  switch (e->X_op)
411
0
    {
412
0
    case O_index:
413
0
      if (e->X_add_symbol)
414
0
  {
415
0
    if (!i386_intel_simplify_symbol (e->X_add_symbol)
416
0
        || !i386_intel_check(the_reg, intel_state.base,
417
0
           intel_state.index))
418
0
      return 0;
419
0
  }
420
0
      if (!intel_state.in_offset)
421
0
  ++intel_state.in_bracket;
422
0
      ret = i386_intel_simplify_symbol (e->X_op_symbol);
423
0
      if (!intel_state.in_offset)
424
0
  --intel_state.in_bracket;
425
0
      if (!ret)
426
0
  return 0;
427
0
      if (e->X_add_symbol)
428
0
  e->X_op = O_add;
429
0
      else
430
0
  i386_intel_fold (e, e->X_op_symbol);
431
0
      break;
432
433
0
    case O_offset:
434
0
      intel_state.has_offset = 1;
435
0
      ++intel_state.in_offset;
436
0
      ret = i386_intel_simplify_symbol (e->X_add_symbol);
437
0
      --intel_state.in_offset;
438
0
      if (!ret || !i386_intel_check(the_reg, base, state_index))
439
0
  return 0;
440
0
      i386_intel_fold (e, e->X_add_symbol);
441
0
      return ret;
442
443
0
    case O_byte_ptr:
444
0
    case O_word_ptr:
445
0
    case O_dword_ptr:
446
0
    case O_fword_ptr:
447
0
    case O_qword_ptr: /* O_mmword_ptr */
448
0
    case O_tbyte_ptr:
449
0
    case O_oword_ptr: /* O_xmmword_ptr */
450
0
    case O_ymmword_ptr:
451
0
    case O_zmmword_ptr:
452
0
    case O_near_ptr:
453
0
    case O_far_ptr:
454
0
      if (intel_state.op_modifier == O_absent)
455
0
  intel_state.op_modifier = e->X_op;
456
      /* FALLTHROUGH */
457
0
    case O_short:
458
0
      if (symbol_get_value_expression (e->X_add_symbol)->X_op
459
0
    == O_register)
460
0
  {
461
0
    as_bad (_("invalid use of register"));
462
0
    return 0;
463
0
  }
464
0
      if (!i386_intel_simplify_symbol (e->X_add_symbol))
465
0
  return 0;
466
0
      i386_intel_fold (e, e->X_add_symbol);
467
0
      break;
468
469
0
    case O_full_ptr:
470
0
      if (symbol_get_value_expression (e->X_op_symbol)->X_op
471
0
    == O_register)
472
0
  {
473
0
    as_bad (_("invalid use of register"));
474
0
    return 0;
475
0
  }
476
0
      if (!i386_intel_simplify_symbol (e->X_op_symbol)
477
0
    || !i386_intel_check(the_reg, intel_state.base,
478
0
             intel_state.index))
479
0
  return 0;
480
0
      if (!intel_state.in_offset)
481
0
  {
482
0
    if (!intel_state.seg)
483
0
      intel_state.seg = e->X_add_symbol;
484
0
    else
485
0
      {
486
0
        expressionS exp;
487
488
0
        exp.X_op = O_full_ptr;
489
0
        exp.X_add_symbol = e->X_add_symbol;
490
0
        exp.X_op_symbol = intel_state.seg;
491
0
        intel_state.seg = make_expr_symbol (&exp);
492
0
      }
493
0
  }
494
0
      i386_intel_fold (e, e->X_op_symbol);
495
0
      break;
496
497
0
    case O_multiply:
498
0
      if (this_operand >= 0 && intel_state.in_bracket)
499
0
  {
500
0
    expressionS *scale = NULL;
501
0
    int has_index = (intel_state.index != NULL);
502
503
0
    if (!intel_state.in_scale++)
504
0
      intel_state.scale_factor = 1;
505
506
0
    ret = i386_intel_simplify_symbol (e->X_add_symbol);
507
0
    if (ret && !has_index && intel_state.index)
508
0
      scale = symbol_get_value_expression (e->X_op_symbol);
509
510
0
    if (ret)
511
0
      ret = i386_intel_simplify_symbol (e->X_op_symbol);
512
0
    if (ret && !scale && !has_index && intel_state.index)
513
0
      scale = symbol_get_value_expression (e->X_add_symbol);
514
515
0
    if (ret && scale)
516
0
      {
517
0
        resolve_expression (scale);
518
0
        if (scale->X_op != O_constant
519
0
      || intel_state.index->reg_type.bitfield.word)
520
0
    scale->X_add_number = 0;
521
0
        intel_state.scale_factor *= scale->X_add_number;
522
0
      }
523
524
0
    --intel_state.in_scale;
525
0
    if (!ret)
526
0
      return 0;
527
528
0
    if (!intel_state.in_scale)
529
0
      switch (intel_state.scale_factor)
530
0
        {
531
0
        case 1:
532
0
    i.log2_scale_factor = 0;
533
0
    break;
534
0
        case 2:
535
0
    i.log2_scale_factor = 1;
536
0
    break;
537
0
        case 4:
538
0
    i.log2_scale_factor = 2;
539
0
    break;
540
0
        case 8:
541
0
    i.log2_scale_factor = 3;
542
0
    break;
543
0
        default:
544
    /* esp is invalid as index */
545
0
    intel_state.index = reg_eax + ESP_REG_NUM;
546
0
    break;
547
0
        }
548
549
0
    break;
550
0
  }
551
0
      goto fallthrough;
552
553
0
    case O_register:
554
0
      ret = i386_intel_simplify_register (e);
555
0
      if (ret == 2)
556
0
  {
557
0
    gas_assert (e->X_add_number < (unsigned short) -1);
558
0
    e->X_md = (unsigned short) e->X_add_number + 1;
559
0
    e->X_op = O_constant;
560
0
    e->X_add_number = 0;
561
0
  }
562
0
      return ret;
563
564
0
    case O_constant:
565
0
      if (e->X_md)
566
0
  return i386_intel_simplify_register (e);
567
568
      /* FALLTHROUGH */
569
0
    default:
570
0
    fallthrough:
571
0
      if (e->X_add_symbol
572
0
    && !i386_intel_simplify_symbol (e->X_add_symbol))
573
0
  return 0;
574
0
      if (!the_reg && this_operand >= 0
575
0
    && e->X_op == O_symbol && !e->X_add_number)
576
0
  the_reg = i.op[this_operand].regs;
577
0
      if (e->X_op == O_add || e->X_op == O_subtract)
578
0
  {
579
0
    base = intel_state.base;
580
0
    state_index = intel_state.index;
581
0
  }
582
0
      if (!i386_intel_check (the_reg, base, state_index)
583
0
    || (e->X_op_symbol
584
0
        && !i386_intel_simplify_symbol (e->X_op_symbol))
585
0
    || !i386_intel_check (the_reg,
586
0
        (e->X_op != O_add
587
0
         ? base : intel_state.base),
588
0
        (e->X_op != O_add
589
0
         ? state_index : intel_state.index)))
590
0
  return 0;
591
0
      break;
592
0
    }
593
594
0
  if (this_operand >= 0
595
0
      && e->X_op == O_symbol
596
0
      && !intel_state.in_offset)
597
0
    {
598
0
      segT seg = S_GET_SEGMENT (e->X_add_symbol);
599
600
0
      if (seg != absolute_section
601
0
    && seg != reg_section
602
0
    && seg != expr_section)
603
0
  intel_state.is_mem |= 2 - !intel_state.in_bracket;
604
0
    }
605
606
0
  return 1;
607
0
}
608
609
int i386_need_index_operator (void)
610
105
{
611
105
  return intel_syntax < 0;
612
105
}
613
614
static int
615
i386_intel_operand (char *operand_string, int got_a_float)
616
0
{
617
0
  char *saved_input_line_pointer, *buf;
618
0
  segT exp_seg;
619
0
  expressionS exp, *expP;
620
0
  char suffix = 0;
621
0
  bool rc_sae_modifier = i.rounding.type != rc_none && i.rounding.modifier;
622
0
  int ret;
623
624
  /* Handle vector immediates.  */
625
0
  if (RC_SAE_immediate (operand_string))
626
0
    {
627
0
      if (i.imm_operands)
628
0
  {
629
0
    as_bad (_("`%s': RC/SAE operand must precede immediate operands"),
630
0
      insn_name (current_templates.start));
631
0
    return 0;
632
0
  }
633
634
0
      return 1;
635
0
    }
636
637
  /* Initialize state structure.  */
638
0
  intel_state.op_modifier = O_absent;
639
0
  intel_state.is_mem = 0;
640
0
  intel_state.is_indirect = 0;
641
0
  intel_state.has_offset = 0;
642
0
  intel_state.base = NULL;
643
0
  intel_state.index = NULL;
644
0
  intel_state.seg = NULL;
645
0
  operand_type_set (&intel_state.reloc_types, ~0);
646
0
  gas_assert (!intel_state.in_offset);
647
0
  gas_assert (!intel_state.in_bracket);
648
0
  gas_assert (!intel_state.in_scale);
649
650
0
  saved_input_line_pointer = input_line_pointer;
651
0
  input_line_pointer = buf = xstrdup (operand_string);
652
653
0
  intel_syntax = -1;
654
0
  expr_mode = expr_operator_none;
655
0
  memset (&exp, 0, sizeof(exp));
656
0
  exp_seg = expression (&exp);
657
0
  ret = i386_intel_simplify (&exp);
658
0
  intel_syntax = 1;
659
660
0
  SKIP_WHITESPACE ();
661
662
  /* Handle vector operations.  */
663
0
  if (*input_line_pointer == '{')
664
0
    {
665
0
      char *end = check_VecOperations (input_line_pointer);
666
0
      if (end)
667
0
  input_line_pointer = end;
668
0
      else
669
0
  ret = 0;
670
0
    }
671
672
0
  if (!is_end_of_stmt (*input_line_pointer))
673
0
    {
674
0
      if (ret)
675
0
  as_bad (_("junk `%s' after expression"), input_line_pointer);
676
0
      ret = 0;
677
0
    }
678
0
  else if (exp.X_op == O_illegal || exp.X_op == O_absent)
679
0
    {
680
0
      if (ret)
681
0
  as_bad (_("invalid expression"));
682
0
      ret = 0;
683
0
    }
684
0
  else if (!intel_state.has_offset
685
0
     && input_line_pointer > buf
686
0
     && *(input_line_pointer - 1) == ']')
687
0
    {
688
0
      intel_state.is_mem |= 1;
689
0
      intel_state.is_indirect = 1;
690
0
    }
691
692
0
  input_line_pointer = saved_input_line_pointer;
693
0
  free (buf);
694
695
0
  gas_assert (!intel_state.in_offset);
696
0
  gas_assert (!intel_state.in_bracket);
697
0
  gas_assert (!intel_state.in_scale);
698
699
0
  if (!ret)
700
0
    return 0;
701
702
0
  if (intel_state.op_modifier != O_absent
703
0
      && current_templates.start->mnem_off != MN_lea)
704
0
    {
705
0
      i.types[this_operand].bitfield.unspecified = 0;
706
707
0
      switch (intel_state.op_modifier)
708
0
  {
709
0
  case O_byte_ptr:
710
0
    i.types[this_operand].bitfield.byte = 1;
711
0
    suffix = BYTE_MNEM_SUFFIX;
712
0
    break;
713
714
0
  case O_word_ptr:
715
0
    i.types[this_operand].bitfield.word = 1;
716
0
    if (got_a_float == 2) /* "fi..." */
717
0
      suffix = SHORT_MNEM_SUFFIX;
718
0
    else if (current_templates.start->mnem_off != MN_lar
719
0
       && current_templates.start->mnem_off != MN_lsl
720
0
       && current_templates.start->mnem_off != MN_arpl)
721
0
      suffix = WORD_MNEM_SUFFIX;
722
0
    break;
723
724
0
  case O_dword_ptr:
725
0
    i.types[this_operand].bitfield.dword = 1;
726
0
    if ((insn_name (current_templates.start)[0] == 'l'
727
0
         && insn_name (current_templates.start)[2] == 's'
728
0
         && insn_name (current_templates.start)[3] == 0)
729
0
        || current_templates.start->mnem_off == MN_bound)
730
0
      suffix = WORD_MNEM_SUFFIX;
731
0
    else if (flag_code != CODE_32BIT
732
0
       && (current_templates.start->opcode_modifier.jump == JUMP
733
0
           || current_templates.start->opcode_modifier.jump
734
0
        == JUMP_DWORD))
735
0
      {
736
0
        i.far_branch = true;
737
0
        suffix = WORD_MNEM_SUFFIX;
738
0
      }
739
0
    else if (got_a_float == 1) /* "f..." */
740
0
      suffix = SHORT_MNEM_SUFFIX;
741
0
    else
742
0
      suffix = LONG_MNEM_SUFFIX;
743
0
    break;
744
745
0
  case O_fword_ptr:
746
0
    i.types[this_operand].bitfield.fword = 1;
747
0
    if (current_templates.start->mnem_off == MN_les
748
0
        || current_templates.start->mnem_off == MN_lds
749
0
        || current_templates.start->mnem_off == MN_lss
750
0
        || current_templates.start->mnem_off == MN_lfs
751
0
        || current_templates.start->mnem_off == MN_lgs)
752
0
      suffix = LONG_MNEM_SUFFIX;
753
0
    else if (!got_a_float)
754
0
      {
755
0
        if (flag_code == CODE_16BIT)
756
0
    add_prefix (DATA_PREFIX_OPCODE);
757
0
        i.far_branch = true;
758
0
      }
759
0
    break;
760
761
0
  case O_qword_ptr: /* O_mmword_ptr */
762
0
    i.types[this_operand].bitfield.qword = 1;
763
0
    if (current_templates.start->mnem_off == MN_bound
764
0
        || got_a_float == 1) /* "f..." */
765
0
      suffix = LONG_MNEM_SUFFIX;
766
0
    else
767
0
      suffix = QWORD_MNEM_SUFFIX;
768
0
    break;
769
770
0
  case O_tbyte_ptr:
771
0
    i.types[this_operand].bitfield.tbyte = 1;
772
0
    if (got_a_float)
773
0
      break;
774
0
    if (flag_code == CODE_64BIT
775
0
        && (current_templates.start->operand_types[0].bitfield.fword
776
0
      || current_templates.start->operand_types[0].bitfield.tbyte
777
0
      || current_templates.start->opcode_modifier.jump == JUMP_DWORD
778
0
      || current_templates.start->opcode_modifier.jump == JUMP))
779
0
      suffix = QWORD_MNEM_SUFFIX; /* l[fgs]s, [ls][gi]dt, call, jmp */
780
0
    else
781
0
      i.types[this_operand].bitfield.byte = 1; /* cause an error */
782
0
    break;
783
784
0
  case O_oword_ptr: /* O_xmmword_ptr */
785
0
    i.types[this_operand].bitfield.xmmword = 1;
786
0
    break;
787
788
0
  case O_ymmword_ptr:
789
0
    if (vector_size < VSZ256)
790
0
      {
791
0
        as_bad (_("256-bit operands disabled"));
792
0
        return 0;
793
0
      }
794
0
    i.types[this_operand].bitfield.ymmword = 1;
795
0
    break;
796
797
0
  case O_zmmword_ptr:
798
0
    if (vector_size < VSZ512)
799
0
      {
800
0
        as_bad (_("512-bit operands disabled"));
801
0
        return 0;
802
0
      }
803
0
    i.types[this_operand].bitfield.zmmword = 1;
804
0
    break;
805
806
0
  case O_far_ptr:
807
0
    i.far_branch = true;
808
    /* FALLTHROUGH */
809
0
  case O_near_ptr:
810
0
    if (current_templates.start->opcode_modifier.jump != JUMP
811
0
        && current_templates.start->opcode_modifier.jump != JUMP_DWORD)
812
0
      {
813
        /* cause an error */
814
0
        i.types[this_operand].bitfield.byte = 1;
815
0
        i.types[this_operand].bitfield.tbyte = 1;
816
0
        suffix = i.suffix;
817
0
      }
818
0
    break;
819
820
0
  default:
821
0
    BAD_CASE (intel_state.op_modifier);
822
0
    break;
823
0
  }
824
825
      /* Now check whether we actually want to infer an AT&T-like suffix.
826
   We really only need to do this when operand size determination (incl.
827
   REX.W) is going to be derived from it.  For this we check whether the
828
   given suffix is valid for any of the candidate templates.  */
829
0
      if (suffix && suffix != i.suffix
830
0
    && current_templates.start->mnem_off != MN_bound)
831
0
  {
832
0
    const insn_template *t;
833
834
0
    for (t = current_templates.start; t < current_templates.end; ++t)
835
0
      {
836
        /* Operands haven't been swapped yet.  */
837
0
        unsigned int op = t->operands - 1 - this_operand;
838
839
        /* Easy checks to skip templates which won't match anyway.  */
840
0
        if (this_operand >= t->operands
841
0
      || t->opcode_modifier.dialect >= ATT_SYNTAX)
842
0
    continue;
843
844
0
        switch (suffix)
845
0
    {
846
0
    case BYTE_MNEM_SUFFIX:
847
0
      if (t->opcode_modifier.no_bsuf)
848
0
        continue;
849
0
      break;
850
0
    case WORD_MNEM_SUFFIX:
851
0
      if (t->opcode_modifier.no_wsuf)
852
0
        continue;
853
0
      break;
854
0
    case LONG_MNEM_SUFFIX:
855
0
      if (t->opcode_modifier.no_lsuf)
856
0
        continue;
857
0
      break;
858
0
    case QWORD_MNEM_SUFFIX:
859
0
      if (t->opcode_modifier.no_qsuf || !q_suffix_allowed (t))
860
0
        continue;
861
0
      break;
862
0
    case SHORT_MNEM_SUFFIX:
863
0
      if (t->opcode_modifier.no_ssuf)
864
0
        continue;
865
0
      break;
866
0
    default:
867
0
      abort ();
868
0
    }
869
870
        /* We can skip templates with swappable operands here, as one
871
     operand will be a register, which operand size can be
872
     determined from.  */
873
0
        if (t->opcode_modifier.d)
874
0
    continue;
875
876
        /* In a few cases suffixes are permitted, but we can nevertheless
877
     derive that these aren't going to be needed.  This is only of
878
     interest for insns using ModR/M.  */
879
0
        if (!t->opcode_modifier.modrm)
880
0
    break;
881
882
0
        if (!t->operand_types[op].bitfield.baseindex)
883
0
    continue;
884
885
0
        switch (t->operand_types[op].bitfield.class)
886
0
    {
887
0
    case RegMMX:
888
0
    case RegSIMD:
889
0
    case RegMask:
890
0
      continue;
891
0
    }
892
893
0
        break;
894
0
      }
895
896
0
    if (t == current_templates.end)
897
0
      suffix = 0;
898
0
  }
899
900
0
      if (!i.suffix)
901
0
  i.suffix = suffix;
902
0
      else if (suffix && i.suffix != suffix)
903
0
  {
904
0
    as_bad (_("conflicting operand size modifiers"));
905
0
    return 0;
906
0
  }
907
0
    }
908
909
  /* Operands for jump/call need special consideration.  */
910
0
  if (current_templates.start->opcode_modifier.jump
911
0
      || current_templates.start->mnem_off == MN_jmpabs)
912
0
    {
913
0
      bool jumpabsolute = false;
914
915
0
      if (i.op[this_operand].regs
916
0
    || intel_state.base
917
0
    || intel_state.index
918
0
    || intel_state.is_mem > 1)
919
0
  jumpabsolute = true;
920
0
      else
921
0
  switch (intel_state.op_modifier)
922
0
    {
923
0
    case O_near_ptr:
924
0
      if (intel_state.seg)
925
0
        jumpabsolute = true;
926
0
      else
927
0
        intel_state.is_mem = 1;
928
0
      break;
929
0
    case O_far_ptr:
930
0
    case O_absent:
931
0
      if (!intel_state.seg)
932
0
        {
933
0
    intel_state.is_mem = 1;
934
0
    if (intel_state.op_modifier == O_absent)
935
0
      {
936
0
        if (intel_state.is_indirect == 1)
937
0
          jumpabsolute = true;
938
0
        break;
939
0
      }
940
0
    as_bad (_("cannot infer the segment part of the operand"));
941
0
    return 0;
942
0
        }
943
0
      else if (S_GET_SEGMENT (intel_state.seg) == reg_section)
944
0
        {
945
0
    jumpabsolute = true;
946
0
    if (intel_state.op_modifier == O_far_ptr)
947
0
      i.far_branch = true;
948
0
        }
949
0
      else
950
0
        {
951
0
    i386_operand_type types;
952
953
0
    if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
954
0
      {
955
0
        as_bad (_("at most %d immediate operands are allowed"),
956
0
          MAX_IMMEDIATE_OPERANDS);
957
0
        return 0;
958
0
      }
959
0
    expP = &im_expressions[i.imm_operands++];
960
0
    memset (expP, 0, sizeof(*expP));
961
0
    expP->X_op = O_symbol;
962
0
    expP->X_add_symbol = intel_state.seg;
963
0
    i.op[this_operand].imms = expP;
964
965
0
    resolve_expression (expP);
966
0
    operand_type_set (&types, ~0);
967
0
    if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg),
968
0
                expP, types, operand_string))
969
0
      return 0;
970
0
    if (i.operands < MAX_OPERANDS)
971
0
      {
972
0
        this_operand = i.operands++;
973
0
        i.types[this_operand].bitfield.unspecified = 1;
974
0
      }
975
0
    intel_state.seg = NULL;
976
0
    intel_state.is_mem = 0;
977
0
        }
978
0
      break;
979
0
    default:
980
0
      jumpabsolute = true;
981
0
      break;
982
0
    }
983
0
      if (jumpabsolute)
984
0
  {
985
0
    i.jumpabsolute = true;
986
0
    intel_state.is_mem |= 1;
987
0
  }
988
0
    }
989
0
  else if (intel_state.seg)
990
0
    intel_state.is_mem |= 1;
991
992
0
  if (i.op[this_operand].regs)
993
0
    {
994
0
      i386_operand_type temp;
995
996
      /* Register operand.  */
997
0
      if (intel_state.base || intel_state.index || intel_state.seg
998
0
          || i.imm_bits[this_operand])
999
0
  {
1000
0
    as_bad (_("invalid operand"));
1001
0
    return 0;
1002
0
  }
1003
1004
0
      temp = i.op[this_operand].regs->reg_type;
1005
0
      temp.bitfield.baseindex = 0;
1006
0
      i.types[this_operand] = operand_type_or (i.types[this_operand],
1007
0
                 temp);
1008
0
      i.types[this_operand].bitfield.unspecified = 0;
1009
0
      ++i.reg_operands;
1010
1011
0
      if ((i.rounding.type != rc_none && !i.rounding.modifier
1012
0
     && temp.bitfield.class != Reg)
1013
0
    || rc_sae_modifier)
1014
0
  {
1015
0
    unsigned int j;
1016
1017
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
1018
0
      if (i.rounding.type == RC_NamesTable[j].type)
1019
0
        break;
1020
0
    as_bad (_("`%s': misplaced `{%s}'"),
1021
0
      insn_name (current_templates.start), RC_NamesTable[j].name);
1022
0
    return 0;
1023
0
  }
1024
0
    }
1025
0
  else if (intel_state.base
1026
0
     || intel_state.index
1027
0
     || intel_state.seg
1028
0
     || intel_state.is_mem)
1029
0
    {
1030
      /* Memory operand.  */
1031
0
      if (i.imm_bits[this_operand])
1032
0
  {
1033
0
    as_bad (_("invalid operand"));
1034
0
    return 0;
1035
0
  }
1036
1037
0
      if (i.mem_operands)
1038
0
  {
1039
    /* Handle
1040
1041
       call 0x9090,0x90909090
1042
       lcall  0x9090,0x90909090
1043
       jmp  0x9090,0x90909090
1044
       ljmp 0x9090,0x90909090
1045
     */
1046
1047
0
    if (current_templates.start->opcode_modifier.jump
1048
0
        && this_operand == 1
1049
0
        && intel_state.seg == NULL
1050
0
        && i.mem_operands == 1
1051
0
        && i.disp_operands == 1
1052
0
        && intel_state.op_modifier == O_absent)
1053
0
      {
1054
        /* Try to process the first operand as immediate,  */
1055
0
        this_operand = 0;
1056
0
        if (i386_finalize_immediate (exp_seg, i.op[0].imms,
1057
0
             intel_state.reloc_types,
1058
0
             NULL))
1059
0
    {
1060
0
      this_operand = 1;
1061
0
      expP = &im_expressions[0];
1062
0
      i.op[this_operand].imms = expP;
1063
0
      *expP = exp;
1064
1065
      /* Try to process the second operand as immediate,  */
1066
0
      if (i386_finalize_immediate (exp_seg, expP,
1067
0
                 intel_state.reloc_types,
1068
0
                 NULL))
1069
0
        {
1070
0
          i.mem_operands = 0;
1071
0
          i.disp_operands = 0;
1072
0
          i.imm_operands = 2;
1073
0
          i.flags[0] &= ~Operand_Mem;
1074
0
          i.types[0].bitfield.disp16 = 0;
1075
0
          i.types[0].bitfield.disp32 = 0;
1076
0
          return 1;
1077
0
        }
1078
0
    }
1079
0
      }
1080
0
  }
1081
1082
      /* Swap base and index in 16-bit memory operands like
1083
   [si+bx]. Since i386_index_check is also used in AT&T
1084
   mode we have to do this here.  */
1085
0
      if (intel_state.base
1086
0
    && intel_state.index
1087
0
    && intel_state.base->reg_type.bitfield.word
1088
0
    && intel_state.index->reg_type.bitfield.word
1089
0
    && intel_state.base->reg_num >= 6
1090
0
    && intel_state.index->reg_num < 6)
1091
0
  {
1092
0
    i.base_reg = intel_state.index;
1093
0
    i.index_reg = intel_state.base;
1094
0
  }
1095
0
      else
1096
0
  {
1097
0
    i.base_reg = intel_state.base;
1098
0
    i.index_reg = intel_state.index;
1099
0
  }
1100
1101
0
      if (i.base_reg || i.index_reg)
1102
0
  i.types[this_operand].bitfield.baseindex = 1;
1103
1104
0
      expP = &disp_expressions[i.disp_operands];
1105
0
      memcpy (expP, &exp, sizeof(exp));
1106
0
      resolve_expression (expP);
1107
1108
0
      if (expP->X_op != O_constant
1109
0
    || expP->X_add_number
1110
0
    || !i.types[this_operand].bitfield.baseindex)
1111
0
  {
1112
0
    i.op[this_operand].disps = expP;
1113
0
    i.disp_operands++;
1114
1115
0
    i386_addressing_mode ();
1116
1117
0
    if (flag_code == CODE_64BIT)
1118
0
      {
1119
0
        i.types[this_operand].bitfield.disp32 = 1;
1120
0
        if (!i.prefix[ADDR_PREFIX])
1121
0
    i.types[this_operand].bitfield.disp64 = 1;
1122
0
      }
1123
0
    else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
1124
0
      i.types[this_operand].bitfield.disp32 = 1;
1125
0
    else
1126
0
      i.types[this_operand].bitfield.disp16 = 1;
1127
1128
#ifdef OBJ_AOUT
1129
    /*
1130
     * exp_seg is used only for verification in
1131
     * i386_finalize_displacement, and we can end up seeing reg_section
1132
     * here - but we know we removed all registers from the expression
1133
     * (or error-ed on any remaining ones) in i386_intel_simplify.  I
1134
     * consider the check in i386_finalize_displacement bogus anyway, in
1135
     * particular because it doesn't allow for expr_section, so I'd
1136
     * rather see that check (and the similar one in
1137
     * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
1138
     * expert I can't really say whether that would have other bad side
1139
     * effects.
1140
     */
1141
    if (OUTPUT_FLAVOR == bfd_target_aout_flavour
1142
        && exp_seg == reg_section)
1143
      exp_seg = expP->X_op != O_constant ? undefined_section
1144
                 : absolute_section;
1145
#endif
1146
1147
0
    if (!i386_finalize_displacement (exp_seg, expP,
1148
0
             intel_state.reloc_types,
1149
0
             operand_string))
1150
0
      return 0;
1151
0
  }
1152
1153
0
      if (intel_state.seg)
1154
0
  {
1155
0
    for (ret = check_none; ; ret = operand_check)
1156
0
      {
1157
0
        expP = symbol_get_value_expression (intel_state.seg);
1158
0
        if (expP->X_op != O_full_ptr 
1159
0
      || symbol_get_value_expression (expP->X_op_symbol)->X_op
1160
0
         != O_register)
1161
0
    break;
1162
0
        intel_state.seg = expP->X_add_symbol;
1163
0
      }
1164
0
    if (expP->X_op != O_register)
1165
0
      {
1166
0
        as_bad (_("segment register name expected"));
1167
0
        return 0;
1168
0
      }
1169
0
    if (i386_regtab[expP->X_add_number].reg_type.bitfield.class != SReg)
1170
0
      {
1171
0
        as_bad (_("invalid use of register"));
1172
0
        return 0;
1173
0
      }
1174
0
    switch (ret)
1175
0
      {
1176
0
      case check_error:
1177
0
        as_bad (_("redundant segment overrides"));
1178
0
        return 0;
1179
0
      case check_warning:
1180
0
        as_warn (_("redundant segment overrides"));
1181
0
        break;
1182
0
      }
1183
0
    if (i386_regtab[expP->X_add_number].reg_num == RegFlat)
1184
0
      i.seg[i.mem_operands] = NULL;
1185
0
    else
1186
0
      i.seg[i.mem_operands] = &i386_regtab[expP->X_add_number];
1187
0
  }
1188
1189
0
      if (!i386_index_check (operand_string))
1190
0
  return 0;
1191
1192
0
      i.flags[this_operand] |= Operand_Mem;
1193
0
      ++i.mem_operands;
1194
0
    }
1195
0
  else
1196
0
    {
1197
      /* Immediate.  */
1198
0
      if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
1199
0
  {
1200
0
    as_bad (_("at most %d immediate operands are allowed"),
1201
0
      MAX_IMMEDIATE_OPERANDS);
1202
0
    return 0;
1203
0
  }
1204
1205
0
      expP = &im_expressions[i.imm_operands++];
1206
0
      i.op[this_operand].imms = expP;
1207
0
      *expP = exp;
1208
1209
0
      return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
1210
0
              operand_string);
1211
0
    }
1212
1213
0
  return 1;
1214
0
}