Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/opcodes/or1k-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-2025 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 "or1k-desc.h"
36
#include "or1k-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
static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
55
static const char * INVALID_STORE_RELOC = N_("relocation invalid for store");
56
static const char * INVALID_RELOC_TYPE = N_("internal relocation type invalid");
57
58
#define CGEN_VERBOSE_ASSEMBLER_ERRORS
59
60
static const char *
61
parse_disp26 (CGEN_CPU_DESC cd,
62
        const char ** strp,
63
        int opindex,
64
        int opinfo ATTRIBUTE_UNUSED,
65
        enum cgen_parse_operand_result * resultp,
66
        bfd_vma * valuep)
67
0
{
68
0
  const char *str = *strp;
69
0
  const char *errmsg = NULL;
70
0
  bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_REL_26;
71
72
0
  if (strncasecmp (str, "plta(", 5) == 0)
73
0
    {
74
0
      *strp = str + 5;
75
0
      reloc = BFD_RELOC_OR1K_PLTA26;
76
0
    }
77
0
  else if (strncasecmp (str, "plt(", 4) == 0)
78
0
    {
79
0
      *strp = str + 4;
80
0
      reloc = BFD_RELOC_OR1K_PLT26;
81
0
    }
82
83
0
  errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
84
85
0
  if (reloc != BFD_RELOC_OR1K_REL_26)
86
0
    {
87
0
      if (**strp != ')')
88
0
  errmsg = MISSING_CLOSING_PARENTHESIS;
89
0
      else
90
0
  ++*strp;
91
0
    }
92
93
0
  return errmsg;
94
0
}
95
96
static const char *
97
parse_disp21 (CGEN_CPU_DESC cd,
98
        const char ** strp,
99
        int opindex,
100
        int opinfo ATTRIBUTE_UNUSED,
101
        enum cgen_parse_operand_result * resultp,
102
        bfd_vma * valuep)
103
0
{
104
0
  const char *str = *strp;
105
0
  const char *errmsg = NULL;
106
0
  bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_PCREL_PG21;
107
108
0
  if (strncasecmp (str, "got(", 4) == 0)
109
0
    {
110
0
      *strp = str + 4;
111
0
      reloc = BFD_RELOC_OR1K_GOT_PG21;
112
0
    }
113
0
  else if (strncasecmp (str, "tlsgd(", 6) == 0)
114
0
    {
115
0
      *strp = str + 6;
116
0
      reloc = BFD_RELOC_OR1K_TLS_GD_PG21;
117
0
    }
118
0
  else if (strncasecmp (str, "tlsldm(", 7) == 0)
119
0
    {
120
0
      *strp = str + 7;
121
0
      reloc = BFD_RELOC_OR1K_TLS_LDM_PG21;
122
0
    }
123
0
  else if (strncasecmp (str, "gottp(", 6) == 0)
124
0
    {
125
0
      *strp = str + 6;
126
0
      reloc = BFD_RELOC_OR1K_TLS_IE_PG21;
127
0
    }
128
129
0
  errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
130
131
0
  if (reloc != BFD_RELOC_OR1K_PCREL_PG21)
132
0
    {
133
0
      if (**strp != ')')
134
0
  errmsg = MISSING_CLOSING_PARENTHESIS;
135
0
      else
136
0
  ++*strp;
137
0
    }
138
139
0
  return errmsg;
140
0
}
141
142
enum or1k_rclass
143
{
144
  RCLASS_DIRECT   = 0,
145
  RCLASS_GOT      = 1,
146
  RCLASS_GOTPC    = 2,
147
  RCLASS_GOTOFF   = 3,
148
  RCLASS_TLSGD    = 4,
149
  RCLASS_TLSLDM   = 5,
150
  RCLASS_DTPOFF   = 6,
151
  RCLASS_GOTTPOFF = 7,
152
  RCLASS_TPOFF    = 8,
153
};
154
155
enum or1k_rtype
156
{
157
  RTYPE_LO = 0,
158
  RTYPE_SLO = 1,
159
  RTYPE_PO = 2,
160
  RTYPE_SPO = 3,
161
  RTYPE_HI = 4,
162
  RTYPE_AHI = 5,
163
};
164
165
0
#define RCLASS_SHIFT 3
166
0
#define RTYPE_MASK   7
167
168
static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
169
  { BFD_RELOC_LO16,
170
    BFD_RELOC_OR1K_SLO16,
171
    BFD_RELOC_OR1K_LO13,
172
    BFD_RELOC_OR1K_SLO13,
173
    BFD_RELOC_HI16,
174
    BFD_RELOC_HI16_S, },
