From a9e3c42ac5696c56adaffbb4007e9d969563337b Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Wed, 20 May 2020 18:28:31 -0700 Subject: feat: prototype of AST --- sys/cmd/cc/cc.c | 6 ++ sys/cmd/cc/cc.h | 245 +++++++++++++++++++++++++++++++++++++++++-------------- sys/cmd/cc/lex.c | 10 +++ sys/cmd/cc/pp.c | 3 +- 4 files changed, 201 insertions(+), 63 deletions(-) (limited to 'sys') diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index cb37af4..fcb9217 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -253,6 +253,12 @@ init(void) C.lxr = (Lexer){ 0 }; C.lxr.b = C.lxr.buf; + + /* predefine macros */ + dodefine(&C.lxr, "__LINE__"); + dodefine(&C.lxr, "__FILE__"); + C.lxr.macline = (uintptr)lookup(&C.lxr.sym, "__LINE__"); + C.lxr.macfile = (uintptr)lookup(&C.lxr.sym, "__FILE__"); } error diff --git a/sys/cmd/cc/cc.h b/sys/cmd/cc/cc.h index b45c5fa..144eb4a 100644 --- a/sys/cmd/cc/cc.h +++ b/sys/cmd/cc/cc.h @@ -16,7 +16,8 @@ typedef struct Lexer Lexer; typedef struct Sym Sym; typedef struct Type Type; -typedef struct Spec Spec; +typedef struct Ptr Ptr; +typedef struct Name Name; typedef struct Decl Decl; typedef struct Stmt Stmt; typedef struct Expr Expr; @@ -64,7 +65,8 @@ typedef struct Compiler Compiler; KEYWORD(Kgoto,"goto") \ KEYWORD(Kreturn,"return") \ KEYWORD(Ksizeof,"sizeof") \ - KEYWORD(Kalignof,"alignof") + KEYWORD(Kalignof,"alignof") \ + KEYWORD(Kalignas,"alignas") #define KEYWORD(a, b) a, enum { KEYWORDS NUM_KEYWORDS }; @@ -250,6 +252,10 @@ struct Lexer SymTab sym; byte *b; byte buf[2*1024]; + + /* predefined dynamic macros */ + uintptr macfile; + uintptr macline; }; /* lex.c functions */ @@ -270,96 +276,206 @@ void puttok(Token); // parsing & type resolution // tokens -> ast -/* statements */ +struct Node +{ + Range pos; + uint32 kind; +}; + +/* ast types */ enum { - Sbad, - Slabel, + Nbad, + /* labels */ + Slabel, Scase, Sblock, Sexpr, Sselect, - Sloop, - Sjump, + /* loops */ + Sfor, Swhile, Sdo, + /* jumps */ + Sgoto, Scontin, Sbreak, Sreturn, + /* forks */ + Sif, Sswitch, + + + /* assignments */ + Xasn, Xmulasn, Xdivasn, Xmodasn, Xsubasn, Xaddasn, + Xlsftasn, Xrsftasn, Xandasn, Xxorasn, Xorasn, + /* conditional */ + Xternary, + /* unary prefix ops */ + Xref, Xstar, Xplus, Xminus, Xneg, Xnot, Xsizeof, Xalnof, Xpreinc, Xpredec, + Xcast, + /* unary postfix ops */ + Xpostinc, Xpostdec, Xindex, Xcall, Xsel, Xcmpndlit, + /* binary ops */ + Xoror, Xandand, Xor, Xxor, Xand, Xeql, Xgt, Xlt, Xgteq, Xlteq, Xlsft, Xrsft, + Xadd, Xsub, Xmul, Xdiv, Xmod, + /* primary */ + Xparen, Xident, Xlit, + /* lists */ + Xcomma, + + Dfunc, + Dvar, }; +/* statements */ struct Stmt { - union - { - + struct Node; + union { + struct { + union { + string ident; + Expr *x; + }; + Stmt *stmt; + } lbl; + struct { + long n; + struct Node **item; + } blk; + Expr *x; + struct { + union { + Expr *x; + Decl *d; + } init; + Expr *cond; + Expr *step; + Stmt *body; + } loop; + union{ + string lbl; + Expr *x; + } jmp; + struct { + Expr *cond; + Stmt *body; + Stmt *orelse; + } sel; }; }; /* expressions */ -enum +struct Expr { - Xbad, - Xparen, - Xident, - Xlit, - Xassign, - Xcomma, - Xternary, - Xbinary, - Xprefix, - Xunary, - Xpostfix, - Xcast, + struct Node; + union { + struct { + Expr *l; + Expr *r; + } asn; + struct { + Expr *c; + Expr *t; + Expr *e; + } cond; + union { + Expr *pre; + Expr *post; + } unary; + struct { + uint64 to; + Expr *x; + } cast; + struct { + Expr *l; + Expr *r; + } binary; + struct { + Expr *x[2]; + } comma; + }; }; -struct Expr +/* declarations */ + +struct Ptr { - union - { + uint64 kind; + Ptr *link; +}; +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; }; }; -/* declarations */ +struct Dtor +{ + Range pos; + Ptr ptr; + Name name; +}; // specifiers enum { - Mauto = iota(0), // Corresponds to auto int + /* memory */ + Mauto = iota(0), Mtype = iota(1), Mstatic = iota(2), Mreg = iota(3), - - Qconst = iota(4), - Qrestr = iota(5), - Qvoltl = iota(6), - Finlne = iota(7), - - Tlong = iota(8), - Tvlong = iota(9), - Tsign = iota(10), - Tunsign = iota(11), - - Tvoid = iota(12), - Tchar = iota(13), - Tshort = iota(14), - Tfloat = iota(15), - Tdouble = iota(16), - Tcmplx = iota(17), - Timag = iota(18), - - Taggr = iota(19), - Tenum = iota(20), - Tname = iota(21), -}; - -enum -{ - Dnil, - Dbad, - Dfunc, - Dvar, + Mtls = iota(4), + + /* qualifiers */ + Qconst = iota(5), + Qrestr = iota(6), + Qvoltl = iota(7), + Qatom = iota(8), + + Finlne = iota(9), + Fnoret = iota(10), + + /* 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), }; struct Decl { - Pos pos; - uint kind; + struct Node; + union { + + }; }; // ----------------------------------------------------------------------- @@ -426,9 +542,14 @@ struct Compiler string *path; } omit; - string outfile; - Lexer lxr; + + string outfile; + struct { + int cap; + int len; + Decl *decls; + } ast; }; extern Compiler C; diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 7a08373..e35ea7e 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -636,6 +636,16 @@ Dispatch: sym = lookup(&lx->sym, tok.val.s); if (sym && ((uintptr)sym->name != (uintptr)lx->io->path)) { + if ((uintptr)sym == lx->macline) { + tok.kind = Alit | Vint; + tok.val.i = lx->pos.line; + goto Return; + } + if ((uintptr)sym == lx->macfile) { + tok.kind = Alit | Vstr; + tok.val.s = lx->pos.path; + goto Return; + } io = makeio(sym->name); io->rdr.end += expandmacro(lx, sym, io->b); *io->rdr.end++ = EOF; diff --git a/sys/cmd/cc/pp.c b/sys/cmd/cc/pp.c index 737e018..30a162b 100644 --- a/sys/cmd/cc/pp.c +++ b/sys/cmd/cc/pp.c @@ -1099,7 +1099,8 @@ dodefine(Lexer *lx, string s) errorf("redefinition of symbol '%s'", sym->name); return 1; } - sym->macro = "\001"; + sym = define(&lx->sym, s, Smacro); + sym->macro = "\00\02"; } return 0; -- cgit v1.2.1