ack/lang/occam/comp/symtab.c

209 lines
4.7 KiB
C
Raw Permalink Normal View History

1994-06-24 14:02:31 +00:00
/* $Id$ */
1987-03-09 16:02:32 +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".
*/
1987-02-24 17:05:53 +00:00
#include "symtab.h"
#include "expr.h"
#include "sizes.h"
int curr_level=0; /* Current local level */
int curr_offset=0; /* Current offset within this level */
int min_offset=0; /* Minimum of all offsets within current level */
static struct symtab *sym_table=nil;
char *Malloc();
1987-02-24 17:05:53 +00:00
static struct symbol **search_sym(tree, name)
struct symbol **tree;
char *name;
/* Returns a hook in the tree to the where the given name is or should be. */
{
register struct symbol **aps=tree, *ps;
register cmp;
1987-03-18 09:29:56 +00:00
while ((ps= *aps)!=nil && (cmp=strcmp(name, ps->s_name))!=0)
aps= cmp<0 ? &ps->s_left : &ps->s_right;
1987-02-24 17:05:53 +00:00
return aps;
}
struct symbol *insert(name, type, arr_siz, info)
char *name;
int type, arr_siz;
1987-02-25 16:56:31 +00:00
union type_info *info;
1987-02-24 17:05:53 +00:00
/* Inserts an object with given name and other info into the current symbol
* tree. A pointer is returned to the inserted symbol so that more info may
* or changed. Nil is returned on redeclaration.
*/
{
register struct symbol **aps, *ps;
extern included;
if (*(aps=search_sym(&sym_table->local, name))!=nil) {
report("%s redeclared", name);
return nil;
}
ps= (struct symbol *) Malloc(sizeof *ps);
1987-02-24 17:05:53 +00:00
1987-03-18 09:29:56 +00:00
ps->s_name=name;
1987-02-24 17:05:53 +00:00
if (included && curr_level==0) /* Top_level symbol in include file */
type|=T_USED; /* are always used */
1987-03-18 09:29:56 +00:00
ps->s_type=type;
ps->s_arr_siz=arr_siz;
ps->s_info= *info;
ps->s_left=ps->s_right=nil;
1987-02-24 17:05:53 +00:00
*aps=ps;
return ps;
}
struct symbol *searchall(name) char *name;
/* Searches for name in all symbol trees from the inner to the outermost.
* If it can't be found then it is inserted as undefined.
*/
{
register struct symtab *tab=sym_table;
register struct symbol *ps;
while (tab!=nil) {
if ((ps= *search_sym(&tab->local, name))!=nil) return ps;
tab=tab->global;
}
report("%s not declared", name);
1987-02-25 16:56:31 +00:00
return insert(name, T_NOTDECL, 0, &none);
1987-02-24 17:05:53 +00:00
}
void check_recursion(proc)
register struct expr *proc;
{
1987-03-18 09:29:56 +00:00
if (proc->kind==E_VAR && proc->u.var->s_type&T_RECURS)
1987-02-24 17:05:53 +00:00
warning("recursion not allowed");
}
void sym_down()
{
register struct symtab *ps;
ps= (struct symtab *) Malloc(sizeof *ps);
1987-02-24 17:05:53 +00:00
ps->local=nil;
ps->global=sym_table;
ps->old_offset=curr_offset;
sym_table=ps;
}
static void sym_destroy(ps) register struct symbol *ps;
{
if (ps!=nil) {
1987-03-18 09:29:56 +00:00
sym_destroy(ps->s_left);
sym_destroy(ps->s_right);
if ( !(ps->s_type&T_NOTDECL) ) {
if ( !(ps->s_type&T_USED) )
warning("%s: never used", ps->s_name);
1987-02-24 17:05:53 +00:00
else
1987-03-18 09:29:56 +00:00
if ( !(ps->s_type&T_ASSIGNED) && (ps->s_type&T_TYPE)==T_VAR)
warning("%s: never assigned", ps->s_name);
1987-02-24 17:05:53 +00:00
}
1987-03-18 09:29:56 +00:00
if ((ps->s_type&T_TYPE)==T_PROC) {
1987-02-24 17:05:53 +00:00
register struct par_list *par, *junk;
1987-03-18 09:29:56 +00:00
par=ps->s_info.proc.pars;
1987-02-24 17:05:53 +00:00
while (par!=nil) {
junk=par;
1987-03-18 09:29:56 +00:00
par=par->pr_next;
free((char *)junk);
1987-02-24 17:05:53 +00:00
}
} else
1987-03-18 09:29:56 +00:00
if ((ps->s_type&T_TYPE)==T_CONST)
1987-03-23 23:27:03 +00:00
destroy(ps->s_info.t_const);
free((char *)(ps->s_name));
free((char *)ps);
1987-02-24 17:05:53 +00:00
}
}
void sym_up()
{
register struct symtab *ps;
ps=sym_table->global;
curr_offset=sym_table->old_offset;
sym_destroy(sym_table->local);
free(sym_table);
sym_table=ps;
}
void var_memory(info, type, n) register union type_info *info; int type, n;
/* Reserves local memory for an object, and stores it in its info field. */
{
info->vc.st.level=curr_level;
curr_offset-= (type&T_BYTE) ? (n+wz-1) & (~(wz-1)) : n*vz;
info->vc.offset=curr_offset;
if (curr_offset<min_offset) min_offset=curr_offset;
}
void chan_memory(info, n) register union type_info *info; int n;
{
info->vc.st.level=curr_level;
info->vc.offset= curr_offset-=n*(vz+wz);
if (curr_offset<min_offset) min_offset=curr_offset;
}
int memory(z) int z;
/* Reserves z memory bytes */
{
curr_offset-=z;
if (curr_offset<min_offset) min_offset=curr_offset;
return curr_offset;
}
void pars_add(aapars, type, var)
register struct par_list ***aapars;
int type;
struct symbol *var;
/* Add a formal variable to a parameter list using a hook to a hook. */
{
register struct par_list *pl;
pl= (struct par_list *) Malloc(sizeof *pl);
1987-02-24 17:05:53 +00:00
1987-03-18 09:29:56 +00:00
pl->pr_type=type;
pl->pr_var=var;
pl->pr_next= **aapars;
1987-02-24 17:05:53 +00:00
**aapars=pl;
1987-03-18 09:29:56 +00:00
*aapars= &pl->pr_next;
1987-02-24 17:05:53 +00:00
}
int form_offsets(pars) register struct par_list *pars;
/* Recursively assign offsets to formal variables. */
{
register struct symbol *var;
if (pars==nil) return pz;
1987-03-18 09:29:56 +00:00
if ((var=pars->pr_var)!=nil) {
register offset=form_offsets(pars->pr_next);
1987-02-24 17:05:53 +00:00
1987-03-18 09:29:56 +00:00
switch (var->s_type&T_TYPE) {
1987-02-24 17:05:53 +00:00
case T_VAR:
case T_CHAN:
1987-03-18 09:29:56 +00:00
var->s_info.vc.st.level=curr_level;
var->s_info.vc.offset=offset;
1987-02-24 17:05:53 +00:00
return offset+pz;
case T_VALUE:
1987-03-18 09:29:56 +00:00
var->s_info.vc.st.level=curr_level;
var->s_info.vc.offset=offset;
return offset+ ((var->s_type&T_ARR) ? pz : vz);
1987-02-24 17:05:53 +00:00
}
}
return pz;
1987-02-24 17:05:53 +00:00
}