1987-03-09 19:15:41 +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 09:59:28 +00:00
|
|
|
#ifndef NORCSID
|
2019-05-10 17:17:24 +00:00
|
|
|
static char rcsid[] = "$Id$";
|
1985-01-08 09:59:28 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
#include <assert.h>
|
1985-01-08 09:59:28 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "set.h"
|
2013-05-10 11:04:21 +00:00
|
|
|
#include "extern.h"
|
2019-05-10 17:17:24 +00:00
|
|
|
#include "hall.h"
|
1985-01-08 09:59:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements the marriage thesis from Hall.
|
2019-05-10 17:17:24 +00:00
|
|
|
* The thesis says that givoid hallverbose(void)ven a number, say N, of subsets from
|
1985-01-08 09:59:28 +00:00
|
|
|
* a finite set, it is possible to create a set with cardinality N,
|
|
|
|
* that contains one member for each of the subsets,
|
|
|
|
* iff for each number, say M, of subsets from 2 to N the union of
|
|
|
|
* each M-tuple sets has cardinality >= M.
|
|
|
|
*
|
|
|
|
* So what, you might say. As indeed I did.
|
|
|
|
* But this is actually used here to check the possibility of each
|
|
|
|
* code rule. If a code rule has a number of token_sets in the with
|
|
|
|
* clause and a number of properties in the uses rule it must be
|
|
|
|
* possible to do this from an empty fakestack. Hall helps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAXHALL (TOKPATMAX+MAXALLREG)
|
|
|
|
short hallsets[MAXHALL][SETSIZE];
|
2019-05-10 17:17:24 +00:00
|
|
|
int nhallsets = -1;
|
1985-01-08 09:59:28 +00:00
|
|
|
int hallfreq[MAXHALL][2];
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
int recurhall(int, short [][SETSIZE]);
|
|
|
|
void unite(register short *, short *);
|
|
|
|
|
|
|
|
|
|
|
|
void hallverbose(void)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register int max;
|
|
|
|
|
|
|
|
fprintf(stderr, "Table of hall frequencies\n # pre post\n");
|
|
|
|
for (max = MAXHALL - 1; hallfreq[max][0] == 0 && hallfreq[max][1] == 0;
|
|
|
|
max--)
|
1985-01-08 09:59:28 +00:00
|
|
|
;
|
2019-05-10 17:17:24 +00:00
|
|
|
for (i = 0; i <= max; i++)
|
|
|
|
fprintf(stderr, "%3d%6d%6d\n", i, hallfreq[i][0], hallfreq[i][1]);
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
void inithall(void)
|
|
|
|
{
|
1985-01-08 09:59:28 +00:00
|
|
|
|
|
|
|
assert(nhallsets == -1);
|
2019-05-10 17:17:24 +00:00
|
|
|
nhallsets = 0;
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
void nexthall(register short *sp)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
assert(nhallsets >= 0);
|
|
|
|
for (i = 0; i < SETSIZE; i++)
|
1985-01-08 09:59:28 +00:00
|
|
|
hallsets[nhallsets][i] = sp[i];
|
|
|
|
nhallsets++;
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
int card(register short *sp)
|
|
|
|
{
|
|
|
|
register int sum, i;
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < 8 * sizeof(short) * SETSIZE; i++)
|
|
|
|
if (BIT(sp, i))
|
1985-01-08 09:59:28 +00:00
|
|
|
sum++;
|
2019-05-10 17:17:24 +00:00
|
|
|
return (sum);
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
void checkhall(void)
|
|
|
|
{
|
|
|
|
assert(nhallsets >= 0);
|
1985-01-08 09:59:28 +00:00
|
|
|
if (!hall())
|
|
|
|
error("Hall says: \"You can't have those registers\"");
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
int hall(void)
|
|
|
|
{
|
|
|
|
register int i, j, k;
|
1985-01-08 09:59:28 +00:00
|
|
|
int ok;
|
2019-05-10 17:17:24 +00:00
|
|
|
|
1985-01-08 09:59:28 +00:00
|
|
|
hallfreq[nhallsets][0]++;
|
|
|
|
/*
|
|
|
|
* If a set has cardinality >= nhallsets it can never be the cause
|
|
|
|
* of the hall algorithm failing. So it can be thrown away.
|
|
|
|
* But then nhallsets is less, so this step can be re-applied.
|
|
|
|
*/
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
do
|
|
|
|
{
|
1985-01-08 09:59:28 +00:00
|
|
|
ok = 0;
|
2019-05-10 17:17:24 +00:00
|
|
|
for (i = 0; i < nhallsets; i++)
|
|
|
|
if (card(hallsets[i]) >= nhallsets)
|
|
|
|
{
|
|
|
|
for (j = i + 1; j < nhallsets; j++)
|
|
|
|
for (k = 0; k < SETSIZE; k++)
|
|
|
|
hallsets[j - 1][k] = hallsets[j][k];
|
1985-01-08 09:59:28 +00:00
|
|
|
nhallsets--;
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (ok);
|
2019-05-10 17:17:24 +00:00
|
|
|
|
1985-01-08 09:59:28 +00:00
|
|
|
/*
|
|
|
|
* Now all sets have cardinality < nhallsets
|
|
|
|
*/
|
2019-05-10 17:17:24 +00:00
|
|
|
|
1985-01-08 09:59:28 +00:00
|
|
|
hallfreq[nhallsets][1]++;
|
2019-05-10 17:17:24 +00:00
|
|
|
ok = recurhall(nhallsets, hallsets);
|
1985-01-08 09:59:28 +00:00
|
|
|
nhallsets = -1;
|
2019-05-10 17:17:24 +00:00
|
|
|
return (ok);
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
int recurhall(int nhallsets, short hallsets[][SETSIZE])
|
|
|
|
{
|
1985-01-08 09:59:28 +00:00
|
|
|
short copysets[MAXHALL][SETSIZE];
|
|
|
|
short setsum[SETSIZE];
|
2019-05-10 17:17:24 +00:00
|
|
|
register int i, j, k, ncopys;
|
|
|
|
|
1985-01-08 09:59:28 +00:00
|
|
|
/*
|
|
|
|
* First check cardinality of union of all
|
|
|
|
*/
|
2019-05-10 17:17:24 +00:00
|
|
|
for (k = 0; k < SETSIZE; k++)
|
|
|
|
setsum[k] = 0;
|
|
|
|
for (i = 0; i < nhallsets; i++)
|
|
|
|
unite(hallsets[i], setsum);
|
|
|
|
if (card(setsum) < nhallsets)
|
|
|
|
return (0);
|
1985-01-08 09:59:28 +00:00
|
|
|
/*
|
|
|
|
* Now check the hall property of everything but one set,
|
|
|
|
* for all sets
|
|
|
|
*/
|
2019-05-10 17:17:24 +00:00
|
|
|
for (i = 0; i < nhallsets; i++)
|
|
|
|
{
|
|
|
|
ncopys = 0;
|
|
|
|
for (j = 0; j < nhallsets; j++)
|
|
|
|
if (j != i)
|
|
|
|
{
|
|
|
|
for (k = 0; k < SETSIZE; k++)
|
|
|
|
copysets[ncopys][k] = hallsets[j][k];
|
|
|
|
ncopys++;
|
|
|
|
}
|
|
|
|
assert(ncopys == nhallsets - 1);
|
|
|
|
if (!recurhall(ncopys, copysets))
|
|
|
|
return (0);
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
2019-05-10 17:17:24 +00:00
|
|
|
return (1);
|
1985-01-08 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:24 +00:00
|
|
|
void unite(register short *sp, short *into)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SETSIZE; i++)
|
1985-01-08 09:59:28 +00:00
|
|
|
into[i] |= sp[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Limerick (rot13)
|
|
|
|
*
|
|
|
|
* N zngurzngvpvna anzrq Unyy
|
|
|
|
* Unf n urknurqebavpny onyy,
|
|
|
|
* Naq gur phor bs vgf jrvtug
|
|
|
|
* Gvzrf uvf crpxre'f, cyhf rvtug
|
|
|
|
* Vf uvf cubar ahzore -- tvir uvz n pnyy..
|
|
|
|
*/
|