2019-05-10 17:11:54 +00:00
|
|
|
/*
|
|
|
|
* (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
|
|
|
|
*/
|
2006-07-18 17:10:29 +00:00
|
|
|
#include <stdlib.h>
|
1984-05-18 21:27:39 +00:00
|
|
|
#include "assert.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "tables.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include <cg_pattern.h>
|
|
|
|
#include "data.h"
|
|
|
|
#include "result.h"
|
|
|
|
#include "extern.h"
|
2019-05-10 17:11:54 +00:00
|
|
|
#include "subr.h"
|
|
|
|
#include "salloc.h"
|
|
|
|
|
1984-05-18 21:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Package for string allocation and garbage collection.
|
|
|
|
* Call salloc(size) to get room for string.
|
|
|
|
* Every now and then call garbage_collect() from toplevel.
|
|
|
|
*/
|
|
|
|
|
1985-09-06 11:06:32 +00:00
|
|
|
#define MAXSTAB 2500
|
1984-05-18 21:27:39 +00:00
|
|
|
#define THRESHOLD 200
|
|
|
|
|
|
|
|
char *stab[MAXSTAB];
|
|
|
|
int nstab=0;
|
|
|
|
|
2019-05-10 17:11:54 +00:00
|
|
|
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)
|
|
|
|
{
|
1984-05-18 21:27:39 +00:00
|
|
|
register string p;
|
|
|
|
|
|
|
|
p = (string) malloc(size);
|
|
|
|
if (p==0)
|
|
|
|
fatal("Out of memory");
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:11:54 +00:00
|
|
|
void myfree(void* p)
|
|
|
|
{
|
1984-05-18 21:27:39 +00:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:11:54 +00:00
|
|
|
void popstr(int nnstab)
|
|
|
|
{
|
|
|
|
register int i;
|
1984-05-18 21:27:39 +00:00
|
|
|
for (i=nnstab;i<nstab;i++)
|
|
|
|
myfree(stab[i]);
|
|
|
|
nstab = nnstab;
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:11:54 +00:00
|
|
|
char *salloc(int size)
|
|
|
|
{
|
1984-05-18 21:27:39 +00:00
|
|
|
register char *p;
|
|
|
|
if (nstab==MAXSTAB)
|
|
|
|
fatal("String table overflow");
|
|
|
|
p = myalloc(size+1); /* extra room for terminating zero */
|
|
|
|
stab[nstab++] = p;
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2022-08-01 20:08:23 +00:00
|
|
|
int compar(const void* pp1, const void* pp2)
|
2019-05-10 17:11:54 +00:00
|
|
|
{
|
2022-08-01 20:08:23 +00:00
|
|
|
const char **p1 = (const char**) pp1;
|
|
|
|
const char **p2 = (const char**) pp2;
|
|
|
|
|
1984-05-18 21:27:39 +00:00
|
|
|
assert(*p1 != *p2);
|
|
|
|
if (*p1 < *p2)
|
|
|
|
return(-1);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:11:54 +00:00
|
|
|
void garbage_collect(void) {
|
|
|
|
register int i;
|
1984-05-18 21:27:39 +00:00
|
|
|
struct emline *emlp;
|
|
|
|
token_p tp;
|
|
|
|
tkdef_p tdp;
|
|
|
|
struct reginfo *rp;
|
|
|
|
register char **fillp,**scanp;
|
|
|
|
char used[MAXSTAB]; /* could be bitarray */
|
|
|
|
|
|
|
|
if (nstab<THRESHOLD)
|
|
|
|
return;
|
|
|
|
qsort(stab,nstab,sizeof (char *),compar);
|
|
|
|
for (i=0;i<nstab;i++)
|
|
|
|
used[i]= FALSE;
|
|
|
|
for(emlp=emlines;emlp<emlines+nemlines;emlp++)
|
|
|
|
chkstr(emlp->em_soper,used);
|
|
|
|
for (tp= fakestack;tp<&fakestack[stackheight];tp++) {
|
|
|
|
if (tp->t_token== -1)
|
|
|
|
continue;
|
|
|
|
tdp = &tokens[tp->t_token];
|
|
|
|
for (i=0;i<TOKENSIZE;i++)
|
|
|
|
if (tdp->t_type[i] == EV_STR)
|
|
|
|
chkstr(tp->t_att[i].as,used);
|
|
|
|
}
|
|
|
|
for (rp= machregs; rp<machregs+NREGS; rp++) {
|
|
|
|
tp = &rp->r_contents;
|
|
|
|
assert(tp->t_token != -1);
|
|
|
|
tdp= &tokens[tp->t_token];
|
|
|
|
for (i=0;i<TOKENSIZE;i++)
|
|
|
|
if (tdp->t_type[i] == EV_STR)
|
|
|
|
chkstr(tp->t_att[i].as,used);
|
|
|
|
}
|
|
|
|
for (i=0;i<nstab;i++)
|
|
|
|
if (!used[i]) {
|
|
|
|
myfree(stab[i]);
|
|
|
|
stab[i]=0;
|
|
|
|
}
|
|
|
|
fillp=stab;
|
|
|
|
for (scanp=stab;scanp<stab+nstab;scanp++)
|
|
|
|
if (*scanp != 0)
|
|
|
|
*fillp++ = *scanp;
|
|
|
|
nstab = fillp-stab;
|
|
|
|
}
|
|
|
|
|