]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/igen/misc.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / sim / igen / misc.c
CommitLineData
a4c97499
AC
1/* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22#include <stdio.h>
23#include <stdarg.h>
24#include <ctype.h>
25
26#include "config.h"
27#include "misc.h"
28
29#ifdef HAVE_STDLIB_H
30#include <stdlib.h>
31#endif
32
33#ifdef HAVE_STRING_H
34#include <string.h>
35#else
36#ifdef HAVE_STRINGS_H
37#include <strings.h>
38#endif
39#endif
40
41void
42error (char *msg, ...)
43{
44 va_list ap;
45 va_start(ap, msg);
46 vprintf(msg, ap);
47 va_end(ap);
48 exit (1);
49}
50
51void *
52zalloc(long size)
53{
54 void *memory = malloc(size);
55 if (memory == NULL)
56 error("zalloc failed\n");
57 memset(memory, 0, size);
58 return memory;
59}
60
61void
62dumpf (int indent, char *msg, ...)
63{
64 va_list ap;
65 for (; indent > 0; indent--)
66 printf(" ");
67 va_start(ap, msg);
68 vprintf(msg, ap);
69 va_end(ap);
70}
71
72
73unsigned long long
74a2i(const char *a)
75{
76 int neg = 0;
77 int base = 10;
78 unsigned long long num = 0;
79 int looping;
80
81 while (isspace (*a))
82 a++;
83
84 if (*a == '-') {
85 neg = 1;
86 a++;
87 }
88
89 if (*a == '0') {
90 if (a[1] == 'x' || a[1] == 'X') {
91 a += 2;
92 base = 16;
93 } else if (a[1] == 'b' || a[1] == 'b') {
94 a += 2;
95 base = 2;
96 }
97 else
98 base = 8;
99 }
100
101 looping = 1;
102 while (looping) {
103 int ch = *a++;
104
105 switch (base) {
106 default:
107 looping = 0;
108 break;
109
110 case 2:
111 if (ch >= '0' && ch <= '1') {
112 num = (num * 2) + (ch - '0');
113 } else {
114 looping = 0;
115 }
116 break;
117
118 case 10:
119 if (ch >= '0' && ch <= '9') {
120 num = (num * 10) + (ch - '0');
121 } else {
122 looping = 0;
123 }
124 break;
125
126 case 8:
127 if (ch >= '0' && ch <= '7') {
128 num = (num * 8) + (ch - '0');
129 } else {
130 looping = 0;
131 }
132 break;
133
134 case 16:
135 if (ch >= '0' && ch <= '9') {
136 num = (num * 16) + (ch - '0');
137 } else if (ch >= 'a' && ch <= 'f') {
138 num = (num * 16) + (ch - 'a' + 10);
139 } else if (ch >= 'A' && ch <= 'F') {
140 num = (num * 16) + (ch - 'A' + 10);
141 } else {
142 looping = 0;
143 }
144 break;
145 }
146 }
147
148 if (neg)
149 num = - num;
150
151 return num;
152}
153
154unsigned
155target_a2i(int ms_bit_nr,
156 const char *a)
157{
158 if (ms_bit_nr)
159 return (ms_bit_nr - a2i(a));
160 else
161 return a2i(a);
162}
163
164unsigned
165i2target(int ms_bit_nr,
166 unsigned bit)
167{
168 if (ms_bit_nr)
169 return ms_bit_nr - bit;
170 else
171 return bit;
172}
173
174
175int
176name2i(const char *names,
177 const name_map *map)
178{
179 const name_map *curr;
180 const char *name = names;
181 while (*name != '\0') {
182 /* find our name */
183 char *end = strchr(name, ',');
184 char *next;
185 int len;
186 if (end == NULL) {
187 end = strchr(name, '\0');
188 next = end;
189 }
190 else {
191 next = end + 1;
192 }
193 len = end - name;
194 /* look it up */
195 curr = map;
196 while (curr->name != NULL) {
197 if (strncmp(curr->name, name, len) == 0
198 && strlen(curr->name) == len)
199 return curr->i;
200 curr++;
201 }
202 name = next;
203 }
204 /* nothing found, possibly return a default */
205 curr = map;
206 while (curr->name != NULL)
207 curr++;
208 if (curr->i >= 0)
209 return curr->i;
210 else
211 error("%s contains no valid names\n", names);
212 return 0;
213}
214
215const char *
216i2name(const int i,
217 const name_map *map)
218{
219 while (map->name != NULL) {
220 if (map->i == i)
221 return map->name;
222 map++;
223 }
224 error("map lookup failed for %d\n", i);
225 return NULL;
226}