aboutsummaryrefslogtreecommitdiff
path: root/bfd/xtensa-isa.c
blob: 761e5c6bb697efb8c2dce150ce040ff5900d720b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* Configurable Xtensa ISA support.
   Copyright 2003 Free Software Foundation, Inc.

   This file is part of BFD, the Binary File Descriptor library.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

#include "xtensa-isa.h"
#include "xtensa-isa-internal.h"

xtensa_isa xtensa_default_isa = NULL;

static int
opname_lookup_compare (const void *v1, const void *v2)
{
  opname_lookup_entry *e1 = (opname_lookup_entry *)v1;
  opname_lookup_entry *e2 = (opname_lookup_entry *)v2;

  return strcmp (e1->key, e2->key);
}


xtensa_isa
xtensa_isa_init (void)
{
  xtensa_isa isa;
  int mod;

  isa = xtensa_load_isa (0);
  if (isa == 0)
    {
      fprintf (stderr, "Failed to initialize Xtensa base ISA module\n");
      return NULL;
    }

  for (mod = 1; xtensa_isa_modules[mod].get_num_opcodes_fn; mod++)
    {
      if (!xtensa_extend_isa (isa, mod))
	{
	  fprintf (stderr, "Failed to initialize Xtensa TIE ISA module\n");
	  return NULL;
	}
    }

  return isa;
}

/* ISA information.  */

static int
xtensa_check_isa_config (xtensa_isa_internal *isa,
			 struct config_struct *config_table)
{
  int i, j;

  if (!config_table)
    {
      fprintf (stderr, "Error: Empty configuration table in ISA DLL\n");
      return 0;
    }

  /* For the first module, save a pointer to the table and record the
     specified endianness and availability of the density option.  */

  if (isa->num_modules == 0)
    {
      int found_memory_order = 0;

      isa->config = config_table;
      isa->has_density = 1;  /* Default to have density option.  */

      for (i = 0; config_table[i].param_name; i++)
	{
	  if (!strcmp (config_table[i].param_name, "IsaMemoryOrder"))
	    {
	      isa->is_big_endian =
		(strcmp (config_table[i].param_value, "BigEndian") == 0);
	      found_memory_order = 1;
	    }
	  if (!strcmp (config_table[i].param_name, "IsaUseDensityInstruction"))
	    {
	      isa->has_density = atoi (config_table[i].param_value);
	    }
	}
      if (!found_memory_order)
	{
	  fprintf (stderr, "Error: \"IsaMemoryOrder\" missing from "
		   "configuration table in ISA DLL\n");
	  return 0;
	}

      return 1;
    }

  /* For subsequent modules, check that the parameters match.  Note: This
     code is sufficient to handle the current model where there are never
     more than 2 modules; we might at some point want to handle cases where
     module N > 0 specifies some parameters not included in the base table,
     and we would then add those to isa->config so that subsequent modules
     would check against them. */

  for (i = 0; config_table[i].param_name; i++)
    {
      for (j = 0; isa->config[j].param_name; j++)
	{
	  if (!strcmp (config_table[i].param_name, isa->config[j].param_name))
	    {
	      int mismatch;
	      if (!strcmp (config_table[i].param_name, "IsaCoprocessorCount"))
		{
		  /* Only require the coprocessor count to be <= the base.  */
		  int tiecnt = atoi (config_table[i].param_value);
		  int basecnt = atoi (isa->config[j].param_value);
		  mismatch = (tiecnt > basecnt);
		}
	      else
		mismatch = strcmp (config_table[i].param_value,
				   isa->config[j].param_value);
	      if (mismatch)
		{
#define MISMATCH_MESSAGE \
"Error: Configuration mismatch in the \"%s\" parameter:\n\
the configuration used when the TIE file was compiled had a value of\n\
\"%s\", while the current configuration has a value of\n\
\"%s\". Please rerun the TIE compiler with a matching\n\
configuration.\n"
		  fprintf (stderr, MISMATCH_MESSAGE,
			   config_table[i].param_name,
			   config_table[i].param_value,
			   isa->config[j].param_value);
		  return 0;
		}
	      break;
	    }
	}
    }

