aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/cc/cc.h
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/cc/cc.h')
-rw-r--r--sys/cmd/cc/cc.h259
1 files changed, 183 insertions, 76 deletions
diff --git a/sys/cmd/cc/cc.h b/sys/cmd/cc/cc.h
index 144eb4a..7c9d679 100644
--- a/sys/cmd/cc/cc.h
+++ b/sys/cmd/cc/cc.h
@@ -15,14 +15,18 @@ typedef struct Lexer Lexer;
typedef struct Sym Sym;
typedef struct Type Type;
+typedef struct Scope Scope;
+typedef struct Parser Parser;
+
+typedef struct Node Node;
typedef struct Ptr Ptr;
typedef struct Name Name;
+typedef struct Dtor Dtor;
typedef struct Decl Decl;
typedef struct Stmt Stmt;
typedef struct Expr Expr;
-/* maps */
typedef struct SymTab SymTab;
typedef struct StrTab StrTab;
@@ -34,21 +38,26 @@ typedef struct Compiler Compiler;
KEYWORD(Kregister,"register") \
KEYWORD(Kstatic,"static") \
KEYWORD(Kextern,"extern") \
+ KEYWORD(Ktls,"thread_local") \
KEYWORD(Ktypedef,"typedef") \
KEYWORD(Kconst,"const") \
KEYWORD(Kvolatile,"volatile") \
KEYWORD(Krestrict,"restrict") \
+ KEYWORD(Katomic,"_Atomic") \
KEYWORD(Kinline,"inline") \
+ KEYWORD(Knoret,"_Noreturn") \
+ KEYWORD(Ksigned,"signed") \
+ KEYWORD(Kunsigned,"unsigned") \
KEYWORD(Kvoid,"void") \
+ KEYWORD(Kbool,"_Bool") \
KEYWORD(Kchar,"char") \
- KEYWORD(Kint,"int") \
KEYWORD(Kfloat,"float") \
KEYWORD(Kdouble,"double") \
- KEYWORD(Ksigned,"signed") \
- KEYWORD(Kunsigned,"unsigned") \
+ KEYWORD(Kcomplex,"complex") \
+ KEYWORD(Kimaginary,"imaginary") \
+ KEYWORD(Kint,"int") \
KEYWORD(Kshort,"short") \
KEYWORD(Klong,"long") \
- KEYWORD(Kvlong,"vlong") \
KEYWORD(Kstruct,"struct") \
KEYWORD(Kunion,"union") \
KEYWORD(Kenum,"enum") \
@@ -201,7 +210,7 @@ extern byte *tokens[NUM_TOKENS];
struct Token
{
uint32 kind;
- Range pos;
+ Pos pos;
union {
byte *s;
double f;
@@ -215,9 +224,12 @@ struct Token
enum
{
- Svar = 1 << 0,
- Sfunc = 1 << 1,
- Smacro = 1 << 2,
+ Sobj = iota(0),
+ Stype = iota(2),
+ Stag = iota(3),
+ Senum = iota(4),
+ Sstmt = iota(5),
+ Smacro = iota(6),
};
struct Sym
@@ -226,7 +238,9 @@ struct Sym
string name;
union {
string macro;
- /*Func *func;*/
+ Decl *obj;
+ Type *tag;
+ Stmt *blk;
};
};
@@ -244,6 +258,7 @@ struct SymTab
Sym *define(SymTab *tab, string ident, int kind);
Sym *lookup(SymTab *tab, string ident);
error forget(SymTab *tab, string ident);
+void forgetall(SymTab *tab);
struct Lexer
{
@@ -276,6 +291,7 @@ void puttok(Token);
// parsing & type resolution
// tokens -> ast
+/* parent data */
struct Node
{
Range pos;
@@ -287,9 +303,9 @@ enum
{
Nbad,
/* labels */
- Slabel, Scase,
+ Sempty, Slabel, Scase,
Sblock,
- Sexpr,
+ Sexpr, Sdecl,
Sselect,
/* loops */
Sfor, Swhile, Sdo,
@@ -317,8 +333,17 @@ enum
/* lists */
Xcomma,
+
Dfunc,
+ Dtype,
Dvar,
+ Dvars,
+
+ /* names (shouldn't interact with base AST node enumeration */
+ Nident = iota(0),
+ Nparen = iota(1),
+ Nindex = iota(2),
+ Ncall = iota(3),
};
/* statements */
@@ -331,7 +356,7 @@ struct Stmt
string ident;
Expr *x;
};
- Stmt *stmt;
+ Node *stmt;
} lbl;
struct {
long n;
@@ -339,13 +364,10 @@ struct Stmt
} blk;
Expr *x;
struct {
- union {
- Expr *x;
- Decl *d;
- } init;
+ Node *init;
Expr *cond;
Expr *step;
- Stmt *body;
+ Node *body;
} loop;
union{
string lbl;
@@ -353,9 +375,9 @@ struct Stmt
} jmp;
struct {
Expr *cond;
- Stmt *body;
- Stmt *orelse;
- } sel;
+ Node *body;
+ Node *orelse;
+ } br;
};
};
@@ -363,6 +385,7 @@ struct Stmt
struct Expr
{
struct Node;
+ uint64 type;
union {
struct {
Expr *l;
@@ -403,83 +426,162 @@ struct Name
{
struct Node;
union {
- string ident;
- struct Dtor *paren;
- struct {
- uint32 kind;
- Name *base;
- union {
- Expr *index;
- /* TODO:
- * func params
- * variadic arrays
- * compound set notation
- */
- };
- } suffix;
+ string ident;
+ struct Dtor *paren;
+ };
+ union {
+ Expr *i;
+ Expr *p;
};
};
struct Dtor
{
- Range pos;
Ptr ptr;
Name name;
};
// specifiers
+/*
+ * the design is the following:
+ * type info is held w/in a 64 bit integer.
+ * the bottom 32 bits are associated to specializations
+ * the top 32 bits index into a type-info array held by the compiler.
+ */
enum
{
/* memory */
- Mauto = iota(0),
- Mtype = iota(1),
- Mstatic = iota(2),
- Mreg = iota(3),
- Mtls = iota(4),
+ Mauto = iota(Kauto),
+ Mstatic = iota(Kstatic),
+ Mreg = iota(Kregister),
+ Mtls = iota(Ktls),
+ Mtype = iota(Ktypedef),
+
+ MaskMem = Mauto | Mstatic | Mreg | Mtls | Mtype,
/* qualifiers */
- Qconst = iota(5),
- Qrestr = iota(6),
- Qvoltl = iota(7),
- Qatom = iota(8),
+ Qconst = iota(Kconst),
+ Qrestr = iota(Krestrict),
+ Qvoltl = iota(Kvolatile),
+ Qatom = iota(Katomic),
+
+ MaskQul = Qconst | Qrestr | Qvoltl | Qatom,
- Finlne = iota(9),
- Fnoret = iota(10),
+ Finlne = iota(Kinline),
+ Fnoret = iota(Knoret),
+
+ MaskFcn = Finlne | Fnoret,
/* types */
- Tlong = iota(12),
- Tvlong = iota(13),
- Tsign = iota(14),
- Tunsign = iota(15),
-
- Tvoid = iota(16),
- Tchar = iota(17),
- Tshort = iota(18),
- Tfloat = iota(19),
- Tdouble = iota(20),
- Tcmplx = iota(21),
- Timag = iota(22),
-
- Tatom = iota(23),
-
- Taggr = iota(24),
- Tenum = iota(25),
- Tname = iota(26),
-
- /* alignment */
- Alnas = iota(27),
+ Tsign = iota(Ksigned),
+ Tunsign = iota(Kunsigned),
+
+ MaskSgn = Tsign | Tunsign,
+
+ Tvoid = iota(Kvoid),
+
+ Tfloat = iota(Kfloat),
+ Tdouble = iota(Kdouble),
+ Tcmplx = iota(Kcomplex),
+ Timag = iota(Kimaginary),
+
+ MaskFlt = Tfloat | Tdouble | Tcmplx | Timag,
+
+ Tchar = iota(Kchar),
+ Tbool = iota(Kbool),
+
+ Tshort = iota(Kshort),
+ Tint = iota(Kint),
+ Tlong = iota(Klong),
+ Tvlong = iota(Klong+1),
+
+ MaskInt = Tshort | Tint | Tlong | Tvlong,
+ MaskTyp = Tvoid | Tbool | Tchar | Tint | Tfloat | Timag | Tcmplx,
+ /*
+ * NOTE IMPORTANT: vlong takes over the struct bit place
+ * DON'T MOVE KEYWORDS WITHOUT REORGANIZING
+ */
+ Tstruct = iota(Kstruct+1),
+ Tunion = iota(Kunion+1),
+ Tenum = iota(Kenum+1),
+ Tname = iota(Kenum+2),
+
+ Sbad = -1,
+};
+
+struct Decls
+{
+ union {
+ struct Dtor;
+ Dtor dtor;
+ };
+ Expr *init;
+ struct Decls *link;
};
struct Decl
{
struct Node;
+ uint64 spec;
union {
+ struct {
+ struct Dtor;
+ Stmt *body;
+ } func;
+ struct {
+ struct Dtor;
+ Expr *init;
+ struct Decls *link;
+ } var;
+ struct Dtor type;
+ };
+};
+/* types */
+struct Type
+{
+ Sym *sym;
+ string ident;
+ uintptr size;
+ uint16 align;
+ union {
+ struct {
+ int len;
+ Dtor *d;
+ Expr *x;
+ } su;
+ struct {
+ int len;
+ string *s;
+ Expr *x;
+ } en;
};
};
+struct Scope
+{
+ SymTab tags;
+ SymTab objs;
+};
+
+struct Parser
+{
+ Token tok[2];
+ struct {
+ int cap;
+ int len;
+ Decl **decls;
+ } ast;
+
+ Scope *sp;
+ Scope spstk[20];
+};
+
+/* ast.c functions */
+void parse(Parser *, Lexer *);
+
// -----------------------------------------------------------------------
-// compiler
+// global compiler
enum
{
@@ -525,9 +627,18 @@ string internview(byte* beg, byte *end);
/* main data */
struct Compiler
{
+ /* storage */
memĀ·Arena *heap;
StrTab strs;
+ string outfile;
+
+ struct {
+ int cap;
+ int len;
+ Type *info;
+ } type;
+ /* i/o data */
struct {
int cap;
int len;
@@ -542,15 +653,11 @@ struct Compiler
string *path;
} omit;
+ /* partitioned data for stages */
Lexer lxr;
-
- string outfile;
- struct {
- int cap;
- int len;
- Decl *decls;
- } ast;
+ Parser psr;
};
+
extern Compiler C;
/* cc.c compiler functions */