2001-02-27 H.J. Lu <hjl@gnu.org>
[binutils.git] / opcodes / arc-ext.c
blob0b1067795a441f85e7fdb28037183f1e5106af29
1 /* ARC target-dependent stuff. Extension structure access functions
2 Copyright (C) 1995, 1997, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "bfd.h"
23 #include "arc-ext.h"
24 #include "libiberty.h"
26 /* Extension structure */
27 static struct arcExtMap arc_extension_map;
29 /* Get the name of an extension instruction. */
31 const char *
32 arcExtMap_instName(int opcode, int minor, int *flags)
34 if (opcode == 3)
36 /* FIXME: ??? need to also check 0/1/2 in bit0 for (3f) brk/sleep/swi */
37 if (minor < 0x09 || minor == 0x3f)
38 return 0;
39 else
40 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
42 else
43 if (opcode < 0x10)
44 return 0;
45 else
46 opcode -= 0x10;
47 if (!arc_extension_map.instructions[opcode])
48 return 0;
49 *flags = arc_extension_map.instructions[opcode]->flags;
50 return arc_extension_map.instructions[opcode]->name;
53 /* Get the name of an extension core register. */
55 const char *
56 arcExtMap_coreRegName(int value)
58 if (value < 32)
59 return 0;
60 return (const char *) arc_extension_map.coreRegisters[value-32];
63 /* Get the name of an extension condition code. */
65 const char *
66 arcExtMap_condCodeName(int value)
68 if (value < 16)
69 return 0;
70 return (const char *) arc_extension_map.condCodes[value-16];
73 /* Get the name of an extension aux register. */
75 const char *
76 arcExtMap_auxRegName(long address)
78 /* walk the list of aux reg names and find the name */
79 struct ExtAuxRegister *r;
81 for (r = arc_extension_map.auxRegisters; r; r = r->next) {
82 if (r->address == address)
83 return (const char *) r->name;
85 return 0;
88 /* Recursively free auxilliary register strcture pointers until
89 the list is empty. */
91 static void
92 clean_aux_registers(struct ExtAuxRegister *r)
94 if (r -> next)
96 clean_aux_registers( r->next);
97 free(r -> name);
98 free(r -> next);
99 r ->next = NULL;
101 else
102 free(r -> name);
105 /* Free memory that has been allocated for the extensions. */
107 static void
108 cleanup_ext_map(void)
110 struct ExtAuxRegister *r;
111 struct ExtInstruction *insn;
112 int i;
114 /* clean aux reg structure */
115 r = arc_extension_map.auxRegisters;
116 if (r)
118 (clean_aux_registers(r));
119 free(r);
122 /* clean instructions */
123 for (i = 0; i < NUM_EXT_INST; i++)
125 insn = arc_extension_map.instructions[i];
126 if (insn)
127 free(insn->name);
130 /* clean core reg struct */
131 for (i = 0; i < NUM_EXT_CORE; i++)
133 if (arc_extension_map.coreRegisters[i])
134 free(arc_extension_map.coreRegisters[i]);
137 for (i = 0; i < NUM_EXT_COND; i++) {
138 if (arc_extension_map.condCodes[i])
139 free(arc_extension_map.condCodes[i]);
142 memset(&arc_extension_map, 0, sizeof(struct arcExtMap));
145 int
146 arcExtMap_add(void *base, unsigned long length)
148 unsigned char *block = base;
149 unsigned char *p = block;
151 /* Clean up and reset everything if needed. */
152 cleanup_ext_map();
154 while (p && p < (block + length))
156 /* p[0] == length of record
157 p[1] == type of record
158 For instructions:
159 p[2] = opcode
160 p[3] = minor opcode (if opcode == 3)
161 p[4] = flags
162 p[5]+ = name
163 For core regs and condition codes:
164 p[2] = value
165 p[3]+ = name
166 For aux regs:
167 p[2..5] = value
168 p[6]+ = name
169 (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5]) */
171 if (p[0] == 0)
172 return -1;
174 switch (p[1])
176 case EXT_INSTRUCTION:
178 char opcode = p[2];
179 char minor = p[3];
180 char * insn_name = (char *) xmalloc(( (int)*p-5) * sizeof(char));
181 struct ExtInstruction * insn =
182 (struct ExtInstruction *) xmalloc(sizeof(struct ExtInstruction));
184 if (opcode==3)
185 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
186 else
187 opcode -= 0x10;
188 insn -> flags = (char) *(p+4);
189 strcpy(insn_name, (p+5));
190 insn -> name = insn_name;
191 arc_extension_map.instructions[(int) opcode] = insn;
193 break;
195 case EXT_CORE_REGISTER:
197 char * core_name = (char *) xmalloc(((int)*p-3) * sizeof(char));
199 strcpy(core_name, (p+3));
200 arc_extension_map.coreRegisters[p[2]-32] = core_name;
202 break;
204 case EXT_COND_CODE:
206 char * cc_name = (char *) xmalloc( ((int)*p-3) * sizeof(char));
207 strcpy(cc_name, (p+3));
208 arc_extension_map.condCodes[p[2]-16] = cc_name;
210 break;
212 case EXT_AUX_REGISTER:
214 /* trickier -- need to store linked list to these */
215 struct ExtAuxRegister *newAuxRegister =
216 (struct ExtAuxRegister *)malloc(sizeof(struct ExtAuxRegister));
217 char * aux_name = (char *) xmalloc ( ((int)*p-6) * sizeof(char));
219 strcpy (aux_name, (p+6));
220 newAuxRegister->name = aux_name;
221 newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5];
222 newAuxRegister->next = arc_extension_map.auxRegisters;
223 arc_extension_map.auxRegisters = newAuxRegister;
225 break;
227 default:
228 return -1;
231 p += p[0]; /* move to next record */
234 return 0;
237 /* Load hw extension descibed in .extArcMap ELF section. */
239 void
240 build_ARC_extmap (text_bfd)
241 bfd *text_bfd;
243 char *arcExtMap;
244 bfd_size_type count;
245 asection *p;
247 for (p = text_bfd->sections; p != NULL; p = p->next)
248 if (!strcmp (p->name, ".arcextmap"))
250 count = p->_raw_size;
251 arcExtMap = (char *) xmalloc (count);
252 if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count))
254 arcExtMap_add ((PTR) arcExtMap, count);
255 break;
257 free ((PTR) arcExtMap);