Coverage Report

Created: 2023-06-29 07:13

/src/binutils-gdb/opcodes/bpf-asm.c
Line
Count
Source (jump to first uncovered line)
1
/* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */
2
/* Assembler interface for targets using CGEN. -*- C -*-
3
   CGEN: Cpu tools GENerator
4
5
   THIS FILE IS MACHINE GENERATED WITH CGEN.
6
   - the resultant file is machine generated, cgen-asm.in isn't
7
8
   Copyright (C) 1996-2023 Free Software Foundation, Inc.
9
10
   This file is part of libopcodes.
11
12
   This library is free software; you can redistribute it and/or modify
13
   it under the terms of the GNU General Public License as published by
14
   the Free Software Foundation; either version 3, or (at your option)
15
   any later version.
16
17
   It is distributed in the hope that it will be useful, but WITHOUT
18
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
20
   License for more details.
21
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, write to the Free Software Foundation, Inc.,
24
   51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
25
26
27
/* ??? Eventually more and more of this stuff can go to cpu-independent files.
28
   Keep that in mind.  */
29
30
#include "sysdep.h"
31
#include <stdio.h>
32
#include "ansidecl.h"
33
#include "bfd.h"
34
#include "symcat.h"
35
#include "bpf-desc.h"
36
#include "bpf-opc.h"
37
#include "opintl.h"
38
#include "xregex.h"
39
#include "libiberty.h"
40
#include "safe-ctype.h"
41
42
#undef  min
43
#define min(a,b) ((a) < (b) ? (a) : (b))
44
#undef  max
45
#define max(a,b) ((a) > (b) ? (a) : (b))
46
47
static const char * parse_insn_normal
48
  (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
49

50
/* -- assembler routines inserted here.  */
51
52
/* -- asm.c */
53
54
/* Parse a signed 64-bit immediate.  */
55
56
static const char *
57
parse_imm64 (CGEN_CPU_DESC cd,
58
             const char **strp,
59
             int opindex,
60
             int64_t *valuep)
61
0
{
62
0
  bfd_vma value;
63
0
  enum cgen_parse_operand_result result;
64
0
  const char *errmsg;
65
66
0
  errmsg = (* cd->parse_operand_fn)
67
0
    (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
68
0
     &result, &value);
69
0
  if (!errmsg)
70
0
    *valuep = value;
71
72
0
  return errmsg;
73
0
}
74
75
/* Endianness size operands are integer immediates whose values can be
76
   16, 32 or 64.  */
77
78
static const char *
79
parse_endsize (CGEN_CPU_DESC cd,
80
               const char **strp,
81
               int opindex,
82
               unsigned long *valuep)
83
0
{
84
0
  const char *errmsg;
85
86
0
  errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
87
0
  if (errmsg)
88
0
    return errmsg;
89
90
0
  switch (*valuep)
91
0
    {
92
0
    case 16:
93
0
    case 32:
94
0
    case 64:
95
0
      break;
96
0
    default:
97
0
      return _("expected 16, 32 or 64 in");
98
0
    }
99
100
0
  return NULL;
101
0
}
102
103
/* Special check to ensure that the right instruction variant is used
104
   for the given endianness induced by the ISA selected in the CPU.
105
   See bpf.cpu for a discussion on how eBPF is really two instruction
106
   sets.  */
107
108
int
109
bpf_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
110
9.84M
{
111
9.84M
  CGEN_BITSET isas = CGEN_INSN_BITSET_ATTR_VALUE (insn, CGEN_INSN_ISA);
112
113
9.84M
  return cgen_bitset_intersect_p (&isas, cd->isas);
114
9.84M
}
115
116

117
/* -- dis.c */
118
119
const char * bpf_cgen_parse_operand
120
  (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
121
122
/* Main entry point for operand parsing.
123
124
   This function is basically just a big switch statement.  Earlier versions
125
   used tables to look up the function to use, but
126
   - if the table contains both assembler and disassembler functions then
127
     the disassembler contains much of the assembler and vice-versa,
128
   - there's a lot of inlining possibilities as things grow,
129
   - using a switch statement avoids the function call overhead.
130
131
   This function could be moved into `parse_insn_normal', but keeping it
132
   separate makes clear the interface between `parse_insn_normal' and each of
133
   the handlers.  */
134
135
const char *
136
bpf_cgen_parse_operand (CGEN_CPU_DESC cd,
137
         int opindex,
138
         const char ** strp,
139
         CGEN_FIELDS * fields)
140
0
{
141
0
  const char * errmsg = NULL;
142
  /* Used by scalar operands that still need to be parsed.  */
143
0
  long junk ATTRIBUTE_UNUSED;
144
145
0
  switch (opindex)
146
0
    {
147
0
    case BPF_OPERAND_DISP16 :
148
0
      errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_DISP16, (long *) (& fields->f_offset16));
149
0
      break;
150
0
    case BPF_OPERAND_DISP32 :
151
0
      errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_DISP32, (long *) (& fields->f_imm32));
152
0
      break;
153
0
    case BPF_OPERAND_DSTBE :
154
0
      errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_dstbe);
155
0
      break;
156
0
    case BPF_OPERAND_DSTLE :
157
0
      errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_dstle);
