1985-01-08 15:34:54 +00:00
|
|
|
#ifndef NORCSID
|
1994-06-24 14:02:31 +00:00
|
|
|
static char rcsid[] = "$Id$";
|
1985-01-08 15:34:54 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-12 00:35:48 +00:00
|
|
|
#include <assert.h>
|
2006-07-18 17:10:29 +00:00
|
|
|
#include <stdlib.h>
|
2013-05-07 23:48:48 +00:00
|
|
|
#include <stdio.h>
|
1985-01-08 15:34:54 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "tables.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include <cgg_cg.h>
|
|
|
|
#include "data.h"
|
|
|
|
#include "result.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
1987-03-10 01:26:51 +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".
|
1985-01-08 15:34:54 +00:00
|
|
|
*
|
|
|
|
* Author: Hans van Staveren
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Package for string allocation and garbage collection.
|
|
|
|
* Call salloc(size) to get room for string.
|
|
|
|
* Every now and then call garbage_collect() from toplevel.
|
|
|
|
*/
|
|
|
|
|
1987-08-06 13:00:47 +00:00
|
|
|
#define MAXSTAB 1500
|
1985-01-08 15:34:54 +00:00
|
|
|
#define THRESHOLD 200
|
|
|
|
|
|
|
|
char *stab[MAXSTAB];
|
|
|
|
int nstab=0;
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
static void chkstr(string, char *);
|
2016-11-10 21:04:18 +00:00
|
|
|
|
1985-01-08 15:34:54 +00:00
|
|
|
string myalloc(size) {
|
2017-11-13 17:44:17 +00:00
|
|
|
string p;
|
1985-01-08 15:34:54 +00:00
|
|
|
|
2017-01-16 21:45:03 +00:00
|
|
|
p = (string) calloc((unsigned)size, 1);
|
1985-01-08 15:34:54 +00:00
|
|
|
if (p==0)
|
|
|
|
fatal("Out of memory");
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
void myfree(string p) {
|
1985-01-08 15:34:54 +00:00
|
|
|
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
void popstr(int nnstab) {
|
|
|
|
int i;
|
1985-01-08 15:34:54 +00:00
|
|
|
|
|
|
|
for (i=nnstab;i<nstab;i++)
|
|
|
|
myfree(stab[i]);
|
|
|
|
nstab = nnstab;
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
char *salloc(int size) {
|
|
|
|
char *p;
|
1985-01-08 15:34:54 +00:00
|
|
|
|
|
|
|
if (nstab==MAXSTAB)
|
|
|
|
fatal("String table overflow");
|
|
|
|
p = myalloc(size+1); /* extra room for terminating zero */
|
|
|
|
stab[nstab++] = p;
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2017-11-13 17:44:17 +00:00
|
|
|
static int compar(const void *v1, const void *v2) {
|
|
|
|
char *const *p1 = v1;
|
|
|
|
char *const *p2 = v2;
|
1985-01-08 15:34:54 +00:00
|
|
|
|
|
|
|
assert(*p1 != *p2);
|
|
|
|
if (*p1 < *p2)
|
|
|
|
return(-1);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
void garbage_collect(void) {
|
|
|
|
int i;
|
1985-01-08 15:34:54 +00:00
|
|
|
struct emline *emlp;
|
|
|
|
token_p tp;
|
|
|
|
tkdef_p tdp;
|
|
|
|
struct reginfo *rp;
|
2017-11-12 16:25:18 +00:00
|
|
|
char **fillp,**scanp;
|
1985-01-08 15:34:54 +00:00
|
|
|
char used[MAXSTAB]; /* could be bitarray */
|
|
|
|
|
|
|
|
if (nstab<THRESHOLD)
|
|
|
|
return;
|
1988-06-28 14:41:02 +00:00
|
|
|
qsort((char *)stab,nstab,sizeof (char *),compar);
|
1985-01-08 15:34:54 +00:00
|
|
|
for (i=0;i<nstab;i++)
|
|
|
|
used[i]= FALSE;
|
2017-11-13 17:44:17 +00:00
|
|
|
for (emlp=emlines;emlp<emlines+nemlines;emlp++)
|
1985-01-08 15:34:54 +00:00
|
|
|
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_ADDR)
|
|
|
|
chkstr(tp->t_att[i].aa.ea_str,used);
|
|
|
|
}
|
|
|
|
for (rp= machregs+1; 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_ADDR)
|
|
|
|
chkstr(tp->t_att[i].aa.ea_str,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;
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:25:18 +00:00
|
|
|
static void
|
|
|
|
chkstr(string str, char *used) {
|
|
|
|
int low,middle,high;
|
1985-01-08 15:34:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|