2002-02-18 David O'Brien <obrien@FreeBSD.org>
[binutils.git] / binutils / sysinfo.y
blob4aa46073bbbe0650c6cd1b343d25b138f6948446
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 printf("#include <ansidecl.h>\n");
76 break;
79 it_list {
80 switch (writecode) {
81 case 'i':
82 case 'p':
83 case 'g':
84 case 'c':
85 printf("#endif\n");
86 break;
87 case 'd':
88 break;
95 it_list: it it_list
99 it:
100 '(' NAME NUMBER
102 it = $2; code = $3;
103 switch (writecode)
105 case 'd':
106 printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
107 printf("struct IT_%s;\n", it);
108 printf("extern void sysroff_swap_%s_in PARAMS ((struct IT_%s *));\n",
109 $2, it);
110 printf("extern void sysroff_swap_%s_out PARAMS ((FILE *, struct IT_%s *));\n",
111 $2, it);
112 printf("extern void sysroff_print_%s_out PARAMS ((struct IT_%s *));\n",
113 $2, it);
114 printf("struct IT_%s { \n", it);
115 break;
116 case 'i':
117 printf("void sysroff_swap_%s_in(ptr)\n",$2);
118 printf("struct IT_%s *ptr;\n", it);
119 printf("{\n");
120 printf("char raw[255];\n");
121 printf("\tint idx = 0 ;\n");
122 printf("\tint size;\n");
123 printf("memset(raw,0,255);\n");
124 printf("memset(ptr,0,sizeof(*ptr));\n");
125 printf("size = fillup(raw);\n");
126 break;
127 case 'g':
128 printf("void sysroff_swap_%s_out(file,ptr)\n",$2);
129 printf("FILE * file;\n");
130 printf("struct IT_%s *ptr;\n", it);
131 printf("{\n");
132 printf("\tchar raw[255];\n");
133 printf("\tint idx = 16 ;\n");
134 printf("\tmemset (raw, 0, 255);\n");
135 printf("\tcode = IT_%s_CODE;\n", it);
136 break;
137 case 'o':
138 printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2);
139 printf("bfd * abfd;\n");
140 printf("struct IT_%s *ptr;\n",it);
141 printf("{\n");
142 printf("int idx = 0 ;\n");
143 break;
144 case 'c':
145 printf("void sysroff_print_%s_out(ptr)\n",$2);
146 printf("struct IT_%s *ptr;\n", it);
147 printf("{\n");
148 printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
149 break;
151 case 't':
152 break;
156 it_field_list
159 switch (writecode) {
160 case 'd':
161 printf("};\n");
162 break;
163 case 'g':
164 printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it);
166 case 'i':
168 case 'o':
169 case 'c':
170 printf("}\n");
177 it_field_list:
178 it_field it_field_list
179 | cond_it_field it_field_list
180 | repeat_it_field it_field_list
184 repeat_it_field: '(' REPEAT NAME
186 rdepth++;
187 switch (writecode)
189 case 'c':
190 if (rdepth==1)
191 printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
192 if (rdepth==2)
193 printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
194 case 'i':
195 case 'g':
196 case 'o':
198 if (rdepth==1)
200 printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3);
202 if (rdepth == 2) {
203 printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3);
206 break;
209 oldrepeat = repeat;
210 repeat = $3;
213 it_field_list ')'
216 repeat = oldrepeat;
217 oldrepeat =0;
218 rdepth--;
219 switch (writecode)
221 case 'i':
222 case 'g':
223 case 'o':
224 case 'c':
225 printf("\t}}\n");
231 cond_it_field: '(' COND NAME
233 switch (writecode)
235 case 'i':
236 case 'g':
237 case 'o':
238 case 'c':
239 printf("\tif (%s) {\n", $3);
240 break;
244 it_field_list ')'
246 switch (writecode)
248 case 'i':
249 case 'g':
250 case 'o':
251 case 'c':
252 printf("\t}\n");
257 it_field:
258 '(' attr_desc '(' attr_type attr_size ')' attr_id
259 {name = $7; }
260 enums ')'
262 char *desc = $2;
263 char *type = $4;
264 int size = $5;
265 char *id = $7;
266 char *p = names[rdepth];
267 char *ptr = pnames[rdepth];
268 switch (writecode)
270 case 'g':
271 if (size % 8)
274 printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
276 names[rdepth], size);
279 else {
280 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n",
281 type,
283 names[rdepth],size/8);
285 break;
286 case 'i':
289 if (rdepth >= 1)
292 printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n",
293 id,
295 type,
296 repeat,
297 id);
300 if (rdepth == 2)
302 printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n",
303 id,
305 type,
306 repeat,
307 id);
312 if (size % 8)
314 printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
316 names[rdepth],
317 size);
319 else {
320 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
322 names[rdepth],
323 type,
324 size/8);
326 break;
327 case 'o':
328 printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
329 break;
330 case 'd':
331 if (repeat)
332 printf("\t/* repeat %s */\n", repeat);
334 if (type[0] == 'I') {
335 printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
337 else if (type[0] =='C') {
338 printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
340 else {
341 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
343 break;
344 case 'c':
345 printf("tabout();\n");
346 printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
348 if (type[0] == 'I')
349 printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
350 else if (type[0] == 'C')
351 printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
353 else if (type[0] == 'B')
355 printf("\tpbarray(&ptr->%s%s);\n", id,p);
357 else abort();
358 break;
365 attr_type:
366 TYPE { $$ = $1; }
367 | { $$ = "INT";}
370 attr_desc:
371 '(' NAME ')'
372 { $$ = $2; }
375 attr_size:
376 NUMBER UNIT
377 { $$ = $1 * $2; }
381 attr_id:
382 '(' NAME ')' { $$ = $2; }
383 | { $$ = "dummy";}
386 enums:
387 | '(' enum_list ')' ;
389 enum_list:
391 enum_list '(' NAME NAME ')' {
392 switch (writecode)
394 case 'd':
395 printf("#define %s %s\n", $3,$4);
396 break;
397 case 'c':
398 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
407 /* four modes
409 -d write structure defintions for sysroff in host format
410 -i write functions to swap into sysroff format in
411 -o write functions to swap into sysroff format out
412 -c write code to print info in human form */
414 int yydebug;
415 char writecode;
417 int
418 main(ac,av)
419 int ac;
420 char **av;
422 yydebug=0;
423 if (ac > 1)
424 writecode = av[1][1];
425 if (writecode == 'd')
427 printf("typedef struct { unsigned char *data; int len; } barray; \n");
428 printf("typedef int INT;\n");
429 printf("typedef char * CHARS;\n");
432 yyparse();
433 return 0;
437 yyerror(s)
438 char *s;
440 fprintf(stderr, "%s\n" , s);
441 return 0;