| 1 | /* Copyright 2001, 2003 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 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 | |
| 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[]= {"","*","**"}; |
| 40 | %} |
| 41 | |
| 42 | |
| 43 | %union { |
| 44 | int i; |
| 45 | char *s; |
| 46 | } |
| 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 |
| 55 | %% |
| 56 | |
| 57 | top: { |
| 58 | switch (writecode) |
| 59 | { |
| 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; |
| 77 | } |
| 78 | } |
| 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; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ; |
| 93 | |
| 94 | |
| 95 | it_list: it it_list |
| 96 | | |
| 97 | ; |
| 98 | |
| 99 | it: |
| 100 | '(' NAME NUMBER |
| 101 | { |
| 102 | it = $2; code = $3; |
| 103 | switch (writecode) |
| 104 | { |
| 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; |
| 150 | |
| 151 | case 't': |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | it_field_list |
| 157 | ')' |
| 158 | { |
| 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); |
| 165 | |
| 166 | case 'i': |
| 167 | |
| 168 | case 'o': |
| 169 | case 'c': |
| 170 | printf("}\n"); |
| 171 | } |
| 172 | } |
| 173 | ; |
| 174 | |
| 175 | |
| 176 | |
| 177 | it_field_list: |
| 178 | it_field it_field_list |
| 179 | | cond_it_field it_field_list |
| 180 | | repeat_it_field it_field_list |
| 181 | | |
| 182 | ; |
| 183 | |
| 184 | repeat_it_field: '(' REPEAT NAME |
| 185 | { |
| 186 | rdepth++; |
| 187 | switch (writecode) |
| 188 | { |
| 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': |
| 197 | |
| 198 | if (rdepth==1) |
| 199 | { |
| 200 | printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3); |
| 201 | } |
| 202 | if (rdepth == 2) { |
| 203 | printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3); |
| 204 | } |
| 205 | |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | oldrepeat = repeat; |
| 210 | repeat = $3; |
| 211 | } |
| 212 | |
| 213 | it_field_list ')' |
| 214 | |
| 215 | { |
| 216 | repeat = oldrepeat; |
| 217 | oldrepeat =0; |
| 218 | rdepth--; |
| 219 | switch (writecode) |
| 220 | { |
| 221 | case 'i': |
| 222 | case 'g': |
| 223 | case 'o': |
| 224 | case 'c': |
| 225 | printf("\t}}\n"); |
| 226 | } |
| 227 | } |
| 228 | ; |
| 229 | |
| 230 | |
| 231 | cond_it_field: '(' COND NAME |
| 232 | { |
| 233 | switch (writecode) |
| 234 | { |
| 235 | case 'i': |
| 236 | case 'g': |
| 237 | case 'o': |
| 238 | case 'c': |
| 239 | printf("\tif (%s) {\n", $3); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | it_field_list ')' |
| 245 | { |
| 246 | switch (writecode) |
| 247 | { |
| 248 | case 'i': |
| 249 | case 'g': |
| 250 | case 'o': |
| 251 | case 'c': |
| 252 | printf("\t}\n"); |
| 253 | } |
| 254 | } |
| 255 | ; |
| 256 | |
| 257 | it_field: |
| 258 | '(' attr_desc '(' attr_type attr_size ')' attr_id |
| 259 | {name = $7; } |
| 260 | enums ')' |
| 261 | { |
| 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) |
| 269 | { |
| 270 | case 'g': |
| 271 | if (size % 8) |
| 272 | { |
| 273 | |
| 274 | printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n", |
| 275 | id, |
| 276 | names[rdepth], size); |
| 277 | |
| 278 | } |
| 279 | else { |
| 280 | printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n", |
| 281 | type, |
| 282 | id, |
| 283 | names[rdepth],size/8); |
| 284 | } |
| 285 | break; |
| 286 | case 'i': |
| 287 | { |
| 288 | |
| 289 | if (rdepth >= 1) |
| 290 | |
| 291 | { |
| 292 | printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n", |
| 293 | id, |
| 294 | id, |
| 295 | type, |
| 296 | repeat, |
| 297 | id); |
| 298 | } |
| 299 | |
| 300 | if (rdepth == 2) |
| 301 | { |
| 302 | printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n", |
| 303 | id, |
| 304 | id, |
| 305 | type, |
| 306 | repeat, |
| 307 | id); |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
| 312 | if (size % 8) |
| 313 | { |
| 314 | printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n", |
| 315 | id, |
| 316 | names[rdepth], |
| 317 | size); |
| 318 | } |
| 319 | else { |
| 320 | printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n", |
| 321 | id, |
| 322 | names[rdepth], |
| 323 | type, |
| 324 | size/8); |
| 325 | } |
| 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); |
| 333 | |
| 334 | if (type[0] == 'I') { |
| 335 | printf("\tint %s%s; \t/* %s */\n",ptr,id, desc); |
| 336 | } |
| 337 | else if (type[0] =='C') { |
| 338 | printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc); |
| 339 | } |
| 340 | else { |
| 341 | printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc); |
| 342 | } |
| 343 | break; |
| 344 | case 'c': |
| 345 | printf("tabout();\n"); |
| 346 | printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id); |
| 347 | |
| 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); |
| 352 | |
| 353 | else if (type[0] == 'B') |
| 354 | { |
| 355 | printf("\tpbarray(&ptr->%s%s);\n", id,p); |
| 356 | } |
| 357 | else abort(); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | ; |
| 363 | |
| 364 | |
| 365 | attr_type: |
| 366 | TYPE { $$ = $1; } |
| 367 | | { $$ = "INT";} |
| 368 | ; |
| 369 | |
| 370 | attr_desc: |
| 371 | '(' NAME ')' |
| 372 | { $$ = $2; } |
| 373 | ; |
| 374 | |
| 375 | attr_size: |
| 376 | NUMBER UNIT |
| 377 | { $$ = $1 * $2; } |
| 378 | ; |
| 379 | |
| 380 | |
| 381 | attr_id: |
| 382 | '(' NAME ')' { $$ = $2; } |
| 383 | | { $$ = "dummy";} |
| 384 | ; |
| 385 | |
| 386 | enums: |
| 387 | | '(' enum_list ')' ; |
| 388 | |
| 389 | enum_list: |
| 390 | | |
| 391 | enum_list '(' NAME NAME ')' { |
| 392 | switch (writecode) |
| 393 | { |
| 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); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | ; |
| 403 | |
| 404 | |
| 405 | |
| 406 | %% |
| 407 | /* four modes |
| 408 | |
| 409 | -d write structure definitions 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 */ |
| 413 | |
| 414 | int yydebug; |
| 415 | char writecode; |
| 416 | |
| 417 | int |
| 418 | main (int ac, char **av) |
| 419 | { |
| 420 | yydebug=0; |
| 421 | if (ac > 1) |
| 422 | writecode = av[1][1]; |
| 423 | if (writecode == 'd') |
| 424 | { |
| 425 | printf("typedef struct { unsigned char *data; int len; } barray; \n"); |
| 426 | printf("typedef int INT;\n"); |
| 427 | printf("typedef char * CHARS;\n"); |
| 428 | |
| 429 | } |
| 430 | yyparse(); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | int |
| 435 | yyerror (char *s) |
| 436 | { |
| 437 | fprintf(stderr, "%s\n" , s); |
| 438 | return 0; |
| 439 | } |