PR gprof/13325
[binutils.git] / binutils / sysinfo.y
blobbab635ed74970c312265e40466481267cbb999c2
1 /* Copyright 2001, 2003, 2005, 2007, 2009 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
22 #include <stdio.h>
23 #include <stdlib.h>
25 static char writecode;
26 static char *it;
27 static int code;
28 static char * repeat;
29 static char *oldrepeat;
30 static char *name;
31 static int rdepth;
32 static char *names[] = {" ","[n]","[n][m]"};
33 static char *pnames[]= {"","*","**"};
35 static int yyerror (char *s);
36 extern int yylex (void);
40 %union {
41 int i;
42 char *s;
44 %token COND
45 %token REPEAT
46 %token '(' ')'
47 %token <s> TYPE
48 %token <s> NAME
49 %token <i> NUMBER UNIT
50 %type <i> attr_size
51 %type <s> attr_desc attr_id attr_type
54 top: {
55 switch (writecode)
57 case 'i':
58 printf("#ifdef SYSROFF_SWAP_IN\n");
59 break;
60 case 'p':
61 printf("#ifdef SYSROFF_p\n");
62 break;
63 case 'd':
64 break;
65 case 'g':
66 printf("#ifdef SYSROFF_SWAP_OUT\n");
67 break;
68 case 'c':
69 printf("#ifdef SYSROFF_PRINT\n");
70 printf("#include <stdio.h>\n");
71 printf("#include <stdlib.h>\n");
72 printf("#include <ansidecl.h>\n");
73 break;
76 it_list {
77 switch (writecode) {
78 case 'i':
79 case 'p':
80 case 'g':
81 case 'c':
82 printf("#endif\n");
83 break;
84 case 'd':
85 break;
92 it_list: it it_list
96 it:
97 '(' NAME NUMBER
99 it = $2; code = $3;
100 switch (writecode)
102 case 'd':
103 printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
104 printf("struct IT_%s;\n", it);
105 printf("extern void sysroff_swap_%s_in (struct IT_%s *);\n",
106 $2, it);
107 printf("extern void sysroff_swap_%s_out (FILE *, struct IT_%s *);\n",
108 $2, it);
109 printf("extern void sysroff_print_%s_out (struct IT_%s *);\n",
110 $2, it);
111 printf("struct IT_%s { \n", it);
112 break;
113 case 'i':
114 printf("void sysroff_swap_%s_in (struct IT_%s * ptr)\n",$2,it);
115 printf("{\n");
116 printf("\tunsigned char raw[255];\n");
117 printf("\tint idx = 0;\n");
118 printf("\tint size;\n");
119 printf("\tmemset(raw,0,255);\n");
120 printf("\tmemset(ptr,0,sizeof(*ptr));\n");
121 printf("\tsize = fillup(raw);\n");
122 break;
123 case 'g':
124 printf("void sysroff_swap_%s_out (FILE * ffile, struct IT_%s * ptr)\n",$2,it);
125 printf("{\n");
126 printf("\tunsigned char raw[255];\n");
127 printf("\tint idx = 16;\n");
128 printf("\tmemset (raw, 0, 255);\n");
129 printf("\tcode = IT_%s_CODE;\n", it);
130 break;
131 case 'o':
132 printf("void sysroff_swap_%s_out (bfd * abfd, struct IT_%s * ptr)\n",$2, it);
133 printf("{\n");
134 printf("\tint idx = 0;\n");
135 break;
136 case 'c':
137 printf("void sysroff_print_%s_out (struct IT_%s *ptr)\n",$2,it);
138 printf("{\n");
139 printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
140 break;
142 case 't':
143 break;
147 it_field_list
150 switch (writecode) {
151 case 'd':
152 printf("};\n");
153 break;
154 case 'g':
155 printf("\tchecksum(ffile,raw, idx, IT_%s_CODE);\n", it);
157 case 'i':
159 case 'o':
160 case 'c':
161 printf("}\n");
168 it_field_list:
169 it_field it_field_list
170 | cond_it_field it_field_list
171 | repeat_it_field it_field_list
175 repeat_it_field: '(' REPEAT NAME
177 rdepth++;
178 switch (writecode)
180 case 'c':
181 if (rdepth==1)
182 printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
183 if (rdepth==2)
184 printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
185 case 'i':
186 case 'g':
187 case 'o':
189 if (rdepth==1)
191 printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3);
193 if (rdepth == 2) {
194 printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3);
197 break;
200 oldrepeat = repeat;
201 repeat = $3;
204 it_field_list ')'
207 repeat = oldrepeat;
208 oldrepeat =0;
209 rdepth--;
210 switch (writecode)
212 case 'i':
213 case 'g':
214 case 'o':
215 case 'c':
216 printf("\t}}\n");
222 cond_it_field: '(' COND NAME
224 switch (writecode)
226 case 'i':
227 case 'g':
228 case 'o':
229 case 'c':
230 printf("\tif (%s) {\n", $3);
231 break;
235 it_field_list ')'
237 switch (writecode)
239 case 'i':
240 case 'g':
241 case 'o':
242 case 'c':
243 printf("\t}\n");
248 it_field:
249 '(' attr_desc '(' attr_type attr_size ')' attr_id
250 {name = $7; }
251 enums ')'
253 char *desc = $2;
254 char *type = $4;
255 int size = $5;
256 char *id = $7;
257 char *p = names[rdepth];
258 char *ptr = pnames[rdepth];
259 switch (writecode)
261 case 'g':
262 if (size % 8)
265 printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
267 names[rdepth], size);
270 else {
271 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,ffile);\n",
272 type,
274 names[rdepth],size/8);
276 break;
277 case 'i':
280 if (rdepth >= 1)
283 printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n",
284 id,
286 type,
287 repeat,
288 id);
291 if (rdepth == 2)
293 printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n",
294 id,
296 type,
297 repeat,
298 id);
303 if (size % 8)
305 printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
307 names[rdepth],
308 size);
310 else {
311 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
313 names[rdepth],
314 type,
315 size/8);
317 break;
318 case 'o':
319 printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
320 break;
321 case 'd':
322 if (repeat)
323 printf("\t/* repeat %s */\n", repeat);
325 if (type[0] == 'I') {
326 printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
328 else if (type[0] =='C') {
329 printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
331 else {
332 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
334 break;
335 case 'c':
336 printf("tabout();\n");
337 printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
339 if (type[0] == 'I')
340 printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
341 else if (type[0] == 'C')
342 printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
344 else if (type[0] == 'B')
346 printf("\tpbarray(&ptr->%s%s);\n", id,p);
348 else abort();
349 break;
356 attr_type:
357 TYPE { $$ = $1; }
358 | { $$ = "INT";}
361 attr_desc:
362 '(' NAME ')'
363 { $$ = $2; }
366 attr_size:
367 NUMBER UNIT
368 { $$ = $1 * $2; }
372 attr_id:
373 '(' NAME ')' { $$ = $2; }
374 | { $$ = "dummy";}
377 enums:
378 | '(' enum_list ')' ;
380 enum_list:
382 enum_list '(' NAME NAME ')' {
383 switch (writecode)
385 case 'd':
386 printf("#define %s %s\n", $3,$4);
387 break;
388 case 'c':
389 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
398 /* four modes
400 -d write structure definitions for sysroff in host format
401 -i write functions to swap into sysroff format in
402 -o write functions to swap into sysroff format out
403 -c write code to print info in human form */
405 int yydebug;
407 int
408 main (int ac, char **av)
410 yydebug=0;
411 if (ac > 1)
412 writecode = av[1][1];
413 if (writecode == 'd')
415 printf("typedef struct { unsigned char *data; int len; } barray; \n");
416 printf("typedef int INT;\n");
417 printf("typedef char * CHARS;\n");
420 yyparse();
421 return 0;
424 static int
425 yyerror (char *s)
427 fprintf(stderr, "%s\n" , s);
428 return 0;