158
0
      break;
159
0
    case BPF_OPERAND_ENDSIZE :
160
0
      errmsg = parse_endsize (cd, strp, BPF_OPERAND_ENDSIZE, (unsigned long *) (& fields->f_imm32));
161
0
      break;
162
0
    case BPF_OPERAND_IMM32 :
163
0
      errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_IMM32, (long *) (& fields->f_imm32));
164
0
      break;
165
0
    case BPF_OPERAND_IMM64 :
166
0
      errmsg = parse_imm64 (cd, strp, BPF_OPERAND_IMM64, (int64_t *) (& fields->f_imm64));
167
0
      break;
168
0
    case BPF_OPERAND_OFFSET16 :
169
0
      errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_OFFSET16, (long *) (& fields->f_offset16));
170
0
      break;
171
0
    case BPF_OPERAND_SRCBE :
172
0
      errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_srcbe);
173
0
      break;
174
0
    case BPF_OPERAND_SRCLE :
175
0
      errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_srcle);
176
0
      break;
177
178
0
    default :
179
      /* xgettext:c-format */
180
0
      opcodes_error_handler
181
0
  (_("internal error: unrecognized field %d while parsing"),
182
0
   opindex);
183
0
      abort ();
184
0
  }
185
186
0
  return errmsg;
187
0
}
188
189
cgen_parse_fn * const bpf_cgen_parse_handlers[] =
190
{
191
  parse_insn_normal,
192
};
193
194
void
195
bpf_cgen_init_asm (CGEN_CPU_DESC cd)
196
0
{
197
0
  bpf_cgen_init_opcode_table (cd);
198
0
  bpf_cgen_init_ibld_table (cd);
199
0
  cd->parse_handlers = & bpf_cgen_parse_handlers[0];
200
0
  cd->parse_operand = bpf_cgen_parse_operand;
201
#ifdef CGEN_ASM_INIT_HOOK
202
CGEN_ASM_INIT_HOOK
203
#endif
204
0
}
205
206

207
208
/* Regex construction routine.
209
210
   This translates an opcode syntax string into a regex string,
211
   by replacing any non-character syntax element (such as an
212
   opcode) with the pattern '.*'
213
214
   It then compiles the regex and stores it in the opcode, for
215
   later use by bpf_cgen_assemble_insn
216
217
   Returns NULL for success, an error message for failure.  */
