jack-compiler/compiler/compiler-scopes.c

318 lines
8.1 KiB
C
Raw Normal View History

2020-12-21 13:05:49 -05:00
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
2020-12-31 11:39:07 -05:00
#include <pthread.h>
#include "compiler.h"
2020-12-21 13:05:49 -05:00
#include "compiler-scopes.h"
#include "os.h"
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
typedef enum { local, staticseg, arg, fieldseg } MEMSEGMENT;
char* memsegnames[] = { "local", "static", "argument", "this" };
2020-12-21 13:05:49 -05:00
// Error messages
2020-12-27 16:52:28 -05:00
void doubledeclaration(const char* name, DEBUGINFO* d1, DEBUGINFO* d2);
2021-01-09 13:31:59 -05:00
void ensurenoduplicate(SCOPE* s, DEBUGINFO* d, char* name);
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
// Getters
VAR* getvarinvars(VAR* vars, const char* name);
CLASS* getclass(SCOPE* s, const char* name);
SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name);
SUBROUTDEC* getmethod(SCOPE* s, VAR* parent, SUBROUTCALL* call);
SUBROUTDEC* getfunction(SCOPE* s, SUBROUTCALL* call);
SUBROUTDEC* getsubroutdecwithparent(SCOPE* s, SUBROUTCALL* call, VAR** varret);
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdecwithoutparent(SCOPE* s, SUBROUTCALL* call);
SUBROUTDEC* getsubroutdec(SCOPE* s, const char* name);
2020-12-21 13:05:49 -05:00
// Scope adding
2020-12-31 11:39:07 -05:00
VAR* mkvar(char* type, char* name, bool primitive, DEBUGINFO* debug, MEMSEGMENT seg, int i);
2020-12-27 16:52:28 -05:00
void addvar(SCOPE* s, VAR** dest, VAR* v);
2020-12-31 14:49:38 -05:00
void addlocalvar(SCOPE* s, VARDEC* v, int* i);
2021-01-03 14:08:54 -05:00
void addstaticvar(SCOPE* s, CLASSVARDEC* v);
2020-12-31 14:49:38 -05:00
void addfield(SCOPE* s, CLASSVARDEC* v, int* i);
2021-01-03 14:08:54 -05:00
void addclassvardec(SCOPE* s, CLASSVARDEC* v, int* i);
2020-12-31 14:49:38 -05:00
void addparameter(SCOPE* s, PARAMETER* p, int* i);
2020-12-21 13:05:49 -05:00
// Error messages
2020-12-27 16:52:28 -05:00
void doubledeclaration(const char* name, DEBUGINFO* d1, DEBUGINFO* d2) {
2020-12-21 16:11:23 -05:00
eprintf("Double declaration of '%s' at '%s', line %i; previously defined at '%s', line %i\n",
2020-12-27 16:52:28 -05:00
name, d1->file, d1->definedat, d2->file, d2->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
2020-12-21 19:50:55 -05:00
void notdeclared(const char* name, DEBUGINFO* debug) {
2020-12-21 16:11:23 -05:00
eprintf("'%s' not declared; file '%s', line %i\n", name, debug->file, debug->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
void invalidparent(SUBROUTCALL* call) {
2020-12-21 16:11:23 -05:00
eprintf("Invalid subroutine parent '%s'; file '%s', line %i\n", call->parentname, call->debug->file, call->debug->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
2021-01-09 13:31:59 -05:00
void ensurenoduplicate(SCOPE* s, DEBUGINFO* d, char* name) {
2020-12-27 16:52:28 -05:00
VAR* v = getvar(s, name);
if(v != NULL)
2021-01-09 13:31:59 -05:00
doubledeclaration(name, d, v->debug);
2020-12-27 16:52:28 -05:00
CLASS* c = getclass(s, name);
if(c != NULL)
2021-01-09 13:31:59 -05:00
doubledeclaration(name, d, c->debug);
2020-12-27 16:52:28 -05:00
SUBROUTDEC* sr = getsubroutdec(s, name);
if(sr != NULL)
2021-01-09 13:31:59 -05:00
doubledeclaration(name, d, sr->debug);
2020-12-21 13:05:49 -05:00
}
// Scope handling
SCOPE* mkscope(SCOPE* prev) {
SCOPE* s = (SCOPE*)malloc(sizeof(SCOPE));
s->previous = prev;
2021-01-03 14:08:54 -05:00
if(prev != NULL)
s->compiler = prev->compiler;
2020-12-27 16:52:28 -05:00
s->localvars = NULL;
s->fields = NULL;
s->staticvars = NULL;
s->parameters = NULL;
s->classes = NULL;
s->subroutines = NULL;
2020-12-21 13:05:49 -05:00
return s;
}
2020-12-27 16:52:28 -05:00
// Getters
VAR* getvarinvars(VAR* vars, const char* name) {
while(vars != NULL) {
if(!strcmp(vars->name, name))
return vars;
vars = vars->next;
}
return NULL;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
VAR* getvar(SCOPE* s, const char* name) {
VAR* var = getvarinvars(s->localvars, name);
if(var != NULL)
return var;
var = getvarinvars(s->parameters, name);
if(var != NULL)
return var;
var = getvarinvars(s->fields, name);
if(var != NULL)
return var;
var = getvarinvars(s->staticvars, name);
if(var != NULL)
return var;
if(s->previous != NULL)
return getvar(s->previous, name);
return NULL;
2020-12-21 13:05:49 -05:00
}
2021-01-06 19:18:52 -05:00
VAR* getvarmustexist(SCOPE* s, DEBUGINFO* d, const char* name) {
VAR* v = getvar(s, name);
if(v == NULL)
notdeclared(name, d);
return v;
}
2020-12-27 16:52:28 -05:00
CLASS* getclass(SCOPE* s, const char* name) {
CLASS* curr = s->classes;
2020-12-21 13:05:49 -05:00
while(curr != NULL) {
if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
2020-12-27 16:52:28 -05:00
if(s->previous != NULL)
return getclass(s->previous, name);
2021-01-03 14:08:54 -05:00
return getosclass(s->compiler->os, name);
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name) {
while(start != NULL) {
if(!strcmp(start->name, name))
return start;
start = start->next;
}
return NULL;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getmethod(SCOPE* s, VAR* parent, SUBROUTCALL* call) {
CLASS* c = getclass(s, parent->type);
SUBROUTDEC* d = getsubroutdecfromlist(c->subroutdecs, call->name);
if(d == NULL)
return NULL;
2020-12-30 18:47:11 -05:00
if(d->subroutclass != method) {
eprintf("Calling a function/constructor as if it were a method; file '%s', line %i\n", call->debug->file, call->debug->definedat);
2020-12-27 16:52:28 -05:00
exit(1);
}
return d;
}
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getfunction(SCOPE* s, SUBROUTCALL* call) {
CLASS* c = getclass(s, call->parentname);
if(c == NULL)
2020-12-21 13:05:49 -05:00
notdeclared(call->parentname, call->debug);
2020-12-27 16:52:28 -05:00
SUBROUTDEC* d = getsubroutdecfromlist(c->subroutdecs, call->name);
if(d == NULL)
return NULL;
2020-12-27 16:52:28 -05:00
if(d->subroutclass == method) {
eprintf("Calling a method as if it were a function; file '%s', line %i\n", call->debug->file, call->debug->definedat);
exit(1);
}
return d;
}
2020-12-21 13:05:49 -05:00
SUBROUTDEC* getsubroutdecwithparent(SCOPE* s, SUBROUTCALL* call, VAR** varret) {
2020-12-27 16:52:28 -05:00
VAR* parent = getvar(s, call->parentname);
if(parent != NULL) {
if(parent->primitive) {
eprintf("Primitive type does not have subroutines; file '%s', line %i\n", call->debug->file, call->debug->definedat);
exit(1);
}
*varret = parent;
2020-12-27 16:52:28 -05:00
return getmethod(s, parent, call);
}
else
2020-12-27 16:52:28 -05:00
return getfunction(s, call);
}
SUBROUTDEC* getsubroutdecwithoutparent(SCOPE* s, SUBROUTCALL* call) {
SUBROUTDEC* d = getsubroutdecfromlist(s->currclass->subroutdecs, call->name);
return d;
2020-12-21 13:05:49 -05:00
}
SUBROUTDEC* getsubroutdecfromcall(SCOPE* s, SUBROUTCALL* call, VAR** varret) {
SUBROUTDEC* d;
*varret = NULL;
if(call->parentname != NULL) {
2021-01-03 14:08:54 -05:00
d = getossubroutdec(s->compiler->os, call);
if(d == NULL)
d = getsubroutdecwithparent(s, call, varret);
}
else {
d = getsubroutdecwithoutparent(s, call);
}
if(d == NULL)
notdeclared(call->name, call->debug);
return d;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdec(SCOPE* s, const char* name) {
SUBROUTDEC* curr = s->subroutines;
2020-12-24 14:22:22 -05:00
while(curr != NULL) {
2020-12-27 16:52:28 -05:00
if(!strcmp(curr->name, name))
return curr;
2020-12-24 14:22:22 -05:00
curr = curr->next;
2020-12-21 13:05:49 -05:00
}
if(s->previous != NULL)
2020-12-27 16:52:28 -05:00
return getsubroutdec(s->previous, name);
2020-12-21 13:05:49 -05:00
return NULL;
}
2020-12-27 16:52:28 -05:00
// Scope adding
2020-12-31 11:39:07 -05:00
VAR* mkvar(char* type, char* name, bool primitive, DEBUGINFO* debug, MEMSEGMENT seg, int i) {
2020-12-27 16:52:28 -05:00
VAR* v = (VAR*)malloc(sizeof(VAR));
v->name = name;
v->type = type;
v->debug = debug;
v->memsegment = memsegnames[seg];
v->primitive = primitive;
2020-12-31 11:39:07 -05:00
v->index = i;
2020-12-27 16:52:28 -05:00
return v;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addvar(SCOPE* s, VAR** dest, VAR* v) {
2021-01-09 13:31:59 -05:00
ensurenoduplicate(s, v->debug, v->name);
2020-12-27 16:52:28 -05:00
if(!v->primitive) {
CLASS* type = getclass(s, v->type);
if(type == NULL)
notdeclared(v->type, v->debug);
}
v->next = *dest;
*dest = v;
2020-12-21 13:05:49 -05:00
}
2020-12-31 14:49:38 -05:00
void addlocalvar(SCOPE* s, VARDEC* v, int* i) {
2020-12-27 16:52:28 -05:00
STRINGLIST* currname = v->names;
while(currname != NULL) {
2020-12-31 14:49:38 -05:00
addvar(s, &(s->localvars), mkvar(v->type, currname->content, v->primitive, v->debug, local, *i));
2020-12-27 16:52:28 -05:00
currname = currname->next;
2020-12-31 14:49:38 -05:00
(*i)++;
2020-12-27 16:52:28 -05:00
}
2020-12-21 13:05:49 -05:00
}
2021-01-03 14:08:54 -05:00
void addstaticvar(SCOPE* s, CLASSVARDEC* v) {
2020-12-27 16:52:28 -05:00
STRINGLIST* currname = v->base->names;
2021-01-03 14:08:54 -05:00
pthread_mutex_lock(&(s->compiler->staticmutex));
2020-12-31 14:49:38 -05:00
static int i = 0;
2020-12-27 16:52:28 -05:00
while(currname != NULL) {
2020-12-31 11:39:07 -05:00
addvar(s, &(s->staticvars), mkvar(v->base->type, currname->content, v->base->primitive, v->base->debug, staticseg, i));
2020-12-27 16:52:28 -05:00
currname = currname->next;
2020-12-31 14:49:38 -05:00
i++;
2020-12-27 16:52:28 -05:00
}
2021-01-03 14:08:54 -05:00
pthread_mutex_unlock(&(s->compiler->staticmutex));
2020-12-21 13:05:49 -05:00
}
2020-12-31 14:49:38 -05:00
void addfield(SCOPE* s, CLASSVARDEC* v, int* i) {
2020-12-27 16:52:28 -05:00
STRINGLIST* currname = v->base->names;
while(currname != NULL) {
2020-12-31 14:49:38 -05:00
addvar(s, &(s->fields), mkvar(v->base->type, currname->content, v->base->primitive, v->base->debug, fieldseg, *i));
2020-12-27 16:52:28 -05:00
currname = currname->next;
2020-12-31 14:49:38 -05:00
(*i)++;
2020-12-27 16:52:28 -05:00
}
2020-12-21 13:05:49 -05:00
}
2021-01-03 14:08:54 -05:00
void addclassvardec(SCOPE* s, CLASSVARDEC* v, int* i) {
if(v->type == stat)
2021-01-03 14:08:54 -05:00
addstaticvar(s, v);
2020-12-31 11:39:07 -05:00
else {
2020-12-31 14:49:38 -05:00
addfield(s, v, i);
2020-12-31 11:39:07 -05:00
}
2020-12-21 13:05:49 -05:00
}
2020-12-31 14:49:38 -05:00
void addparameter(SCOPE* s, PARAMETER* p, int* i) {
addvar(s, &(s->parameters), mkvar(p->type, p->name, p->primitive, p->debug, arg, *i));
(*i)++;
2020-12-24 14:22:22 -05:00
}
2020-12-27 16:52:28 -05:00
// Group adding
2021-01-03 14:08:54 -05:00
void addclassvardecs(SCOPE* s, CLASSVARDEC* classvardecs) {
2020-12-31 11:39:07 -05:00
int i = 0;
2020-12-27 16:52:28 -05:00
while(classvardecs != NULL) {
2021-01-03 14:08:54 -05:00
addclassvardec(s, classvardecs, &i);
2020-12-27 16:52:28 -05:00
classvardecs = classvardecs->next;
}
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addlocalvars(SCOPE* s, VARDEC* localvars) {
2020-12-31 11:39:07 -05:00
int i = 0;
2020-12-27 16:52:28 -05:00
while(localvars != NULL) {
2020-12-31 14:49:38 -05:00
addlocalvar(s, localvars, &i);
2020-12-27 16:52:28 -05:00
localvars = localvars->next;
}
2020-12-24 14:22:22 -05:00
}
2020-12-31 15:38:41 -05:00
void addparameters(SCOPE* s, bool isformethod, PARAMETER* params) {
int i = isformethod ? 1 : 0;
2020-12-27 16:52:28 -05:00
while(params != NULL) {
2020-12-31 14:49:38 -05:00
addparameter(s, params, &i);
2020-12-27 16:52:28 -05:00
params = params->next;
}
2020-12-21 13:05:49 -05:00
}
2021-01-03 14:08:54 -05:00
void freevars(VAR* v) {
if(v != NULL) {
VAR* next = v->next;
free(v);
freevars(next);
}
}
void freescope(SCOPE* s) {
freevars(s->fields);
freevars(s->staticvars);
freevars(s->localvars);
freevars(s->parameters);
free(s);
};