175
  { BFD_RELOC_OR1K_GOT16,
176
    BFD_RELOC_UNUSED,
177
    BFD_RELOC_OR1K_GOT_LO13,
178
    BFD_RELOC_UNUSED,
179
    BFD_RELOC_UNUSED,
180
    BFD_RELOC_OR1K_GOT_AHI16 },
181
  { BFD_RELOC_OR1K_GOTPC_LO16,
182
    BFD_RELOC_UNUSED,
183
    BFD_RELOC_UNUSED,
184
    BFD_RELOC_UNUSED,
185
    BFD_RELOC_OR1K_GOTPC_HI16,
186
    BFD_RELOC_UNUSED },
187
  { BFD_RELOC_LO16_GOTOFF,
188
    BFD_RELOC_OR1K_GOTOFF_SLO16,
189
    BFD_RELOC_UNUSED,
190
    BFD_RELOC_UNUSED,
191
    BFD_RELOC_HI16_GOTOFF,
192
    BFD_RELOC_HI16_S_GOTOFF },
193
  { BFD_RELOC_OR1K_TLS_GD_LO16,
194
    BFD_RELOC_UNUSED,
195
    BFD_RELOC_OR1K_TLS_GD_LO13,
196
    BFD_RELOC_UNUSED,
197
    BFD_RELOC_OR1K_TLS_GD_HI16,
198
    BFD_RELOC_UNUSED },
199
  { BFD_RELOC_OR1K_TLS_LDM_LO16,
200
    BFD_RELOC_UNUSED,
201
    BFD_RELOC_OR1K_TLS_LDM_LO13,
202
    BFD_RELOC_UNUSED,
203
    BFD_RELOC_OR1K_TLS_LDM_HI16,
204
    BFD_RELOC_UNUSED },
205
  { BFD_RELOC_OR1K_TLS_LDO_LO16,
206
    BFD_RELOC_UNUSED,
207
    BFD_RELOC_UNUSED,
208
    BFD_RELOC_UNUSED,
209
    BFD_RELOC_OR1K_TLS_LDO_HI16,
210
    BFD_RELOC_UNUSED },
211
  { BFD_RELOC_OR1K_TLS_IE_LO16,
212
    BFD_RELOC_UNUSED,
213
    BFD_RELOC_OR1K_TLS_IE_LO13,
214
    BFD_RELOC_UNUSED,
215
    BFD_RELOC_OR1K_TLS_IE_HI16,
216
    BFD_RELOC_OR1K_TLS_IE_AHI16 },
217
  { BFD_RELOC_OR1K_TLS_LE_LO16,
218
    BFD_RELOC_OR1K_TLS_LE_SLO16,
219
    BFD_RELOC_UNUSED,
220
    BFD_RELOC_UNUSED,
221
    BFD_RELOC_OR1K_TLS_LE_HI16,
222
    BFD_RELOC_OR1K_TLS_LE_AHI16 },