  return 1;
}


static int
xtensa_add_isa (xtensa_isa_internal *isa, libisa_module_specifier libisa)
{
  int (*get_num_opcodes_fn) (void);
  struct config_struct *(*get_config_table_fn) (void);
  xtensa_opcode_internal **(*get_opcodes_fn) (void);
  int (*decode_insn_fn) (const xtensa_insnbuf);
  xtensa_opcode_internal **opcodes;
  int opc, insn_size, prev_num_opcodes, new_num_opcodes, this_module;

  get_num_opcodes_fn = xtensa_isa_modules[libisa].get_num_opcodes_fn;
  get_opcodes_fn = xtensa_isa_modules[libisa].get_opcodes_fn;
  decode_insn_fn = xtensa_isa_modules[libisa].decode_insn_fn;
  get_config_table_fn = xtensa_isa_modules[libisa].get_config_table_fn;

  if (!get_num_opcodes_fn || !get_opcodes_fn || !decode_insn_fn
      || (!get_config_table_fn && isa->num_modules == 0))
    return 0;

  if (get_config_table_fn
      && !xtensa_check_isa_config (isa, get_config_table_fn ()))
    return 0;

  prev_num_opcodes = isa->num_opcodes;
  new_num_opcodes = (*get_num_opcodes_fn) ();

  isa->num_opcodes += new_num_opcodes;
  isa->opcode_table = (xtensa_opcode_internal **)
    realloc (isa->opcode_table, isa->num_opcodes *
	     sizeof (xtensa_opcode_internal *));
  isa->opname_lookup_table = (opname_lookup_entry *)
    realloc (isa->opname_lookup_table, isa->num_opcodes *
	     sizeof (opname_lookup_entry));

  opcodes = (*get_opcodes_fn) ();

  insn_size = isa->insn_size;
  for (opc = 0; opc < new_num_opcodes; opc++)
    {
      xtensa_opcode_internal *intopc = opcodes[opc];
      int newopc = prev_num_opcodes + opc;
      isa->opcode_table[newopc] = intopc;
      isa->opname_lookup_table[newopc].key = intopc->name;
      isa->opname_lookup_table[newopc].opcode = newopc;
      if (intopc->length > insn_size)
	insn_size = intopc->length;
    }

  isa->insn_size = insn_size;
  isa->insnbuf_size = ((isa->insn_size + sizeof (xtensa_insnbuf_word) - 1) /
		       sizeof (xtensa_insnbuf_word));

  qsort (isa->opname_lookup_table, isa->num_opcodes,
	 sizeof (opname_lookup_entry), opname_lookup_compare);

  /* Check for duplicate opcode names.  */
  for (opc = 1; opc < isa->num_opcodes; opc++)
    {
      if (!opname_lookup_compare (&isa->opname_lookup_table[opc-1],
				  &isa->opname_lookup_table[opc]))
	{
	  fprintf (stderr, "Error: Duplicate TIE opcode \"%s\"\n",
		   isa->opname_lookup_table[opc].key);
	  return 0;
	}
    }

  this_module = isa->num_modules;
  isa->num_modules += 1;

  isa->module_opcode_base = (int *) realloc (isa->module_opcode_base,
					     isa->num_modules * sizeof (int));
  isa->module_decode_fn = (xtensa_insn_decode_fn *)
    realloc (isa->module_decode_fn, isa->num_modules *
	     sizeof (xtensa_insn_decode_fn));

  isa->module_opcode_base[this_module] = prev_num_opcodes;
  isa->module_decode_fn[this_module] = decode_insn_fn;

  xtensa_default_isa = isa;

  return 1;	/* Library was successfully added.  */
}


