2002-11-21 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / sim / igen / misc.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3 Copyright 2002 Free Software Foundation, Inc.
4
5 Contributed by Andrew Cagney.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24
25
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29
30 #include "config.h"
31 #include "misc.h"
32
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #else
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
43 #endif
44
45 /* NB: Because warning and error can be interchanged, neither append a
46 trailing '\n' */
47
48 void
49 error (const line_ref *line,
50 char *msg,
51 ...)
52 {
53 va_list ap;
54 if (line != NULL)
55 fprintf (stderr, "%s:%d: ", line->file_name, line->line_nr);
56 va_start (ap, msg);
57 vfprintf (stderr, msg, ap);
58 va_end (ap);
59 exit (1);
60 }
61
62 void
63 warning (const line_ref *line,
64 char *msg,
65 ...)
66 {
67 va_list ap;
68 if (line != NULL)
69 fprintf (stderr, "%s:%d: warning: ", line->file_name, line->line_nr);
70 va_start (ap, msg);
71 vfprintf (stderr, msg, ap);
72 va_end (ap);
73 }
74
75 void
76 notify (const line_ref *line,
77 char *msg,
78 ...)
79 {
80 va_list ap;
81 if (line != NULL)
82 fprintf (stdout, "%s %d: info: ", line->file_name, line->line_nr);
83 va_start(ap, msg);
84 vfprintf (stdout, msg, ap);
85 va_end(ap);
86 }
87
88 void *
89 zalloc(long size)
90 {
91 void *memory = malloc(size);
92 if (memory == NULL)
93 ERROR ("zalloc failed");
94 memset(memory, 0, size);
95 return memory;
96 }
97
98
99 unsigned long long
100 a2i (const char *a)
101 {
102 int neg = 0;
103 int base = 10;
104 unsigned long long num = 0;
105 int looping;
106
107 while (isspace (*a))
108 a++;
109
110 if (strcmp (a, "true") == 0
111 || strcmp (a, "TRUE") == 0)
112 return 1;
113
114 if (strcmp (a, "false") == 0
115 || strcmp (a, "false") == 0)
116 return 0;
117
118 if (*a == '-')
119 {
120 neg = 1;
121 a++;
122 }
123
124 if (*a == '0')
125 {
126 if (a[1] == 'x' || a[1] == 'X')
127 {
128 a += 2;
129 base = 16;
130 }
131 else if (a[1] == 'b' || a[1] == 'b')
132 {
133 a += 2;
134 base = 2;
135 }
136 else
137 base = 8;
138 }
139
140 looping = 1;
141 while (looping)
142 {
143 int ch = *a++;
144
145 switch (base)
146 {
147 default:
148 looping = 0;
149 break;
150
151 case 2:
152 if (ch >= '0' && ch <= '1')
153 {
154 num = (num * 2) + (ch - '0');
155 }
156 else
157 {
158 looping = 0;
159 }
160 break;
161
162 case 10:
163 if (ch >= '0' && ch <= '9')
164 {
165 num = (num * 10) + (ch - '0');
166 }
167 else
168 {
169 looping = 0;
170 }
171 break;
172
173 case 8:
174 if (ch >= '0' && ch <= '7')
175 {
176 num = (num * 8) + (ch - '0');
177 }
178 else
179 {
180 looping = 0;
181 }
182 break;
183
184 case 16:
185 if (ch >= '0' && ch <= '9')
186 {
187 num = (num * 16) + (ch - '0');
188 }
189 else if (ch >= 'a' && ch <= 'f')
190 {
191 num = (num * 16) + (ch - 'a' + 10);
192 }
193 else if (ch >= 'A' && ch <= 'F')
194 {
195 num = (num * 16) + (ch - 'A' + 10);
196 }
197 else
198 {
199 looping = 0;
200 }
201 break;
202 }
203 }
204
205 if (neg)
206 num = - num;
207
208 return num;
209 }
210
211 unsigned
212 target_a2i(int ms_bit_nr,
213 const char *a)
214 {
215 if (ms_bit_nr)
216 return (ms_bit_nr - a2i(a));
217 else
218 return a2i(a);
219 }
220
221 unsigned
222 i2target(int ms_bit_nr,
223 unsigned bit)
224 {
225 if (ms_bit_nr)
226 return ms_bit_nr - bit;
227 else
228 return bit;
229 }
230
231
232 int
233 name2i (const char *names,
234 const name_map *map)
235 {
236 const name_map *curr;
237 const char *name = names;
238 while (*name != '\0')
239 {
240 /* find our name */
241 char *end = strchr(name, ',');
242 char *next;
243 unsigned len;
244 if (end == NULL)
245 {
246 end = strchr(name, '\0');
247 next = end;
248 }
249 else
250 {
251 next = end + 1;
252 }
253 len = end - name;
254 /* look it up */
255 curr = map;
256 while (curr->name != NULL)
257 {
258 if (strncmp (curr->name, name, len) == 0
259 && strlen (curr->name) == len)
260 return curr->i;
261 curr++;
262 }
263 name = next;
264 }
265 /* nothing found, possibly return a default */
266 curr = map;
267 while (curr->name != NULL)
268 curr++;
269 if (curr->i >= 0)
270 return curr->i;
271 else
272 error (NULL, "%s contains no valid names", names);
273 return 0;
274 }
275
276 const char *
277 i2name (const int i,
278 const name_map *map)
279 {
280 while (map->name != NULL)
281 {
282 if (map->i == i)
283 return map->name;
284 map++;
285 }
286 error (NULL, "map lookup failed for %d\n", i);
287 return NULL;
288 }
This page took 0.038159 seconds and 4 git commands to generate.