1*d2201f2fSdrahn /* Configurable Xtensa ISA support.
2*d2201f2fSdrahn Copyright 2003 Free Software Foundation, Inc.
3*d2201f2fSdrahn
4*d2201f2fSdrahn This file is part of BFD, the Binary File Descriptor library.
5*d2201f2fSdrahn
6*d2201f2fSdrahn This program is free software; you can redistribute it and/or modify
7*d2201f2fSdrahn it under the terms of the GNU General Public License as published by
8*d2201f2fSdrahn the Free Software Foundation; either version 2 of the License, or
9*d2201f2fSdrahn (at your option) any later version.
10*d2201f2fSdrahn
11*d2201f2fSdrahn This program is distributed in the hope that it will be useful,
12*d2201f2fSdrahn but WITHOUT ANY WARRANTY; without even the implied warranty of
13*d2201f2fSdrahn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*d2201f2fSdrahn GNU General Public License for more details.
15*d2201f2fSdrahn
16*d2201f2fSdrahn You should have received a copy of the GNU General Public License
17*d2201f2fSdrahn along with this program; if not, write to the Free Software
18*d2201f2fSdrahn Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19*d2201f2fSdrahn
20*d2201f2fSdrahn #include <stdio.h>
21*d2201f2fSdrahn #include <stdlib.h>
22*d2201f2fSdrahn #include <sys/types.h>
23*d2201f2fSdrahn #include <string.h>
24*d2201f2fSdrahn
25*d2201f2fSdrahn #include "xtensa-isa.h"
26*d2201f2fSdrahn #include "xtensa-isa-internal.h"
27*d2201f2fSdrahn
28*d2201f2fSdrahn xtensa_isa xtensa_default_isa = NULL;
29*d2201f2fSdrahn
30*d2201f2fSdrahn static int
opname_lookup_compare(const void * v1,const void * v2)31*d2201f2fSdrahn opname_lookup_compare (const void *v1, const void *v2)
32*d2201f2fSdrahn {
33*d2201f2fSdrahn opname_lookup_entry *e1 = (opname_lookup_entry *)v1;
34*d2201f2fSdrahn opname_lookup_entry *e2 = (opname_lookup_entry *)v2;
35*d2201f2fSdrahn
36*d2201f2fSdrahn return strcmp (e1->key, e2->key);
37*d2201f2fSdrahn }
38*d2201f2fSdrahn
39*d2201f2fSdrahn
40*d2201f2fSdrahn xtensa_isa
xtensa_isa_init(void)41*d2201f2fSdrahn xtensa_isa_init (void)
42*d2201f2fSdrahn {
43*d2201f2fSdrahn xtensa_isa isa;
44*d2201f2fSdrahn int mod;
45*d2201f2fSdrahn
46*d2201f2fSdrahn isa = xtensa_load_isa (0);
47*d2201f2fSdrahn if (isa == 0)
48*d2201f2fSdrahn {
49*d2201f2fSdrahn fprintf (stderr, "Failed to initialize Xtensa base ISA module\n");
50*d2201f2fSdrahn return NULL;
51*d2201f2fSdrahn }
52*d2201f2fSdrahn
53*d2201f2fSdrahn for (mod = 1; xtensa_isa_modules[mod].get_num_opcodes_fn; mod++)
54*d2201f2fSdrahn {
55*d2201f2fSdrahn if (!xtensa_extend_isa (isa, mod))
56*d2201f2fSdrahn {
57*d2201f2fSdrahn fprintf (stderr, "Failed to initialize Xtensa TIE ISA module\n");
58*d2201f2fSdrahn return NULL;
59*d2201f2fSdrahn }
60*d2201f2fSdrahn }
61*d2201f2fSdrahn
62*d2201f2fSdrahn return isa;
63*d2201f2fSdrahn }
64*d2201f2fSdrahn
65*d2201f2fSdrahn /* ISA information. */
66*d2201f2fSdrahn
67*d2201f2fSdrahn static int
xtensa_check_isa_config(xtensa_isa_internal * isa,struct config_struct * config_table)68*d2201f2fSdrahn xtensa_check_isa_config (xtensa_isa_internal *isa,
69*d2201f2fSdrahn struct config_struct *config_table)
70*d2201f2fSdrahn {
71*d2201f2fSdrahn int i, j;
72*d2201f2fSdrahn
73*d2201f2fSdrahn if (!config_table)
74*d2201f2fSdrahn {
75*d2201f2fSdrahn fprintf (stderr, "Error: Empty configuration table in ISA DLL\n");
76*d2201f2fSdrahn return 0;
77*d2201f2fSdrahn }
78*d2201f2fSdrahn
79*d2201f2fSdrahn /* For the first module, save a pointer to the table and record the
80*d2201f2fSdrahn specified endianness and availability of the density option. */
81*d2201f2fSdrahn
82*d2201f2fSdrahn if (isa->num_modules == 0)
83*d2201f2fSdrahn {
84*d2201f2fSdrahn int found_memory_order = 0;
85*d2201f2fSdrahn
86*d2201f2fSdrahn isa->config = config_table;
87*d2201f2fSdrahn isa->has_density = 1; /* Default to have density option. */
88*d2201f2fSdrahn
89*d2201f2fSdrahn for (i = 0; config_table[i].param_name; i++)
90*d2201f2fSdrahn {
91*d2201f2fSdrahn if (!strcmp (config_table[i].param_name, "IsaMemoryOrder"))
92*d2201f2fSdrahn {
93*d2201f2fSdrahn isa->is_big_endian =
94*d2201f2fSdrahn (strcmp (config_table[i].param_value, "BigEndian") == 0);
95*d2201f2fSdrahn found_memory_order = 1;
96*d2201f2fSdrahn }
97*d2201f2fSdrahn if (!strcmp (config_table[i].param_name, "IsaUseDensityInstruction"))
98*d2201f2fSdrahn {
99*d2201f2fSdrahn isa->has_density = atoi (config_table[i].param_value);
100*d2201f2fSdrahn }
101*d2201f2fSdrahn }
102*d2201f2fSdrahn if (!found_memory_order)
103*d2201f2fSdrahn {
104*d2201f2fSdrahn fprintf (stderr, "Error: \"IsaMemoryOrder\" missing from "
105*d2201f2fSdrahn "configuration table in ISA DLL\n");
106*d2201f2fSdrahn return 0;
107*d2201f2fSdrahn }
108*d2201f2fSdrahn
109*d2201f2fSdrahn return 1;
110*d2201f2fSdrahn }
111*d2201f2fSdrahn
112*d2201f2fSdrahn /* For subsequent modules, check that the parameters match. Note: This
113*d2201f2fSdrahn code is sufficient to handle the current model where there are never
114*d2201f2fSdrahn more than 2 modules; we might at some point want to handle cases where
115*d2201f2fSdrahn module N > 0 specifies some parameters not included in the base table,
116*d2201f2fSdrahn and we would then add those to isa->config so that subsequent modules
117*d2201f2fSdrahn would check against them. */
118*d2201f2fSdrahn
119*d2201f2fSdrahn for (i = 0; config_table[i].param_name; i++)
120*d2201f2fSdrahn {
121*d2201f2fSdrahn for (j = 0; isa->config[j].param_name; j++)
122*d2201f2fSdrahn {
123*d2201f2fSdrahn if (!strcmp (config_table[i].param_name, isa->config[j].param_name))
124*d2201f2fSdrahn {
125*d2201f2fSdrahn int mismatch;
126*d2201f2fSdrahn if (!strcmp (config_table[i].param_name, "IsaCoprocessorCount"))
127*d2201f2fSdrahn {
128*d2201f2fSdrahn /* Only require the coprocessor count to be <= the base. */
129*d2201f2fSdrahn int tiecnt = atoi (config_table[i].param_value);
130*d2201f2fSdrahn int basecnt = atoi (isa->config[j].param_value);
131*d2201f2fSdrahn mismatch = (tiecnt > basecnt);
132*d2201f2fSdrahn }
133*d2201f2fSdrahn else
134*d2201f2fSdrahn mismatch = strcmp (config_table[i].param_value,
135*d2201f2fSdrahn isa->config[j].param_value);
136*d2201f2fSdrahn if (mismatch)
137*d2201f2fSdrahn {
138*d2201f2fSdrahn #define MISMATCH_MESSAGE \
139*d2201f2fSdrahn "Error: Configuration mismatch in the \"%s\" parameter:\n\
140*d2201f2fSdrahn the configuration used when the TIE file was compiled had a value of\n\
141*d2201f2fSdrahn \"%s\", while the current configuration has a value of\n\
142*d2201f2fSdrahn \"%s\". Please rerun the TIE compiler with a matching\n\
143*d2201f2fSdrahn configuration.\n"
144*d2201f2fSdrahn fprintf (stderr, MISMATCH_MESSAGE,
145*d2201f2fSdrahn config_table[i].param_name,
146*d2201f2fSdrahn config_table[i].param_value,
147*d2201f2fSdrahn isa->config[j].param_value);
148*d2201f2fSdrahn return 0;
149*d2201f2fSdrahn }
150*d2201f2fSdrahn break;
151*d2201f2fSdrahn }
152*d2201f2fSdrahn }
153*d2201f2fSdrahn }
154*d2201f2fSdrahn
155*d2201f2fSdrahn return 1;
156*d2201f2fSdrahn }
157*d2201f2fSdrahn
158*d2201f2fSdrahn
159*d2201f2fSdrahn static int
xtensa_add_isa(xtensa_isa_internal * isa,libisa_module_specifier libisa)160*d2201f2fSdrahn xtensa_add_isa (xtensa_isa_internal *isa, libisa_module_specifier libisa)
161*d2201f2fSdrahn {
162*d2201f2fSdrahn int (*get_num_opcodes_fn) (void);
163*d2201f2fSdrahn struct config_struct *(*get_config_table_fn) (void);
164*d2201f2fSdrahn xtensa_opcode_internal **(*get_opcodes_fn) (void);
165*d2201f2fSdrahn int (*decode_insn_fn) (const xtensa_insnbuf);
166*d2201f2fSdrahn xtensa_opcode_internal **opcodes;
167*d2201f2fSdrahn int opc, insn_size, prev_num_opcodes, new_num_opcodes, this_module;
168*d2201f2fSdrahn
169*d2201f2fSdrahn get_num_opcodes_fn = xtensa_isa_modules[libisa].get_num_opcodes_fn;
170*d2201f2fSdrahn get_opcodes_fn = xtensa_isa_modules[libisa].get_opcodes_fn;
171*d2201f2fSdrahn decode_insn_fn = xtensa_isa_modules[libisa].decode_insn_fn;
172*d2201f2fSdrahn get_config_table_fn = xtensa_isa_modules[libisa].get_config_table_fn;
173*d2201f2fSdrahn
174*d2201f2fSdrahn if (!get_num_opcodes_fn || !get_opcodes_fn || !decode_insn_fn
175*d2201f2fSdrahn || (!get_config_table_fn && isa->num_modules == 0))
176*d2201f2fSdrahn return 0;
177*d2201f2fSdrahn
178*d2201f2fSdrahn if (get_config_table_fn
179*d2201f2fSdrahn && !xtensa_check_isa_config (isa, get_config_table_fn ()))
180*d2201f2fSdrahn return 0;
181*d2201f2fSdrahn
182*d2201f2fSdrahn prev_num_opcodes = isa->num_opcodes;
183*d2201f2fSdrahn new_num_opcodes = (*get_num_opcodes_fn) ();
184*d2201f2fSdrahn
185*d2201f2fSdrahn isa->num_opcodes += new_num_opcodes;
186*d2201f2fSdrahn isa->opcode_table = (xtensa_opcode_internal **)
187*d2201f2fSdrahn realloc (isa->opcode_table, isa->num_opcodes *
188*d2201f2fSdrahn sizeof (xtensa_opcode_internal *));
189*d2201f2fSdrahn isa->opname_lookup_table = (opname_lookup_entry *)
190*d2201f2fSdrahn realloc (isa->opname_lookup_table, isa->num_opcodes *
191*d2201f2fSdrahn sizeof (opname_lookup_entry));
192*d2201f2fSdrahn
193*d2201f2fSdrahn opcodes = (*get_opcodes_fn) ();
194*d2201f2fSdrahn
195*d2201f2fSdrahn insn_size = isa->insn_size;
196*d2201f2fSdrahn for (opc = 0; opc < new_num_opcodes; opc++)
197*d2201f2fSdrahn {
198*d2201f2fSdrahn xtensa_opcode_internal *intopc = opcodes[opc];
199*d2201f2fSdrahn int newopc = prev_num_opcodes + opc;
200*d2201f2fSdrahn isa->opcode_table[newopc] = intopc;
201*d2201f2fSdrahn isa->opname_lookup_table[newopc].key = intopc->name;
202*d2201f2fSdrahn isa->opname_lookup_table[newopc].opcode = newopc;
203*d2201f2fSdrahn if (intopc->length > insn_size)
204*d2201f2fSdrahn insn_size = intopc->length;
205*d2201f2fSdrahn }
206*d2201f2fSdrahn
207*d2201f2fSdrahn isa->insn_size = insn_size;
208*d2201f2fSdrahn isa->insnbuf_size = ((isa->insn_size + sizeof (xtensa_insnbuf_word) - 1) /
209*d2201f2fSdrahn sizeof (xtensa_insnbuf_word));
210*d2201f2fSdrahn
211*d2201f2fSdrahn qsort (isa->opname_lookup_table, isa->num_opcodes,
212*d2201f2fSdrahn sizeof (opname_lookup_entry), opname_lookup_compare);
213*d2201f2fSdrahn
214*d2201f2fSdrahn /* Check for duplicate opcode names. */
215*d2201f2fSdrahn for (opc = 1; opc < isa->num_opcodes; opc++)
216*d2201f2fSdrahn {
217*d2201f2fSdrahn if (!opname_lookup_compare (&isa->opname_lookup_table[opc-1],
218*d2201f2fSdrahn &isa->opname_lookup_table[opc]))
219*d2201f2fSdrahn {
220*d2201f2fSdrahn fprintf (stderr, "Error: Duplicate TIE opcode \"%s\"\n",
221*d2201f2fSdrahn isa->opname_lookup_table[opc].key);
222*d2201f2fSdrahn return 0;
223*d2201f2fSdrahn }
224*d2201f2fSdrahn }
225*d2201f2fSdrahn
226*d2201f2fSdrahn this_module = isa->num_modules;
227*d2201f2fSdrahn isa->num_modules += 1;
228*d2201f2fSdrahn
229*d2201f2fSdrahn isa->module_opcode_base = (int *) realloc (isa->module_opcode_base,
230*d2201f2fSdrahn isa->num_modules * sizeof (int));
231*d2201f2fSdrahn isa->module_decode_fn = (xtensa_insn_decode_fn *)
232*d2201f2fSdrahn realloc (isa->module_decode_fn, isa->num_modules *
233*d2201f2fSdrahn sizeof (xtensa_insn_decode_fn));
234*d2201f2fSdrahn
235*d2201f2fSdrahn isa->module_opcode_base[this_module] = prev_num_opcodes;
236*d2201f2fSdrahn isa->module_decode_fn[this_module] = decode_insn_fn;
237*d2201f2fSdrahn
238*d2201f2fSdrahn xtensa_default_isa = isa;
239*d2201f2fSdrahn
240*d2201f2fSdrahn return 1; /* Library was successfully added. */
241*d2201f2fSdrahn }
242*d2201f2fSdrahn
243*d2201f2fSdrahn
244*d2201f2fSdrahn xtensa_isa
xtensa_load_isa(libisa_module_specifier libisa)245*d2201f2fSdrahn xtensa_load_isa (libisa_module_specifier libisa)
246*d2201f2fSdrahn {
247*d2201f2fSdrahn xtensa_isa_internal *isa;
248*d2201f2fSdrahn
249*d2201f2fSdrahn isa = (xtensa_isa_internal *) malloc (sizeof (xtensa_isa_internal));
250*d2201f2fSdrahn memset (isa, 0, sizeof (xtensa_isa_internal));
251*d2201f2fSdrahn if (!xtensa_add_isa (isa, libisa))
252*d2201f2fSdrahn {
253*d2201f2fSdrahn xtensa_isa_free (isa);
254*d2201f2fSdrahn return NULL;
255*d2201f2fSdrahn }
256*d2201f2fSdrahn return (xtensa_isa) isa;
257*d2201f2fSdrahn }
258*d2201f2fSdrahn
259*d2201f2fSdrahn
260*d2201f2fSdrahn int
xtensa_extend_isa(xtensa_isa isa,libisa_module_specifier libisa)261*d2201f2fSdrahn xtensa_extend_isa (xtensa_isa isa, libisa_module_specifier libisa)
262*d2201f2fSdrahn {
263*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
264*d2201f2fSdrahn return xtensa_add_isa (intisa, libisa);
265*d2201f2fSdrahn }
266*d2201f2fSdrahn
267*d2201f2fSdrahn
268*d2201f2fSdrahn void
xtensa_isa_free(xtensa_isa isa)269*d2201f2fSdrahn xtensa_isa_free (xtensa_isa isa)
270*d2201f2fSdrahn {
271*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
272*d2201f2fSdrahn if (intisa->opcode_table)
273*d2201f2fSdrahn free (intisa->opcode_table);
274*d2201f2fSdrahn if (intisa->opname_lookup_table)
275*d2201f2fSdrahn free (intisa->opname_lookup_table);
276*d2201f2fSdrahn if (intisa->module_opcode_base)
277*d2201f2fSdrahn free (intisa->module_opcode_base);
278*d2201f2fSdrahn if (intisa->module_decode_fn)
279*d2201f2fSdrahn free (intisa->module_decode_fn);
280*d2201f2fSdrahn free (intisa);
281*d2201f2fSdrahn }
282*d2201f2fSdrahn
283*d2201f2fSdrahn
284*d2201f2fSdrahn int
xtensa_insn_maxlength(xtensa_isa isa)285*d2201f2fSdrahn xtensa_insn_maxlength (xtensa_isa isa)
286*d2201f2fSdrahn {
287*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
288*d2201f2fSdrahn return intisa->insn_size;
289*d2201f2fSdrahn }
290*d2201f2fSdrahn
291*d2201f2fSdrahn
292*d2201f2fSdrahn int
xtensa_insnbuf_size(xtensa_isa isa)293*d2201f2fSdrahn xtensa_insnbuf_size (xtensa_isa isa)
294*d2201f2fSdrahn {
295*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *)isa;
296*d2201f2fSdrahn return intisa->insnbuf_size;
297*d2201f2fSdrahn }
298*d2201f2fSdrahn
299*d2201f2fSdrahn
300*d2201f2fSdrahn int
xtensa_num_opcodes(xtensa_isa isa)301*d2201f2fSdrahn xtensa_num_opcodes (xtensa_isa isa)
302*d2201f2fSdrahn {
303*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
304*d2201f2fSdrahn return intisa->num_opcodes;
305*d2201f2fSdrahn }
306*d2201f2fSdrahn
307*d2201f2fSdrahn
308*d2201f2fSdrahn xtensa_opcode
xtensa_opcode_lookup(xtensa_isa isa,const char * opname)309*d2201f2fSdrahn xtensa_opcode_lookup (xtensa_isa isa, const char *opname)
310*d2201f2fSdrahn {
311*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
312*d2201f2fSdrahn opname_lookup_entry entry, *result;
313*d2201f2fSdrahn
314*d2201f2fSdrahn entry.key = opname;
315*d2201f2fSdrahn result = bsearch (&entry, intisa->opname_lookup_table, intisa->num_opcodes,
316*d2201f2fSdrahn sizeof (opname_lookup_entry), opname_lookup_compare);
317*d2201f2fSdrahn if (!result) return XTENSA_UNDEFINED;
318*d2201f2fSdrahn return result->opcode;
319*d2201f2fSdrahn }
320*d2201f2fSdrahn
321*d2201f2fSdrahn
322*d2201f2fSdrahn xtensa_opcode
xtensa_decode_insn(xtensa_isa isa,const xtensa_insnbuf insn)323*d2201f2fSdrahn xtensa_decode_insn (xtensa_isa isa, const xtensa_insnbuf insn)
324*d2201f2fSdrahn {
325*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
326*d2201f2fSdrahn int n, opc;
327*d2201f2fSdrahn for (n = 0; n < intisa->num_modules; n++) {
328*d2201f2fSdrahn opc = (intisa->module_decode_fn[n]) (insn);
329*d2201f2fSdrahn if (opc != XTENSA_UNDEFINED)
330*d2201f2fSdrahn return intisa->module_opcode_base[n] + opc;
331*d2201f2fSdrahn }
332*d2201f2fSdrahn return XTENSA_UNDEFINED;
333*d2201f2fSdrahn }
334*d2201f2fSdrahn
335*d2201f2fSdrahn
336*d2201f2fSdrahn /* Opcode information. */
337*d2201f2fSdrahn
338*d2201f2fSdrahn void
xtensa_encode_insn(xtensa_isa isa,xtensa_opcode opc,xtensa_insnbuf insn)339*d2201f2fSdrahn xtensa_encode_insn (xtensa_isa isa, xtensa_opcode opc, xtensa_insnbuf insn)
340*d2201f2fSdrahn {
341*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
342*d2201f2fSdrahn xtensa_insnbuf template = intisa->opcode_table[opc]->template();
343*d2201f2fSdrahn int len = intisa->opcode_table[opc]->length;
344*d2201f2fSdrahn int n;
345*d2201f2fSdrahn
346*d2201f2fSdrahn /* Convert length to 32-bit words. */
347*d2201f2fSdrahn len = (len + 3) / 4;
348*d2201f2fSdrahn
349*d2201f2fSdrahn /* Copy the template. */
350*d2201f2fSdrahn for (n = 0; n < len; n++)
351*d2201f2fSdrahn insn[n] = template[n];
352*d2201f2fSdrahn
353*d2201f2fSdrahn /* Fill any unused buffer space with zeros. */
354*d2201f2fSdrahn for ( ; n < intisa->insnbuf_size; n++)
355*d2201f2fSdrahn insn[n] = 0;
356*d2201f2fSdrahn }
357*d2201f2fSdrahn
358*d2201f2fSdrahn
359*d2201f2fSdrahn const char *
xtensa_opcode_name(xtensa_isa isa,xtensa_opcode opc)360*d2201f2fSdrahn xtensa_opcode_name (xtensa_isa isa, xtensa_opcode opc)
361*d2201f2fSdrahn {
362*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
363*d2201f2fSdrahn return intisa->opcode_table[opc]->name;
364*d2201f2fSdrahn }
365*d2201f2fSdrahn
366*d2201f2fSdrahn
367*d2201f2fSdrahn int
xtensa_insn_length(xtensa_isa isa,xtensa_opcode opc)368*d2201f2fSdrahn xtensa_insn_length (xtensa_isa isa, xtensa_opcode opc)
369*d2201f2fSdrahn {
370*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
371*d2201f2fSdrahn return intisa->opcode_table[opc]->length;
372*d2201f2fSdrahn }
373*d2201f2fSdrahn
374*d2201f2fSdrahn
375*d2201f2fSdrahn int
xtensa_insn_length_from_first_byte(xtensa_isa isa,char first_byte)376*d2201f2fSdrahn xtensa_insn_length_from_first_byte (xtensa_isa isa, char first_byte)
377*d2201f2fSdrahn {
378*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
379*d2201f2fSdrahn int is_density = (first_byte & (intisa->is_big_endian ? 0x80 : 0x08)) != 0;
380*d2201f2fSdrahn return (intisa->has_density && is_density ? 2 : 3);
381*d2201f2fSdrahn }
382*d2201f2fSdrahn
383*d2201f2fSdrahn
384*d2201f2fSdrahn int
xtensa_num_operands(xtensa_isa isa,xtensa_opcode opc)385*d2201f2fSdrahn xtensa_num_operands (xtensa_isa isa, xtensa_opcode opc)
386*d2201f2fSdrahn {
387*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
388*d2201f2fSdrahn return intisa->opcode_table[opc]->iclass->num_operands;
389*d2201f2fSdrahn }
390*d2201f2fSdrahn
391*d2201f2fSdrahn
392*d2201f2fSdrahn xtensa_operand
xtensa_get_operand(xtensa_isa isa,xtensa_opcode opc,int opnd)393*d2201f2fSdrahn xtensa_get_operand (xtensa_isa isa, xtensa_opcode opc, int opnd)
394*d2201f2fSdrahn {
395*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
396*d2201f2fSdrahn xtensa_iclass_internal *iclass = intisa->opcode_table[opc]->iclass;
397*d2201f2fSdrahn if (opnd >= iclass->num_operands)
398*d2201f2fSdrahn return NULL;
399*d2201f2fSdrahn return (xtensa_operand) iclass->operands[opnd];
400*d2201f2fSdrahn }
401*d2201f2fSdrahn
402*d2201f2fSdrahn
403*d2201f2fSdrahn /* Operand information. */
404*d2201f2fSdrahn
405*d2201f2fSdrahn char *
xtensa_operand_kind(xtensa_operand opnd)406*d2201f2fSdrahn xtensa_operand_kind (xtensa_operand opnd)
407*d2201f2fSdrahn {
408*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
409*d2201f2fSdrahn return intop->operand_kind;
410*d2201f2fSdrahn }
411*d2201f2fSdrahn
412*d2201f2fSdrahn
413*d2201f2fSdrahn char
xtensa_operand_inout(xtensa_operand opnd)414*d2201f2fSdrahn xtensa_operand_inout (xtensa_operand opnd)
415*d2201f2fSdrahn {
416*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
417*d2201f2fSdrahn return intop->inout;
418*d2201f2fSdrahn }
419*d2201f2fSdrahn
420*d2201f2fSdrahn
421*d2201f2fSdrahn uint32
xtensa_operand_get_field(xtensa_operand opnd,const xtensa_insnbuf insn)422*d2201f2fSdrahn xtensa_operand_get_field (xtensa_operand opnd, const xtensa_insnbuf insn)
423*d2201f2fSdrahn {
424*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
425*d2201f2fSdrahn return (*intop->get_field) (insn);
426*d2201f2fSdrahn }
427*d2201f2fSdrahn
428*d2201f2fSdrahn
429*d2201f2fSdrahn void
xtensa_operand_set_field(xtensa_operand opnd,xtensa_insnbuf insn,uint32 val)430*d2201f2fSdrahn xtensa_operand_set_field (xtensa_operand opnd, xtensa_insnbuf insn, uint32 val)
431*d2201f2fSdrahn {
432*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
433*d2201f2fSdrahn return (*intop->set_field) (insn, val);
434*d2201f2fSdrahn }
435*d2201f2fSdrahn
436*d2201f2fSdrahn
437*d2201f2fSdrahn xtensa_encode_result
xtensa_operand_encode(xtensa_operand opnd,uint32 * valp)438*d2201f2fSdrahn xtensa_operand_encode (xtensa_operand opnd, uint32 *valp)
439*d2201f2fSdrahn {
440*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
441*d2201f2fSdrahn return (*intop->encode) (valp);
442*d2201f2fSdrahn }
443*d2201f2fSdrahn
444*d2201f2fSdrahn
445*d2201f2fSdrahn uint32
xtensa_operand_decode(xtensa_operand opnd,uint32 val)446*d2201f2fSdrahn xtensa_operand_decode (xtensa_operand opnd, uint32 val)
447*d2201f2fSdrahn {
448*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
449*d2201f2fSdrahn return (*intop->decode) (val);
450*d2201f2fSdrahn }
451*d2201f2fSdrahn
452*d2201f2fSdrahn
453*d2201f2fSdrahn int
xtensa_operand_isPCRelative(xtensa_operand opnd)454*d2201f2fSdrahn xtensa_operand_isPCRelative (xtensa_operand opnd)
455*d2201f2fSdrahn {
456*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
457*d2201f2fSdrahn return intop->isPCRelative;
458*d2201f2fSdrahn }
459*d2201f2fSdrahn
460*d2201f2fSdrahn
461*d2201f2fSdrahn uint32
xtensa_operand_do_reloc(xtensa_operand opnd,uint32 addr,uint32 pc)462*d2201f2fSdrahn xtensa_operand_do_reloc (xtensa_operand opnd, uint32 addr, uint32 pc)
463*d2201f2fSdrahn {
464*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
465*d2201f2fSdrahn if (!intop->isPCRelative)
466*d2201f2fSdrahn return addr;
467*d2201f2fSdrahn return (*intop->do_reloc) (addr, pc);
468*d2201f2fSdrahn }
469*d2201f2fSdrahn
470*d2201f2fSdrahn
471*d2201f2fSdrahn uint32
xtensa_operand_undo_reloc(xtensa_operand opnd,uint32 offset,uint32 pc)472*d2201f2fSdrahn xtensa_operand_undo_reloc (xtensa_operand opnd, uint32 offset, uint32 pc)
473*d2201f2fSdrahn {
474*d2201f2fSdrahn xtensa_operand_internal *intop = (xtensa_operand_internal *) opnd;
475*d2201f2fSdrahn if (!intop->isPCRelative)
476*d2201f2fSdrahn return offset;
477*d2201f2fSdrahn return (*intop->undo_reloc) (offset, pc);
478*d2201f2fSdrahn }
479*d2201f2fSdrahn
480*d2201f2fSdrahn
481*d2201f2fSdrahn /* Instruction buffers. */
482*d2201f2fSdrahn
483*d2201f2fSdrahn xtensa_insnbuf
xtensa_insnbuf_alloc(xtensa_isa isa)484*d2201f2fSdrahn xtensa_insnbuf_alloc (xtensa_isa isa)
485*d2201f2fSdrahn {
486*d2201f2fSdrahn return (xtensa_insnbuf) malloc (xtensa_insnbuf_size (isa) *
487*d2201f2fSdrahn sizeof (xtensa_insnbuf_word));
488*d2201f2fSdrahn }
489*d2201f2fSdrahn
490*d2201f2fSdrahn
491*d2201f2fSdrahn void
xtensa_insnbuf_free(xtensa_insnbuf buf)492*d2201f2fSdrahn xtensa_insnbuf_free (xtensa_insnbuf buf)
493*d2201f2fSdrahn {
494*d2201f2fSdrahn free( buf );
495*d2201f2fSdrahn }
496*d2201f2fSdrahn
497*d2201f2fSdrahn
498*d2201f2fSdrahn /* Given <byte_index>, the index of a byte in a xtensa_insnbuf, our
499*d2201f2fSdrahn internal representation of a xtensa instruction word, return the index of
500*d2201f2fSdrahn its word and the bit index of its low order byte in the xtensa_insnbuf. */
501*d2201f2fSdrahn
502*d2201f2fSdrahn static inline int
byte_to_word_index(int byte_index)503*d2201f2fSdrahn byte_to_word_index (int byte_index)
504*d2201f2fSdrahn {
505*d2201f2fSdrahn return byte_index / sizeof (xtensa_insnbuf_word);
506*d2201f2fSdrahn }
507*d2201f2fSdrahn
508*d2201f2fSdrahn
509*d2201f2fSdrahn static inline int
byte_to_bit_index(int byte_index)510*d2201f2fSdrahn byte_to_bit_index (int byte_index)
511*d2201f2fSdrahn {
512*d2201f2fSdrahn return (byte_index & 0x3) * 8;
513*d2201f2fSdrahn }
514*d2201f2fSdrahn
515*d2201f2fSdrahn
516*d2201f2fSdrahn /* Copy an instruction in the 32 bit words pointed at by <insn> to characters
517*d2201f2fSdrahn pointed at by <cp>. This is more complicated than you might think because
518*d2201f2fSdrahn we want 16 bit instructions in bytes 2,3 for big endian. This function
519*d2201f2fSdrahn allows us to specify which byte in <insn> to start with and which way to
520*d2201f2fSdrahn increment, allowing trivial implementation for both big and little endian.
521*d2201f2fSdrahn And it seems to make pretty good code for both. */
522*d2201f2fSdrahn
523*d2201f2fSdrahn void
xtensa_insnbuf_to_chars(xtensa_isa isa,const xtensa_insnbuf insn,char * cp)524*d2201f2fSdrahn xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn, char *cp)
525*d2201f2fSdrahn {
526*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
527*d2201f2fSdrahn int insn_size = xtensa_insn_maxlength (intisa);
528*d2201f2fSdrahn int fence_post, start, increment, i, byte_count;
529*d2201f2fSdrahn xtensa_opcode opc;
530*d2201f2fSdrahn
531*d2201f2fSdrahn if (intisa->is_big_endian)
532*d2201f2fSdrahn {
533*d2201f2fSdrahn start = insn_size - 1;
534*d2201f2fSdrahn increment = -1;
535*d2201f2fSdrahn }
536*d2201f2fSdrahn else
537*d2201f2fSdrahn {
538*d2201f2fSdrahn start = 0;
539*d2201f2fSdrahn increment = 1;
540*d2201f2fSdrahn }
541*d2201f2fSdrahn
542*d2201f2fSdrahn /* Find the opcode; do nothing if the buffer does not contain a valid
543*d2201f2fSdrahn instruction since we need to know how many bytes to copy. */
544*d2201f2fSdrahn opc = xtensa_decode_insn (isa, insn);
545*d2201f2fSdrahn if (opc == XTENSA_UNDEFINED)
546*d2201f2fSdrahn return;
547*d2201f2fSdrahn
548*d2201f2fSdrahn byte_count = xtensa_insn_length (isa, opc);
549*d2201f2fSdrahn fence_post = start + (byte_count * increment);
550*d2201f2fSdrahn
551*d2201f2fSdrahn for (i = start; i != fence_post; i += increment, ++cp)
552*d2201f2fSdrahn {
553*d2201f2fSdrahn int word_inx = byte_to_word_index (i);
554*d2201f2fSdrahn int bit_inx = byte_to_bit_index (i);
555*d2201f2fSdrahn
556*d2201f2fSdrahn *cp = (insn[word_inx] >> bit_inx) & 0xff;
557*d2201f2fSdrahn }
558*d2201f2fSdrahn }
559*d2201f2fSdrahn
560*d2201f2fSdrahn /* Inward conversion from byte stream to xtensa_insnbuf. See
561*d2201f2fSdrahn xtensa_insnbuf_to_chars for a discussion of why this is
562*d2201f2fSdrahn complicated by endianness. */
563*d2201f2fSdrahn
564*d2201f2fSdrahn void
xtensa_insnbuf_from_chars(xtensa_isa isa,xtensa_insnbuf insn,const char * cp)565*d2201f2fSdrahn xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn, const char* cp)
566*d2201f2fSdrahn {
567*d2201f2fSdrahn xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
568*d2201f2fSdrahn int insn_size = xtensa_insn_maxlength (intisa);
569*d2201f2fSdrahn int fence_post, start, increment, i;
570*d2201f2fSdrahn
571*d2201f2fSdrahn if (intisa->is_big_endian)
572*d2201f2fSdrahn {
573*d2201f2fSdrahn start = insn_size - 1;
574*d2201f2fSdrahn increment = -1;
575*d2201f2fSdrahn }
576*d2201f2fSdrahn else
577*d2201f2fSdrahn {
578*d2201f2fSdrahn start = 0;
579*d2201f2fSdrahn increment = 1;
580*d2201f2fSdrahn }
581*d2201f2fSdrahn
582*d2201f2fSdrahn fence_post = start + (insn_size * increment);
583*d2201f2fSdrahn memset (insn, 0, xtensa_insnbuf_size (isa) * sizeof (xtensa_insnbuf_word));
584*d2201f2fSdrahn
585*d2201f2fSdrahn for ( i = start; i != fence_post; i += increment, ++cp )
586*d2201f2fSdrahn {
587*d2201f2fSdrahn int word_inx = byte_to_word_index (i);
588*d2201f2fSdrahn int bit_inx = byte_to_bit_index (i);
589*d2201f2fSdrahn
590*d2201f2fSdrahn insn[word_inx] |= (*cp & 0xff) << bit_inx;
591*d2201f2fSdrahn }
592*d2201f2fSdrahn }
593*d2201f2fSdrahn
594