|
|
1.1 root 1: /*ident "@(#)ctrans:src/main.c 1.6.7.63" */
2: /***********************************************************************
3:
4: C++ source for cfront, the C++ compiler front-end
5: written in the computer science research center of Bell Labs
6:
7: Copyright (c) 1984 AT&T, Inc. All rigths Reserved
8: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
9:
10: main.c:
11:
12: Initialize global environment
13: Read argument line
14: Start compilation
15: Clean up and exit
16:
17: **************************************************************************/
18:
19: #ifdef __cplusplus
20: #include <stdlib.h>
21: #endif
22:
23: #include <ctype.h>
24: #include "size.h"
25: #include "cfront.h"
26:
27: char* prog_name = "<<AT&T C++ Translator 2.1.0+ 04/01/90>>";
28: char* src_file_name = "";
29: char* line_format = "\n# %d \"%s\"\n";
30:
31: #ifdef unix
32: #include <signal.h>
33:
34: static void core_dump(int = 0)
35: {
36: if (error_count)
37: fprintf(stderr,"sorry, cannot recover from previous error\n");
38: else
39: error('i',"bus error (or something nasty like that)");
40: ext(99);
41: }
42: #endif
43:
44: Plist isf_list;
45: Pstmt st_ilist;
46: Pstmt st_dlist;
47: Ptable sti_tbl;
48: Ptable std_tbl;
49: Plist stat_mem_list;
50:
51: int vtbl_opt = -1; // how to deal with vtbls:
52: // -1 static and defined
53: // 0 external and supposed to be defined elsewhere
54: // 1 external and defined
55: int debug_opt;
56: int gplus_opt; // -g to cc, overrides print optimization
57: int ansi_opt;
58: int strict_opt; // disallow features from the anachronism section
59: // of the manual
60: int warning_opt; // produce more warnings if set
61: bit stmtno = 0;
62:
63: extern simpl_init();
64: extern typ_init();
65: extern syn_init();
66: extern lex_init();
67: extern error_init();
68:
69: char *st_name(char*); // generates names of static ctor, dtor callers, ptbl_vec
70: Pname def_name; // first definition in file
71: Pname pdef_name; // used with ptbl_vec if function is first definition
72:
73: void run()
74: /*
75: run the appropriate stages
76: */
77: {
78: Pname n;
79:
80: while (n=syn()) {
81: if (n == Pname(1)) continue;
82: if (n->n_list) PERM(n->tp);
83:
84: DB( if(Rdebug>=1) error('d',"run: syn(): '%s'", n->string););
85:
86: for (Pname nx, nn=n; nn; nn=nx) {
87: Pname rr;
88: nx = nn->n_list;
89: nn->n_list = 0;
90:
91: DB( if(Rdebug>=1) error('d',"run: dcl(): %n",nn););
92:
93: if ((rr=nn->dcl(gtbl,EXTERN))==0
94: || nn->base==0
95: || error_count) continue;
96:
97: DB( if(Rdebug>=1) error('d',"run: simpl(): %n",nn););
98:
99: nn->simpl();
100:
101: if (error_count) continue;
102:
103: DB( if(Rdebug>=1) error('d',"run:print(): %n (%k) tp %t",nn,nn->base,nn->tp););
104:
105: if (nn->base == TNAME)
106: nn->dcl_print(0);
107: else {
108: Ptype t = nn->tp;
109: llxx:
110: switch (t->base) {
111: case TYPE:
112: t = Pbase(t)->b_name->tp; goto llxx;
113: case CLASS:
114: DB( if(Rdebug>=1) fprintf(stderr," -- class\n"););
115: if (vtbl_opt==1 || gplus_opt) n->dcl_print(0);
116: break;
117: case ENUM:
118: DB( if(Rdebug>=1) fprintf(stderr," -- enum\n"););
119: Penum(n->tp)->dcl_print(0);
120: break;
121: case FCT:
122: DB( if(Rdebug>=1) error('d'," -- fct%n inline %d body %d",nn,Pfct(t)->f_inline,Pfct(t)->body););
123: if (Pfct(t)->body==0
124: || (debug_opt==0 && Pfct(t)->f_inline && ((n->n_table!=gtbl) || strcmp(nn->string,"main")))
125: || Pfct(t)->f_imeasure && Pfct(t)->f_inline==0) break;
126: DB( if(Rdebug>=1) error('d',"rr %n inline %d body %d",rr,Pfct(rr->tp)->f_inline,Pfct(rr->tp)->body););
127: rr->dcl_print(0);
128: break;
129:
130: default:
131: DB( if(Rdebug>=1) error('d'," -- %n tpbase: %k",nn,nn->tp->base););
132: // print class def, if appropriate
133: // can't use tsizeof() since this may
134: // print an error if class isn't defined
135: { for(Ptype tx = t;;) {
136: switch ( tx->base ) {
137: case VEC:
138: tx=Pvec(tx)->typ;
139: continue;
140: case TYPE: case COBJ:
141: tx=Pbase(tx)->b_name->tp;
142: continue;
143: }
144: Pclass cl = Pclass(tx);
145: if ( cl->base == CLASS
146: && (cl->defined&(DEFINED|SIMPLIFIED))
147: && cl->c_body==1 )
148: cl->dcl_print(0);
149: break;
150: }
151: }
152: nn->dcl_print(0);
153: }
154: }
155:
156: if (error_count) continue;
157:
158: DB( if(Rdebug>=1) error('d',"run: cleanup"););
159: switch (nn->tp->base) { // clean up
160: default:
161: { Pexpr i = nn->n_initializer;
162: DB( if(Rdebug>=1) error('d',"default nn %n i %d",nn,i););
163: if (i && i!=Pexpr(1)) {
164: DEL(i);
165: nn->n_initializer = 0;
166: if (def_name==0 && rr->n_scope != STATIC) {
167: def_name = rr;
168: if (pdef_name == 0) ptbl_init(0);
169: }
170: }
171: break;
172: }
173:
174: case FCT:
175: {
176: Pfct f = Pfct(nn->tp);
177: DB( if(Rdebug>=1) error('d', "function nn: %n local_class: %d %d", nn, local_class, f->local_class ););
178: if (f->body
179: && f->f_inline==0
180: && f->f_imeasure==0) {
181: extern void delete_local();
182: if ( local_class = f->local_class ) {
183: delete_local();
184: local_class = 0;
185: }
186: if (f->f_this) f->f_this->n_table = 0;
187: DEL(f->body);
188: if (def_name==0) {
189: def_name = rr;
190: if (pdef_name == 0) ptbl_init(0);
191: }
192: }
193: break;
194: }
195:
196: case CLASS:
197: { Pclass cl = Pclass(nn->tp);
198: for (Pname px, p=cl->mem_list; p; p=px) {
199: px = p->n_list;
200: if (p->tp) {
201: switch (p->tp->base) {
202: case FCT:
203: { Pfct f = (Pfct)p->tp;
204: if (f->body) {
205: if (f->f_inline==0
206: && f->f_imeasure==0) {
207: if ( local_class = f->local_class ) {
208: delete_local();
209: local_class = 0;
210: }
211: DEL(f->body);
212: f->body = 0;
213: }
214: }
215: }
216: case CLASS:
217: case ENUM:
218: break;
219: case COBJ:
220: case EOBJ:
221: DEL(p);
222: break;
223: default:
224: delete p;
225: }
226: } else {
227: delete p;
228: }
229: } // for
230: cl->mem_list = 0;
231: cl->permanent = 3;
232: break;
233: }
234: }
235: DEL(nn);
236: }
237: lex_clear();
238: }
239:
240: switch (no_of_undcl) {
241: case 1: {error('w',"undeclaredF%n called",undcl);}
242: case 0: break;
243: default: {error('w',"%d undeclaredFs called; for example%n",no_of_undcl,undcl);}
244: }
245:
246: switch (no_of_badcall) {
247: case 1: {error('w',"%n declaredWoutAs calledWAs",badcall);}
248: case 0: break;
249: default: {error('w',"%d Fs declaredWoutAs calledWAs; for example%n",no_of_badcall,badcall);}
250: }
251:
252: if (error_count) return;
253:
254: int i = 1;
255: for (Pname m=gtbl->get_mem(i); m; m=gtbl->get_mem(++i)) {
256: if (m->base==TNAME
257: || m->n_sto==EXTERN
258: || m->n_stclass == ENUM) continue;
259:
260: Ptype t = m->tp;
261: if (t == 0) continue;
262: ll:
263: switch (t->base) {
264: case TYPE: t=Pbase(t)->b_name->tp; goto ll;
265: case CLASS:
266: case ENUM:
267: case OVERLOAD: continue;
268: case COBJ:
269: //case ANON:
270: case VEC: break;
271: case FCT:
272: if (Pfct(t)->f_inline || Pfct(t)->body==0) continue;
273: }
274:
275: //error('d',&m->where,"%n %d %d %s",m,m->n_addr_taken,m->n_used,m->n_sto==STATIC?"static":"");
276: //xxx doesn't check anon union members as their use bits are set differently
277: if (m->n_addr_taken==0
278: && m->n_used==0
279: && m->n_sto == STATIC) { // (static or anon?)
280: for(;;) {
281: //error('d',&m->where," %t tconst(): %d",t,t->tconst());
282: if(t->base==TYPE) {
283: if(t->tconst()) break;
284: t=Pbase(t)->b_name->tp;
285: } else if(t->base==VEC) {
286: t=Pvec(t)->typ;
287: } else {
288: if ( t->tconst() == 0 ) {
289: if ( t->base != COBJ )
290: error('w',&m->where,"%n defined but not used",m);
291: else { Pclass cl;
292: cl = Pclass(Pbase(t)->b_name->tp);
293: if ( cl->has_ctor()==0 )
294: if ( cl->csu == ANON )
295: ;//error('w',&m->where,"anonymous union defined but not used");
296: else
297: error('w',&m->where,"%n defined but not used",m);
298: }
299: } // if const
300: break;
301: }
302: } // for
303: } // if static and not used
304: }
305:
306: Pname ctor = 0;
307: Pname dtor = 0;
308: --curloc.line;
309:
310: if (st_ilist) { // make an "init" function;
311: // it calls all constructors for static objects
312: DB( if(Rdebug>=1) error('d',"make sti"); );
313: Pname n = new name( st_name("__sti__") );
314: Pfct f = new fct(void_type,0,1);
315: n->tp = f;
316: f->body = new block(st_ilist->where,0,st_ilist);
317: // f->body->s = st_ilist;
318: f->body->memtbl = sti_tbl;
319: n->n_sto = EXTERN;
320: // assignments here are really initializations:
321: ignore_const++;
322: f->f_linkage = linkage_C;
323: f->sign();
324: (void) n->dcl(gtbl,EXTERN);
325: ignore_const--;
326: n->simpl();
327: n->dcl_print(0);
328: ctor = n;
329: }
330:
331: if (st_dlist) { // make a "done" function;
332: // it calls all destructors for static objects
333: DB( if(Rdebug>=1) error('d',"make std"); );
334: Pname n = new name( st_name("__std__") );
335: Pfct f = new fct(void_type,0,1);
336: n->tp = f;
337: f->body = new block(st_dlist->where,0,st_dlist);
338: // f->body->s = st_dlist;
339: f->body->memtbl = std_tbl;
340: n->n_sto = EXTERN;
341: f->f_linkage = linkage_C;
342: f->sign();
343: (void) n->dcl(gtbl,EXTERN);
344: n->simpl();
345: n->dcl_print(0);
346: dtor = n;
347: }
348:
349: #ifdef PATCH
350: /*For fast load: make a static "__link" */
351: if (ctor || dtor)
352: {
353: printf("static struct __linkl { struct __linkl * next;\n");
354: if ( ansi_opt )
355: printf("void (*ctor)(); void (*dtor)(); } __link = \n");
356: else
357: printf("char (*ctor)(); char (*dtor)(); } __link = \n");
358: // printf("{ (struct __linkl *)0, %s, %s };\n",
359: // ctor_name ? ctor_name : "0",
360: // dtor_name ? dtor_name : "0");
361: putstring("{ (struct __linkl *)0, ");
362: if (ctor) ctor->print(); else putch('0');
363: putch(',');
364: if (dtor) dtor->print(); else putch('0');
365: putstring("};\n");
366: }
367: #endif
368:
369: DB( if(Rdebug>=1) error( 'd', "run: vlist: %d", vlist ); );
370: do {
371: for (vl* v = vlist; v; v = v->next) v->cl->really_print(v->vt);
372: vlist = 0;
373:
374: for (Plist l=isf_list; l; l=l->l) {
375: Pname n = l->f;
376: Pfct f = Pfct(n->tp);
377: DB( if(Rdebug>=1)error('d',"isf %n %t f %d addr %d",n,f,f,n->n_addr_taken); );
378: if (f->base == OVERLOAD) {
379: n = Pgen(f)->fct_list->f; // first fct
380: f = Pfct(n->tp);
381: }
382:
383: if (debug_opt==0 &&
384: n->n_addr_taken) {
385: f->f_inline = 0;
386: if (n->n_dcl_printed<2) {
387: if (warning_opt)
388: error('w',"out-of-line copy of %n created",n);
389: n->dcl_print(0);
390: }
391: }
392: }
393: } while (vlist);
394:
395: if (strict_opt == 0) { // define static members
396: // patch for SysV VAX -g linkage botch
397: for (Plist l=stat_mem_list; l; l=l->l) {
398: Pname n = l->f;
399: // error('d',"stat mem %n %d %d ",n,n->n_initializer,n->n_evaluated);
400: if (n->n_initializer==0 && n->n_evaluated==0) {
401: Ptype t = n->tp;
402: n->n_sto = 0;
403: Pname cn = t->is_cl_obj();
404: if (cn==0) cn = cl_obj_vec;
405: if (cn) (void) t->tsizeof(); // be sure to print class
406: if ((cn && Pclass(cn->tp)->has_ctor())
407: || t->is_ref())
408: ; // force explicit definition
409: else
410: n->dcl_print(0);
411: }
412: }
413: }
414:
415: i = 1;
416: for (Pname nm=ptbl->get_mem(i); nm; nm=ptbl->get_mem(++i)) {
417: DB( if(Rdebug>=1) error('d',"ptbl anme %s string2 %s key:%k",nm->string==0?"???":nm->string2,nm->string2==0?"???":nm->string2,nm->n_key); );
418: if ( nm->n_key == 0 ) { // ptbl used in file, generate definition
419: int str1 = (*src_file_name)?strlen(src_file_name)+2:0; // +2 for __
420: int str2 = strlen(nm->string2) - str1;
421: char *ps = new char[ str2 + 1 ];
422: strncpy( ps, nm->string2, str2 ); // grab vtbl name
423: ps[str2]='\0';
424: ps[2] = 'v';
425: fprintf(out_file,"extern struct __mptr %s[];\n",ps);
426: // fprintf(out_file,"struct __mptr* %s = %s;\n",nm->string2,ps);
427: ptbl_add_pair(nm->string2,ps);
428: delete ps;
429: }
430: }
431:
432: ptbl_init(1);
433: curloc.putline();
434: fprintf(out_file,"\n/* the end */\n");
435: }
436:
437:
438: #ifdef DBG
439: int Adebug = 0;
440: int Ddebug = 0;
441: int Edebug = 0;
442: int Ldebug = 0;
443: int Mdebug = 0;
444: int Ndebug = 0;
445: int Pdebug = 0;
446: int Rdebug = 0;
447: int Sdebug = 0;
448: int Tdebug = 0;
449: //Ydebug == yydebug initialized in y.tab.c
450: void
451: process_debug_flags( char* p )
452: {
453: // arg to +Dxxx or debugging comments //@!xxx
454: // format of xxx == sequence of any combination of
455: // +flags -- increment flags (default)
456: // -flags -- decrement flags
457: // 0flags -- unset flags
458: // where flags are single chars
459: int incr = 1;
460: char c;
461: fprintf(stderr,"\n*** processing debugging flags '%s'\n",p);
462: while ( c = *p++ ) {
463: switch ( c ) {
464: case '+': incr = 1; break;
465: case '-': incr = -1; break;
466: case '0': incr = 0; break;
467: case ' ': break;
468: // flags...
469: case 'A': if(incr==0) Adebug=0; else Adebug+=incr; break;
470: case 'D': if(incr==0) Ddebug=0; else Ddebug+=incr; break;
471: case 'E': if(incr==0) Edebug=0; else Edebug+=incr; break;
472: case 'L': if(incr==0) Ldebug=0; else Ldebug+=incr; break;
473: case 'M': if(incr==0) Mdebug=0; else Mdebug+=incr; break;
474: case 'N': if(incr==0) Ndebug=0; else Ndebug+=incr; break;
475: case 'P': if(incr==0) Pdebug=0; else Pdebug+=incr; break;
476: case 'R': if(incr==0) Rdebug=0; else Rdebug+=incr; break;
477: case 'S': if(incr==0) Sdebug=0; else Sdebug+=incr; break;
478: case 'T': if(incr==0) Tdebug=0; else Tdebug+=incr; break;
479: case 'Y': if(incr==0) Ydebug=0; else Ydebug+=incr; break;
480: default: error('w',"unknown debugging flag '%c'",c);
481: }
482: }
483: }
484: #endif /*DBG*/
485:
486: int no_of_undcl, no_of_badcall;
487: Pname undcl, badcall;
488:
489: main(int argc, char* argv[])
490: /*
491: read options, initialize, and run
492: */
493: {
494: register char * cp;
495: char* afile = "";
496: /*
497: #ifdef unix
498:
499: #ifdef COMPLETE_SIG_PF
500: signal(SIGILL,core_dump);
501: signal(SIGIOT,core_dump);
502: signal(SIGEMT,core_dump);
503: signal(SIGFPE,core_dump);
504: signal(SIGBUS,core_dump);
505: signal(SIGSEGV,core_dump);
506: #else
507: typedef void (*ST)(int ...); // trick to circumvent problems with old
508: ST sick = ST(&signal); // (or C) versions <signal.h>
509: (*sick)(SIGILL,core_dump);
510: (*sick)(SIGIOT,core_dump);
511: (*sick)(SIGEMT,core_dump);
512: (*sick)(SIGFPE,core_dump);
513: (*sick)(SIGBUS,core_dump);
514: (*sick)(SIGSEGV,core_dump);
515:
516: #endif
517: #endif
518: */
519:
520: // SUM: not needed
521: // #ifdef apollo
522: // set_sbrk_size(1000000); // resets free store size
523: //#else
524: #ifndef apollo
525: (void) malloc(0); // suppress cashing in V8 malloc
526: #endif
527:
528: error_init();
529:
530: for (int i=1; i<argc; ++i) {
531: switch (*(cp=argv[i])) {
532: case '+':
533: while (*++cp) {
534: switch(*cp) {
535: case 'f':
536: src_file_name = cp+1;
537: goto xx;
538: case 'x': // read cross compilation table
539: if (read_align(afile = cp+1)) {
540: fprintf(stderr,"bad size-table (option +x)\n");
541: exit(11);
542: }
543: goto xx;
544: case 'e':
545: switch (*++cp) {
546: case '0':
547: case '1':
548: vtbl_opt = *cp-'0';
549: break;
550: default:
551: fprintf(stderr,"bad +e option\n");
552: exit(11);
553: }
554: break;
555: case 'd':
556: debug_opt = 1;
557: break;
558: case 'g':
559: gplus_opt = 1;
560: break;
561: case 'D':
562: #ifdef DBG
563: process_debug_flags(++cp);
564: #else
565: error('w',"cfront not compiled for debugging -- +D%s ignored",++cp);
566: #endif
567: goto xx;
568: case 'w':
569: warning_opt = 1;
570: break;
571: case 'a':
572: switch (*++cp) {
573: case '0':
574: case '1':
575: ansi_opt = *cp-'0';
576: break;
577: default:
578: fprintf(stderr,"bad +a option\n");
579: exit(11);
580: }
581: break;
582: case 'p':
583: strict_opt = 1;
584: break;
585: case 'L':
586: line_format = "\n#line %d \"%s\"\n";
587: break;
588: default:
589: fprintf(stderr,"%s: unexpected option: +%c ignored\n",prog_name,*cp);
590:
591: }
592: }
593: xx:
594: break;
595: default:
596: fprintf(stderr,"%s: bad argument \"%s\"\n",prog_name,cp);
597: exit(11);
598: }
599: }
600:
601:
602: fprintf(out_file,line_format+1,1,src_file_name); // strips leading \n
603: fprintf(out_file,"\n/* %s */\n",prog_name);
604: if (*src_file_name) fprintf(out_file,"/* < %s > */\n",src_file_name);
605:
606: // if (Nspy) {
607: // start_time = time(0);
608: // print_align(afile);
609: // }
610: fflush(stderr);
611: otbl_init();
612: lex_init();
613: syn_init();
614: typ_init();
615: simpl_init();
616: scan_started = 1;
617: curloc.putline();
618: if ((BI_IN_BYTE==0) && (SZ_INT==0) && (SZ_WPTR==0))
619: error("no size/alignment values - use +x of provide #ifdef in size.h");
620: run();
621: // if (Nspy) {
622: // stop_time = time(0);
623: // spy(src_file_name);
624: // }
625: exit( (0<=error_count && error_count<127) ? error_count : 127);
626: }
627:
628: char* st_name(char* pref)
629: /*
630: make name "pref|source_file_name|_" or "pref|source_file_name|_"
631: where non alphanumeric characters are replaced with '_'
632: and add def_name at end to ensure uniqueness
633: */
634: {
635: int prefl = strlen(pref);
636: int strl = prefl + 2; // trailing '_' and 0
637: if (*src_file_name) strl += strlen(src_file_name);
638: char* defs;
639: int defl;
640: if (def_name) {
641: defs = def_name->string;
642: defl = strlen(defs)+1; // '_'
643: }
644: else {
645: defs = 0;
646: defl = 0;
647: }
648: char* name = new char[strl+defl];
649: strcpy(name,pref);
650: if (*src_file_name) strcpy(name+prefl,src_file_name);
651: name[strl-2] = '_';
652: name[strl-1] = 0;
653: // char *p = name;
654: // while ( *++p ) if (!isalpha(*p) && !isdigit(*p)) *p = '_';
655: for (char* p = name; *p; p++) if (!isalpha(*p) && !isdigit(*p)) *p = '_';
656: if (defs) {
657: strcpy(name+strl-1,defs); // after the '_'
658: name[strl+defl-2] = '_';
659: name[strl+defl-1] = 0;
660: }
661: #ifdef DENSE
662: void chop(char*);
663: chop(name);
664: #endif
665:
666: return name;
667: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.