218
219
char *
220
bpf_cgen_build_insn_regex (CGEN_INSN *insn)
221
1.03k
{
222
1.03k
  CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
223
1.03k
  const char *mnem = CGEN_INSN_MNEMONIC (insn);
224
1.03k
  char rxbuf[CGEN_MAX_RX_ELEMENTS];
225
1.03k
  char *rx = rxbuf;
226
1.03k
  const CGEN_SYNTAX_CHAR_TYPE *syn;
227
1.03k
  int reg_err;
228
229
1.03k
  syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
230
231
  /* Mnemonics come first in the syntax string.  */
232
1.03k
  if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
233
4
    return _("missing mnemonic in syntax string");
234
1.02k
  ++syn;
235
236
  /* Generate a case sensitive regular expression that emulates case
237
     insensitive matching in the "C" locale.  We cannot generate a case
238
     insensitive regular expression because in Turkish locales, 'i' and 'I'
239
     are not equal modulo case conversion.  */
240
241
  /* Copy the literal mnemonic out of the insn.  */
242
5.50k
  for (; *mnem; mnem++)
243
4.47k
    {
244
4.47k
      char c = *mnem;
245
246
4.47k
      if (ISALPHA (c))
247
3.65k
  {
248
3.65k
    *rx++ = '[';
249
3.65k
    *rx++ = TOLOWER (c);
250
3.65k
    *rx++ = TOUPPER (c);
251
3.65k
    *rx++ = ']';
252
3.65k
  }
253
816
      else
254
816
  *rx++ = c;
255
4.47k
    }
256
257
  /* Copy any remaining literals from the syntax string into the rx.  */
258
6.15k
  for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
259
5.12k
    {
260
5.12k
      if (CGEN_SYNTAX_CHAR_P (* syn))
261
2.67k
  {
262
2.67k
    char c = CGEN_SYNTAX_CHAR (* syn);
263
264
2.67k
    switch (c)
265
2.67k
      {
266
        /* Escape any regex metacharacters in the syntax.  */
267
112
      case '.': case '[': case '\\':
268
112
      case '*': case '^': case '$':
269
270
#ifdef CGEN_ESCAPE_EXTENDED_REGEX
271
      case '?': case '{': case '}':
272
      case '(': case ')': case '*':
273
      case '|': case '+': case ']':
274
#endif
275
112
        *rx++ = '\\';
276
112
        *rx++ = c;
277
112
        break;
278
279
2.56k
      default:
280
2.56k
        if (ISALPHA (c))
281
0
    {
282
0
      *rx++ = '[';
283
0
      *rx++ = TOLOWER (c);
284
0
      *rx++ = TOUPPER (c);
285
0
      *rx++ = ']';
286
0
    }
287
2.56k
        else
288
2.56k
    *rx++ = c;
289
2.56k
        break;
290
2.67k
      }
291
2.67k
  }
292
2.45k
      else
293
2.45k
  {
294
    /* Replace non-syntax fields with globs.  */
295
2.45k
    *rx++ = '.';
296
2.45k
    *rx++ = '*';
297
2.45k
  }
298
5.12k
    }
299
300
  /* Trailing whitespace ok.  */
301
1.02k
  * rx++ = '[';
302
1.02k
  * rx++ = ' ';
303
1.02k
  * rx++ = '\t';
304
1.02k
  * rx++ = ']';
305
1.02k
  * rx++ = '*';
306
307
  /* But anchor it after that.  */
308
1.02k
  * rx++ = '$';
309
1.02k
  * rx = '\0';
310
311
1.02k
  CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
312
1.02k
  reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
313
314
1.02k
  if (reg_err == 0)
315
1.02k
    return NULL;
316
0
  else
317
0
    {
318
0
      static char msg[80];
319
320
0
      regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
321
0
      regfree ((regex_t *) CGEN_INSN_RX (insn));
322
0
      free (CGEN_INSN_RX (insn));
323
0
      (CGEN_INSN_RX (insn)) = NULL;
324
0
      return msg;
325
0
    }
326
1.02k
}
327
328

329
/* Default insn parser.
330
331
   The syntax string is scanned and operands are parsed and stored in FIELDS.
332
   Relocs are queued as we go via other callbacks.
333
334
   ??? Note that this is currently an all-or-nothing parser.  If we fail to
335
   parse the instruction, we return 0 and the caller will start over from
336
   the beginning.  Backtracking will be necessary in parsing subexpressions,
337
   but that can be handled there.  Not handling backtracking here may get
338
   expensive in the case of the m68k.  Deal with later.
339
340
   Returns NULL for success, an error message for failure.  */
341
342
static const char *
343
parse_insn_normal (CGEN_CPU_DESC cd,
344
       const CGEN_INSN *insn,
345
       const char **strp,
346
       CGEN_FIELDS *fields)
