2001-05-25 H.J. Lu <hjl@gnu.org>
[binutils.git] / binutils / sysinfo.y
blobfef16cbddc66062fa6822a975660ae756a97863b
1 /* Copyright 2001 Free Software Foundation, Inc.
2 Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
4 This file is part of GNU binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <stdio.h>
22 #include <stdlib.h>
24 extern char *word;
25 extern char writecode;
26 extern int number;
27 extern int unit;
28 char nice_name[1000];
29 char *it;
30 int sofar;
31 int width;
32 int code;
33 char * repeat;
34 char *oldrepeat;
35 char *name;
36 int rdepth;
37 char *loop [] = {"","n","m","/*BAD*/"};
38 char *names[] = {" ","[n]","[n][m]"};
39 char *pnames[]= {"","*","**"};
43 %union {
44 int i;
45 char *s;
47 %token COND
48 %token REPEAT
49 %token '(' ')'
50 %token <s> TYPE
51 %token <s> NAME
52 %token <i> NUMBER UNIT
53 %type <i> attr_size
54 %type <s> attr_desc attr_id attr_type
57 top: {
58 switch (writecode)
60 case 'i':
61 printf("#ifdef SYSROFF_SWAP_IN\n");
62 break;
63 case 'p':
64 printf("#ifdef SYSROFF_p\n");
65 break;
66 case 'd':
67 break;
68 case 'g':
69 printf("#ifdef SYSROFF_SWAP_OUT\n");
70 break;
71 case 'c':
72 printf("#ifdef SYSROFF_PRINT\n");
73 printf("#include <stdio.h>\n");
74 printf("#include <stdlib.h>\n");
75 break;
78 it_list {
79 switch (writecode) {
80 case 'i':
81 case 'p':
82 case 'g':
83 case 'c':
84 printf("#endif\n");
85 break;
86 case 'd':
87 break;
94 it_list: it it_list
98 it:
99 '(' NAME NUMBER
101 it = $2; code = $3;
102 switch (writecode)
104 case 'd':
105 printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
106 printf("struct IT_%s { \n", it);
107 break;
108 case 'i':
109 printf("void sysroff_swap_%s_in(ptr)\n",$2);
110 printf("struct IT_%s *ptr;\n", it);
111 printf("{\n");
112 printf("char raw[255];\n");
113 printf("\tint idx = 0 ;\n");
114 printf("\tint size;\n");
115 printf("memset(raw,0,255);\n");
116 printf("memset(ptr,0,sizeof(*ptr));\n");
117 printf("size = fillup(raw);\n");
118 break;
119 case 'g':
120 printf("void sysroff_swap_%s_out(file,ptr)\n",$2);
121 printf("FILE * file;\n");
122 printf("struct IT_%s *ptr;\n", it);
123 printf("{\n");
124 printf("\tchar raw[255];\n");
125 printf("\tint idx = 16 ;\n");
126 printf("\tmemset (raw, 0, 255);\n");
127 printf("\tcode = IT_%s_CODE;\n", it);
128 break;
129 case 'o':
130 printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2);
131 printf("bfd * abfd;\n");
132 printf("struct IT_%s *ptr;\n",it);
133 printf("{\n");
134 printf("int idx = 0 ;\n");
135 break;
136 case 'c':
137 printf("void sysroff_print_%s_out(ptr)\n",$2);
138 printf("struct IT_%s *ptr;\n", it);
139 printf("{\n");
140 printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
141 break;
143 case 't':
144 break;
148 it_field_list
151 switch (writecode) {
152 case 'd':
153 printf("};\n");
154 break;
155 case 'g':
156 printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it);
158 case 'i':
160 case 'o':
161 case 'c':
162 printf("}\n");
169 it_field_list:
170 it_field it_field_list
171 | cond_it_field it_field_list
172 | repeat_it_field it_field_list
176 repeat_it_field: '(' REPEAT NAME
178 rdepth++;
179 switch (writecode)
181 case 'c':
182 if (rdepth==1)
183 printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
184 if (rdepth==2)
185 printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
186 case 'i':
187 case 'g':
188 case 'o':
190 if (rdepth==1)
192 printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3);
194 if (rdepth == 2) {
195 printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3);
198 break;
201 oldrepeat = repeat;
202 repeat = $3;
205 it_field_list ')'
208 repeat = oldrepeat;
209 oldrepeat =0;
210 rdepth--;
211 switch (writecode)
213 case 'i':
214 case 'g':
215 case 'o':
216 case 'c':
217 printf("\t}}\n");
223 cond_it_field: '(' COND NAME
225 switch (writecode)
227 case 'i':
228 case 'g':
229 case 'o':
230 case 'c':
231 printf("\tif (%s) {\n", $3);
232 break;
236 it_field_list ')'
238 switch (writecode)
240 case 'i':
241 case 'g':
242 case 'o':
243 case 'c':
244 printf("\t}\n");
249 it_field:
250 '(' attr_desc '(' attr_type attr_size ')' attr_id
251 {name = $7; }
252 enums ')'
254 char *desc = $2;
255 char *type = $4;
256 int size = $5;
257 char *id = $7;
258 char *p = names[rdepth];
259 char *ptr = pnames[rdepth];
260 switch (writecode)
262 case 'g':
263 if (size % 8)
266 printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
268 names[rdepth], size);
271 else {
272 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n",
273 type,
275 names[rdepth],size/8);
277 break;
278 case 'i':
281 if (rdepth >= 1)
284 printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n",
285 id,
287 type,
288 repeat,
289 id);
292 if (rdepth == 2)
294 printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n",
295 id,
297 type,
298 repeat,
299 id);
304 if (size % 8)
306 printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
308 names[rdepth],
309 size);
311 else {
312 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
314 names[rdepth],
315 type,
316 size/8);
318 break;
319 case 'o':
320 printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
321 break;
322 case 'd':
323 if (repeat)
324 printf("\t/* repeat %s */\n", repeat);
326 if (type[0] == 'I') {
327 printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
329 else if (type[0] =='C') {
330 printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
332 else {
333 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
335 break;
336 case 'c':
337 printf("tabout();\n");
338 printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
340 if (type[0] == 'I')
341 printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
342 else if (type[0] == 'C')
343 printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
345 else if (type[0] == 'B')
347 printf("\tpbarray(&ptr->%s%s);\n", id,p);
349 else abort();
350 break;
357 attr_type:
358 TYPE { $$ = $1; }
359 | { $$ = "INT";}
362 attr_desc:
363 '(' NAME ')'
364 { $$ = $2; }
367 attr_size:
368 NUMBER UNIT
369 { $$ = $1 * $2; }
373 attr_id:
374 '(' NAME ')' { $$ = $2; }
375 | { $$ = "dummy";}
378 enums:
379 | '(' enum_list ')' ;
381 enum_list:
383 enum_list '(' NAME NAME ')' {
384 switch (writecode)
386 case 'd':
387 printf("#define %s %s\n", $3,$4);
388 break;
389 case 'c':
390 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
399 /* four modes
401 -d write structure defintions for sysroff in host format
402 -i write functions to swap into sysroff format in
403 -o write functions to swap into sysroff format out
404 -c write code to print info in human form */
406 int yydebug;
407 char writecode;
409 int
410 main(ac,av)
411 int ac;
412 char **av;
414 yydebug=0;
415 if (ac > 1)
416 writecode = av[1][1];
417 if (writecode == 'd')
419 printf("typedef struct { unsigned char *data; int len; } barray; \n");
420 printf("typedef int INT;\n");
421 printf("typedef char * CHARS;\n");
424 yyparse();
425 return 0;
429 yyerror(s)
430 char *s;
432 fprintf(stderr, "%s\n" , s);
433 return 0;