]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/igen/misc.c
sim: clean up C11 header includes
[thirdparty/binutils-gdb.git] / sim / igen / misc.c
CommitLineData
feaee4bd
AC
1/* The IGEN simulator generator for GDB, the GNU Debugger.
2
3666a048 3 Copyright 2002-2021 Free Software Foundation, Inc.
feaee4bd
AC
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
4744ac1b 11 the Free Software Foundation; either version 3 of the License, or
feaee4bd
AC
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
4744ac1b 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
feaee4bd 21
c906108c
SS
22
23
24#include <stdio.h>
25#include <stdarg.h>
26#include <ctype.h>
27
28#include "config.h"
29#include "misc.h"
30
c906108c 31#include <stdlib.h>
c906108c 32#include <string.h>
c906108c
SS
33
34/* NB: Because warning and error can be interchanged, neither append a
35 trailing '\n' */
36
37void
4e0bf4c4 38error (const line_ref *line, char *msg, ...)
c906108c
SS
39{
40 va_list ap;
41 if (line != NULL)
42 fprintf (stderr, "%s:%d: ", line->file_name, line->line_nr);
43 va_start (ap, msg);
44 vfprintf (stderr, msg, ap);
45 va_end (ap);
46 exit (1);
47}
48
49void
4e0bf4c4 50warning (const line_ref *line, char *msg, ...)
c906108c
SS
51{
52 va_list ap;
53 if (line != NULL)
54 fprintf (stderr, "%s:%d: warning: ", line->file_name, line->line_nr);
55 va_start (ap, msg);
56 vfprintf (stderr, msg, ap);
57 va_end (ap);
58}
59
60void
4e0bf4c4 61notify (const line_ref *line, char *msg, ...)
c906108c
SS
62{
63 va_list ap;
64 if (line != NULL)
65 fprintf (stdout, "%s %d: info: ", line->file_name, line->line_nr);
4e0bf4c4 66 va_start (ap, msg);
c906108c 67 vfprintf (stdout, msg, ap);
4e0bf4c4 68 va_end (ap);
c906108c
SS
69}
70
71void *
4e0bf4c4 72zalloc (long size)
c906108c 73{
4e0bf4c4 74 void *memory = malloc (size);
c906108c
SS
75 if (memory == NULL)
76 ERROR ("zalloc failed");
4e0bf4c4 77 memset (memory, 0, size);
c906108c
SS
78 return memory;
79}
80
81
82unsigned long long
83a2i (const char *a)
84{
85 int neg = 0;
86 int base = 10;
87 unsigned long long num = 0;
88 int looping;
4e0bf4c4 89
c906108c
SS
90 while (isspace (*a))
91 a++;
4e0bf4c4
AC
92
93 if (strcmp (a, "true") == 0 || strcmp (a, "TRUE") == 0)
c906108c
SS
94 return 1;
95
de7669bf 96 if (strcmp (a, "false") == 0 || strcmp (a, "FALSE") == 0)
c906108c
SS
97 return 0;
98
99 if (*a == '-')
100 {
101 neg = 1;
102 a++;
103 }
4e0bf4c4 104
c906108c
SS
105 if (*a == '0')
106 {
107 if (a[1] == 'x' || a[1] == 'X')
108 {
109 a += 2;
110 base = 16;
111 }
de7669bf 112 else if (a[1] == 'b' || a[1] == 'B')
c906108c
SS
113 {
114 a += 2;
115 base = 2;
116 }
117 else
118 base = 8;
119 }
4e0bf4c4 120
c906108c
SS
121 looping = 1;
122 while (looping)
123 {
124 int ch = *a++;
4e0bf4c4 125
c906108c
SS
126 switch (base)
127 {
128 default:
129 looping = 0;
130 break;
4e0bf4c4 131
c906108c
SS
132 case 2:
133 if (ch >= '0' && ch <= '1')
134 {
135 num = (num * 2) + (ch - '0');
136 }
137 else
138 {
139 looping = 0;
140 }
141 break;
4e0bf4c4 142
c906108c
SS
143 case 10:
144 if (ch >= '0' && ch <= '9')
145 {
146 num = (num * 10) + (ch - '0');
147 }
148 else
149 {
150 looping = 0;
151 }
152 break;
4e0bf4c4 153
c906108c
SS
154 case 8:
155 if (ch >= '0' && ch <= '7')
156 {
157 num = (num * 8) + (ch - '0');
158 }
159 else
160 {
161 looping = 0;
162 }
163 break;
4e0bf4c4 164
c906108c
SS
165 case 16:
166 if (ch >= '0' && ch <= '9')
167 {
168 num = (num * 16) + (ch - '0');
169 }
170 else if (ch >= 'a' && ch <= 'f')
171 {
172 num = (num * 16) + (ch - 'a' + 10);
173 }
174 else if (ch >= 'A' && ch <= 'F')
175 {
176 num = (num * 16) + (ch - 'A' + 10);
177 }
178 else
179 {
180 looping = 0;
181 }
182 break;
183 }
184 }
4e0bf4c4 185
c906108c 186 if (neg)
4e0bf4c4 187 num = -num;
c906108c
SS
188
189 return num;
190}
191
192unsigned
4e0bf4c4 193target_a2i (int ms_bit_nr, const char *a)
c906108c
SS
194{
195 if (ms_bit_nr)
4e0bf4c4 196 return (ms_bit_nr - a2i (a));
c906108c 197 else
4e0bf4c4 198 return a2i (a);
c906108c
SS
199}
200
201unsigned
4e0bf4c4 202i2target (int ms_bit_nr, unsigned bit)
c906108c
SS
203{
204 if (ms_bit_nr)
205 return ms_bit_nr - bit;
206 else
207 return bit;
208}
209
210
211int
4e0bf4c4 212name2i (const char *names, const name_map * map)
c906108c
SS
213{
214 const name_map *curr;
215 const char *name = names;
216 while (*name != '\0')
217 {
218 /* find our name */
4e0bf4c4 219 char *end = strchr (name, ',');
c906108c
SS
220 char *next;
221 unsigned len;
222 if (end == NULL)
223 {
4e0bf4c4 224 end = strchr (name, '\0');
c906108c
SS
225 next = end;
226 }
227 else
228 {
229 next = end + 1;
230 }
231 len = end - name;
232 /* look it up */
233 curr = map;
234 while (curr->name != NULL)
235 {
236 if (strncmp (curr->name, name, len) == 0
237 && strlen (curr->name) == len)
238 return curr->i;
239 curr++;
240 }
241 name = next;
242 }
243 /* nothing found, possibly return a default */
244 curr = map;
245 while (curr->name != NULL)
246 curr++;
247 if (curr->i >= 0)
248 return curr->i;
249 else
250 error (NULL, "%s contains no valid names", names);
251 return 0;
252}
253
254const char *
4e0bf4c4 255i2name (const int i, const name_map * map)
c906108c
SS
256{
257 while (map->name != NULL)
258 {
259 if (map->i == i)
260 return map->name;
261 map++;
262 }
263 error (NULL, "map lookup failed for %d\n", i);
264 return NULL;
265}