Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/config/tc-i386-intel.c
Line
Count
Source
1
/* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
2
   Copyright (C) 2009-2026 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
141k
{
124
141k
  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
141k
  if (!intel_syntax)
137
38.3k
    return O_absent;
138
139
102k
  if (!name)
140
53.7k
    {
141
53.7k
      if (operands != 2)
142
0
  return O_illegal;
143
53.7k
      switch (*input_line_pointer)
144
53.7k
  {
145
2.02k
  case ':':
146
2.02k
    ++input_line_pointer;
147
2.02k
    return O_full_ptr;
148
292
  case '[':
149
292
    ++input_line_pointer;
150
292
    return O_index;
151
334
  case '@':
152
334
    if (this_operand >= 0 && i.reloc[this_operand] == NO_RELOC)
153
150
      {
154
150
        int adjust = 0;
155
150
        char *gotfree_input_line = lex_got (&i.reloc[this_operand],
156
150
              &adjust,
157
150
              &intel_state.reloc_types);
158
159
150
        if (!gotfree_input_line)
160
84
    break;
161
66
        free (gotfree_input_line);
162
66
        *input_line_pointer++ = '+';
163
66
        memset (input_line_pointer, '0', adjust - 1);
164
66
        input_line_pointer[adjust - 1] = ' ';
165
66
        return O_add;
166
150
      }
167
184
    break;
168
53.7k
  }
169
51.3k
      return O_illegal;
170
53.7k
    }
171
172
  /* See the quotation related comment in i386_parse_name().  */
173
49.2k
  if (*pc == '"')
174
1.30k
    return O_absent;
175
176
766k
  for (j = 0; i386_operators[j].name; ++j)
177
718k
    if (strcasecmp (i386_operators[j].name, name) == 0)
178
19
      {
179
19
  if (i386_operators[j].operands
180
19
      && i386_operators[j].operands != operands)
181
10
    return O_illegal;
182
9
  return i386_operators[j].op;
183
19
      }
184
185
670k
  for (j = 0; i386_types[j].name; ++j)
186
622k
    if (strcasecmp (i386_types[j].name, name) == 0)
187
58
      break;
188
189
47.9k
  if (i386_types[j].name && is_whitespace (*pc))
190
22
    {
191
22
      const char *start = ++input_line_pointer;
192
22
      char *pname;
193
22
      char c = get_symbol_name (&pname);
194
195
22
      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
22
      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
22
      (void) restore_line_pointer (c);
233
22
      input_line_pointer = pname - 1;
234
22
    }
235
236
47.9k
  return O_absent;
237
47.9k
}
238
239
static int i386_intel_parse_name (const char *name,
240
          expressionS *e,
241
          enum expr_mode mode)
242
30.6k
{
243
30.6k
  unsigned int j;
244
245
30.6k
  if (! strcmp (name, "$"))
246
507
    {
247
507
      current_location (e, mode);
248
507
      return 1;
249
507
    }
250
251
422k
  for (j = 0; i386_types[j].name; ++j)
252
392k
    if (strcasecmp(i386_types[j].name, name) == 0)
253
14
      {
254
14
  e->X_op = O_constant;
255
14
  e->X_add_number = i386_types[j].sz[flag_code];
256
14
  e->X_add_symbol = NULL;
257
14
  e->X_op_symbol = NULL;
258
14
  return 1;
259
14
      }
260
261
30.1k
  return 0;
262
30.1k
}
263
264
static INLINE int i386_intel_check (const reg_entry *rreg,
265
            const reg_entry *base,
266
            const reg_entry *iindex)
