misc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* The IGEN simulator generator for GDB, the GNU Debugger.
  2. Copyright 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Andrew Cagney.
  4. This file is part of GDB.
  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 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <ctype.h>
  18. #include "misc.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. /* NB: Because warning and error can be interchanged, neither append a
  22. trailing '\n' */
  23. void
  24. error (const line_ref *line, char *msg, ...)
  25. {
  26. va_list ap;
  27. if (line != NULL)
  28. fprintf (stderr, "%s:%d: ", line->file_name, line->line_nr);
  29. va_start (ap, msg);
  30. vfprintf (stderr, msg, ap);
  31. va_end (ap);
  32. exit (1);
  33. }
  34. void
  35. warning (const line_ref *line, char *msg, ...)
  36. {
  37. va_list ap;
  38. if (line != NULL)
  39. fprintf (stderr, "%s:%d: warning: ", line->file_name, line->line_nr);
  40. va_start (ap, msg);
  41. vfprintf (stderr, msg, ap);
  42. va_end (ap);
  43. }
  44. void
  45. notify (const line_ref *line, char *msg, ...)
  46. {
  47. va_list ap;
  48. if (line != NULL)
  49. fprintf (stdout, "%s %d: info: ", line->file_name, line->line_nr);
  50. va_start (ap, msg);
  51. vfprintf (stdout, msg, ap);
  52. va_end (ap);
  53. }
  54. void *
  55. zalloc (long size)
  56. {
  57. void *memory = malloc (size);
  58. if (memory == NULL)
  59. ERROR ("zalloc failed");
  60. memset (memory, 0, size);
  61. return memory;
  62. }
  63. unsigned long long
  64. a2i (const char *a)
  65. {
  66. int neg = 0;
  67. int base = 10;
  68. unsigned long long num = 0;
  69. int looping;
  70. while (isspace (*a))
  71. a++;
  72. if (strcmp (a, "true") == 0 || strcmp (a, "TRUE") == 0)
  73. return 1;
  74. if (strcmp (a, "false") == 0 || strcmp (a, "FALSE") == 0)
  75. return 0;
  76. if (*a == '-')
  77. {
  78. neg = 1;
  79. a++;
  80. }
  81. if (*a == '0')
  82. {
  83. if (a[1] == 'x' || a[1] == 'X')
  84. {
  85. a += 2;
  86. base = 16;
  87. }
  88. else if (a[1] == 'b' || a[1] == 'B')
  89. {
  90. a += 2;
  91. base = 2;
  92. }
  93. else
  94. base = 8;
  95. }
  96. looping = 1;
  97. while (looping)
  98. {
  99. int ch = *a++;
  100. switch (base)
  101. {
  102. default:
  103. looping = 0;
  104. break;
  105. case 2:
  106. if (ch >= '0' && ch <= '1')
  107. {
  108. num = (num * 2) + (ch - '0');
  109. }
  110. else
  111. {
  112. looping = 0;
  113. }
  114. break;
  115. case 10:
  116. if (ch >= '0' && ch <= '9')
  117. {
  118. num = (num * 10) + (ch - '0');
  119. }
  120. else
  121. {
  122. looping = 0;
  123. }
  124. break;
  125. case 8:
  126. if (ch >= '0' && ch <= '7')
  127. {
  128. num = (num * 8) + (ch - '0');
  129. }
  130. else
  131. {
  132. looping = 0;
  133. }
  134. break;
  135. case 16:
  136. if (ch >= '0' && ch <= '9')
  137. {
  138. num = (num * 16) + (ch - '0');
  139. }
  140. else if (ch >= 'a' && ch <= 'f')
  141. {
  142. num = (num * 16) + (ch - 'a' + 10);
  143. }
  144. else if (ch >= 'A' && ch <= 'F')
  145. {
  146. num = (num * 16) + (ch - 'A' + 10);
  147. }
  148. else
  149. {
  150. looping = 0;
  151. }
  152. break;
  153. }
  154. }
  155. if (neg)
  156. num = -num;
  157. return num;
  158. }
  159. unsigned
  160. target_a2i (int ms_bit_nr, const char *a)
  161. {
  162. if (ms_bit_nr)
  163. return (ms_bit_nr - a2i (a));
  164. else
  165. return a2i (a);
  166. }
  167. unsigned
  168. i2target (int ms_bit_nr, unsigned bit)
  169. {
  170. if (ms_bit_nr)
  171. return ms_bit_nr - bit;
  172. else
  173. return bit;
  174. }
  175. int
  176. name2i (const char *names, const name_map * map)
  177. {
  178. const name_map *curr;
  179. const char *name = names;
  180. while (*name != '\0')
  181. {
  182. /* find our name */
  183. char *end = strchr (name, ',');
  184. char *next;
  185. unsigned len;
  186. if (end == NULL)
  187. {
  188. end = strchr (name, '\0');
  189. next = end;
  190. }
  191. else
  192. {
  193. next = end + 1;
  194. }
  195. len = end - name;
  196. /* look it up */
  197. curr = map;
  198. while (curr->name != NULL)
  199. {
  200. if (strncmp (curr->name, name, len) == 0
  201. && strlen (curr->name) == len)
  202. return curr->i;
  203. curr++;
  204. }
  205. name = next;
  206. }
  207. /* nothing found, possibly return a default */
  208. curr = map;
  209. while (curr->name != NULL)
  210. curr++;
  211. if (curr->i >= 0)
  212. return curr->i;
  213. else
  214. error (NULL, "%s contains no valid names", names);
  215. return 0;
  216. }
  217. const char *
  218. i2name (const int i, const name_map * map)
  219. {
  220. while (map->name != NULL)
  221. {
  222. if (map->i == i)
  223. return map->name;
  224. map++;
  225. }
  226. error (NULL, "map lookup failed for %d\n", i);
  227. return NULL;
  228. }