223
};
224
225
static int
226
parse_reloc (const char **strp)
227
0
{
228
0
    const char *str = *strp;
229
0
    enum or1k_rclass cls = RCLASS_DIRECT;
230
0
    enum or1k_rtype typ;
231
232
0
    if (strncasecmp (str, "got(", 4) == 0)
233
0
      {
234
0
  *strp = str + 4;
235
0
  return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_LO;
236
0
      }
237
0
    if (strncasecmp (str, "gotpo(", 6) == 0)
238
0
      {
239
0
  *strp = str + 6;
240
0
  return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_PO;
241
0
      }
242
0
    if (strncasecmp (str, "gottppo(", 8) == 0)
243
0
      {
244
0
  *strp = str + 8;
245
0
  return (RCLASS_GOTTPOFF << RCLASS_SHIFT) | RTYPE_PO;
246
0
      }
247
248
0
    if (strncasecmp (str, "gotpc", 5) == 0)
249
0
      {
250
0
  str += 5;
251
0
  cls = RCLASS_GOTPC;
252
0
      }
253
0
    else if (strncasecmp (str, "gotoff", 6) == 0)
254
0
      {
255
0
  str += 6;
256
0
  cls = RCLASS_GOTOFF;
257
0
      }
258
0
    else if (strncasecmp (str, "tlsgd", 5) == 0)
259
0
      {
260
0
  str += 5;
261
0
  cls = RCLASS_TLSGD;
262
0
      }
263
0
    else if (strncasecmp (str, "tlsldm", 6) == 0)
264
0
      {
265
0
  str += 6;
266
0
  cls = RCLASS_TLSLDM;
267
0
      }
268
0
    else if (strncasecmp (str, "dtpoff", 6) == 0)
269
0
      {
270
0
  str += 6;
271
0
  cls = RCLASS_DTPOFF;
272
0
      }
273
0
    else if (strncasecmp (str, "gottpoff", 8) == 0)
274
0
      {
275
0
  str += 8;
276
0
  cls = RCLASS_GOTTPOFF;
277
0
      }
278
0
    else if (strncasecmp (str, "tpoff", 5) == 0)
279
0
      {
280
0
  str += 5;
281
0
  cls = RCLASS_TPOFF;
282
0
      }
283
0
    else if (strncasecmp (str, "got", 3) == 0)
284
0
      {
285
0
  str += 3;
286
0
  cls = RCLASS_GOT;
287
0
      }
288
289
0
    if (strncasecmp (str, "hi(", 3) == 0)
290
0
      {
291
0
  str += 3;
292
0
  typ = RTYPE_HI;
293
0
      }
294
0
    else if (strncasecmp (str, "lo(", 3) == 0)
295
0
      {
296
0
  str += 3;
297
0
  typ = RTYPE_LO;
298
0
      }
299
0
    else if (strncasecmp (str, "ha(", 3) == 0)
300
0
      {
301
0
  str += 3;
302
0
  typ = RTYPE_AHI;
303
0
      }
304
0
    else if (strncasecmp (str, "po(", 3) == 0 && cls != RCLASS_GOTTPOFF)
305
0
      {
306
0
  str += 3;
307
0
  typ = RTYPE_PO;
308
0
      }
309
0
    else
310
0
      return -1;
311
312
0
    *strp = str;
313
0
    return (cls << RCLASS_SHIFT) | typ;
314
0
}
315
316
static const char *
317
parse_imm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
318
       long *valuep, int splitp)
319
0
{
320
0
  const char *errmsg;
321
0
  enum cgen_parse_operand_result result_type;
322
0
  bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
323
0
  enum or1k_rtype reloc_type;
324
0
  int reloc_code;
325
0
  bfd_vma ret;
326
327
0
  if (**strp == '#')
328
0
    ++*strp;
329
330
0
  reloc_code = parse_reloc (strp);
331
0
  reloc_type = reloc_code & RTYPE_MASK;
332
0
  if (reloc_code >= 0)
333
0
    {
334
0
      enum or1k_rclass reloc_class = reloc_code >> RCLASS_SHIFT;
335
0
      if (splitp)
336
0
  {
337
0
    if ((reloc_type == RTYPE_LO || reloc_type == RTYPE_PO)
338
0
        && reloc_class != RCLASS_GOT)
339
      /* If split we or up the type to RTYPE_SLO or RTYPE_SPO.  */
340
0
      reloc_type |= 1;
341
0
    else
342
0
      return INVALID_STORE_RELOC;
343
0
  }
344
0
      reloc = or1k_imm16_relocs[reloc_class][reloc_type];
345
0
    }
346
347
0
  if (reloc != BFD_RELOC_UNUSED)
348
0
    {
349
0
      bfd_vma value;
350
351
0
      errmsg = cgen_parse_address (cd, strp, opindex, reloc,
352
0
           &result_type, &value);
353
0
      if (**strp != ')')
354
0
  errmsg = MISSING_CLOSING_PARENTHESIS;
355
0
      ++*strp;
356
357
0
      ret = value;
358
359
0
      if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
360
0
  switch (reloc_type)
361
0
    {
362
0
    case RTYPE_AHI:
363
0
      ret += 0x8000;
364
      /* FALLTHRU */
365
0
    case RTYPE_HI:
366
0
      ret >>= 16;
367
      /* FALLTHRU */
368
0
    case RTYPE_LO:
369
0
    case RTYPE_SLO:
370
0
      ret &= 0xffff;
371
0
      ret = (ret ^ 0x8000) - 0x8000;
372
0
      break;
373
0
    case RTYPE_PO:
374
0
    case RTYPE_SPO:
375
0
      ret &= 0x1fff;
376
0
      break;
377
0
    default:
378
0
      errmsg = INVALID_RELOC_TYPE;
379
0
    }
380
0
    }
381
0
  else
382
0
    {
383
0
      long value;
384
0
      errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
385
0
      ret = value;
386
0
    }
387
388
0
  if (errmsg == NULL)
389
0
    *valuep = ret;
390
391
0
  return errmsg;
392
0
}
393
394
static const char *
395
parse_simm16 (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
396
0
{
397
0
  return parse_imm16(cd, strp, opindex, (long *) valuep, 0);
398
0
}
399
400
static const char *
401
parse_simm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
402
        long *valuep)
