sysinfo.y 8.0 KB

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