347
0
{
348
  /* ??? Runtime added insns not handled yet.  */
349
0
  const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
350
0
  const char *str = *strp;
351
0
  const char *errmsg;
352
0
  const char *p;
353
0
  const CGEN_SYNTAX_CHAR_TYPE * syn;
354
0
#ifdef CGEN_MNEMONIC_OPERANDS
355
  /* FIXME: wip */
356
0
  int past_opcode_p;
357
0
#endif
358
359
  /* For now we assume the mnemonic is first (there are no leading operands).
360
     We can parse it without needing to set up operand parsing.
361
     GAS's input scrubber will ensure mnemonics are lowercase, but we may
362
     not be called from GAS.  */
363
0
  p = CGEN_INSN_MNEMONIC (insn);
364
0
  while (*p && TOLOWER (*p) == TOLOWER (*str))
365
0
    ++p, ++str;
366
367
0
  if (* p)
368
0
    return _("unrecognized instruction");
369
370
#ifndef CGEN_MNEMONIC_OPERANDS
371
  if (* str && ! ISSPACE (* str))
372
    return _("unrecognized instruction");
373
#endif
374
375
0
  CGEN_INIT_PARSE (cd);
376
0
  cgen_init_parse_operand (cd);
377
0
#ifdef CGEN_MNEMONIC_OPERANDS
378
0
  past_opcode_p = 0;
379
0
#endif
380
381
  /* We don't check for (*str != '\0') here because we want to parse
382
     any trailing fake arguments in the syntax string.  */
383
0
  syn = CGEN_SYNTAX_STRING (syntax);
384
385
  /* Mnemonics come first for now, ensure valid string.  */
386
0
  if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
387
0
    abort ();
388
389
0
  ++syn;
390
391
0
  while (* syn != 0)
392
0
    {
393
      /* Non operand chars must match exactly.  */
394
0
      if (CGEN_SYNTAX_CHAR_P (* syn))
395
0
  {
396
    /* FIXME: While we allow for non-GAS callers above, we assume the
397
       first char after the mnemonic part is a space.  */
398
    /* FIXME: We also take inappropriate advantage of the fact that
399
       GAS's input scrubber will remove extraneous blanks.  */
400
0
    if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
401
0
      {
402
0
#ifdef CGEN_MNEMONIC_OPERANDS
403
0
        if (CGEN_SYNTAX_CHAR(* syn) == ' ')
404
0
    past_opcode_p = 1;
405
0
#endif
406
0
        ++ syn;
407
0
        ++ str;
408
0
      }
409
0
    else if (*str)
410
0
      {
411
        /* Syntax char didn't match.  Can't be this insn.  */
412
0
        static char msg [80];
413
414
        /* xgettext:c-format */
415
0
        sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
416
0
           CGEN_SYNTAX_CHAR(*syn), *str);
417
0
        return msg;
418
0
      }
419
0
    else
420
0
      {
421
        /* Ran out of input.  */
422
0
        static char msg [80];
423
424
        /* xgettext:c-format */
425
0
        sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
426
0
           CGEN_SYNTAX_CHAR(*syn));
427
0
        return msg;
428
0
      }
429
0
    continue;
430
0
  }
431
432
0
#ifdef CGEN_MNEMONIC_OPERANDS
433
0
      (void) past_opcode_p;
434
0
#endif
435
      /* We have an operand of some sort.  */
436
0
      errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
437
0
      if (errmsg)
438
0
  return errmsg;
439
440
      /* Done with this operand, continue with next one.  */
441
0
      ++ syn;
442
0
    }
443
444
  /* If we're at the end of the syntax string, we're done.  */
445
0
  if (* syn == 0)
446
0
    {
447
      /* FIXME: For the moment we assume a valid `str' can only contain
448
   blanks now.  IE: We needn't try again with a longer version of
449
   the insn and it is assumed that longer versions of insns appear
450
   before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
451
0
      while (ISSPACE (* str))
452
0
  ++ str;
453
454
0
      if (* str != '\0')
455
0
  return _("junk at end of line"); /* FIXME: would like to include `str' */
456
457
0
      return NULL;
458
0
    }
459
460
  /* We couldn't parse it.  */
461
0
  return _("unrecognized instruction");
462
0
}
463

