Fix an overflow checking bug uncovered when a 32 bit target is compiled
[binutils.git] / binutils / sysinfo.y
blob0aa87375dd63e8d8b5b852c0be6b42e12be4a979
1 %{
2 #include <stdio.h>
3 #include <stdlib.h>
5 extern char *word;
6 extern char writecode;
7 extern int number;
8 extern int unit;
9 char nice_name[1000];
10 char *it;
11 int sofar;
12 int width;
13 int code;
14 char * repeat;
15 char *oldrepeat;
16 char *name;
17 int rdepth;
18 char *loop [] = {"","n","m","/*BAD*/"};
19 char *names[] = {" ","[n]","[n][m]"};
20 char *pnames[]= {"","*","**"};
24 %union {
25 int i;
26 char *s;
28 %token COND
29 %token REPEAT
30 %token '(' ')'
31 %token <s> TYPE
32 %token <s> NAME
33 %token <i> NUMBER UNIT
34 %type <i> attr_size
35 %type <s> attr_desc attr_id attr_type
38 top: {
39 switch (writecode)
41 case 'i':
42 printf("#ifdef SYSROFF_SWAP_IN\n");
43 break;
44 case 'p':
45 printf("#ifdef SYSROFF_p\n");
46 break;
47 case 'd':
48 break;
49 case 'g':
50 printf("#ifdef SYSROFF_SWAP_OUT\n");
51 break;
52 case 'c':
53 printf("#ifdef SYSROFF_PRINT\n");
54 printf("#include <stdio.h>\n");
55 printf("#include <stdlib.h>\n");
56 break;
59 it_list {
60 switch (writecode) {
61 case 'i':
62 case 'p':
63 case 'g':
64 case 'c':
65 printf("#endif\n");
66 break;
67 case 'd':
68 break;
75 it_list: it it_list
79 it:
80 '(' NAME NUMBER
82 it = $2; code = $3;
83 switch (writecode)
85 case 'd':
86 printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
87 printf("struct IT_%s { \n", it);
88 break;
89 case 'i':
90 printf("void sysroff_swap_%s_in(ptr)\n",$2);
91 printf("struct IT_%s *ptr;\n", it);
92 printf("{\n");
93 printf("char raw[255];\n");
94 printf("\tint idx = 0 ;\n");
95 printf("\tint size;\n");
96 printf("memset(raw,0,255);\n");
97 printf("memset(ptr,0,sizeof(*ptr));\n");
98 printf("size = fillup(raw);\n");
99 break;
100 case 'g':
101 printf("void sysroff_swap_%s_out(file,ptr)\n",$2);
102 printf("FILE * file;\n");
103 printf("struct IT_%s *ptr;\n", it);
104 printf("{\n");
105 printf("\tchar raw[255];\n");
106 printf("\tint idx = 16 ;\n");
107 printf("\tmemset (raw, 0, 255);\n");
108 printf("\tcode = IT_%s_CODE;\n", it);
109 break;
110 case 'o':
111 printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2);
112 printf("bfd * abfd;\n");
113 printf("struct IT_%s *ptr;\n",it);
114 printf("{\n");
115 printf("int idx = 0 ;\n");
116 break;
117 case 'c':
118 printf("void sysroff_print_%s_out(ptr)\n",$2);
119 printf("struct IT_%s *ptr;\n", it);
120 printf("{\n");
121 printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
122 break;
124 case 't':
125 break;
129 it_field_list
132 switch (writecode) {
133 case 'd':
134 printf("};\n");
135 break;
136 case 'g':
137 printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it);
139 case 'i':
141 case 'o':
142 case 'c':
143 printf("}\n");
150 it_field_list:
151 it_field it_field_list
152 | cond_it_field it_field_list
153 | repeat_it_field it_field_list
157 repeat_it_field: '(' REPEAT NAME
159 rdepth++;
160 switch (writecode)
162 case 'c':
163 if (rdepth==1)
164 printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
165 if (rdepth==2)
166 printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
167 case 'i':
168 case 'g':
169 case 'o':
171 if (rdepth==1)
173 printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3);
175 if (rdepth == 2) {
176 printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3);
179 break;
182 oldrepeat = repeat;
183 repeat = $3;
186 it_field_list ')'
189 repeat = oldrepeat;
190 oldrepeat =0;
191 rdepth--;
192 switch (writecode)
194 case 'i':
195 case 'g':
196 case 'o':
197 case 'c':
198 printf("\t}}\n");
204 cond_it_field: '(' COND NAME
206 switch (writecode)
208 case 'i':
209 case 'g':
210 case 'o':
211 case 'c':
212 printf("\tif (%s) {\n", $3);
213 break;
217 it_field_list ')'
219 switch (writecode)
221 case 'i':
222 case 'g':
223 case 'o':
224 case 'c':
225 printf("\t}\n");
230 it_field:
231 '(' attr_desc '(' attr_type attr_size ')' attr_id
232 {name = $7; }
233 enums ')'
235 char *desc = $2;
236 char *type = $4;
237 int size = $5;
238 char *id = $7;
239 char *p = names[rdepth];
240 char *ptr = pnames[rdepth];
241 switch (writecode)
243 case 'g':
244 if (size % 8)
247 printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
249 names[rdepth], size);
252 else {
253 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n",
254 type,
256 names[rdepth],size/8);
258 break;
259 case 'i':
262 if (rdepth >= 1)
265 printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n",
266 id,
268 type,
269 repeat,
270 id);
273 if (rdepth == 2)
275 printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n",
276 id,
278 type,
279 repeat,
280 id);
285 if (size % 8)
287 printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
289 names[rdepth],
290 size);
292 else {
293 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
295 names[rdepth],
296 type,
297 size/8);
299 break;
300 case 'o':
301 printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
302 break;
303 case 'd':
304 if (repeat)
305 printf("\t/* repeat %s */\n", repeat);
307 if (type[0] == 'I') {
308 printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
310 else if (type[0] =='C') {
311 printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
313 else {
314 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
316 break;
317 case 'c':
318 printf("tabout();\n");
319 printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
321 if (type[0] == 'I')
322 printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
323 else if (type[0] == 'C')
324 printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
326 else if (type[0] == 'B')
328 printf("\tpbarray(&ptr->%s%s);\n", id,p);
330 else abort();
331 break;
338 attr_type:
339 TYPE { $$ = $1; }
340 | { $$ = "INT";}
343 attr_desc:
344 '(' NAME ')'
345 { $$ = $2; }
348 attr_size:
349 NUMBER UNIT
350 { $$ = $1 * $2; }
354 attr_id:
355 '(' NAME ')' { $$ = $2; }
356 | { $$ = "dummy";}
359 enums:
360 | '(' enum_list ')' ;
362 enum_list:
364 enum_list '(' NAME NAME ')' {
365 switch (writecode)
367 case 'd':
368 printf("#define %s %s\n", $3,$4);
369 break;
370 case 'c':
371 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
380 /* four modes
382 -d write structure defintions for sysroff in host format
383 -i write functions to swap into sysroff format in
384 -o write functions to swap into sysroff format out
385 -c write code to print info in human form */
387 int yydebug;
388 char writecode;
390 int
391 main(ac,av)
392 int ac;
393 char **av;
395 yydebug=0;
396 if (ac > 1)
397 writecode = av[1][1];
398 if (writecode == 'd')
400 printf("typedef struct { unsigned char *data; int len; } barray; \n");
401 printf("typedef int INT;\n");
402 printf("typedef char * CHARS;\n");
405 yyparse();
406 return 0;
410 yyerror(s)
411 char *s;
413 fprintf(stderr, "%s\n" , s);
414 return 0;