More build fixes in opcodes
[binutils.git] / opcodes / arc-ext.c
blob1e6c1f8a7f85d75b920f895990e70d418f900440
1 /* ARC target-dependent stuff. Extension structure access functions
2 Copyright 1995, 1997, 2000, 2001, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This file is part of libopcodes.
7 This library 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 3, or (at your option)
10 any later version.
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 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,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "bfd.h"
26 #include "arc-ext.h"
27 #include "libiberty.h"
29 /* Extension structure */
30 static struct arcExtMap arc_extension_map;
32 /* Get the name of an extension instruction. */
34 const char *
35 arcExtMap_instName(int opcode, int minor, int *flags)
37 if (opcode == 3)
39 /* FIXME: ??? need to also check 0/1/2 in bit0 for (3f) brk/sleep/swi */
40 if (minor < 0x09 || minor == 0x3f)
41 return 0;
42 else
43 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
45 else
46 if (opcode < 0x10)
47 return 0;
48 else
49 opcode -= 0x10;
50 if (!arc_extension_map.instructions[opcode])
51 return 0;
52 *flags = arc_extension_map.instructions[opcode]->flags;
53 return arc_extension_map.instructions[opcode]->name;
56 /* Get the name of an extension core register. */
58 const char *
59 arcExtMap_coreRegName(int value)
61 if (value < 32)
62 return 0;
63 return arc_extension_map.coreRegisters[value-32];
66 /* Get the name of an extension condition code. */
68 const char *
69 arcExtMap_condCodeName(int value)
71 if (value < 16)
72 return 0;
73 return arc_extension_map.condCodes[value-16];
76 /* Get the name of an extension aux register. */
78 const char *
79 arcExtMap_auxRegName(long address)
81 /* walk the list of aux reg names and find the name */
82 struct ExtAuxRegister *r;
84 for (r = arc_extension_map.auxRegisters; r; r = r->next) {
85 if (r->address == address)
86 return (const char *) r->name;
88 return 0;
91 /* Recursively free auxilliary register strcture pointers until
92 the list is empty. */
94 static void
95 clean_aux_registers(struct ExtAuxRegister *r)
97 if (r -> next)
99 clean_aux_registers( r->next);
100 free(r -> name);
101 free(r -> next);
102 r ->next = NULL;
104 else
105 free(r -> name);
108 /* Free memory that has been allocated for the extensions. */
110 static void
111 cleanup_ext_map(void)
113 struct ExtAuxRegister *r;
114 struct ExtInstruction *insn;
115 int i;
117 /* clean aux reg structure */
118 r = arc_extension_map.auxRegisters;
119 if (r)
121 (clean_aux_registers(r));
122 free(r);
125 /* clean instructions */
126 for (i = 0; i < NUM_EXT_INST; i++)
128 insn = arc_extension_map.instructions[i];
129 if (insn)
130 free(insn->name);
133 /* clean core reg struct */
134 for (i = 0; i < NUM_EXT_CORE; i++)
136 if (arc_extension_map.coreRegisters[i])
137 free(arc_extension_map.coreRegisters[i]);
140 for (i = 0; i < NUM_EXT_COND; i++) {
141 if (arc_extension_map.condCodes[i])
142 free(arc_extension_map.condCodes[i]);
145 memset(&arc_extension_map, 0, sizeof(struct arcExtMap));
149 arcExtMap_add(void *base, unsigned long length)
151 unsigned char *block = base;
152 unsigned char *p = block;
154 /* Clean up and reset everything if needed. */
155 cleanup_ext_map();
157 while (p && p < (block + length))
159 /* p[0] == length of record
160 p[1] == type of record
161 For instructions:
162 p[2] = opcode
163 p[3] = minor opcode (if opcode == 3)
164 p[4] = flags
165 p[5]+ = name
166 For core regs and condition codes:
167 p[2] = value
168 p[3]+ = name
169 For aux regs:
170 p[2..5] = value
171 p[6]+ = name
172 (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5]) */
174 if (p[0] == 0)
175 return -1;
177 switch (p[1])
179 case EXT_INSTRUCTION:
181 char opcode = p[2];
182 char minor = p[3];
183 char * insn_name = (char *) xmalloc(( (int)*p-5) * sizeof(char));
184 struct ExtInstruction * insn =
185 (struct ExtInstruction *) xmalloc(sizeof(struct ExtInstruction));
187 if (opcode==3)
188 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
189 else
190 opcode -= 0x10;
191 insn -> flags = (char) *(p+4);
192 strcpy (insn_name, (char *) (p+5));
193 insn -> name = insn_name;
194 arc_extension_map.instructions[(int) opcode] = insn;
196 break;
198 case EXT_CORE_REGISTER:
200 char * core_name = (char *) xmalloc(((int)*p-3) * sizeof(char));
202 strcpy(core_name, (char *) (p+3));
203 arc_extension_map.coreRegisters[p[2]-32] = core_name;
205 break;
207 case EXT_COND_CODE:
209 char * cc_name = (char *) xmalloc( ((int)*p-3) * sizeof(char));
210 strcpy(cc_name, (char *) (p+3));
211 arc_extension_map.condCodes[p[2]-16] = cc_name;
213 break;
215 case EXT_AUX_REGISTER:
217 /* trickier -- need to store linked list to these */
218 struct ExtAuxRegister *newAuxRegister =
219 (struct ExtAuxRegister *)malloc(sizeof(struct ExtAuxRegister));
220 char * aux_name = (char *) xmalloc ( ((int)*p-6) * sizeof(char));
222 strcpy (aux_name, (char *) (p+6));
223 newAuxRegister->name = aux_name;
224 newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5];
225 newAuxRegister->next = arc_extension_map.auxRegisters;
226 arc_extension_map.auxRegisters = newAuxRegister;
228 break;
230 default:
231 return -1;
234 p += p[0]; /* move to next record */
237 return 0;
240 /* Load hw extension descibed in .extArcMap ELF section. */
242 void
243 build_ARC_extmap (text_bfd)
244 bfd *text_bfd;
246 char *arcExtMap;
247 bfd_size_type count;
248 asection *p;
250 for (p = text_bfd->sections; p != NULL; p = p->next)
251 if (!strcmp (p->name, ".arcextmap"))
253 count = bfd_get_section_size (p);
254 arcExtMap = (char *) xmalloc (count);
255 if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count))
257 arcExtMap_add ((PTR) arcExtMap, count);
258 break;
260 free ((PTR) arcExtMap);