ANSI C conversion

This commit is contained in:
carl 2019-05-11 01:11:54 +08:00
parent 708c1ba1b0
commit de45c1036d
27 changed files with 1370 additions and 960 deletions

View file

@ -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,7 +51,11 @@ 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;
@ -93,69 +99,89 @@ unsigned codegen(codep,ply,toplevel,costlimit,forced) byte *codep; unsigned cost
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 stackpad;
struct perm *tup,*ntup,*besttup,*tuples();
struct perm *tup, *ntup, *besttup;
#ifndef NDEBUG
level++;
DEBUG("Entering codegen");
#endif
for (;;) {
switch( (*codep++)&037 ) {
for (;;)
{
switch ((*codep++) & 037)
{
default:
assert(FALSE);
/* NOTREACHED */
case DO_NEXTEM:
DEBUG("NEXTEM");
DEBUG("NEXTEM")
;
tokpatlen = 0;
nallreg = 0;
if (toplevel) {
if (toplevel)
{
garbage_collect();
totalcost = 0;
} else {
}
else
{
if (--ply <= 0)
goto doreturn;
}
if (stackheight > MAXFSTACK - 7)
totalcost += stackupto(&fakestack[6], ply, toplevel);
bp = nextem(toplevel);
if (bp == 0) {
if (bp == 0)
{
/*
* No pattern found, can be pseudo or error
* in table.
*/
if (toplevel) {
if (toplevel)
{
codep--;
DEBUG("pseudo");
dopseudo();
} else
}
else
goto doreturn;
} else {
}
else
{
#ifndef NDEBUG
chkregs();
#endif
if (! toplevel) {
if (!toplevel)
{
ply -= emp - saveemp + 1;
if (ply <= 0) ply = 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++) {
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);
fprintf(stderr, "distance of pos %d is %u\n", i,
dist);
#endif
if (dist<=mindistance) {
if (dist<mindistance) {
if (dist <= mindistance)
{
if (dist < mindistance)
{
#ifdef SHORTCUT
if (dist == 0)
goto gotit;
@ -167,35 +193,45 @@ if (Debug)
}
}
assert(mindistance<MAXINT);
if (npos>1) {
if (npos > 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++) {
for (i = 0; i < npos; i++)
{
t = codegen(&coderules[pos[i]], ply, FALSE,
costlimit<MAXINT?mincost:MAXINT,0);
costlimit < MAXINT ? mincost : MAXINT,
0);
#ifndef NDEBUG
if (Debug)
fprintf(stderr,"mincost %u,cost %u,pos %d\n",mincost,t,i);
fprintf(stderr,
"mincost %u,cost %u,pos %d\n",
mincost, t, i);
#endif
if (t<mincost) {
if (t < mincost)
{
mincost = t;
cindex = pos[i];
}
RESTST;
}
FREEST;
if (totalcost+mincost>costlimit) {
} FREEST;
if (totalcost + mincost > costlimit)
{
totalcost += mincost;
BROKE();
}
} else {
}
else
{
cindex = pos[0];
}
} else {
}
else
{
getint(cindex, bp);
}
@ -208,34 +244,43 @@ if (Debug)
}
break;
case DO_COERC:
DEBUG("COERC");
DEBUG("COERC")
;
tokpatlen = 1;
inscoerc = 1;
break;
case DO_XXMATCH:
DEBUG("XXMATCH");
DEBUG("XXMATCH")
;
case DO_XMATCH:
DEBUG("XMATCH");
DEBUG("XMATCH")
;
tokpatlen = (codep[-1] >> 5) & 07;
for (i = 0; i < tokpatlen; i++)
getint(tokexp[i],codep);
getint(tokexp[i], codep)
;
tokexp[i] = 0;
break; /* match already checked by distance() */
case DO_MATCH:
DEBUG("MATCH");
DEBUG("MATCH")
;
tokpatlen = (codep[-1] >> 5) & 07;
for (i = 0; i < tokpatlen; i++)
getint(tokexp[i],codep);
getint(tokexp[i], codep)
;
tokexp[i] = 0;
tp = &fakestack[stackheight - 1];
i = 0;
while (i<tokpatlen && tp>=fakestack) {
while (i < tokpatlen && tp >= fakestack)
{
size = tsize(tp);
while (i<tokpatlen && (lsize=ssize(tokexp[i]))<=size) {
while (i < tokpatlen && (lsize = ssize(tokexp[i])) <= size)
{
size -= lsize;
i++;
}
if (i<tokpatlen && size!=0) {
if (i < tokpatlen && size != 0)
{
totalcost += stackupto(tp, ply, toplevel);
CHKCOST();
break;
@ -244,13 +289,16 @@ if (Debug)
}
tp = &fakestack[stackheight - 1];
i = 0;
while (i<tokpatlen && tp >= fakestack) {
while (i < tokpatlen && tp >= fakestack)
{
size = tsize(tp);
lsize = ssize(tokexp[i]);
if (size != lsize) { /* find coercion */
if (size != lsize)
{ /* find coercion */
#ifdef MAXSPLIT
sret = split(tp,&tokexp[i],ply,toplevel);
if (sret==0) {
if (sret==0)
{
#endif /* MAXSPLIT */
totalcost += stackupto(tp, ply, toplevel);
CHKCOST();
@ -259,27 +307,36 @@ if (Debug)
}
i += sret;
#endif /* MAXSPLIT */
} else
}
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)) {
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) {
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) {
}
else
{
if (cp->c3_prop == 0)
{
totalcost += docoerc(tp, cp, ply, toplevel, 0);
CHKCOST();
} else {
}
else
{
assert(nregneeded<MAXCREG);
regtp[nregneeded] = tp;
regcp[nregneeded] = cp;
@ -288,12 +345,16 @@ if (Debug)
}
}
}
i++; tp--;
i++;
tp--;
}
if (tokpatlen>stackheight) {
if (tokpatlen > stackheight)
{
int k;
stackpad = tokpatlen - stackheight;
for (j=stackheight-1, k = j + stackpad;j>=0;j--, k--) {
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
@ -305,28 +366,36 @@ if (Debug)
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) {
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) {
if (cp->c3_prop == 0)
{
totalcost += docoerc(tp, cp, ply, toplevel, 0);
CHKCOST();
} else {
}
else
{
assert(nregneeded<MAXCREG);
regtp[nregneeded] = tp;
regcp[nregneeded] = cp;
regls[nregneeded] = curreglist;
nregneeded++;
}
i++; tp--;
i++;
tp--;
}
} else
}
else
stackpad = 0;
assert(i == tokpatlen);
if (nregneeded == 0)
@ -335,49 +404,62 @@ if (Debug)
mincost = costlimit - totalcost + 1;
tup = tuples(regls, nregneeded);
besttup = 0;
for (; tup != 0; tup = ntup) {
for (; tup != 0; tup = ntup)
{
#ifndef NDEBUG
if(Debug>1) { fprintf(stderr,"Next tuple %d,%d,%d,%d\n",
tup->p_rar[0],
tup->p_rar[1],
tup->p_rar[2],
if (Debug > 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",
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]);
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);
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) {
t += codegen(codep, ply, FALSE,
mincost < MAXINT ? mincost - t : MAXINT, 0);
if (t < mincost)
{
mincost = t;
besttup = tup;
} else
}
else
myfree(tup);
RESTST;
}
FREEST;
for (i = 0; i < nregneeded; i++)
myfree(regls[i]);
if (totalcost+mincost>costlimit) {
if (totalcost + mincost > costlimit)
{
if (besttup)
myfree(besttup);
if (stackpad!=tokpatlen) {
if (stackpad) {
if (costlimit<MAXINT) {
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[stackheight - 1],
ply, toplevel);
}
else
totalcost += stackupto(fakestack, ply, toplevel);
CHKCOST();
goto nextmatch;
@ -386,20 +468,27 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
BROKE();
}
for (i = 0; i < nregneeded; i++)
totalcost += docoerc(regtp[i],regcp[i],ply,toplevel,besttup->p_rar[i]);
totalcost += docoerc(regtp[i], regcp[i], ply, toplevel,
besttup->p_rar[i]);
myfree(besttup);
break;
case DO_REMOVE:
DEBUG("REMOVE");
if (codep[-1]&32) {
DEBUG("REMOVE")
;
if (codep[-1] & 32)
{
getint(texpno, codep);
getint(nodeno, codep);
} else {
}
else
{
getint(texpno, codep);
nodeno = 0;
}
for (tp= &fakestack[stackheight-tokpatlen-1];tp>=&fakestack[0];tp--)
if (match(tp,&machsets[texpno],nodeno)) {
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();
@ -410,34 +499,44 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
rp->r_contents.t_token = 0;
break;
case DO_RREMOVE: /* register remove */
DEBUG("RREMOVE");
getint(nodeno,codep);
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) {
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 {
}
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)
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();
CHKCOST()
;
break;
case DO_DEALLOCATE:
DEBUG("DEALLOCATE");
getint(tinstno,codep);
DEBUG("DEALLOCATE")
;
getint(tinstno, codep)
;
instance(tinstno, &token);
if (token.t_token == -1)
chrefcount(token.t_att[0].ar, -1, TRUE);
else {
else
{
tdp = &tokens[token.t_token];
for (i = 0; i < TOKENSIZE; i++)
if (tdp->t_type[i] == EV_REG)
@ -445,28 +544,37 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
}
break;
case DO_REALLOCATE:
DEBUG("REALLOCATE");
DEBUG("REALLOCATE")
;
for (rp = machregs; rp < machregs + NREGS; rp++)
if(rp->r_tcount) {
if (rp->r_tcount)
{
rp->r_refcount -= rp->r_tcount;
rp->r_tcount = 0;
}
break;
case DO_ALLOCATE:
DEBUG("ALLOCATE");
if (codep[-1]&32) {
DEBUG("ALLOCATE")
;
if (codep[-1] & 32)
{
getint(propno, codep);
getint(tinstno, codep);
} else {
}
else
{
getint(propno, codep);
tinstno = 0;
}
instance(tinstno, &token);
if (!forced) {
do {
if (!forced)
{
do
{
npos = exactmatch = 0;
for(rpp=reglist[propno];rp= *rpp; rpp++)
if (getrefcount((int)(rp-machregs), FALSE)==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++;
@ -475,24 +583,33 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
* 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) {
if (npos == 0)
{
if (stackheight <= tokpatlen)
{
if (!toplevel)
{
totalcost = INFINITY;
BROKE();
} else {
}
else
{
fatal("No regs available");
}
}
totalcost += stackupto( &fakestack[0],ply,toplevel);
totalcost += stackupto(&fakestack[0], ply,
toplevel);
CHKCOST();
}
} while (npos == 0);
if (!exactmatch) {
if (!exactmatch)
{
npos2 = npos;
for (i = 0; i < npos; i++)
pos2[i] = pos[i];
} else {
}
else
{
/*
* Now we are reducing the number of possible registers.
* We take only one equally likely register out of every
@ -501,10 +618,12 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
mtoken = token;
npos2 = 0;
for (i = 0; i < npos; i++)
if (eqtoken(&machregs[pos[i]].r_contents,&mtoken)) {
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])) {
if (eqregclass(pos2[j], pos[i]))
{
npos2--;
break;
}
@ -519,36 +638,45 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
token2.t_att[i].aw = 0;
if (npos2 == 1)
decision = pos2[0];
else {
else
{
SAVEST;
mincost = costlimit - totalcost + 1;
for(j=0;j<npos2;j++) {
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 {
else
{
t = 0;
erasereg(pos2[j]);
}
if (t < mincost)
t += codegen(codep,ply,FALSE,mincost<MAXINT?mincost-t:MAXINT,0);
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) {
} FREEST;
if (totalcost + mincost > costlimit)
{
totalcost = INFINITY;
BROKE();
}
}
} else {
}
else
{
decision = forced;
if (getrefcount(decision, FALSE)!=0) {
if (getrefcount(decision, FALSE) != 0)
{
totalcost = INFINITY;
BROKE();
}
@ -556,64 +684,84 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
}
chrefcount(decision, 1, FALSE);
token2.t_att[0].ar = decision;
if (token.t_token != 0) {
if (token.t_token != 0)
{
totalcost += move(&token, &token2, ply, toplevel, MAXINT);
CHKCOST();
} else
}
else
erasereg(decision);
allreg[nallreg++] = decision;
break;
case DO_LOUTPUT:
DEBUG("LOUTPUT");
getint(stringno,codep);
getint(nodeno,codep);
if (toplevel) {
DEBUG("LOUTPUT")
;
getint(stringno, codep)
;
getint(nodeno, codep)
;
if (toplevel)
{
gencode(codestrings[stringno]);
genexpr(nodeno);
}
break;
case DO_ROUTPUT:
DEBUG("ROUTPUT");
DEBUG("ROUTPUT")
;
i = ((codep[-1] >> 5) & 07);
do {
do
{
getint(stringno, codep);
if (toplevel) {
if (toplevel)
{
gencode(codestrings[stringno]);
gennl();
}
} while (i--);
break;
case DO_MOVE:
DEBUG("MOVE");
getint(tinstno,codep);
DEBUG("MOVE")
;
getint(tinstno, codep)
;
instance(tinstno, &token);
getint(tinstno,codep);
getint(tinstno, codep)
;
instance(tinstno, &token2);
totalcost += move(&token,&token2,ply,toplevel,costlimit-totalcost+1);
CHKCOST();
totalcost += move(&token, &token2, ply, toplevel,
costlimit - totalcost + 1);
CHKCOST()
;
break;
case DO_ERASE:
DEBUG("ERASE");
getint(nodeno,codep);
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");
DEBUG("TOKREPLACE")
;
assert(stackheight >= tokpatlen);
repllen = (codep[-1] >> 5) & 07;
for(i=0;i<repllen;i++) {
for (i = 0; i < repllen; i++)
{
getint(tinstno, codep);
instance(tinstno, &reptoken[i]);
tref(&reptoken[i], 1);
}
for(i=0;i<tokpatlen;i++) {
for (i = 0; i < tokpatlen; i++)
{
if (!inscoerc)
tref(&fakestack[stackheight - 1], -1);
stackheight--;
}
for (i=0;i<repllen;i++) {
for (i = 0; i < repllen; i++)
{
assert(stackheight<MAXFSTACK);
fakestack[stackheight] = reptoken[i];
stackheight++;
@ -625,10 +773,12 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
chrefcount(allreg[i], -1, FALSE);
break;
case DO_EMREPLACE:
DEBUG("EMREPLACE");
DEBUG("EMREPLACE")
;
emrepllen = (codep[-1] >> 5) & 07;
j = emp - emlines;
if (emrepllen>j) {
if (emrepllen > j)
{
int k = nemlines + emrepllen - j;
assert(k<MAXEMLINES);
for (i = nemlines; i >= 0; i--, k--)
@ -637,12 +787,14 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
emp += emrepllen - j;
}
emp -= emrepllen;
for (i=0;i<emrepllen;i++) {
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) {
switch (result.e_typ)
{
default:
assert(FALSE);
case 0:
@ -660,27 +812,34 @@ if (Debug > 1) fprintf(stderr, "cost after coercions: %u\n", t);
break;
}
}
if (!toplevel) {
if (!toplevel)
{
ply += emrepllen;
}
break;
case DO_COST:
DEBUG("COST");
getint(cost.c_size,codep);
getint(cost.c_time,codep);
DEBUG("COST")
;
getint(cost.c_size, codep)
;
getint(cost.c_time, codep)
;
totalcost += costcalc(cost);
CHKCOST();
CHKCOST()
;
break;
#ifdef REGVARS
case DO_PRETURN:
if (toplevel) {
if (toplevel)
{
swtxt();
regreturn(); /* in mach.c */
}
break;
#endif
case DO_RETURN:
DEBUG("RETURN");
DEBUG("RETURN")
;
assert(origcp != startupcode);
doreturn:
#ifndef NDEBUG

13
mach/proto/cg/codegen.h Normal file
View file

@ -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_ */

View file

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -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;

View file

@ -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_ */

View file

@ -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)
{

View file

@ -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_ */

View file

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -18,23 +20,48 @@ static char rcsid2[] = "$Id$";
#include <cg_pattern.h>
#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 <em_reg.h>
#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));

22
mach/proto/cg/fillem.h Normal file
View file

@ -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_ */

View file

@ -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 <stdio.h>
#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) {
if (Debug)
{
codefile = stderr;
if (!isatty(2))
setbuf(stderr,stderrbuff);
} else {
}
else
{
#endif
if (filename == (char *) 0)
codefile = stdout;
else
if ((codefile=freopen(filename,"w",stdout))==NULL)
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;

22
mach/proto/cg/gencode.h Normal file
View file

@ -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_ */

View file

@ -1,28 +1,26 @@
#ifndef NORCSID
static char rcsid[] = "$Id$";
#endif
#include <stdlib.h>
#include <string.h>
#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 <stdlib.h>
#include <string.h>
#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)

View file

@ -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_ */

View file

@ -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 <stdlib.h>
#include "tables.h"
#include "types.h"
#include <cg_pattern.h>
#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);
}

View file

@ -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);

View file

@ -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 <em_spec.h>
#include <em_flag.h>
#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 <stdio.h>
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;

View file

@ -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;rp<machregs+NREGS;rp++) {
rp->r_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;

17
mach/proto/cg/reg.h Normal file
View file

@ -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_ */

View file

@ -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 <cg_pattern.h>
@ -15,18 +19,20 @@ static char rcsid[] = "$Id$";
#include <em_reg.h>
#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;

View file

@ -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_ */

View file

@ -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 <stdlib.h>
#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 (str<stab[middle])
high = middle-1;
else
low = middle+1;
}
if (low==high) {
if (str==stab[low]) {
used[low]=1;
}
return;
}
}
string myalloc(int size)
{
register string p;
p = (string) malloc(size);
@ -40,22 +65,22 @@ string myalloc(size) {
return(p);
}
myfree(p) string p; {
void myfree(void* p)
{
free(p);
}
popstr(nnstab) {
register i;
void popstr(int nnstab)
{
register int i;
for (i=nnstab;i<nstab;i++)
myfree(stab[i]);
nstab = nnstab;
}
char *salloc(size) {
char *salloc(int size)
{
register char *p;
if (nstab==MAXSTAB)
fatal("String table overflow");
p = myalloc(size+1); /* extra room for terminating zero */
@ -63,16 +88,16 @@ char *salloc(size) {
return(p);
}
compar(p1,p2) char **p1,**p2; {
int compar(char **p1, char **p2)
{
assert(*p1 != *p2);
if (*p1 < *p2)
return(-1);
return(1);
}
garbage_collect() {
register i;
void garbage_collect(void) {
register int i;
struct emline *emlp;
token_p tp;
tkdef_p tdp;
@ -115,25 +140,3 @@ garbage_collect() {
nstab = fillp-stab;
}
chkstr(str,used) string str; char used[]; {
register low,middle,high;
low=0; high=nstab-1;
while (high>low) {
middle= (low+high)>>1;
if (str==stab[middle]) {
used[middle]=1;
return;
}
if (str<stab[middle])
high = middle-1;
else
low = middle+1;
}
if (low==high) {
if (str==stab[low]) {
used[low]=1;
}
return;
}
}

20
mach/proto/cg/salloc.h Normal file
View file

@ -0,0 +1,20 @@
/* Copyright (c) 2019 ACK Project.
* See the copyright notice in the ACK home directory,
* in the file "Copyright".
*
* Created on: 2019-04-14
*
*/
#ifndef SALLOC_H_
#define SALLOC_H_
#include "types.h"
string myalloc(int size);
void myfree(void* p);
void popstr(int nnstab);
char *salloc(int size);
int compar(char **p1, char **p2);
void garbage_collect(void);
#endif /* SALLOC_H_ */

View file

@ -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"
@ -11,13 +13,10 @@ static char rcsid[] = "$Id$";
#include "result.h"
#include "state.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
*/
extern int nstab; /* salloc.c */
@ -27,10 +26,24 @@ extern string myalloc();
state_p stlist=0;
#endif
static void bmove(register short *from, register short *to, register int nbytes)
{
if (nbytes<=0)
return;
assert(sizeof(short)==2 && (nbytes&1)==0);
nbytes>>=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);
}

View file

@ -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_ */

View file

@ -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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "assert.h"
#include <stdio.h>
#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);
}

47
mach/proto/cg/subr.h Normal file
View file

@ -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_ */

View file

@ -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_ */

View file

@ -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];