sysinfo.y 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* Copyright (C) 2001-2022 Free Software Foundation, Inc.
  2. Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
  3. This file is part of GNU binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. %{
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static char writecode;
  21. static char *it;
  22. static int code;
  23. static char * repeat;
  24. static char *oldrepeat;
  25. static char *name;
  26. static int rdepth;
  27. static char *names[] = {" ","[n]","[n][m]"};
  28. static char *pnames[]= {"","*","**"};
  29. static void yyerror (const char *s);
  30. extern int yylex (void);
  31. %}
  32. %union {
  33. int i;
  34. char *s;
  35. }
  36. %token COND
  37. %token REPEAT
  38. %token '(' ')'
  39. %token <s> TYPE
  40. %token <s> NAME
  41. %token <i> NUMBER UNIT
  42. %type <i> attr_size
  43. %type <s> attr_desc attr_id attr_type
  44. %%
  45. top: {
  46. switch (writecode)
  47. {
  48. case 'i':
  49. printf("#ifdef SYSROFF_SWAP_IN\n");
  50. break;
  51. case 'p':
  52. printf("#ifdef SYSROFF_p\n");
  53. break;
  54. case 'd':
  55. break;
  56. case 'g':
  57. printf("#ifdef SYSROFF_SWAP_OUT\n");
  58. break;
  59. case 'c':
  60. printf("#ifdef SYSROFF_PRINT\n");
  61. printf("#include <stdio.h>\n");
  62. printf("#include <stdlib.h>\n");
  63. printf("#include <ansidecl.h>\n");
  64. break;
  65. }
  66. }
  67. it_list {
  68. switch (writecode) {
  69. case 'i':
  70. case 'p':
  71. case 'g':
  72. case 'c':
  73. printf("#endif\n");
  74. break;
  75. case 'd':
  76. break;
  77. }
  78. }
  79. ;
  80. it_list: it it_list
  81. |
  82. ;
  83. it:
  84. '(' NAME NUMBER
  85. {
  86. it = $2; code = $3;
  87. switch (writecode)
  88. {
  89. case 'd':
  90. printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
  91. printf("struct IT_%s;\n", it);
  92. printf("extern void sysroff_swap_%s_in (struct IT_%s *);\n",
  93. $2, it);
  94. printf("extern void sysroff_swap_%s_out (FILE *, struct IT_%s *);\n",
  95. $2, it);
  96. printf("extern void sysroff_print_%s_out (struct IT_%s *);\n",
  97. $2, it);
  98. printf("struct IT_%s { \n", it);
  99. break;
  100. case 'i':
  101. printf("void sysroff_swap_%s_in (struct IT_%s * ptr)\n",$2,it);
  102. printf("{\n");
  103. printf("\tunsigned char raw[255];\n");
  104. printf("\tint idx = 0;\n");
  105. printf("\tint size;\n");
  106. printf("\tmemset(raw,0,255);\n");
  107. printf("\tmemset(ptr,0,sizeof(*ptr));\n");
  108. printf("\tsize = fillup(raw);\n");
  109. break;
  110. case 'g':
  111. printf("void sysroff_swap_%s_out (FILE * ffile, struct IT_%s * ptr)\n",$2,it);
  112. printf("{\n");
  113. printf("\tunsigned char raw[255];\n");
  114. printf("\tint idx = 16;\n");
  115. printf("\tmemset (raw, 0, 255);\n");
  116. printf("\tcode = IT_%s_CODE;\n", it);
  117. break;
  118. case 'o':
  119. printf("void sysroff_swap_%s_out (bfd * abfd, struct IT_%s * ptr)\n",$2, it);
  120. printf("{\n");
  121. printf("\tint idx = 0;\n");
  122. break;
  123. case 'c':
  124. printf("void sysroff_print_%s_out (struct IT_%s *ptr)\n",$2,it);
  125. printf("{\n");
  126. printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
  127. break;
  128. case 't':
  129. break;
  130. }
  131. }
  132. it_field_list
  133. ')'
  134. {
  135. switch (writecode) {
  136. case 'd':
  137. printf("};\n");
  138. break;
  139. case 'g':
  140. printf("\tchecksum(ffile,raw, idx, IT_%s_CODE);\n", it);
  141. /* Fall through. */
  142. case 'i':
  143. case 'o':
  144. case 'c':
  145. printf("}\n");
  146. }
  147. free (it);
  148. }
  149. ;
  150. it_field_list:
  151. it_field it_field_list
  152. | cond_it_field it_field_list
  153. | repeat_it_field it_field_list
  154. |
  155. ;
  156. repeat_it_field: '(' REPEAT NAME
  157. {
  158. rdepth++;
  159. switch (writecode)
  160. {
  161. case 'c':
  162. if (rdepth==1)
  163. printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
  164. if (rdepth==2)
  165. printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
  166. /* Fall through. */
  167. case 'i':
  168. case 'g':
  169. case 'o':
  170. if (rdepth==1)
  171. {
  172. printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3);
  173. }
  174. if (rdepth == 2) {
  175. printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3);
  176. }
  177. break;
  178. }
  179. oldrepeat = repeat;
  180. repeat = $3;
  181. }
  182. it_field_list ')'
  183. {
  184. free (repeat);
  185. repeat = oldrepeat;
  186. oldrepeat =0;
  187. rdepth--;
  188. switch (writecode)
  189. {
  190. case 'i':
  191. case 'g':
  192. case 'o':
  193. case 'c':
  194. printf("\t}}\n");
  195. }
  196. }
  197. ;
  198. cond_it_field: '(' COND NAME
  199. {
  200. switch (writecode)
  201. {
  202. case 'i':
  203. case 'g':
  204. case 'o':
  205. case 'c':
  206. printf("\tif (%s) {\n", $3);
  207. break;
  208. }
  209. free ($3);
  210. }
  211. it_field_list ')'
  212. {
  213. switch (writecode)
  214. {
  215. case 'i':
  216. case 'g':
  217. case 'o':
  218. case 'c':
  219. printf("\t}\n");
  220. }
  221. }
  222. ;
  223. it_field:
  224. '(' attr_desc '(' attr_type attr_size ')' attr_id
  225. {name = $7; }
  226. enums ')'
  227. {
  228. char *desc = $2;
  229. char *type = $4;
  230. int size = $5;
  231. char *id = $7;
  232. char *p = names[rdepth];
  233. char *ptr = pnames[rdepth];
  234. switch (writecode)
  235. {
  236. case 'g':
  237. if (size % 8)
  238. {
  239. printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
  240. id,
  241. names[rdepth], size);
  242. }
  243. else {
  244. printf("\twrite%s(ptr->%s%s,raw,&idx,%d,ffile);\n",
  245. type,
  246. id,
  247. names[rdepth],size/8);
  248. }
  249. break;
  250. case 'i':
  251. {
  252. if (rdepth >= 1)
  253. {
  254. printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n",
  255. id,
  256. id,
  257. type,
  258. repeat,
  259. id);
  260. }
  261. if (rdepth == 2)
  262. {
  263. printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n",
  264. id,
  265. id,
  266. type,
  267. repeat,
  268. id);
  269. }
  270. }
  271. if (size % 8)
  272. {
  273. printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
  274. id,
  275. names[rdepth],
  276. size);
  277. }
  278. else {
  279. printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
  280. id,
  281. names[rdepth],
  282. type,
  283. size/8);
  284. }
  285. break;
  286. case 'o':
  287. printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
  288. break;
  289. case 'd':
  290. if (repeat)
  291. printf("\t/* repeat %s */\n", repeat);
  292. if (type[0] == 'I') {
  293. printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
  294. }
  295. else if (type[0] =='C') {
  296. printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
  297. }
  298. else {
  299. printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
  300. }
  301. break;
  302. case 'c':
  303. printf("tabout();\n");
  304. printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
  305. if (type[0] == 'I')
  306. printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
  307. else if (type[0] == 'C')
  308. printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
  309. else if (type[0] == 'B')
  310. {
  311. printf("\tpbarray(&ptr->%s%s);\n", id,p);
  312. }
  313. else abort();
  314. break;
  315. }
  316. free (desc);
  317. free (id);
  318. }
  319. ;
  320. attr_type:
  321. TYPE { $$ = $1; }
  322. | { $$ = "INT";}
  323. ;
  324. attr_desc:
  325. '(' NAME ')'
  326. { $$ = $2; }
  327. ;
  328. attr_size:
  329. NUMBER UNIT
  330. { $$ = $1 * $2; }
  331. ;
  332. attr_id:
  333. '(' NAME ')' { $$ = $2; }
  334. | { $$ = strdup ("dummy");}
  335. ;
  336. enums:
  337. | '(' enum_list ')' ;
  338. enum_list:
  339. |
  340. enum_list '(' NAME NAME ')' {
  341. switch (writecode)
  342. {
  343. case 'd':
  344. printf("#define %s %s\n", $3,$4);
  345. break;
  346. case 'c':
  347. printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
  348. }
  349. free ($3);
  350. free ($4);
  351. }
  352. ;
  353. %%
  354. /* four modes
  355. -d write structure definitions for sysroff in host format
  356. -i write functions to swap into sysroff format in
  357. -o write functions to swap into sysroff format out
  358. -c write code to print info in human form */
  359. int yydebug;
  360. int
  361. main (int ac, char **av)
  362. {
  363. yydebug=0;
  364. if (ac > 1)
  365. writecode = av[1][1];
  366. if (writecode == 'd')
  367. {
  368. printf("typedef struct { unsigned char *data; int len; } barray; \n");
  369. printf("typedef int INT;\n");
  370. printf("typedef char * CHARS;\n");
  371. }
  372. yyparse();
  373. return 0;
  374. }
  375. static void
  376. yyerror (const char *s)
  377. {
  378. fprintf(stderr, "%s\n" , s);
  379. }