ack/lang/m2/comp/enter.c

201 lines
4.5 KiB
C
Raw Normal View History

1986-03-26 17:53:13 +00:00
/* H I G H L E V E L S Y M B O L E N T R Y A N D L O O K U P */
1986-05-01 19:06:53 +00:00
#ifndef NORCSID
1986-03-26 17:53:13 +00:00
static char *RcsId = "$Header$";
1986-05-01 19:06:53 +00:00
#endif
#include "debug.h"
1986-03-26 17:53:13 +00:00
1986-03-26 22:46:48 +00:00
#include <alloc.h>
1986-03-26 17:53:13 +00:00
#include <em_arith.h>
#include <em_label.h>
1986-04-07 17:40:38 +00:00
#include <assert.h>
1986-05-01 19:06:53 +00:00
1986-03-26 17:53:13 +00:00
#include "idf.h"
#include "def.h"
#include "type.h"
#include "scope.h"
1986-04-06 17:42:56 +00:00
#include "LLlex.h"
#include "node.h"
1986-04-18 17:53:47 +00:00
#include "main.h"
1986-03-26 17:53:13 +00:00
1986-03-26 22:46:48 +00:00
struct def *
1986-03-26 17:53:13 +00:00
Enter(name, kind, type, pnam)
char *name;
struct type *type;
{
1986-04-07 17:40:38 +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.
*/
1986-03-26 17:53:13 +00:00
struct idf *id;
struct def *df;
id = str2idf(name, 0);
if (!id) fatal("Out of core");
df = define(id, CurrentScope, kind);
df->df_type = type;
1986-04-11 11:57:19 +00:00
if (type = std_type) {
1986-03-26 17:53:13 +00:00
df->df_value.df_stdname = pnam;
}
1986-03-26 22:46:48 +00:00
return df;
}
1986-04-15 17:51:53 +00:00
EnterIdList(idlist, kind, flags, type, scope, addr)
1986-04-06 17:42:56 +00:00
register struct node *idlist;
1986-03-26 22:46:48 +00:00
struct type *type;
1986-04-03 00:44:39 +00:00
struct scope *scope;
1986-04-15 17:51:53 +00:00
arith *addr;
1986-03-26 22:46:48 +00:00
{
1986-04-07 17:40:38 +00:00
/* Put a list of identifiers in the symbol table.
They all have kind "kind", and type "type", and are put
in scope "scope". "flags" initializes the "df_flags" field
of the definition structure.
Also assign numbers to enumeration literals, and link
them together.
*/
1986-03-26 22:46:48 +00:00
register struct def *df;
1986-03-27 17:37:41 +00:00
struct def *first = 0, *last = 0;
1986-03-26 22:46:48 +00:00
int assval = 0;
1986-04-15 17:51:53 +00:00
arith off;
1986-03-26 22:46:48 +00:00
while (idlist) {
1986-04-06 17:42:56 +00:00
df = define(idlist->nd_IDF, scope, kind);
1986-03-26 22:46:48 +00:00
df->df_type = type;
1986-04-11 11:57:19 +00:00
df->df_flags |= flags;
1986-04-15 17:51:53 +00:00
if (addr) {
if (*addr >= 0) {
off = align(*addr, type->tp_align);
*addr = off + type->tp_size;
}
else {
off = -align(-*addr, type->tp_align);
*addr = off - type->tp_size;
}
if (kind == D_VARIABLE) {
df->var_off = off;
}
else {
assert(kind == D_FIELD);
1986-04-22 22:36:16 +00:00
1986-04-15 17:51:53 +00:00
df->fld_off = off;
}
}
1986-03-26 22:46:48 +00:00
if (kind == D_ENUM) {
1986-03-27 17:37:41 +00:00
if (!first) first = df;
1986-04-07 17:40:38 +00:00
df->enm_val = assval++;
if (last) last->enm_next = df;
1986-03-26 22:46:48 +00:00
last = df;
}
idlist = idlist->next;
}
1986-03-27 17:37:41 +00:00
if (last) {
1986-04-07 17:40:38 +00:00
/* Also meaning : kind == D_ENUM */
assert(kind == D_ENUM);
last->enm_next = 0;
1986-03-29 01:04:49 +00:00
type->enm_enums = first;
type->enm_ncst = assval;
1986-03-27 17:37:41 +00:00
}
}
1986-04-15 17:51:53 +00:00
EnterVarList(IdList, type, local)
register struct node *IdList;
struct type *type;
{
1986-04-21 17:27:06 +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
1986-05-16 17:15:36 +00:00
procedure.
1986-04-21 17:27:06 +00:00
*/
1986-04-15 17:51:53 +00:00
register struct def *df;
1986-04-28 18:06:58 +00:00
register struct scopelist *sc;
1986-04-21 17:27:06 +00:00
char buf[256];
extern char *sprint(), *Malloc(), *strcpy();
1986-04-15 17:51:53 +00:00
1986-04-28 18:06:58 +00:00
sc = CurrVis;
1986-04-22 22:36:16 +00:00
1986-04-15 17:51:53 +00:00
if (local) {
/* Find the closest enclosing open scope. This
is the procedure that we are dealing with
*/
1986-04-28 18:06:58 +00:00
while (sc->sc_scope->sc_scopeclosed) sc = enclosing(sc);
1986-04-15 17:51:53 +00:00
}
while (IdList) {
df = define(IdList->nd_IDF, CurrentScope, D_VARIABLE);
df->df_type = type;
if (IdList->nd_left) {
1986-04-21 17:27:06 +00:00
/* An address was supplied
*/
1986-04-15 17:51:53 +00:00
df->var_addrgiven = 1;
if (IdList->nd_left->nd_type != card_type) {
node_error(IdList->nd_left,"Illegal type for address");
}
df->var_off = IdList->nd_left->nd_INT;
}
else if (local) {
1986-04-22 22:36:16 +00:00
/* subtract aligned size of variable to the offset,
as the variable list exists only local to a
procedure
1986-04-15 17:51:53 +00:00
*/
1986-04-28 18:06:58 +00:00
sc->sc_scope->sc_off =
-align(type->tp_size - sc->sc_scope->sc_off,
1986-04-22 22:36:16 +00:00
type->tp_align);
1986-04-28 18:06:58 +00:00
df->var_off = sc->sc_scope->sc_off;
1986-04-15 17:51:53 +00:00
}
1986-05-16 17:15:36 +00:00
else if (!DefinitionModule && CurrVis != Defined->mod_vis) {
1986-04-22 22:36:16 +00:00
/* variable list belongs to an internal global
1986-05-16 17:15:36 +00:00
module.
Align offset and add size
1986-04-22 22:36:16 +00:00
*/
1986-04-28 18:06:58 +00:00
sc->sc_scope->sc_off =
align(sc->sc_scope->sc_off, type->tp_align);
df->var_off = sc->sc_scope->sc_off;
1986-05-16 17:15:36 +00:00
df->var_name = 0;
1986-04-28 18:06:58 +00:00
sc->sc_scope->sc_off += type->tp_size;
1986-04-21 17:27:06 +00:00
}
else {
1986-04-22 22:36:16 +00:00
/* Global name, possibly external
*/
1986-04-28 18:06:58 +00:00
sprint(buf,"%s_%s", sc->sc_scope->sc_name,
1986-04-21 17:27:06 +00:00
df->df_idf->id_text);
df->var_name = Malloc((unsigned)(strlen(buf)+1));
strcpy(df->var_name, buf);
1986-05-16 17:15:36 +00:00
1986-04-21 17:27:06 +00:00
if (DefinitionModule) {
C_exa_dnam(df->var_name);
}
else {
C_ina_dnam(df->var_name);
}
1986-04-18 17:53:47 +00:00
}
1986-05-16 17:15:36 +00:00
1986-04-15 17:51:53 +00:00
IdList = IdList->nd_right;
}
}
1986-03-27 17:37:41 +00:00
struct def *
1986-04-28 18:06:58 +00:00
lookfor(id, vis, give_error)
1986-04-08 18:15:46 +00:00
struct node *id;
1986-04-28 18:06:58 +00:00
struct scopelist *vis;
1986-03-27 17:37:41 +00:00
{
1986-04-02 17:34:21 +00:00
/* Look for an identifier in the visibility range started by
1986-04-28 18:06:58 +00:00
"vis".
1986-04-18 17:53:47 +00:00
If it is not defined, maybe give an error message, and
1986-04-02 17:34:21 +00:00
create a dummy definition.
*/
1986-03-27 17:37:41 +00:00
struct def *df;
1986-04-28 18:06:58 +00:00
register struct scopelist *sc = vis;
1986-04-18 17:53:47 +00:00
struct def *MkDef();
1986-03-27 17:37:41 +00:00
while (sc) {
1986-04-28 18:06:58 +00:00
df = lookup(id->nd_IDF, sc->sc_scope);
1986-03-27 17:37:41 +00:00
if (df) return df;
sc = nextvisible(sc);
}
1986-04-18 17:53:47 +00:00
1986-04-03 17:41:26 +00:00
if (give_error) id_not_declared(id);
1986-04-18 17:53:47 +00:00
1986-04-28 18:06:58 +00:00
return MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
1986-03-26 17:53:13 +00:00
}