lf.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "lf.h"
  20. #include <stdlib.h>
  21. #include <string.h>
  22. struct _lf
  23. {
  24. FILE *stream;
  25. int line_nr; /* nr complete lines written, curr line is line_nr+1 */
  26. int indent;
  27. int line_blank;
  28. const char *name;
  29. const char *program;
  30. lf_file_references references;
  31. lf_file_type type;
  32. };
  33. lf *
  34. lf_open (char *name,
  35. char *real_name,
  36. lf_file_references references,
  37. lf_file_type type, const char *program)
  38. {
  39. /* create a file object */
  40. lf *new_lf = ZALLOC (lf);
  41. ASSERT (new_lf != NULL);
  42. new_lf->references = references;
  43. new_lf->type = type;
  44. new_lf->name = (real_name == NULL ? name : real_name);
  45. new_lf->program = program;
  46. /* attach to stdout if pipe */
  47. if (!strcmp (name, "-"))
  48. {
  49. new_lf->stream = stdout;
  50. }
  51. else
  52. {
  53. /* create a new file */
  54. new_lf->stream = fopen (name, "w");
  55. if (new_lf->stream == NULL)
  56. {
  57. perror (name);
  58. exit (1);
  59. }
  60. }
  61. return new_lf;
  62. }
  63. lf_file_type
  64. lf_get_file_type (const lf *file)
  65. {
  66. return file->type;
  67. }
  68. void
  69. lf_close (lf *file)
  70. {
  71. if (file->stream != stdout)
  72. {
  73. if (fclose (file->stream))
  74. {
  75. perror ("lf_close.fclose");
  76. exit (1);
  77. }
  78. free (file);
  79. }
  80. }
  81. int
  82. lf_putchr (lf *file, const char chr)
  83. {
  84. int nr = 0;
  85. if (chr == '\n')
  86. {
  87. file->line_nr += 1;
  88. file->line_blank = 1;
  89. }
  90. else if (file->line_blank)
  91. {
  92. int pad;
  93. for (pad = file->indent; pad > 0; pad--)
  94. putc (' ', file->stream);
  95. nr += file->indent;
  96. file->line_blank = 0;
  97. }
  98. putc (chr, file->stream);
  99. nr += 1;
  100. return nr;
  101. }
  102. int
  103. lf_write (lf *file, const char *string, int strlen_string)
  104. {
  105. int nr = 0;
  106. int i;
  107. for (i = 0; i < strlen_string; i++)
  108. nr += lf_putchr (file, string[i]);
  109. return nr;
  110. }
  111. void
  112. lf_indent_suppress (lf *file)
  113. {
  114. file->line_blank = 0;
  115. }
  116. int
  117. lf_putstr (lf *file, const char *string)
  118. {
  119. int nr = 0;
  120. const char *chp;
  121. if (string != NULL)
  122. {
  123. for (chp = string; *chp != '\0'; chp++)
  124. {
  125. nr += lf_putchr (file, *chp);
  126. }
  127. }
  128. return nr;
  129. }
  130. static int
  131. do_lf_putunsigned (lf *file, unsigned u)
  132. {
  133. int nr = 0;
  134. if (u > 0)
  135. {
  136. nr += do_lf_putunsigned (file, u / 10);
  137. nr += lf_putchr (file, (u % 10) + '0');
  138. }
  139. return nr;
  140. }
  141. int
  142. lf_putint (lf *file, int decimal)
  143. {
  144. int nr = 0;
  145. if (decimal == 0)
  146. nr += lf_putchr (file, '0');
  147. else if (decimal < 0)
  148. {
  149. nr += lf_putchr (file, '-');
  150. nr += do_lf_putunsigned (file, -decimal);
  151. }
  152. else if (decimal > 0)
  153. {
  154. nr += do_lf_putunsigned (file, decimal);
  155. }
  156. else
  157. ASSERT (0);
  158. return nr;
  159. }
  160. int
  161. lf_printf (lf *file, const char *fmt, ...)
  162. {
  163. int nr = 0;
  164. char buf[1024];
  165. va_list ap;
  166. va_start (ap, fmt);
  167. vsprintf (buf, fmt, ap);
  168. /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
  169. ASSERT (strlen (buf) < sizeof (buf));
  170. nr += lf_putstr (file, buf);
  171. va_end (ap);
  172. return nr;
  173. }
  174. int
  175. lf_print__line_ref (lf *file, line_ref *line)
  176. {
  177. return lf_print__external_ref (file, line->line_nr, line->file_name);
  178. }
  179. int
  180. lf_print__external_ref (lf *file, int line_nr, const char *file_name)
  181. {
  182. int nr = 0;
  183. switch (file->references)
  184. {
  185. case lf_include_references:
  186. lf_indent_suppress (file);
  187. nr += lf_putstr (file, "#line ");
  188. nr += lf_putint (file, line_nr);
  189. nr += lf_putstr (file, " \"");
  190. nr += lf_putstr (file, file_name);
  191. nr += lf_putstr (file, "\"\n");
  192. break;
  193. case lf_omit_references:
  194. nr += lf_putstr (file, "/* ");
  195. nr += lf_putstr (file, file_name);
  196. nr += lf_putstr (file, ":");
  197. nr += lf_putint (file, line_nr);
  198. nr += lf_putstr (file, "*/\n");
  199. break;
  200. }
  201. return nr;
  202. }
  203. int
  204. lf_print__internal_ref (lf *file)
  205. {
  206. int nr = 0;
  207. nr += lf_print__external_ref (file, file->line_nr + 2, file->name);
  208. /* line_nr == last_line, want to number from next */
  209. return nr;
  210. }
  211. void
  212. lf_indent (lf *file, int delta)
  213. {
  214. file->indent += delta;
  215. }
  216. int
  217. lf_print__gnu_copyleft (lf *file)
  218. {
  219. int nr = 0;
  220. switch (file->type)
  221. {
  222. case lf_is_c:
  223. case lf_is_h:
  224. nr += lf_printf (file, "\
  225. /* This file is part of GDB.\n\
  226. \n\
  227. Copyright 2002, 2007 Free Software Foundation, Inc.\n\
  228. \n\
  229. This program is free software; you can redistribute it and/or modify\n\
  230. it under the terms of the GNU General Public License as published by\n\
  231. the Free Software Foundation; either version 3 of the License, or\n\
  232. (at your option) any later version.\n\
  233. \n\
  234. This program is distributed in the hope that it will be useful,\n\
  235. but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
  236. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
  237. GNU General Public License for more details.\n\
  238. \n\
  239. You should have received a copy of the GNU General Public License\n\
  240. along with this program. If not, see <http://www.gnu.org/licenses/>.\n\
  241. \n\
  242. --\n\
  243. \n\
  244. This file was generated by the program %s */\n\
  245. ", filter_filename (file->program));
  246. break;
  247. default:
  248. ASSERT (0);
  249. break;
  250. }
  251. return nr;
  252. }
  253. int
  254. lf_putbin (lf *file, int decimal, int width)
  255. {
  256. int nr = 0;
  257. int bit;
  258. ASSERT (width > 0);
  259. for (bit = 1 << (width - 1); bit != 0; bit >>= 1)
  260. {
  261. if (decimal & bit)
  262. nr += lf_putchr (file, '1');
  263. else
  264. nr += lf_putchr (file, '0');
  265. }
  266. return nr;
  267. }
  268. int
  269. lf_print__this_file_is_empty (lf *file, const char *reason)
  270. {
  271. int nr = 0;
  272. switch (file->type)
  273. {
  274. case lf_is_c:
  275. case lf_is_h:
  276. nr += lf_printf (file,
  277. "/* This generated file (%s) is intentionally left blank",
  278. file->name);
  279. if (reason != NULL)
  280. nr += lf_printf (file, " - %s", reason);
  281. nr += lf_printf (file, " */\n");
  282. break;
  283. default:
  284. ERROR ("Bad switch");
  285. }
  286. return nr;
  287. }
  288. int
  289. lf_print__ucase_filename (lf *file)
  290. {
  291. int nr = 0;
  292. const char *chp = file->name;
  293. while (*chp != '\0')
  294. {
  295. char ch = *chp;
  296. if (islower (ch))
  297. {
  298. nr += lf_putchr (file, toupper (ch));
  299. }
  300. else if (ch == '.')
  301. nr += lf_putchr (file, '_');
  302. else
  303. nr += lf_putchr (file, ch);
  304. chp++;
  305. }
  306. return nr;
  307. }
  308. int
  309. lf_print__file_start (lf *file)
  310. {
  311. int nr = 0;
  312. switch (file->type)
  313. {
  314. case lf_is_h:
  315. case lf_is_c:
  316. nr += lf_print__gnu_copyleft (file);
  317. nr += lf_printf (file, "\n");
  318. nr += lf_printf (file, "#ifndef ");
  319. nr += lf_print__ucase_filename (file);
  320. nr += lf_printf (file, "\n");
  321. nr += lf_printf (file, "#define ");
  322. nr += lf_print__ucase_filename (file);
  323. nr += lf_printf (file, "\n");
  324. nr += lf_printf (file, "\n");
  325. break;
  326. default:
  327. ASSERT (0);
  328. }
  329. return nr;
  330. }
  331. int
  332. lf_print__file_finish (lf *file)
  333. {
  334. int nr = 0;
  335. switch (file->type)
  336. {
  337. case lf_is_h:
  338. case lf_is_c:
  339. nr += lf_printf (file, "\n");
  340. nr += lf_printf (file, "#endif /* _");
  341. nr += lf_print__ucase_filename (file);
  342. nr += lf_printf (file, "_*/\n");
  343. break;
  344. default:
  345. ASSERT (0);
  346. }
  347. return nr;
  348. }
  349. int
  350. lf_print__function_type (lf *file,
  351. const char *type,
  352. const char *prefix, const char *trailing_space)
  353. {
  354. int nr = 0;
  355. nr += lf_printf (file, "%s\\\n(%s)", prefix, type);
  356. if (trailing_space != NULL)
  357. nr += lf_printf (file, "%s", trailing_space);
  358. return nr;
  359. }
  360. int
  361. lf_print__function_type_function (lf *file,
  362. print_function * print_type,
  363. const char *prefix,
  364. const char *trailing_space)
  365. {
  366. int nr = 0;
  367. nr += lf_printf (file, "%s\\\n(", prefix);
  368. nr += print_type (file);
  369. nr += lf_printf (file, ")");
  370. if (trailing_space != NULL)
  371. nr += lf_printf (file, "%s", trailing_space);
  372. return nr;
  373. }