403
0
{
404
0
  return parse_imm16(cd, strp, opindex, (long *) valuep, 1);
405
0
}
406
407
static const char *
408
parse_uimm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
409
        unsigned long *valuep)
410
0
{
411
0
  const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 0);
412
0
  if (errmsg == NULL)
413
0
    *valuep &= 0xffff;
414
0
  return errmsg;
415
0
}
416
417
static const char *
418
parse_uimm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
419
        unsigned long *valuep)
420
0
{
421
0
  const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 1);
422
0
  if (errmsg == NULL)
423
0
    *valuep &= 0xffff;
424
0
  return errmsg;
425
0
}
426
427
/* Parse register pairs with syntax rA,rB to a flag + rA value.  */
428
429
static const char *
430
parse_regpair (CGEN_CPU_DESC cd, const char **strp,
431
         int opindex ATTRIBUTE_UNUSED, unsigned long *valuep)
432
0
{
433
0
  long reg1_index;
434
0
  long reg2_index;
435
0
  const char *errmsg;
436
437
  /* The first part should just be a register.  */
438
0
  errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
439
0
             &reg1_index);
440
441
  /* If that worked skip the comma separator.  */
442
0
  if (errmsg == NULL)
443
0
    {
444
0
      if (**strp == ',')
445
0
  ++*strp;
446
0
      else
447
0
  errmsg = "Unexpected character, expected ','";
448
0
    }
449
450
  /* If that worked the next part is just another register.  */
451
0
  if (errmsg == NULL)
452
0
    errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
453
0
         &reg2_index);
454
455
  /* Validate the register pair is valid and create the output value.  */
456
0
  if (errmsg == NULL)
457
0
    {
458
0
      int regoffset = reg2_index - reg1_index;
459
460
0
      if (regoffset == 1 || regoffset == 2)
461
0
  {
462
0
    unsigned short offsetmask;
463
0
    unsigned short value;
464
465
0
    offsetmask = ((regoffset == 2 ? 1 : 0) << 5);
466
0
    value = offsetmask | reg1_index;
467
468
0
    *valuep = value;
469
0
  }
470
0
      else
471
0
  errmsg = "Invalid register pair, offset not 1 or 2.";
472
0
    }
473
474
0
  return errmsg;
475
0
}
476
477
/* -- */
478
479
const char * or1k_cgen_parse_operand
480
  (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
481
482
/* Main entry point for operand parsing.
483
484
   This function is basically just a big switch statement.  Earlier versions
485
   used tables to look up the function to use, but
486
   - if the table contains both assembler and disassembler functions then
487
     the disassembler contains much of the assembler and vice-versa,
488
   - there's a lot of inlining possibilities as things grow,
489
   - using a switch statement avoids the function call overhead.
490
491
   This function could be moved into `parse_insn_normal', but keeping it
492
   separate makes clear the interface between `parse_insn_normal' and each of
493
   the handlers.  */
494
495
const char *
496
or1k_cgen_parse_operand (CGEN_CPU_DESC cd,
497
         int opindex,
498
         const char ** strp,
499
         CGEN_FIELDS * fields)
500
0
{
501
0
  const char * errmsg = NULL;
502
  /* Used by scalar operands that still need to be parsed.  */
503
0
  long junk ATTRIBUTE_UNUSED;
504
505
0
  switch (opindex)
506
0
    {
507
0
    case OR1K_OPERAND_DISP21 :
508
0
      {
509
0
        bfd_vma value = 0;
510
0
        errmsg = parse_disp21 (cd, strp, OR1K_OPERAND_DISP21, 0, NULL,  & value);
511
0
        fields->f_disp21 = value;
512
0
      }
513
0
      break;
514
0
    case OR1K_OPERAND_DISP26 :
515
0
      {
516
0
        bfd_vma value = 0;
517
0
        errmsg = parse_disp26 (cd, strp, OR1K_OPERAND_DISP26, 0, NULL,  & value);
518
0
        fields->f_disp26 = value;
519
0
      }
520
0
      break;
521
0
    case OR1K_OPERAND_RA :
522
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r2);
523
0
      break;
524
0
    case OR1K_OPERAND_RAD32F :
525
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RAD32F, (unsigned long *) (& fields->f_rad32));
526
0
      break;
527
0
    case OR1K_OPERAND_RADI :
528
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RADI, (unsigned long *) (& fields->f_rad32));
529
0
      break;
