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