ack/lang/pc/comp/enter.c

278 lines
6.1 KiB
C
Raw Normal View History

1988-10-26 15:21:11 +00:00
/* H I G H L E V E L S Y M B O L E N T R Y */
#include <string.h>
1988-10-26 15:21:11 +00:00
#include <alloc.h>
#include <assert.h>
#include <em_arith.h>
#include <em_label.h>
#include "parameters.h"
1988-10-26 15:21:11 +00:00
#include "LLlex.h"
#include "def.h"
#include "idf.h"
#include "main.h"
#include "node.h"
#include "scope.h"
#include "type.h"
#include "progs.h"
#include "enter.h"
#ifdef DBSYMTAB
#include "stab.h"
#endif
extern int proclevel;
extern int parlevel;
1988-10-26 15:21:11 +00:00
struct def *Enter(char *name, long kind, register struct type *type, int pnam)
1988-10-26 15:21:11 +00:00
{
/* Enter a definition for "name" with kind "kind" and type
"type" in the Current Scope. If it is a standard name, also
put its number in the definition structure, and mark the
name as set, to inhibit warnings about used before set.
*/
1988-10-26 15:21:11 +00:00
register struct def *df;
df = define(str2idf(name, 0), CurrentScope, kind);
df->df_type = type;
if (pnam)
{
1989-05-03 10:30:22 +00:00
df->df_value.df_reqname = pnam;
df->df_flags |= D_SET;
}
#ifdef DBSYMTAB
else if (options['g'])
stb_string(df, kind);
#endif /* DBSYMTAB */
1988-10-26 15:21:11 +00:00
return df;
}
void EnterProgList(register struct node *Idlist)
1988-10-26 15:21:11 +00:00
{
register struct node *idlist = Idlist;
register struct def *df;
for (; idlist; idlist = idlist->nd_next)
if (!strcmp(input, idlist->nd_IDF->id_text)
|| !strcmp(output, idlist->nd_IDF->id_text))
{
1988-10-26 15:21:11 +00:00
/* the occurence of input or output as program-
1989-05-03 10:30:22 +00:00
* parameter is their declaration as a GLOBAL
* variable of type text
1988-10-26 15:21:11 +00:00
*/
if ( (df = define(idlist->nd_IDF, CurrentScope,
D_VARIABLE)) )
{
1988-10-26 15:21:11 +00:00
df->df_type = text_type;
1989-05-03 10:30:22 +00:00
df->df_flags |= (D_SET | D_PROGPAR | D_NOREG);
if (!strcmp(input, idlist->nd_IDF->id_text))
{
1988-10-26 15:21:11 +00:00
df->var_name = input;
set_inp();
1988-10-26 15:21:11 +00:00
}
else
{
1988-10-26 15:21:11 +00:00
df->var_name = output;
set_outp();
1988-10-26 15:21:11 +00:00
}
#ifdef DBSYMTAB
if (options['g'])
stb_string(df, D_VARIABLE);
#endif /* DBSYMTAB */
1988-10-26 15:21:11 +00:00
}
}
else
{
if ( (df = define(idlist->nd_IDF, CurrentScope,
D_PARAMETER)) )
{
1988-10-26 15:21:11 +00:00
df->df_type = error_type;
df->df_flags |= D_PROGPAR;
1989-05-03 10:30:22 +00:00
df->var_name = idlist->nd_IDF->id_text;
1988-10-26 15:21:11 +00:00
}
}
1988-10-26 15:21:11 +00:00
FreeNode(Idlist);
}
void EnterEnumList(struct node *Idlist, register struct type *type)
1988-10-26 15:21:11 +00:00
{
/* Put a list of enumeration literals in the symbol table.
They all have type "type". Also assign numbers to them.
*/
1990-12-11 13:52:08 +00:00
register struct def *df, *df1 = 0;
1988-10-26 15:21:11 +00:00
register struct node *idlist = Idlist;
type->enm_ncst = 0;
for (; idlist; idlist = idlist->nd_next)
if ( (df = define(idlist->nd_IDF, CurrentScope, D_ENUM)) )
{
1988-10-26 15:21:11 +00:00
df->df_type = type;
df->enm_val = (type->enm_ncst)++;
1989-05-03 10:30:22 +00:00
df->df_flags |= D_SET;
if (!df1)
{
1990-12-11 13:52:08 +00:00
type->enm_enums = df;
}
else
df1->enm_next = df;
1990-12-11 13:52:08 +00:00
df1 = df;
1988-10-26 15:21:11 +00:00
}
FreeNode(Idlist);
}
void EnterFieldList(struct node *Idlist, register struct type *type,
struct scope *scope, arith *addr, unsigned short packed)
1988-10-26 15:21:11 +00:00
{
/* Put a list of fields in the symbol table.
They all have type "type", and are put in scope "scope".
*/
1988-10-26 15:21:11 +00:00
register struct def *df;
register struct node *idlist = Idlist;
for (; idlist; idlist = idlist->nd_next)
if ( (df = define(idlist->nd_IDF, scope, D_FIELD)) )
{
1988-10-26 15:21:11 +00:00
df->df_type = type;
if (packed)
{
1988-10-26 15:21:11 +00:00
df->fld_flags |= F_PACKED;
df->fld_off = align(*addr, type->tp_palign);
*addr = df->fld_off + type->tp_psize;
}
else
{
1988-10-26 15:21:11 +00:00
df->fld_off = align(*addr, type->tp_align);
*addr = df->fld_off + type->tp_size;
}
}
FreeNode(Idlist);
}
void EnterVarList(struct node *Idlist, struct type *type, int local)
1988-10-26 15:21:11 +00:00
{
/* Enter a list of identifiers representing variables into the
name list. "type" represents the type of the variables.
"local" is set if the variables are declared local to a
procedure.
*/
1988-10-26 15:21:11 +00:00
register struct def *df;
register struct node *idlist = Idlist;
register struct scopelist *sc = CurrVis;
for (; idlist; idlist = idlist->nd_next)
{
if (!(df = define(idlist->nd_IDF, CurrentScope, D_VARIABLE)))
continue; /* skip this identifier */
1988-10-26 15:21:11 +00:00
df->df_type = type;
if (local)
{
1988-10-26 15:21:11 +00:00
/* subtract size, which is already aligned, of
* variable to the offset, as the variable list
* exists only local to a procedure
*/
sc->sc_scope->sc_off -= type->tp_size;
df->var_off = sc->sc_scope->sc_off;
}
else
{ /* Global name */
1988-10-26 15:21:11 +00:00
df->var_name = df->df_idf->id_text;
df->df_flags |= D_NOREG;
}
#ifdef DBSYMTAB
if (options['g'])
stb_string(df, D_VARIABLE);
#endif /* DBSYMTAB */
1988-10-26 15:21:11 +00:00
}
FreeNode(Idlist);
}
static void LinkParam(struct paramlist **parlist, struct def *df)
{
static struct paramlist *pr;
if (!*parlist)
*parlist = pr = new_paramlist();
else
{
pr->next = new_paramlist();
pr = pr->next;
}
pr->par_def = df;
}
arith EnterParamList(register struct node *fpl, struct paramlist **parlist)
1988-10-26 15:21:11 +00:00
{
register arith nb_pars = (proclevel > 1) ? pointer_size : 0;
register struct node *id;
struct type *tp;
struct def *df;
for (; fpl; fpl = fpl->nd_right)
{
1988-10-26 15:21:11 +00:00
assert(fpl->nd_class == Link);
tp = fpl->nd_type;
for (id = fpl->nd_left; id; id = id->nd_next)
if ( (df = define(id->nd_IDF, CurrentScope, D_VARIABLE)) )
{
df->var_off = nb_pars;
if (fpl->nd_INT & D_VARPAR || IsConformantArray(tp))
nb_pars += pointer_size;
else
nb_pars += tp->tp_size;
LinkParam(parlist, df);
df->df_type = tp;
df->df_flags |= fpl->nd_INT;
}
1988-10-26 15:21:11 +00:00
while (IsConformantArray(tp))
{
1988-10-26 15:21:11 +00:00
/* we need room for the descriptors */
tp->arr_sclevel = CurrentScope->sc_level;
tp->arr_cfdescr = nb_pars;
nb_pars += 3 * word_size;
tp = tp->arr_elem;
}
1988-10-26 15:21:11 +00:00
}
return nb_pars;
}
arith EnterParTypes(register struct node *fpl, struct paramlist **parlist)
1988-10-26 15:21:11 +00:00
{
/* parameters.h in heading of procedural and functional
parameters (only types are important, not the names).
*/
1989-05-03 10:30:22 +00:00
register arith nb_pars = 0;
1988-10-26 15:21:11 +00:00
register struct node *id;
1989-05-03 10:30:22 +00:00
struct type *tp;
1988-10-26 15:21:11 +00:00
struct def *df;
for (; fpl; fpl = fpl->nd_right)
{
1989-05-03 10:30:22 +00:00
tp = fpl->nd_type;
for (id = fpl->nd_left; id; id = id->nd_next)
if ( (df = new_def()) )
{
if (fpl->nd_INT & D_VARPAR || IsConformantArray(tp))
1989-05-03 10:30:22 +00:00
nb_pars += pointer_size;
else
nb_pars += tp->tp_size;
1988-10-26 15:21:11 +00:00
LinkParam(parlist, df);
1989-05-03 10:30:22 +00:00
df->df_type = tp;
1988-10-26 15:21:11 +00:00
df->df_flags |= fpl->nd_INT;
}
while (IsConformantArray(tp))
{
1989-05-03 10:30:22 +00:00
nb_pars += 3 * word_size;
tp = tp->arr_elem;
}
}
return nb_pars;
1988-10-26 15:21:11 +00:00
}