530
0
    case OR1K_OPERAND_RASF :
531
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r2);
532
0
      break;
533
0
    case OR1K_OPERAND_RB :
534
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r3);
535
0
      break;
536
0
    case OR1K_OPERAND_RBD32F :
537
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBD32F, (unsigned long *) (& fields->f_rbd32));
538
0
      break;
539
0
    case OR1K_OPERAND_RBDI :
540
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBDI, (unsigned long *) (& fields->f_rbd32));
541
0
      break;
542
0
    case OR1K_OPERAND_RBSF :
543
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r3);
544
0
      break;
545
0
    case OR1K_OPERAND_RD :
546
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r1);
547
0
      break;
548
0
    case OR1K_OPERAND_RDD32F :
549
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDD32F, (unsigned long *) (& fields->f_rdd32));
550
0
      break;
551
0
    case OR1K_OPERAND_RDDI :
552
0
      errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDDI, (unsigned long *) (& fields->f_rdd32));
553
0
      break;
554
0
    case OR1K_OPERAND_RDSF :
555
0
      errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r1);
556
0
      break;
557
0
    case OR1K_OPERAND_SIMM16 :
558
0
      errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16, (long *) (& fields->f_simm16));
559
0
      break;
560
0
    case OR1K_OPERAND_SIMM16_SPLIT :
561
0
      errmsg = parse_simm16_split (cd, strp, OR1K_OPERAND_SIMM16_SPLIT, (long *) (& fields->f_simm16_split));
562
0
      break;
563
0
    case OR1K_OPERAND_UIMM16 :
564
0
      errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
565
0
      break;
566
0
    case OR1K_OPERAND_UIMM16_SPLIT :
567
0
      errmsg = parse_uimm16_split (cd, strp, OR1K_OPERAND_UIMM16_SPLIT, (unsigned long *) (& fields->f_uimm16_split));
568
0
      break;
569
0
    case OR1K_OPERAND_UIMM6 :
570
0
      errmsg = cgen_parse_unsigned_integer (cd, strp, OR1K_OPERAND_UIMM6, (unsigned long *) (& fields->f_uimm6));
571
0
      break;
572
573
0
    default :
574
      /* xgettext:c-format */
575
0
      opcodes_error_handler
576
0
  (_("internal error: unrecognized field %d while parsing"),
577
0
   opindex);
578
0
      abort ();
579
0
  }
580
581
0
  return errmsg;
582
0
}
583
584
cgen_parse_fn * const or1k_cgen_parse_handlers[] =
585
{
586
  parse_insn_normal,
587
};
588
589
void
590
or1k_cgen_init_asm (CGEN_CPU_DESC cd)
591
0
{
592
0
  or1k_cgen_init_opcode_table (cd);
593
0
  or1k_cgen_init_ibld_table (cd);
594
0
  cd->parse_handlers = & or1k_cgen_parse_handlers[0];
595
0
  cd->parse_operand = or1k_cgen_parse_operand;
596
#ifdef CGEN_ASM_INIT_HOOK
597
CGEN_ASM_INIT_HOOK
598
#endif
599
0
}
600
601

602
603
/* Regex construction routine.
604
605
   This translates an opcode syntax string into a regex string,
606
   by replacing any non-character syntax element (such as an
607
   opcode) with the pattern '.*'
608
609
   It then compiles the regex and stores it in the opcode, for
610
   later use by or1k_cgen_assemble_insn
611
612
   Returns NULL for success, an error message for failure.  */
