Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
Memory optimization

Functions to reduce the memory footprint. More...

Functions

 pack_names ()
 
 pack_symbols ()
 
 pack_grammar ()
 

Detailed Description

Functions to reduce the memory footprint.

The functions in this module are used to pack the symbols manipulated by the program, switching from many small allocations to a few big memory chunks. This slightly reduces the memory footprint of the program, while also improving locality, thus incrementing the performance of the subsequent algorithms, which very often need to iterate on the data

Function Documentation

pack_grammar ( )

Definition at line 1680 of file reader.c.

References bucket::assoc, FREE, bucket::index, MALLOC, nitems, no_space(), nrules, prec, bucket::prec, rassoc, REALLOC, ritem, rlhs, rprec, rrhs, start_symbol, TERM, TOKEN, and UNDEFINED.

Referenced by reader().

1681 {
1682  register int i, j;
1683  int assoc, prec;
1684 
1685  ritem = (short *) MALLOC(nitems*sizeof(short));
1686  if (ritem == 0) no_space();
1687  rlhs = (short *) MALLOC(nrules*sizeof(short));
1688  if (rlhs == 0) no_space();
1689  rrhs = (short *) MALLOC((nrules+1)*sizeof(short));
1690  if (rrhs == 0) no_space();
1691  rprec = (short *) REALLOC(rprec, nrules*sizeof(short));
1692  if (rprec == 0) no_space();
1694  if (rassoc == 0) no_space();
1695 
1696  ritem[0] = -1;
1697  ritem[1] = goal->index;
1698  ritem[2] = 0;
1699  ritem[3] = -2;
1700  rlhs[0] = 0;
1701  rlhs[1] = 0;
1702  rlhs[2] = start_symbol;
1703  rrhs[0] = 0;
1704  rrhs[1] = 0;
1705  rrhs[2] = 1;
1706 
1707  j = 4;
1708  for (i = 3; i < nrules; ++i)
1709  {
1710  rlhs[i] = plhs[i]->index;
1711  rrhs[i] = j;
1712  assoc = TOKEN;
1713  prec = 0;
1714  while (pitem[j])
1715  {
1716  ritem[j] = pitem[j]->index;
1717  if (pitem[j]->class == TERM)
1718  {
1719  prec = pitem[j]->prec;
1720  assoc = pitem[j]->assoc;
1721  }
1722  ++j;
1723  }
1724  ritem[j] = -i;
1725  ++j;
1726  if (rprec[i] == UNDEFINED)
1727  {
1728  rprec[i] = prec;
1729  rassoc[i] = assoc;
1730  }
1731  }
1732  rrhs[i] = j;
1733 
1734  FREE(plhs);
1735  FREE(pitem);
1736 }
#define TOKEN
Definition: defs.h:56
#define MALLOC(n)
Definition: defs.h:103
short * rlhs
List of left-hand sides of all rules.
Definition: main.c:119
#define UNDEFINED
Definition: defs.h:77
int nitems
Definition: main.c:40
char * rassoc
Definition: main.c:128
bucket ** pitem
Definition: reader.c:26
#define TERM
Definition: defs.h:71
short prec
Definition: defs.h:120
bucket ** plhs
Definition: reader.c:29
int nrules
The number of rules in the grammar.
Definition: main.c:45
short * rprec
Definition: main.c:127
no_space()
Definition: error.c:27
short * rrhs
List of right-hand sides of all rules.
Definition: main.c:126
short index
Definition: defs.h:119
#define FREE(x)
Definition: defs.h:102
short * ritem
Representation of all productions (and items)
Definition: main.c:112
int start_symbol
Index of the starting symbol of the grammar.
Definition: main.c:82
bucket * goal
Definition: reader.c:20
int prec
Definition: reader.c:21
char assoc
Definition: defs.h:122
#define REALLOC(p, n)
Definition: defs.h:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

pack_names ( )

Definition at line 1507 of file reader.c.

References first_symbol, FREE, MALLOC, bucket::name, name_pool, name_pool_size, bucket::next, no_space(), and strcpy().

Referenced by reader().

1508 {
1509  register bucket *bp;
1510  register char *p, *s, *t;
1511 
1512  name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
1513  for (bp = first_symbol; bp; bp = bp->next)
1514  name_pool_size += strlen(bp->name) + 1;
1516  if (name_pool == 0) no_space();
1517 
1518  strcpy(name_pool, "$accept");
1519  strcpy(name_pool+8, "$end");
1520  t = name_pool + 13;
1521  for (bp = first_symbol; bp; bp = bp->next)
1522  {
1523  p = t;
1524  s = bp->name;
1525  while (*t++ = *s++) continue;
1526  FREE(bp->name);
1527  bp->name = p;
1528  }
1529 }
char * name_pool
Definition: reader.c:32
#define MALLOC(n)
Definition: defs.h:103
char * name
Definition: defs.h:116
bucket * first_symbol
Definition: symtab.c:11
no_space()
Definition: error.c:27
#define FREE(x)
Definition: defs.h:102
int name_pool_size
Definition: reader.c:31
struct bucket * next
Definition: defs.h:115
char * strcpy()
Definition: defs.h:112

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

pack_symbols ( )

Definition at line 1553 of file reader.c.

