Coverage Report

Created: 2024-05-21 06:29

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