xtensa_isa
xtensa_load_isa (libisa_module_specifier libisa)
{
  xtensa_isa_internal *isa;

  isa = (xtensa_isa_internal *) malloc (sizeof (xtensa_isa_internal));
  memset (isa, 0, sizeof (xtensa_isa_internal));
  if (!xtensa_add_isa (isa, libisa))
    {
      xtensa_isa_free (isa);
      return NULL;
    }
  return (xtensa_isa) isa;
}


int
xtensa_extend_isa (xtensa_isa isa, libisa_module_specifier libisa)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return xtensa_add_isa (intisa, libisa);
}


void
xtensa_isa_free (xtensa_isa isa)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  if (intisa->opcode_table)
    free (intisa->opcode_table);
  if (intisa->opname_lookup_table)
    free (intisa->opname_lookup_table);
  if (intisa->module_opcode_base)
    free (intisa->module_opcode_base);
  if (intisa->module_decode_fn)
    free (intisa->module_decode_fn);
  free (intisa);
}


int
xtensa_insn_maxlength (xtensa_isa isa)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return intisa->insn_size;
}


int
xtensa_insnbuf_size (xtensa_isa isa)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *)isa;
  return intisa->insnbuf_size;
}


int
xtensa_num_opcodes (xtensa_isa isa)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return intisa->num_opcodes;
}


xtensa_opcode
xtensa_opcode_lookup (xtensa_isa isa, const char *opname)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  opname_lookup_entry entry, *result;

  entry.key = opname;
  result = bsearch (&entry, intisa->opname_lookup_table, intisa->num_opcodes,
		    sizeof (opname_lookup_entry), opname_lookup_compare);
  if (!result) return XTENSA_UNDEFINED;
  return result->opcode;
}


xtensa_opcode
xtensa_decode_insn (xtensa_isa isa, const xtensa_insnbuf insn)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  int n, opc;
  for (n = 0; n < intisa->num_modules; n++) {
    opc = (intisa->module_decode_fn[n]) (insn);
    if (opc != XTENSA_UNDEFINED)
      return intisa->module_opcode_base[n] + opc;
  }
  return XTENSA_UNDEFINED;
}


/* Opcode information.  */

void
xtensa_encode_insn (xtensa_isa isa, xtensa_opcode opc, xtensa_insnbuf insn)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  xtensa_insnbuf template = intisa->opcode_table[opc]->template();
  int len = intisa->opcode_table[opc]->length;
  int n;

  /* Convert length to 32-bit words.  */
  len = (len + 3) / 4;

  /* Copy the template.  */
  for (n = 0; n < len; n++)
    insn[n] = template[n];

  /* Fill any unused buffer space with zeros.  */
  for ( ; n < intisa->insnbuf_size; n++)
    insn[n] = 0;
}


const char *
xtensa_opcode_name (xtensa_isa isa, xtensa_opcode opc)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return intisa->opcode_table[opc]->name;
}


int
xtensa_insn_length (xtensa_isa isa, xtensa_opcode opc)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return intisa->opcode_table[opc]->length;
}


int
xtensa_insn_length_from_first_byte (xtensa_isa isa, char first_byte)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  int is_density = (first_byte & (intisa->is_big_endian ? 0x80 : 0x08)) != 0;
  return (intisa->has_density && is_density ? 2 : 3);
}


int
xtensa_num_operands (xtensa_isa isa, xtensa_opcode opc)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  return intisa->opcode_table[opc]->iclass->num_operands;
}


xtensa_operand
xtensa_get_operand (xtensa_isa isa, xtensa_opcode opc, int opnd)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  xtensa_iclass_internal *iclass = intisa->opcode_table[opc]->iclass;
  if (opnd >= iclass->num_operands)
    return NULL;
  return (xtensa_operand) iclass->operands[opnd];
}


/* Operand information.  */

char *
xtensa_operand_kind (xtensa_operand opnd)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return intop->operand_kind;
}


char
xtensa_operand_inout (xtensa_operand opnd)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return intop->inout;
}


uint32
xtensa_operand_get_field (xtensa_operand opnd, const xtensa_insnbuf insn)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return (*intop->get_field) (insn);
}