267
40.6k
{
268
40.6k
  if ((this_operand >= 0
269
36.2k
       && rreg != i.op[this_operand].regs)
270
40.4k
      || base != intel_state.base
271
40.4k
      || iindex != intel_state.index)
272
203
    {
273
203
      as_bad (_("invalid use of register"));
274
203
      return 0;
275
203
    }
276
40.4k
  return 1;
277
40.6k
}
278
279
static INLINE void i386_intel_fold (expressionS *e, symbolS *sym)
280
195
{
281
195
  expressionS *exp = symbol_get_value_expression (sym);
282
195
  if (S_GET_SEGMENT (sym) == absolute_section)
283
152
    {
284
152
      offsetT val = e->X_add_number;
285
286
152
      *e = *exp;
287
152
      e->X_add_number += val;
288
152
    }
289
43
  else
290
43
    {
291
43
      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
43
      e->X_add_symbol = sym;
296
43
      e->X_op_symbol = NULL;
297
43
      e->X_op = O_symbol;
298
43
    }
299
195
}
300
301
static int
302
i386_intel_simplify_register (expressionS *e)
303
640
{
304
640
  int reg_num;
305
306
640
  if (this_operand < 0 || intel_state.in_offset)
307
144
    {
308
144
      as_bad (_("invalid use of register"));
309
144
      return 0;
310
144
    }
311
312
496
  if (e->X_op == O_register)
313
496
    reg_num = e->X_add_number;
314
0
  else
315
0
    reg_num = e->X_md - 1;
316
317
496
  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
496
  if (!check_register (&i386_regtab[reg_num]))
324
9
    {
325
9
      as_bad (_("register '%s%s' cannot be used here"),
326
9
        register_prefix, i386_regtab[reg_num].reg_name);
327
9
      return 0;
328
9
    }
329
330
487
  if (!intel_state.in_bracket)
331
485
    {
332
485
      if (i.op[this_operand].regs)
333
0
  {
334
0
    as_bad (_("invalid use of register"));
335
0
    return 0;
336
0
  }
337
485
      if ((i386_regtab[reg_num].reg_type.bitfield.class == SReg
338
221
     && i386_regtab[reg_num].reg_num == RegFlat)
339
485
    || (dot_insn ()
340
6
        && 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
485
      i.op[this_operand].regs = i386_regtab + reg_num;
346
485
    }
347
2
  else if (!intel_state.index
348
2
     && (i386_regtab[reg_num].reg_type.bitfield.xmmword
349
2
         || i386_regtab[reg_num].reg_type.bitfield.ymmword
350
2
         || i386_regtab[reg_num].reg_type.bitfield.zmmword
351
2
         || i386_regtab[reg_num].reg_num == RegIZ))
352
0
    intel_state.index = i386_regtab + reg_num;
353
2
  else if (!intel_state.base && !intel_state.in_scale)
354
1
    intel_state.base = i386_regtab + reg_num;
355
1
  else if (!intel_state.index)
356
1
    {
357
1
      const insn_template *t = current_templates.start;
358
359
1
      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
1
  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
1
    }
373
0
  else
374
0
    {
375
      /* esp is invalid as index */
376
0
      intel_state.index = reg_eax + ESP_REG_NUM;
377
0
    }
378
487
  return 2;
379
487
}
380
381
static int
382
i386_intel_simplify_symbol (symbolS *sym)
383
10.2k
{
384
10.2k
  if (symbol_resolving_p (sym))
385
3
    return 1;
386
387
10.2k
  symbol_mark_resolving (sym);
388
10.2k
  int ret = i386_intel_simplify (symbol_get_value_expression (sym));
389
10.2k
  if (ret == 2)
390
205
    {
391
205
      S_SET_SEGMENT (sym, absolute_section);
392
205
      ret = 1;
393
205
    }
394
10.2k
  symbol_clear_resolving (sym);
395
10.2k
  return ret;
396
10.2k
}
397
398
static int
399
i386_intel_simplify (expressionS *e)
400
21.3k
{
401
21.3k
  const reg_entry *the_reg = (this_operand >= 0
402
21.3k
            ? i.op[this_operand].regs : NULL);
403
21.3k
  const reg_entry *base = intel_state.base;
404
21.3k
  const reg_entry *state_index = intel_state.index;
405
21.3k
  int ret;
406
407
21.3k
  if (!intel_syntax)
408
0
    return 1;
409
410
21.3k
  switch (e->X_op)
411
21.3k
    {
412
79
    case O_index:
413
79
      if (e->X_add_symbol)
414
17
  {
415
17
    if (!i386_intel_simplify_symbol (e->X_add_symbol)
416
17
        || !i386_intel_check(the_reg, intel_state.base,
417
17
           intel_state.index))
418
3
      return 0;
419
17
  }
420
76
      if (!intel_state.in_offset)
421
76
  ++intel_state.in_bracket;
422
76
      ret = i386_intel_simplify_symbol (e->X_op_symbol);
423
76
      if (!intel_state.in_offset)
424
76
  --intel_state.in_bracket;
425
76
      if (!ret)
426
0
  return 0;
427
76
      if (e->X_add_symbol)
428
14
  e->X_op = O_add;
429
62
      else
430
62
  i386_intel_fold (e, e->X_op_symbol);
431
76
      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
160
    case O_full_ptr:
470
160
      if (symbol_get_value_expression (e->X_op_symbol)->X_op
471
160
    == O_register)
472
27
  {
473
27
    as_bad (_("invalid use of register"));
474
27
    return 0;
475
27
  }
476
133
      if (!i386_intel_simplify_symbol (e->X_op_symbol)
477
133
    || !i386_intel_check(the_reg, intel_state.base,
478
133
             intel_state.index))
479
0
  return 0;
480
133
      if (!intel_state.in_offset)
481
133
  {
482
133
    if (!intel_state.seg)
483
100
      intel_state.seg = e->X_add_symbol;
484
33
    else
485
33
      {
486
33
        expressionS exp = {
487
33
    .X_op = O_full_ptr,
488
33
    .X_add_symbol = e->X_add_symbol,
489
33
    .X_op_symbol = intel_state.seg
490
33
        };
491
33
        intel_state.seg = make_expr_symbol (&exp);
492
33
      }
493
133
  }
494
133
      i386_intel_fold (e, e->X_op_symbol);
495
133
      break;
496
497
995
    case O_multiply:
498
995
      if (this_operand >= 0 && intel_state.in_bracket)
499
1
  {
500
1
    expressionS *scale = NULL;
501
1
    int has_index = (intel_state.index != NULL);
502
503
1
    if (!intel_state.in_scale++)
504
1
      intel_state.scale_factor = 1;
505
506
1
    ret = i386_intel_simplify_symbol (e->X_add_symbol);
507
1
    if (ret && !has_index && intel_state.index)
508
1
      scale = symbol_get_value_expression (e->X_op_symbol);
509
510
1
    if (ret)
511
1
      ret = i386_intel_simplify_symbol (e->X_op_symbol);
512
1
    if (ret && !scale && !has_index && intel_state.index)
513
0
      scale = symbol_get_value_expression (e->X_add_symbol);
514
515
1
    if (ret && scale)
516
1
      {
517
1
        resolve_expression (scale);
518
1
        if (scale->X_op != O_constant
519
1
      || intel_state.index->reg_type.bitfield.word)
520
0
    scale->X_add_number = 0;
521
1
        intel_state.scale_factor *= scale->X_add_number;
522
1
      }
523
524
1
    --intel_state.in_scale;
525
1
    if (!ret)
526
0
      return 0;
527
528
1
    if (!intel_state.in_scale)
529
1
      switch (intel_state.scale_factor)
530
1
        {
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
1
        default:
544
    /* esp is invalid as index */
545
1
    intel_state.index = reg_eax + ESP_REG_NUM;
546
1
    break;
547
1
        }
548
549
1
    break;
550
1
  }
551
994
      goto fallthrough;
552
553
994
    case O_register:
554
640
      ret = i386_intel_simplify_register (e);
555
640
      if (ret == 2)
556
487
  {
557
487
    gas_assert (e->X_add_number < (unsigned short) -1);
558
487
    e->X_md = (unsigned short) e->X_add_number + 1;
559
487
    e->X_op = O_constant;
560
487
    e->X_add_number = 0;
561
487
  }
562
640
      return ret;
563
564
11.9k
    case O_constant:
565
11.9k
      if (e->X_md)
566
0
  return i386_intel_simplify_register (e);
567
568
      /* FALLTHROUGH */
569
19.4k
    default:
570
20.4k
    fallthrough:
571
20.4k
      if (e->X_add_symbol
572
8.41k
    && !i386_intel_simplify_symbol (e->X_add_symbol))
573
3
  return 0;
574
20.4k
      if (!the_reg && this_operand >= 0
575
18.1k
    && e->X_op == O_symbol && !e->X_add_number)
576
3.44k
  the_reg = i.op[this_operand].regs;
577
20.4k
      if (e->X_op == O_add || e->X_op == O_subtract)
578
398
  {
579
398
    base = intel_state.base;
580
398
    state_index = intel_state.index;
581
398
  }
582
20.4k
      if (!i386_intel_check (the_reg, base, state_index)
583
20.2k
    || (e->X_op_symbol
584
1.57k
        && !i386_intel_simplify_symbol (e->X_op_symbol))
585
20.0k
    || !i386_intel_check (the_reg,
586
20.0k
        (e->X_op != O_add
587
20.0k
         ? base : intel_state.base),
588
20.0k
        (e->X_op != O_add
589
20.0k
         ? state_index : intel_state.index)))
590
346
  return 0;
591
20.0k
      break;
592
21.3k
    }
593
594
20.3k
  if (this_operand >= 0
595
18.1k
      && e->X_op == O_symbol
596
5.93k
      && !intel_state.in_offset)
597
5.93k
    {
598
5.93k
      segT seg = S_GET_SEGMENT (e->X_add_symbol);
599
600
5.93k
      if (seg != absolute_section
601
5.93k
    && seg != reg_section
602
5.93k
    && seg != expr_section)
603
5.93k
  intel_state.is_mem |= 2 - !intel_state.in_bracket;
604
5.93k
    }
605
606
20.3k
  return 1;
607
21.3k
}
608
609
int i386_need_index_operator (void)
610
618
{
611
618
  return intel_syntax < 0;
612
618
}
613
614
static int
615
i386_intel_operand (char *operand_string, int got_a_float)
616
10.1k
{
617
10.1k
  char *saved_input_line_pointer, *buf;
618
10.1k
  segT exp_seg;
619
10.1k
  expressionS exp, *expP;
620
10.1k
  char suffix = 0;
621
10.1k
  bool rc_sae_modifier = i.rounding.type != rc_none && i.rounding.modifier;
622
10.1k
  int ret;
623
624
  /* Handle vector immediates.  */
625
10.1k
  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
10.1k
  intel_state.op_modifier = O_absent;
639
10.1k
  intel_state.is_mem = 0;
640
10.1k
  intel_state.is_indirect = 0;
641
10.1k
  intel_state.has_offset = 0;
642
10.1k
  intel_state.base = NULL;
643
10.1k
  intel_state.index = NULL;
644
10.1k
  intel_state.seg = NULL;
645
10.1k
  operand_type_set (&intel_state.reloc_types, ~0);
646
10.1k
  gas_assert (!intel_state.in_offset);
647
10.1k
  gas_assert (!intel_state.in_bracket);
648
10.1k
  gas_assert (!intel_state.in_scale);
649
650
10.1k
  saved_input_line_pointer = input_line_pointer;
651
10.1k
  input_line_pointer = buf = xstrdup (operand_string);
652
653
10.1k
  intel_syntax = -1;
654
10.1k
  expr_mode = expr_operator_none;
655
10.1k
  memset (&exp, 0, sizeof(exp));
656
10.1k
  exp_seg = expression (&exp);
657
10.1k
  ret = i386_intel_simplify (&exp);
658
10.1k
  intel_syntax = 1;
659
660
10.1k
  SKIP_WHITESPACE ();
661
662
  /* Handle vector operations.  */
663
10.1k
  if (*input_line_pointer == '{')
664
538
    {
665
538
      char *end = check_VecOperations (input_line_pointer);
666
538
      if (end)
667
0
  input_line_pointer = end;
668
538
      else
669
538
  ret = 0;
670
538
    }
671
672
10.1k
  if (!is_end_of_stmt (*input_line_pointer))
673
5.09k
    {
674
5.09k
      if (ret)
675
4.32k
  as_bad (_("junk `%s' after expression"), input_line_pointer);
676
5.09k
      ret = 0;
677
5.09k
    }
678
5.03k
  else if (exp.X_op == O_illegal || exp.X_op == O_absent)
679
14
    {
680
14
      if (ret)
681
14
  as_bad (_("invalid expression"));
682
14
      ret = 0;
683
14
    }
684
5.02k
  else if (!intel_state.has_offset
685
5.02k
     && input_line_pointer > buf
686
5.02k
     && *(input_line_pointer - 1) == ']')
687
0
    {
688
0
      intel_state.is_mem |= 1;
689
0
      intel_state.is_indirect = 1;
690
0
    }
691
692
10.1k
  input_line_pointer = saved_input_line_pointer;
693
10.1k
  free (buf);
694
695
10.1k
  gas_assert (!intel_state.in_offset);
696
10.1k
  gas_assert (!intel_state.in_bracket);
697
10.1k
  gas_assert (!intel_state.in_scale);
698
699
10.1k
  if (!ret)
700
5.11k
    return 0;
701
702
5.01k
  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
5.01k
  if (current_templates.start->opcode_modifier.jump
911
4.77k
      || current_templates.start->mnem_off == MN_jmpabs)
912
235
    {
913
235
      bool jumpabsolute = false;
914
915
235
      if (i.op[this_operand].regs
916
235
    || intel_state.base
917
234
    || intel_state.index
918
233
    || intel_state.is_mem > 1)
919
2
  jumpabsolute = true;
920
233
      else
921
233
  switch (intel_state.op_modifier)
922
233
    {
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
233
    case O_absent:
931
233
      if (!intel_state.seg)
932
233
        {
933
233
    intel_state.is_mem = 1;
934
233
    if (intel_state.op_modifier == O_absent)
935
233
      {
936
233
        if (intel_state.is_indirect == 1)
937
0
          jumpabsolute = true;
938
233
        break;
939
233
      }
940
0
    as_bad (_("cannot infer the segment part of the operand"));
941
0
    return 0;
942
233
        }
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
233
    }
983
235
      if (jumpabsolute)
984
2
  {
985
2
    i.jumpabsolute = true;
986
2
    intel_state.is_mem |= 1;
987
2
  }
988
235
    }
989
4.77k
  else if (intel_state.seg)
990
98
    intel_state.is_mem |= 1;
991
992
5.01k
  if (i.op[this_operand].regs)
993
244
    {
994
244
      i386_operand_type temp;
995
996
      /* Register operand.  */
997
244
      if (intel_state.base || intel_state.index || intel_state.seg
998
244
          || i.imm_bits[this_operand])
999
0
  {
1000
0
    as_bad (_("invalid operand"));
1001
0
    return 0;
1002
0
  }
1003
1004
244
      temp = i.op[this_operand].regs->reg_type;
1005
244
      temp.bitfield.baseindex = 0;
1006
244
      i.types[this_operand] = operand_type_or (i.types[this_operand],
1007
244
                 temp);
1008
244
      i.types[this_operand].bitfield.unspecified = 0;
1009
244
      ++i.reg_operands;
1010
1011
244
      if ((i.rounding.type != rc_none && !i.rounding.modifier
1012
0
     && temp.bitfield.class != Reg)
1013
244
    || 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
244
    }
1025
4.76k
  else if (intel_state.base
1026
4.76k
     || intel_state.index
1027
4.76k
     || intel_state.seg
1028
4.66k
     || intel_state.is_mem)
1029
3.91k
    {
1030
      /* Memory operand.  */
1031
3.91k
      if (i.imm_bits[this_operand])
1032
0
  {
1033
0
    as_bad (_("invalid operand"));
1034
0
    return 0;
1035
0
  }
1036
1037
3.91k
      if (i.mem_operands)
1038
20
  {
1039
    /* Handle
1040
1041
       call 0x9090,0x90909090
1042
       lcall  0x9090,0x90909090
1043
       jmp  0x9090,0x90909090
1044
       ljmp 0x9090,0x90909090
1045
     */
1046
1047
20
    if (current_templates.start->opcode_modifier.jump
1048
13
        && this_operand == 1
1049
13
        && intel_state.seg == NULL
1050
13
        && i.mem_operands == 1
1051
13
        && i.disp_operands == 1
1052
13
        && intel_state.op_modifier == O_absent)
1053
13
      {
1054
        /* Try to process the first operand as immediate,  */
1055
13
        this_operand = 0;
1056
13
        if (i386_finalize_immediate (exp_seg, i.op[0].imms,
1057
13
             intel_state.reloc_types,
1058
13
             NULL))
1059
13
    {
1060
13
      this_operand = 1;
1061
13
      expP = &im_expressions[0];
1062
13
      i.op[this_operand].imms = expP;
1063
13
      *expP = exp;
1064
1065
      /* Try to process the second operand as immediate,  */
1066
13
      if (i386_finalize_immediate (exp_seg, expP,
1067
13
                 intel_state.reloc_types,
1068
13
                 NULL))
1069
13
        {
1070
13
          i.mem_operands = 0;
1071
13
          i.disp_operands = 0;
1072
13
          i.imm_operands = 2;
1073
13
          i.flags[0] &= ~Operand_Mem;
1074
13
          i.types[0].bitfield.disp16 = 0;
1075
13
          i.types[0].bitfield.disp32 = 0;
1076
13
          return 1;
1077
13
        }
1078
13
    }
1079
13
      }
1080
20
  }
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
3.89k
      if (intel_state.base
1086
1
    && 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
3.89k
      else
1096
3.89k
  {
1097
3.89k
    i.base_reg = intel_state.base;
1098
3.89k
    i.index_reg = intel_state.index;
1099
3.89k
  }
1100
1101
3.89k
      if (i.base_reg || i.index_reg)
1102
2
  i.types[this_operand].bitfield.baseindex = 1;
1103
1104
3.89k
      expP = &disp_expressions[i.disp_operands];
1105
3.89k
      memcpy (expP, &exp, sizeof(exp));
1106
3.89k
      resolve_expression (expP);
1107
1108
3.89k
      if (expP->X_op != O_constant
1109
88
    || expP->X_add_number
1110
71
    || !i.types[this_operand].bitfield.baseindex)
1111
3.89k
  {
1112
3.89k
    i.op[this_operand].disps = expP;
1113
3.89k
    i.disp_operands++;
1114
1115
3.89k
    i386_addressing_mode ();
1116
1117
3.89k
    if (flag_code == CODE_64BIT)
1118
1.59k
      {
1119
1.59k
        i.types[this_operand].bitfield.disp32 = 1;
1120
1.59k
        if (!i.prefix[ADDR_PREFIX])
1121
1.59k
    i.types[this_operand].bitfield.disp64 = 1;
1122
1.59k
      }
1123
2.30k
    else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT))
1124
1.37k
      i.types[this_operand].bitfield.disp32 = 1;
1125
923
    else
1126
923
      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
3.89k
    if (!i386_finalize_displacement (exp_seg, expP,
1148
3.89k
             intel_state.reloc_types,
1149
3.89k
             operand_string))
1150
0
      return 0;
1151
3.89k
  }
1152
1153
3.89k
      if (intel_state.seg)
1154
98
  {
1155
98
    for (ret = check_none; ; ret = operand_check)
1156
98
      {
1157
98
        expP = symbol_get_value_expression (intel_state.seg);
1158
98
        if (expP->X_op != O_full_ptr 
1159
33
      || symbol_get_value_expression (expP->X_op_symbol)->X_op
1160
33
         != O_register)
1161
98
    break;
1162
0
        intel_state.seg = expP->X_add_symbol;
1163
0
      }
1164
98
    if (expP->X_op != O_register)
1165
40
      {
1166
40
        as_bad (_("segment register name expected"));
1167
40
        return 0;
1168
40
      }
1169
58
    if (i386_regtab[expP->X_add_number].reg_type.bitfield.class != SReg)
1170
55
      {
1171
55
        as_bad (_("invalid use of register"));
1172
55
        return 0;
1173
55
      }
1174
3
    switch (ret)
1175
3
      {
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
3
      }
1183
3
    if (i386_regtab[expP->X_add_number].reg_num == RegFlat)
1184
0
      i.seg[i.mem_operands] = NULL;
1185
3
    else
1186
3
      i.seg[i.mem_operands] = &i386_regtab[expP->X_add_number];
1187
3
  }
1188
1189
3.80k
      if (!i386_index_check (operand_string))
1190
2
  return 0;
1191
1192
3.80k
      i.flags[this_operand] |= Operand_Mem;
1193
3.80k
      ++i.mem_operands;
1194
3.80k
    }
1195
858
  else
1196
858
    {
1197
      /* Immediate.  */
1198
858
      if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS)
1199
1
  {
1200
1
    as_bad (_("at most %d immediate operands are allowed"),
1201
1
      MAX_IMMEDIATE_OPERANDS);
1202
1
    return 0;
1203
1
  }
1204
1205
857
      expP = &im_expressions[i.imm_operands++];
1206
857
      i.op[this_operand].imms = expP;
1207
857
      *expP = exp;
1208
1209
857
      return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types,
1210
857
              operand_string);
1211
858
    }
1212
1213
4.04k
  return 1;
1214
5.01k
}