#include #include #include #include "../2c/2.out.h" #include "a.h" #include "y.tab.h" void main(int argc, char *argv[]) { char ofile[100], incfile[20], *p; int nout, nproc, status, i, c, of; thechar = '2'; thestring = "68020"; memset(debug, 0, sizeof(debug)); cinit(); outfile = 0; include[ninclude++] = "."; ARGBEGIN { default: c = ARGC(); if(c >= 0 || c < sizeof(debug)) debug[c] = 1; break; case 'o': outfile = ARGF(); break; case 'D': p = ARGF(); if(p) Dlist[nDlist++] = p; break; case 'I': p = ARGF(); setinclude(p); break; } ARGEND if(*argv == 0) { print("usage: %ca [-options] file.s\n", thechar); errorexit(); } nproc = 3; if(p = getenv("NPROC")) nproc = atol(p); if(argc > 1) { c = 0; nout = 0; for(;;) { while(nout < nproc && argc > 0) { i = fork(); if(i < 0) { i = mywait(&status); if(i < 0) errorexit(); if(status) c++; nout--; continue; } if(i == 0) { print("%s:\n", *argv); goto child; } nout++; argc--; argv++; } i = mywait(&status); if(i < 0) { if(c) errorexit(); exits(0); } if(status) c++; nout--; } } child: strcpy(ofile, *argv); if(p = strrchr(ofile, '/')) { include[0] = ofile; *p++ = 0; } else p = ofile; if(outfile == 0) { outfile = p; if(p = strrchr(outfile, '.')) if(p[1] == 's' && p[2] == 0) p[0] = 0; p = strrchr(outfile, 0); p[0] = '.'; p[1] = thechar; p[2] = 0; } /* copied from amos's code. needs lib9 */ if(plan9()) { strcpy(incfile, "/"); strcat(incfile, thestring); strcat(incfile, "/include"); } else { strcpy(incfile, "/usr/%include"); p = strrchr(incfile, '%'); if(p) *p = thechar; } if(!(p = getenv("INCLUDE"))) p = incfile; setinclude(p); of = mycreat(outfile, 0664); if(of < 0) { yyerror("%ca: cannot create %s", thechar, outfile); errorexit(); } Binit(&obuf, of, OWRITE); pass = 1; pinit(*argv); for(i=0; itype = itab[i].type; s->value = itab[i].value; } ALLOCN(pathname, 0, 100); if(getwd(pathname, 99) == 0) { ALLOCN(pathname, 100, 900); if(getwd(pathname, 999) == 0) strcpy(pathname, "/???"); } } void syminit(Sym *s) { s->type = LNAME; s->value = 0; } void cclean(void) { Gen2 g2; g2.from = nullgen; g2.to = nullgen; outcode(AEND, &g2); Bflush(&obuf); } void zname(char *n, int t, int s) { Bputc(&obuf, ANAME); /* as */ Bputc(&obuf, ANAME>>8); Bputc(&obuf, t); /* type */ Bputc(&obuf, s); /* sym */ while(*n) { Bputc(&obuf, *n); n++; } Bputc(&obuf, 0); } void zaddr(Gen *a, int s) { long l; int i, t; char *n; Ieee e; t = 0; if(a->field) t |= T_FIELD; if(a->index != D_NONE || a->displace != 0) t |= T_INDEX; if(a->s0.offset != 0) t |= T_OFFSET; if(s != 0) t |= T_SYM; if(a->type == D_FCONST) t |= T_FCONST; else if(a->type == D_SCONST) t |= T_SCONST; else if(a->type & ~0xff) t |= T_TYPE; Bputc(&obuf, t); if(t & T_FIELD) { /* implies field */ i = a->field; Bputc(&obuf, i); Bputc(&obuf, i>>8); } if(t & T_INDEX) { /* implies index, scale, displace */ i = a->index; Bputc(&obuf, i); Bputc(&obuf, i>>8); Bputc(&obuf, a->scale); l = a->displace; Bputc(&obuf, l); Bputc(&obuf, l>>8); Bputc(&obuf, l>>16); Bputc(&obuf, l>>24); } if(t & T_OFFSET) { /* implies offset */ l = a->s0.offset; Bputc(&obuf, l); Bputc(&obuf, l>>8); Bputc(&obuf, l>>16); Bputc(&obuf, l>>24); } if(t & T_SYM) /* implies sym */ Bputc(&obuf, s); if(t & T_FCONST) { ieeedtod(&e, a->dval); l = e.l; Bputc(&obuf, l); Bputc(&obuf, l>>8); Bputc(&obuf, l>>16); Bputc(&obuf, l>>24); l = e.h; Bputc(&obuf, l); Bputc(&obuf, l>>8); Bputc(&obuf, l>>16); Bputc(&obuf, l>>24); return; } if(t & T_SCONST) { n = a->sval; for(i=0; itype; Bputc(&obuf, i); if(t & T_TYPE) Bputc(&obuf, i>>8); } void outcode(int a, Gen2 *g2) { int sf, st, t; Sym *s; if(pass == 1) goto out; jackpot: sf = 0; s = g2->from.s0.sym; while(s != S) { sf = s->sym; if(sf < 0 || sf >= NSYM) sf = 0; t = g2->from.type & D_MASK; if(h[sf].type == t) if(h[sf].sym == s) break; zname(s->name, t, sym); s->sym = sym; h[sym].sym = s; h[sym].type = t; sf = sym; sym++; if(sym >= NSYM) sym = 1; break; } st = 0; s = g2->to.s0.sym; while(s != S) { st = s->sym; if(st < 0 || st >= NSYM) st = 0; t = g2->to.type & D_MASK; if(h[st].type == t) if(h[st].sym == s) break; zname(s->name, t, sym); s->sym = sym; h[sym].sym = s; h[sym].type = t; st = sym; sym++; if(sym >= NSYM) sym = 1; if(st == sf) goto jackpot; break; } Bputc(&obuf, a); Bputc(&obuf, a>>8); Bputc(&obuf, lineno); Bputc(&obuf, lineno>>8); Bputc(&obuf, lineno>>16); Bputc(&obuf, lineno>>24); zaddr(&g2->from, sf); zaddr(&g2->to, st); out: if(a != AGLOBL && a != ADATA) pc++; } void outhist(void) { Gen g; Hist *h; char *p, *q, *op; int n; g = nullgen; for(h = hist; h != H; h = h->link) { p = h->name; op = 0; if(p && p[0] != '/' && h->offset == 0 && pathname && pathname[0] == '/') { op = p; p = pathname; } while(p) { q = strchr(p, '/'); if(q) { n = q-p; if(n == 0) n = 1; /* leading "/" */ q++; } else { n = strlen(p); q = 0; } if(n) { Bputc(&obuf, ANAME); Bputc(&obuf, ANAME>>8); Bputc(&obuf, D_FILE); /* type */ Bputc(&obuf, 1); /* sym */ Bputc(&obuf, '<'); Bwrite(&obuf, p, n); Bputc(&obuf, 0); } p = q; if(p == 0 && op) { p = op; op = 0; } } g.s0.offset = h->offset; Bputc(&obuf, AHISTORY); Bputc(&obuf, AHISTORY>>8); Bputc(&obuf, h->line); Bputc(&obuf, h->line>>8); Bputc(&obuf, h->line>>16); Bputc(&obuf, h->line>>24); zaddr(&nullgen, 0); zaddr(&g, 0); } } #include "../cc/lexbody" #include "../cc/macbody"