464
/* Main entry point.
465
   This routine is called for each instruction to be assembled.
466
   STR points to the insn to be assembled.
467
   We assume all necessary tables have been initialized.
468
   The assembled instruction, less any fixups, is stored in BUF.
469
   Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
470
   still needs to be converted to target byte order, otherwise BUF is an array
471
   of bytes in target byte order.
472
   The result is a pointer to the insn's entry in the opcode table,
473
   or NULL if an error occured (an error message will have already been
474
   printed).
475
476
   Note that when processing (non-alias) macro-insns,
477
   this function recurses.
478
479
   ??? It's possible to make this cpu-independent.
480
   One would have to deal with a few minor things.
481
   At this point in time doing so would be more of a curiosity than useful
482
   [for example this file isn't _that_ big], but keeping the possibility in
483
   mind helps keep the design clean.  */
484
485
const CGEN_INSN *
486
bpf_cgen_assemble_insn (CGEN_CPU_DESC cd,
487
         const char *str,
488
         CGEN_FIELDS *fields,
489
         CGEN_INSN_BYTES_PTR buf,
490
         char **errmsg)
491
0
{
492
0
  const char *start;
493
0
  CGEN_INSN_LIST *ilist;
494
0
  const char *parse_errmsg = NULL;
495
0
  const char *insert_errmsg = NULL;
496
0
  int recognized_mnemonic = 0;
497
498
  /* Skip leading white space.  */
499
0
  while (ISSPACE (* str))
500
0
    ++ str;
501
502
  /* The instructions are stored in hashed lists.
503
     Get the first in the list.  */
504
0
  ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
505
506
  /* Keep looking until we find a match.  */
507
0
  start = str;
508
0
  for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
509
0
    {
510
0
      const CGEN_INSN *insn = ilist->insn;
511
0
      recognized_mnemonic = 1;
512
513
0
#ifdef CGEN_VALIDATE_INSN_SUPPORTED
514
      /* Not usually needed as unsupported opcodes
515
   shouldn't be in the hash lists.  */
516
      /* Is this insn supported by the selected cpu?  */
517
0
      if (! bpf_cgen_insn_supported (cd, insn))
518
0
  continue;
519
0
#endif
520
      /* If the RELAXED attribute is set, this is an insn that shouldn't be
521
   chosen immediately.  Instead, it is used during assembler/linker
522
   relaxation if possible.  */
523
0
      if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
524
0
  continue;
525
526
0
      str = start;
527
528
      /* Skip this insn if str doesn't look right lexically.  */
529
0
      if (CGEN_INSN_RX (insn) != NULL &&
530
0
    regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
531
0
  continue;
532
533
      /* Allow parse/insert handlers to obtain length of insn.  */
534
0
      CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
535
536
0
      parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
537
0
      if (parse_errmsg != NULL)
538
0
  continue;
539
540
      /* ??? 0 is passed for `pc'.  */
541
0
      insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
542
0
             (bfd_vma) 0);
543
0
      if (insert_errmsg != NULL)
544
0
        continue;
545
546
      /* It is up to the caller to actually output the insn and any
547
         queued relocs.  */
548
0
      return insn;
549
0
    }
550
551
0
  {
552
0
    static char errbuf[150];
553
0
    const char *tmp_errmsg;
554
0
#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
555
0
#define be_verbose 1
556
#else
557
#define be_verbose 0
558
#endif
559
560
0
    if (be_verbose)
561
0
      {
562
  /* If requesting verbose error messages, use insert_errmsg.
563
     Failing that, use parse_errmsg.  */
564
0
  tmp_errmsg = (insert_errmsg ? insert_errmsg :
565
0
          parse_errmsg ? parse_errmsg :
566
0
          recognized_mnemonic ?
567
0
          _("unrecognized form of instruction") :
568
0
          _("unrecognized instruction"));
569
570
0
  if (strlen (start) > 50)
571
    /* xgettext:c-format */
572
0
    sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
573
0
  else
574
    /* xgettext:c-format */
575
0
    sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
576
0
      }
577
0
    else
578
0
      {
579
0
  if (strlen (start) > 50)
580
    /* xgettext:c-format */
581
0
    sprintf (errbuf, _("bad instruction `%.50s...'"), start);
582
0
  else
583
    /* xgettext:c-format */
584
0
    sprintf (errbuf, _("bad instruction `%.50s'"), start);
585
0
      }
586
587
0
    *errmsg = errbuf;
588
0
    return NULL;
589
0
  }
590
0
}