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