void
xtensa_operand_set_field (xtensa_operand opnd, xtensa_insnbuf insn, uint32 val)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return (*intop->set_field) (insn, val);
}


xtensa_encode_result
xtensa_operand_encode (xtensa_operand opnd, uint32 *valp)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return (*intop->encode) (valp);
}


uint32
xtensa_operand_decode (xtensa_operand opnd, uint32 val)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return (*intop->decode) (val);
}


int
xtensa_operand_isPCRelative (xtensa_operand opnd)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  return intop->isPCRelative;
}


uint32
xtensa_operand_do_reloc (xtensa_operand opnd, uint32 addr, uint32 pc)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  if (!intop->isPCRelative)
    return addr;
  return (*intop->do_reloc) (addr, pc);
}


uint32
xtensa_operand_undo_reloc (xtensa_operand opnd, uint32 offset, uint32 pc)
{
  xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
  if (!intop->isPCRelative)
    return offset;
  return (*intop->undo_reloc) (offset, pc);
}


/* Instruction buffers.  */

xtensa_insnbuf
xtensa_insnbuf_alloc (xtensa_isa isa)
{
  return (xtensa_insnbuf) malloc (xtensa_insnbuf_size (isa) *
				  sizeof (xtensa_insnbuf_word));
}


void
xtensa_insnbuf_free (xtensa_insnbuf buf)
{
  free( buf );
}


/* Given <byte_index>, the index of a byte in a xtensa_insnbuf, our
   internal representation of a xtensa instruction word, return the index of
   its word and the bit index of its low order byte in the xtensa_insnbuf.  */

static inline int
byte_to_word_index (int byte_index)
{
  return byte_index / sizeof (xtensa_insnbuf_word);
}


static inline int
byte_to_bit_index (int byte_index)
{
  return (byte_index & 0x3) * 8;
}


/* Copy an instruction in the 32 bit words pointed at by <insn> to characters
   pointed at by <cp>.  This is more complicated than you might think because
   we want 16 bit instructions in bytes 2,3 for big endian. This function
   allows us to specify which byte in <insn> to start with and which way to
   increment, allowing trivial implementation for both big and little endian.
   And it seems to make pretty good code for both.  */

void
xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn, char *cp)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  int insn_size = xtensa_insn_maxlength (intisa);
  int fence_post, start, increment, i, byte_count;
  xtensa_opcode opc;

  if (intisa->is_big_endian)
    {
      start = insn_size - 1;
      increment = -1;
    }
  else
    {
      start = 0;
      increment = 1;
    }

  /* Find the opcode; do nothing if the buffer does not contain a valid
     instruction since we need to know how many bytes to copy.  */
  opc = xtensa_decode_insn (isa, insn);
  if (opc == XTENSA_UNDEFINED)
    return;

  byte_count = xtensa_insn_length (isa, opc);
  fence_post = start + (byte_count * increment);

  for (i = start; i != fence_post; i += increment, ++cp)
    {
      int word_inx = byte_to_word_index (i);
      int bit_inx = byte_to_bit_index (i);

      *cp = (insn[word_inx] >> bit_inx) & 0xff;
    }
}

/* Inward conversion from byte stream to xtensa_insnbuf.  See
   xtensa_insnbuf_to_chars for a discussion of why this is
   complicated by endianness.  */
    
void
xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn, const char* cp)
{
  xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
  int insn_size = xtensa_insn_maxlength (intisa);
  int fence_post, start, increment, i;

  if (intisa->is_big_endian)
    {
      start = insn_size - 1;
      increment = -1;
    }
  else
    {
      start = 0;
      increment = 1;
    }

  fence_post = start + (insn_size * increment);
  memset (insn, 0, xtensa_insnbuf_size (isa) * sizeof (xtensa_insnbuf_word));

  for ( i = start; i != fence_post; i += increment, ++cp )
    {
      int word_inx = byte_to_word_index (i);
      int bit_inx = byte_to_bit_index (i);

      insn[word_inx] |= (*cp & 0xff) << bit_inx;
    }
}