References bucket::assoc, bucket::class, first_symbol, FREE, bucket::index, MALLOC, bucket::name, name_pool, bucket::next, no_space(), nsyms, ntokens, nvars, bucket::prec, start_symbol, symbol_assoc, symbol_name, symbol_prec, symbol_value, TERM, TOKEN, UNDEFINED, shorts::value, and bucket::value.

Referenced by reader().

1554 {
1555  register bucket *bp;
1556  register bucket **v;
1557  register int i, j, k, n;
1558 
1559  nsyms = 2;
1560  ntokens = 1;
1561  for (bp = first_symbol; bp; bp = bp->next)
1562  {
1563  ++nsyms;
1564  if (bp->class == TERM) ++ntokens;
1565  }
1567  nvars = nsyms - ntokens;
1568 
1569  symbol_name = (char **) MALLOC(nsyms*sizeof(char *));
1570  if (symbol_name == 0) no_space();
1571  symbol_value = (short *) MALLOC(nsyms*sizeof(short));
1572  if (symbol_value == 0) no_space();
1573  symbol_prec = (short *) MALLOC(nsyms*sizeof(short));
1574  if (symbol_prec == 0) no_space();
1576  if (symbol_assoc == 0) no_space();
1577 
1578  v = (bucket **) MALLOC(nsyms*sizeof(bucket *));
1579  if (v == 0) no_space();
1580 
1581  v[0] = 0;
1582  v[start_symbol] = 0;
1583 
1584  i = 1;
1585  j = start_symbol + 1;
1586  for (bp = first_symbol; bp; bp = bp->next)
1587  {
1588  if (bp->class == TERM)
1589  v[i++] = bp;
1590  else
1591  v[j++] = bp;
1592  }
1593  assert(i == ntokens && j == nsyms);
1594 
1595  for (i = 1; i < ntokens; ++i)
1596  v[i]->index = i;
1597 
1598  goal->index = start_symbol + 1;
1599  k = start_symbol + 2;
1600  while (++i < nsyms)
1601  if (v[i] != goal)
1602  {
1603  v[i]->index = k;
1604  ++k;
1605  }
1606 
1607  goal->value = 0;
1608  k = 1;
1609  for (i = start_symbol + 1; i < nsyms; ++i)
1610  {
1611  if (v[i] != goal)
1612  {
1613  v[i]->value = k;
1614  ++k;
1615  }
1616  }
1617 
1618  k = 0;
1619  for (i = 1; i < ntokens; ++i)
1620  {
1621  n = v[i]->value;
1622  if (n > 256)
1623  {
1624  for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
1625  symbol_value[j] = symbol_value[j-1];
1626  symbol_value[j] = n;
1627  }
1628  }
1629 
1630  if (v[1]->value == UNDEFINED)
1631  v[1]->value = 256;
1632 
1633  j = 0;
1634  n = 257;
1635  for (i = 2; i < ntokens; ++i)
1636  {
1637  if (v[i]->value == UNDEFINED)
1638  {
1639  while (j < k && n == symbol_value[j])
1640  {
1641  while (++j < k && n == symbol_value[j]) continue;
1642  ++n;
1643  }
1644  v[i]->value = n;
1645  ++n;
1646  }
1647  }
1648 
1649  symbol_name[0] = name_pool + 8;
1650  symbol_value[0] = 0;
1651  symbol_prec[0] = 0;
1652  symbol_assoc[0] = TOKEN;
1653  for (i = 1; i < ntokens; ++i)
1654  {
1655  symbol_name[i] = v[i]->name;
1656  symbol_value[i] = v[i]->value;
1657  symbol_prec[i] = v[i]->prec;
1658  symbol_assoc[i] = v[i]->assoc;
1659  }
1661  symbol_value[start_symbol] = -1;
1664  for (++i; i < nsyms; ++i)
1665  {
1666  k = v[i]->index;
1667  symbol_name[k] = v[i]->name;
1668  symbol_value[k] = v[i]->value;
1669  symbol_prec[k] = v[i]->prec;
1670  symbol_assoc[k] = v[i]->assoc;
1671  }
1672 
1673  FREE(v);
1674 }
char * name_pool
Definition: reader.c:32
#define TOKEN
Definition: defs.h:56
#define MALLOC(n)
Definition: defs.h:103
#define UNDEFINED
Definition: defs.h:77
char * name
Definition: defs.h:116
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
#define TERM
Definition: defs.h:71
short prec
Definition: defs.h:120
bucket * first_symbol
Definition: symtab.c:11
no_space()
Definition: error.c:27
short * symbol_prec
Definition: main.c:92
char class
Definition: defs.h:121
int nvars
The number of variables (non-terminals) in the grammar.
Definition: main.c:74
int nsyms
The number of symbols (terminals + non-terminals) in the grammar.
Definition: main.c:55
short index
Definition: defs.h:119
#define FREE(x)
Definition: defs.h:102
char * symbol_assoc
Definition: main.c:93
struct bucket * next
Definition: defs.h:115
int start_symbol
Index of the starting symbol of the grammar.
Definition: main.c:82
bucket * goal
Definition: reader.c:20
short value
Definition: defs.h:118
char ** symbol_name
Array of symbol names.
Definition: main.c:90
short * symbol_value
Definition: main.c:91
char assoc
Definition: defs.h:122
Definition: defs.h:112

+ Here is the call graph for this function:

+ Here is the caller graph for this function: