xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/arc-ext.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* ARC target-dependent stuff. Extension structure access functions
2*3d8817e4Smiod    Copyright 1995, 1997, 2000, 2001, 2004, 2005
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of GDB.
6*3d8817e4Smiod 
7*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod    (at your option) any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with this program; if not, write to the Free Software
19*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20*3d8817e4Smiod 
21*3d8817e4Smiod #include "sysdep.h"
22*3d8817e4Smiod #include <stdlib.h>
23*3d8817e4Smiod #include <stdio.h>
24*3d8817e4Smiod #include "bfd.h"
25*3d8817e4Smiod #include "arc-ext.h"
26*3d8817e4Smiod #include "libiberty.h"
27*3d8817e4Smiod 
28*3d8817e4Smiod /* Extension structure  */
29*3d8817e4Smiod static struct arcExtMap arc_extension_map;
30*3d8817e4Smiod 
31*3d8817e4Smiod /* Get the name of an extension instruction.  */
32*3d8817e4Smiod 
33*3d8817e4Smiod const char *
arcExtMap_instName(int opcode,int minor,int * flags)34*3d8817e4Smiod arcExtMap_instName(int opcode, int minor, int *flags)
35*3d8817e4Smiod {
36*3d8817e4Smiod     if (opcode == 3)
37*3d8817e4Smiod       {
38*3d8817e4Smiod 	/* FIXME: ??? need to also check 0/1/2 in bit0 for (3f) brk/sleep/swi  */
39*3d8817e4Smiod 	if (minor < 0x09 || minor == 0x3f)
40*3d8817e4Smiod 	  return 0;
41*3d8817e4Smiod 	else
42*3d8817e4Smiod 	  opcode = 0x1f - 0x10 + minor - 0x09 + 1;
43*3d8817e4Smiod       }
44*3d8817e4Smiod     else
45*3d8817e4Smiod       if (opcode < 0x10)
46*3d8817e4Smiod 	return 0;
47*3d8817e4Smiod     else
48*3d8817e4Smiod       opcode -= 0x10;
49*3d8817e4Smiod     if (!arc_extension_map.instructions[opcode])
50*3d8817e4Smiod       return 0;
51*3d8817e4Smiod     *flags = arc_extension_map.instructions[opcode]->flags;
52*3d8817e4Smiod     return arc_extension_map.instructions[opcode]->name;
53*3d8817e4Smiod }
54*3d8817e4Smiod 
55*3d8817e4Smiod /* Get the name of an extension core register.  */
56*3d8817e4Smiod 
57*3d8817e4Smiod const char *
arcExtMap_coreRegName(int value)58*3d8817e4Smiod arcExtMap_coreRegName(int value)
59*3d8817e4Smiod {
60*3d8817e4Smiod   if (value < 32)
61*3d8817e4Smiod     return 0;
62*3d8817e4Smiod   return arc_extension_map.coreRegisters[value-32];
63*3d8817e4Smiod }
64*3d8817e4Smiod 
65*3d8817e4Smiod /* Get the name of an extension condition code.  */
66*3d8817e4Smiod 
67*3d8817e4Smiod const char *
arcExtMap_condCodeName(int value)68*3d8817e4Smiod arcExtMap_condCodeName(int value)
69*3d8817e4Smiod {
70*3d8817e4Smiod   if (value < 16)
71*3d8817e4Smiod     return 0;
72*3d8817e4Smiod   return arc_extension_map.condCodes[value-16];
73*3d8817e4Smiod }
74*3d8817e4Smiod 
75*3d8817e4Smiod /* Get the name of an extension aux register.  */
76*3d8817e4Smiod 
77*3d8817e4Smiod const char *
arcExtMap_auxRegName(long address)78*3d8817e4Smiod arcExtMap_auxRegName(long address)
79*3d8817e4Smiod {
80*3d8817e4Smiod   /* walk the list of aux reg names and find the name  */
81*3d8817e4Smiod   struct ExtAuxRegister *r;
82*3d8817e4Smiod 
83*3d8817e4Smiod   for (r = arc_extension_map.auxRegisters; r; r = r->next) {
84*3d8817e4Smiod     if (r->address == address)
85*3d8817e4Smiod       return (const char *) r->name;
86*3d8817e4Smiod   }
87*3d8817e4Smiod   return 0;
88*3d8817e4Smiod }
89*3d8817e4Smiod 
90*3d8817e4Smiod /* Recursively free auxilliary register strcture pointers until
91*3d8817e4Smiod    the list is empty.  */
92*3d8817e4Smiod 
93*3d8817e4Smiod static void
clean_aux_registers(struct ExtAuxRegister * r)94*3d8817e4Smiod clean_aux_registers(struct ExtAuxRegister *r)
95*3d8817e4Smiod {
96*3d8817e4Smiod   if (r -> next)
97*3d8817e4Smiod     {
98*3d8817e4Smiod       clean_aux_registers( r->next);
99*3d8817e4Smiod       free(r -> name);
100*3d8817e4Smiod       free(r -> next);
101*3d8817e4Smiod       r ->next = NULL;
102*3d8817e4Smiod     }
103*3d8817e4Smiod   else
104*3d8817e4Smiod     free(r -> name);
105*3d8817e4Smiod }
106*3d8817e4Smiod 
107*3d8817e4Smiod /* Free memory that has been allocated for the extensions.  */
108*3d8817e4Smiod 
109*3d8817e4Smiod static void
cleanup_ext_map(void)110*3d8817e4Smiod cleanup_ext_map(void)
111*3d8817e4Smiod {
112*3d8817e4Smiod   struct ExtAuxRegister *r;
113*3d8817e4Smiod   struct ExtInstruction *insn;
114*3d8817e4Smiod   int i;
115*3d8817e4Smiod 
116*3d8817e4Smiod   /* clean aux reg structure  */
117*3d8817e4Smiod   r = arc_extension_map.auxRegisters;
118*3d8817e4Smiod   if (r)
119*3d8817e4Smiod     {
120*3d8817e4Smiod       (clean_aux_registers(r));
121*3d8817e4Smiod       free(r);
122*3d8817e4Smiod     }
123*3d8817e4Smiod 
124*3d8817e4Smiod   /* clean instructions  */
125*3d8817e4Smiod   for (i = 0; i < NUM_EXT_INST; i++)
126*3d8817e4Smiod     {
127*3d8817e4Smiod       insn = arc_extension_map.instructions[i];
128*3d8817e4Smiod       if (insn)
129*3d8817e4Smiod 	free(insn->name);
130*3d8817e4Smiod     }
131*3d8817e4Smiod 
132*3d8817e4Smiod   /* clean core reg struct  */
133*3d8817e4Smiod   for (i = 0; i < NUM_EXT_CORE; i++)
134*3d8817e4Smiod     {
135*3d8817e4Smiod       if (arc_extension_map.coreRegisters[i])
136*3d8817e4Smiod 	free(arc_extension_map.coreRegisters[i]);
137*3d8817e4Smiod     }
138*3d8817e4Smiod 
139*3d8817e4Smiod   for (i = 0; i < NUM_EXT_COND; i++) {
140*3d8817e4Smiod     if (arc_extension_map.condCodes[i])
141*3d8817e4Smiod       free(arc_extension_map.condCodes[i]);
142*3d8817e4Smiod   }
143*3d8817e4Smiod 
144*3d8817e4Smiod   memset(&arc_extension_map, 0, sizeof(struct arcExtMap));
145*3d8817e4Smiod }
146*3d8817e4Smiod 
147*3d8817e4Smiod int
arcExtMap_add(void * base,unsigned long length)148*3d8817e4Smiod arcExtMap_add(void *base, unsigned long length)
149*3d8817e4Smiod {
150*3d8817e4Smiod   unsigned char *block = base;
151*3d8817e4Smiod   unsigned char *p = block;
152*3d8817e4Smiod 
153*3d8817e4Smiod   /* Clean up and reset everything if needed.  */
154*3d8817e4Smiod   cleanup_ext_map();
155*3d8817e4Smiod 
156*3d8817e4Smiod   while (p && p < (block + length))
157*3d8817e4Smiod     {
158*3d8817e4Smiod       /* p[0] == length of record
159*3d8817e4Smiod 	 p[1] == type of record
160*3d8817e4Smiod 	 For instructions:
161*3d8817e4Smiod 	   p[2]  = opcode
162*3d8817e4Smiod 	   p[3]  = minor opcode (if opcode == 3)
163*3d8817e4Smiod 	   p[4]  = flags
164*3d8817e4Smiod 	   p[5]+ = name
165*3d8817e4Smiod 	 For core regs and condition codes:
166*3d8817e4Smiod 	   p[2]  = value
167*3d8817e4Smiod 	   p[3]+ = name
168*3d8817e4Smiod 	 For aux regs:
169*3d8817e4Smiod 	   p[2..5] = value
170*3d8817e4Smiod 	   p[6]+   = name
171*3d8817e4Smiod 	 (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5])  */
172*3d8817e4Smiod 
173*3d8817e4Smiod       if (p[0] == 0)
174*3d8817e4Smiod 	return -1;
175*3d8817e4Smiod 
176*3d8817e4Smiod       switch (p[1])
177*3d8817e4Smiod 	{
178*3d8817e4Smiod 	case EXT_INSTRUCTION:
179*3d8817e4Smiod 	  {
180*3d8817e4Smiod 	    char opcode = p[2];
181*3d8817e4Smiod 	    char minor  = p[3];
182*3d8817e4Smiod 	    char * insn_name = (char *) xmalloc(( (int)*p-5) * sizeof(char));
183*3d8817e4Smiod 	    struct ExtInstruction * insn =
184*3d8817e4Smiod 	      (struct ExtInstruction *) xmalloc(sizeof(struct ExtInstruction));
185*3d8817e4Smiod 
186*3d8817e4Smiod 	    if (opcode==3)
187*3d8817e4Smiod 	      opcode = 0x1f - 0x10 + minor - 0x09 + 1;
188*3d8817e4Smiod 	    else
189*3d8817e4Smiod 	      opcode -= 0x10;
190*3d8817e4Smiod 	    insn -> flags = (char) *(p+4);
191*3d8817e4Smiod 	    strcpy (insn_name, (char *) (p+5));
192*3d8817e4Smiod 	    insn -> name = insn_name;
193*3d8817e4Smiod 	    arc_extension_map.instructions[(int) opcode] = insn;
194*3d8817e4Smiod 	  }
195*3d8817e4Smiod 	  break;
196*3d8817e4Smiod 
197*3d8817e4Smiod 	case EXT_CORE_REGISTER:
198*3d8817e4Smiod 	  {
199*3d8817e4Smiod 	    char * core_name = (char *) xmalloc(((int)*p-3) * sizeof(char));
200*3d8817e4Smiod 
201*3d8817e4Smiod 	    strcpy(core_name, (char *) (p+3));
202*3d8817e4Smiod 	    arc_extension_map.coreRegisters[p[2]-32] = core_name;
203*3d8817e4Smiod 	  }
204*3d8817e4Smiod 	  break;
205*3d8817e4Smiod 
206*3d8817e4Smiod 	case EXT_COND_CODE:
207*3d8817e4Smiod 	  {
208*3d8817e4Smiod 	    char * cc_name = (char *) xmalloc( ((int)*p-3) * sizeof(char));
209*3d8817e4Smiod 	    strcpy(cc_name, (char *) (p+3));
210*3d8817e4Smiod 	    arc_extension_map.condCodes[p[2]-16] = cc_name;
211*3d8817e4Smiod 	  }
212*3d8817e4Smiod 	  break;
213*3d8817e4Smiod 
214*3d8817e4Smiod 	case EXT_AUX_REGISTER:
215*3d8817e4Smiod 	  {
216*3d8817e4Smiod 	    /* trickier -- need to store linked list to these  */
217*3d8817e4Smiod 	    struct ExtAuxRegister *newAuxRegister =
218*3d8817e4Smiod 	      (struct ExtAuxRegister *)malloc(sizeof(struct ExtAuxRegister));
219*3d8817e4Smiod 	    char * aux_name = (char *) xmalloc ( ((int)*p-6) * sizeof(char));
220*3d8817e4Smiod 
221*3d8817e4Smiod 	    strcpy (aux_name, (char *) (p+6));
222*3d8817e4Smiod 	    newAuxRegister->name = aux_name;
223*3d8817e4Smiod 	    newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8  | p[5];
224*3d8817e4Smiod 	    newAuxRegister->next = arc_extension_map.auxRegisters;
225*3d8817e4Smiod 	    arc_extension_map.auxRegisters = newAuxRegister;
226*3d8817e4Smiod 	  }
227*3d8817e4Smiod 	  break;
228*3d8817e4Smiod 
229*3d8817e4Smiod 	default:
230*3d8817e4Smiod 	  return -1;
231*3d8817e4Smiod 
232*3d8817e4Smiod 	}
233*3d8817e4Smiod       p += p[0]; /* move to next record  */
234*3d8817e4Smiod     }
235*3d8817e4Smiod 
236*3d8817e4Smiod   return 0;
237*3d8817e4Smiod }
238*3d8817e4Smiod 
239*3d8817e4Smiod /* Load hw extension descibed in .extArcMap ELF section.  */
240*3d8817e4Smiod 
241*3d8817e4Smiod void
build_ARC_extmap(text_bfd)242*3d8817e4Smiod build_ARC_extmap (text_bfd)
243*3d8817e4Smiod   bfd *text_bfd;
244*3d8817e4Smiod {
245*3d8817e4Smiod   char *arcExtMap;
246*3d8817e4Smiod   bfd_size_type count;
247*3d8817e4Smiod   asection *p;
248*3d8817e4Smiod 
249*3d8817e4Smiod   for (p = text_bfd->sections; p != NULL; p = p->next)
250*3d8817e4Smiod     if (!strcmp (p->name, ".arcextmap"))
251*3d8817e4Smiod       {
252*3d8817e4Smiod         count = bfd_get_section_size (p);
253*3d8817e4Smiod         arcExtMap = (char *) xmalloc (count);
254*3d8817e4Smiod         if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count))
255*3d8817e4Smiod           {
256*3d8817e4Smiod             arcExtMap_add ((PTR) arcExtMap, count);
257*3d8817e4Smiod             break;
258*3d8817e4Smiod           }
259*3d8817e4Smiod         free ((PTR) arcExtMap);
260*3d8817e4Smiod       }
261*3d8817e4Smiod }
262