00001
00002 #
00003 # define DEBUG DEBUG
00004 # define LDBUG
00005 char copyright1[] = "Copyright 1986-1989 Hans-Juergen Boehm, Alan J. Demers, Kumar Srikantan, Vernon Lee, and Lucy Hederman.\n";
00006 char copyright2[] = "Copyright (c) 1990-1993 by Xerox Corporation. All rights reserved.\n";
00007 char copyright3[] = "This material is provided as is, with no warranty expressed or implied. Any use is at your own risk.\n";
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 # include <stdio.h>
00109 # include <a.out.h>
00110 # include "parm.h"
00111 # include "arith.h"
00112
00113 typedef long NODE;
00114
00115
00116 # define NIL ((NODE *)0)
00117
00118
00119 boolean BADflag;
00120 boolean initflag;
00121 boolean dloadflag;
00122 boolean aflag;
00123 boolean Mflag;
00124 boolean cflag;
00125 boolean pflag;
00126 boolean Lflag;
00127 boolean Pflag;
00128 boolean Tflag;
00129 boolean Gflag;
00130 boolean Vflag;
00131 boolean Nflag;
00132 boolean fflag;
00133 boolean Fflag;
00134 boolean Rflag;
00135 boolean Oflag;
00136 boolean OOflag;
00137 boolean OOOflag;
00138 boolean Xflag;
00139 boolean xflag;
00140 boolean hflag;
00141 long max_int_regs;
00142 long max_addr_regs;
00143
00144 char * entry_name;
00145
00146 extern int GC_dont_gc;
00147
00148
00149
00150
00151 char *ifname;
00152 char *ofname;
00153 char *sigfname;
00154 char *optfname;
00155 char *actual_ifname;
00156 FILE *ofile;
00157 FILE *sigfile;
00158 FILE *optfile;
00159
00160
00161
00162
00163
00164 char * Vspcl_to_inline();
00165 char * Gspcl_to_inline();
00166 char * (* spcl_to_inline)();
00167
00168
00169
00170
00171 char * Vinline_cnvt(s)
00172 char *s;
00173 { return(s); }
00174
00175 char * Ginline_cnvt();
00176
00177 char * (* inline_cnvt)();
00178
00179
00180
00181
00182 extern int yynerrs;
00183 extern int yydebug;
00184
00185
00186
00187
00188 extern unsigned yyinfnm;
00189 extern int yyline;
00190
00191
00192
00193
00194
00195 extern etext;
00196
00197
00198
00199
00200 extern NODE * stxtree;
00201 extern NODE * insrtptr;
00202
00203 main(argc,argv)
00204 int argc;
00205 char **argv;
00206 { int argn;
00207
00208 # ifdef VERBOSE
00209 fprintf(stderr, "Starting up\n");
00210 # endif
00211 if (insrtptr == NIL) {
00212
00213
00214 GC_init();
00215 }
00216
00217 dloadflag = initflag = BADflag = aflag = yydebug = FALSE;
00218
00219 ifname = ofname = sigfname = optfname = actual_ifname = (char *)0;
00220 for (argn = 1; argn < argc; argn++) {
00221 switch ( argv[argn][0] ) {
00222 case '-' :
00223 { char *flagp;
00224 for (flagp = &(argv[argn][1]); *flagp != '\0'; flagp++) {
00225 switch (*flagp) {
00226 case 'B':
00227 BADflag++;
00228 break;
00229 case 'D':
00230 yydebug++;
00231 break;
00232 case 'S':
00233 catchsigs();
00234 break;
00235 case 'i':
00236 initflag = TRUE;
00237 break;
00238 case 'd':
00239 initflag = TRUE;
00240 dloadflag = TRUE;
00241 break;
00242 case 'a':
00243 aflag = TRUE;
00244 break;
00245 case 'M':
00246 Mflag = TRUE;
00247 break;
00248 case 'c':
00249 cflag = TRUE;
00250 break;
00251 case 'p':
00252 pflag = TRUE;
00253 break;
00254 case 'L':
00255 Lflag = TRUE;
00256 break;
00257 case 'P':
00258 Pflag = TRUE;
00259 break;
00260 case 'T':
00261 Tflag = TRUE;
00262 break;
00263 case 'G':
00264 Gflag = TRUE;
00265 break;
00266 case 'V':
00267 Vflag = TRUE;
00268 break;
00269 case 'N':
00270 Nflag = TRUE;
00271 break;
00272 case 'f':
00273 fflag = TRUE;
00274 break;
00275 case 'F':
00276 fflag = TRUE;
00277 Fflag = TRUE;
00278 break;
00279 case 'R':
00280 Rflag = TRUE;
00281 break;
00282 case 'O':
00283 if (OOflag) {
00284 OOOflag = TRUE;
00285 } else if (Oflag) {
00286 OOflag = TRUE;
00287 } else {
00288 Oflag = TRUE;
00289 }
00290 break;
00291 case 'X':
00292 Xflag = TRUE;
00293 break;
00294 case 'x':
00295 xflag = TRUE;
00296 break;
00297 case 'h':
00298 hflag = TRUE;
00299 break;
00300 default:
00301 dbgmsg("Rc: illegal flag option: %c\n", *flagp);
00302 abort();
00303 }
00304 }
00305 }
00306
00307 case '\0' :
00308 break;
00309
00310 default:
00311 if ( actual_ifname == (char *)0 ) {
00312 actual_ifname = argv[argn];
00313 } else if ( ifname == (char *)0 ) {
00314 ifname = argv[argn];
00315 } else if (ofname == (char *)0) {
00316 ofname = argv[argn];
00317 } else if (sigfname == ((char *)0) && cflag) {
00318 sigfname = argv[argn];
00319 } else if (optfname == ((char *)0) && OOflag) {
00320 optfname = argv[argn];
00321 } else {
00322 fprintf(stderr, "Rc: Too many args\n");
00323 exit(1);
00324 }
00325 break;
00326 }
00327 }
00328
00329 if (ofname == ((char *)0)) {
00330 fprintf(stderr, "Rc: Too few arguments\n");
00331 exit(1);
00332 }
00333 if (cflag && sigfname == ((char *)0)) {
00334 fprintf(stderr, "Rc: Missing file name for signature info\n");
00335 exit(1);
00336 }
00337 if (OOflag && optfname == ((char *)0)) {
00338 fprintf(stderr, "Rc: Missing file name for optimization info\n");
00339 exit(1);
00340 }
00341
00342 if (Xflag && !Gflag) {
00343 fprintf(stderr, "Vax code generator cannot be used with -X\n");
00344 exit(1);
00345 }
00346
00347
00348 max_int_regs = 0;
00349 max_addr_regs = 0;
00350 if (Mflag && Rflag) {
00351 fprintf (stderr, "-M contradicts -R, ignoring both\n");
00352 Mflag = Rflag = FALSE;
00353 }
00354 # ifdef SUN
00355 if (!Mflag) {
00356 # ifdef GEN_C
00357
00358
00359 Rflag = TRUE;
00360 # else
00361
00362
00363 max_int_regs = 3;
00364 max_addr_regs = 1;
00365 # endif
00366 }
00367 # endif
00368 # ifdef RT
00369 if (!Mflag) {
00370
00371
00372 Rflag = TRUE;
00373 }
00374 # endif
00375
00376
00377 if (insrtptr == NIL ) {
00378 initids();
00379 if (Gflag) {
00380 init_RIC_table();
00381 }
00382 }
00383
00384
00385 if ( freopen( ifname, "r", stdin ) == NULL ) {
00386 fprintf(stderr, "Can't open %s\n", ifname);
00387 exit(1);
00388 }
00389
00390 if (OOflag) {
00391
00392 optfile = fopen(optfname, "w");
00393 if (optfile == NULL) {
00394 fprintf(stderr, "Can't open %s\n", optfname);
00395 exit(1);
00396 }
00397 }
00398
00399 if (cflag || Xflag) {
00400 int ifname_len = strlen(actual_ifname);
00401 char * p;
00402
00403 entry_name = (char *) malloc(ifname_len+1);
00404 strcpy(entry_name, actual_ifname);
00405 if (entry_name[ifname_len-2] == '.') {
00406 entry_name[ifname_len - 2] = '\0';
00407 }
00408
00409 for (p = entry_name; *p != '\0'; p++) {
00410 if (*p == '/') {
00411 *p = '.';
00412 }
00413 }
00414 } else {
00415 entry_name = "";
00416 }
00417
00418
00419 if (!pflag) {
00420 yyinfnm = stt_enter(ifname, strlen(ifname)+1);
00421 yyline = 0;
00422 addposition(yyinfnm, yyline);
00423 }
00424
00425 if (!Gflag) { Rflag = FALSE; }
00426
00427
00428 if (Gflag) {
00429 inline_cnvt = Ginline_cnvt;
00430 } else {
00431 inline_cnvt = Vinline_cnvt;
00432 }
00433
00434
00435
00436
00437 # ifdef VERBOSE
00438 fprintf(stderr, "Calling yyparse\n");
00439 # endif
00440 yyparse();
00441
00442
00443 if( yynerrs != 0 )
00444 exit(1);
00445
00446 if( initflag ) {
00447
00448
00449 int rcxdescr;
00450 struct exec hdr;
00451 unsigned sbrkval;
00452 long symtsize = 4;
00453 long end_text;
00454 long page_size;
00455 # ifdef SUN
00456 long begin_data = roundup(((long) (&etext)), SEGSIZ);
00457 # endif
00458
00459
00460 fflush(stdout);
00461
00462 if ( (rcxdescr = creat((Gflag? "Grc.x" : "rc.x"),0755)) == -1 ) {
00463 fprintf(stderr, "Can't create %s\n",
00464 (Gflag? "Grc.x" : "rc.x"));
00465 exit(1);
00466 }
00467
00468 # ifdef SUN
00469 page_size = PAGSIZ;
00470 # else
00471 # ifdef PYRAMID
00472 page_size = 2048;
00473 # else
00474 # ifdef RT
00475 page_size = 2048;
00476 # else
00477 page_size = 1024;
00478 # endif
00479 # endif
00480 # endif
00481 sbrkval = (long) sbrk(0);
00482 if (dloadflag) {
00483 # ifdef SUN
00484 # ifdef LDBUG
00485 end_text = roundup((((long) &etext)-32), page_size);
00486
00487
00488
00489 # else
00490 end_text = roundup(((long) &etext), page_size);
00491 # endif
00492 # else
00493 end_text = roundup(((long) &etext), page_size);
00494 # endif
00495 } else {
00496 end_text = (long) &etext;
00497 }
00498 # ifdef SUN
00499 if (!dloadflag) {
00500 fprintf(stderr, "Use -d instead of -i\n");
00501 exit(1);
00502 }
00503 hdr.a_text = end_text - page_size;
00504 # else
00505 hdr.a_text = end_text;
00506 # endif
00507 if (dloadflag) {
00508 # ifdef VERBOSE
00509 fprintf(stderr,
00510 "Generating demand paged core image\n");
00511 # endif
00512
00513 hdr.a_magic = ZMAGIC;
00514 } else {
00515 # ifdef VERBOSE
00516 fprintf(stderr,
00517 "Generating core image\n");
00518 # endif
00519 hdr.a_magic = OMAGIC;
00520 }
00521 # ifdef SUN
00522 hdr.a_machtype = MACH_TYPE;
00523 hdr.a_data = sbrkval - begin_data;
00524 # else
00525 # ifdef RT
00526 hdr.a_data = sbrkval - 0x10000000;
00527 # else
00528
00529 hdr.a_data = sbrkval - end_text;
00530 # endif
00531 # endif
00532 if (dloadflag) hdr.a_data = roundup(hdr.a_data,page_size);
00533 hdr.a_bss = 0;
00534 hdr.a_syms = 0;
00535 # ifdef SUN
00536 hdr.a_entry = page_size + sizeof(struct exec);
00537 # else
00538 hdr.a_entry = 0;
00539 # endif
00540 hdr.a_trsize = hdr.a_drsize = 0;
00541
00542 write(rcxdescr, &hdr, sizeof (struct exec));
00543 # ifdef SUN
00544
00545 if (write(rcxdescr, hdr.a_entry,
00546 (end_text - hdr.a_entry)) == -1) {
00547 fprintf(stderr, "Couldn't write text segment\n");
00548 perror("Rc");
00549 # ifdef UNDEFINED
00550 fprintf(stderr,
00551 "Descriptor:%X, start:%X, size:%X\n",
00552 rcxdescr, hdr.a_entry,
00553 end_text - hdr.a_entry);
00554 # endif
00555 exit(* ((char *) 0x80000000));
00556 }
00557
00558 if (write(rcxdescr, begin_data,
00559 sbrkval - begin_data) == -1) {
00560 fprintf(stderr, "Couldn't write data segment\n");
00561 perror("Rc");
00562 exit(1);
00563 }
00564 # else
00565 if (dloadflag)
00566 lseek(rcxdescr, page_size, 0);
00567 # ifdef RT
00568 if (write(rcxdescr, (char *) 0, end_text) == -1) {
00569 fprintf(stderr, "Couldn't write text segment\n");
00570 perror("Rc");
00571 exit(1);
00572 }
00573 if (write(rcxdescr, (char *)0x10000000,
00574 sbrkval - 0x10000000)
00575 == -1) {
00576 fprintf(stderr, "Couldn't write data segment\n");
00577 perror("Rc");
00578 exit(1);
00579 }
00580 # else
00581 if (write(rcxdescr, (char *) 0, sbrkval) == -1) {
00582 fprintf(stderr, "Couldn't write initialized file\n");
00583 perror("Rc");
00584 exit(1);
00585 }
00586 # endif
00587 # endif
00588
00589 # ifdef SUN
00590 lseek(rcxdescr,
00591 hdr.a_data + hdr.a_text,
00592 0);
00593 # else
00594 lseek(rcxdescr,
00595 (dloadflag? page_size : sizeof (struct exec))
00596 + hdr.a_data + hdr.a_text,
00597 0);
00598 # endif
00599 write(rcxdescr, &symtsize, sizeof (long));
00600 exit(0);
00601 }
00602
00603
00604
00605 build_Idtable();
00606
00607 if( yynerrs != 0 )
00608 exit(1);
00609
00610
00611
00612
00613 # ifdef VERBOSE
00614 fprintf(stderr, "Building symbol table\n");
00615 # endif
00616 build_symtab( stxtree );
00617 if (yynerrs != 0) exit(2);
00618
00619
00620
00621
00622
00623 # ifdef VERBOSE
00624 fprintf(stderr, "Fixing up syntax tree\n");
00625 # endif
00626 fixup (stxtree);
00627 if (yynerrs != 0) exit(3);
00628
00629
00630 if (yydebug)
00631 prtree(stxtree);
00632
00633 GC_dont_gc = FALSE;
00634
00635
00636
00637
00638
00639
00640
00641 if (Gflag) {
00642 spcl_to_inline = Gspcl_to_inline;
00643 } else {
00644 spcl_to_inline = Vspcl_to_inline;
00645 }
00646
00647 # ifdef VERBOSE
00648 fprintf(stderr, "Checking signatures\n");
00649 # endif
00650 tl_checksigs(stxtree);
00651 if (yynerrs != 0) exit(1);
00652
00653 if (yydebug)
00654 prtree(stxtree);
00655
00656
00657
00658
00659
00660
00661 # ifdef VERBOSE
00662 fprintf(stderr, "Generating code\n");
00663 # endif
00664 ofile = fopen(ofname, "w");
00665 if ( ofile == NULL ) {
00666 fprintf(stderr, "Can't create output file (%s)\n", ofname);
00667 exit(8);
00668 }
00669 if (aflag)
00670
00671 fprintf(stderr,
00672 "Abstract syntax generation no longer implemented\n");
00673 else {
00674 extern NODE * bld_den_seq_f();
00675 extern NODE * signature_f();
00676 extern NODE * first_elmnt();
00677
00678
00679 if (cflag) {
00680
00681
00682 if (Gflag) {
00683 Ggeneratepcode( ofile, stxtree,
00684 first_elmnt(bld_den_seq_f(insrtptr)) );
00685 } else {
00686 Vgeneratepcode( ofile, stxtree,
00687 first_elmnt(bld_den_seq_f(insrtptr)) );
00688 }
00689 if (yynerrs > 0) exit(1);
00690 sigfile = fopen(sigfname, "w");
00691 if (sigfile == NULL) {
00692 fprintf(stderr, "Can't open %s\n", sigfname);
00693 exit(1);
00694 }
00695
00696 putw(&etext, sigfile);
00697
00698
00699
00700 {
00701 extern FILE * objfilelist;
00702 int c;
00703
00704
00705
00706 if (objfilelist != NULL) {
00707 fclose(objfilelist);
00708 }
00709 objfilelist = fopen(OBJFILELIST, "r");
00710 if (objfilelist != NULL) {
00711 while ((c = getc(objfilelist)) != EOF) {
00712 putc(c, sigfile);
00713 }
00714 }
00715 putc('\n', sigfile);
00716 }
00717 sig_out(sigfile, signature_f(insrtptr));
00718 fclose(sigfile);
00719 } else if (Xflag) {
00720
00721
00722 GgenerateXcode( ofile, stxtree,
00723 first_elmnt(bld_den_seq_f(insrtptr)) );
00724 } else {
00725 if (Gflag) {
00726 Ggeneratecode( ofile, stxtree );
00727 } else {
00728 Vgeneratecode( ofile, stxtree );
00729 }
00730 if (yynerrs > 0) exit(1);
00731 }
00732 }
00733 fflush( ofile );
00734 exit(0);
00735 }