613
614
char *
615
or1k_cgen_build_insn_regex (CGEN_INSN *insn)
616
715
{
617
715
  CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
618
715
  const char *mnem = CGEN_INSN_MNEMONIC (insn);
619
715
  char rxbuf[CGEN_MAX_RX_ELEMENTS];
620
715
  char *rx = rxbuf;
621
715
  const CGEN_SYNTAX_CHAR_TYPE *syn;
622
715
  int reg_err;
623
624
715
  syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
625
626
  /* Mnemonics come first in the syntax string.  */
627
715
  if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
628
5
    return _("missing mnemonic in syntax string");
629
710
  ++syn;
630
631
  /* Generate a case sensitive regular expression that emulates case
632
     insensitive matching in the "C" locale.  We cannot generate a case
633
     insensitive regular expression because in Turkish locales, 'i' and 'I'
634
     are not equal modulo case conversion.  */
635
636
  /* Copy the literal mnemonic out of the insn.  */
637
5.67k
  for (; *mnem; mnem++)
638
4.96k
    {
639
4.96k
      char c = *mnem;
640
641
4.96k
      if (ISALPHA (c))
642
3.97k
  {
643
3.97k
    *rx++ = '[';
644
3.97k
    *rx++ = TOLOWER (c);
645
3.97k
    *rx++ = TOUPPER (c);
646
3.97k
    *rx++ = ']';
647
3.97k
  }
648
990
      else
649
990
  *rx++ = c;
650
4.96k
    }
651
652
  /* Copy any remaining literals from the syntax string into the rx.  */
653
3.72k
  for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
654
3.01k
    {
655
3.01k
      if (CGEN_SYNTAX_CHAR_P (* syn))
656
1.53k
  {
657
1.53k
    char c = CGEN_SYNTAX_CHAR (* syn);
658
659
1.53k
    switch (c)
660
1.53k
      {
661
        /* Escape any regex metacharacters in the syntax.  */
662
0
      case '.': case '[': case '\\':
663
0
      case '*': case '^': case '$':
664
665
#ifdef CGEN_ESCAPE_EXTENDED_REGEX
666
      case '?': case '{': case '}':
667
      case '(': case ')': case '*':
668
      case '|': case '+': case ']':
669
#endif
670
0
        *rx++ = '\\';
671
0
        *rx++ = c;
672
0
        break;
673
674
1.53k
      default:
675
1.53k
        if (ISALPHA (c))
676
0
    {
677
0
      *rx++ = '[';
678
0
      *rx++ = TOLOWER (c);
679
0
      *rx++ = TOUPPER (c);
680
0
      *rx++ = ']';
681
0
    }
682
1.53k
        else
683
1.53k
    *rx++ = c;
684
1.53k
        break;
685
1.53k
      }
686
1.53k
  }
687
1.48k
      else
688
1.48k
  {
689
    /* Replace non-syntax fields with globs.  */
690
1.48k
    *rx++ = '.';
691
1.48k
    *rx++ = '*';
692
1.48k
  }
693
3.01k
    }
694
695
  /* Trailing whitespace ok.  */
696
710
  * rx++ = '[';
697
710
  * rx++ = ' ';
698
710
  * rx++ = '\t';
699
710
  * rx++ = ']';
700
710
  * rx++ = '*';
701
702
  /* But anchor it after that.  */
703
710
  * rx++ = '$';
704
710
  * rx = '\0';
705
706
710
  CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
707
710
  reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
708
709
710
  if (reg_err == 0)
710
710
    return NULL;
711
0
  else
712
0
    {
713
0
      static char msg[80];
714
715
0
      regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
716
0
      regfree ((regex_t *) CGEN_INSN_RX (insn));
717
0
      free (CGEN_INSN_RX (insn));
718
0
      (CGEN_INSN_RX (insn)) = NULL;
719
0
      return msg;
720
0
    }
721
710
}
722
723

724
/* Default insn parser.
725
726
   The syntax string is scanned and operands are parsed and stored in FIELDS.
727
   Relocs are queued as we go via other callbacks.
728
729
   ??? Note that this is currently an all-or-nothing parser.  If we fail to
730
   parse the instruction, we return 0 and the caller will start over from
731
   the beginning.  Backtracking will be necessary in parsing subexpressions,
732
   but that can be handled there.  Not handling backtracking here may get
733
   expensive in the case of the m68k.  Deal with later.
734
735
   Returns NULL for success, an error message for failure.  */
736
737
static const char *
738
parse_insn_normal (CGEN_CPU_DESC cd,
739
       const CGEN_INSN *insn,
740
       const char **strp,
741
       CGEN_FIELDS *fields)
