nctref/src/scope.h

106 lines
2.2 KiB
C

#ifndef NCTREF_VARTABLE_H
#define NCTREF_VARTABLE_H
#include"types.h"
#include<stdbool.h>
struct Token;
union AST;
typedef enum {
SCOPEITEM_SYMBOL, SCOPEITEM_VAR, SCOPEITEM_TYPE, SCOPEITEM_CEXPR
} ScopeItemKind;
union AST;
typedef struct UseDef {
union AST *def; // assign stmt
union AST *use; // corresponding AST_EXPR_VAR
union AST *stmt; // whole using stmt
struct UseDef *next;
} UseDef;
typedef struct ReachingDefs {
size_t defCount;
union AST **defs;
} ReachingDefs;
struct Scope;
typedef struct ScopeItem {
Type *type;
struct Scope *owner;
ScopeItemKind kind;
union {
struct {
char isLocal;
char isExternal;
const char *name;
struct {
struct Scope *scope;
struct Token *rangeTokens;
size_t startTokI;
size_t endTokI;
} genfunc;
} symbol;
struct {
// For debugging
const char *name;
// Register allocation
// vars in loops have higher priority
// a more advanced approach would be to use weights for different colors (e.g. multipliers "should" be in eax)
uint8_t priority;
int16_t color, degree;
bool precolored, preclassed;
int registerClass;
// Used by ast_usedef_pass
//ReachingDefs *reachingDefs;
union AST *declaration;
UseDef *usedefFirst;
UseDef *usedefLast;
union AST *liveRangeStart;
union AST *liveRangeEnd;
} var;
struct {
Type *ptr;
} type;
struct {
// cexpr is used for expression parametization as opposed to type parametrization
// I don't like the idea of having a special ScopeItem kind for these, but all other places were worse
const char *paramName;
size_t paramIdx;
// If the cexpr has been parametrized (as opposed to just being a symbol), this field will be non-NULL
union AST *concrete;
} cexpr;
} data;
} ScopeItem;
typedef struct Scope {
struct Scope *parent;
size_t count;
const char **names;
ScopeItem **data;
} Scope;
Scope *scope_new(Scope*);
ScopeItem *scope_get(Scope*, const char*);
ScopeItem *scope_find(Scope*, const char*);
ScopeItem *scope_set(Scope*, const char*, ScopeItem*);
ScopeItem *scope_find_int(Scope*, intmax_t, const char*);
Scope *scope_merge(Scope *child);
void vte_precolor(ScopeItem *vte, int resclass, int color);
#endif