diff --git a/mach/proto/cg/codegen.c b/mach/proto/cg/codegen.c index cfc1e37bf..9bec53ea1 100644 --- a/mach/proto/cg/codegen.c +++ b/mach/proto/cg/codegen.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include "param.h" #include "tables.h" @@ -12,13 +14,11 @@ static char rcsid[] = "$Id$"; #include "state.h" #include "equiv.h" #include "extern.h" - -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +#include "subr.h" +#include "gencode.h" +#include "reg.h" +#include "salloc.h" +#include "fillem.h" #define SHORTCUT /* Stop searching at distance 0 */ @@ -31,14 +31,16 @@ static char rcsid[] = "$Id$"; #define MAXPATTERN 5 #define MAXREPLLEN 5 /* Max length of EM-replacement, should come from boot */ -byte startupcode[] = { DO_NEXTEM }; +byte startupcode[] = +{ DO_NEXTEM }; -byte *nextem(); -unsigned costcalc(); -unsigned docoerc(); -unsigned stackupto(); +extern byte *nextem(int); string tostring(); +extern int move(token_p,token_p,int,int,unsigned int); +extern struct perm* tuples(rl_p*, int); + + #ifdef NDEBUG #define DEBUG(xxxxx) #else @@ -49,18 +51,22 @@ string tostring(); #define BROKE() {assert(origcp!=startupcode);DEBUG("BROKE");goto doreturn;} #define CHKCOST() {if (totalcost>=costlimit) BROKE();} -unsigned codegen(codep,ply,toplevel,costlimit,forced) byte *codep; unsigned costlimit; { + + + +unsigned int codegen(byte *codep, int ply, int toplevel, unsigned int costlimit, int forced) +{ #ifndef NDEBUG - byte *origcp=codep; - static int level=0; + byte *origcp = codep; + static int level = 0; #endif unsigned totalcost = 0; byte *bp; int n; - unsigned mindistance,dist; + unsigned mindistance, dist; register int i; int cindex; - int npos,npos2,pos[MAXPOS],pos2[MAXPOS]; + int npos, npos2, pos[MAXPOS], pos2[MAXPOS]; #ifdef STONSTACK state_t state; #define SAVEST savestatus(&state) @@ -72,14 +78,14 @@ unsigned codegen(codep,ply,toplevel,costlimit,forced) byte *codep; unsigned cost #define RESTST restorestatus(state) #define FREEST freestatus(state) #endif - unsigned mincost,t; - int texpno,nodeno; + unsigned mincost, t; + int texpno, nodeno; token_p tp; tkdef_p tdp; int tinstno; register struct reginfo *rp; struct reginfo **rpp; - token_t token,mtoken,token2; + token_t token, mtoken, token2; int propno; int exactmatch; int j; @@ -87,606 +93,759 @@ unsigned codegen(codep,ply,toplevel,costlimit,forced) byte *codep; unsigned cost int stringno; result_t result; cost_t cost; - int size,lsize,repllen; + int size, lsize, repllen; int tokexp[MAXPATTERN]; int nregneeded; token_p regtp[MAXCREG]; c3_p regcp[MAXCREG]; rl_p regls[MAXCREG]; - c3_p findcoerc(); +#ifdef MAXSPLIT int sret; +#endif /* MAXSPLIT */ token_t reptoken[MAXREPLLEN]; - int emrepllen,eminstr; - int inscoerc=0; + int emrepllen, eminstr; + int inscoerc = 0; int stackpad; - struct perm *tup,*ntup,*besttup,*tuples(); + struct perm *tup, *ntup, *besttup; #ifndef NDEBUG level++; DEBUG("Entering codegen"); #endif - for (;;) { - switch( (*codep++)&037 ) { - default: - assert(FALSE); - /* NOTREACHED */ - case DO_NEXTEM: - DEBUG("NEXTEM"); - tokpatlen = 0; - nallreg=0; - if (toplevel) { - garbage_collect(); - totalcost=0; - } else { - if (--ply <= 0) - goto doreturn; - } - if (stackheight>MAXFSTACK-7) - totalcost += stackupto(&fakestack[6],ply,toplevel); - bp = nextem(toplevel); - if (bp == 0) { - /* - * No pattern found, can be pseudo or error - * in table. - */ - if (toplevel) { - codep--; - DEBUG("pseudo"); - dopseudo(); - } else - goto doreturn; - } else { -#ifndef NDEBUG - chkregs(); -#endif - if (! toplevel) { - ply -= emp-saveemp+1; - if (ply <= 0) ply = 1; - } - n = *bp++; - assert(n>0 && n<=MAXRULE); - if (n>1) { - mindistance = MAXINT; npos=0; - for(i=0;i1) { - /* - * More than 1 tokenpattern is a candidate. - * Decision has to be made by lookahead. - */ - SAVEST; - mincost = costlimit-totalcost+1; - for(i=0;i MAXFSTACK - 7) + totalcost += stackupto(&fakestack[6], ply, toplevel); + bp = nextem(toplevel); + if (bp == 0) + { + /* + * No pattern found, can be pseudo or error + * in table. + */ + if (toplevel) + { + codep--; + DEBUG("pseudo"); + dopseudo(); } + else + goto doreturn; + } + else + { +#ifndef NDEBUG + chkregs(); +#endif + if (!toplevel) + { + ply -= emp - saveemp + 1; + if (ply <= 0) + ply = 1; + } + n = *bp++; + assert(n>0 && n<=MAXRULE); + if (n > 1) + { + mindistance = MAXINT; + npos = 0; + for (i = 0; i < n; i++) + { + getint(cindex, bp); + dist = distance(cindex); +#ifndef NDEBUG + if (Debug) + fprintf(stderr, "distance of pos %d is %u\n", i, + dist); +#endif + if (dist <= mindistance) + { + if (dist < mindistance) + { +#ifdef SHORTCUT + if (dist == 0) + goto gotit; +#endif + npos = 0; + mindistance = dist; + } + pos[npos++] = cindex; + } + } + assert(mindistance 1) + { + /* + * More than 1 tokenpattern is a candidate. + * Decision has to be made by lookahead. + */ + SAVEST; + mincost = costlimit - totalcost + 1; + for (i = 0; i < npos; i++) + { + t = codegen(&coderules[pos[i]], ply, FALSE, + costlimit < MAXINT ? mincost : MAXINT, + 0); +#ifndef NDEBUG + if (Debug) + fprintf(stderr, + "mincost %u,cost %u,pos %d\n", + mincost, t, i); +#endif + if (t < mincost) + { + mincost = t; + cindex = pos[i]; + } + RESTST; + } FREEST; + if (totalcost + mincost > costlimit) + { + totalcost += mincost; + BROKE(); + } + } + else + { + cindex = pos[0]; + } + } + else + { + getint(cindex, bp); + } + + gotit: + /* + * Now cindex contains the code-index of the best candidate + * so proceed to use it. + */ + codep = &coderules[cindex]; + } + break; + case DO_COERC: + DEBUG("COERC") + ; + tokpatlen = 1; + inscoerc = 1; + break; + case DO_XXMATCH: + DEBUG("XXMATCH") + ; + case DO_XMATCH: + DEBUG("XMATCH") + ; + tokpatlen = (codep[-1] >> 5) & 07; + for (i = 0; i < tokpatlen; i++) + getint(tokexp[i], codep) + ; + tokexp[i] = 0; + break; /* match already checked by distance() */ + case DO_MATCH: + DEBUG("MATCH") + ; + tokpatlen = (codep[-1] >> 5) & 07; + for (i = 0; i < tokpatlen; i++) + getint(tokexp[i], codep) + ; + tokexp[i] = 0; + tp = &fakestack[stackheight - 1]; + i = 0; + while (i < tokpatlen && tp >= fakestack) + { + size = tsize(tp); + while (i < tokpatlen && (lsize = ssize(tokexp[i])) <= size) + { + size -= lsize; + i++; + } + if (i < tokpatlen && size != 0) + { + totalcost += stackupto(tp, ply, toplevel); + CHKCOST(); + break; + } + tp--; + } + tp = &fakestack[stackheight - 1]; + i = 0; + while (i < tokpatlen && tp >= fakestack) + { + size = tsize(tp); + lsize = ssize(tokexp[i]); + if (size != lsize) + { /* find coercion */ +#ifdef MAXSPLIT + sret = split(tp,&tokexp[i],ply,toplevel); + if (sret==0) + { +#endif /* MAXSPLIT */ + totalcost += stackupto(tp, ply, toplevel); + CHKCOST(); + break; +#ifdef MAXSPLIT + } + i += sret; +#endif /* MAXSPLIT */ + } + else + i += 1; + tp--; + } + nextmatch: tp = &fakestack[stackheight - 1]; + i = 0; + nregneeded = 0; + while (i < tokpatlen && tp >= fakestack) + { + if (!match(tp, &machsets[tokexp[i]], 0)) + { + register c3_p cp = findcoerc(tp, &machsets[tokexp[i]]); + if (cp == 0) + { + for (j = 0; j < nregneeded; j++) + regtp[j] -= (tp - fakestack + 1); + totalcost += stackupto(tp, ply, toplevel); + CHKCOST(); + break; + } + else + { + if (cp->c3_prop == 0) + { + totalcost += docoerc(tp, cp, ply, toplevel, 0); + CHKCOST(); + } + else + { + assert(nregneeded stackheight) + { + int k; + stackpad = tokpatlen - stackheight; + for (j = stackheight - 1, k = j + stackpad; j >= 0; + j--, k--) + { + fakestack[k] = fakestack[j]; + /* fakestack[j+stackpad] = fakestack[j]; does not + compile under Xenix + */ + } + for (j = 0; j < stackpad; j++) + fakestack[j].t_token = 0; + stackheight += stackpad; + for (j = 0; j < nregneeded; j++) + regtp[j] += stackpad; + tp = &fakestack[stackpad - 1]; + while (i < tokpatlen && tp >= fakestack) + { + register c3_p cp = findcoerc((token_p) 0, + &machsets[tokexp[i]]); + if (cp == 0) + { + assert(!toplevel); + for (j = 0; j < nregneeded; j++) + myfree(regls[j]); + totalcost = INFINITY; + BROKE(); + } + if (cp->c3_prop == 0) + { + totalcost += docoerc(tp, cp, ply, toplevel, 0); + CHKCOST(); + } + else + { + assert(nregneeded 1) + { + fprintf(stderr, "Next tuple %d,%d,%d,%d\n", + tup->p_rar[0], tup->p_rar[1], tup->p_rar[2], + tup->p_rar[3]); + fprintf(stderr, + "totalcost = %u, costlimit = %u, mincost = %u\n", + totalcost, costlimit, mincost); + } +#endif + ntup = tup->p_next; + for (i = 0, t = 0; i < nregneeded && t < mincost; i++) + t += docoerc(regtp[i], regcp[i], ply, FALSE, + tup->p_rar[i]); +#ifndef NDEBUG + if (Debug > 1) + fprintf(stderr, "cost after coercions: %u\n", t); +#endif + if (t < mincost) + t += codegen(codep, ply, FALSE, + mincost < MAXINT ? mincost - t : MAXINT, 0); + if (t < mincost) + { + mincost = t; + besttup = tup; + } + else + myfree(tup); RESTST; } FREEST; - if (totalcost+mincost>costlimit) { + for (i = 0; i < nregneeded; i++) + myfree(regls[i]); + if (totalcost + mincost > costlimit) + { + if (besttup) + myfree(besttup); + if (stackpad != tokpatlen) + { + if (stackpad) + { + if (costlimit < MAXINT) + { + totalcost = costlimit + 1; + BROKE(); + } + for (i = 0; i < stackheight - stackpad; i++) + fakestack[i] = fakestack[i + stackpad]; + stackheight -= stackpad; + totalcost += stackupto(&fakestack[stackheight - 1], + ply, toplevel); + } + else + totalcost += stackupto(fakestack, ply, toplevel); + CHKCOST(); + goto nextmatch; + } totalcost += mincost; BROKE(); } - } else { - cindex = pos[0]; - } - } else { - getint(cindex,bp); - } - - gotit: - /* - * Now cindex contains the code-index of the best candidate - * so proceed to use it. - */ - codep = &coderules[cindex]; - } - break; - case DO_COERC: - DEBUG("COERC"); - tokpatlen=1; - inscoerc=1; - break; - case DO_XXMATCH: - DEBUG("XXMATCH"); - case DO_XMATCH: - DEBUG("XMATCH"); - tokpatlen=(codep[-1]>>5)&07; - for (i=0;i>5)&07; - for(i=0;i=fakestack) { - size=tsize(tp); - while (i= fakestack) { - size = tsize(tp); - lsize= ssize(tokexp[i]); - if (size != lsize) { /* find coercion */ -#ifdef MAXSPLIT - sret = split(tp,&tokexp[i],ply,toplevel); - if (sret==0) { -#endif /* MAXSPLIT */ - totalcost += stackupto(tp,ply,toplevel); - CHKCOST(); + for (i = 0; i < nregneeded; i++) + totalcost += docoerc(regtp[i], regcp[i], ply, toplevel, + besttup->p_rar[i]); + myfree(besttup); break; -#ifdef MAXSPLIT - } - i += sret; -#endif /* MAXSPLIT */ - } else - i += 1; - tp--; - } - nextmatch: - tp = &fakestack[stackheight-1]; - i=0; nregneeded = 0; - while (i=fakestack) { - if (!match(tp,&machsets[tokexp[i]],0)) { - register c3_p cp = findcoerc(tp, &machsets[tokexp[i]]); - if (cp==0) { - for (j=0;j= &fakestack[0]; tp--) + if (match(tp, &machsets[texpno], nodeno)) + { + /* investigate possible coercion to register */ + totalcost += stackupto(tp, ply, toplevel); + CHKCOST(); + break; + } + for (rp = machregs + 2; rp < machregs + NREGS; rp++) + if (match(&rp->r_contents, &machsets[texpno], nodeno)) + rp->r_contents.t_token = 0; break; - } else { - if (cp->c3_prop==0) { - totalcost+=docoerc(tp,cp,ply,toplevel,0); - CHKCOST(); - } else { - assert(nregneeded= &fakestack[0]; tp--) + if (tp->t_token == -1) + { + if (tp->t_att[0].ar == result.e_v.e_reg) + goto gotone; + } + else + { + tdp = &tokens[tp->t_token]; + for (i = 0; i < TOKENSIZE; i++) + if (tdp->t_type[i] == EV_REG + && tp->t_att[i].ar == result.e_v.e_reg) + goto gotone; + } + break; + gotone: + /* investigate possible coercion to register */ + totalcost += stackupto(tp, ply, toplevel); + CHKCOST() + ; + break; + case DO_DEALLOCATE: + DEBUG("DEALLOCATE") + ; + getint(tinstno, codep) + ; + instance(tinstno, &token); + if (token.t_token == -1) + chrefcount(token.t_att[0].ar, -1, TRUE); + else + { + tdp = &tokens[token.t_token]; + for (i = 0; i < TOKENSIZE; i++) + if (tdp->t_type[i] == EV_REG) + chrefcount(token.t_att[i].ar, -1, TRUE); } - } - } - i++; tp--; - } - if (tokpatlen>stackheight) { - int k; - stackpad = tokpatlen-stackheight; - for (j=stackheight-1, k = j + stackpad;j>=0;j--, k--) { - fakestack[k] = fakestack[j]; - /* fakestack[j+stackpad] = fakestack[j]; does not - compile under Xenix - */ - } - for (j=0;j=fakestack) { - register c3_p cp = findcoerc((token_p) 0, &machsets[tokexp[i]]); - if (cp==0) { - assert(!toplevel); - for (j=0;jc3_prop==0) { - totalcost+=docoerc(tp,cp,ply,toplevel,0); - CHKCOST(); - } else { - assert(nregneeded1) { fprintf(stderr,"Next tuple %d,%d,%d,%d\n", - tup->p_rar[0], - tup->p_rar[1], - tup->p_rar[2], - tup->p_rar[3]); - fprintf(stderr, "totalcost = %u, costlimit = %u, mincost = %u\n", - totalcost, costlimit, mincost); - } -#endif - ntup = tup->p_next; - for (i=0,t=0;ip_rar[i]); -#ifndef NDEBUG -if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t); -#endif - if (tcostlimit) { - if (besttup) - myfree(besttup); - if (stackpad!=tokpatlen) { - if (stackpad) { - if (costlimitr_tcount) + { + rp->r_refcount -= rp->r_tcount; + rp->r_tcount = 0; + } + break; + case DO_ALLOCATE: + DEBUG("ALLOCATE") + ; + if (codep[-1] & 32) + { + getint(propno, codep); + getint(tinstno, codep); } - for (i=0;ip_rar[i]); - myfree(besttup); - break; - case DO_REMOVE: - DEBUG("REMOVE"); - if (codep[-1]&32) { - getint(texpno,codep); - getint(nodeno,codep); - } else { - getint(texpno,codep); - nodeno=0; - } - for (tp= &fakestack[stackheight-tokpatlen-1];tp>=&fakestack[0];tp--) - if (match(tp,&machsets[texpno],nodeno)) { - /* investigate possible coercion to register */ - totalcost += stackupto(tp,ply,toplevel); - CHKCOST(); - break; - } - for (rp=machregs+2;rpr_contents,&machsets[texpno],nodeno)) - rp->r_contents.t_token=0; - break; - case DO_RREMOVE: /* register remove */ - DEBUG("RREMOVE"); - getint(nodeno,codep); - result=compute(&enodes[nodeno]); - assert(result.e_typ==EV_REG); - for (tp= &fakestack[stackheight-tokpatlen-1];tp>=&fakestack[0];tp--) - if (tp->t_token==-1) { - if(tp->t_att[0].ar==result.e_v.e_reg) - goto gotone; - } else { - tdp = &tokens[tp->t_token]; - for(i=0;it_type[i]==EV_REG && - tp->t_att[i].ar==result.e_v.e_reg) - goto gotone; - } - break; - gotone: - /* investigate possible coercion to register */ - totalcost += stackupto(tp,ply,toplevel); - CHKCOST(); - break; - case DO_DEALLOCATE: - DEBUG("DEALLOCATE"); - getint(tinstno,codep); - instance(tinstno,&token); - if (token.t_token==-1) - chrefcount(token.t_att[0].ar,-1,TRUE); - else { - tdp= &tokens[token.t_token]; - for (i=0;it_type[i]==EV_REG) - chrefcount(token.t_att[i].ar,-1,TRUE); - } - break; - case DO_REALLOCATE: - DEBUG("REALLOCATE"); - for(rp=machregs;rpr_tcount) { - rp->r_refcount -= rp->r_tcount; - rp->r_tcount = 0; - } - break; - case DO_ALLOCATE: - DEBUG("ALLOCATE"); - if (codep[-1]&32) { - getint(propno,codep); - getint(tinstno,codep); - } else { - getint(propno,codep); - tinstno=0; - } - instance(tinstno,&token); - if (!forced) { - do { - npos=exactmatch=0; - for(rpp=reglist[propno];rp= *rpp; rpp++) - if (getrefcount((int)(rp-machregs), FALSE)==0) { - pos[npos++] = rp-machregs; - if (eqtoken(&rp->r_contents,&token)) - exactmatch++; + else + { + getint(propno, codep); + tinstno = 0; } - /* - * Now pos[] contains all free registers with desired - * property. If none then some stacking has to take place. - */ - if (npos==0) { - if (stackheight<=tokpatlen) { - if (!toplevel) { - totalcost = INFINITY; - BROKE(); - } else { - fatal("No regs available"); + instance(tinstno, &token); + if (!forced) + { + do + { + npos = exactmatch = 0; + for (rpp = reglist[propno]; (rp = *rpp); rpp++) + if (getrefcount((int) (rp - machregs), FALSE) == 0) + { + pos[npos++] = rp - machregs; + if (eqtoken(&rp->r_contents, &token)) + exactmatch++; + } + /* + * Now pos[] contains all free registers with desired + * property. If none then some stacking has to take place. + */ + if (npos == 0) + { + if (stackheight <= tokpatlen) + { + if (!toplevel) + { + totalcost = INFINITY; + BROKE(); + } + else + { + fatal("No regs available"); + } + } + totalcost += stackupto(&fakestack[0], ply, + toplevel); + CHKCOST(); + } + } while (npos == 0); + if (!exactmatch) + { + npos2 = npos; + for (i = 0; i < npos; i++) + pos2[i] = pos[i]; + } + else + { + /* + * Now we are reducing the number of possible registers. + * We take only one equally likely register out of every + * equivalence class as given by set of properties. + */ + mtoken = token; + npos2 = 0; + for (i = 0; i < npos; i++) + if (eqtoken(&machregs[pos[i]].r_contents, &mtoken)) + { + pos2[npos2++] = pos[i]; + for (j = 0; j < npos2 - 1; j++) + if (eqregclass(pos2[j], pos[i])) + { + npos2--; + break; + } + } + } + /* + * Now pos2[] contains all possibilities to try, if more than + * one, lookahead is necessary. + */ + token2.t_token = -1; + for (i = 1; i < TOKENSIZE; i++) + token2.t_att[i].aw = 0; + if (npos2 == 1) + decision = pos2[0]; + else + { + SAVEST; + mincost = costlimit - totalcost + 1; + for (j = 0; j < npos2; j++) + { + chrefcount(pos2[j], 1, FALSE); + token2.t_att[0].ar = pos2[j]; + allreg[nallreg++] = pos2[j]; + if (token.t_token != 0) + t = move(&token, &token2, ply, FALSE, mincost); + else + { + t = 0; + erasereg(pos2[j]); + } + if (t < mincost) + t += codegen(codep, ply, FALSE, + mincost < MAXINT ? mincost - t : MAXINT, + 0); + if (t < mincost) + { + mincost = t; + decision = pos2[j]; + } + RESTST; + } FREEST; + if (totalcost + mincost > costlimit) + { + totalcost = INFINITY; + BROKE(); + } } } - totalcost += stackupto( &fakestack[0],ply,toplevel); - CHKCOST(); - } - } while (npos==0); - if (!exactmatch) { - npos2=npos; - for(i=0;icostlimit) { - totalcost = INFINITY; - BROKE(); - } - } - } else { - decision = forced; - if (getrefcount(decision, FALSE)!=0) { - totalcost = INFINITY; - BROKE(); - } - token2.t_token = -1; - } - chrefcount(decision,1,FALSE); - token2.t_att[0].ar=decision; - if (token.t_token != 0) { - totalcost+=move(&token,&token2,ply,toplevel,MAXINT); - CHKCOST(); - } else - erasereg(decision); - allreg[nallreg++]=decision; - break; - case DO_LOUTPUT: - DEBUG("LOUTPUT"); - getint(stringno,codep); - getint(nodeno,codep); - if (toplevel) { - gencode(codestrings[stringno]); - genexpr(nodeno); - } - break; - case DO_ROUTPUT: - DEBUG("ROUTPUT"); - i=((codep[-1]>>5)&07); - do { - getint(stringno,codep); - if (toplevel) { - gencode(codestrings[stringno]); - gennl(); - } - } while (i--); - break; - case DO_MOVE: - DEBUG("MOVE"); - getint(tinstno,codep); - instance(tinstno,&token); - getint(tinstno,codep); - instance(tinstno,&token2); - totalcost += move(&token,&token2,ply,toplevel,costlimit-totalcost+1); - CHKCOST(); - break; - case DO_ERASE: - DEBUG("ERASE"); - getint(nodeno,codep); - result=compute(&enodes[nodeno]); - assert(result.e_typ==EV_REG); - erasereg(result.e_v.e_reg); - break; - case DO_TOKREPLACE: - DEBUG("TOKREPLACE"); - assert(stackheight>=tokpatlen); - repllen=(codep[-1]>>5)&07; - for(i=0;i>5)&07; - j=emp-emlines; - if (emrepllen>j) { - int k = nemlines + emrepllen - j; - assert(k=0;i--, k--) - emlines[k] = emlines[i]; - nemlines += emrepllen-j; - emp += emrepllen-j; - } - emp -= emrepllen; - for (i=0;i> 5) & 07); + do + { + getint(stringno, codep); + if (toplevel) + { + gencode(codestrings[stringno]); + gennl(); + } + } while (i--); + break; + case DO_MOVE: + DEBUG("MOVE") + ; + getint(tinstno, codep) + ; + instance(tinstno, &token); + getint(tinstno, codep) + ; + instance(tinstno, &token2); + totalcost += move(&token, &token2, ply, toplevel, + costlimit - totalcost + 1); + CHKCOST() + ; + break; + case DO_ERASE: + DEBUG("ERASE") + ; + getint(nodeno, codep) + ; + result = compute(&enodes[nodeno]); + assert(result.e_typ==EV_REG); + erasereg(result.e_v.e_reg); + break; + case DO_TOKREPLACE: + DEBUG("TOKREPLACE") + ; + assert(stackheight >= tokpatlen); + repllen = (codep[-1] >> 5) & 07; + for (i = 0; i < repllen; i++) + { + getint(tinstno, codep); + instance(tinstno, &reptoken[i]); + tref(&reptoken[i], 1); + } + for (i = 0; i < tokpatlen; i++) + { + if (!inscoerc) + tref(&fakestack[stackheight - 1], -1); + stackheight--; + } + for (i = 0; i < repllen; i++) + { + assert(stackheight> 5) & 07; + j = emp - emlines; + if (emrepllen > j) + { + int k = nemlines + emrepllen - j; + assert(k= 0; i--, k--) + emlines[k] = emlines[i]; + nemlines += emrepllen - j; + emp += emrepllen - j; + } + emp -= emrepllen; + for (i = 0; i < emrepllen; i++) + { + getint(eminstr, codep); + getint(nodeno, codep); + emp[i].em_instr = eminstr; + result = compute(&enodes[nodeno]); + switch (result.e_typ) + { + default: + assert(FALSE); + case 0: + emp[i].em_optyp = OPNO; + emp[i].em_soper = 0; + break; + case EV_INT: + emp[i].em_optyp = OPINT; + emp[i].em_soper = tostring(result.e_v.e_con); + emp[i].em_u.em_ioper = result.e_v.e_con; + break; + case EV_STR: + emp[i].em_optyp = OPSYMBOL; + emp[i].em_soper = result.e_v.e_str; + break; + } + } + if (!toplevel) + { + ply += emrepllen; + } + break; + case DO_COST: + DEBUG("COST") + ; + getint(cost.c_size, codep) + ; + getint(cost.c_time, codep) + ; + totalcost += costcalc(cost); + CHKCOST() + ; + break; #ifdef REGVARS - case DO_PRETURN: - if (toplevel) { - swtxt(); - regreturn(); /* in mach.c */ - } - break; + case DO_PRETURN: + if (toplevel) + { + swtxt(); + regreturn(); /* in mach.c */ + } + break; #endif - case DO_RETURN: - DEBUG("RETURN"); - assert(origcp!=startupcode); - doreturn: + case DO_RETURN: + DEBUG("RETURN") + ; + assert(origcp != startupcode); + doreturn: #ifndef NDEBUG - level--; + level--; #endif - return(totalcost); - } + return (totalcost); + } } } diff --git a/mach/proto/cg/codegen.h b/mach/proto/cg/codegen.h new file mode 100644 index 000000000..7c288de19 --- /dev/null +++ b/mach/proto/cg/codegen.h @@ -0,0 +1,13 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-14 + * + */ +#ifndef CODEGEN_H_ +#define CODEGEN_H_ + +unsigned int codegen(byte *codep, int ply, int toplevel, unsigned int costlimit, int forced); + +#endif /* CODEGEN_H_ */ diff --git a/mach/proto/cg/compute.c b/mach/proto/cg/compute.c index 49d2381fa..a55a1ace0 100644 --- a/mach/proto/cg/compute.c +++ b/mach/proto/cg/compute.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include @@ -14,13 +16,9 @@ static char rcsid[] = "$Id$"; #include "result.h" #include "glosym.h" #include "extern.h" +#include "subr.h" +#include "salloc.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ #define LLEAF 01 #define LDEF 02 @@ -73,9 +71,10 @@ char opdesc[] = { LLDEF, /* EX_REGVAR */ }; -string salloc(); -string mycat(s1,s2) string s1,s2; { + +string mycat(string s1,string s2) +{ register string s; s=salloc(strlen(s1)+strlen(s2)); @@ -84,7 +83,8 @@ string mycat(s1,s2) string s1,s2; { return(s); } -string mystrcpy(s) string s; { +string mystrcpy(string s) +{ register string r; r=salloc(strlen(s)); @@ -94,7 +94,8 @@ string mystrcpy(s) string s; { char digstr[21][15]; -string tostring(n) word n; { +string tostring(word n) +{ char buf[25]; if (n>=-20 && n<=20 && (n&1)==0) { @@ -108,7 +109,8 @@ string tostring(n) word n; { result_t undefres= {EV_UNDEF}; -result_t compute(node) register node_p node; { +result_t compute(register node_p node) +{ result_t leaf1,leaf2,result; token_p tp; int desc; diff --git a/mach/proto/cg/data.h b/mach/proto/cg/data.h index e02cb2726..b55fea2a3 100644 --- a/mach/proto/cg/data.h +++ b/mach/proto/cg/data.h @@ -2,7 +2,10 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef DATA_H_ +#define DATA_H_ + +#include "types.h" typedef struct { int t_token; /* kind of token, -1 for register */ @@ -56,3 +59,5 @@ typedef struct { int rl_n; /* number in list */ int rl_list[NREGS]; } rl_t,*rl_p; + +#endif /* DATA_H_ */ diff --git a/mach/proto/cg/equiv.c b/mach/proto/cg/equiv.c index 77e1c0c0a..f18e4dcb2 100644 --- a/mach/proto/cg/equiv.c +++ b/mach/proto/cg/equiv.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include "equiv.h" #include "param.h" @@ -11,15 +13,12 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" +#include "subr.h" +#include "salloc.h" +#include "reg.h" + -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ -extern string myalloc(); int rar[MAXCREG]; rl_p* lar; @@ -27,12 +26,12 @@ int maxindex; int regclass[NREGS]; struct perm* perms; -void permute(int index); +static void permute(int index); struct perm* tuples(rl_p* regls, int nregneeded) { int class = 0; - register i, j; + register int i, j; /* * First compute equivalence classes of registers. @@ -66,11 +65,11 @@ struct perm* tuples(rl_p* regls, int nregneeded) return (perms); } -void permute(int index) +static void permute(int index) { register struct perm* pp; register rl_p rlp; - register i, j; + register int i, j; if (index == maxindex) { diff --git a/mach/proto/cg/equiv.h b/mach/proto/cg/equiv.h index fcd7971da..fe97bd999 100644 --- a/mach/proto/cg/equiv.h +++ b/mach/proto/cg/equiv.h @@ -2,7 +2,9 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef EQUIV_H_ +#define EQUIV_H_ + #define MAXCREG 4 @@ -10,3 +12,6 @@ struct perm { struct perm *p_next; int p_rar[MAXCREG]; }; + + +#endif /* EQUIV_H_ */ diff --git a/mach/proto/cg/fillem.c b/mach/proto/cg/fillem.c index 25caeb513..f33520e8b 100644 --- a/mach/proto/cg/fillem.c +++ b/mach/proto/cg/fillem.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid2[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include @@ -18,23 +20,48 @@ static char rcsid2[] = "$Id$"; #include #include "data.h" #include "result.h" +#include "subr.h" +#include "reg.h" +#include "salloc.h" +#include "gencode.h" +#include "glosym.h" #ifdef REGVARS #include "regvar.h" #include #endif #include "extern.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ + #ifndef newplb /* retrofit for older mach.h */ #define newplb newilb #endif +string tostring(); +static string holstr(word n); +static char *strarg(int t); +string mystrcpy(); +static int get16(void); +static long get32(void); +static int getarg(int typset); +static void getstring(void); +static void switchseg(int s); +static int table1(void); +static int table2(void); +static int table3(int i); +static void bss(full n, int t, int b); +void swtxt(void); +static void savelab(void); +static void dumplab(void); +static void xdumplab(void); +static void part_flush(void); +static long con(int t); + + + + + + #ifdef fmt_id #ifdef id_first It is an error to define both fmt_id and id_first. @@ -55,7 +82,7 @@ Read the documentation. #define SEGROM 2 #define SEGBSS 3 -long con(); + #define get8() getc(emfile) @@ -63,7 +90,6 @@ long con(); FILE *emfile; extern FILE *codefile; -extern FILE *freopen(); int nextispseu,savetab1; int opcode; @@ -84,14 +110,14 @@ int regallowed=0; extern char em_flag[]; extern short em_ptyp[]; -extern double atof(); -extern void con_float(void); + + + /* Own version of atol that continues computing on overflow. We don't know that about the ANSI C one. */ -long our_atol(s) -register char *s; +static long our_atol(register char *s) { register long total = 0; register unsigned digit; @@ -112,13 +138,8 @@ register char *s; #define sp_cstx sp_cst2 -string tostring(); -string holstr(); -string strarg(); -string mystrcpy(); -long get32(); -in_init(filename) char *filename; { +void in_init(char *filename) { if ((emfile=freopen(filename,"r",stdin))==NULL) error("Can't open %s",filename); @@ -126,16 +147,16 @@ in_init(filename) char *filename; { error("Bad format %s",filename); } -in_start() { +void in_start(void) { #ifdef modhead fprintf(codefile,"%s",modhead) ; #endif } -in_finish() { +void in_finish(void) { } -fillemlines() { +void fillemlines(void) { int t,i; register struct emline *lp; @@ -214,14 +235,14 @@ fillemlines() { } } -dopseudo() { - register b,t; +void dopseudo(void) { + register int b,t; register full n; register long save; word romcont[MAXROM+1]; int nromwords; int rombit,rommask; - unsigned dummy,stackupto(); + int dummy; if (nextispseu==0 || nemlines>0) error("No table entry for %d",emlines[0].em_instr); @@ -396,8 +417,8 @@ dopseudo() { /* ----- input ----- */ -int getarg(typset) { - register t,argtyp; +static int getarg(int typset) { + register int t,argtyp; argtyp = t = table2(); if (t == EOF) @@ -409,8 +430,8 @@ int getarg(typset) { return(argtyp); } -int table1() { - register i; +static int table1(void) { + register int i; i = get8(); if (i < sp_fmnem+sp_nmnem && i >= sp_fmnem) { @@ -428,8 +449,8 @@ int table1() { return(table3(i)); } -int table2() { - register i; +static int table2(void) { + register int i; i = get8(); if (i < sp_fcst0+sp_ncst0 && i >= sp_fcst0) { @@ -439,7 +460,7 @@ int table2() { return(table3(i)); } -int table3(i) { +static int table3(int i) { word consiz; switch(i) { @@ -482,7 +503,7 @@ int table3(i) { return(i); } -int get16() { +static int get16(void) { register int l_byte, h_byte; l_byte = get8(); @@ -491,7 +512,7 @@ int get16() { return l_byte | (h_byte*256) ; } -long get32() { +static long get32(void) { register long l; register int h_byte; @@ -503,9 +524,9 @@ long get32() { return l | (h_byte*256L*256*256L) ; } -getstring() { +static void getstring(void) { register char *p; - register n; + register int n; getarg(cst_ptyp); if (argval < 0 || argval > MAXSTR-1) @@ -517,7 +538,7 @@ getstring() { *p++ = '\0'; } -char *strarg(t) { +static char *strarg(int t) { register char *p; switch (t) { @@ -565,7 +586,8 @@ char *strarg(t) { return(mystrcpy(argstr)); } -bss(n,t,b) full n; { +static void bss(full n, int t, int b) +{ register long s; if (n % TEM_WSIZE) @@ -588,8 +610,8 @@ bss(n,t,b) full n; { fatal("bad BSS initializer"); } -long con(t) { - register i; +static long con(int t) { + register int i; strarg(t); switch (t) { @@ -633,11 +655,11 @@ long con(t) { extern char *segname[]; -swtxt() { +void swtxt(void) { switchseg(SEGTXT); } -switchseg(s) { +static void switchseg(int s) { if (s == curseg) return; @@ -646,9 +668,9 @@ switchseg(s) { fprintf(codefile,"%s\n",segname[s]); } -savelab() { +static void savelab(void) +{ register char *p,*q; - part_flush(); if (labstr[0]) { dlbdlb(argstr,labstr); @@ -656,11 +678,11 @@ savelab() { } p = argstr; q = labstr; - while (*q++ = *p++) + while ((*q++ = *p++)) ; } -dumplab() { +static void dumplab(void) { if (labstr[0] == 0) return; @@ -669,7 +691,7 @@ dumplab() { labstr[0] = 0; } -xdumplab() { +static void xdumplab(void) { if (labstr[0] == 0) return; @@ -677,7 +699,7 @@ xdumplab() { newdlb(labstr); } -part_flush() { +static void part_flush(void) { /* * Each new data fragment and each data label starts at @@ -690,7 +712,7 @@ part_flush() { part_word = 0; } -string holstr(n) word n; { +static string holstr(word n) { sprintf(str,hol_off,n,holno); return(mystrcpy(str)); diff --git a/mach/proto/cg/fillem.h b/mach/proto/cg/fillem.h new file mode 100644 index 000000000..e8708b32c --- /dev/null +++ b/mach/proto/cg/fillem.h @@ -0,0 +1,22 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-14 + * + */ +#ifndef FILLEM_H_ +#define FILLEM_H_ + +/** Switch to text segment. */ +void swtxt(void); + + +void in_init(char *filename); +void in_start(void); +void in_finish(void); +void fillemlines(void); + +void dopseudo(void); + +#endif /* FILLEM_H_ */ diff --git a/mach/proto/cg/gencode.c b/mach/proto/cg/gencode.c index c15bd9b94..7582808ab 100644 --- a/mach/proto/cg/gencode.c +++ b/mach/proto/cg/gencode.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include #include "param.h" @@ -11,39 +13,35 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" - -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +#include "subr.h" +#include "gencode.h" +#include "fillem.h" FILE *codefile; -extern FILE *freopen(); -out_init(filename) char *filename; { + +void out_init(char *filename) +{ #ifndef NDEBUG - static char stderrbuff[BUFSIZ]; - if (Debug) { - codefile = stderr; - if (!isatty(2)) - setbuf(stderr,stderrbuff); - } else { -#endif - if (filename == (char *) 0) - codefile = stdout; + if (Debug) + { + codefile = stderr; + } else - if ((codefile=freopen(filename,"w",stdout))==NULL) - error("Can't create %s",filename); + { +#endif + if (filename == (char *) 0) + codefile = stdout; + else if ((codefile = freopen(filename, "w", stdout)) == NULL) + error("Can't create %s", filename); #ifndef NDEBUG - } + } #endif } -out_finish() { +void out_finish(void) { #ifndef NDEBUG if (Debug) @@ -53,14 +51,15 @@ out_finish() { fclose(codefile); } -tstoutput() { +void tstoutput(void) { if (ferror(codefile)) error("Write error on output"); } -gencode(code) register char *code; { - register c; +void gencode(register char *code) +{ + register int c; int tokno,fldno,insno,regno,subno; register token_p tp; @@ -131,7 +130,8 @@ gencode(code) register char *code; { } } -genexpr(nodeno) { +void genexpr(int nodeno) +{ result_t result; result= compute(&enodes[nodeno]); @@ -149,12 +149,14 @@ genexpr(nodeno) { } } -gennl() { +void gennl(void) +{ fputc('\n',codefile); } -prtoken(tp) token_p tp; { - register c; +void prtoken(token_p tp) +{ + register int c; register char *code; register tkdef_p tdp; diff --git a/mach/proto/cg/gencode.h b/mach/proto/cg/gencode.h new file mode 100644 index 000000000..467df4539 --- /dev/null +++ b/mach/proto/cg/gencode.h @@ -0,0 +1,22 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-12 + * + */ +#ifndef GENCODE_H_ +#define GENCODE_H_ + +#include "data.h" + +void out_init(char *filename); +void out_finish(void); +void tstoutput(void); +void gencode(register char *code); +void genexpr(int nodeno); +void gennl(void); +void prtoken(token_p tp); + + +#endif /* GENCODE_H_ */ diff --git a/mach/proto/cg/glosym.c b/mach/proto/cg/glosym.c index 4052c1bfe..55e283ffc 100644 --- a/mach/proto/cg/glosym.c +++ b/mach/proto/cg/glosym.c @@ -1,28 +1,26 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include -#include -#include "param.h" -#include "tables.h" -#include "types.h" -#include "glosym.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ +#include +#include +#include "param.h" +#include "tables.h" +#include "types.h" +#include "glosym.h" +#include "salloc.h" + + -extern string myalloc(); glosym_p glolist= (glosym_p) 0; -enterglo(name,romp) string name; word *romp; { +void enterglo(string name,word *romp) +{ register glosym_p gp; - register i; + register int i; gp = (glosym_p) myalloc(sizeof *gp); gp->gl_next = glolist; @@ -33,7 +31,8 @@ enterglo(name,romp) string name; word *romp; { glolist = gp; } -glosym_p lookglo(name) string name; { +glosym_p lookglo(string name) +{ register glosym_p gp; for (gp=glolist;gp != (glosym_p) 0; gp=gp->gl_next) diff --git a/mach/proto/cg/glosym.h b/mach/proto/cg/glosym.h index 1449b9b1b..b4b2157fe 100644 --- a/mach/proto/cg/glosym.h +++ b/mach/proto/cg/glosym.h @@ -2,7 +2,9 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef GLOSYM_H_ +#define GLOSYM_H_ + typedef struct glosym { struct glosym *gl_next; @@ -10,4 +12,7 @@ typedef struct glosym { word gl_rom[MAXROM+1]; } glosym_t,*glosym_p; +void enterglo(string name, word *romp); glosym_p lookglo(); + +#endif /* GLOSYM_H_ */ diff --git a/mach/proto/cg/main.c b/mach/proto/cg/main.c index 592bcb877..f7fc09a11 100644 --- a/mach/proto/cg/main.c +++ b/mach/proto/cg/main.c @@ -1,19 +1,24 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include "param.h" -#include "mach.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ +#include +#include "tables.h" +#include "types.h" +#include +#include "data.h" +#include "tables.h" +#include "param.h" +#include "mach.h" +#include "subr.h" +#include "fillem.h" +#include "gencode.h" +#include "codegen.h" char *progname; -extern char startupcode[]; +extern byte startupcode[]; int maxply=1; #ifndef NDEBUG int Debug=0; @@ -21,10 +26,23 @@ int Debug=0; extern int endofprog; -main(argc,argv) char **argv; { + +unsigned ggd(unsigned int a,unsigned int b) +{ + register unsigned c; + + do { + c = a%b; a=b; b=c; + } while (c!=0); + return(a); +} + + +int main(int argc, char **argv) +{ register unsigned n; extern unsigned cc1,cc2,cc3,cc4; - unsigned ggd(); + progname = argv[0]; while (--argc && **++argv == '-') { @@ -70,11 +88,3 @@ main(argc,argv) char **argv; { out_finish(); } -unsigned ggd(a,b) register unsigned a,b; { - register unsigned c; - - do { - c = a%b; a=b; b=c; - } while (c!=0); - return(a); -} diff --git a/mach/proto/cg/move.c b/mach/proto/cg/move.c index f0fe5da15..8aa739f8f 100644 --- a/mach/proto/cg/move.c +++ b/mach/proto/cg/move.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include "param.h" #include "tables.h" @@ -10,23 +12,18 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" +#include "subr.h" +#include "reg.h" +#include "codegen.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ -unsigned costcalc(); - -move(tp1,tp2,ply,toplevel,maxcost) token_p tp1,tp2; unsigned maxcost; { +int move(token_p tp1,token_p tp2,int ply,int toplevel,unsigned int maxcost) +{ register move_p mp; register unsigned t; register struct reginfo *rp; tkdef_p tdp; int i; - unsigned codegen(); if (eqtoken(tp1,tp2)) return(0); diff --git a/mach/proto/cg/nextem.c b/mach/proto/cg/nextem.c index 765d14d1d..f0d9af598 100644 --- a/mach/proto/cg/nextem.c +++ b/mach/proto/cg/nextem.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include "assert.h" @@ -12,21 +14,36 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" - -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +#include "fillem.h" #ifndef NDEBUG #include extern char em_mnem[][4]; #endif -byte *trypat(bp,len) register byte *bp; { - register patlen,i; +extern char em_flag[]; + +static int argtyp(int mn) { + + switch(em_flag[mn-sp_fmnem]&EM_PAR) { + case PAR_W: + case PAR_S: + case PAR_Z: + case PAR_O: + case PAR_N: + case PAR_L: + case PAR_F: + case PAR_R: + case PAR_C: + return(EV_INT); + default: + return(EV_STR); + } +} + +byte *trypat(register byte *bp, int len) +{ + register int patlen,i; result_t result; getint(patlen,bp); @@ -71,28 +88,10 @@ byte *trypat(bp,len) register byte *bp; { return(bp); } -extern char em_flag[]; -argtyp(mn) { - switch(em_flag[mn-sp_fmnem]&EM_PAR) { - case PAR_W: - case PAR_S: - case PAR_Z: - case PAR_O: - case PAR_N: - case PAR_L: - case PAR_F: - case PAR_R: - case PAR_C: - return(EV_INT); - default: - return(EV_STR); - } -} - -byte *nextem(toplevel) { - register i; +byte *nextem(int toplevel) { + register int i; short hash[3]; register byte *bp; byte *cp; diff --git a/mach/proto/cg/reg.c b/mach/proto/cg/reg.c index 705fc71d7..4675c3557 100644 --- a/mach/proto/cg/reg.c +++ b/mach/proto/cg/reg.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include "param.h" #include "tables.h" @@ -10,17 +12,16 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" +#include "subr.h" +#include "reg.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +/* Froward local declarations */ +static void awayreg(int); -chrefcount(regno,amount,tflag) { +void chrefcount(int regno, int amount, int tflag) +{ register struct reginfo *rp; - register i; + register int i; rp= &machregs[regno]; #if MAXMEMBERS!=0 @@ -38,9 +39,10 @@ chrefcount(regno,amount,tflag) { #endif } -getrefcount(regno, tflag) { +int getrefcount(int regno, int tflag) +{ register struct reginfo *rp; - register i,maxcount; + register int i,maxcount; rp= &machregs[regno]; #if MAXMEMBERS!=0 @@ -58,7 +60,8 @@ getrefcount(regno, tflag) { #endif } -erasereg(regno) { +void erasereg(int regno) +{ register struct reginfo *rp; register int i; @@ -83,10 +86,11 @@ erasereg(regno) { #endif } -awayreg(regno) { +static void awayreg(int regno) +{ register struct reginfo *rp; register tkdef_p tdp; - register i; + register int i; /* Now erase recursively all registers containing * something using this one @@ -107,9 +111,10 @@ awayreg(regno) { } } -cleanregs() { +void cleanregs(void) +{ register struct reginfo *rp; - register i; + register int i; for (rp=machregs;rpr_contents.t_token = 0; @@ -119,9 +124,10 @@ cleanregs() { } #ifndef NDEBUG -inctcount(regno) { +void inctcount(int regno) +{ register struct reginfo *rp; - register i; + register int i; rp = &machregs[regno]; #if MAXMEMBERS!=0 @@ -137,7 +143,8 @@ inctcount(regno) { #endif } -chkregs() { +void chkregs(void) +{ register struct reginfo *rp; register token_p tp; register tkdef_p tdp; diff --git a/mach/proto/cg/reg.h b/mach/proto/cg/reg.h new file mode 100644 index 000000000..70525b15c --- /dev/null +++ b/mach/proto/cg/reg.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-14 + * + */ +#ifndef REG_H_ +#define REG_H_ + +void chrefcount(int regno, int amount, int tflag); +int getrefcount(int regno, int tflag); +void erasereg(int regno); +void cleanregs(void); +void chkregs(void); + +#endif /* REG_H_ */ diff --git a/mach/proto/cg/regvar.c b/mach/proto/cg/regvar.c index 5b5859615..03dbe14b0 100644 --- a/mach/proto/cg/regvar.c +++ b/mach/proto/cg/regvar.c @@ -1,12 +1,16 @@ +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "assert.h" #include "param.h" #include "tables.h" #ifdef REGVARS -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif + #include "types.h" #include @@ -15,18 +19,20 @@ static char rcsid[] = "$Id$"; #include #include "result.h" #include "extern.h" +#include "salloc.h" +#include "fillem.h" +#include "regvar.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ -extern string myalloc(); struct regvar *rvlist; -struct regvar * -linkreg(of,sz,tp,sc) long of; { +/* Defined in mach.c of the specific machine. */ +extern int regscore(long off,int size,int typ,int score,int totyp); +extern void i_regsave(void); +extern void f_regsave(void); +void regsave(char *regstr,long off,int size); + +struct regvar *linkreg(long of,int sz,int tp,int sc) +{ struct regvar *rvlp; rvlp= (struct regvar *) myalloc(sizeof *rvlp); @@ -40,9 +46,10 @@ linkreg(of,sz,tp,sc) long of; { return(rvlp); } -tryreg(rvlp,typ) struct regvar *rvlp; { +void tryreg(struct regvar *rvlp, int typ) +{ int score; - register i; + register int i; struct regassigned *ra; struct regvar *save; @@ -85,9 +92,10 @@ tryreg(rvlp,typ) struct regvar *rvlp; { } } -fixregvars(saveall) { +void fixregvars(int saveall) +{ register struct regvar *rv; - register rvtyp,i; + register int rvtyp,i; swtxt(); i_regsave(); /* machine dependent initialization */ @@ -107,7 +115,8 @@ fixregvars(saveall) { f_regsave(); } -isregvar(off) long off; { +int isregvar(long off) +{ register struct regvar *rvlp; for(rvlp=rvlist;rvlp!=0;rvlp=rvlp->rv_next) @@ -116,7 +125,7 @@ isregvar(off) long off; { return(-1); } -unlinkregs() { +void unlinkregs(void) { register struct regvar *rvlp,*t; register struct regassigned *ra; int rvtyp,i; diff --git a/mach/proto/cg/regvar.h b/mach/proto/cg/regvar.h index e04350356..34298cad8 100644 --- a/mach/proto/cg/regvar.h +++ b/mach/proto/cg/regvar.h @@ -2,7 +2,8 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef REGVAR_H_ +#define REGVAR_H_ struct regvar { struct regvar *rv_next; @@ -21,3 +22,12 @@ struct regassigned { extern struct regvar *rvlist; extern int nregvar[]; extern struct regassigned *regassigned[]; + + +struct regvar *linkreg(long of,int sz,int tp,int sc); +void tryreg(struct regvar *rvlp, int typ); +void fixregvars(int saveall); +int isregvar(long off); +void unlinkregs(void); + +#endif /* REGVAR_H_ */ diff --git a/mach/proto/cg/salloc.c b/mach/proto/cg/salloc.c index 30554a471..73d081650 100644 --- a/mach/proto/cg/salloc.c +++ b/mach/proto/cg/salloc.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "assert.h" #include "param.h" @@ -11,13 +13,10 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" +#include "subr.h" +#include "salloc.h" + -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ /* * Package for string allocation and garbage collection. @@ -31,7 +30,33 @@ static char rcsid[] = "$Id$"; char *stab[MAXSTAB]; int nstab=0; -string myalloc(size) { +static void chkstr(string str,char used[]) +{ + register int low,middle,high; + + low=0; high=nstab-1; + while (high>low) { + middle= (low+high)>>1; + if (str==stab[middle]) { + used[middle]=1; + return; + } + if (strlow) { - middle= (low+high)>>1; - if (str==stab[middle]) { - used[middle]=1; - return; - } - if (str>=1; + do + *to++ = *from++; + while (--nbytes); +} + #ifdef STONSTACK -savestatus(sp) register state_p sp; { +state_p savestatus(register state_p sp) +{ #else -state_p savestatus() { +state_p state_p savestatus(void) +{ register state_p sp; if ((sp=stlist)==0) @@ -56,8 +69,8 @@ state_p savestatus() { #endif } -restorestatus(sp) register state_p sp; { - +void restorestatus(register state_p sp) +{ stackheight = sp->st_sh; bmove((short *)sp->st_fs,(short *)fakestack,stackheight*sizeof(token_t)); nallreg = sp->st_na; @@ -74,20 +87,11 @@ restorestatus(sp) register state_p sp; { } #ifndef STONSTACK -freestatus(sp) state_p sp; { - +void freestatus(state_p sp) +{ sp->st_next = stlist; stlist = sp; } #endif -bmove(from,to,nbytes) register short *from,*to; register nbytes; { - if (nbytes<=0) - return; - assert(sizeof(short)==2 && (nbytes&1)==0); - nbytes>>=1; - do - *to++ = *from++; - while (--nbytes); -} diff --git a/mach/proto/cg/state.h b/mach/proto/cg/state.h index f7f10e3ba..0e4eb5ca1 100644 --- a/mach/proto/cg/state.h +++ b/mach/proto/cg/state.h @@ -2,7 +2,9 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef STATE_H_ +#define STATE_H_ + #define STONSTACK /* if defined state is saved in stackframe */ @@ -24,5 +26,10 @@ typedef struct state { } state_t,*state_p; #ifndef STONSTACK -state_p savestatus(); +state_p state_p savestatus(void); #endif + +void restorestatus(register state_p sp); +state_p savestatus(register state_p sp); + +#endif /* STATE_H_ */ diff --git a/mach/proto/cg/subr.c b/mach/proto/cg/subr.c index 325f54a2c..dd7a77806 100644 --- a/mach/proto/cg/subr.c +++ b/mach/proto/cg/subr.c @@ -1,8 +1,12 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include +#include +#include #include "assert.h" #include #include "param.h" @@ -12,19 +16,20 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" #include "extern.h" +#include "subr.h" +#include "reg.h" +#include "salloc.h" +#include "gencode.h" +#include "regvar.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ -extern string myalloc(); unsigned codegen(); +extern unsigned cc1,cc2,cc3,cc4; -match(tp,tep,optexp) register token_p tp; register set_p tep; { - register bitno; + +int match(register token_p tp, register set_p tep, int optexp) +{ + register int bitno; token_p ct; result_t result; @@ -49,7 +54,8 @@ match(tp,tep,optexp) register token_p tp; register set_p tep; { return(result.e_v.e_con); } -instance(instno,token) register token_p token; { +void instance(int instno,register token_p token) +{ register inst_p inp; int i; token_p tp; @@ -128,7 +134,9 @@ instance(instno,token) register token_p token; { } } -cinstance(instno,token,tp,regno) register token_p token,tp; { +void cinstance(int instno,register token_p token, + register token_p tp,int regno) +{ register inst_p inp; int i; struct reginfo *rp; @@ -195,8 +203,9 @@ cinstance(instno,token,tp,regno) register token_p token,tp; { } } -eqtoken(tp1,tp2) token_p tp1,tp2; { - register i; +int eqtoken(token_p tp1,token_p tp2) +{ + register int i; register tkdef_p tdp; if (tp1->t_token!=tp2->t_token) @@ -229,9 +238,9 @@ eqtoken(tp1,tp2) token_p tp1,tp2; { return(1); } -distance(cindex) { - register char *bp; - register i; +int distance(int cindex) { + register byte *bp; + register int i; register token_p tp; int tokexp,tpl; int expsize,toksize,exact; @@ -281,9 +290,9 @@ distance(cindex) { return(20-exact); } -unsigned costcalc(cost) cost_t cost; { +unsigned costcalc(cost_t cost) +{ result_t result1,result2; - extern unsigned cc1,cc2,cc3,cc4; result1=compute(&enodes[cost.c_size]); result2=compute(&enodes[cost.c_time]); @@ -291,20 +300,20 @@ unsigned costcalc(cost) cost_t cost; { return(result1.e_v.e_con*cc1/cc2 + result2.e_v.e_con*cc3/cc4); } -ssize(tokexpno) { +int ssize(int tokexpno) { return(machsets[tokexpno].set_size); } -tsize(tp) register token_p tp; { - +int tsize(register token_p tp) +{ if (tp->t_token==-1) return(machregs[tp->t_att[0].ar].r_size); return(tokens[tp->t_token].t_size); } #ifdef MAXSPLIT -instsize(tinstno,tp) token_p tp; { +int instsize(int tinstno,token_p tp) { inst_p inp; struct reginfo *rp; @@ -335,8 +344,8 @@ instsize(tinstno,tp) token_p tp; { } #endif /* MAXSPLIT */ -tref(tp,amount) register token_p tp; { - register i; +void tref(register token_p tp,int amount) { + register int i; register tkdef_p tdp; if (tp->t_token==-1) @@ -352,11 +361,12 @@ tref(tp,amount) register token_p tp; { #define MAXSAVE 10 #ifdef MAXSPLIT -split(tp,ip,ply,toplevel) token_p tp; int *ip; { +int split(token_p tp,int *ip,int ply,int toplevel) +{ c2_p cp; token_t savestack[MAXSAVE]; int ok; - register i; + register int i; int diff; token_p stp; int tpl; @@ -392,11 +402,12 @@ found: } #endif /* MAXSPLIT */ -unsigned docoerc(tp,cp,ply,toplevel,forced) token_p tp; c3_p cp; { +unsigned docoerc(token_p tp,c3_p cp,int ply,int toplevel,int forced) +{ token_t savestack[MAXSAVE]; token_p stp; int i,diff; - unsigned cost; + unsigned int cost; int tpl; /* saved tokpatlen */ stp = &fakestack[stackheight-1]; @@ -415,7 +426,8 @@ unsigned docoerc(tp,cp,ply,toplevel,forced) token_p tp; c3_p cp; { return(cost); } -unsigned stackupto(limit,ply,toplevel) token_p limit; { +unsigned stackupto(token_p limit,int ply,int toplevel) +{ token_t savestack[MAXFSTACK]; token_p stp; int i,diff; @@ -473,10 +485,11 @@ unsigned stackupto(limit,ply,toplevel) token_p limit; { return(totalcost); } -c3_p findcoerc(tp,tep) token_p tp; set_p tep; { +c3_p findcoerc(token_p tp,set_p tep) +{ register c3_p cp; token_t rtoken; - register i; + register int i; register struct reginfo **rpp; for (cp=c3coercs;cp< &c3coercs[NC3]; cp++) { @@ -508,30 +521,42 @@ c3_p findcoerc(tp,tep) token_p tp; set_p tep; { return(0); /* nothing found */ } - -error(s,a1,a2,a3,a4,a5,a6,a7,a8) char *s; { - - fatal(s,a1,a2,a3,a4,a5,a6,a7,a8); -} - -fatal(s,a1,a2,a3,a4,a5,a6,a7,a8) char *s; { - +static void error_str(char *s, va_list argptr) +{ fprintf(stderr,"Error: "); - fprintf(stderr,s,a1,a2,a3,a4,a5,a6,a7,a8); + vfprintf(stderr, s, argptr); fprintf(stderr,"\n"); out_finish(); abort(); - exit(-1); + exit(EXIT_FAILURE); + } -#ifndef NDEBUG -badassertion(asstr,file,line) char *asstr, *file; { +void error(char *s, ...) +{ + va_list ap; + va_start(ap, s); + error_str(s, ap); + va_end(ap); +} +void fatal(char *s, ...) +{ + va_list ap; + va_start(ap, s); + error_str(s, ap); + va_end(ap); +} + + +#ifndef NDEBUG +void badassertion(char* asstr,char *file,int line) +{ fatal("Assertion \"%s\" failed %s(%d)",asstr,file,line); } #endif -max(a,b) { - +int max(int a,int b) +{ return(a>b ? a : b); } diff --git a/mach/proto/cg/subr.h b/mach/proto/cg/subr.h new file mode 100644 index 000000000..8d3e78add --- /dev/null +++ b/mach/proto/cg/subr.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-12 + * + */ +#ifndef SUBR_H_ +#define SUBR_H_ + +#include "data.h" +#include "types.h" + +int match(register token_p tp, register set_p tep, int optexp); +void instance(int instno,register token_p token); +void cinstance(int instno,register token_p token, + register token_p tp,int regno); +int eqtoken(token_p tp1,token_p tp2); +int distance(int cindex); +unsigned costcalc(cost_t cost); +int ssize(int tokexpno); +int tsize(register token_p tp); + +#ifdef MAXSPLIT +int instsize(int tinstno,token_p tp); +#endif /* MAXSPLIT */ + +void tref(register token_p tp,int amount); + +#ifdef MAXSPLIT +int split(token_p tp,int *ip,int ply,int toplevel); +#endif /* MAXSPLIT */ + +unsigned docoerc(token_p tp,c3_p cp,int ply,int toplevel,int forced); +unsigned stackupto(token_p limit,int ply,int toplevel); +c3_p findcoerc(token_p tp,set_p tep); +void error(char *s, ...); +void fatal(char *s, ...); + +#ifndef NDEBUG +void badassertion(char* asstr,char *file,int line); +#endif + +int max(int a,int b); + + +#endif /* SUBR_H_ */ diff --git a/mach/proto/cg/types.h b/mach/proto/cg/types.h index 1b8a86b13..b8029ed68 100644 --- a/mach/proto/cg/types.h +++ b/mach/proto/cg/types.h @@ -2,7 +2,9 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef TYPES_H_ +#define TYPES_H_ + #ifndef TEM_WSIZE TEM_WSIZE should be defined at this point @@ -15,7 +17,7 @@ Implementation will not be correct unless a long integer has more then 4 bytes of precision. #endif -typedef char byte; +typedef unsigned char byte; typedef char * string; #if TEM_WSIZE>2 || TEM_PSIZE>2 @@ -28,3 +30,5 @@ typedef char * string; #ifndef WRD_FMT #define WRD_FMT "%ld" #endif /* WRD_FMT */ + +#endif /* TYPES_H_ */ diff --git a/mach/proto/cg/var.c b/mach/proto/cg/var.c index e55fe545e..b62486243 100644 --- a/mach/proto/cg/var.c +++ b/mach/proto/cg/var.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "param.h" #include "tables.h" #include "types.h" @@ -9,12 +11,6 @@ static char rcsid[] = "$Id$"; #include "data.h" #include "result.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ int stackheight = 0; token_t fakestack[MAXFSTACK];