742
0
{
743
  /* ??? Runtime added insns not handled yet.  */
744
0
  const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
745
0
  const char *str = *strp;
746
0
  const char *errmsg;
747
0
  const char *p;
748
0
  const CGEN_SYNTAX_CHAR_TYPE * syn;
749
0
#ifdef CGEN_MNEMONIC_OPERANDS
750
  /* FIXME: wip */
751
0
  int past_opcode_p;
752
0
#endif
753
754
  /* For now we assume the mnemonic is first (there are no leading operands).
755
     We can parse it without needing to set up operand parsing.
756
     GAS's input scrubber will ensure mnemonics are lowercase, but we may
757
     not be called from GAS.  */
758
0
  p = CGEN_INSN_MNEMONIC (insn);
759
0
  while (*p && TOLOWER (*p) == TOLOWER (*str))
760
0
    ++p, ++str;
761
762
0
  if (* p)
763
0
    return _("unrecognized instruction");
764
765
#ifndef CGEN_MNEMONIC_OPERANDS
766
  if (* str && ! ISSPACE (* str))
767
    return _("unrecognized instruction");
768
#endif
769
770
0
  CGEN_INIT_PARSE (cd);
771
0
  cgen_init_parse_operand (cd);
772
0
#ifdef CGEN_MNEMONIC_OPERANDS
773
0
  past_opcode_p = 0;
774
0
#endif
775
776
  /* We don't check for (*str != '\0') here because we want to parse
777
     any trailing fake arguments in the syntax string.  */
778
0
  syn = CGEN_SYNTAX_STRING (syntax);
779
780
  /* Mnemonics come first for now, ensure valid string.  */
781
0
  if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
782
0
    abort ();
783
784
0
  ++syn;
785
786
0
  while (* syn != 0)
787
0
    {
788
      /* Non operand chars must match exactly.  */
789
0
      if (CGEN_SYNTAX_CHAR_P (* syn))
790
0
  {
791
    /* FIXME: While we allow for non-GAS callers above, we assume the
792
       first char after the mnemonic part is a space.  */
793
    /* FIXME: We also take inappropriate advantage of the fact that
794
       GAS's input scrubber will remove extraneous blanks.  */
795
0
    if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
796
0
      {
797
0
#ifdef CGEN_MNEMONIC_OPERANDS
798
0
        if (CGEN_SYNTAX_CHAR(* syn) == ' ')
799
0
    past_opcode_p = 1;
800
0
#endif
801
0
        ++ syn;
802
0
        ++ str;
803
0
      }
804
0
    else if (*str)
805
0
      {
806
        /* Syntax char didn't match.  Can't be this insn.  */
807
0
        static char msg [80];
808
809
        /* xgettext:c-format */
810
0
        sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
811
0
           CGEN_SYNTAX_CHAR(*syn), *str);
812
0
        return msg;
813
0
      }
814
0
    else
815
0
      {
816
        /* Ran out of input.  */
817
0
        static char msg [80];
818
819
        /* xgettext:c-format */
820
0
        sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
821
0
           CGEN_SYNTAX_CHAR(*syn));
822
0
        return msg;
823
0
      }
824
0
    continue;
825
0
  }
826
827
0
#ifdef CGEN_MNEMONIC_OPERANDS
828
0
      (void) past_opcode_p;
829
0
#endif
830
      /* We have an operand of some sort.  */
831
0
      errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
832
0
      if (errmsg)
833
0
  return errmsg;
834
835
      /* Done with this operand, continue with next one.  */
836
0
      ++ syn;
837
0
    }
838
839
  /* If we're at the end of the syntax string, we're done.  */
840
0
  if (* syn == 0)
841
0
    {
842
      /* FIXME: For the moment we assume a valid `str' can only contain
843
   blanks now.  IE: We needn't try again with a longer version of
844
   the insn and it is assumed that longer versions of insns appear
845
   before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
846
0
      while (ISSPACE (* str))
847
0
  ++ str;
848
849
0
      if (* str != '\0')
850
0
  return _("junk at end of line"); /* FIXME: would like to include `str' */
851
852
0
      return NULL;
853
0
    }
854
855
  /* We couldn't parse it.  */
856
0
  return _("unrecognized instruction");
857
0
}
858

