daily update
[external/binutils.git] / binutils / sysinfo.y
1 /* Copyright 2001, 2003, 2005, 2007 Free Software Foundation, Inc.
2    Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
3
4    This file is part of GNU binutils.
5
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.
10
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.
15
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.  */
20
21 %{
22 #include <stdio.h>
23 #include <stdlib.h>
24
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[]= {"","*","**"};
34
35 static int yyerror (char *s);
36 extern int yylex (void);
37 %}
38
39
40 %union {
41  int i;
42  char *s;
43
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
52 %%
53
54 top:  {
55   switch (writecode)
56     {
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;
74     }
75  } 
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;
86   }
87 }
88
89   ;
90
91
92 it_list: it it_list
93   |
94   ;
95
96 it:
97         '(' NAME NUMBER 
98       {
99         it = $2; code = $3;
100         switch (writecode) 
101           {
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 PARAMS ((struct IT_%s *));\n",
106                    $2, it);
107             printf("extern void sysroff_swap_%s_out PARAMS ((FILE *, struct IT_%s *));\n",
108                    $2, it);
109             printf("extern void sysroff_print_%s_out PARAMS ((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(ptr)\n",$2);
115             printf("struct IT_%s *ptr;\n", it);
116             printf("{\n");
117             printf("unsigned char raw[255];\n");
118             printf("\tint idx = 0 ;\n");
119             printf("\tint size;\n");
120             printf("memset(raw,0,255);\n");     
121             printf("memset(ptr,0,sizeof(*ptr));\n");
122             printf("size = fillup(raw);\n");
123             break;
124           case 'g':
125             printf("void sysroff_swap_%s_out(file,ptr)\n",$2);
126             printf("FILE * file;\n");
127             printf("struct IT_%s *ptr;\n", it);
128             printf("{\n");
129             printf("\tunsigned char raw[255];\n");
130             printf("\tint idx = 16 ;\n");
131             printf("\tmemset (raw, 0, 255);\n");
132             printf("\tcode = IT_%s_CODE;\n", it);
133             break;
134           case 'o':
135             printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2);
136             printf("bfd * abfd;\n");
137             printf("struct IT_%s *ptr;\n",it);
138             printf("{\n");
139             printf("int idx = 0 ;\n");
140             break;
141           case 'c':
142             printf("void sysroff_print_%s_out(ptr)\n",$2);
143             printf("struct IT_%s *ptr;\n", it);
144             printf("{\n");
145             printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
146             break;
147
148           case 't':
149             break;
150           }
151
152       } 
153         it_field_list 
154 ')'
155 {
156   switch (writecode) {
157   case 'd': 
158     printf("};\n");
159     break;
160   case 'g':
161     printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it);
162     
163   case 'i':
164
165   case 'o':
166   case 'c':
167     printf("}\n");
168   }
169 }
170 ;
171
172
173
174 it_field_list:
175                 it_field it_field_list
176         |       cond_it_field it_field_list     
177         |       repeat_it_field it_field_list
178         |
179         ;
180
181 repeat_it_field: '(' REPEAT NAME
182         {
183           rdepth++;
184           switch (writecode) 
185             {
186             case 'c':
187               if (rdepth==1)
188               printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
189               if (rdepth==2)
190               printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
191             case 'i':
192             case 'g':
193             case 'o':
194
195               if (rdepth==1) 
196                 {
197               printf("\t{ int n; for (n = 0; n < %s; n++) {\n",    $3);
198             }
199               if (rdepth == 2) {
200               printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n",    $3);
201             }           
202
203               break;
204             }
205
206           oldrepeat = repeat;
207          repeat = $3;
208         }
209
210          it_field_list ')' 
211
212         {
213           repeat = oldrepeat;
214           oldrepeat =0;
215           rdepth--;
216           switch (writecode)
217             {
218             case 'i':
219             case 'g':
220             case 'o':
221             case 'c':
222           printf("\t}}\n");
223         }
224         }
225        ;
226
227
228 cond_it_field: '(' COND NAME
229         {
230           switch (writecode) 
231             {
232             case 'i':
233             case 'g':
234             case 'o':
235             case 'c':
236               printf("\tif (%s) {\n", $3);
237               break;
238             }
239         }
240
241          it_field_list ')' 
242         {
243           switch (writecode)
244             {
245             case 'i':
246             case 'g':
247             case 'o':
248             case 'c':
249           printf("\t}\n");
250         }
251         }
252        ;
253
254 it_field:
255         '(' attr_desc '(' attr_type attr_size ')' attr_id 
256         {name = $7; } 
257         enums ')'
258         {
259           char *desc = $2;
260           char *type = $4;
261           int size = $5;
262           char *id = $7;
263 char *p = names[rdepth];
264 char *ptr = pnames[rdepth];
265           switch (writecode) 
266             {
267             case 'g':
268               if (size % 8) 
269                 {
270                   
271                   printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
272                          id,
273                          names[rdepth], size);
274
275                 }
276               else {
277                 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n",
278                        type,
279                        id,
280                        names[rdepth],size/8);
281                 }
282               break;          
283             case 'i':
284               {
285
286                 if (rdepth >= 1)
287
288                   {
289                     printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n", 
290                            id, 
291                            id,
292                            type,
293                            repeat,
294                            id);
295                   }
296
297                 if (rdepth == 2)
298                   {
299                     printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n", 
300                            id, 
301                            id,
302                            type,
303                            repeat,
304                            id);
305                   }
306
307               }
308
309               if (size % 8) 
310                 {
311                   printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
312                          id,
313                          names[rdepth], 
314                          size);
315                 }
316               else {
317                 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
318                        id,
319                        names[rdepth],
320                        type,
321                        size/8);
322                 }
323               break;
324             case 'o':
325               printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
326               break;
327             case 'd':
328               if (repeat) 
329                 printf("\t/* repeat %s */\n", repeat);
330
331                   if (type[0] == 'I') {
332                   printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
333                 }
334                   else if (type[0] =='C') {
335                   printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
336                 }
337               else {
338                 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
339               }
340                   break;
341                 case 'c':
342               printf("tabout();\n");
343                   printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
344
345                   if (type[0] == 'I')
346                   printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
347                   else   if (type[0] == 'C')
348                   printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
349
350                   else   if (type[0] == 'B') 
351                     {
352                   printf("\tpbarray(&ptr->%s%s);\n", id,p);
353                 }
354               else abort();
355                   break;
356                 }
357         }
358
359         ;
360
361
362 attr_type:      
363          TYPE { $$ = $1; }
364         |  { $$ = "INT";}
365         ;
366
367 attr_desc: 
368         '(' NAME ')'    
369         { $$ = $2; }
370         ;
371
372 attr_size:
373          NUMBER UNIT 
374         { $$ = $1 * $2; }
375         ;
376
377
378 attr_id:
379                 '(' NAME ')'    { $$ = $2; }
380         |       { $$ = "dummy";}
381         ;       
382         
383 enums: 
384         | '(' enum_list ')' ;
385
386 enum_list:
387         |
388         enum_list '(' NAME NAME ')' { 
389           switch (writecode) 
390             {
391             case 'd':
392               printf("#define %s %s\n", $3,$4);
393               break;
394             case 'c':
395                 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
396             }
397         }
398
399         ;
400
401
402
403 %%
404 /* four modes
405
406    -d write structure definitions for sysroff in host format
407    -i write functions to swap into sysroff format in
408    -o write functions to swap into sysroff format out
409    -c write code to print info in human form */
410
411 int yydebug;
412
413 int 
414 main (int ac, char **av)
415 {
416   yydebug=0;
417   if (ac > 1)
418     writecode = av[1][1];
419 if (writecode == 'd')
420   {
421     printf("typedef struct { unsigned char *data; int len; } barray; \n");
422     printf("typedef  int INT;\n");
423     printf("typedef  char * CHARS;\n");
424
425   }
426   yyparse();
427 return 0;
428 }
429
430 static int
431 yyerror (char *s)
432 {
433   fprintf(stderr, "%s\n" , s);
434   return 0;
435 }