859
/* Main entry point.
860
   This routine is called for each instruction to be assembled.
861
   STR points to the insn to be assembled.
862
   We assume all necessary tables have been initialized.
863
   The assembled instruction, less any fixups, is stored in BUF.
864
   Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
865
   still needs to be converted to target byte order, otherwise BUF is an array
866
   of bytes in target byte order.
867
   The result is a pointer to the insn's entry in the opcode table,
868
   or NULL if an error occured (an error message will have already been
869
   printed).
870
871
   Note that when processing (non-alias) macro-insns,
872
   this function recurses.
873
874
   ??? It's possible to make this cpu-independent.
875
   One would have to deal with a few minor things.
876
   At this point in time doing so would be more of a curiosity than useful
877
   [for example this file isn't _that_ big], but keeping the possibility in
878
   mind helps keep the design clean.  */
879
880
const CGEN_INSN *
881
or1k_cgen_assemble_insn (CGEN_CPU_DESC cd,
882
         const char *str,
883
         CGEN_FIELDS *fields,
884
         CGEN_INSN_BYTES_PTR buf,
885
         char **errmsg)
886
0
{
887
0
  const char *start;
888
0
  CGEN_INSN_LIST *ilist;
889
0
  const char *parse_errmsg = NULL;
890
0
  const char *insert_errmsg = NULL;
891
0
  int recognized_mnemonic = 0;
892
893
  /* Skip leading white space.  */
894
0
  while (ISSPACE (* str))
895
0
    ++ str;
896
897
  /* The instructions are stored in hashed lists.
898
     Get the first in the list.  */
899
0
  ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
900
901
  /* Keep looking until we find a match.  */
902
0
  start = str;
903
0
  for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
904
0
    {
905
0
      const CGEN_INSN *insn = ilist->insn;
906
0
      recognized_mnemonic = 1;
907
908
0
#ifdef CGEN_VALIDATE_INSN_SUPPORTED
909
      /* Not usually needed as unsupported opcodes
910
   shouldn't be in the hash lists.  */
911
      /* Is this insn supported by the selected cpu?  */
912
0
      if (! or1k_cgen_insn_supported (cd, insn))
913
0
  continue;
914
0
#endif
915
      /* If the RELAXED attribute is set, this is an insn that shouldn't be
916
   chosen immediately.  Instead, it is used during assembler/linker
917
   relaxation if possible.  */
918
0
      if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
919
0
  continue;
920
921
0
      str = start;
922
923
      /* Skip this insn if str doesn't look right lexically.  */
924
0
      if (CGEN_INSN_RX (insn) != NULL &&
925
0
    regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
926
0
  continue;
927
928
      /* Allow parse/insert handlers to obtain length of insn.  */
929
0
      CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
930
931
0
      parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
932
0
      if (parse_errmsg != NULL)
933
0
  continue;
934
935
      /* ??? 0 is passed for `pc'.  */
936
0
      insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
937
0
             (bfd_vma) 0);
938
0
      if (insert_errmsg != NULL)
939
0
        continue;
940
941
      /* It is up to the caller to actually output the insn and any
942
         queued relocs.  */
943
0
      return insn;
944
0
    }
945
946
0
  {
947
0
    static char errbuf[150];
948
0
    const char *tmp_errmsg;
949
0
#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
950
0
#define be_verbose 1
951
#else
952
#define be_verbose 0
953
#endif
954
955
0
    if (be_verbose)
956
0
      {
957
  /* If requesting verbose error messages, use insert_errmsg.
958
     Failing that, use parse_errmsg.  */
959
0
  tmp_errmsg = (insert_errmsg ? insert_errmsg :
960
0
          parse_errmsg ? parse_errmsg :
961
0
          recognized_mnemonic ?
962
0
          _("unrecognized form of instruction") :
963
0
          _("unrecognized instruction"));
964
965
0
  if (strlen (start) > 50)
966
    /* xgettext:c-format */
967
0
    sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
968
0
  else
969
    /* xgettext:c-format */
970
0
    sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
971
0
      }
972
0
    else
973
0
      {
974
0
  if (strlen (start) > 50)
975
    /* xgettext:c-format */
976
0
    sprintf (errbuf, _("bad instruction `%.50s...'"), start);
977
0
  else
978
    /* xgettext:c-format */
979
0
    sprintf (errbuf, _("bad instruction `%.50s'"), start);
980
0
      }
981
982
0
    *errmsg = errbuf;
983
0
    return NULL;
984
0
  }
985
0
}