diff options
| author | 2026-03-18 11:33:41 +0100 | |
|---|---|---|
| committer | 2026-03-18 11:33:41 +0100 | |
| commit | 1d9e19fb3bb941cdc28e9d4c3063d3e213fd8312 (patch) | |
| tree | e18eddb587f91455a439c0fd4f1bb3b3216ea2df | |
| parent | 1fee6a61abdf2cf332fffbc50bf7adc1842feb40 (diff) | |
Refactor: use typedefs and CamelCase for aggregate types
Looks nicer
42 files changed, 2092 insertions, 2066 deletions
diff --git a/src/a_embedfilesdir.c b/src/a_embedfilesdir.c index d1c8dd4..c7fd706 100644 --- a/src/a_embedfilesdir.c +++ b/src/a_embedfilesdir.c @@ -1,14 +1,12 @@ #include <stddef.h> -struct embedfile { +#define S(s) s"\0\0\0\0\0", (sizeof s) - 1 + +struct EmbedFile { const char *name; const char *s; size_t len; -}; - -#define S(s) s"\0\0\0\0\0", (sizeof s) - 1 - -struct embedfile embedfilesdir[] = { +} embedfilesdir[] = { {"stddef.h", S("\ #pragma once\n\ typedef __typeof__((char*)0 - (char*)0) ptrdiff_t;\n\ diff --git a/src/a_main.c b/src/a_main.c index 303d513..3db1f84 100644 --- a/src/a_main.c +++ b/src/a_main.c @@ -10,22 +10,23 @@ #include <unistd.h> #include <time.h> -struct option ccopt; -struct cinclpaths cinclpaths[5]; +CCOption ccopt; +CInclPath *cinclpaths[5]; +static CInclPath **cinclpath_tails[5]; static void addinclpath(int ord, const char *path) { - struct inclpath *p = alloc(&globarena, sizeof *p, 0); + CInclPath *p = alloc(&globarena, sizeof *p, 0); assert((uint)ord < countof(cinclpaths)); p->path = path; p->next = NULL; - if (cinclpaths[ord].list) { - *cinclpaths[ord].tail = p; + if (cinclpaths[ord]) { + *cinclpath_tails[ord] = p; } else { - cinclpaths[ord].list = p; + cinclpaths[ord] = p; } - cinclpaths[ord].tail = &p->next; + cinclpath_tails[ord] = &p->next; } /* parse an argument of the form 'opt=abcd' @@ -68,10 +69,10 @@ ftdetect(const char *s) } static union { - struct arena a; - char mem[sizeof(struct arena) + (1<<10)]; + Arena a; + char mem[sizeof(Arena) + (1<<10)]; } _arenamem; -struct arena *globarena = &_arenamem.a; +Arena *globarena = &_arenamem.a; /* withext("x/y.c", "o") -> "y.o"; withext("f9", "s") -> "f9.s" */ static const char * @@ -101,20 +102,21 @@ withext(const char *path, const char *ext) return res; } -struct infile { +typedef struct { enum inft ft; const char *path, *temp; -}; -static struct infile infilebuf[16]; -static struct task { +} InFile; +typedef struct Task { enum outft { OFTexe, OFTdll, OFTobj, OFTasm, OFTc } outft; const char *out; const char *targ; - vec_of(struct infile) inf; + vec_of(InFile) inf; char **runargs; vec_of(const char *) linkargs; bool verbose, run, syntaxonly; -} task = { .inf = VINIT(infilebuf, countof(infilebuf)) }; +} Task; +static InFile infilebuf[16]; +static Task task = { .inf = VINIT(infilebuf, countof(infilebuf)) }; static void prihelp(void); @@ -126,7 +128,7 @@ optparse(char **args) while ((arg = *++args)) { if (*arg++ != '-' || !*arg) { - vpush(&task.inf, ((struct infile) { + vpush(&task.inf, ((InFile) { ft ? ft : ftdetect(arg-1), arg[-1] != '-' ? arg-1 : "/dev/stdin" })); @@ -165,7 +167,7 @@ optparse(char **args) } else if (!strcmp(arg, "trigraphs")) { ccopt.trigraph = 1; } else if (*arg == 'd' && arg[1]) { - /* see common.h§struct option */ + /* see common.h§CCOption */ while (*++arg) switch (*arg | 32) { case 'p': ccopt.dbg.p = 1; break; case 'a': ccopt.dbg.a = 1; break; @@ -250,7 +252,7 @@ optparse(char **args) cinclord = CINCL_idirafter; goto CIncl; } else if (!strcmp(arg, "nostdinc") || !strcmp(arg, "-no-standard-includes")) { - cinclpaths[CINCLsys].list = NULL; + cinclpaths[CINCLsys] = NULL; } else if (*arg == 'M') { ++arg; if (*arg == 'F' || *arg == 'T' || *arg == 'Q') { @@ -292,7 +294,7 @@ tempfile(const char *path, const char *ext) static char sbuf[1024]; const char *tmpdir; const char *file = path; - struct wbuf fbuf = MEMBUF(sbuf, sizeof sbuf); + WriteBuf fbuf = MEMBUF(sbuf, sizeof sbuf); tmpdir = getenv("TMPDIR"); tmpdir = tmpdir ? tmpdir : "/tmp"; @@ -420,17 +422,17 @@ iscrosscc(void) return target.os != HOST_OS || target.arch != HOST_ARCH || target.abi != HOST_ABI; } -struct cmdargs { vec_of(const char *); }; +typedef vec_of(const char *) CmdArgs; static void -findlinkcmd(struct cmdargs *cmd) +findlinkcmd(CmdArgs *cmd) { if (task.targ && iscrosscc()) { /* try to find a cross compiling toolchain, e.g. aarch64-linux-gnu-gcc */ static const char *ccs[] = {"cc", "gcc", "clang"}; char cross[1024]; for (int i = 0; i < countof(ccs); ++i) { - struct wbuf wbuf = MEMBUF(cross, sizeof cross); + WriteBuf wbuf = MEMBUF(cross, sizeof cross); int n = bfmt(&wbuf, "%s-%s", task.targ, ccs[i]); assert(n < sizeof cross-1); cross[n] = 0; @@ -459,7 +461,7 @@ dolink(void) const char *cmdbuf[100]; pid_t p; int wstat; - struct cmdargs cmd = VINIT(cmdbuf, countof(cmdbuf)); + CmdArgs cmd = VINIT(cmdbuf, countof(cmdbuf)); findlinkcmd(&cmd); if (!strcmp(cmd.p[0], "zig")) { @@ -560,7 +562,7 @@ dorun(void) static int driver(void) { - void cpp(struct wbuf *, const char *); + void cpp(WriteBuf *, const char *); if (task.verbose) efmt("# Target: %s\n", task.targ ? task.targ : HOST_TRIPLE); if (task.syntaxonly) @@ -571,7 +573,7 @@ driver(void) fatal(NULL, "not a C source file: %s", task.inf.p[0].path); return cc1(task.out, task.inf.p[0].path); } else if (task.outft == OFTc) { - struct wbuf _buf = {0}, *buf = &bstdout; + WriteBuf _buf = {0}, *buf = &bstdout; if (task.out) { buf = &_buf; buf->buf = alloc(&globarena, buf->cap = 1<<12, 1); @@ -682,7 +684,7 @@ prihelp(void) int main(int argc, char **argv) { - globarena->cap = sizeof(_arenamem.mem) - sizeof(struct arena); + globarena->cap = sizeof(_arenamem.mem) - sizeof(Arena); ioinit(); /* setup defaults */ diff --git a/src/a_targ.c b/src/a_targ.c index 0a14554..f094f60 100644 --- a/src/a_targ.c +++ b/src/a_targ.c @@ -1,27 +1,27 @@ #include "antcc.h" #include "c_type.h" -extern const struct mctarg t_x86_64_sysv, t_aarch64_aapcs; -static const struct targ { +extern const struct MCTarg t_x86_64_sysv, t_aarch64_aapcs; +static const struct Targ { struct { enum mcarch arch; uint oss, abis; }; struct { uchar longsize, vlongsize, ptrsize, valistsize; }; struct { uchar longalign, vlongalign, doublealign, ptralign; }; bool charsigned; uchar sizetype, ptrdifftype, wchartype; - const struct mctarg *mctarg; + const struct MCTarg *mctarg; } targs[] = { { {ISx86_64, -1, -1}, {8,8,8,24}, {8,8,8,8}, 1, TYULONG, TYLONG, TYINT, &t_x86_64_sysv }, { {ISaarch64, -1, -1}, {8,8,8,32}, {8,8,8,8}, 0, TYULONG, TYLONG, TYUINT, &t_aarch64_aapcs }, }; -struct targtriple target; +TargTriple target; uchar targ_primsizes[TYPTR+1]; uchar targ_primalign[TYPTR+1]; uint targ_valistsize; enum typetag targ_sizetype, targ_ptrdifftype, targ_wchartype; bool targ_charsigned, targ_bigendian, targ_64bit; enum mcarch targ_arch; -const struct mctarg *mctarg; +const struct MCTarg *mctarg; static bool matchstr(const char **s, const char *pat) @@ -36,7 +36,7 @@ matchstr(const char **s, const char *pat) } static bool -parsetriple(struct targtriple *trg, const char *str) +parsetriple(TargTriple *trg, const char *str) { if (matchstr(&str, "x86_64-") || matchstr(&str, "amd64-")) trg->arch = ISx86_64; @@ -67,7 +67,7 @@ parsetriple(struct targtriple *trg, const char *str) void targ_init(const char *starg) { - const struct targ *t = NULL; + const struct Targ *t = NULL; uchar *sizes = targ_primsizes, *align = targ_primalign; if (!starg) { diff --git a/src/antcc.h b/src/antcc.h index 3224f05..1a059c7 100644 --- a/src/antcc.h +++ b/src/antcc.h @@ -101,7 +101,7 @@ enum cstd { STDC11, STDC23, }; -struct option { +typedef struct CCOption { enum cstd cstd; bool pedant; bool trigraph; @@ -128,15 +128,14 @@ struct option { }; uint any; } dbg; - struct wbuf *dbgout; -}; -extern struct option ccopt; -extern struct cinclpaths { - struct inclpath { - struct inclpath *next; - const char *path; - } *list, **tail; -} cinclpaths[5]; + struct WriteBuf *dbgout; +} CCOption; +extern CCOption ccopt; + +typedef struct CInclPath { + struct CInclPath *next; + const char *path; +} CInclPath; enum { /* GCC include directory search order: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Options-for-Directory-Search */ CINCL_iquote, CINCL_I, @@ -144,18 +143,20 @@ enum { /* GCC include directory search order: https://gcc.gnu.org/onlinedocs/gcc CINCLsys, CINCL_idirafter, }; +extern CInclPath *cinclpaths[5]; /**********/ /* Target */ /**********/ -struct targtriple { +typedef struct TargTriple { enum mcarch { ISxxx, ISx86_64, ISaarch64 } arch; enum mcos { OSunknown, OSlinux } os; enum mcabi { ABInone, ABIgnu, ABImusl } abi; -}; -extern const struct mctarg *mctarg; -extern struct targtriple target; +} TargTriple; +extern TargTriple target; +enum objkind { OBJELF }; +extern const struct MCTarg *mctarg; void targ_init(const char *); /*********/ @@ -169,7 +170,7 @@ void *xrealloc(void *, size_t); void free(void *); /* string interning */ -typedef const struct internstr {char c;} *internstr; +typedef const struct {char c;} *internstr; internstr intern_(const char *, uint len); #define intern(s) intern_(s, 0) @@ -183,7 +184,6 @@ xbgrow_(void **p, size_t n) if (!*p) { *p = xbnew_(n); xbcap_(*p) = n; assert(n>0); } else if (xbcap_(*p) < n) { size_t k = xbcap_(*p); - assert(k > 0); do k *= 2; while (k < n); *p = 1 + (size_t *)xrealloc(&xbcap_(*(p)), sizeof(size_t) + k); xbcap_(*p) = k; @@ -201,26 +201,31 @@ xbgrow_(void **p, size_t n) /** arenas **/ -struct arena { +typedef struct Arena { uint cap : 31, dyn : 1; uint n; - struct arena *prev; + struct Arena *prev; uchar mem[]; -}; +} Arena; -extern struct arena *globarena; +extern Arena *globarena; -struct arena *newarena(uint chunksiz); -void *alloc(struct arena **, uint siz, uint align); -void *allocz(struct arena **, uint siz, uint align); +Arena *newarena(uint chunksiz); +void *alloc(Arena **, uint siz, uint align); +void *allocz(Arena **, uint siz, uint align); static inline void * -alloccopy(struct arena **arena, const void *src, uint siz, uint align) +alloccopy(Arena **arena, const void *src, uint siz, uint align) { if (!siz) return NULL; return memcpy(alloc(arena, siz, align), src, siz); } -void freearena(struct arena **); +void freearena(Arena **); + +/** bitset (u_bits.h) **/ +typedef struct { size_t u; } BitSet; +enum { BSNBIT = 8 * sizeof(BitSet) }; +#define BSSIZE(nbit) ((nbit)/BSNBIT + ((nbit)%BSNBIT != 0)) /** vec **/ struct vecbase { void *p; uint n; uint cap : 31, dyn : 1; }; @@ -241,7 +246,7 @@ void vresize_(struct vecbase *, uint siz, uint N); /** map of short -> T **/ #define imap_of(T) struct { T *v; int tmp; struct imapbase mb; } -struct imapbase { short *k; struct bitset *bs; uint n, N; }; +struct imapbase { short *k; BitSet *bs; uint n, N; }; void imap_init_(struct imapbase *, void **v, uint vsiz, uint N); int imap_get_(struct imapbase *, short k); @@ -262,7 +267,7 @@ int imap_set_(struct imapbase *, void **v, uint vsiz, short k); (dst)->mb.n = (src)->mb.n; \ imap_init((dst), N); \ memcpy((dst)->mb.k, (src)->mb.k, \ - N*(sizeof*(src)->mb.k + sizeof*(src)->v) + BSSIZE(N)*sizeof(struct bitset)); \ + N*(sizeof*(src)->mb.k + sizeof*(src)->v) + BSSIZE(N)*sizeof(BitSet)); \ } while (0) @@ -285,16 +290,11 @@ extern char pmap_tombstone_[]; for (size_t _i = 0; _i < (m)->mb.N && ((kx) = (m)->mb.k[_i], (pvx) = &(m)->v[_i], 1); ++_i) \ if (kx && kx != pmap_tombstone_) -/** bitset (u_bits.h) **/ -struct bitset { size_t u; }; -enum { BSNBIT = 8 * sizeof(struct bitset) }; -#define BSSIZE(nbit) ((nbit)/BSNBIT + ((nbit)%BSNBIT != 0)) - /********/ /** IO **/ /********/ -struct wbuf { +typedef struct WriteBuf { union { struct { char *buf; @@ -306,41 +306,35 @@ struct wbuf { }; bool err; bool isfp; -}; +} WriteBuf; /* read-only file mapping that is at least 1 page larger than the real file * so it's legal to read a few bytes beyond it to avoid some bounds checks */ -struct memfile { +typedef struct MemFile { const uchar *p; uint n; bool statik; -}; - -struct embedfile { - const char *name; - const char *s; - size_t len; -}; +} MemFile; #define MEMBUF(buf_, cap_) { .buf = (buf_), .cap = (cap_), .fd = -1 } #define FDBUF(buf_, cap_, fd_) { .buf = (buf_), .cap = (cap_), .fd = (fd_) } -extern struct wbuf bstdout, bstderr; +extern WriteBuf bstdout, bstderr; void ioinit(void); -void iowrite(struct wbuf *, const void *src, int n); -void ioputc(struct wbuf *, uchar); -void ioflush(struct wbuf *); -int vbfmt(struct wbuf *, const char *, va_list ap); -int bfmt(struct wbuf *, const char *, ...); +void iowrite(WriteBuf *, const void *src, int n); +void ioputc(WriteBuf *, uchar); +void ioflush(WriteBuf *); +int vbfmt(WriteBuf *, const char *, va_list ap); +int bfmt(WriteBuf *, const char *, ...); #define pfmt(...) bfmt(&bstdout, __VA_ARGS__) #define efmt(...) bfmt(&bstderr, __VA_ARGS__) -struct memfile mapopen(const char **err, const char *path); -void mapclose(struct memfile *); +MemFile mapopen(const char **err, const char *path); +void mapclose(MemFile *); void *mapzeros(uint); int munmap(void *, size_t); -int getpredeffile(struct memfile **, const char *name); -int openfile(const char **err, struct memfile **, const char *path); +int getpredeffile(MemFile **, const char *name); +int openfile(const char **err, MemFile **, const char *path); const char *getfilename(int id, uint atoff); -struct memfile *getfile(int id); +MemFile *getfile(int id); void addfileline(int id, uint off); void setfileline(int id, uint off, int line, const char *file); const char *getfilepos(int *line, int *col, int id, uint off); @@ -350,24 +344,26 @@ void markfileseen(int id); bool isfileseen(int id); void closefile(int id); -enum { SPANFILEBITS = 10 }; -struct span { - struct span0 { - uint off; - uint len : 32-SPANFILEBITS, - file : SPANFILEBITS; - } sl, /* original source location */ - ex; /* the location after #include/macro expansion */ -}; +#define SPANFILEBITS 10 +typedef struct { + uint off, + len : 32-SPANFILEBITS, + file : SPANFILEBITS; +} Span0; + +typedef struct { + Span0 sl, /* original source location */ + ex; /* the location after #include/macro expansion */ +} Span; enum diagkind { DGERROR, DGWARN, DGNOTE, }; -void vdiag(const struct span *, enum diagkind, const char *, va_list); -NORETURN void fatal(const struct span *, const char *, ...); -void error(const struct span *, const char *, ...); -void warn(const struct span *, const char *, ...); -void note(const struct span *, const char *, ...); -ushort *utf8to16(uint *ulen, struct arena **, const uchar *s, size_t len); -uint *utf8to32(uint *ulen, struct arena **, const uchar *s, size_t len); +void vdiag(const Span *, enum diagkind, const char *, va_list); +NORETURN void fatal(const Span *, const char *, ...); +void error(const Span *, const char *, ...); +void warn(const Span *, const char *, ...); +void note(const Span *, const char *, ...); +ushort *utf8to16(uint *ulen, Arena **, const uchar *s, size_t len); +uint *utf8to32(uint *ulen, Arena **, const uchar *s, size_t len); int utf8enc(char out[4], uint cp); #endif /* COMMON_H_ */ @@ -8,9 +8,9 @@ /** Parsing helper functions **/ #define peek(Cm,Tk) lexpeek((Cm)->lx,Tk) static int -lexc(struct comp *cm, struct token *tk) +lexc(CComp *cm, Token *tk) { - struct token tk2, tk_[1]; + Token tk2, tk_[1]; int t = lex(cm->lx, tk ? tk : tk_); if (t == TKSTRLIT && peek(cm, &tk2) == TKSTRLIT && tk2.wide == tk->wide) { /* 5.1.1.2 Translation phase 6: concatenate adjacent string literal tokens */ @@ -46,7 +46,7 @@ lexc(struct comp *cm, struct token *tk) vfree(&rest); } if (ccopt.pedant && in_range(t, TKWBEGIN_, TKWEND_) && (tk = tk ? tk : tk_)->extwarn) { - static struct bitset already[BSSIZE(TKWEND_-TKWBEGIN_+1)]; + static BitSet already[BSSIZE(TKWEND_-TKWBEGIN_+1)]; if (!bstest(already, t-TKWBEGIN_)) { bsset(already, t-TKWBEGIN_); warn(&tk->span, "%'tk in %M is an extension", tk); @@ -56,7 +56,7 @@ lexc(struct comp *cm, struct token *tk) } #define lex(Cm,Tk) lexc(Cm,Tk) static bool -match(struct comp *cm, struct token *tk, enum toktag t) +match(CComp *cm, Token *tk, enum toktag t) { if (peek(cm, NULL) == t) { lex(cm, tk); @@ -65,9 +65,9 @@ match(struct comp *cm, struct token *tk, enum toktag t) return 0; } static bool -expect(struct comp *cm, enum toktag t, const char *s) +expect(CComp *cm, enum toktag t, const char *s) { - struct token tk; + Token tk; if (!match(cm, &tk, t)) { peek(cm, &tk); if (aisprint(t)) tk.span.ex.len = tk.span.sl.len = 1; @@ -96,9 +96,9 @@ enum declkind { * st.more indicates whether there are more decls left to parse (the coroutine * has yielded), or this declaration list is done (the coroutine has finalized) */ -struct declstate { +typedef struct DeclState { enum declkind kind; - union type base; + Type base; uchar scls; uchar qual; bool fnnoreturn : 1, @@ -115,21 +115,21 @@ struct declstate { tagdecl, /* declarator is a tagged type */ empty; /* nothing decl (';') */ internstr *pnames; /* param names for function definition */ - struct span *pspans; /* param spans ditto */ + Span *pspans; /* param spans ditto */ uchar *pqual; /* param quals ditto */ int attr; -}; -static struct decl pdecl(struct declstate *st, struct comp *cm); +} DeclState; +static Decl pdecl(DeclState *st, CComp *cm); -static struct decl *finddecl(struct comp *cm, internstr name); +static Decl *finddecl(CComp *cm, internstr name); /* next token starts a decl? */ static bool -isdecltok(struct comp *cm) +isdecltok(CComp *cm) { - struct token tk; + Token tk; if (peek(cm, &tk) == TKIDENT) { - struct decl *decl = finddecl(cm, tk.name); + Decl *decl = finddecl(cm, tk.name); return decl && decl->scls == SCTYPEDEF; } else { static const bool kws[] = { @@ -152,11 +152,11 @@ isdecltok(struct comp *cm) /* next token starts an expr? */ static bool -isexprtok(struct comp *cm) +isexprtok(CComp *cm) { - struct token tk; + Token tk; if (peek(cm, &tk) == TKIDENT) { - struct decl *decl = finddecl(cm, tk.name); + Decl *decl = finddecl(cm, tk.name); return !decl || decl->scls != SCTYPEDEF; } else { static const bool tks[] = { @@ -174,15 +174,15 @@ isexprtok(struct comp *cm) /* Environment (scope) management */ /**********************************/ -struct envdecls declsbuf; -struct tagged { /* a tagged type declaration */ - union type ty; - struct span span; -}; -static struct tagged envtaggedbuf[1<<7]; -static vec_of(struct tagged) envtagged = VINIT(envtaggedbuf, countof(envtaggedbuf)); -struct env { - struct env *up; +struct declsbuf declsbuf; +typedef struct { /* a tagged type declaration */ + Type ty; + Span span; +} Tagged; +static Tagged envtaggedbuf[1<<7]; +static vec_of(Tagged) envtagged = VINIT(envtaggedbuf, countof(envtaggedbuf)); +struct Env { + Env *up; /* list of decls is implicitly envdecls[decl..ndecl] */ ushort decl, ndecl; /* ditto for envtagged[] */ @@ -192,7 +192,7 @@ struct env { static pmap_of(ushort) tldeclmap; static void -envdown(struct comp *cm, struct env *e) +envdown(CComp *cm, Env *e) { assert(cm->env->decl + cm->env->ndecl == declsbuf.n); assert(cm->env->tagged + cm->env->ntagged == envtagged.n); @@ -204,9 +204,9 @@ envdown(struct comp *cm, struct env *e) } static void -envup(struct comp *cm) +envup(CComp *cm) { - struct env *env = cm->env; + Env *env = cm->env; assert(env->decl + env->ndecl == declsbuf.n); declsbuf.n -= env->ndecl; envtagged.n -= env->ntagged; @@ -215,7 +215,7 @@ envup(struct comp *cm) } int -envadddecl(struct env *env, const struct decl *d) +envadddecl(Env *env, const Decl *d) { assert(env->decl + env->ndecl == declsbuf.n); vpush(&declsbuf, *d); @@ -228,7 +228,7 @@ envadddecl(struct env *env, const struct decl *d) /* iters in reversed order of insertion (most to least recent) */ /* use like so: for (d = NULL; enviterdecl(&d, env);) ... */ static inline bool -enviterdecl(struct decl **d, struct env *env) +enviterdecl(Decl **d, Env *env) { if (!env->ndecl) return 0; if (!*d) *d = &declsbuf.p[env->decl + env->ndecl - 1]; @@ -237,10 +237,10 @@ enviterdecl(struct decl **d, struct env *env) return 1; } -static struct tagged * -envaddtagged(struct env *env, union type ty, const struct span *span) +static Tagged * +envaddtagged(Env *env, Type ty, const Span *span) { - struct tagged tagged = { ty, *span }; + Tagged tagged = { ty, *span }; assert(env->tagged + env->ntagged == envtagged.n); vpush(&envtagged, tagged); assert(envtagged.n < 1<<16); @@ -250,7 +250,7 @@ envaddtagged(struct env *env, union type ty, const struct span *span) /* like enviterdecl */ static inline bool -envitertagged(struct tagged **l, struct env *env) +envitertagged(Tagged **l, Env *env) { if (!env->ntagged) return 0; if (!*l) *l = &envtagged.p[env->tagged + env->ntagged - 1]; @@ -260,7 +260,7 @@ envitertagged(struct tagged **l, struct env *env) } static bool -redeclarationok(const struct decl *old, const struct decl *new) +redeclarationok(const Decl *old, const Decl *new) { bool takeoldscls = 0; if (old->scls != new->scls) { @@ -284,7 +284,7 @@ redeclarationok(const struct decl *old, const struct decl *new) && typedata[old->ty.dat].ret.bits == typedata[new->ty.dat].ret.bits && (typedata[old->ty.dat].kandr || typedata[new->ty.dat].kandr)) { OkFuncs: - if (takeoldscls) ((struct decl *)new)->scls = old->scls; + if (takeoldscls) ((Decl *)new)->scls = old->scls; return 1; } return 0; @@ -295,10 +295,10 @@ redeclarationok(const struct decl *old, const struct decl *new) } static int -putdecl(struct comp *cm, const struct decl *decl) +putdecl(CComp *cm, const Decl *decl) { - for (struct env *env = cm->env; env; env = env->up) { - struct decl *l; + for (Env *env = cm->env; env; env = env->up) { + Decl *l; if (!env->up) { ushort *pi = pmap_get(&tldeclmap, decl->name); if (pi) { @@ -326,15 +326,15 @@ putdecl(struct comp *cm, const struct decl *decl) return envadddecl(cm->env, decl); } -static struct decl * -finddecl(struct comp *cm, internstr name) +static Decl * +finddecl(CComp *cm, internstr name) { assert(name); - for (struct env *e = cm->env; e; e = e->up) { + for (Env *e = cm->env; e; e = e->up) { if (!e->up) { ushort *pi = pmap_get(&tldeclmap, name); if (pi) return &declsbuf.p[*pi]; - } else for (struct decl *l = NULL; enviterdecl(&l, e);) { + } else for (Decl *l = NULL; enviterdecl(&l, e);) { if (name == l->name) return l; } @@ -342,13 +342,13 @@ finddecl(struct comp *cm, internstr name) return NULL; } -static union type -gettagged(struct comp *cm, struct span *span, enum typetag tt, internstr name, bool dodef) +static Type +gettagged(CComp *cm, Span *span, enum typetag tt, internstr name, bool dodef) { - struct typedata td = {0}; + TypeData td = {0}; assert(name); - for (struct env *e = cm->env; e; e = e->up) { - for (struct tagged *l = NULL; envitertagged(&l, e);) { + for (Env *e = cm->env; e; e = e->up) { + for (Tagged *l = NULL; envitertagged(&l, e);) { if (name == ttypenames[typedata[l->ty.dat].id]) { if (dodef && e != cm->env) goto Break2; @@ -365,12 +365,12 @@ Break2: return envaddtagged(cm->env, mktagtype(name, &td), span)->ty; } -static union type -deftagged(struct comp *cm, struct span *span, enum typetag tt, internstr name, union type ty) +static Type +deftagged(CComp *cm, Span *span, enum typetag tt, internstr name, Type ty) { - struct typedata td = {0}; + TypeData td = {0}; assert(name); - for (struct tagged *l = NULL; envitertagged(&l, cm->env);) { + for (Tagged *l = NULL; envitertagged(&l, cm->env);) { if (name == ttypenames[typedata[l->ty.dat].id]) { *span = l->span; return l->ty; @@ -387,14 +387,14 @@ deftagged(struct comp *cm, struct span *span, enum typetag tt, internstr name, u #define iszero(ex) ((ex).t == ENUMLIT && isint((ex).ty) && (ex).u == 0) static bool -islvalue(const struct expr *ex) +islvalue(const Expr *ex) { if (ex->t == EGETF) return islvalue(ex->sub); return ex->t == ESYM || ex->t == EDEREF || ex->t == EINIT || ex->t == ESTRLIT; } -static union type /* 6.5.2.6 default argument promotions */ -argpromote(union type t) +static Type /* 6.5.2.6 default argument promotions */ +argpromote(Type t) { if (isint(t)) t.t = intpromote(t.t); else if (t.t == TYFLOAT) t.t = TYDOUBLE; @@ -404,9 +404,9 @@ argpromote(union type t) } bool -assigncheck(union type t, const struct expr *src) +assigncheck(Type t, const Expr *src) { - union type srcty = typedecay(src->ty);; + Type srcty = typedecay(src->ty);; if (assigncompat(t, srcty)) { if (t.t == TYPTR && srcty.t == TYPTR && (t.flag & TFCHLDQUAL & srcty.flag & TFCHLDQUAL) != (srcty.flag & TFCHLDQUAL)) { @@ -421,7 +421,7 @@ assigncheck(union type t, const struct expr *src) } static bool -initcheck(union type t, const struct expr *src) +initcheck(Type t, const Expr *src) { if (assigncheck(t, src)) return 1; if (t.bits == src->ty.bits && (src->t == EINIT || src->t == ESTRLIT)) return 1; @@ -429,7 +429,7 @@ initcheck(union type t, const struct expr *src) } static void -incdeccheck(enum toktag tt, const struct expr *ex, const struct span *span) +incdeccheck(enum toktag tt, const Expr *ex, const Span *span) { if (!isscalar(ex->ty)) error(&ex->span, "invalid operand to %tt '%ty'", tt, ex->ty); @@ -442,9 +442,9 @@ incdeccheck(enum toktag tt, const struct expr *ex, const struct span *span) } static bool /* 6.5.4 Cast operators */ -castcheck(union type to, const struct expr *ex) +castcheck(Type to, const Expr *ex) { - union type src = ex->ty; + Type src = ex->ty; if (to.t == TYVOID) return 1; if (isagg(to)) return 0; if (to.bits == src.bits) return 1; @@ -455,10 +455,10 @@ castcheck(union type to, const struct expr *ex) return 0; } -static union type /* 6.5.2.1 Array subscripting */ -subscriptcheck(const struct expr *ex, const struct expr *rhs, const struct span *span) +static Type /* 6.5.2.1 Array subscripting */ +subscriptcheck(const Expr *ex, const Expr *rhs, const Span *span) { - union type ty; + Type ty; if (ex->ty.t == TYPTR || ex->ty.t == TYARRAY) { if (isincomplete(typedecay(ty = typechild(ex->ty)))) { error(span, "cannot dereference pointer to incomplete type '%ty'", ty); @@ -477,7 +477,7 @@ subscriptcheck(const struct expr *ex, const struct expr *rhs, const struct span } static uint /* 6.5.3.4 The sizeof and _Alignof operators */ -sizeofalignofcheck(const struct span *span, enum toktag tt, union type ty, const struct expr *ex) +sizeofalignofcheck(const Span *span, enum toktag tt, Type ty, const Expr *ex) { uint r = (tt == TKWsizeof ? typesize : typealign)(ty); if (ty.t == TYVOID) { @@ -496,9 +496,9 @@ sizeofalignofcheck(const struct span *span, enum toktag tt, union type ty, const } static bool /* 6.5.8 Relational operators */ -relationalcheck(const struct expr *a, const struct expr *b) +relationalcheck(const Expr *a, const Expr *b) { - union type t1 = a->ty, t2 = b->ty; + Type t1 = a->ty, t2 = b->ty; if (isarith(t1) && isarith(t2)) return 1; if (isptrcvt(t1) && isptrcvt(t2)) { t1 = typedecay(t1); @@ -509,20 +509,20 @@ relationalcheck(const struct expr *a, const struct expr *b) } static bool -isnullpo(const struct expr *ex) /* match '0' or '(void *) 0' */ +isnullpo(const Expr *ex) /* match '0' or '(void *) 0' */ { - static const union type voidptr = {{ TYPTR, .flag = TFCHLDPRIM, .child = TYVOID }}; + static const Type voidptr = {{ TYPTR, .flag = TFCHLDPRIM, .child = TYVOID }}; while (ex->t == ECAST && ex->ty.bits == voidptr.bits) ex = ex->sub; if (iszero(*ex)) return 1; - return eval((struct expr *)ex, EVINTCONST) /* GNU extension. should we warn? */ + return eval((Expr *)ex, EVINTCONST) /* GNU extension. should we warn? */ && iszero(*ex); } static bool /* 6.5.9 Equality operators */ -equalitycheck(const struct expr *a, const struct expr *b) +equalitycheck(const Expr *a, const Expr *b) { - union type t1 = a->ty, t2 = b->ty; + Type t1 = a->ty, t2 = b->ty; if (isarith(t1) && isarith(t2)) return 1; if (isptrcvt(t1) && isptrcvt(t2)) { t1 = typedecay(t1), t2 = typedecay(t2); @@ -533,10 +533,10 @@ equalitycheck(const struct expr *a, const struct expr *b) return (isptrcvt(t1) && isnullpo(b)) || (isptrcvt(t2) && isnullpo(a)); } -static union type /* 6.5.15 Conditional operator */ -condtype(const struct expr *a, const struct expr *b) +static Type /* 6.5.15 Conditional operator */ +condtype(const Expr *a, const Expr *b) { - union type t1 = typedecay(a->ty), t2 = typedecay(b->ty), s1, s2; + Type t1 = typedecay(a->ty), t2 = typedecay(b->ty), s1, s2; if (isarith(t1) && isarith(t2)) return cvtarith(t1, t2); if (t1.bits == t2.bits) return t1; if (t1.t == TYPTR && isnullpo(b)) return t1; @@ -551,7 +551,7 @@ condtype(const struct expr *a, const struct expr *b) } static void -bintypeerr(const struct span *span, enum toktag tt, union type lhs, union type rhs) +bintypeerr(const Span *span, enum toktag tt, Type lhs, Type rhs) { error(span, "bad operands to %tt ('%ty', '%ty')", tt, lhs, rhs); } @@ -592,11 +592,11 @@ static const struct { uchar prec, t, k; } bintab[] = { [','] = {1, ESEQ, BCSEQ} }; -static union type -bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct expr *rhs) +static Type +bintypecheck(const Span *span, enum toktag tt, Expr *lhs, Expr *rhs) { enum binopclass k = bintab[tt].k; - union type ty = lhs->ty; + Type ty = lhs->ty; assert(k); if (k & BCSET) { @@ -624,7 +624,7 @@ bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct e case BCADDITIVE: if (tt == '+' && isptrcvt(rhs->ty)) { /* int + ptr -> ptr + int (for convenience) */ - const struct expr swaptmp = *lhs; + const Expr swaptmp = *lhs; *lhs = *rhs; *rhs = swaptmp; ty = lhs->ty; @@ -635,7 +635,7 @@ bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct e assert(ty.t); } else if ((ty.t == TYPTR || ty.t == TYARRAY) && isint(rhs->ty)) { /* ptr +/- int */ - union type pointee = typechild(ty); + Type pointee = typechild(ty); if (isincomplete(pointee)) error(span, "arithmetic on pointer to incomplete type '%ty'", ty); else if (pointee.t == TYFUNC) @@ -643,7 +643,7 @@ bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct e ty = typedecay(ty); } else if (tt == '-' && isptrcvt(ty) && isptrcvt(rhs->ty)) { /* ptr - ptr */ - union type pointee1 = typechild(typedecay(ty)), + Type pointee1 = typechild(typedecay(ty)), pointee2 = typechild(typedecay(rhs->ty)); if (isincomplete(pointee1)) error(span, "arithmetic on pointer to incomplete type '%ty'", ty); @@ -709,39 +709,39 @@ bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct e /* Expr Parsing */ /****************/ -#define mkexpr(t_,span_,ty_,...) ((struct expr){.t=(t_), .ty=(ty_), .span=(span_), __VA_ARGS__}) +#define mkexpr(t_,span_,ty_,...) ((Expr){.t=(t_), .ty=(ty_), .span=(span_), __VA_ARGS__}) -static struct expr * -exprdup(struct comp *cm, const struct expr *e) +static Expr * +exprdup(CComp *cm, const Expr *e) { return alloccopy(&cm->exarena, e, sizeof *e, 0); } -static struct expr * -exprdup2(struct comp *cm, const struct expr *e1, const struct expr *e2) +static Expr * +exprdup2(CComp *cm, const Expr *e1, const Expr *e2) { - struct expr *r = alloc(&cm->exarena, 2*sizeof *r, 0); + Expr *r = alloc(&cm->exarena, 2*sizeof *r, 0); r[0] = *e1, r[1] = *e2; return r; } -static struct expr expr(struct comp *cm); -static struct expr commaexpr(struct comp *cm); +static Expr expr(CComp *cm); +static Expr commaexpr(CComp *cm); enum { IMPLICITSYMTY = 0xFF, }; -static struct expr /* 6.5.2.2 Function calls */ -callexpr(struct comp *cm, const struct span *span_, const struct expr *callee) +static Expr /* 6.5.2.2 Function calls */ +callexpr(CComp *cm, const Span *span_, const Expr *callee) { - struct token tk; - struct expr ex, arg; - struct span span = callee->span; - union type ty = callee->ty; - const struct typedata *td = NULL; - struct expr argbuf[10]; - vec_of(struct expr) args = VINIT(argbuf, countof(argbuf)); + Token tk; + Expr ex, arg; + Span span = callee->span; + Type ty = callee->ty; + const TypeData *td = NULL; + Expr argbuf[10]; + vec_of(Expr) args = VINIT(argbuf, countof(argbuf)); bool spanok = joinspan(&span.ex, span_->ex); bool printsig = 0; - const struct builtin *builtin = NULL; + const Builtin *builtin = NULL; if (callee->t == ESYM && !callee->ty.t && declsbuf.p[callee->decl].isbuiltin) { builtin = declsbuf.p[callee->decl].builtin; @@ -750,13 +750,13 @@ callexpr(struct comp *cm, const struct span *span_, const struct expr *callee) if (callee->t == ESYM && ty.t == IMPLICITSYMTY) { /* implicit function decl.. */ internstr name = callee->implicitsym; - struct decl decl = { + Decl decl = { (ty = mkfntype(mktype(TYINT), 0, NULL, /* kandr */ 1, 0)), .scls = SCEXTERN, .span = span, .name = name, .sym = name }; warn(&span, "call to undeclared function '%s'", name); - ((struct expr *)callee)->ty = decl.ty; - ((struct expr *)callee)->decl = putdecl(cm, &decl); + ((Expr *)callee)->ty = decl.ty; + ((Expr *)callee)->decl = putdecl(cm, &decl); } if (!builtin) { @@ -804,9 +804,9 @@ callexpr(struct comp *cm, const struct span *span_, const struct expr *callee) if (printsig) note(&callee->span, "function signature is '%ty'", ty); ex = mkexpr(ECALL, span, ty.t == TYFUNC ? td->ret : ty, .narg = args.n, - .sub = alloc(&cm->exarena, (args.n+1)*sizeof(struct expr), 0)); + .sub = alloc(&cm->exarena, (args.n+1)*sizeof(Expr), 0)); ex.sub[0] = *callee; - memcpy(ex.sub+1, args.p, args.n*sizeof(struct expr)); + memcpy(ex.sub+1, args.p, args.n*sizeof(Expr)); vfree(&args); if (builtin) { builtin->sema(cm, &ex); @@ -815,12 +815,12 @@ callexpr(struct comp *cm, const struct span *span_, const struct expr *callee) } static void -ppostfixopers(struct comp *cm, struct expr *ex) +ppostfixopers(CComp *cm, Expr *ex) { - struct expr tmp, rhs; - struct token tk, tk2; - struct span span; - union type ty; + Expr tmp, rhs; + Token tk, tk2; + Span span; + Type ty; for (;;) switch (peek(cm, &tk)) { default: return; @@ -886,7 +886,7 @@ ppostfixopers(struct comp *cm, struct expr *ex) error(&span, "member access operand is not an aggregate: '%ty'%s", ex->ty, ex->ty.t == TYPTR && isagg(typechild(ex->ty)) ? "; did you mean to use '->'?" : ""); } else { - struct fielddata fld = {.t = mktype(TYINT)}; + FieldData fld = {.t = mktype(TYINT)}; if (*tk2.s && !getfield(&fld, ex->ty, tk2.name)) error(&span, "'%ty' has no such field: '%s'", ex->ty, tk2.name); if (ex->t == EGETF && ex->qual == fld.qual) { /* accumulate */ @@ -904,17 +904,17 @@ ppostfixopers(struct comp *cm, struct expr *ex) } } -static struct expr -vaargexpr(struct comp *cm, struct span *span) +static Expr +vaargexpr(CComp *cm, Span *span) { - struct token tk; - struct expr ex = mkexpr(EXXX, *span, mktype(TYVOID), ); + Token tk; + Expr ex = mkexpr(EXXX, *span, mktype(TYVOID), ); if (expect(cm, '(', "after __builtin_va_arg")) { - struct expr arg = expr(cm); - struct decl decl; - union type ty; + Expr arg = expr(cm); + Decl decl; + Type ty; expect(cm, ',', NULL); - decl = pdecl(&(struct declstate){DCASTEXPR}, cm); + decl = pdecl(&(DeclState){DCASTEXPR}, cm); ty = decl.ty; peek(cm, &tk); if (expect(cm, ')', NULL)) @@ -936,12 +936,12 @@ vaargexpr(struct comp *cm, struct span *span) return ex; } -static struct expr -genericexpr(struct comp *cm, struct span *span) +static Expr +genericexpr(CComp *cm, Span *span) { - struct token tk; + Token tk; if (expect(cm, '(', "after _Generic")) { - struct expr control = expr(cm), dfault = {0}, ex = {0}; + Expr control = expr(cm), dfault = {0}, ex = {0}; expect(cm, ',', NULL); for (;;) { if (match(cm, &tk, TKWdefault)) { @@ -953,8 +953,8 @@ genericexpr(struct comp *cm, struct span *span) dfault = expr(cm); } } else { - struct decl decl = pdecl(&(struct declstate){DCASTEXPR}, cm); - union type ty = decl.ty; + Decl decl = pdecl(&(DeclState){DCASTEXPR}, cm); + Type ty = decl.ty; expect(cm, ':', NULL); if (!ex.t && (typedecay(ty).bits == typedecay(control.ty).bits @@ -991,7 +991,7 @@ tkprec(int tt) return ((uint)tt < countof(bintab)) ? bintab[tt].prec : 0; } -static struct expr initializer(struct comp *cm, union type *ty, enum evalmode ev, +static Expr initializer(CComp *cm, Type *ty, enum evalmode ev, bool globl, enum qualifier qual, internstr name); static internstr istr__func__, istr_main, istr_memset; @@ -1002,17 +1002,17 @@ static internstr mkhiddensym(const char *fnname, const char *name, int id); /* param ident is a kludge to support block labels without backtracking or extra lookahead * see stmt() */ enum exprctx { EFROMSTMT = 1, EARRAYCOUNT, EATTRARG }; -static struct expr -exprparse(struct comp *cm, int prec, const struct token *ident, enum exprctx ctx) +static Expr +exprparse(CComp *cm, int prec, const Token *ident, enum exprctx ctx) { - struct token tk; - struct span span; - struct expr ex; - union type ty; + Token tk; + Span span; + Expr ex; + Type ty; struct { - struct span span; + Span span; union { - union type ty; /* cast type */ + Type ty; /* cast type */ struct { uchar t0; /* t == 0 */ short tt; /* token */ @@ -1034,7 +1034,7 @@ Unary: case '*': if (ctx == EARRAYCOUNT && peek(cm, NULL) == ']') { /* kludge for C99 `int x[*]` (unk VLA size) */ - return (struct expr) { 0, .span = tk.span }; + return (Expr) { 0, .span = tk.span }; } /* fallthru */ case '+': case '-': case '~': case '!': @@ -1052,16 +1052,16 @@ Unary: /* might be unary op (cast) or primary expr */ case '(': if (!isdecltok(cm)) { /* (expr) */ - struct span span = tk.span; + Span span = tk.span; ex = commaexpr(cm); joinspan(&span.ex, ex.span.ex); peek(cm, &tk); if (expect(cm, ')', NULL)) joinspan(&span.ex, tk.span.ex); ex.span = span; } else { /* (type) expr */ - struct declstate st = { DCASTEXPR }; - struct decl decl = pdecl(&st, cm); - struct span span = tk.span; + DeclState st = { DCASTEXPR }; + Decl decl = pdecl(&st, cm); + Span span = tk.span; assert(decl.ty.t); peek(cm, &tk); if (expect(cm, ')', NULL)) @@ -1098,7 +1098,7 @@ Unary: ex = mkexpr(ESTRLIT, tk.span, mkarrtype(ty, 0, tk.len+1), { .s.p = (void *)tk.s, .s.n = tk.len }); break; case TKIDENT: Ident: { - struct decl *decl = finddecl(cm, tk.name); + Decl *decl = finddecl(cm, tk.name); if (!decl) { if (cm->env->up && tk.name->c == '_' && (!strcmp(&tk.name->c, "__FUNCTION__") || !strcmp(&tk.name->c, "__PRETTY_FUNCTION__"))) { @@ -1139,14 +1139,14 @@ Unary: if (!match(cm, NULL, '(')) /* sizeof/alignof expr */ goto Unops; else if (isdecltok(cm)) { /* sizeof/alignof (type) */ - struct declstate st = { DCASTEXPR }; + DeclState st = { DCASTEXPR }; ty = pdecl(&st, cm).ty; peek(cm, &tk); if (expect(cm, ')', NULL)) joinspan(&span.ex, tk.span.ex); res = sizeofalignofcheck(&span, tt, ty, NULL); } else { /* sizeof/alignof expr */ - struct expr tmp = commaexpr(cm); + Expr tmp = commaexpr(cm); peek(cm, &tk); if (expect(cm, ')', NULL)) joinspan(&span.ex, tk.span.ex); @@ -1243,7 +1243,7 @@ Unary: /* binary operators */ for (int opprec; (opprec = tkprec(peek(cm, &tk))) >= prec;) { enum exprkind ek = bintab[tk.t].t; - struct expr rhs, tmp; + Expr rhs, tmp; lex(cm, NULL); if (ek != ECOND) { /* only the assignment operators are right-associative */ @@ -1259,7 +1259,7 @@ Unary: ex = mkexpr(ek, span, ty, .sub = exprdup2(cm, &ex, &rhs)); } else { /* logical-OR-expression ? expression : conditional-expression */ - struct expr *sub; + Expr *sub; span.sl = tk.span.sl; span.ex = ex.span.ex; if (!isscalar(ex.ty) && !isptrcvt(ex.ty)) @@ -1285,26 +1285,26 @@ Unary: return ex; } -static struct expr -expr(struct comp *cm) +static Expr +expr(CComp *cm) { return exprparse(cm, bintab['='].prec, NULL, 0); /* non-comma expr */ } -static struct expr -arraycountexpr(struct comp *cm) +static Expr +arraycountexpr(CComp *cm) { return exprparse(cm, bintab['='].prec, NULL, EARRAYCOUNT); /* non-comma expr, or lone '*' */ } -static struct expr -constantexpr(struct comp *cm) +static Expr +constantexpr(CComp *cm) { return exprparse(cm, bintab['?'].prec, NULL, 0); /* conditional-expr */ } -static struct expr -commaexpr(struct comp *cm) +static Expr +commaexpr(CComp *cm) { return exprparse(cm, 1, NULL, 0); } @@ -1314,7 +1314,7 @@ commaexpr(struct comp *cm) /****************/ static uint -nmemb(union type ty) +nmemb(Type ty) { switch (ty.t) { case TYARRAY: return typearrlen(ty) ? typearrlen(ty) : -1u; @@ -1324,20 +1324,20 @@ nmemb(union type ty) } static bool -objectp(union type ty) +objectp(Type ty) { return isagg(ty) || ty.t == TYARRAY; } static bool -chrarrayof(union type ty, union type chld) +chrarrayof(Type ty, Type chld) { assert(isint(chld)); return ty.t == TYARRAY && isint(typechild(ty)) && typesize(typechild(ty)) == typesize(chld); } -static union type -membertype(uint *off, uint *bitsiz, uint *bitoff, union type ty, uint idx) +static Type +membertype(uint *off, uint *bitsiz, uint *bitoff, Type ty, uint idx) { *bitsiz = *bitoff = 0; if (!objectp(ty)) { @@ -1347,7 +1347,7 @@ membertype(uint *off, uint *bitsiz, uint *bitoff, union type ty, uint idx) *off = typesize(typechild(ty)) * idx; return typechild(ty); } else if (idx < typedata[ty.dat].nmemb) { - struct fielddata fld = typedata[ty.dat].fld[idx].f; + FieldData fld = typedata[ty.dat].fld[idx].f; *off = fld.off; *bitsiz = fld.bitsiz, *bitoff = fld.bitoff; return fld.t; @@ -1356,14 +1356,16 @@ membertype(uint *off, uint *bitsiz, uint *bitoff, union type ty, uint idx) return mktype(0); } -struct initparser { - struct initcur { - union type ty; +typedef struct InitCur InitCur; +typedef struct InitReloc InitReloc; +typedef struct InitParser { + struct InitCur { + Type ty; uint idx; uint off; short prev; } buf[32], *cur, *sub; - struct arena **arena; + Arena **arena; uint arrlen; enum evalmode ev; bool dyn; /* when set, data is written to a temporary buffer first, because either: @@ -1371,27 +1373,27 @@ struct initparser { - data section is not known until parsing done (to avoid relocs in .rodata) otherwise write to the corresponding object data section buffer directly */ union { - struct init *init; /* for initializer with automatic storage */ + Init *init; /* for initializer with automatic storage */ struct { /* for static storage (dyn = 0) */ enum section sec; uint off; }; struct { /* for static storage (dyn = 1) */ vec_of(uchar) ddat; - struct dreloc { - struct dreloc *link; + struct InitReloc { + struct InitReloc *link; internstr sym; vlong addend; uint off; } *drel; }; }; -}; +} InitParser; static void -excesscheck(struct initparser *ip, const struct span *span) +excesscheck(InitParser *ip, const Span *span) { - union type sub = ip->sub->ty; + Type sub = ip->sub->ty; uint n = nmemb(sub); if (ip->sub->idx == n) { if (sub.t == TYARRAY) @@ -1410,10 +1412,10 @@ excesscheck(struct initparser *ip, const struct span *span) #else /* debugging */ static void -dumpini(struct initparser *ip) +dumpini(InitParser *ip) { efmt(">>>\n"); - for (struct initcur *s = ip->buf; s < ip->sub+1; ++s) { + for (InitCur *s = ip->buf; s < ip->sub+1; ++s) { efmt(" "); efmt("%d. [%ty, %u]", s- ip->buf, s->ty, s->idx); if (s == ip->cur) efmt(" <-- cursor"); @@ -1424,7 +1426,7 @@ dumpini(struct initparser *ip) #endif static vlong /* -> returns addend */ -expr2reloc(internstr *psym, const struct expr *ex) +expr2reloc(internstr *psym, const Expr *ex) { if (ex->t == ESSYMREF) { *psym = ex->ssym.sym; @@ -1444,13 +1446,13 @@ rodatarelocok(void) } static void -iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bitoff, union type ty, struct expr *ex) +iniwrite(CComp *cm, InitParser *ip, uint off, uint bitsiz, uint bitoff, Type ty, Expr *ex) { if (ex->ty.t == TYSTRUCT && ip->ev == EVSTATICINI) { assert(ty.bits == ex->ty.bits); for (uint i = 0, n = nmemb(ex->ty); i < n; ++i) { uint suboff; - union type sub = membertype(&suboff, &bitsiz, &bitoff, ex->ty, i); + Type sub = membertype(&suboff, &bitsiz, &bitoff, ex->ty, i); iniwrite(cm, ip, off + suboff, bitsiz, bitoff, sub, exprdup(cm, &mkexpr(EGETF, ex->span, sub, .sub = ex))); } } else if (ip->ev == EVSTATICINI) { @@ -1470,7 +1472,7 @@ iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bit } if (ex->t == ENUMLIT) { - struct expr *e = ex, tmp; + Expr *e = ex, tmp; if (ex->ty.bits != ty.bits && ty.t != TYPTR) { tmp = mkexpr(ECAST, ex->span, ty, .sub = ex); e = &tmp; @@ -1507,7 +1509,7 @@ iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bit objreloc(sym, targ_64bit ? REL_ABS64 : REL_ABS32, ip->sec, ip->off + off, addend); } else { - struct dreloc *rel = alloc(ip->arena, sizeof *rel, 0); + InitReloc *rel = alloc(ip->arena, sizeof *rel, 0); rel->link = ip->drel; rel->sym = sym; rel->off = off; @@ -1517,8 +1519,8 @@ iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bit } } else { assert(cm != NULL); - struct init *init = ip->init; - struct initval val = { + Init *init = ip->init; + InitElem val = { .off = off, .bitsiz = bitsiz, .bitoff = bitoff, @@ -1534,10 +1536,10 @@ iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bit } static bool -iniwriterec(struct comp *cm, struct initparser *ip, uint off, struct expr *ex) +iniwriterec(CComp *cm, InitParser *ip, uint off, Expr *ex) { assert(ex->t == EINIT); - for (struct initval *v = ex->init->vals; v; v = v->next) { + for (InitElem *v = ex->init->vals; v; v = v->next) { if (v->ex.t == EINIT) iniwriterec(cm, ip, off + v->off, &v->ex); else if (ip->ev && !eval(&v->ex, ip->ev) && ip->ev != EVFOLD) return 0; else iniwrite(cm, ip, off + v->off, v->bitsiz, v->bitoff, v->ex.ty, &v->ex); @@ -1545,8 +1547,8 @@ iniwriterec(struct comp *cm, struct initparser *ip, uint off, struct expr *ex) return 1; } -static struct initcur * -iniadvance(struct initparser *ip, struct initcur *c, const struct span *span) +static InitCur * +iniadvance(InitParser *ip, InitCur *c, const Span *span) { if (c - ip->buf >= countof(ip->buf) - 1) fatal(span, "too many nested initializers"); @@ -1555,15 +1557,15 @@ iniadvance(struct initparser *ip, struct initcur *c, const struct span *span) /* set the initializer cursor object */ static void -inifocus(struct initparser *ip, struct comp *cm, const struct span *span, uint idx) +inifocus(InitParser *ip, CComp *cm, const Span *span, uint idx) { while (idx >= nmemb(ip->sub->ty) && ip->sub != ip->cur) { --ip->sub; idx = ip->sub->idx; } uint off, bitsiz, bitoff; - union type targ = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, idx); - struct initcur *next = iniadvance(ip, ip->cur, span); + Type targ = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, idx); + InitCur *next = iniadvance(ip, ip->cur, span); assert(!bitsiz); if (isagg(ip->sub->ty) && targ.t == TYARRAY && !typearrlen(targ)) @@ -1580,7 +1582,7 @@ inifocus(struct initparser *ip, struct comp *cm, const struct span *span, uint i /* initialize a character array with a string literal */ static void -inistrlit(struct comp *cm, struct expr *ex, union type *ty) +inistrlit(CComp *cm, Expr *ex, Type *ty) { if (isincomplete(*ty)) { *ty = mkarrtype(typechild(*ty), ty->flag & TFCHLDQUAL, ex->s.n + 1); @@ -1593,11 +1595,11 @@ inistrlit(struct comp *cm, struct expr *ex, union type *ty) /* read scalar initializer into initializer list and avance */ static void -ininext(struct initparser *ip, struct comp *cm) +ininext(InitParser *ip, CComp *cm) { uint off, bitsiz, bitoff; - union type targ; - struct expr ex = expr(cm); + Type targ; + Expr ex = expr(cm); Retry: targ = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx); @@ -1627,11 +1629,11 @@ Retry: --ip->sub; goto Retry; } else if (objectp(targ) && targ.bits != ex.ty.bits) { - struct initcur *next = iniadvance(ip, ip->sub, &ex.span); + InitCur *next = iniadvance(ip, ip->sub, &ex.span); if (ip->sub - ip->buf == countof(ip->buf) - 1) fatal(&ex.span, "too many nested initializers"); ++ip->sub->idx; - *next = (struct initcur) { targ, .off = ip->sub->off + off }; + *next = (InitCur) { targ, .off = ip->sub->off + off }; ip->sub = next; goto Retry; } @@ -1648,7 +1650,7 @@ Retry: CannotEval: error(&ex.span, "cannot evaluate expression statically"); } else { - struct expr *pex = &ex; + Expr *pex = &ex; if (ip->ev != EVSTATICINI) { if (ex.ty.bits != targ.bits) ex = mkexpr(ECAST, ex.span, targ, .sub = exprdup(cm, &ex)); @@ -1668,19 +1670,19 @@ Retry: } static int -aggdesignator(struct initparser *ip, union type ty, internstr name, const struct span *span) +aggdesignator(InitParser *ip, Type ty, internstr name, const Span *span) { - const struct typedata *td = &typedata[ty.dat]; + const TypeData *td = &typedata[ty.dat]; for (int i = 0; i < td->nmemb; ++i) { - struct namedfield *fld = &td->fld[i]; + NamedField *fld = &td->fld[i]; if (fld->name == name) { return i; } else if (!fld->name) { int save, sub; - struct initcur *next = iniadvance(ip, ip->sub, span); + InitCur *next = iniadvance(ip, ip->sub, span); save = ip->sub->idx; ip->sub->idx = i+1; - *next = (struct initcur) { fld->f.t, .off = ip->sub->off + fld->f.off }; + *next = (InitCur) { fld->f.t, .off = ip->sub->off + fld->f.off }; ip->sub = next; sub = aggdesignator(ip, fld->f.t, name, span); if (sub == -1) { @@ -1693,25 +1695,25 @@ aggdesignator(struct initparser *ip, union type ty, internstr name, const struct } static bool -designators(struct initparser *ip, struct comp *cm) +designators(InitParser *ip, CComp *cm) { - struct token tk; - struct span span; + Token tk; + Span span; bool some = 0; for (;;) { uint off, bitsiz, bitoff; uvlong idx = ~0ull; if (match(cm, &tk, '[')) { - struct expr ex = commaexpr(cm); + Expr ex = commaexpr(cm); span = tk.span; joinspan(&span.ex, ex.span.ex); peek(cm, &tk); if (some) { - union type ty = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx++); - struct initcur *next = iniadvance(ip, ip->sub, &tk.span); + Type ty = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx++); + InitCur *next = iniadvance(ip, ip->sub, &tk.span); assert(!bitsiz); - *next = (struct initcur) { ty, .off = ip->sub->off + off }; + *next = (InitCur) { ty, .off = ip->sub->off + off }; ip->sub = next; dumpini(ip); } @@ -1736,9 +1738,9 @@ designators(struct initparser *ip, struct comp *cm) span = tk.span; peek(cm, &tk); if (some) { - union type ty = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx++); - struct initcur *next = iniadvance(ip, ip->sub, &tk.span); - *next = (struct initcur) { ty, .off = ip->sub->off + off }; + Type ty = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx++); + InitCur *next = iniadvance(ip, ip->sub, &tk.span); + *next = (InitCur) { ty, .off = ip->sub->off + off }; ip->sub = next; dumpini(ip); } @@ -1767,14 +1769,14 @@ designators(struct initparser *ip, struct comp *cm) } } -static struct expr -initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, +static Expr +initializer(CComp *cm, Type *ty, enum evalmode ev, bool globl, enum qualifier qual, internstr sym) { - struct token tk; - struct span span; - struct init res = {0}; - struct initparser ip[1] = {0}; + Token tk; + Span span; + Init res = {0}; + InitParser ip[1] = {0}; ip->arena = &cm->exarena; ip->ev = ev; @@ -1796,7 +1798,7 @@ initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, } if (!match(cm, &tk, '{')) { - struct expr ex = expr(cm); + Expr ex = expr(cm); if (ex.t == ESTRLIT && chrarrayof(*ty, typechild(ex.ty))) { inistrlit(cm, &ex, ty); iniwrite(cm, ip, 0, 0, 0, *ty, &ex); @@ -1832,7 +1834,7 @@ initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, ip->sub = ip->cur = ip->buf + ip->cur->prev; dumpini(ip); } else if (match(cm, &tk, '{')) { - struct span span = tk.span; + Span span = tk.span; inifocus(ip, cm, &tk.span, ip->sub->idx); if (peek(cm, &tk) == '}') { if (!joinspan(&span.ex, tk.span.ex)) span = tk.span; @@ -1879,7 +1881,7 @@ initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, p = sec == Srodata ? objout.rodata.p : objout.data.p; memcpy(p + off, ip->ddat.p, ip->ddat.n); memset(p + off + ip->ddat.n, 0, typesize(*ty) - ip->ddat.n); - for (struct dreloc *rel = ip->drel; rel; rel = rel->link) { + for (InitReloc *rel = ip->drel; rel; rel = rel->link) { objreloc(rel->sym, targ_64bit ? REL_ABS64 : REL_ABS32, sec, off + rel->off, rel->addend); } } @@ -1888,7 +1890,7 @@ initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, dumpini(ip); if (ev == EVSTATICINI) { - return (struct expr){.span = span}; + return (Expr){.span = span}; } else { uint siz; if (isincomplete(*ty)) { @@ -1909,7 +1911,7 @@ initializer(struct comp *cm, union type *ty, enum evalmode ev, bool globl, /* debugging */ void -dumpexpr(const struct expr *ex, bool prity) +dumpexpr(const Expr *ex, bool prity) { static const char *name[] = { [EXXX] = "xxx", [ENUMLIT] = "numlit", [ESTRLIT] = "strlit", @@ -1969,24 +1971,24 @@ dumpexpr(const struct expr *ex, bool prity) /* Decls Parsing */ /*****************/ -static union type -buildagg(struct comp *cm, enum typetag tt, internstr name, int id) +static Type +buildagg(CComp *cm, enum typetag tt, internstr name, int id) { - struct token tk; - union type t; - struct span flexspan; - struct namedfield fbuf[32]; - vec_of(struct namedfield) fld = VINIT(fbuf, countof(fbuf)); - struct typedata td = {tt}; + Token tk; + Type t; + Span flexspan; + NamedField fbuf[32]; + vec_of(NamedField) fld = VINIT(fbuf, countof(fbuf)); + TypeData td = {tt}; bool isunion = tt == TYUNION; const char *tag = isunion ? "union" : "struct"; uint bitsiz = 0, bitfbyteoff = 0, bitoff = 0, bitftypesiz = 0; while (!match(cm, &tk, '}')) { - struct declstate st = { DFIELD }; + DeclState st = { DFIELD }; do { - struct decl decl = pdecl(&st, cm); + Decl decl = pdecl(&st, cm); uint tysize; if (st.empty) { if (ccopt.pedant) @@ -2008,7 +2010,7 @@ buildagg(struct comp *cm, enum typetag tt, internstr name, int id) } bitsiz = 0; if (st.bitf) { - struct expr ex = constantexpr(cm); + Expr ex = constantexpr(cm); const char *name = decl.name ? &decl.name->c : "<anonymous>"; if (!isint(decl.ty)) { error(&decl.span, "bit-field '%s' has non-integer type '%ty'", name, decl.ty); @@ -2053,7 +2055,7 @@ buildagg(struct comp *cm, enum typetag tt, internstr name, int id) uint align = typealign(decl.ty); uint siz = tysize; uint off = bitftypesiz ? bitfbyteoff : isunion ? 0 : alignup(td.siz, align); - struct namedfield f = { decl.name, { decl.ty, off, bitsiz, bitoff, .qual = decl.qual }}; + NamedField f = { decl.name, { decl.ty, off, bitsiz, bitoff, .qual = decl.qual }}; if (bitftypesiz && siz != bitftypesiz) while (f.f.bitoff + f.f.bitsiz > 8*siz) { /* adjust bitfields narrower than container type */ f.f.off += siz; @@ -2090,7 +2092,7 @@ buildagg(struct comp *cm, enum typetag tt, internstr name, int id) if (td.flexi && ccopt.cstd < STDC99 && ccopt.pedant) warn(&flexspan, "flexible array member in %M is an extension"); if (fld.n == 0) { - struct namedfield dummy = { intern(""), { mktype(TYCHAR), 0 }}; + NamedField dummy = { intern(""), { mktype(TYCHAR), 0 }}; error(&tk.span, "%s cannot have zero members", tag); vpush(&fld, dummy); td.siz = td.align = 1; @@ -2123,25 +2125,25 @@ inttyminmax(vlong *min, uvlong *max, enum typetag tt) * and this is similar to existing compiler's de-facto behaviour (though gcc * prefers to use unsigned types when possible). should add support for -fshort-enums */ -static union type -buildenum(struct comp *cm, internstr name, const struct span *span, int id) +static Type +buildenum(CComp *cm, internstr name, const Span *span, int id) { - struct token tk; + Token tk; vlong tymin, minv = 0; uvlong tymax, maxv = 0; - struct typedata td = {TYENUM, .backing = TYINT}; - union type ty = mktype(td.backing); - struct span maxvspan; + TypeData td = {TYENUM, .backing = TYINT}; + Type ty = mktype(td.backing); + Span maxvspan; vlong iota = 0; bool somelonglong = 0; inttyminmax(&tymin, &tymax, td.backing); while (!match(cm, &tk, '}')) { - struct decl decl = {0}; + Decl decl = {0}; peek(cm, &tk); expect(cm, TKIDENT, NULL); if (match(cm, NULL, '=') || (peek(cm, NULL) == TKNUMLIT && !expect(cm, '=', NULL))) { - struct expr ex = expr(cm); + Expr ex = expr(cm); if (eval(&ex, EVINTCONST)) { iota = ex.i; if (ex.ty.t != ty.t) @@ -2199,12 +2201,12 @@ buildenum(struct comp *cm, internstr name, const struct span *span, int id) return ty; } -static union type -tagtype(struct comp *cm, enum toktag kind) +static Type +tagtype(CComp *cm, enum toktag kind) { - struct token tk; - union type t; - struct span span; + Token tk; + Type t; + Span span; enum typetag tt = kind == TKWenum ? TYENUM : kind == TKWstruct ? TYSTRUCT : TYUNION; internstr tag = NULL; @@ -2246,7 +2248,7 @@ tagtype(struct comp *cm, enum toktag kind) } static bool -attrspec(struct comp *cm, int *attr) +attrspec(CComp *cm, int *attr) { /* __attribute__ (( attribute-list )) */ if (!match(cm, NULL, TKW__attribute__)) return 0; if (!expect(cm, '(', "after __attribute__") || !expect(cm, '(', "after __attribute__")) { @@ -2254,7 +2256,7 @@ attrspec(struct comp *cm, int *attr) fatal(NULL, NULL); } while (!match(cm, NULL, ')')) { - struct token tk; + Token tk; lex(cm, &tk); if (tk.t != TKIDENT && !in_range(tk.t, TKWBEGIN_, TKWEND_)) { fatal(&tk.span, "expected attribute name"); @@ -2285,13 +2287,13 @@ attrspec(struct comp *cm, int *attr) return 1; } -static union type -ptypeof(struct comp *cm) +static Type +ptypeof(CComp *cm) { - union type ty; + Type ty; expect(cm, '(', NULL); if (isdecltok(cm)) { /* typeof (type) */ - struct declstate st = { DCASTEXPR }; + DeclState st = { DCASTEXPR }; ty = pdecl(&st, cm).ty; } else { /* typeof (expr) */ ty = commaexpr(cm).ty; @@ -2301,10 +2303,10 @@ ptypeof(struct comp *cm) } static void -declspec(struct declstate *st, struct comp *cm, struct span *pspan) +declspec(DeclState *st, CComp *cm, Span *pspan) { - struct token tk; - struct decl *decl; + Token tk; + Decl *decl; enum arith { KSIGNED = 1<<0, KUNSIGNED = 1<<1, @@ -2318,8 +2320,8 @@ declspec(struct declstate *st, struct comp *cm, struct span *pspan) KDOUBLE = 1<<9, KCOMPLEX = 1<<10, } arith = 0; - struct span span = {0}; - union type ty = st->base; + Span span = {0}; + Type ty = st->base; bool properdecl = st->kind == DFUNCVAR || st->kind == DTOPLEVEL; for (bool first = 1;; first = 0) { @@ -2519,33 +2521,34 @@ End: /* circular doubly linked list used to parse declarators */ enum { EARRAYUNSIZED = 0xFF /* for count.t */ }; -static struct decllist { - struct decllist *prev, *next; +typedef struct DeclList DeclList; +static struct DeclList { + DeclList *prev, *next; uchar t; /* TYPTR, TYARRAY or TYFUNC */ union { uchar qual; /* TYPTR */ - struct expr count; /* TYARRAY */ + Expr count; /* TYARRAY */ struct { /* TYFUNC */ - union type *param; + Type *param; internstr *pnames; - struct span *pspans; + Span *pspans; uchar *pqual; short npar; bool kandr : 1, variadic : 1; }; }; - struct span span; + Span span; } decltmp[64], *declfreelist; static bool usingdeclparamtmp; -static union type declparamtmp[16]; +static Type declparamtmp[16]; static internstr declpnamestmp[16]; -static struct span declpspanstmp[16]; +static Span declpspanstmp[16]; static uchar declpqualtmp[16]; static void -declinsert(struct decllist *list, const struct decllist *node) +declinsert(DeclList *list, const DeclList *node) { - struct decllist *pnode = declfreelist; + DeclList *pnode = declfreelist; if (!pnode) fatal(NULL, "too many nested declarators"); declfreelist = declfreelist->next; *pnode = *node; @@ -2556,9 +2559,9 @@ declinsert(struct decllist *list, const struct decllist *node) } static int -cvqual(struct comp *cm) +cvqual(CComp *cm) { - struct token tk; + Token tk; int q = 0; while (match(cm, &tk, TKWconst) || match(cm, &tk, TKWvolatile) || match(cm, &tk, TKWrestrict)) q |= tk.t == TKWconst ? QCONST : tk.t == TKWvolatile ? QVOLATILE : 0; @@ -2566,10 +2569,10 @@ cvqual(struct comp *cm) } static void -decltypes(struct comp *cm, struct decllist *list, internstr *name, struct span *span, struct span *namespan) +decltypes(CComp *cm, DeclList *list, internstr *name, Span *span, Span *namespan) { - struct token tk; - struct decllist *ptr, node; + Token tk; + DeclList *ptr, node; while (match(cm, &tk, '*')) { node.t = TYPTR; @@ -2642,10 +2645,10 @@ decltypes(struct comp *cm, struct decllist *list, internstr *name, struct span * declinsert(ptr->prev, &node); joinspan(&span->ex, node.span.ex); } else if (match(cm, &tk, '(')) Func: { - vec_of(union type) params = {0}; + vec_of(Type) params = {0}; vec_of(uchar) qual = {0}; vec_of(internstr) names = {0}; - vec_of(struct span) spans = {0}; + vec_of(Span) spans = {0}; if (!usingdeclparamtmp) { usingdeclparamtmp = 1; @@ -2677,8 +2680,8 @@ decltypes(struct comp *cm, struct decllist *list, internstr *name, struct span * } else if (!isdecltok(cm) && peek(cm, &tk) != TKIDENT) { error(&tk.span, "expected parameter declarator"); } else { - struct declstate st = { DFUNCPARAM }; - struct decl decl; + DeclState st = { DFUNCPARAM }; + Decl decl; decl = pdecl(&st, cm); decl.ty = typedecay(decl.ty); vpush(¶ms, decl.ty); @@ -2721,12 +2724,12 @@ decltypes(struct comp *cm, struct decllist *list, internstr *name, struct span * } } -static struct decl -declarator(struct declstate *st, struct comp *cm, struct span span0) { - struct decl decl = { st->base, st->scls, .qual = st->qual, .align = st->align, .span = span0 }; - struct decllist list = { &list, &list }, *l; - static bool inidecltmp = 0; - struct span namespan ={0}; +static Decl +declarator(DeclState *st, CComp *cm, Span span0) { + Decl decl = { st->base, st->scls, .qual = st->qual, .align = st->align, .span = span0 }; + DeclList list = { &list, &list }, *l; + Span namespan ={0}; + static bool inidecltmp; if (!inidecltmp) { inidecltmp = 1; for (int i = 0; i < countof(decltmp); ++i) { @@ -2755,7 +2758,7 @@ declarator(struct declstate *st, struct comp *cm, struct span span0) { decl.ty = mkarrtype(decl.ty, decl.qual, 0); else { uint n = 0; - struct expr *ex = &l->count; + Expr *ex = &l->count; if (!ex->t) { /* ['*'] */ if (l->prev != &list) error(&l->span, "[*] array declarator is not allowed here"); } else if (!eval(ex, EVINTCONST)) { @@ -2786,7 +2789,7 @@ declarator(struct declstate *st, struct comp *cm, struct span span0) { if (l->param != declparamtmp) free(l->param); if (l->prev == &list && l->npar) { /* last */ st->pnames = alloccopy(&cm->fnarena, l->pnames, l->npar * sizeof(char *), 0); - st->pspans = alloccopy(&cm->fnarena, l->pspans, l->npar * sizeof(struct span), 0); + st->pspans = alloccopy(&cm->fnarena, l->pspans, l->npar * sizeof(Span), 0); st->pqual = l->pqual ? alloccopy(&cm->fnarena, l->pqual, l->npar, 1) : NULL; decl.inlin = st->fninline; decl.noret = st->fnnoreturn; @@ -2808,10 +2811,10 @@ declarator(struct declstate *st, struct comp *cm, struct span span0) { } static void -pstaticassert(struct comp *cm, struct span *span) +pstaticassert(CComp *cm, Span *span) { - struct expr ex; - struct token tk, msg = {0}; + Expr ex; + Token tk, msg = {0}; /* _Static_assert '(' <expr> [ ',' <strlit> ] ')' ';' */ expect(cm, '(', NULL); @@ -2838,10 +2841,10 @@ pstaticassert(struct comp *cm, struct span *span) } } -static struct decl -pdecl(struct declstate *st, struct comp *cm) { - struct token tk; - struct decl decl; +static Decl +pdecl(DeclState *st, CComp *cm) { + Token tk; + Decl decl; bool properdecl = st->kind == DTOPLEVEL || st->kind == DFUNCVAR; bool first = 0; @@ -2857,12 +2860,12 @@ pdecl(struct declstate *st, struct comp *cm) { if (!st->base.t) { if (properdecl && (match(cm, &tk, TKW_Static_assert) || match(cm, &tk, TKWstatic_assert))) { pstaticassert(cm, &tk.span); - return (struct decl){0}; + return (Decl){0}; } first = 1; if (match(cm, &tk, ';')) { st->empty = 1; - return (struct decl){.span = tk.span}; + return (Decl){.span = tk.span}; } DeclSpec: @@ -2876,10 +2879,10 @@ pdecl(struct declstate *st, struct comp *cm) { if (st->scls == SCTYPEDEF) properdecl = 0; if (first && st->tagdecl && match(cm, &tk, ';')) { - decl = (struct decl) { st->base, st->scls, st->qual, .align = st->align, .span = decl.span }; + decl = (Decl) { st->base, st->scls, st->qual, .align = st->align, .span = decl.span }; return decl; } else if (st->kind == DFIELD && match(cm, &tk, ':')) { - decl = (struct decl) { st->base, st->scls, st->qual, .align = st->align, .span = decl.span }; + decl = (Decl) { st->base, st->scls, st->qual, .align = st->align, .span = decl.span }; st->bitf = 1; return decl; } @@ -2928,39 +2931,39 @@ AfterIniBitf: /* IR Generation */ /*****************/ -static inline union ref -exprvalue(struct function *fn, const struct expr *ex) +static inline Ref +exprvalue(Function *fn, const Expr *ex) { return compileexpr(fn, ex, /*discard*/ 0); } static inline void -expreffects(struct function *fn, const struct expr *ex) +expreffects(Function *fn, const Expr *ex) { compileexpr(fn, ex, /*discard*/ 1); } static void -structcopy(struct function *fn, union type ty, union ref dst, union ref src) +structcopy(Function *fn, Type ty, Ref dst, Ref src) { - union irtype typ = mkirtype(ty); + IRType typ = mkirtype(ty); addinstr(fn, mkarginstr(typ, dst)); addinstr(fn, mkarginstr(typ, src)); addinstr(fn, mkintrin(INstructcopy, 0, 2)); } -static union ref -structreturn(struct function *fn, const struct expr *src) +static Ref +structreturn(Function *fn, const Expr *src) { return expraddr(fn, src); } -static union ref compilecall(struct function *fn, const struct expr *ex); +static Ref compilecall(Function *fn, const Expr *ex); static internstr mkhiddensym(const char *fnname, const char *name, int id) { char buf[200]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); assert(id > 0); if (fnname) bfmt(&wbuf, "%s.%s.%d", fnname, name, id-1); @@ -2971,14 +2974,14 @@ mkhiddensym(const char *fnname, const char *name, int id) return intern(buf); } -static void geninit(struct function *fn, union type t, union ref dst, const struct expr *src); -static union ref condexprvalue(struct function *fn, const struct expr *ex, bool discard); +static void geninit(Function *fn, Type t, Ref dst, const Expr *src); +static Ref condexprvalue(Function *fn, const Expr *ex, bool discard); -union ref -expraddr(struct function *fn, const struct expr *ex) +Ref +expraddr(Function *fn, const Expr *ex) { - struct decl *decl; - union ref r; + Decl *decl; + Ref r; switch (ex->t) { case ESYM: @@ -3027,14 +3030,14 @@ expraddr(struct function *fn, const struct expr *ex) } else { /* emit static dat */ static int id; - struct initparser ip[1] = {0}; - union type ty = ex->ty; + InitParser ip[1] = {0}; + Type ty = ex->ty; internstr sym = mkhiddensym(NULL, ".LC", ++id); ip->sec = Sdata; /* TODO put in rodata if possible */ ip->ev = EVSTATICINI; assert(!isincomplete(ty)); ip->off = objnewdat(sym, ip->sec, 0, typesize(ty), typealign(ty)); - if (!iniwriterec(NULL, ip, 0, (struct expr *)ex)) + if (!iniwriterec(NULL, ip, 0, (Expr *)ex)) error(&ex->span, "cannot not evaluate expression statically"); return mksymref(sym, 0); } @@ -3050,10 +3053,10 @@ expraddr(struct function *fn, const struct expr *ex) } -static union ref -genload(struct function *fn, union type t, union ref ref, bool volatyl) +static Ref +genload(Function *fn, Type t, Ref ref, bool volatyl) { - struct instr ins = {0}; + Instr ins = {0}; assert(isscalar(t)); ins.cls = type2cls[scalartypet(t)]; @@ -3070,10 +3073,10 @@ genload(struct function *fn, union type t, union ref ref, bool volatyl) return addinstr(fn, ins); } -static union ref -genstore(struct function *fn, union type t, union ref ptr, union ref val) +static Ref +genstore(Function *fn, Type t, Ref ptr, Ref val) { - struct instr ins = {0}; + Instr ins = {0}; assert(isscalar(t)); switch (typesize(t)) { @@ -3088,18 +3091,18 @@ genstore(struct function *fn, union type t, union ref ptr, union ref val) return addinstr(fn, ins); } -static void genbitfstore(struct function *fn, const union type ty, union ref addr, - const struct exgetfld *fld, union ref tmp, union ref val); +static void genbitfstore(Function *fn, const Type ty, Ref addr, + const ExprGetFld *fld, Ref tmp, Ref val); static void -geninit(struct function *fn, union type t, union ref dst, const struct expr *src) +geninit(Function *fn, Type t, Ref dst, const Expr *src) { - union ref adr; + Ref adr; if (src->t == EINIT) { - struct init *ini = src->init; + Init *ini = src->init; uint siz = typesize(t); uint align = typealign(t); - struct bitset azero[1] = {0}; + BitSet azero[1] = {0}; if (BSSIZE(siz) <= countof(ini->zero)) { for (int i = 0; i < siz; i += align) { @@ -3122,7 +3125,7 @@ geninit(struct function *fn, union type t, union ref dst, const struct expr *src } else Memset0: { /* memset(dst,0,siz) */ /* TODO make it into an intrinsic */ - struct instr call = { Ocall, KPTR }; + Instr call = { Ocall, KPTR }; addinstr(fn, mkarginstr(cls2type(KPTR), dst)); addinstr(fn, mkarginstr(cls2type(KI32), ZEROREF)); addinstr(fn, mkarginstr(cls2type(type2cls[targ_sizetype]), mkintcon(type2cls[targ_sizetype], siz))); @@ -3130,9 +3133,9 @@ geninit(struct function *fn, union type t, union ref dst, const struct expr *src call.r = mkcallarg(cls2type(KPTR), 3, -1); addinstr(fn, call); } - for (struct initval *val = ini->vals; val; val = val->next) { + for (InitElem *val = ini->vals; val; val = val->next) { uint off = val->off; - struct expr *ex = &val->ex; + Expr *ex = &val->ex; adr = irbinop(fn, Oadd, KPTR, dst, mkref(RICON, off)); if (ex->t == EINIT || ex->t == ESTRLIT) { geninit(fn, ex->ty, adr, ex); @@ -3141,12 +3144,12 @@ geninit(struct function *fn, union type t, union ref dst, const struct expr *src } else if (!val->bitsiz) { genstore(fn, ex->ty, adr, exprvalue(fn, ex)); } else { - union ref q = exprvalue(fn, ex); - genbitfstore(fn, ex->ty, adr, &(struct exgetfld){0, val->bitsiz, val->bitoff}, NOREF, q); + Ref q = exprvalue(fn, ex); + genbitfstore(fn, ex->ty, adr, &(ExprGetFld){0, val->bitsiz, val->bitoff}, NOREF, q); } } } else if (src->t == ESTRLIT) { - union type ctyp = typechild(src->ty); + Type ctyp = typechild(src->ty); uint csiz = typesize(ctyp); adr = dst; for (uint i = 0; i < src->s.n; ++i) { @@ -3163,17 +3166,17 @@ geninit(struct function *fn, union type t, union ref dst, const struct expr *src } static bool -isboollike(struct function *fn, union ref r) +isboollike(Function *fn, Ref r) { - struct instr *ins; + Instr *ins; if (r.t == RICON && (r.i == 0 || r.i == 1)) return 1; if (r.t != RTMP) return 0; ins = &instrtab[r.i]; if (oiscmp(ins->op)) /* these instrs already have output range of [0,1] */ return 1; if (ins->op == Ophi) { /* check if all the phi args are boollike */ - struct block *blk; - union ref *phi = NULL; + Block *blk; + Ref *phi = NULL; for (blk = fn->curblk; phi == NULL; blk = blk->lprev) { /* find blk that defines phi */ assert(blk != fn->entry); @@ -3200,8 +3203,8 @@ isboollike(struct function *fn, union ref r) return 0; } -union ref -scalarcvt(struct function *fn, union type to, union type from, union ref ref) +Ref +scalarcvt(Function *fn, Type to, Type from, Ref ref) { enum irclass kto = type2cls[scalartypet(to)], kfrom = type2cls[scalartypet(from)]; enum op op; @@ -3238,8 +3241,8 @@ scalarcvt(struct function *fn, union type to, union type from, union ref ref) return irunop(fn, op, kto, ref); } -static union ref -narrow(struct function *fn, enum irclass to, union type t, union ref ref, uint bitsiz) +static Ref +narrow(Function *fn, enum irclass to, Type t, Ref ref, uint bitsiz) { enum typetag tt = scalartypet(t); assert(isscalar(t)); @@ -3269,12 +3272,12 @@ narrow(struct function *fn, enum irclass to, union type t, union ref ref, uint b return ref; } -union ref -genptroff(struct function *fn, enum op op, uint siz, union ref ptr, - union type t, union ref idx) +Ref +genptroff(Function *fn, enum op op, uint siz, Ref ptr, + Type t, Ref idx) { uint cls = type2cls[targ_sizetype]; - union ref off; + Ref off; assert(siz); idx = scalarcvt(fn, mktype(targ_sizetype), t, idx); @@ -3289,8 +3292,8 @@ genptroff(struct function *fn, enum op op, uint siz, union ref ptr, return irbinop(fn, op, KPTR, ptr, off); } -union ref -genptrdiff(struct function *fn, uint siz, union ref a, union ref b) +Ref +genptrdiff(Function *fn, uint siz, Ref a, Ref b) { uint cls = type2cls[targ_ptrdifftype]; assert(siz > 0); @@ -3304,9 +3307,9 @@ genptrdiff(struct function *fn, uint siz, union ref a, union ref b) /* used to emit the jumps in an in if (), while (), etc condition */ static void -condjump(struct function *fn, const struct expr *ex, struct block *tr, struct block *fl) +condjump(Function *fn, const Expr *ex, Block *tr, Block *fl) { - struct block *next, *next2; + Block *next, *next2; Recur: for (; ex->t == ESEQ; ex = &ex->sub[1]) expreffects(fn, &ex->sub[0]); @@ -3348,55 +3351,55 @@ Recur: } } -struct condphis { - union type typ; - vec_of(union ref) ref; -}; +typedef struct { + Type typ; + vec_of(Ref) refs; +} CondPhi; static void -condexprrec(struct function *fn, const struct expr *ex, struct condphis *phis, struct block *end) +condexprrec(Function *fn, const Expr *ex, CondPhi *phi, Block *end) { Recur: for (; ex->t == ESEQ; ex = &ex->sub[1]) expreffects(fn, &ex->sub[0]); int prevpred = end->npred; if (ex->t == ELOGAND) { - struct block *tr = newblk(fn); + Block *tr = newblk(fn); condjump(fn, &ex->sub[0], tr, end); assert(prevpred <= end->npred); - if (phis) for (int n = end->npred - prevpred; n > 0; --n) { - vpush(&phis->ref, mkref(RICON, 0)); + if (phi) for (int n = end->npred - prevpred; n > 0; --n) { + vpush(&phi->refs, mkref(RICON, 0)); } useblk(fn, tr); ex = &ex->sub[1]; goto Recur; } else if (ex->t == ELOGIOR) { - struct block *fl = newblk(fn); + Block *fl = newblk(fn); condjump(fn, &ex->sub[0], end, fl); assert(prevpred <= end->npred); - if (phis) for (int n = end->npred - prevpred; n > 0; --n) { - vpush(&phis->ref, mkref(RICON, 1)); + if (phi) for (int n = end->npred - prevpred; n > 0; --n) { + vpush(&phi->refs, mkref(RICON, 1)); } useblk(fn, fl); ex = &ex->sub[1]; goto Recur; } else if (ex->t == ECOND) { - struct block *tr = newblk(fn), *fl = newblk(fn); + Block *tr = newblk(fn), *fl = newblk(fn); condjump(fn, &ex->sub[0], tr, fl); useblk(fn, tr); - condexprrec(fn, &ex->sub[1], phis, end); + condexprrec(fn, &ex->sub[1], phi, end); useblk(fn, fl); ex = &ex->sub[2]; goto Recur; } else { - if (!phis) { + if (!phi) { expreffects(fn, ex); } else { - union ref val = exprvalue(fn, ex); - if (isscalar(phis->typ)) - val = scalarcvt(fn, phis->typ, ex->ty, val); - else assert(ex->ty.bits == phis->typ.bits); - vpush(&phis->ref, val); + Ref val = exprvalue(fn, ex); + if (isscalar(phi->typ)) + val = scalarcvt(fn, phi->typ, ex->ty, val); + else assert(ex->ty.bits == phi->typ.bits); + vpush(&phi->refs, val); } putbranch(fn, end); } @@ -3404,13 +3407,13 @@ Recur: /* the naive way to generate something like a ? b : c ? d : e, uses multiple phis, * this code reduces such nested conditional expressions into one phi */ -static union ref -condexprvalue(struct function *fn, const struct expr *ex, bool discard) +static Ref +condexprvalue(Function *fn, const Expr *ex, bool discard) { - union ref refbuf[8]; - struct condphis phis = { ex->t == ECOND ? ex->ty : mktype(TYBOOL), VINIT(refbuf, countof(refbuf)) }; - struct block *dst = newblk(fn); - condexprrec(fn, ex, discard ? NULL : &phis, dst); + Ref refbuf[8]; + CondPhi phi = { ex->t == ECOND ? ex->ty : mktype(TYBOOL), VINIT(refbuf, countof(refbuf)) }; + Block *dst = newblk(fn); + condexprrec(fn, ex, discard ? NULL : &phi, dst); useblk(fn, dst); if (discard) return NOREF; enum irclass k; @@ -3421,22 +3424,22 @@ condexprvalue(struct function *fn, const struct expr *ex, bool discard) assert(isagg(ex->ty) || isptrcvt(ex->ty)); k = KPTR; } - union ref r = addphi(fn, k, phis.ref.p); - vfree(&phis.ref); + Ref r = addphi(fn, k, phi.refs.p); + vfree(&phi.refs); return r; } -static union ref -compilecall(struct function *fn, const struct expr *ex) +static Ref +compilecall(Function *fn, const Expr *ex) { - struct instr ins = {0}; - struct expr *sub = ex->sub; - const struct typedata *td = &typedata[sub[0].ty.dat]; - struct instr insnsbuf[10]; - vec_of(struct instr) insns = VINIT(insnsbuf, countof(insnsbuf)); + Instr ins = {0}; + Expr *sub = ex->sub; + const TypeData *td = &typedata[sub[0].ty.dat]; + Instr insnsbuf[10]; + vec_of(Instr) insns = VINIT(insnsbuf, countof(insnsbuf)); if (sub[0].t == ESYM && declsbuf.p[sub[0].decl].isbuiltin) { - return declsbuf.p[sub[0].decl].builtin->comp(fn, (struct expr *)ex, 0); + return declsbuf.p[sub[0].decl].builtin->comp(fn, (Expr *)ex, 0); } ins.op = Ocall; if (isagg(ex->ty)) { @@ -3448,28 +3451,28 @@ compilecall(struct function *fn, const struct expr *ex) } ins.l = exprvalue(fn, &sub[0]); for (int i = 0; i < ex->narg; ++i) { - struct expr *arg = &sub[i+1]; - union type ty = i < td->nmemb ? td->param[i] : argpromote(arg->ty); - union ref r = scalarcvt(fn, ty, typedecay(arg->ty), exprvalue(fn, arg)); + Expr *arg = &sub[i+1]; + Type ty = i < td->nmemb ? td->param[i] : argpromote(arg->ty); + Ref r = scalarcvt(fn, ty, typedecay(arg->ty), exprvalue(fn, arg)); vpush(&insns, mkarginstr(mkirtype(ty), r)); } for (int i = 0; i < insns.n; ++i) addinstr(fn, insns.p[i]); vfree(&insns); ins.r = mkcallarg(mkirtype(ex->ty), ex->narg, td->variadic ? td->nmemb : td->kandr ? 0 : -1); - union ref r = addinstr(fn, ins); + Ref r = addinstr(fn, ins); if (sub[0].t == ESYM && declsbuf.p[sub[0].decl].noret) /* trap if noreturn func returns */ puttrap(fn); return r; } -static union ref -genbitfload(struct function *fn, union ref *tmpval, const union type ty, union ref *addr, - const struct exgetfld *fld, bool volatyl) +static Ref +genbitfload(Function *fn, Ref *tmpval, const Type ty, Ref *addr, + const ExprGetFld *fld, bool volatyl) { enum irclass k = type2cls[scalartypet(ty)]; uint off = fld->off, bitsiz = fld->bitsiz, bitoff = fld->bitoff; - union ref tmp; + Ref tmp; uvlong mask; assert(k); @@ -3494,8 +3497,8 @@ genbitfload(struct function *fn, union ref *tmpval, const union type ty, union r } static void -genbitfstore(struct function *fn, const union type ty, union ref addr, - const struct exgetfld *fld, union ref tmp, union ref val) +genbitfstore(Function *fn, const Type ty, Ref addr, + const ExprGetFld *fld, Ref tmp, Ref val) { enum irclass k = type2cls[scalartypet(ty)]; uint off = fld->off, bitsiz = fld->bitsiz, bitoff = fld->bitoff; @@ -3527,7 +3530,7 @@ genbitfstore(struct function *fn, const union type ty, union ref addr, } static bool -knowntruthy(bool *t, struct expr *ex) +knowntruthy(bool *t, Expr *ex) { if (!eval(ex, EVFOLD)) return 0; @@ -3544,18 +3547,18 @@ knowntruthy(bool *t, struct expr *ex) return 1; } -union ref -compileexpr(struct function *fn, const struct expr *ex, bool discard) +Ref +compileexpr(Function *fn, const Expr *ex, bool discard) { - union type ty; - union ref l, r, q, adr; + Type ty; + Ref l, r, q, adr; uint bitsiz; enum op op; enum irclass cls = type2cls[scalartypet(ex->ty)]; int swp = 0; - struct expr *sub; + Expr *sub; - //eval((struct expr *)ex, EVFOLD); + //eval((Expr *)ex, EVFOLD); sub = ex->sub; if (ex->ty.t != TYVOID && !isscalar(ex->ty)) { @@ -3680,7 +3683,7 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard) r = isflt(ex->ty) ? mkfltcon(type2cls[ex->ty.t], 1.0) : mkref(RICON, 1); bitsiz = 0; if (sub[0].t == EGETF && (bitsiz = sub->fld.bitsiz)) { - union ref tmp; + Ref tmp; adr = expraddr(fn, &sub[0].sub[0]); l = genbitfload(fn, &tmp, sub[0].ty, &adr, &sub[0].fld, sub[0].qual & QVOLATILE); q = irbinop(fn, op, cls, l, r); @@ -3787,7 +3790,7 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard) r = exprvalue(fn, &sub[1]); if (sub[0].t == EGETF && (bitsiz = sub[0].fld.bitsiz)) { /* bit-field */ - union ref tmp; + Ref tmp; CompoundBitf: adr = expraddr(fn, &sub[0].sub[0]); l = genbitfload(fn, &tmp, sub[0].ty, &adr, &sub[0].fld, sub[0].qual & QVOLATILE); @@ -3822,7 +3825,7 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard) } if (ex->ty.t == TYVOID || discard) { - struct block *tr, *fl, *end; + Block *tr, *fl, *end; condjump(fn, &sub[0], tr = newblk(fn), fl = newblk(fn)); useblk(fn, tr); expreffects(fn, &sub[1]); @@ -3858,61 +3861,62 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard) /************************************/ static void -stmtterm(struct comp *cm) +stmtterm(CComp *cm) { expect(cm, ';', "to terminate previous statement"); } -static void block(struct comp *cm, struct function *fn); -static bool stmt(struct comp *cm, struct function *fn); -static void localdecl(struct comp *cm, struct function *fn, bool forinit); +static void block(CComp *cm, Function *fn); +static bool stmt(CComp *cm, Function *fn); +static void localdecl(CComp *cm, Function *fn, bool forinit); -struct label { - struct label *link; +typedef struct Label Label; +struct Label { + Label *link; internstr name; - struct block *blk; - struct span usespan; + Block *blk; + Span usespan; /* if usespan.ex.len == 0, this label is resolved and blk is the block that * the label starts, otherwise the label is unresolved and blk is the head * of a linked list of relocations, the next list entry is in blk->s1, etc, * terminated by NULL */ }; -static struct label * -findlabel(struct comp *cm, internstr name) +static Label * +findlabel(CComp *cm, internstr name) { - for (struct label *l = cm->labels; l; l = l->link) + for (Label *l = cm->labels; l; l = l->link) if (l->name == name) return l; return NULL; } static void -deflabel(struct comp *cm, struct function *fn, const struct span *span, internstr name) +deflabel(CComp *cm, Function *fn, const Span *span, internstr name) { - struct label *label = findlabel(cm, name); + Label *label = findlabel(cm, name); if (label && label->usespan.ex.len == 0) { error(span, "redefinition of label '%s'", name); } else if (label) { - struct block *new = NULL; + Block *new = NULL; if (!nerror) { new = newblk(fn); if (fn->curblk) putbranch(fn, new); } /* fix up relocations */ - for (struct block *list = label->blk, *next; list; list = next) { + for (Block *list = label->blk, *next; list; list = next) { next = list->s1; if (!nerror) { useblk(fn, list); putbranch(fn, new); } } - label->usespan = (struct span){0}; + label->usespan = (Span){0}; label->blk = new; if (!nerror) useblk(fn, new); } else { - struct label l = { cm->labels, name }; + Label l = { cm->labels, name }; if (!nerror) { - struct block *new = newblk(fn); + Block *new = newblk(fn); if (fn->curblk) putbranch(fn, new); useblk(fn, new); } @@ -3922,9 +3926,9 @@ deflabel(struct comp *cm, struct function *fn, const struct span *span, internst } static bool -loopbody(struct comp *cm, struct function *fn, struct block *brk, struct block *cont) +loopbody(CComp *cm, Function *fn, Block *brk, Block *cont) { - struct block *save[2]; + Block *save[2]; bool terminates = 0; save[0] = cm->breakto, save[1] = cm->loopcont; @@ -3941,42 +3945,42 @@ loopbody(struct comp *cm, struct function *fn, struct block *brk, struct block * #define EMITS if (doemit && !nerror) -struct swcase { +typedef struct { vlong val; - struct block *blk; - struct span span; -}; -struct switchstmt { - struct block *bdefault; - union type condtype; - vec_of(struct swcase) cases; -}; + Block *blk; + Span span; +} SwitchCase; +typedef struct SwitchStmt { + Block *bdefault; + Type condtype; + vec_of(SwitchCase) cases; +} SwitchStmt; static int cmpswcase(const void *aa, const void *bb) { - const struct swcase *a = aa, *b = bb; + const SwitchCase *a = aa, *b = bb; vlong v1 = a->val, v2 = b->val; if (v1 != v2) return v1 < v2 ? -1 : 1; return (a > b) - (a < b); /* preserve original order */ } static void -swsortcases(struct swcase *cs, uint n) +swsortcases(SwitchCase *cs, uint n) { void qsort(void *, size_t n, size_t size, int (*)(const void *, const void *)); qsort(cs, n, sizeof *cs, cmpswcase); } static bool -genswitch(struct comp *cm, struct function *fn, const struct expr *ex) +genswitch(CComp *cm, Function *fn, const Expr *ex) { - union ref sel; + Ref sel; bool doemit = fn->curblk; - struct block *begin = NULL, *end = NULL, *breaksave = cm->breakto; - struct switchstmt *stsave = cm->switchstmt, st = {.condtype = ex->ty}; + Block *begin = NULL, *end = NULL, *breaksave = cm->breakto; + SwitchStmt *stsave = cm->switchstmt, st = {.condtype = ex->ty}; enum irclass k = type2cls[scalartypet(ex->ty)]; - struct swcase casebuf[8]; + SwitchCase casebuf[8]; vinit(&st.cases, casebuf, countof(casebuf)); assert(k); @@ -4009,7 +4013,7 @@ genswitch(struct comp *cm, struct function *fn, const struct expr *ex) */ vlong prev; for (int i = 0; i < st.cases.n; ++i) { - const struct swcase *c = &st.cases.p[i]; + const SwitchCase *c = &st.cases.p[i]; if (i > 0) { assert(c->val >= prev); if (c->val == prev) { @@ -4018,7 +4022,7 @@ genswitch(struct comp *cm, struct function *fn, const struct expr *ex) } } EMITS { - struct block *next = i < st.cases.n - 1 ? newblk(fn) : st.bdefault; + Block *next = i < st.cases.n - 1 ? newblk(fn) : st.bdefault; putcondbranch(fn, irbinop(fn, Oequ, k, sel, mkintcon(k, c->val)), c->blk, next); if (next != st.bdefault) useblk(fn, next); } @@ -4039,18 +4043,18 @@ genswitch(struct comp *cm, struct function *fn, const struct expr *ex) } static bool /* return 1 if stmt is terminating (ends with a jump) */ -stmt(struct comp *cm, struct function *fn) +stmt(CComp *cm, Function *fn) { - struct block *tr, *fl, *end, *begin; + Block *tr, *fl, *end, *begin; union { - struct arena a; - char mem[sizeof(struct arena) + sizeof(struct expr)*4]; - } atmp = { .a.cap = sizeof(struct expr)*4 }; - struct arena *atmpp; - struct expr ex; - struct env e; - union ref r; - struct token tk; + Arena a; + char mem[sizeof(Arena) + sizeof(Expr)*4]; + } atmp = { .a.cap = sizeof(Expr)*4 }; + Arena *atmpp; + Expr ex; + Env e; + Ref r; + Token tk; bool terminates = 0; bool doemit = fn->curblk; @@ -4062,7 +4066,7 @@ stmt(struct comp *cm, struct function *fn) if (!eval(&ex, EVINTCONST)) error(&ex.span, "not an integer constant expression"); else if (cm->switchstmt && ex.ty.bits != cm->switchstmt->condtype.bits) { - struct expr tmp = ex; + Expr tmp = ex; ex = mkexpr(ECAST, ex.span, cm->switchstmt->condtype, .sub = &tmp); bool ok = eval(&ex, EVINTCONST); assert(ok && "cast const int?"); @@ -4076,7 +4080,7 @@ stmt(struct comp *cm, struct function *fn) useblk(fn, begin); } if (cm->switchstmt) - vpush(&cm->switchstmt->cases, ((struct swcase) {ex.i, fn->curblk, ex.span})); + vpush(&cm->switchstmt->cases, ((SwitchCase) {ex.i, fn->curblk, ex.span})); } else if (tk.t == TKWdefault) { /* default ':' */ if (!cm->switchstmt) error(&tk.span, "'default' outside of switch statement"); @@ -4274,7 +4278,7 @@ stmt(struct comp *cm, struct function *fn) /* since exarena is free'd at the end of each stmt, create a new temporary * arena to parse this expression because loop body statements would free it * otherwise */ - struct arena *tmp = cm->exarena; + Arena *tmp = cm->exarena; cm->exarena = &atmp.a; ex = commaexpr(cm); atmpp = cm->exarena; @@ -4330,16 +4334,16 @@ stmt(struct comp *cm, struct function *fn) lex(cm, &tk); peek(cm, &tk); if (expect(cm, TKIDENT, NULL)) { - struct label *label = findlabel(cm, tk.name); + Label *label = findlabel(cm, tk.name); if (!label) { /* create reloc list */ - struct label l = { cm->labels, tk.name, fn->curblk, tk.span }; + Label l = { cm->labels, tk.name, fn->curblk, tk.span }; assert(l.usespan.ex.len); cm->labels = alloccopy(fn->arena, &l, sizeof l, 0); fn->curblk = NULL; } else if (label && label->usespan.ex.len != 0) { /* append to relocs list */ - struct block *next = label->blk; + Block *next = label->blk; label->blk = fn->curblk; EMITS { fn->curblk->s1 = next; @@ -4395,12 +4399,12 @@ stmt(struct comp *cm, struct function *fn) /* parse and compile a function-local declaration */ static void -localdecl(struct comp *cm, struct function *fn, bool forini) +localdecl(CComp *cm, Function *fn, bool forini) { - struct expr ini; - struct token tk; + Expr ini; + Token tk; const bool doemit = fn->curblk; - struct declstate st = { DFUNCVAR }; + DeclState st = { DFUNCVAR }; if (!forini && match(cm, &tk, TKIDENT)) { if (match(cm, NULL, ':')) { @@ -4414,7 +4418,7 @@ localdecl(struct comp *cm, struct function *fn, bool forini) st.base0 = 1; } do { - struct decl decl = pdecl(&st, cm); + Decl decl = pdecl(&st, cm); if (decl.name) { static int staticid; bool put = 0; @@ -4446,14 +4450,14 @@ localdecl(struct comp *cm, struct function *fn, bool forini) } decl.id = -1; if (!nerror) { - struct instr alloc = mkalloca(typesize(decl.ty), typealign(decl.ty)); + Instr alloc = mkalloca(typesize(decl.ty), typealign(decl.ty)); if (fn->curblk) decl.id = addinstr(fn, alloc).i; else decl.id = insertinstr(fn->entry, fn->entry->ins.n, alloc).i; } Initz: if (st.varini) { int d = putdecl(cm, &decl); - union type ty = decl.ty; + Type ty = decl.ty; bool statik = decl.scls & (SCSTATIC | SCEXTERN); ini = initializer(cm, &ty, statik ? EVSTATICINI : EVFOLD, /* globl? */ decl.scls == SCEXTERN, decl.qual, statik ? decl.sym : NULL); @@ -4466,7 +4470,7 @@ localdecl(struct comp *cm, struct function *fn, bool forini) EMITS instrtab[decl.id] = mkalloca(typesize(ty), typealign(ty)); if (!initcheck(ty, &ini)) { - struct span span = decl.span; + Span span = decl.span; joinspan(&span.ex, ini.span.ex); error(&span, "cannot initialize '%ty' variable with '%ty'", ty, ini.ty); @@ -4482,7 +4486,7 @@ localdecl(struct comp *cm, struct function *fn, bool forini) } } } else if (decl.scls == SCEXTERN) { - struct span span = decl.span; + Span span = decl.span; joinspan(&span.ex, ini.span.ex); error(&span, "block local 'extern' variable cannot have an initializer"); } @@ -4509,7 +4513,7 @@ localdecl(struct comp *cm, struct function *fn, bool forini) default: assert(0); } if (st.funcdef) { - struct span span = decl.span; + Span span = decl.span; joinspan(&span.ex, (peek(cm, &tk), tk.span.ex)); error(&span, "function definition not allowed here"); int bal = 1; @@ -4528,9 +4532,9 @@ localdecl(struct comp *cm, struct function *fn, bool forini) } static void -block(struct comp *cm, struct function *fn) +block(CComp *cm, Function *fn) { - struct token tk; + Token tk; while (!match(cm, &tk, '}')) { if (isdecltok(cm)) @@ -4542,19 +4546,19 @@ block(struct comp *cm, struct function *fn) } static void -function(struct comp *cm, struct function *fn, internstr *pnames, const struct span *pspans, uchar *pquals) +function(CComp *cm, Function *fn, internstr *pnames, const Span *pspans, uchar *pquals) { - const struct typedata *td = &typedata[fn->fnty.dat]; + const TypeData *td = &typedata[fn->fnty.dat]; const bool doemit = fn->curblk; - struct env e; - struct token tk; + Env e; + Token tk; envdown(cm, &e); /* emit Oparam instructions */ EMITS { for (int i = 0; i < td->nmemb; ++i) { - union irtype pty = mkirtype(td->param[i]); - union ref r = addinstr(fn, mkinstr(Oparam, pty.isagg ? KPTR : pty.cls, + IRType pty = mkirtype(td->param[i]); + Ref r = addinstr(fn, mkinstr(Oparam, pty.isagg ? KPTR : pty.cls, mkref(RICON, i), mktyperef(pty))); assert(r.t == RTMP && r.i == i); } @@ -4562,7 +4566,7 @@ function(struct comp *cm, struct function *fn, internstr *pnames, const struct s /* add parameters to symbol table and create prologue (arguments) block */ for (int i = 0; i < td->nmemb; ++i) { if (pnames[i]) { - struct decl arg = { .ty = td->param[i], .qual = pquals ? pquals[i] : 0, + Decl arg = { .ty = td->param[i], .qual = pquals ? pquals[i] : 0, .name = pnames[i], .scls = SCAUTO, .span = pspans[i] }; EMITS { if (isscalar(arg.ty)) { @@ -4579,7 +4583,7 @@ function(struct comp *cm, struct function *fn, internstr *pnames, const struct s } /* put __func__, though its data is generated lazily the first time it is encountered */ - putdecl(cm, &(struct decl) { + putdecl(cm, &(Decl) { .ty = mkarrtype(mktype(TYCHAR), QCONST, strlen(&fn->name->c) + 1), .qual = QCONST, .name = istr__func__, .scls = SCSTATIC, .span = (peek(cm, &tk), tk.span), .isbuiltin = 1, .sym = fn->name, @@ -4587,14 +4591,14 @@ function(struct comp *cm, struct function *fn, internstr *pnames, const struct s /* end prologue */ EMITS { - struct block *blk; + Block *blk; putbranch(fn, blk = newblk(fn)); useblk(fn, blk); } cm->labels = NULL; block(cm, fn); envup(cm); - for (struct label *l = cm->labels; l; l = l->link) { + for (Label *l = cm->labels; l; l = l->link) { if (l->usespan.ex.len) { error(&l->usespan, "label '%s' used but never defined", l->name); } @@ -4618,13 +4622,13 @@ function(struct comp *cm, struct function *fn, internstr *pnames, const struct s /* top-level declaration */ static void -tldecl(struct comp *cm) +tldecl(CComp *cm) { - struct declstate st = { DTOPLEVEL }; + DeclState st = { DTOPLEVEL }; do { bool noscls = 0; int nerr = nerror; - struct decl decl = pdecl(&st, cm); + Decl decl = pdecl(&st, cm); if (nerror != nerr && st.varini) { (void)expr(cm); @@ -4639,7 +4643,7 @@ tldecl(struct comp *cm) if (!decl.sym) decl.sym = decl.name; decl.isdef = st.varini; if (st.funcdef) { - const struct typedata *td = &typedata[decl.ty.dat]; + const TypeData *td = &typedata[decl.ty.dat]; if (td->ret.t != TYVOID && isincomplete(td->ret)) error(&decl.span, "function definition with incomplete return type '%ty'", td->ret); for (int i = 0; i < td->nmemb; ++i) { @@ -4648,9 +4652,9 @@ tldecl(struct comp *cm) } decl.isdef = 1; int idecl = putdecl(cm, &decl); - struct decl *d = &declsbuf.p[idecl]; + Decl *d = &declsbuf.p[idecl]; if (d->inlin && decl.scls != SCSTATIC) fatal(&d->span, "non-static inline is unimplemented"); - struct function fn = { &cm->fnarena, .name = d->sym, .globl = d->scls != SCSTATIC, .fnty = decl.ty, .retty = td->ret, .inlin = d->inlin }; + Function fn = { &cm->fnarena, .name = d->sym, .globl = d->scls != SCSTATIC, .fnty = decl.ty, .retty = td->ret, .inlin = d->inlin }; irinit(&fn); function(cm, &fn, st.pnames, st.pspans, st.pqual); if (!nerror && ccopt.dbg.p) @@ -4658,15 +4662,15 @@ tldecl(struct comp *cm) irfini(&fn); } else if (decl.name) { int idecl = putdecl(cm, &decl); - struct decl *d = &declsbuf.p[idecl]; + Decl *d = &declsbuf.p[idecl]; if (st.varini) { if (isagg(decl.ty) && isincomplete(decl.ty)) error(&decl.span, "initialization of variable with incomplete type '%ty'", decl.ty); - struct expr ini = initializer(cm, &decl.ty, EVSTATICINI, d->scls != SCSTATIC, d->qual, d->sym); + Expr ini = initializer(cm, &decl.ty, EVSTATICINI, d->scls != SCSTATIC, d->qual, d->sym); d = &declsbuf.p[idecl]; d->ty = decl.ty; if (d->scls == SCEXTERN && !noscls) { - struct span span = decl.span; + Span span = decl.span; joinspan(&span.ex, ini.span.ex); warn(&span, "'extern' variable has initializer"); } @@ -4698,12 +4702,12 @@ tldecl(struct comp *cm) } while (st.more); } -union type cvalistty; +Type cvalistty; void -docomp(struct comp *cm) +docomp(CComp *cm) { - static struct env toplevel; - struct token tk[1]; + static Env toplevel; + Token tk[1]; istr__func__ = intern("__func__"); istr_main = intern("main"); @@ -4714,14 +4718,14 @@ docomp(struct comp *cm) cm->env = &toplevel; } if (!cvalistty.t) { - struct typedata td = { + TypeData td = { .t = TYSTRUCT, .siz = targ_valistsize, .align = targ_primalign[TYPTR], .nmemb = 1, - .fld = &(struct namedfield){intern("-"), {mkarrtype(mktype(TYPTR), 0, 3)}} + .fld = &(NamedField){intern("-"), {mkarrtype(mktype(TYPTR), 0, 3)}} }; cvalistty = mkarrtype(mktagtype(intern("__builtin_va_list"), &td), 0, 1); } peek(cm, tk); - envadddecl(cm->env, &(struct decl) { cvalistty, SCTYPEDEF, .span = tk->span, .name = intern("__builtin_va_list") }); + envadddecl(cm->env, &(Decl) { cvalistty, SCTYPEDEF, .span = tk->span, .name = intern("__builtin_va_list") }); putbuiltins(cm->env); while (peek(cm, tk) != TKEOF) { @@ -4737,10 +4741,10 @@ docomp(struct comp *cm) } static void -initcm(struct comp *cm, const char *file) +initcm(CComp *cm, const char *file) { enum { N = 1<<12 }; - static union { char m[sizeof(struct arena) + N]; struct arena *_align; } amem[2]; + static union { char m[sizeof(Arena) + N]; Arena *_align; } amem[2]; const char *err; switch (initlexer(cm->lx, &err, file)) { default: assert(0); @@ -4757,15 +4761,15 @@ initcm(struct comp *cm, const char *file) void ccomp(const char *file) { - struct comp cm = {&(struct lexer){0}}; + CComp cm = {&(Lexer){0}}; initcm(&cm, file); docomp(&cm); } void -cpp(struct wbuf *out, const char *file) +cpp(WriteBuf *out, const char *file) { - struct comp cm = {&(struct lexer){0}}; + CComp cm = {&(Lexer){0}}; initcm(&cm, file); lexerdump(cm.lx, out); } @@ -1,10 +1,6 @@ #include "antcc.h" #include "c_type.h" -/*************/ -/* EXPR TREE */ -/*************/ - enum exprkind { EXXX, ENUMLIT, ESTRLIT, ESYM, ESSYMREF, EVAARG, EINIT, EGETF, ECALL, ECOND, /* unary */ @@ -22,16 +18,20 @@ enum exprkind { #define isassign(t) in_range(t, ESET, ESETSHR) #define assigntobinop(t) ((t) - ESETADD + EADD) -struct expr { +typedef struct Expr Expr; +typedef struct ExprGetFld ExprGetFld; +typedef struct Init Init; +typedef struct InitElem InitElem; +struct Expr { uchar t; uchar qual; ushort narg; /* ECALL */ - union type ty; - struct span span; + Type ty; + Span span; union { struct { - struct expr *sub; /* child(ren) */ - struct exgetfld { + Expr *sub; /* child(ren) */ + struct ExprGetFld { ushort off; uchar bitsiz, bitoff; } fld; /* EGETF */ @@ -52,31 +52,34 @@ struct expr { int off; bool func : 1, local : 1; } ssym; /* ESSYMREF (static symbol addr + off) */ - struct init *init; /* EINIT */ + Init *init; /* EINIT */ }; }; -struct init { - struct bitset zero[BSSIZE(64)]; /* bytes to zero out up to 64 */ - struct initval { - struct initval *next; +struct Init { + BitSet zero[BSSIZE(64)]; /* bytes to zero out up to 64 */ + struct InitElem { + InitElem *next; uint off; uchar bitoff, bitsiz; - struct expr ex; + Expr ex; } *vals, **tail; }; +typedef struct Env Env; +typedef struct Builtin Builtin; + /** C compiler state **/ -struct comp { - struct lexer *lx; - struct env *env; - struct arena *fnarena, *exarena; - struct span fnblkspan; +typedef struct { + struct Lexer *lx; + Env *env; + Arena *fnarena, *exarena; + Span fnblkspan; uint loopdepth, switchdepth; - struct block *breakto, *loopcont; - struct switchstmt *switchstmt; - struct label *labels; -}; + struct Block *breakto, *loopcont; + struct SwitchStmt *switchstmt; + struct Label *labels; +} CComp; enum storageclass { SCNONE, @@ -88,8 +91,8 @@ enum storageclass { SCREGISTER = 1<<5, }; -struct decl { - union type ty; +typedef struct Decl { + Type ty; uchar scls; uchar qual : 2, noret : 1, @@ -97,35 +100,36 @@ struct decl { isenum : 1, isdef : 1, isbuiltin : 1; - struct span span; + Span span; internstr name; union { - internstr sym; - struct { ushort align; int id; }; - vlong value; - const struct builtin *builtin; + internstr sym; /* static/extern scls */ + struct { ushort align; int id; }; /* local var */ + vlong value; /* enum constant */ + Builtin *builtin; /* .isbuiltin */ }; -}; +} Decl; -extern struct envdecls {vec_of(struct decl);} declsbuf; -extern union type cvalistty; -struct function; -int envadddecl(struct env *env, const struct decl *d); -bool assigncheck(union type t, const struct expr *src); -union ref expraddr(struct function *, const struct expr *); -union ref scalarcvt(struct function *, union type to, union type from, union ref); -union ref compileexpr(struct function *, const struct expr *, bool discard); -void dumpexpr(const struct expr *, bool printtypes); +/** c.c **/ +extern struct declsbuf {vec_of(Decl);} declsbuf; +extern Type cvalistty; +int envadddecl(Env *env, const Decl *d); +bool assigncheck(Type t, const Expr *src); +struct Function; +union Ref expraddr(struct Function *, const Expr *); +union Ref scalarcvt(struct Function *, Type to, Type from, union Ref); +union Ref compileexpr(struct Function *, const Expr *, bool discard); +void dumpexpr(const Expr *, bool printtypes); -/** builtin.c **/ -struct builtin { - bool (*sema)(struct comp *, struct expr *); - union ref (*comp)(struct function *, struct expr *, bool discard); +struct Builtin { + bool (*sema)(CComp *, Expr *); + union Ref (*comp)(struct Function *, Expr *, bool discard); }; -void putbuiltins(struct env *); -union ref builtin_va_arg_comp(struct function *, const struct expr *, bool discard); -/** eval.c **/ +/** builtin.c **/ +void putbuiltins(Env *); +union Ref builtin_va_arg_comp(struct Function *, const Expr *, bool discard); + enum evalmode { EVNONE, EVINTCONST, @@ -134,6 +138,7 @@ enum evalmode { EVFOLD, }; -bool eval(struct expr *, enum evalmode); +/** eval.c **/ +bool eval(Expr *, enum evalmode); /* vim:set ts=3 sw=3 expandtab: */ diff --git a/src/c_builtin.c b/src/c_builtin.c index ddcfa82..a8d4f4f 100644 --- a/src/c_builtin.c +++ b/src/c_builtin.c @@ -2,7 +2,7 @@ #include "ir.h" static bool -callcheck(const struct span *span, int nparam, const union type *param, int narg, struct expr *args) +callcheck(const Span *span, int nparam, const Type *param, int narg, Expr *args) { bool ok = 1; for (int i = 0, n = narg < nparam ? narg : nparam; i < n; ++i) { @@ -26,21 +26,21 @@ callcheck(const struct span *span, int nparam, const union type *param, int narg #define DEF_FNLIKE_SEMA(name, retty, ...) \ static bool \ - name##_sema(struct comp *cm, struct expr *ex) { \ - union type par[] = { {{0}}, __VA_ARGS__ }; \ + name##_sema(CComp *cm, Expr *ex) { \ + Type par[] = { {{0}}, __VA_ARGS__ }; \ ex->ty = retty; \ return callcheck(&ex->span, countof(par)-1, par+1, ex->narg, ex->sub+1); \ } /* __builtin_va_start */ static bool -va_start_sema(struct comp *cm, struct expr *ex) +va_start_sema(CComp *cm, Expr *ex) { ex->ty = mktype(TYVOID); return callcheck(&ex->span, 1, &cvalistty, ex->narg, ex->sub+1); } -static union ref -va_start_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +va_start_comp(Function *fn, Expr *ex, bool discard) { assert(ex->t == ECALL && ex->narg == 1); assert(typedecay(ex->sub[1].ty).bits == typedecay(cvalistty).bits); @@ -52,27 +52,27 @@ va_start_comp(struct function *fn, struct expr *ex, bool discard) /* __builtin_va_end */ static bool -va_end_sema(struct comp *cm, struct expr *ex) +va_end_sema(CComp *cm, Expr *ex) { ex->ty = mktype(TYVOID); return callcheck(&ex->span, 1, &cvalistty, ex->narg, ex->sub+1); } -static union ref -va_end_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +va_end_comp(Function *fn, Expr *ex, bool discard) { return NOREF; } /* __builtin_va_copy */ DEF_FNLIKE_SEMA(va_copy, mktype(TYVOID), cvalistty, cvalistty) -static union ref -va_copy_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +va_copy_comp(Function *fn, Expr *ex, bool discard) { - union irtype typ = mkirtype(cvalistty.t == TYARRAY ? typechild(cvalistty) : cvalistty); + IRType typ = mkirtype(cvalistty.t == TYARRAY ? typechild(cvalistty) : cvalistty); for (int i = 1; i <= 2; ++i) assert(typedecay(ex->sub[i].ty).bits == typedecay(cvalistty).bits); - union ref dst = compileexpr(fn, &ex->sub[1], 0), src = compileexpr(fn, &ex->sub[2], 0); + Ref dst = compileexpr(fn, &ex->sub[1], 0), src = compileexpr(fn, &ex->sub[2], 0); addinstr(fn, mkarginstr(typ, dst)); addinstr(fn, mkarginstr(typ, src)); addinstr(fn, mkintrin(INstructcopy, 0, 2)); @@ -81,16 +81,16 @@ va_copy_comp(struct function *fn, struct expr *ex, bool discard) /* __builtin_trap */ DEF_FNLIKE_SEMA(trap, mktype(TYVOID), ) -static union ref -trap_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +trap_comp(Function *fn, Expr *ex, bool discard) { puttrap(fn); useblk(fn, newblk(fn)); /* unreachable block, but simplifies expr codegen */ return NOREF; } -static inline union ref -cvtintref(struct function *fn, enum irclass dst, union ref src) +static inline Ref +cvtintref(Function *fn, enum irclass dst, Ref src) { if (src.t == RTMP) { if (insrescls(instrtab[src.i]) != dst) @@ -105,8 +105,8 @@ cvtintref(struct function *fn, enum irclass dst, union ref src) /* __builtin_bswap16 */ DEF_FNLIKE_SEMA(bswap16, mktype(TYUSHORT), mktype(TYUSHORT)) -static union ref -bswap16_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +bswap16_comp(Function *fn, Expr *ex, bool discard) { assert(isint(ex->ty)); return irunop(fn, Obswap16, KI32, scalarcvt(fn, ex->ty, ex->sub[1].ty, @@ -114,8 +114,8 @@ bswap16_comp(struct function *fn, struct expr *ex, bool discard) } /* __builtin_bswap32 */ DEF_FNLIKE_SEMA(bswap32, mktype(TYUINT), mktype(TYUINT)) -static union ref -bswap32_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +bswap32_comp(Function *fn, Expr *ex, bool discard) { assert(isint(ex->ty)); return irunop(fn, Obswap32, KI32, scalarcvt(fn, ex->ty, ex->sub[1].ty, @@ -123,8 +123,8 @@ bswap32_comp(struct function *fn, struct expr *ex, bool discard) } /* __builtin_bswap64 */ DEF_FNLIKE_SEMA(bswap64, mktype(TYUVLONG), mktype(TYUVLONG)) -static union ref -bswap64_comp(struct function *fn, struct expr *ex, bool discard) +static Ref +bswap64_comp(Function *fn, Expr *ex, bool discard) { assert(isint(ex->ty)); return irunop(fn, Obswap64, KI64, scalarcvt(fn, ex->ty, ex->sub[1].ty, @@ -137,7 +137,7 @@ bswap64_comp(struct function *fn, struct expr *ex, bool discard) static const struct { const char *name; - struct builtin b; + Builtin b; } tab[] = { #define FNS(x) { "__builtin_" #x, { x##_sema, x##_comp } }, LIST_BUILTINS(FNS) @@ -145,10 +145,10 @@ static const struct { }; void -putbuiltins(struct env *env) +putbuiltins(Env *env) { for (int i = 0; i < countof(tab); ++i) { - envadddecl(env, &(struct decl) { + envadddecl(env, &(Decl) { .name = intern(tab[i].name), .isbuiltin = 1, .builtin = &tab[i].b, @@ -157,8 +157,8 @@ putbuiltins(struct env *env) } /* this is separate because it's a keyword */ -union ref -builtin_va_arg_comp(struct function *fn, const struct expr *ex, bool discard) +Ref +builtin_va_arg_comp(Function *fn, const Expr *ex, bool discard) { assert(ex->t == EVAARG && ex->ty.t); enum irclass k = isagg(ex->ty) ? KPTR : type2cls[scalartypet(ex->ty)]; diff --git a/src/c_eval.c b/src/c_eval.c index ad5cf40..eff7e94 100644 --- a/src/c_eval.c +++ b/src/c_eval.c @@ -27,12 +27,12 @@ targ2hosttype(enum typetag t) } static bool -numcast(union type ty, struct expr *dst, const struct expr *src) +numcast(Type ty, Expr *dst, const Expr *src) { enum typetag td = targ2hosttype(scalartypet(ty)), ts = targ2hosttype(scalartypet(src->ty)); vlong isrc; - struct expr tmp; + Expr tmp; if (src == dst) tmp = *src, src = &tmp; assert(src->t == ENUMLIT); @@ -78,8 +78,8 @@ numcast(union type ty, struct expr *dst, const struct expr *src) return 1; } -static struct expr * -lit2ssym(struct expr *ex) +static Expr * +lit2ssym(Expr *ex) { ex->ssym.sym = xcon2sym(expraddr(NULL, ex).i); ex->ssym.local = 1; @@ -88,12 +88,12 @@ lit2ssym(struct expr *ex) return ex; } -static struct expr -staticaddrof(struct expr *ex, enum evalmode mode) +static Expr +staticaddrof(Expr *ex, enum evalmode mode) { - struct expr ret = { .ty = mkptrtype(ex->ty, ex->qual), .span = ex->span }; + Expr ret = { .ty = mkptrtype(ex->ty, ex->qual), .span = ex->span }; if (ex->t == ESYM && ex->ty.t < NTYPETAG) { - const struct decl *decl = &declsbuf.p[ex->decl]; + const Decl *decl = &declsbuf.p[ex->decl]; if (decl->sym && (decl->scls & (SCAUTO|SCREGISTER)) == 0) { ret.t = ESSYMREF; ret.ssym.sym = decl->sym; @@ -119,13 +119,13 @@ staticaddrof(struct expr *ex, enum evalmode mode) } static bool -isstaticlval(const struct expr *ex, enum evalmode mode) +isstaticlval(const Expr *ex, enum evalmode mode) { return ex->t == ESTRLIT || ex->t == ESSYMREF || (mode == EVSTATICINI && ex->t == EINIT); } static bool -truthy(const struct expr *ex) +truthy(const Expr *ex) { switch (ex->t) { default: assert(0 && "!scalar?"); @@ -137,9 +137,9 @@ truthy(const struct expr *ex) } static bool -unop(struct expr *ex, enum evalmode mode) +unop(Expr *ex, enum evalmode mode) { - struct expr *sub = ex->sub; + Expr *sub = ex->sub; if (mode >= EVSTATICINI && ex->t == EDEREF) { uvlong off; @@ -178,7 +178,7 @@ unop(struct expr *ex, enum evalmode mode) } else return 0; } else if (ex->t == EADDROF) { assert(ex->ty.t == TYPTR); - struct expr ex2 = staticaddrof(ex->sub, mode); + Expr ex2 = staticaddrof(ex->sub, mode); if (!ex2.t) return 0; ex2.span = ex->span; ex2.ty = ex->ty; @@ -187,7 +187,7 @@ unop(struct expr *ex, enum evalmode mode) } else if (ex->t == EGETF && !ex->fld.bitsiz) { /* <lvalue>.memb -> is an address lvalue if 'memb' is of array type */ if (ex->ty.t == TYARRAY) { - struct expr ex2; + Expr ex2; if ((ex2 = staticaddrof(ex->sub, mode)).t) { if (ex2.t == ENUMLIT) { ex->t = ENUMLIT; @@ -247,11 +247,11 @@ unop(struct expr *ex, enum evalmode mode) } static bool -binop(struct expr *ex, enum evalmode mode) +binop(Expr *ex, enum evalmode mode) { - struct expr *a = &ex->sub[0], *b = &ex->sub[1]; + Expr *a = &ex->sub[0], *b = &ex->sub[1]; if (!eval(a, mode)) return 0; - union type opty; + Type opty; if (in_range(ex->t, EADD, ESHR)) opty = ex->ty; else /* compare, logical, set (result type != operation type) */ @@ -393,7 +393,7 @@ binop(struct expr *ex, enum evalmode mode) } bool -eval(struct expr *ex, enum evalmode mode) +eval(Expr *ex, enum evalmode mode) { switch (ex->t) { case EGETF: goto Unop; @@ -406,7 +406,7 @@ eval(struct expr *ex, enum evalmode mode) *ex = ex->sub[2-truthy(&ex->sub[0])]; return eval(ex, mode); case EINIT: - for (struct initval *v = ex->init->vals; v; v = v->next) { + for (InitElem *v = ex->init->vals; v; v = v->next) { if (!eval(&v->ex, mode)) return 0; } return 1; @@ -416,9 +416,9 @@ eval(struct expr *ex, enum evalmode mode) case ESYM: if (in_range(ex->ty.t, TYARRAY, TYFUNC) && mode >= EVSTATICINI) { - struct expr ex2 = staticaddrof(ex, mode); + Expr ex2 = staticaddrof(ex, mode); if (ex2.t) { - union type ty = ex->ty; + Type ty = ex->ty; *ex = ex2; ex->ty = ty; return 1; diff --git a/src/c_lex.c b/src/c_lex.c index 8b707fe..826b9a4 100644 --- a/src/c_lex.c +++ b/src/c_lex.c @@ -7,7 +7,7 @@ /* fill internal circular character buffer with input after translation phase 1 & 2 * (trigraph substitution and backslash-newline deletion */ static void -fillchrbuf(struct lexer *lx) +fillchrbuf(Lexer *lx) { const uchar *p = lx->dat + lx->idx; int i = lx->chrbuf0, idx = lx->idx; @@ -72,7 +72,7 @@ fillchrbuf(struct lexer *lx) } static uchar -next(struct lexer *lx) +next(Lexer *lx) { if (lx->chrbuf0 >= countof(lx->chrbuf)) fillchrbuf(lx); @@ -84,7 +84,7 @@ next(struct lexer *lx) } static uchar -peek(struct lexer *lx, int off) +peek(Lexer *lx, int off) { assert(off < countof(lx->chrbuf)); if (lx->chrbuf0 + off >= countof(lx->chrbuf)) @@ -93,7 +93,7 @@ peek(struct lexer *lx, int off) } static bool -match(struct lexer *lx, uchar c) +match(Lexer *lx, uchar c) { if (!lx->eof && peek(lx, 0) == c) { next(lx); @@ -120,7 +120,7 @@ aissep(int c) { } enum typetag -parsenumlit(uvlong *outi, double *outf, const struct token *tk, bool ispp) +parsenumlit(uvlong *outi, double *outf, const Token *tk, bool ispp) { if (tk->t == TKCHRLIT) { uvlong n = 0; @@ -240,12 +240,12 @@ parsenumlit(uvlong *outi, double *outf, const struct token *tk, bool ispp) } static void -readstrchrlit(struct lexer *lx, struct token *tk, char delim, int wide) +readstrchrlit(Lexer *lx, Token *tk, char delim, int wide) { int c, i; uchar tmp[200]; vec_of(uchar) b = VINIT(tmp, sizeof tmp); - struct span span = {0}; + Span span = {0}; uint n, beginoff, idx; beginoff = idx = lx->chridx; @@ -253,11 +253,11 @@ readstrchrlit(struct lexer *lx, struct token *tk, char delim, int wide) static uint wmax[] = {0xFF, 0xFFFF, 0xFFFFFFFFu}; if (c == '\n' || c == TKEOF) { Noterm: - span.sl = (struct span0) { idx, lx->chridx - idx, lx->fileid }; + span.sl = (Span0) { idx, lx->chridx - idx, lx->fileid }; error(&span, "missing terminating %c character", delim); break; } else if (c == '\\') { - span.sl = (struct span0) { idx, lx->chridx - idx, lx->fileid }; + span.sl = (Span0) { idx, lx->chridx - idx, lx->fileid }; switch (c = next(lx)) { case '\n': case TKEOF: goto Noterm; @@ -336,10 +336,10 @@ readstrchrlit(struct lexer *lx, struct token *tk, char delim, int wide) } } else { if (b.n == 0) { - span.sl = (struct span0) { idx, lx->chridx - idx, lx->fileid }; + span.sl = (Span0) { idx, lx->chridx - idx, lx->fileid }; error(&span, "empty character literal"); } else if (b.n > targ_primsizes[TYINT]) { - span.sl = (struct span0) { idx, lx->chridx - idx, lx->fileid }; + span.sl = (Span0) { idx, lx->chridx - idx, lx->fileid }; error(&span, "multicharacter literal too long"); } tk->t = TKCHRLIT; @@ -363,18 +363,18 @@ readstrchrlit(struct lexer *lx, struct token *tk, char delim, int wide) /* for #include directive, read "header" or <header> */ static void -readheadername(struct lexer *lx, struct token *tk, char delim) +readheadername(Lexer *lx, Token *tk, char delim) { int c; uchar tmp[200]; vec_of(uchar) b = VINIT(tmp, sizeof tmp); - struct span span = {0}; + Span span = {0}; uint beginoff, idx; beginoff = idx = lx->chridx; while ((c = next(lx)) != delim) { if (c == '\n' || lx->eof) { - span.sl = (struct span0) { idx, lx->chridx - idx, lx->fileid }; + span.sl = (Span0) { idx, lx->chridx - idx, lx->fileid }; error(&span, "missing terminating %c character", delim); break; } @@ -407,7 +407,7 @@ isppnum(char prev, char c) enum { MAXLITLEN = 256 }; /* maximum length of num literals and identifiers */ static int -lex0(struct lexer *lx, struct token *tk, bool includeheader) +lex0(Lexer *lx, Token *tk, bool includeheader) { int idx,q; bool space = 0; @@ -491,7 +491,7 @@ Begin: fillchrbuf(lx); lx->chridx = lx->chridxbuf[lx->chrbuf0]; if ((lx->eof = (lx->chridx >= lx->ndat))) { - struct span span = {{ idx, lx->chridx - idx, lx->fileid }}; + Span span = {{ idx, lx->chridx - idx, lx->fileid }}; fatal(&span, "unterminated comment"); } } @@ -559,7 +559,7 @@ Begin: if (n >= MAXLITLEN) { lx->chridx = lx->chridxbuf[lx->chrbuf0+n-1]; TooLong: - fatal(&(struct span) {{ idx, lx->chridx - idx, lx->fileid }}, + fatal(&(Span) {{ idx, lx->chridx - idx, lx->fileid }}, "token is too long"); } } @@ -595,7 +595,7 @@ Begin: case 0: if (lx->idx >= lx->ndat) RET(TKEOF); #undef TK2 } - fatal(&(struct span) {{ idx, lx->chridx - idx, lx->fileid }}, + fatal(&(Span) {{ idx, lx->chridx - idx, lx->fileid }}, "unexpected character %'c at %d (%d)", c, idx, lx->idx); End: tk->space = space; @@ -612,7 +612,7 @@ End: /****************/ static bool -tokequ(const struct token *a, const struct token *b) +tokequ(const Token *a, const Token *b) { if (a->t != b->t) return 0; if (a->t == TKNUMLIT || a->t == TKSTRLIT || a->t == TKCHRLIT) { @@ -626,12 +626,13 @@ tokequ(const struct token *a, const struct token *b) return 1; } -static vec_of(struct token) mtoksbuf, /* buffers for macro replacement list tokens */ - mdyntoksbuf; /* for function-like macros after parameter substitution */ +static vec_of(Token) mtoksbuf, /* buffers for macro replacement list tokens */ + mdyntoksbuf; /* for function-like macros after parameter substitution */ -struct macro { +typedef struct TokList TokList; +typedef struct Macro { internstr *param; - struct span0 span; + Span0 span; uchar nparam; bool predef : 1, special : 1, @@ -639,18 +640,18 @@ struct macro { variadic : 1; short id; union { - void (*handler)(struct lexer *, struct token *); - struct rlist { + void (*handler)(Lexer *, Token *); + struct TokList { uint off; /* mtoksbuf[] */ int n; } rl; - const struct token *single; /* predef */ - void (*handlerfn)(struct lexer *, struct token *ret, const struct token *arg, int narg); + const Token *single; /* predef */ + void (*handlerfn)(Lexer *, Token *ret, const Token *arg, int narg); }; -}; +} Macro; static bool -macroequ(const struct macro *a, const struct macro *b) +macroequ(const Macro *a, const Macro *b) { if (a->special != b->special) return 0; if (a->fnlike != b->fnlike || a->variadic != b->variadic) return 0; @@ -662,7 +663,7 @@ macroequ(const struct macro *a, const struct macro *b) } if (a->special) return a->handler == b->handler; if (a->rl.n != b->rl.n) return 0; - const struct token *tka = &mtoksbuf.p[a->rl.off], *tkb = &mtoksbuf.p[b->rl.off]; + const Token *tka = &mtoksbuf.p[a->rl.off], *tkb = &mtoksbuf.p[b->rl.off]; for (int i = 0; i < a->rl.n; ++i) { if (!tokequ(&tka[i], &tkb[i])) return 0; @@ -673,28 +674,28 @@ macroequ(const struct macro *a, const struct macro *b) } static void -freemac(struct macro *mac) +freemac(Macro *mac) { if (mac->special) return; free(mac->param); } -static pmap_of(struct macro) macroht; +static pmap_of(Macro) macroht; static void -putmac(internstr name, struct macro *mac) +putmac(internstr name, Macro *mac) { static short id; if (!macroht.v) pmap_init(¯oht, 1<<10); - struct macro *slot = pmap_get(¯oht, name); + Macro *slot = pmap_get(¯oht, name); mac->id = id++; if (slot) { if (!macroequ(slot, mac)) { if (slot->predef) - warn(&(struct span){mac->span}, "redefining builtin macro"); + warn(&(Span){mac->span}, "redefining builtin macro"); else { - warn(&(struct span){mac->span}, "redefining macro"); - note(&(struct span){slot->span}, "previous definition:"); + warn(&(Span){mac->span}, "redefining macro"); + note(&(Span){slot->span}, "previous definition:"); } freemac(slot); *slot = *mac; @@ -709,42 +710,43 @@ putmac(internstr name, struct macro *mac) static void delmac(internstr name) { - struct macro *slot = pmap_get(¯oht, name); + Macro *slot = pmap_get(¯oht, name); if (!slot) return; freemac(slot); pmap_del(¯oht, name); } static inline internstr -macname(struct macro *mac) +macname(Macro *mac) { return macroht.mb.k[mac - macroht.v]; } -static inline struct macro * +static inline Macro * findmac(internstr name) { return pmap_get(¯oht, name); } -static void popmac(struct lexer *, bool all); +static void popmac(Lexer *, bool all); -static struct macrostack { +typedef struct MacroStack MacroStack; +static struct MacroStack { struct { union { uint off; /* mtoksbuf[]/mdyntoksbuf[] */ - const struct token *p; + const Token *p; }; int n; } rl; - struct span0 exspan; + Span0 exspan; int idx; short macid; /* -1 for argument undergoing expansion */ bool space : 1, stop : 1, dyn; } mstk[1200]; static void NORETURN -lxfatal(struct lexer *lx, const struct span *span, const char *fmt, ...) +lxfatal(Lexer *lx, const Span *span, const char *fmt, ...) { if (fmt) { va_list ap; @@ -753,14 +755,14 @@ lxfatal(struct lexer *lx, const struct span *span, const char *fmt, ...) va_end(ap); } int n = lx->macstk ? lx->macstk - mstk : 0, i = 0; - for (struct macrostack *l = lx->macstk; l && l > mstk; --l, ++i) { + for (MacroStack *l = lx->macstk; l && l > mstk; --l, ++i) { if (i < 4 || i > n - 5) { - note(&(struct span){l->exspan}, "expanded from here"); + note(&(Span){l->exspan}, "expanded from here"); } else if (i == 5) { efmt(" (...) \n"); } } - for (struct lexer *sv = lx->save; sv; sv = sv->save) { + for (Lexer *sv = lx->save; sv; sv = sv->save) { int line; const char *f = getfilepos(&line, NULL, sv->fileid, sv->chridx-2); note(NULL, "in file included from %s:%d", f, line); @@ -770,7 +772,7 @@ lxfatal(struct lexer *lx, const struct span *span, const char *fmt, ...) } static void -ppskipline(struct lexer *lx) +ppskipline(Lexer *lx) { while (lx->macstk) popmac(lx, 1); for (int c; (c = peek(lx, 0)) != '\n' && !lx->eof; next(lx)) { @@ -779,7 +781,7 @@ ppskipline(struct lexer *lx) bool done = 0; while (!((c = peek(lx, 0)) == '*' && peek(lx, 1) == '/')) { if (lx->eof) { - struct span span = {{ lx->idx, lx->chridx - lx->idx, lx->fileid }}; + Span span = {{ lx->idx, lx->chridx - lx->idx, lx->fileid }}; lxfatal(lx, &span, "unterminated comment"); } done = c == '\n'; @@ -794,7 +796,7 @@ ppskipline(struct lexer *lx) #define isppident(tk) in_range((tk).t, TKIDENT, TKWEND_) static bool -tokpaste(struct lexer *lx, struct token *dst, const struct token *l, const struct token *r) +tokpaste(Lexer *lx, Token *dst, const Token *l, const Token *r) { int t; if (isppident(*l) && (isppident(*r) || r->t == TKNUMLIT)) { @@ -857,12 +859,12 @@ tokpaste(struct lexer *lx, struct token *dst, const struct token *l, const struc enum { MAXMACROARGS = 128 }; static void -ppdefine(struct lexer *lx) +ppdefine(Lexer *lx) { - struct token tk0, tk; + Token tk0, tk; internstr mname; - struct macro mac = {0}; - struct bitset usedparams[BSSIZE(MAXMACROARGS)] = {0}; + Macro mac = {0}; + BitSet usedparams[BSSIZE(MAXMACROARGS)] = {0}; lex0(lx, &tk0, 0); if (tk0.t != TKIDENT) { @@ -916,7 +918,7 @@ ppdefine(struct lexer *lx) for (int n = 0; lex0(lx, &tk, 0) != '\n' && tk.t != TKEOF;) { if (n == 0 && !tk.space) warn(&tk.span, "no whitespace after macro name"); - struct token *prev = n ? &mtoksbuf.p[mtoksbuf.n-1] : NULL; + Token *prev = n ? &mtoksbuf.p[mtoksbuf.n-1] : NULL; if (mac.fnlike && tk.t == TKIDENT) { for (int i = 0; i < mac.nparam; ++i) { if (tk.name == mac.param[i]) { @@ -934,7 +936,7 @@ ppdefine(struct lexer *lx) } } if (n > 1 && prev->t == TKPPCAT) { - struct token new; + Token new; if (prev[-1].t != TKPPMACARG && tk.t != TKPPMACARG && tokpaste(lx, &new, &prev[-1], &tk)) { @@ -961,9 +963,9 @@ ppdefine(struct lexer *lx) } static void -expecteol(struct lexer *lx, const char *ppname) +expecteol(Lexer *lx, const char *ppname) { - struct token tk; + Token tk; assert(!lx->macstk); if (lex0(lx, &tk, 0) != '\n' && tk.t != TKEOF) { (ccopt.pedant ? error : warn)(&tk.span, "extra tokens after #%s", ppname); @@ -971,9 +973,9 @@ expecteol(struct lexer *lx, const char *ppname) } } static void -ppundef(struct lexer *lx) +ppundef(Lexer *lx) { - struct token tk; + Token tk; lex0(lx, &tk, 0); if (tk.t != TKIDENT) { @@ -986,9 +988,9 @@ ppundef(struct lexer *lx) } static void -pushmacstk(struct lexer *lx, const struct span *span, const struct macrostack *m) +pushmacstk(Lexer *lx, const Span *span, const MacroStack *m) { - struct macrostack *l = lx->macstk; + MacroStack *l = lx->macstk; if (!l) l = mstk; else if ((++l == mstk+countof(mstk))) lxfatal(lx, span, "macro expansion depth limit reached"); *l = *m; @@ -998,9 +1000,9 @@ pushmacstk(struct lexer *lx, const struct span *span, const struct macrostack *m } static void -popmac(struct lexer *lx, bool all) +popmac(Lexer *lx, bool all) { - struct macrostack *stk; + MacroStack *stk; assert(stk = lx->macstk); do { @@ -1013,46 +1015,46 @@ popmac(struct lexer *lx, bool all) } -static inline const struct token * -stkgetrl(struct macrostack *s) +static inline const Token * +stkgetrl(MacroStack *s) { if (s->macid < 0) return s->rl.p; return (s->dyn ? mdyntoksbuf.p : mtoksbuf.p) + s->rl.off; } -static void expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro *mac); +static void expandfnmacro(Lexer *lx, Span *span, internstr mname, Macro *mac); static enum expandres { EXPNONE, EXPINL, EXPSTACK } -tryexpand(struct lexer *lx, struct token *tk) +tryexpand(Lexer *lx, Token *tk) { - struct span span = tk->span; - struct macro *mac = NULL; + Span span = tk->span; + Macro *mac = NULL; internstr mname = tk->name; if (tk->t != TKIDENT || tk->blue || !(mac = findmac(mname))) return EXPNONE; /* prevent infinite recursion */ - for (struct macrostack *l = lx->macstk; l && l+1 > mstk; --l) { + for (MacroStack *l = lx->macstk; l && l+1 > mstk; --l) { if (l->macid == mac->id) { tk->blue = 1; return EXPNONE; } } - struct macrostack *stkprev = lx->macstk; + MacroStack *stkprev = lx->macstk; if (mac->special && !mac->fnlike) { mac->handler(lx, tk); return EXPINL; } else if (mac->fnlike) { /* look if there is a '(' token ahead, expand if so */ - struct macrostack *s = lx->macstk; + MacroStack *s = lx->macstk; if (s && s->idx >= s->rl.n && !s->stop) { popmac(lx, 1); s = lx->macstk; } if (!s) { /* top-level context: looking ahead in file data */ - struct token tk; + Token tk; int t; for (;;) { /* skip whitespace and comments */ if (aisspace(t = peek(lx, 0))) next(lx); @@ -1066,7 +1068,7 @@ tryexpand(struct lexer *lx, struct token *tk) next(lx), next(lx); while (peek(lx, 0) != '*' || peek(lx, 1) != '/') { if (lx->eof) { - struct span span = {{ idx, lx->chridx - idx, lx->fileid }}; + Span span = {{ idx, lx->chridx - idx, lx->fileid }}; lxfatal(lx, &span, "unterminated comment"); } next(lx); @@ -1085,12 +1087,12 @@ tryexpand(struct lexer *lx, struct token *tk) } expandfnmacro(lx, &span, mname, mac); } else if (mac->predef && mac->single) { - struct span span = tk->span; + Span span = tk->span; *tk = *mac->single; tk->span = span; return EXPINL; } else if (mac->rl.n) { - pushmacstk(lx, &span, &(struct macrostack){ + pushmacstk(lx, &span, &(MacroStack){ .rl = { .off = mac->rl.off, .n = mac->rl.n }, .macid = mac->id, .space = tk->space, @@ -1103,9 +1105,9 @@ tryexpand(struct lexer *lx, struct token *tk) } static bool -advancemacstk(struct lexer *lx, struct token *tk) +advancemacstk(Lexer *lx, Token *tk) { - struct macrostack *s = lx->macstk; + MacroStack *s = lx->macstk; assert(s != NULL); if (s->idx >= s->rl.n) { if (s->stop) { @@ -1128,15 +1130,15 @@ advancemacstk(struct lexer *lx, struct token *tk) } static void -expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro *mac) +expandfnmacro(Lexer *lx, Span *span, internstr mname, Macro *mac) { - struct token _argsbuf[30]; - vec_of(struct token) argsbuf = VINIT(_argsbuf, countof(_argsbuf)); /* buffer for argument tokens */ - struct span excessspan; + Token _argsbuf[30]; + vec_of(Token) argsbuf = VINIT(_argsbuf, countof(_argsbuf)); /* buffer for argument tokens */ + Span excessspan; int cur, len, i, bal, narg; - struct token tk; + Token tk; bool toomany = 0; - struct argtks { + struct ArgToks { int idx, n; /* slices of argsbuf */ int idx2, n2; ushort nfirstx, /* for concatenation to work properly with expanded arguments, */ @@ -1145,7 +1147,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro *args = mac->nparam < countof(_args0) ? _args0 : alloc(lx->tmparena, sizeof *args * mac->nparam, 0); cur = i = bal = len = narg = 0; - for (struct macrostack *s = lx->macstk;;) { + for (MacroStack *s = lx->macstk;;) { if (!s) { bool nl = 0; for (;; nl = 1) { @@ -1155,7 +1157,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro tk.space |= nl; } else { - tk = s->idx < s->rl.n ? stkgetrl(s)[s->idx++] : (struct token){TKEOF}; + tk = s->idx < s->rl.n ? stkgetrl(s)[s->idx++] : (Token){TKEOF}; } if (((tk.t == ')' && bal == 0) || tk.t == TKEOF)) break; if (tk.t == ',' && bal == 0) { @@ -1196,22 +1198,22 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro joinspan(&span->ex, tk.span.ex); int expargs0 = argsbuf.n; for (int i = 0; i < mac->nparam; ++i) { - struct argtks *arg = &args[i]; + struct ArgToks *arg = &args[i]; if (i >= narg) { memset(arg, 0, sizeof *arg); } else if (!mac->param || (mac->param[i] && arg->n > 0)) { /* expand args used in the macro body */ - pushmacstk(lx, &tk.span, &(struct macrostack) { + pushmacstk(lx, &tk.span, &(MacroStack) { .rl = { .p = argsbuf.p + arg->idx, .n = arg->n }, .macid = -1, .stop = 1, }); - struct macrostack *l = lx->macstk; + MacroStack *l = lx->macstk; arg->idx2 = argsbuf.n; arg->nfirstx = arg->nlastx = 1; int ilastx = -1; for (bool pad = 0;;) { - struct macrostack *sprev = lx->macstk; + MacroStack *sprev = lx->macstk; if (!advancemacstk(lx, &tk)) { pad |= tk.space && lx->macstk == sprev; /* preserve whitespace empty macro */ if (lx->macstk == l && l->idx == 1) @@ -1244,7 +1246,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro if (mac->special) { mac->handlerfn(lx, &tk, argsbuf.p+expargs0, argsbuf.n-expargs0); vpush(&mdyntoksbuf, tk); - pushmacstk(lx, span, &(struct macrostack){ + pushmacstk(lx, span, &(MacroStack){ .rl = { .off = mdyntoksbuf.n-1, .n = 1 }, .dyn = 1, .macid = mac->id, @@ -1254,8 +1256,8 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro int vaoptbal = 0; uint off = mdyntoksbuf.n; for (int i = 0; i < mac->rl.n; ++i) { - struct argtks *arg; - const struct token *tki = &mtoksbuf.p[mac->rl.off+i]; + struct ArgToks *arg; + const Token *tki = &mtoksbuf.p[mac->rl.off+i]; if (vaoptskip) { assert(vaoptbal > 0); if (tki->t == '(') ++vaoptbal; @@ -1265,7 +1267,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro continue; } if (tki->t == TKPPCAT && i > 0 && i < mac->rl.n-1) { /* concatenation */ - const struct token *lhs = tki-1, + const Token *lhs = tki-1, *rhs = tki+1; bool space = lhs->space | spacepad; if (lhs->t == ',' && mac->variadic @@ -1304,7 +1306,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro if (!lhs) vpush(&mdyntoksbuf, *rhs); else if (!rhs) vpush(&mdyntoksbuf, *lhs); else { - struct token new; + Token new; if (tokpaste(lx, &new, lhs, rhs)) { new.span.sl = tki->span.sl; } @@ -1339,7 +1341,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro spacepad = 1; continue; } - struct token *rl = argsbuf.p + arg->idx2; + Token *rl = argsbuf.p + arg->idx2; int n = arg->n2; bool skipfirst = 0; if (i > 0 && tki[-1].t == TKPPCAT) { @@ -1361,7 +1363,7 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro spacepad = 0; } else { /* PPMACSTR */ char tmp[200]; - struct wbuf buf = MEMBUF(tmp, sizeof tmp); + WriteBuf buf = MEMBUF(tmp, sizeof tmp); int n = 0; arg = &args[tki->argidx]; @@ -1370,19 +1372,19 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro // string with an actual newline, not {'\\','n'} Redo: for (int i = 0; i < arg->n; ++i) { - struct token *tk = &argsbuf.p[arg->idx + i]; + Token *tk = &argsbuf.p[arg->idx + i]; if (i > 0 && tk->space) n += bfmt(&buf, " "); n += bfmt(&buf, "%tk", tk); } ioputc(&buf, 0); if (buf.err) { - struct wbuf new = MEMBUF(alloc(lx->tmparena, n+1, 1), n+1); + WriteBuf new = MEMBUF(alloc(lx->tmparena, n+1, 1), n+1); assert(buf.buf == tmp); memcpy(&buf, &new, sizeof buf); goto Redo; } - vpush(&mdyntoksbuf, ((struct token) { + vpush(&mdyntoksbuf, ((Token) { .t = TKSTRLIT, .wide = 0, .space = tki->space | spacepad, @@ -1395,14 +1397,14 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro uint n = mdyntoksbuf.n - off; if (n) { - pushmacstk(lx, span, &(struct macrostack){ + pushmacstk(lx, span, &(MacroStack){ .rl = { .off = off, .n = n }, .macid = mac->id, .dyn = 1, }); } } else if (mac->rl.n) { - pushmacstk(lx, span, &(struct macrostack){ + pushmacstk(lx, span, &(MacroStack){ .rl = { .off = mac->rl.off, .n = mac->rl.n }, .macid = mac->id, }); @@ -1410,9 +1412,9 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro vfree(&argsbuf); } -static struct token epeektk; +static Token epeektk; static int -elex(struct lexer *lx, struct token *tk) +elex(Lexer *lx, Token *tk) { assert(tk); if (epeektk.t) { @@ -1432,7 +1434,7 @@ elex(struct lexer *lx, struct token *tk) } static int -epeek(struct lexer *lx, struct token *tk) +epeek(Lexer *lx, Token *tk) { if (!epeektk.t) elex(lx, &epeektk); if (tk) *tk = epeektk; @@ -1461,9 +1463,9 @@ tkprec(int tt) } static vlong -expr(struct lexer *lx, bool *pu, int prec, bool ignore) +expr(Lexer *lx, bool *pu, int prec, bool ignore) { - struct token tk; + Token tk; enum typetag ty; char unops[16]; int nunop = 0; @@ -1547,7 +1549,7 @@ Switch: x = !!x | !!expr(lx, &yu, opprec+1, ignore || x); xu = 0; } else if (tk.t == '?') { - struct span span = tk.span; + Span span = tk.span; vlong m = expr(lx, &xu, 1, ignore || !x); if (elex(lx, &tk) != ':') { error(&tk.span, "expected ':'"); @@ -1616,8 +1618,9 @@ enum { PPCNDTRUE, /* the condition was non-zero, emit until #else/#elif */ PPCNDTAKEN /* some branch was already taken, skip until #else */ }; -static struct ppcnd { - struct span0 ifspan; +typedef struct PPCond PPCond; +static struct PPCond { + Span0 ifspan; int filedepth; uchar cnd; bool elsep; @@ -1627,7 +1630,7 @@ static int nppcnd; static int includedepth; static void -ppif(struct lexer *lx, const struct span *span) +ppif(Lexer *lx, const Span *span) { vlong v = expr(lx, NULL, 0, 0); assert(nppcnd < countof(ppcndstk) && "too many nested #if"); @@ -1638,9 +1641,9 @@ ppif(struct lexer *lx, const struct span *span) } static void -ppifxdef(struct lexer *lx, bool defp, const struct span *span) +ppifxdef(Lexer *lx, bool defp, const Span *span) { - struct token tk; + Token tk; lex0(lx, &tk, 0); if (tk.t != TKIDENT) { @@ -1658,10 +1661,10 @@ ppifxdef(struct lexer *lx, bool defp, const struct span *span) } static void -ppelif(struct lexer *lx, const struct span *span) +ppelif(Lexer *lx, const Span *span) { vlong v; - struct ppcnd *cnd; + PPCond *cnd; if (!nppcnd) { error(span, "#elif without matching #if"); @@ -1680,10 +1683,10 @@ ppelif(struct lexer *lx, const struct span *span) } } static void -ppelifxdef(struct lexer *lx, bool defp, const struct span *span) +ppelifxdef(Lexer *lx, bool defp, const Span *span) { - struct token tk; - struct ppcnd *cnd; + Token tk; + PPCond *cnd; if (!nppcnd) { error(span, "#elif%sdef without matching #if", &"n"[defp]); @@ -1710,7 +1713,7 @@ ppelifxdef(struct lexer *lx, bool defp, const struct span *span) } static void -ppendif(struct lexer *lx, const struct span *span) +ppendif(Lexer *lx, const Span *span) { expecteol(lx, "endif"); if (!nppcnd) { @@ -1721,9 +1724,9 @@ ppendif(struct lexer *lx, const struct span *span) } static void -ppelse(struct lexer *lx, const struct span *span) +ppelse(Lexer *lx, const Span *span) { - struct ppcnd *cnd; + PPCond *cnd; expecteol(lx, "else"); if (!nppcnd) { error(span, "#else without matching #if"); @@ -1741,9 +1744,9 @@ ppelse(struct lexer *lx, const struct span *span) enum { MAXINCLUDE = 200 }; static bool -tryincludepath(struct lexer *lx, const struct span *span, char *path) +tryincludepath(Lexer *lx, const Span *span, char *path) { - struct lexer new; + Lexer new; const char *err; switch (initlexer(&new, &err, path)) { default: assert(0); @@ -1769,7 +1772,7 @@ tryincludepath(struct lexer *lx, const struct span *span, char *path) } static bool -doinclude(struct lexer *lx, const struct span *span, bool quote, const char *str, size_t slen) +doinclude(Lexer *lx, const Span *span, bool quote, const char *str, size_t slen) { char *path = NULL; const char *base, *end; @@ -1803,7 +1806,7 @@ doinclude(struct lexer *lx, const struct span *span, bool quote, const char *str * 6. -idirafter */ for (int i = quote ? CINCL_iquote : CINCL_I; i < countof(cinclpaths); ++i) { - for (struct inclpath *p = cinclpaths[i].list; p; p = p->next) { + for (CInclPath *p = cinclpaths[i]; p; p = p->next) { if (i == CINCLsys) { /* try embedded files pseudo-path */ xbgrow(&path, slen + 3); @@ -1828,10 +1831,10 @@ NotFound: } static bool -ppinclude(struct lexer *lx, const struct span *span0) +ppinclude(Lexer *lx, const Span *span0) { - struct token tk; - struct span span = *span0; + Token tk; + Span span = *span0; if (in_range(lex0(lx, &tk, 1), TKPPHDRH, TKPPHDRQ)) { expecteol(lx, "include"); @@ -1842,8 +1845,8 @@ ppinclude(struct lexer *lx, const struct span *span0) } else { /* '#include pp-tokens' * gather and expand pp-tokens */ - struct token tksbuf[8]; - vec_of(struct token) tks = VINIT(tksbuf, countof(tksbuf)); + Token tksbuf[8]; + vec_of(Token) tks = VINIT(tksbuf, countof(tksbuf)); for (;;) { if (!lx->macstk) { if (tryexpand(lx, &tk) == EXPSTACK) continue; @@ -1862,9 +1865,9 @@ ppinclude(struct lexer *lx, const struct span *span0) } else if (tks.n > 2 && tks.p[0].t == '<' && tks.p[tks.n-1].t == '>') { /* <header.h> */ /* this is multiple tokens, concatenate them together */ char buf[4096]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); for (int i = 1; i < tks.n-1; ++i) { - struct token *tk = &tks.p[i]; + Token *tk = &tks.p[i]; bfmt(&wbuf, &" %tk"[!tk->space], tk); } joinspan(&span.ex, tks.p[tks.n-1].span.ex); @@ -1883,11 +1886,11 @@ ppinclude(struct lexer *lx, const struct span *span0) } static void -ppline(struct lexer *lx, struct token *tk0) +ppline(Lexer *lx, Token *tk0) { - struct token tk, tks[2]; + Token tk, tks[2]; int ntk = 0; - struct span span = tk0->span; + Span span = tk0->span; bool ext = 0; if (tk0->t == TKNUMLIT) { /* handles GNU-style post preprocessing directive '# n ...' */ tks[ntk++] = *tk0; @@ -1945,10 +1948,10 @@ ppline(struct lexer *lx, struct token *tk0) } static void -pppragma(struct lexer *lx, const struct span *span0) +pppragma(Lexer *lx, const Span *span0) { - struct token tk; - struct span span = *span0; + Token tk; + Span span = *span0; if (lex0(lx, &tk, 0) == TKIDENT && !strcmp(tk.s, "once")) { markfileonce(lx->fileid, NULL); } else { @@ -1961,7 +1964,7 @@ pppragma(struct lexer *lx, const struct span *span0) } static void -ppdiag(struct lexer *lx, const struct span *span0, bool err) +ppdiag(Lexer *lx, const Span *span0, bool err) { const uchar *p = getfile(lx->fileid)->p; uint off = lx->chridx, end; @@ -1992,7 +1995,7 @@ enum directive { }; static enum directive -findppcmd(const struct token *tk) +findppcmd(const Token *tk) { static const char *tab[] = { /* !sorted */ @@ -2029,7 +2032,7 @@ findppcmd(const struct token *tk) } static void -identkeyword(struct token *tk) +identkeyword(Token *tk) { #ifdef __GNUC__ #pragma GCC diagnostic push @@ -2037,7 +2040,7 @@ identkeyword(struct token *tk) #endif static const struct { const char *s; - struct kw { uchar t, cstd : 4, ext : 1; } kw; + struct Kw { uchar t, cstd : 4, ext : 1; } kw; const char *alias[2]; } kwtab[] = { #define _(kw, cstd, ...) { #kw, {TKW##kw, cstd}, __VA_ARGS__ }, @@ -2047,11 +2050,11 @@ identkeyword(struct token *tk) #ifdef __GNUC__ #pragma GCC diagnostic pop #endif - static pmap_of(struct kw) kwmap; + static pmap_of(struct Kw) kwmap; if (!kwmap.v) { pmap_init(&kwmap, 128); for (int i = 0; i < countof(kwtab); ++i) { - struct kw kw = kwtab[i].kw; + struct Kw kw = kwtab[i].kw; /* allow future keywords but only if they begin with _ */ if (kw.cstd <= ccopt.cstd || kwtab[i].s[0] == '_') { kw.ext = kw.cstd > ccopt.cstd; @@ -2064,7 +2067,7 @@ identkeyword(struct token *tk) } } } - struct kw *kw = pmap_get(&kwmap, tk->name); + struct Kw *kw = pmap_get(&kwmap, tk->name); if (kw) { tk->t = kw->t; tk->extwarn = kw->ext; @@ -2072,9 +2075,9 @@ identkeyword(struct token *tk) } int -lex(struct lexer *lx, struct token *tk_) +lex(Lexer *lx, Token *tk_) { - struct token tkx[1], *tk; + Token tkx[1], *tk; int t; Begin: @@ -2160,7 +2163,7 @@ Begin: if (tryexpand(lx, tk) == EXPSTACK) goto Begin; if (t == TKEOF && nppcnd && ppcndstk[nppcnd-1].filedepth == includedepth) { - struct span span = { ppcndstk[nppcnd-1].ifspan }; + Span span = { ppcndstk[nppcnd-1].ifspan }; error(&span, "#if is not matched by #endif"); } if (t == TKEOF && lx->save) { @@ -2168,7 +2171,7 @@ Begin: if (lastcmd == PPENDIF && lx->inclguard) { markfileonce(lx->fileid, lx->inclguard); } - struct lexer *sv = lx->save; + Lexer *sv = lx->save; if (sv->inclnerror != nerror || sv->inclnwarn != nwarn) { int line; const char *f = getfilepos(&line, NULL, sv->fileid, sv->chridx-2); @@ -2195,9 +2198,9 @@ Begin: } int -lexpeek(struct lexer *lx, struct token *tk_) +lexpeek(Lexer *lx, Token *tk_) { - struct token tkx[1], *tk; + Token tkx[1], *tk; uint t; tk = tk_ ? tk_ : tkx; @@ -2220,7 +2223,7 @@ cpppredef(bool undef, const char *cmd) const char *sep = strchr(cmd, '='), *body = sep ? sep+1 : "1"; uint namelen = sep ? sep - cmd : strlen(cmd); char line[1024]; - struct wbuf wbuf = MEMBUF(line, sizeof line); + WriteBuf wbuf = MEMBUF(line, sizeof line); if (!ppcmdline.p) vinit(&ppcmdline, NULL, 1<<10); int n; if (undef) @@ -2232,7 +2235,7 @@ cpppredef(bool undef, const char *cmd) } static void -mac__file__(struct lexer *lx, struct token *tk) +mac__file__(Lexer *lx, Token *tk) { tk->t = TKSTRLIT; tk->s = getfilename(lx->fileid, lx->chridx); @@ -2241,11 +2244,11 @@ mac__file__(struct lexer *lx, struct token *tk) } static void -mac__line__(struct lexer *lx, struct token *tk) +mac__line__(Lexer *lx, Token *tk) { char buf[20]; int line; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); getfilepos(&line, NULL, lx->fileid, lx->chridx); bfmt(&wbuf, "%d", line), buf[wbuf.len++] = 0; tk->t = TKNUMLIT; @@ -2256,10 +2259,10 @@ mac__line__(struct lexer *lx, struct token *tk) #include <time.h> static void -mac__date__(struct lexer *lx, struct token *tk) +mac__date__(Lexer *lx, Token *tk) { char buf[20]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); time_t tm = time(NULL); struct tm *ts = localtime(&tm); tk->t = TKSTRLIT; @@ -2277,10 +2280,10 @@ mac__date__(struct lexer *lx, struct token *tk) } static void -mac__time__(struct lexer *lx, struct token *tk) +mac__time__(Lexer *lx, Token *tk) { char buf[20]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); time_t tm = time(NULL); struct tm *ts = localtime(&tm); tk->t = TKSTRLIT; @@ -2296,10 +2299,10 @@ mac__time__(struct lexer *lx, struct token *tk) } static void -mac__counter__(struct lexer *lx, struct token *tk) +mac__counter__(Lexer *lx, Token *tk) { char buf[20]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); static int counter; bfmt(&wbuf, "%d", counter++), buf[wbuf.len++] = 0; tk->t = TKNUMLIT; @@ -2308,7 +2311,7 @@ mac__counter__(struct lexer *lx, struct token *tk) } static void -mac__has_builtin(struct lexer *lx, struct token *tk, const struct token *args, int narg) +mac__has_builtin(Lexer *lx, Token *tk, const Token *args, int narg) { extern bool hasbuiltin(const char *, uint n); bool has = 0; @@ -2331,8 +2334,8 @@ mac__has_builtin(struct lexer *lx, struct token *tk, const struct token *args, i static void putdef1(const char *name) { - static const struct token tok_1 = { TKNUMLIT, .s = "1", .len = 1, .litlit = 1 }; - putmac(intern(name), &(struct macro) { + static const Token tok_1 = { TKNUMLIT, .s = "1", .len = 1, .litlit = 1 }; + putmac(intern(name), &(Macro) { .predef = 1, .single = &tok_1 }); @@ -2345,16 +2348,16 @@ putdefs1(const char *s) } static void -addpredefmacros(struct arena **tmparena) +addpredefmacros(Arena **tmparena) { - static struct token tok_stdc = {TKNUMLIT}, + static Token tok_stdc = {TKNUMLIT}, tok_major = {TKNUMLIT, .s = XSTR(ANTCC_VERSION_MAJOR), .len = sizeof XSTR(ANTCC_VERSION_MAJOR) - 1}, tok_minor = {TKNUMLIT, .s = XSTR(ANTCC_VERSION_MINOR), .len = sizeof XSTR(ANTCC_VERSION_MINOR) - 1}, tok_patch = {TKNUMLIT, .s = XSTR(ANTCC_VERSION_PATCH), .len = sizeof XSTR(ANTCC_VERSION_PATCH) - 1}; - static struct { const char *name; struct macro m; } macs[] = { + static struct { const char *name; Macro m; } macs[] = { { "__FILE__", { .predef = 1, .special = 1, .handler = mac__file__ }}, { "__LINE__", { .predef = 1, .special = 1, .handler = mac__line__ }}, { "__DATE__", { .predef = 1, .special = 1, .handler = mac__date__ }}, @@ -2393,8 +2396,8 @@ addpredefmacros(struct arena **tmparena) putdefs1(archpredefs[target.arch]); if (ppcmdline.n) { - struct memfile *f; - struct lexer lx[1] = {0}; + MemFile *f; + Lexer lx[1] = {0}; lx->fileid = getpredeffile(&f, "<command line>"); assert(!f->p); lx->ndat = f->n = ppcmdline.n; @@ -2408,18 +2411,18 @@ addpredefmacros(struct arena **tmparena) } enum initlexer -initlexer(struct lexer *lx, const char **err, const char *file) +initlexer(Lexer *lx, const char **err, const char *file) { enum { NARENA = 1<<12 }; - static union { char m[sizeof(struct arena) + NARENA]; struct arena *_align; } amem; - static struct arena *tmparena = (void *)amem.m; + static union { char m[sizeof(Arena) + NARENA]; Arena *_align; } amem; + static Arena *tmparena = (void *)amem.m; if (!tmparena->cap) tmparena->cap = NARENA; if (!mtoksbuf.p) vinit(&mtoksbuf, NULL, 1024); if (!mdyntoksbuf.p) vinit(&mdyntoksbuf, NULL, 256); if (!macroht.v) addpredefmacros(&tmparena); - struct memfile *f; + MemFile *f; int fileid = openfile(err, &f, file); if (fileid < 0) return LXERR; @@ -2443,7 +2446,7 @@ initlexer(struct lexer *lx, const char **err, const char *file) /* callback to let lexer release temp memory for arena allocated token data */ void -lexerfreetemps(struct lexer *lx) +lexerfreetemps(Lexer *lx) { if (!lx->macstk) { /* some of the tokens could be somewhere in the macro stack */ @@ -2452,9 +2455,9 @@ lexerfreetemps(struct lexer *lx) } void -lexerdump(struct lexer *lx, struct wbuf *out) +lexerdump(Lexer *lx, WriteBuf *out) { - struct token prev = {0}, tok; + Token prev = {0}, tok; int file = lx->fileid, line = 1, col = 1; const char *lastfile = getfilename(file, 0); bfmt(out, "# %d %'s\n", 1, lastfile); diff --git a/src/c_lex.h b/src/c_lex.h index 8797ded..ba3e3b5 100644 --- a/src/c_lex.h +++ b/src/c_lex.h @@ -2,7 +2,7 @@ #include "c_type.h" static inline bool -joinspan(struct span0 *dst, struct span0 snd) +joinspan(Span0 *dst, Span0 snd) { if (dst->file != snd.file) return 0; if (dst->off > snd.off) return 0; @@ -51,7 +51,7 @@ enum toktag { /* single-character tokens' tag value is the character itself */ }; static_assert(NTOKTAG < 256); -struct token { +typedef struct Token { uchar t; /* toktag */ bool litlit : 1, blue : 1, /* preprocessor token painted blue */ @@ -63,7 +63,7 @@ struct token { uint len; ushort argidx; }; - struct span span; + Span span; union { internstr name; const char *s; @@ -87,27 +87,27 @@ struct token { * argidx is index in macro param list, * macidx is macro id of which it is a parameter */ -}; +} Token; extern int nerror, nwarn; -struct lexer { - struct lexer *save; +typedef struct Lexer { + struct Lexer *save; short fileid; const uchar *dat; uint ndat; uint idx, chridx; ushort chrbuf0; - struct macrostack *macstk; - struct token peektok; + struct MacroStack *macstk; + Token peektok; bool eof, err; - struct arena **tmparena; + Arena **tmparena; bool firstdirective; short nppcnd0; short inclnerror, inclnwarn; internstr inclguard; uchar chrbuf[1<<10]; uint chridxbuf[1<<10]; -}; +} Lexer; enum initlexer { LXOK, @@ -116,11 +116,11 @@ enum initlexer { LXERR, }; -int lex(struct lexer *, struct token *); -int lexpeek(struct lexer *, struct token *); -enum typetag parsenumlit(uvlong *, double *, const struct token *, bool ispp); -enum initlexer initlexer(struct lexer *, const char **err, const char *file); -void lexerdump(struct lexer *, struct wbuf *out); -void lexerfreetemps(struct lexer *); +int lex(Lexer *, Token *); +int lexpeek(Lexer *, Token *); +enum typetag parsenumlit(uvlong *, double *, const Token *, bool ispp); +enum initlexer initlexer(Lexer *, const char **err, const char *file); +void lexerdump(Lexer *, WriteBuf *out); +void lexerfreetemps(Lexer *); /* vim:set ts=3 sw=3 expandtab: */ diff --git a/src/c_type.c b/src/c_type.c index 30fad03..5fed20e 100644 --- a/src/c_type.c +++ b/src/c_type.c @@ -1,11 +1,11 @@ #include "c_type.h" #include "u_hash.h" -struct typedata typedata[1<<13]; +TypeData typedata[1<<13]; internstr ttypenames[1<<10]; static ushort -hashtd(const struct typedata *td) +hashtd(const TypeData *td) { uint h = td->t*33; bool t; @@ -37,7 +37,7 @@ hashtd(const struct typedata *td) } static bool -tdequ(const struct typedata *a, const struct typedata *b) +tdequ(const TypeData *a, const TypeData *b) { if (a->t != b->t) return 0; switch (a->t) { @@ -65,17 +65,17 @@ tdequ(const struct typedata *a, const struct typedata *b) } static ushort -interntd(const struct typedata *td) +interntd(const TypeData *td) { uint h, i, n = countof(typedata); for (i = h = hashtd(td); n--; ++i) { - struct typedata *slot = &typedata[i &= countof(typedata) - 1]; + TypeData *slot = &typedata[i &= countof(typedata) - 1]; if (!slot->t) { uint nmemb; - static struct arena *datarena; + static Arena *datarena; if (!datarena) { enum { N = 1<<12 }; - static union { char m[sizeof(struct arena) + N]; struct arena *_align; } amem; + static union { char m[sizeof(Arena) + N]; Arena *_align; } amem; datarena = (void *)amem.m, datarena->cap = N; } @@ -107,7 +107,7 @@ interntd(const struct typedata *td) } bool -isincomplete(union type t) +isincomplete(Type t) { switch (t.t) { case TYVOID: return 1; @@ -122,7 +122,7 @@ isincomplete(union type t) } uint -typesize(union type t) +typesize(Type t) { if (isprim(t) || t.t == TYPTR) return targ_primsizes[t.t]; switch (t.t) { @@ -140,7 +140,7 @@ typesize(union type t) } uint -typealign(union type t) +typealign(Type t) { if (isprim(t) || t.t == TYPTR) return targ_primalign[t.t]; switch (t.t) { @@ -155,36 +155,36 @@ typealign(union type t) return 0; } -union type -mkptrtype(union type t, int qual) +Type +mkptrtype(Type t, int qual) { if (isprim(t)) return mktype(TYPTR, .flag = TFCHLDPRIM | (qual & TFCHLDQUAL), .child = t.t); else if (t.t == TYENUM || t.t == TYFUNC || isagg(t)) return mktype(TYPTR, .flag = TFCHLDISDAT | (qual & TFCHLDQUAL), .dat = t.dat); return mktype(TYPTR, .flag = qual & TFCHLDQUAL, - .dat = interntd(&(struct typedata) { TYPTR, .child = t })); + .dat = interntd(&(TypeData) { TYPTR, .child = t })); } -union type -mkarrtype(union type t, int qual, uint n) +Type +mkarrtype(Type t, int qual, uint n) { if (isprim(t) && n < 256) return mktype(TYARRAY, .flag = TFCHLDPRIM | (qual & TFCHLDQUAL), .child = t.t, .arrlen = n); return mktype(TYARRAY, .flag = qual & TFCHLDQUAL, - .dat = interntd(&(struct typedata) { TYARRAY, .child = t, .arrlen = n, .siz = n * typesize(t) })); + .dat = interntd(&(TypeData) { TYARRAY, .child = t, .arrlen = n, .siz = n * typesize(t) })); } -union type -mkfntype(union type ret, uint n, const union type *par, bool kandr, bool variadic) +Type +mkfntype(Type ret, uint n, const Type *par, bool kandr, bool variadic) { - struct typedata td = { TYFUNC, .ret = ret, .nmemb = n, .param = par }; + TypeData td = { TYFUNC, .ret = ret, .nmemb = n, .param = par }; td.kandr = kandr, td.variadic = variadic; return mktype(TYFUNC, .dat = interntd(&td)); } -union type -completetype(internstr name, int id, struct typedata *td) +Type +completetype(internstr name, int id, TypeData *td) { assert(td->t == TYENUM || td->t == TYSTRUCT || td->t == TYUNION); td->id = id; @@ -196,25 +196,25 @@ completetype(internstr name, int id, struct typedata *td) return mktype(td->t, .dat = interntd(td), .backing = td->t == TYENUM ? td->backing : 0); } -union type -mktagtype(internstr name, struct typedata *td) +Type +mktagtype(internstr name, TypeData *td) { static int id; return completetype(name, id++, td); } static bool -getfieldrec(struct fielddata *res, uint off, const struct typedata *td, internstr name) +getfieldrec(FieldData *res, uint off, const TypeData *td, internstr name) { Begin: for (int i = 0; i < td->nmemb; ++i) { - struct namedfield *fld = &td->fld[i]; + NamedField *fld = &td->fld[i]; if (fld->name == name) { /* match */ *res = fld->f; res->off += off; return 1; } else if (!fld->name) { /* anonymous struct/union */ - const struct typedata *ftd = &typedata[fld->f.t.dat]; + const TypeData *ftd = &typedata[fld->f.t.dat]; assert(isagg(fld->f.t)); if (i == td->nmemb - 1) { /* last field, tail recurse */ off += fld->f.off; @@ -228,14 +228,14 @@ Begin: } bool -getfield(struct fielddata *res, union type ty, internstr name) +getfield(FieldData *res, Type ty, internstr name) { assert(isagg(ty)); return getfieldrec(res, 0, &typedata[ty.dat], name); } -union type -typedecay(union type t) +Type +typedecay(Type t) { if (t.t == TYARRAY) return mkptrtype(typechild(t), t.flag & TFCHLDQUAL); @@ -245,12 +245,12 @@ typedecay(union type t) } bool /* 6.5.16.1 Simple assignment Constraints */ -assigncompat(union type dst, union type src) +assigncompat(Type dst, Type src) { if (dst.bits == src.bits) return 1; if (isarith(dst) && isarith(src)) return 1; if (dst.t == TYPTR && src.t == TYPTR) { - union type ds = typechild(dst), ss = typechild(src); + Type ds = typechild(dst), ss = typechild(src); if (ds.bits == ss.bits) return 1; /* T* with different qualifiers */ if (ss.t == TYVOID || ds.t == TYVOID) return 1; /* T* <-> void* */ enum typetag dt = scalartypet(ds), /* handle enums */ @@ -273,10 +273,10 @@ intpromote(enum typetag t) return t < TYINT ? TYINT : t; } -union type /* 6.3.1.8 Usual arithmetic conversions */ -cvtarith(union type a, union type b) +Type /* 6.3.1.8 Usual arithmetic conversions */ +cvtarith(Type a, Type b) { - const union type none = {0}; + const Type none = {0}; if (!isarith(a) || !isarith(b)) return none; if (a.t == TYENUM) a = typechild(a); diff --git a/src/c_type.h b/src/c_type.h index 12f24b2..df04740 100644 --- a/src/c_type.h +++ b/src/c_type.h @@ -22,12 +22,12 @@ enum typetag { /* ordering is important here! */ }; enum typeflagmask { - TFCHLDQUAL = 3, - TFCHLDPRIM = 1<<2, + TFCHLDQUAL = 3, + TFCHLDPRIM = 1<<2, TFCHLDISDAT = 1<<3, }; -union type { +typedef union Type { struct { uchar t; /* type tag */ union { @@ -43,8 +43,8 @@ union type { }; }; uint bits; -}; -static_assert(sizeof(union type) == 4); +} Type; +static_assert(sizeof(Type) == 4); #define isprimt(t) in_range((t), TYBOOL, TYVOID) #define isintt(t) in_range((t), TYENUM, TYUVLONG) @@ -66,37 +66,38 @@ static_assert(sizeof(union type) == 4); #define isagg(ty) isaggt((ty).t) #define iscomplext(t) in_range((t), TYCOMPLEXF, TYCOMPLEXL) #define iscomplex(ty) iscomplext((ty).t) -#define mktype(...) ((union type) {{ __VA_ARGS__ }}) +#define mktype(...) ((Type) {{ __VA_ARGS__ }}) -struct enumvar { +typedef struct { internstr name; union { vlong i; uvlong u; }; -}; +} EnumVar; -struct fielddata { - union type t; +typedef struct { + Type t; ushort off; uchar bitsiz, bitoff : 6, qual : 2; -}; -struct namedfield { +} FieldData; + +typedef struct { internstr name; - struct fielddata f; -}; + FieldData f; +} NamedField; -struct typedata { +typedef struct TypeData { uchar t; ushort id; union { - union type child; - const union type *param; /* functions */ + Type child; + const Type *param; /* functions */ struct { /* aggregates */ - struct namedfield *fld; + NamedField *fld; }; struct { /* enum */ uchar backing; - struct enumvar *var; + EnumVar *var; }; }; union { @@ -116,40 +117,40 @@ struct typedata { }; union { uint siz; /* aggregate & array */ - union type ret; /* function */ + Type ret; /* function */ }; -}; +} TypeData; -extern struct typedata typedata[]; +extern TypeData typedata[]; extern internstr ttypenames[/*id*/]; -bool isincomplete(union type); -uint typesize(union type); -uint typealign(union type); -union type mkptrtype(union type, int qual); -union type mkarrtype(union type t, int qual, uint n); -union type mkfntype(union type ret, uint n, const union type *, bool kandr, bool variadic); -union type mktagtype(internstr name, struct typedata *td); -bool getfield(struct fielddata *res, union type, internstr); -union type completetype(internstr name, int id, struct typedata *td); -union type typedecay(union type); -bool assigncompat(union type dst, union type src); +bool isincomplete(Type); +uint typesize(Type); +uint typealign(Type); +Type mkptrtype(Type, int qual); +Type mkarrtype(Type t, int qual, uint n); +Type mkfntype(Type ret, uint n, const Type *, bool kandr, bool variadic); +Type mktagtype(internstr name, TypeData *td); +bool getfield(FieldData *res, Type, internstr); +Type completetype(internstr name, int id, TypeData *td); +Type typedecay(Type); +bool assigncompat(Type dst, Type src); enum typetag intpromote(enum typetag); -union type cvtarith(union type a, union type b); -static inline union type -typechild(union type t) +Type cvtarith(Type a, Type b); +static inline Type +typechild(Type t) { if (t.t == TYENUM) return mktype(t.backing ? t.backing : typedata[t.dat].backing); if (t.flag & TFCHLDPRIM) return mktype(t.child); if (t.flag & TFCHLDISDAT) { - union type chld = mktype(typedata[t.dat].t, .dat = t.dat); + Type chld = mktype(typedata[t.dat].t, .dat = t.dat); if (chld.t == TYENUM) chld.backing = typedata[t.dat].backing; return chld; } return typedata[t.dat].child; } static inline enum typetag -scalartypet(union type t) +scalartypet(Type t) { if (t.t == TYENUM) return t.backing ? t.backing : typedata[t.dat].backing; if (isptrcvt(t)) return TYPTR; @@ -157,7 +158,7 @@ scalartypet(union type t) return t.t; } static inline uint -typearrlen(union type t) +typearrlen(Type t) { return (t.flag & TFCHLDPRIM) ? t.arrlen : typedata[t.dat].arrlen; } @@ -27,12 +27,12 @@ const uchar opnarg[] = { #undef _ }; -struct instr instrtab[MAXINSTR]; -struct use *instruse[MAXINSTR]; +Instr instrtab[MAXINSTR]; +IRUse *instruse[MAXINSTR]; int ninstr; static int instrfreelist; -static struct use *usefreelist; -static struct arena **usearena; +static IRUse *usefreelist; +static Arena **usearena; struct calltab calltab; struct phitab phitab; struct dattab dattab; @@ -43,13 +43,13 @@ static int naddrht; int visitmark; void -irinit(struct function *fn) +irinit(Function *fn) { - static struct call callsbuf[64]; - static union ref *phisbuf[64]; - static struct irdat datsbuf[64]; - static struct xcon consbuf[64]; - static struct addr addrsbuf[64]; + static IRCall callsbuf[64]; + static Ref *phisbuf[64]; + static IRDat datsbuf[64]; + static IRCon consbuf[64]; + static IRAddr addrsbuf[64]; assert(fn->arena && !fn->passarena); @@ -66,7 +66,7 @@ irinit(struct function *fn) if (!addrht) xbgrowz(&addrht, naddrht); else if (addrtab.n) memset(addrht, 0, xbcap(addrht) * sizeof *addrht); vinit(&addrtab, addrsbuf, countof(addrsbuf)); - + if (!type2cls[TYINT]) { for (int i = TYBOOL; i <= TYUVLONG; ++i) { int siz = targ_primsizes[i]; @@ -81,21 +81,21 @@ irinit(struct function *fn) cls2load[KPTR] = targ_64bit ? Oloadi64 : Oloads32; cls2store[KPTR] = targ_64bit ? Ostorei64 : Ostorei32; } - fn->entry = fn->curblk = allocz(fn->arena, sizeof(struct block), 0); + fn->entry = fn->curblk = allocz(fn->arena, sizeof(Block), 0); fn->nblk = 1; fn->entry->lprev = fn->entry->lnext = fn->entry; fn->prop = FNUSE; /* builder keeps this */ } static int -newaddr(const struct addr *addr) +newaddr(const IRAddr *addr) { if (addrtab.n >= naddrht/4*3 /*75% load factor */) { xbgrowz(&addrht, naddrht*2); memset(addrht, 0, naddrht * sizeof *addrht); naddrht *= 2; for (int i = 0; i < addrtab.n; ++i) { /* rehash */ - const struct addr *addr = &addrtab.p[i]; + const IRAddr *addr = &addrtab.p[i]; for (uint h = hashb(0, addr, sizeof *addr), j = h;; ++j) { if (!addrht[j &= naddrht - 1]) { addrht[j] = i+1; @@ -115,62 +115,62 @@ newaddr(const struct addr *addr) } } -union ref -newxcon(const struct xcon *con) +Ref +newxcon(const IRCon *con) { assert((con->issym ^ con->isdat) || con->cls); vpush(&contab, *con); return mkref(RXCON, contab.n-1); } -union irtype -mkirtype(union type t) +IRType +mkirtype(Type t) { if (t.t == TYVOID || isscalar(t)) - return (union irtype) { .cls = type2cls[scalartypet(t)] }; + return (IRType) { .cls = type2cls[scalartypet(t)] }; assert(isagg(t)); - return (union irtype) { .isagg = 1, .dat = t.dat }; + return (IRType) { .isagg = 1, .dat = t.dat }; } -union ref +Ref mkintcon(enum irclass k, vlong i) { if (i < 1l << 28 && i >= -(1l << 28)) { return mkref(RICON, i); } else { - struct xcon con = { .cls = k, .i = i }; + IRCon con = { .cls = k, .i = i }; if (cls2siz[k] == 4) /* check upper half is zero or -1 */ assert(in_range((i >> 32) + 1, 0, 1)); return newxcon(&con); } } -union ref +Ref mkfltcon(enum irclass k, double f) { - struct xcon con = { .cls = k, .f = k == KF32 ? (float) f : f }; + IRCon con = { .cls = k, .f = k == KF32 ? (float) f : f }; return newxcon(&con); } -union ref +Ref mksymref(internstr s, enum symflags symflags) { - struct xcon con = { .issym = 1, .sym = s, .flag = symflags }; + IRCon con = { .issym = 1, .sym = s, .flag = symflags }; return newxcon(&con); } -union ref -mkdatref(internstr name, union type ctype, uint siz, uint align, +Ref +mkdatref(internstr name, Type ctype, uint siz, uint align, const void *bytes, uint n, bool deref, bool funclocal) { - struct irdat dat = { .ctype = ctype, .align = align, .siz = siz, .name = name, .section = Srodata }; + IRDat dat = { .ctype = ctype, .align = align, .siz = siz, .name = name, .section = Srodata }; if (funclocal && objout.code && align >= 4 && align <= targ_primsizes[TYPTR] && siz <= 16) dat.section = Stext; assert(n <= siz && siz && align); if (!name) { char buf[32]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); + WriteBuf wbuf = MEMBUF(buf, sizeof buf); bfmt(&wbuf, ".L%c.%d", dat.section == Stext ? 'L' : 'D', dattab.n); ioputc(&wbuf, 0); @@ -182,44 +182,44 @@ mkdatref(internstr name, union type ctype, uint siz, uint align, if (n) memcpy(p, bytes, n); if (dat.section != Stext) memset(p+n, 0, siz - n); vpush(&dattab, dat); - return newxcon(&(struct xcon){.isdat = 1, .deref = deref, .dat = dattab.n - 1, .flag = SLOCAL}); + return newxcon(&(IRCon){.isdat = 1, .deref = deref, .dat = dattab.n - 1, .flag = SLOCAL}); } internstr xcon2sym(int ref) { - struct xcon con = contab.p[ref]; + IRCon con = contab.p[ref]; assert(con.issym ^ con.isdat); return con.issym ? con.sym : dattab.p[con.dat].name; } -struct instr +Instr mkalloca(uint siz, uint align) { - struct instr ins = { .cls = KPTR }; + Instr ins = { .cls = KPTR }; assert(ispo2(align) && align <= 16); ins.op = Oalloca1 + ilog2(align); ins.l = mkref(RICON, siz/align + (siz%align != 0)); return ins; } -union ref -mkcallarg(union irtype ret, uint narg, int vararg) +Ref +mkcallarg(IRType ret, uint narg, int vararg) { - struct call call = { .ret=ret, .narg=narg, .vararg=vararg }; + IRCall call = { .ret=ret, .narg=narg, .vararg=vararg }; assert(vararg == -1 || (uint)vararg <= narg); vpush(&calltab, call); return mkref(RXXX, calltab.n-1); } -union ref -mkaddr(struct addr addr) +Ref +mkaddr(IRAddr addr) { return mkref(RADDR, newaddr(&addr)); } void -addpred(struct block *blk, struct block *p) +addpred(Block *blk, Block *p) { if (blk->npred == 0) { blk->_pred0 = p; @@ -227,7 +227,7 @@ addpred(struct block *blk, struct block *p) return; } if (blk->npred == 1) { - struct block *p0 = blk->_pred0; + Block *p0 = blk->_pred0; blk->_pred = NULL; xbgrow(&blk->_pred, 4); *blk->_pred = p0; @@ -236,12 +236,12 @@ addpred(struct block *blk, struct block *p) } void -delpred(struct block *blk, struct block *p) +delpred(Block *blk, Block *p) { for (int i = 0; i < blk->npred; ++i) { if (blkpred(blk, i) == p) { for (int j = 0; j < blk->phi.n; ++j) { - union ref *phiargs = phitab.p[instrtab[blk->phi.p[j]].l.i]; + Ref *phiargs = phitab.p[instrtab[blk->phi.p[j]].l.i]; for (int k = i; k < blk->npred - 1; ++k) { phiargs[k] = phiargs[k + 1]; } @@ -250,7 +250,7 @@ delpred(struct block *blk, struct block *p) blkpred(blk, k) = blkpred(blk, k + 1); } if (--blk->npred == 1) { - struct block *p0 = blk->_pred[0]; + Block *p0 = blk->_pred[0]; xbfree(blk->_pred); blk->_pred0 = p0; } @@ -260,16 +260,16 @@ delpred(struct block *blk, struct block *p) //assert(0&&"blk not in p"); } -struct block * -newblk(struct function *fn) +Block * +newblk(Function *fn) { - struct block *blk = allocz(fn->arena, sizeof(struct block), 0); + Block *blk = allocz(fn->arena, sizeof(Block), 0); blk->id = -1; return blk; } void -freeblk(struct function *fn, struct block *blk) +freeblk(Function *fn, Block *blk) { if (blk->npred > 1) xbfree(blk->_pred); @@ -278,14 +278,14 @@ freeblk(struct function *fn, struct block *blk) for (int i = 0; i < blk->phi.n; ++i) { int ui = blk->phi.p[i]; - union ref *r = phitab.p[instrtab[ui].l.i]; + Ref *r = phitab.p[instrtab[ui].l.i]; for (int j = 0; j < blk->npred; ++j) { deluse(blk, ui, *r); } } for (int i = 0; i < blk->ins.n; ++i) { int ui = blk->ins.p[i]; - struct instr *ins = &instrtab[ui]; + Instr *ins = &instrtab[ui]; if (ins->l.t == RTMP) deluse(blk, ui, ins->l); if (ins->r.t == RTMP) deluse(blk, ui, ins->r); } @@ -303,11 +303,11 @@ freeblk(struct function *fn, struct block *blk) blk->id = 1u<<31; } -struct block * -insertblk(struct function *fn, struct block *pred, struct block *subst) +Block * +insertblk(Function *fn, Block *pred, Block *subst) { - struct block *new = newblk(fn); - struct block **s = pred->s1 == subst ? &pred->s1 : &pred->s2; + Block *new = newblk(fn); + Block **s = pred->s1 == subst ? &pred->s1 : &pred->s2; assert(*s == subst); new->lnext = pred->lnext; new->lprev = pred; @@ -327,10 +327,10 @@ insertblk(struct function *fn, struct block *pred, struct block *subst) assert(0); } -struct block * -blksplitafter(struct function *fn, struct block *blk, int idx) +Block * +blksplitafter(Function *fn, Block *blk, int idx) { - struct block *new = newblk(fn); + Block *new = newblk(fn); ++fn->nblk; new->lprev = blk; new->lnext = blk->lnext; @@ -348,7 +348,7 @@ blksplitafter(struct function *fn, struct block *blk, int idx) blk->jmp.t = Jb; memset(blk->jmp.arg, 0, sizeof blk->jmp.arg); for (int i = 0; i < 2; ++i) { - struct block *s = (&blk->s1)[i]; + Block *s = (&blk->s1)[i]; if (s) for (int i = 0; i < s->npred; ++i) { if (blkpred(s, i) == blk) blkpred(s, i) = new; @@ -378,9 +378,9 @@ allocinstr(void) } void -adduse(struct block *ublk, int ui, union ref r) { +adduse(Block *ublk, int ui, Ref r) { if (r.t != RTMP) return; - struct use *use; + IRUse *use; if (usefreelist) { use = usefreelist; usefreelist = usefreelist->next; @@ -395,11 +395,11 @@ adduse(struct block *ublk, int ui, union ref r) { } bool -deluse(struct block *ublk, int ui, union ref r) { +deluse(Block *ublk, int ui, Ref r) { if (r.t != RTMP) return 0; - for (struct use **puse = &instruse[r.i]; *puse; puse = &(*puse)->next) { - struct use *use = *puse; + for (IRUse **puse = &instruse[r.i]; *puse; puse = &(*puse)->next) { + IRUse *use = *puse; if (use->blk == ublk && use->u == ui) { *puse = use->next; use->blk = 0; @@ -413,9 +413,9 @@ deluse(struct block *ublk, int ui, union ref r) { } void -filluses(struct function *fn) +filluses(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; for (int i = 0; i < ninstr; ++i) deluses(i); @@ -423,7 +423,7 @@ filluses(struct function *fn) do { for (int i = 0; i < blk->phi.n; ++i) { int ins = blk->phi.p[i]; - union ref *phi = phitab.p[instrtab[ins].l.i]; + Ref *phi = phitab.p[instrtab[ins].l.i]; for (int i = 0; i < blk->npred; ++i) adduse(blk, ins, phi[i]); } @@ -440,7 +440,7 @@ filluses(struct function *fn) } int -newinstr(struct block *at, struct instr ins) +newinstr(Block *at, Instr ins) { int new = allocinstr(); instrtab[new] = ins; @@ -451,8 +451,8 @@ newinstr(struct block *at, struct instr ins) return new; } -union ref -insertinstr(struct block *blk, int idx, struct instr ins) +Ref +insertinstr(Block *blk, int idx, Instr ins) { int new = newinstr(blk, ins); if (idx == blk->ins.n) vpush(&blk->ins, new); @@ -467,11 +467,11 @@ insertinstr(struct block *blk, int idx, struct instr ins) return mkref(RTMP, new); } -union ref -insertphi(struct block *blk, enum irclass cls) +Ref +insertphi(Block *blk, enum irclass cls) { int new = allocinstr(); - union ref *refs = NULL; + Ref *refs = NULL; assert(blk->npred > 0); xbgrowz(&refs, blk->npred); vpush(&phitab, refs); @@ -481,9 +481,9 @@ insertphi(struct block *blk, enum irclass cls) } uint -numberinstrs(struct function *fn) +numberinstrs(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; int start = 0; do { blk->inumstart = start; @@ -493,21 +493,21 @@ numberinstrs(struct function *fn) } static bool -reachablerec(struct function *fn, struct block *blk) +reachablerec(Function *fn, Block *blk) { if (blk == fn->entry) return 1; markvisited(blk); if (blk->npred == 1 && !wasvisited(blkpred(blk, 0))) return reachablerec(fn, blkpred(blk, 0)); else for (int i = 0; i < blk->npred; ++i) { - struct block *p = blkpred(blk, i); + Block *p = blkpred(blk, i); if (!wasvisited(p) && reachablerec(fn, p)) return 1; } return 0; } bool -blkreachable(struct function *fn, struct block *blk) +blkreachable(Function *fn, Block *blk) { startbbvisit(); return reachablerec(fn, blk); @@ -515,11 +515,11 @@ blkreachable(struct function *fn, struct block *blk) /* require use */ void -replcuses(union ref from, union ref to) +replcuses(Ref from, Ref to) { assert(from.t == RTMP); - for (struct use *use = instruse[from.i], *next; use; use = next) { - union ref *u; + for (IRUse *use = instruse[from.i], *next; use; use = next) { + Ref *u; int n, j; next = use->next; if (use->u == from.i) continue; @@ -534,7 +534,7 @@ replcuses(union ref from, union ref to) u = &instrtab[use->u].l; n = 2; } - + for (j = 0; j < n; ++j) { if (u[j].bits == from.bits) { u[j].bits = to.bits; @@ -549,7 +549,7 @@ replcuses(union ref from, union ref to) void deluses(int ins) { - for (struct use *use = instruse[ins], *next; use; use = next) { + for (IRUse *use = instruse[ins], *next; use; use = next) { next = use->next; use->blk = 0; use->u = 0; @@ -560,7 +560,7 @@ deluses(int ins) } void -delinstr(struct block *blk, int idx) +delinstr(Block *blk, int idx) { int t = blk->ins.p[idx]; assert(idx >= 0 && idx < blk->ins.n); @@ -576,7 +576,7 @@ delinstr(struct block *blk, int idx) } void -delphi(struct block *blk, int idx) +delphi(Block *blk, int idx) { int t = blk->phi.p[idx]; assert(idx >= 0 && idx < blk->phi.n); @@ -589,7 +589,7 @@ delphi(struct block *blk, int idx) } void -delnops(struct block *blk) +delnops(Block *blk) { int i, n, t; /* delete trailing nops */ @@ -617,10 +617,10 @@ delnops(struct block *blk) } void -fillblkids(struct function *fn) +fillblkids(Function *fn) { int i = 0; - struct block *blk = fn->entry; + Block *blk = fn->entry; do blk->id = i++; while ((blk = blk->lnext) != fn->entry); fn->prop |= FNBLKID; @@ -629,9 +629,9 @@ fillblkids(struct function *fn) /** Misc **/ static void -freefn(struct function *fn) +freefn(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; do { if (blk->npred > 1) xbfree(blk->_pred); vfree(&blk->phi); @@ -640,11 +640,11 @@ freefn(struct function *fn) } void -irfini(struct function *fn) +irfini(Function *fn) { extern int nerror; - static union { char m[sizeof(struct arena) + (4<<10)]; struct arena *_align; } amem; - struct arena *passarena = (void *)&amem.m; + static union { char m[sizeof(Arena) + (4<<10)]; Arena *_align; } amem; + Arena *passarena = (void *)&amem.m; fn->passarena = &passarena; if (nerror) { freefn(fn); @@ -10,26 +10,20 @@ enum irclass { #define kisint(k) in_range((k), KI32, KPTR) #define kisflt(k) in_range((k), KF32, KF64) -union irtype { - struct { ushort _ : 1, cls : 15; }; - struct { ushort isagg : 1, dat : 15; }; - ushort bits; -}; - -struct irdat { +typedef struct IRDat { uchar align : 6, globl : 1; uchar section; - union type ctype; + Type ctype; uint siz; uint off; internstr name; -}; +} IRDat; enum symflags { SLOCAL = 1, SFUNC = 2, }; -struct xcon { +typedef struct IRCon { bool issym, isdat, deref; uchar cls; uchar flag; @@ -39,25 +33,31 @@ struct xcon { vlong i; double f; }; -}; +} IRCon; -struct abiarg { - union irtype ty; +typedef union IRType { + struct { ushort _ : 1, cls : 15; }; + struct { ushort isagg : 1, dat : 15; }; + ushort bits; +} IRType; + +typedef struct ABIArg { + IRType ty; union { struct { ushort _ : 1, reg : 15; }; struct { ushort isstk : 1, stk : 15; }; }; -}; +} ABIArg; -struct call { - union irtype ret; +typedef struct IRCall { + IRType ret; ushort narg; short vararg; /* first variadic arg or -1 */ ushort argstksiz; - struct abiarg *abiarg; - struct abiarg abiret[2]; + ABIArg *abiarg; + ABIArg abiret[2]; uchar r2off; -}; +} IRCall; enum refkind { RXXX, /* used for empty ref (zeros), undef, and the special args of call,phi,etc */ @@ -70,25 +70,25 @@ enum refkind { RSTACK, /* stack base offset */ }; -union ref { +typedef union Ref { struct { unsigned t : 3; signed i : 29; }; uint bits; -}; -static_assert(sizeof(union ref) == 4); +} Ref; +static_assert(sizeof(Ref) == 4); -struct addr { - union ref base, index; +typedef struct IRAddr { + Ref base, index; int shift, disp; -}; +} IRAddr; #define insrescls(ins) (oiscmp((ins).op) ? KI32 : (ins).cls) -#define NOREF ((union ref) {0}) -#define UNDREF ((union ref) {{ 0, -1 }}) -#define ZEROREF ((union ref) {{ RICON, 0 }}) -#define mkref(t, x) ((union ref) {{ (t), (x) }}) -#define mktyperef(t) ((union ref) {{ RTYPE, (t).bits }}) -#define ref2type(r) ((union irtype) {.bits = (r).i}) -#define rswap(a,b) do { union ref _t = (a); (a) = (b); (b) = _t; } while (0) +#define NOREF ((Ref) {0}) +#define UNDREF ((Ref) {{ 0, -1 }}) +#define ZEROREF ((Ref) {{ RICON, 0 }}) +#define mkref(t, x) ((Ref) {{ (t), (x) }}) +#define mktyperef(t) ((Ref) {{ RTYPE, (t).bits }}) +#define ref2type(r) ((IRType) {.bits = (r).i}) +#define rswap(a,b) do { Ref _t = (a); (a) = (b); (b) = _t; } while (0) enum op { Oxxx, @@ -113,41 +113,46 @@ enum intrin { #undef _ }; -struct instr { +typedef struct Instr { uchar op, cls; /* operation data class; also result class except for cmp ops (always i32) */ uchar skip : 1, /* ignore during codegen: forms part of one machine instruction */ keep : 1; /* for codegen, keep instr even if result seems unused */ uchar inplace : 1; /* set (by isel) for instructions which modify its first arg in place */ uchar reg; /* 0 -> no reg; else reg + 1 */ - union ref l, r; /* args */ -}; -static_assert(sizeof(struct instr) == 4*3); + Ref l, r; /* args */ +} Instr; +static_assert(sizeof(Instr) == 4*3); enum jumpkind { JXXX, Jb, Jret, Jtrap, }; -struct block { +typedef struct Block Block; +struct Block { int id; int npred; int visit; ushort loop; int inumstart; union { - struct block *_pred0; - struct block **_pred; + Block *_pred0; + Block **_pred; }; - struct block *lprev, *lnext; - struct block *s1, *s2; - struct block *idom; + Block *lprev, *lnext; + Block *s1, *s2; + Block *idom; vec_of(ushort) phi; vec_of(ushort) ins; - struct { uchar t; union ref arg[2]; } jmp; + struct { uchar t; Ref arg[2]; } jmp; }; #define blkpred(blk, i) 0[(blk)->npred < 2 ? &(blk)->_pred0 : &(blk)->_pred[i]] enum { USERJUMP = 0xFFFF }; -struct use { struct use *next; struct block *blk; ushort u; }; +typedef struct IRUse { + struct IRUse *next; + Block *blk; + ushort u; +} IRUse; enum { MAXREGS = 64 }; /** register set **/ @@ -172,14 +177,14 @@ enum { FNRPO = 1<<2, FNDOM = 1<<3, }; -struct function { - struct arena **arena, **passarena; +typedef struct Function { + Arena **arena, **passarena; internstr name; - struct block *entry, *curblk; - struct use *use; + Block *entry, *curblk; + IRUse *use; short *nuse; - union type fnty, retty; - struct abiarg *abiarg, abiret[2]; + Type fnty, retty; + ABIArg *abiarg, abiret[2]; uint prop; uint nblk; int stksiz; @@ -188,13 +193,11 @@ struct function { bool isleaf; bool inlin; regset regusage; -}; +} Function; #define FREQUIRE(_prop) assert((fn->prop & (_prop)) == (_prop) && "preconditions not met") -enum objkind { OBJELF }; - -struct mctarg { +typedef struct MCTarg { short gpr0, /* first gpr */ ngpr, /* gpr count */ bpr, /* frame/base pointer reg */ @@ -214,7 +217,7 @@ struct mctarg { * r[1] contains register for hidden pointer argument, * *ni is set to 1 if said register is the first ABI integer argument */ - int (*abiret)(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype); + int (*abiret)(short r[2], uchar cls[2], uchar *r2off, int *ni, IRType); /* abiarg: lower argument type: * scalar/small struct -> returns number of regs (1..2), * r & cls filled with reg and irclass of each scalar arg @@ -225,12 +228,12 @@ struct mctarg { * if passed by pointer cls[0] == KPTR, r[0] contains integer register * or negative SP offset if stack */ - int (*abiarg)(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union irtype); - void (*vastart)(struct function *, struct block *, int *curi); - void (*vaarg)(struct function *, struct block *, int *curi); - void (*isel)(struct function *); - void (*emit)(struct function *); -}; + int (*abiarg)(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, IRType); + void (*vastart)(Function *, Block *, int *curi); + void (*vaarg)(Function *, Block *, int *curi); + void (*isel)(Function *); + void (*emit)(Function *); +} MCTarg; enum { MAXINSTR = 1<<15 }; @@ -240,23 +243,23 @@ extern uchar cls2siz[]; extern uchar cls2load[]; extern uchar cls2store[]; extern const uchar siz2intcls[]; -extern struct instr instrtab[]; -extern struct use *instruse[]; -extern struct calltab {vec_of(struct call);} calltab; -extern struct phitab {vec_of(union ref *);} phitab; -extern struct dattab {vec_of(struct irdat);} dattab; -extern struct contab {vec_of(struct xcon);} contab; -extern struct addrtab {vec_of(struct addr);} addrtab; +extern Instr instrtab[]; +extern IRUse *instruse[]; +extern struct calltab {vec_of(IRCall);} calltab; +extern struct phitab {vec_of(Ref *);} phitab; +extern struct dattab {vec_of(IRDat);} dattab; +extern struct contab {vec_of(IRCon);} contab; +extern struct addrtab {vec_of(IRAddr);} addrtab; extern int visitmark; -#define mkinstr(O, C, ...) ((struct instr) { .op = (O), .cls = (C), .reg=0, __VA_ARGS__ }) +#define mkinstr(O, C, ...) ((Instr) { .op = (O), .cls = (C), .reg=0, __VA_ARGS__ }) #define mkarginstr(ty, x) mkinstr(Oarg, 0, mktyperef(ty), (x)) -void irinit(struct function *); -void irfini(struct function *); -#define cls2type(k) ((union irtype){.cls=(k)}) -union irtype mkirtype(union type); -union ref newxcon(const struct xcon *); -union ref mkintcon(enum irclass, vlong); -union ref mkfltcon(enum irclass, double); +void irinit(Function *); +void irfini(Function *); +#define cls2type(k) ((IRType){.cls=(k)}) +IRType mkirtype(Type); +Ref newxcon(const IRCon *); +Ref mkintcon(enum irclass, vlong); +Ref mkfltcon(enum irclass, double); #define iscon(r) in_range((r).t, RICON, RXCON) #define concls(r) ((r).t == RICON ? KI32 : contab.p[(r).i].cls) #define isintcon(r) (iscon(r) && kisint(concls(r))) @@ -265,89 +268,89 @@ union ref mkfltcon(enum irclass, double); #define isaddrcon(r,derefok) ((r).t == RXCON && !contab.p[(r).i].cls && (derefok || !contab.p[(r).i].deref)) #define intconval(r) ((r).t == RICON ? (r).i : contab.p[(r).i].i) #define fltconval(r) ((r).t == RICON ? (r).i : contab.p[(r).i].f) -union ref mksymref(internstr, enum symflags); -union ref mkdatref(internstr sym, union type ctype, uint siz, uint align, +Ref mksymref(internstr, enum symflags); +Ref mkdatref(internstr sym, Type ctype, uint siz, uint align, const void *, uint n, bool deref, bool funclocal); internstr xcon2sym(int ref); -struct instr mkalloca(uint siz, uint align); -union ref mkcallarg(union irtype ret, uint narg, int vararg); -#define mkintrin(B, C, N) mkinstr(Ointrin, C, {{.t=RICON,B}}, mkcallarg((union irtype){{0}},N,-1)) -union ref mkaddr(struct addr); -void addpred(struct block *blk, struct block *p); - -struct block *newblk(struct function *); -void freeblk(struct function *, struct block *); -struct block *insertblk(struct function *, struct block *pred, struct block *subst); -struct block *blksplitafter(struct function *, struct block *, int idx); -void adduse(struct block *ublk, int ui, union ref r); -int newinstr(struct block *at, struct instr ins); -union ref insertinstr(struct block *, int idx, struct instr); -union ref insertphi(struct block *, enum irclass cls); -void replcuses(union ref from, union ref to); -bool deluse(struct block *ublk, int ui, union ref r); +Instr mkalloca(uint siz, uint align); +Ref mkcallarg(IRType ret, uint narg, int vararg); +#define mkintrin(B, C, N) mkinstr(Ointrin, C, {{.t=RICON,B}}, mkcallarg((IRType){{0}},N,-1)) +Ref mkaddr(IRAddr); +void addpred(Block *blk, Block *p); + +Block *newblk(Function *); +void freeblk(Function *, Block *); +Block *insertblk(Function *, Block *pred, Block *subst); +Block *blksplitafter(Function *, Block *, int idx); +void adduse(Block *ublk, int ui, Ref r); +int newinstr(Block *at, Instr ins); +Ref insertinstr(Block *, int idx, Instr); +Ref insertphi(Block *, enum irclass cls); +void replcuses(Ref from, Ref to); +bool deluse(Block *ublk, int ui, Ref r); void deluses(int ins); -void filluses(struct function *); -void delinstr(struct block *, int idx); -void delphi(struct block *, int idx); -void delnops(struct block *blk); -void delpred(struct block *blk, struct block *p); -void fillblkids(struct function *); +void filluses(Function *); +void delinstr(Block *, int idx); +void delphi(Block *, int idx); +void delnops(Block *blk); +void delpred(Block *blk, Block *p); +void fillblkids(Function *); #define startbbvisit() (void)(++visitmark) #define wasvisited(blk) ((blk)->visit == visitmark) #define markvisited(blk) ((blk)->visit = visitmark) -uint numberinstrs(struct function *); -bool blkreachable(struct function *fn, struct block *blk); +uint numberinstrs(Function *); +bool blkreachable(Function *fn, Block *blk); /** builder.c **/ -union ref irbinop(struct function *, enum op, enum irclass, union ref lhs, union ref rhs); -union ref irunop(struct function *, enum op, enum irclass, union ref); -union ref addinstr(struct function *, struct instr); -union ref addphi(struct function *, enum irclass, union ref []); -void useblk(struct function *, struct block *); -void putbranch(struct function *, struct block *); -void putcondbranch(struct function *, union ref arg, struct block *t, struct block *f); -void putreturn(struct function *, union ref r0, union ref r1); -void puttrap(struct function *); +Ref irbinop(Function *, enum op, enum irclass, Ref lhs, Ref rhs); +Ref irunop(Function *, enum op, enum irclass, Ref); +Ref addinstr(Function *, Instr); +Ref addphi(Function *, enum irclass, Ref []); +void useblk(Function *, Block *); +void putbranch(Function *, Block *); +void putcondbranch(Function *, Ref arg, Block *t, Block *f); +void putreturn(Function *, Ref r0, Ref r1); +void puttrap(Function *); /** fold.c **/ -bool foldbinop(union ref *to, enum op, enum irclass, union ref l, union ref r); -bool foldunop(union ref *to, enum op, enum irclass, union ref); +bool foldbinop(Ref *to, enum op, enum irclass, Ref l, Ref r); +bool foldunop(Ref *to, enum op, enum irclass, Ref); /** irdump.c **/ -void irdump(struct function *); +void irdump(Function *); /** mem2reg.c **/ -void mem2reg(struct function *); +void mem2reg(Function *); /** ssa.c **/ -void copyopt(struct function *); +void copyopt(Function *); /** cfg.c **/ -void sortrpo(struct function *); -void filldom(struct function *); -void fillloop(struct function *); +void sortrpo(Function *); +void filldom(Function *); +void fillloop(Function *); /** abi0.c **/ -void abi0(struct function *); -void abi0_call(struct function *, struct instr *, struct block *blk, int *curi); +void abi0(Function *); +void abi0_call(Function *, Instr *, Block *blk, int *curi); /** simpl.c **/ -void simpl(struct function *); +void simpl(Function *); /** cselim.c **/ -void cselim(struct function *); +void cselim(Function *); /** inliner.c **/ -bool maybeinlinee(struct function *); -void doinline(struct function *); +bool maybeinlinee(Function *); +void doinline(Function *); /** intrin.c **/ -void lowerintrin(struct function *); +void lowerintrin(Function *); /** stack.c **/ -void lowerstack(struct function *); +void lowerstack(Function *); /** regalloc.c **/ -void regalloc(struct function *); +void regalloc(Function *); /* vim:set ts=3 sw=3 expandtab: */ diff --git a/src/ir_abi0.c b/src/ir_abi0.c index a3687d9..cd7ce27 100644 --- a/src/ir_abi0.c +++ b/src/ir_abi0.c @@ -7,10 +7,10 @@ ** exactly narg `arg` instructions with no other instructions in between **/ -struct abiargsvec { vec_of(struct abiarg); }; +typedef vec_of(ABIArg) ABIArgVec; static int -abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *ni, union irtype retty) +abiret(ABIArg abiret[2], ABIArgVec *abiargs, uchar *r2off, int *ni, IRType retty) { short r[2]; uchar cls[2]; @@ -18,7 +18,7 @@ abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *n retreg = mctarg->abiret(r, cls, r2off, ni, retty); if (retty.isagg) { if (!retreg) { - vpush(abiargs, ((struct abiarg) { cls2type(KPTR), .reg = r[1] })); + vpush(abiargs, ((ABIArg) { cls2type(KPTR), .reg = r[1] })); if (r[0] == -1) { memset(abiret, 0, 2*sizeof *abiret); } else { @@ -38,32 +38,32 @@ abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *n } static int -abiarg(struct abiargsvec *abiargs, uchar *r2off, int *ni, int *nf, int *ns, union irtype ty) +abiarg(ABIArgVec *abiargs, uchar *r2off, int *ni, int *nf, int *ns, IRType ty) { short r[2]; uchar cls[2]; int ret = mctarg->abiarg(r, cls, r2off, ni, nf, ns, ty); if (!ret) { /* in stack */ - vpush(abiargs, ((struct abiarg) { ty, .isstk = 1, .stk = r[0] })); + vpush(abiargs, ((ABIArg) { ty, .isstk = 1, .stk = r[0] })); } else if (ret == 1 && ty.isagg && cls[0] == KPTR) { /* aggregate by pointer */ - vpush(abiargs, ((struct abiarg) { cls2type(cls[0]), .reg = r[0] })); + vpush(abiargs, ((ABIArg) { cls2type(cls[0]), .reg = r[0] })); } else { /* by regs */ - vpush(abiargs, ((struct abiarg) { cls2type(cls[0]), .reg = r[0] })); + vpush(abiargs, ((ABIArg) { cls2type(cls[0]), .reg = r[0] })); if (ret == 2) - vpush(abiargs, ((struct abiarg) { cls2type(cls[1]), .reg = r[1] })); + vpush(abiargs, ((ABIArg) { cls2type(cls[1]), .reg = r[1] })); } return ret; } -static struct instr -copyparam(struct function *fn, int *curi, int param, struct abiarg abi) +static Instr +copyparam(Function *fn, int *curi, int param, ABIArg abi) { - struct instr par = mkinstr(Oparam, abi.ty.cls, mkref(RICON, param), mktyperef(abi.ty)); + Instr par = mkinstr(Oparam, abi.ty.cls, mkref(RICON, param), mktyperef(abi.ty)); if (!abi.isstk) { /* reg */ assert(!abi.ty.isagg); return par; } - par.r = mktyperef((union irtype){.cls = KPTR}); + par.r = mktyperef((IRType){.cls = KPTR}); if (!abi.ty.isagg) { /* scalar in stack */ enum op ld; par.cls = KPTR; @@ -83,24 +83,24 @@ copyparam(struct function *fn, int *curi, int param, struct abiarg abi) } static void -patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, struct abiarg abi[2], uchar r2off) +patchparam(Function *fn, int *curi, int *param, int tydat, int nabi, ABIArg abi[2], uchar r2off) { - struct block *blk = fn->entry; + Block *blk = fn->entry; assert(in_range(nabi,1,2)); for (; *curi < blk->ins.n; ++*curi) { - struct instr *ins = &instrtab[blk->ins.p[*curi]]; + Instr *ins = &instrtab[blk->ins.p[*curi]]; if (ins->op != Oparam) continue; assert(ins->r.t == RTYPE - && ins->r.i == (tydat < 0 ? abi[0].ty : (union irtype){.isagg=1, .dat=tydat}).bits); + && ins->r.i == (tydat < 0 ? abi[0].ty : (IRType){.isagg=1, .dat=tydat}).bits); if (abi[0].ty.isagg || tydat < 0) { /* aggregate in stack or scalar, just copy */ assert(nabi == 1); *ins = copyparam(fn, curi, *param, abi[0]); } else { /* aggregate in registers, materialize */ - union ref alloc, r[2]; - struct instr st; - const struct typedata *td; + Ref alloc, r[2]; + Instr st; + const TypeData *td; uint nalloc; uint align; @@ -124,7 +124,7 @@ patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, stru st = mkinstr(cls2store[abi[0].ty.cls], 0, alloc, r[0]); insertinstr(blk, ++*curi, st); if (nabi > 1) { - struct instr tmp = mkinstr(Oadd, KPTR, alloc, mkref(RICON, r2off)); + Instr tmp = mkinstr(Oadd, KPTR, alloc, mkref(RICON, r2off)); st = mkinstr(cls2store[abi[1].ty.cls], 0, insertinstr(blk, ++*curi, tmp), r[1]); insertinstr(blk, ++*curi, st); } @@ -136,7 +136,7 @@ patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, stru } static void -load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct abiarg abi[2], uchar r2off, struct block *blk, int *curi) +load2regs(Ref out[2], IRType typ, Ref src, int nabi, ABIArg abi[2], uchar r2off, Block *blk, int *curi) { uint align = typedata[typ.dat].align; uint siz = typedata[typ.dat].siz; @@ -153,8 +153,8 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab /* XXX this generates pretty bad code for small-alignment structs even on platforms where unaligned loads are available.. */ if (align >= 4) { for (int i = 0; i < nabi; ++i) { - struct instr ins = {0}; - union ref temp; + Instr ins = {0}; + Ref temp; switch (ins.cls = abi[i].ty.cls) { default: assert(0); case KI32: ins.op = Oloadu32; break; @@ -165,7 +165,7 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab if (i == 0) ins.l = src; else { - struct instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, r2off)); + Instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, r2off)); ins.l = insertinstr(blk, (*curi)++, adr); } temp = insertinstr(blk, (*curi)++, ins); @@ -174,8 +174,8 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab } } else { for (int i = 0; i < nabi; ++i) { - struct instr ld = {0}; - union ref reg, temp; + Instr ld = {0}; + Ref reg, temp; uint n = cls2siz[abi[i].ty.cls] / align; assert(n > 0); ld.op = Oloadu8 + ilog2(align)*2; @@ -184,12 +184,12 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab if (i+o == 0) ld.l = src; else { - struct instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, (i == 0 ? 0 : r2off) + o*align)); + Instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, (i == 0 ? 0 : r2off) + o*align)); ld.l = insertinstr(blk, (*curi)++, adr); } temp = insertinstr(blk, (*curi)++, ld); if (o > 0) { - union ref t = insertinstr(blk, (*curi)++, mkinstr(Oshl, ld.cls, temp, mkref(RICON, o*align*8))); + Ref t = insertinstr(blk, (*curi)++, mkinstr(Oshl, ld.cls, temp, mkref(RICON, o*align*8))); reg = insertinstr(blk, (*curi)++, mkinstr(Oior, ld.cls, reg, t)); } else { reg = temp; @@ -202,20 +202,20 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab } static int -patcharg(struct block *blk, int *icall, struct call *call, - int argidx, int nabi, struct abiarg abi[2], uchar r2off) +patcharg(Block *blk, int *icall, IRCall *call, + int argidx, int nabi, ABIArg abi[2], uchar r2off) { int arginst = *icall - (call->narg - argidx); - struct instr *arg = &instrtab[blk->ins.p[arginst]]; + Instr *arg = &instrtab[blk->ins.p[arginst]]; assert(arg->op == Oarg && arg->l.t == RTYPE); if (ref2type(arg->l).isagg) { /* aggregate argument */ if (abi[0].ty.isagg) { /* aggregate in stack */ /* XXX do this better.. */ /* ptr %dst = arg <stk dst> */ /* (blit %dst, %src) */ - union ref dst = mkref(RTMP, arg - instrtab); + Ref dst = mkref(RTMP, arg - instrtab); uint align = typedata[abi->ty.dat].align, siz = typedata[abi->ty.dat].siz; - union ref src = arg->r; + Ref src = arg->r; if (src.t == RTMP && oisalloca(instrtab[src.i].op)) { align = 1 << (instrtab[src.i].op - Oalloca1); } @@ -223,9 +223,9 @@ patcharg(struct block *blk, int *icall, struct call *call, arg->cls = KPTR; arg->r = mkref(RICON, abi->stk); for (uint off = 0; off < siz; off += align) { - union ref sadr = off == 0 ? src : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, src, mkref(RICON, off))); - union ref tmp = insertinstr(blk, ++arginst, mkinstr(Oloads8+2*ilog2(align), align < 8 ? KI32 : KI64, sadr)); - union ref dadr = off == 0 ? dst : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, dst, mkref(RICON, off))); + Ref sadr = off == 0 ? src : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, src, mkref(RICON, off))); + Ref tmp = insertinstr(blk, ++arginst, mkinstr(Oloads8+2*ilog2(align), align < 8 ? KI32 : KI64, sadr)); + Ref dadr = off == 0 ? dst : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, dst, mkref(RICON, off))); insertinstr(blk, ++arginst, mkinstr(Ostorei8+ilog2(align), 0, dadr, tmp)); } *icall = arginst + (call->narg - argidx); @@ -234,7 +234,7 @@ patcharg(struct block *blk, int *icall, struct call *call, arg->cls = KPTR; return 1; } else { /* aggregate in registers */ - union ref r[2]; + Ref r[2]; delinstr(blk, arginst); load2regs(r, ref2type(arg->l), arg->r, nabi, abi, r2off, blk, &arginst); for (int i = 0; i < nabi; ++i) @@ -247,24 +247,24 @@ patcharg(struct block *blk, int *icall, struct call *call, } } void -abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi) +abi0_call(Function *fn, Instr *ins, Block *blk, int *curi) { - union ref retmem; - struct abiarg abiargsbuf[32]; - struct abiargsvec abiargs = {VINIT(abiargsbuf, countof(abiargsbuf))}; + Ref retmem; + ABIArg abiargsbuf[32]; + ABIArgVec abiargs = VINIT(abiargsbuf, countof(abiargsbuf)); bool sretarghidden = 0; int ni, nf, ns, vararg, nret = 0; - struct call *call = &calltab.p[ins->r.i]; + IRCall *call = &calltab.p[ins->r.i]; vararg = call->vararg; ni = nf = ns = 0; assert(!ins->cls == !call->ret.bits); nret = abiret(call->abiret, &abiargs, &call->r2off, &ni, call->ret); if (call->ret.isagg) { /* adjust struct return */ - union irtype retty = call->ret; - struct typedata *td = &typedata[retty.dat]; + IRType retty = call->ret; + TypeData *td = &typedata[retty.dat]; uint align = td->align, ralign; - struct instr alloca; + Instr alloca; int ialloca; for (int i = 0; i < nret; ++i) align = align < (ralign = cls2siz[call->abiret[i].ty.cls]) ? ralign : align; @@ -281,14 +281,14 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi) if (!nret) /* hidden pointer argument */ insertinstr(blk, (*curi)++ - call->narg, - mkinstr(Oarg, 0, mktyperef((union irtype){.cls=KPTR}), retmem)); + mkinstr(Oarg, 0, mktyperef((IRType){.cls=KPTR}), retmem)); } /* adjust args */ for (int i = 0, i2 = ni + sretarghidden; i < call->narg; ++i) { int arginst = *curi - (call->narg - i); - struct instr *arg = &instrtab[blk->ins.p[arginst]]; - union irtype pty = ref2type(arg->l); + Instr *arg = &instrtab[blk->ins.p[arginst]]; + IRType pty = ref2type(arg->l); uchar r2off; int first = abiargs.n; int ret = abiarg(&abiargs, &r2off, &ni, &nf, &ns, pty); @@ -310,8 +310,8 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi) * must be emitted for the register allocator to know */ } } else { /* aggregate returned in regs */ - union ref r[2]; - struct instr ret2; + Ref r[2]; + Instr ret2; assert(in_range(nret, 1, 2)); ins->cls = call->abiret[0].ty.cls; r[0] = mkref(RTMP, ins - instrtab); @@ -320,11 +320,11 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi) r[1] = insertinstr(blk, ++*curi, ret2); } for (int i = 0; i < nret; ++i) { - struct instr store = { cls2store[call->abiret[i].ty.cls] }; + Instr store = { cls2store[call->abiret[i].ty.cls] }; if (i == 0) { store.l = retmem; } else { - struct instr addr = mkinstr(Oadd, KPTR, retmem, mkref(RICON, call->r2off)); + Instr addr = mkinstr(Oadd, KPTR, retmem, mkref(RICON, call->r2off)); store.l = insertinstr(blk, ++*curi, addr); } store.r = r[i]; @@ -333,25 +333,25 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi) } } - if (call->ret.isagg) call->ret = (union irtype){0}; + if (call->ret.isagg) call->ret = (IRType){0}; call->vararg = vararg; - call->abiarg = alloccopy(fn->arena, abiargs.p, abiargs.n * sizeof(struct abiarg), 0); + call->abiarg = alloccopy(fn->arena, abiargs.p, abiargs.n * sizeof(ABIArg), 0); call->narg = abiargs.n; vfree(&abiargs); } void -abi0(struct function *fn) +abi0(Function *fn) { - struct abiarg abiargsbuf[32]; + ABIArg abiargsbuf[32]; uint nparam = typedata[fn->fnty.dat].nmemb; - const union type *paramty = typedata[fn->fnty.dat].param; - struct abiargsvec abiargs = {VINIT(abiargsbuf, countof(abiargsbuf))}; + const Type *paramty = typedata[fn->fnty.dat].param; + ABIArgVec abiargs = VINIT(abiargsbuf, countof(abiargsbuf)); int rvovar = -1; int ni = 0, nf = 0, ns = 0, istart = 0; uchar r2off; - struct block *blk; - union ref sret = {0}; + Block *blk; + Ref sret = {0}; FREQUIRE(FNUSE); @@ -360,12 +360,12 @@ abi0(struct function *fn) } else { fn->nabiret = abiret(fn->abiret, &abiargs, &r2off, &ni, mkirtype(fn->retty)); if (!fn->nabiret && isagg(fn->retty)) { /* ret agg by hidden pointer */ - struct instr param = copyparam(fn, NULL, 0, abiargs.p[0]); + Instr param = copyparam(fn, NULL, 0, abiargs.p[0]); sret = insertinstr(fn->entry, 0, param); ++istart; /* increment real param ordinals */ for (int i = 1; i < fn->entry->ins.n; ++i) { - struct instr *ins = &instrtab[fn->entry->ins.p[i]]; + Instr *ins = &instrtab[fn->entry->ins.p[i]]; if (ins->op == Oparam) ++ins->l.i; } } @@ -373,7 +373,7 @@ abi0(struct function *fn) /* adjust params */ for (int i = 0, param = abiargs.n; i < nparam; ++i) { - union irtype pty = mkirtype(paramty[i]); + IRType pty = mkirtype(paramty[i]); int first = abiargs.n; uchar r2off; int ret = abiarg(&abiargs, &r2off, &ni, &nf, &ns, pty); @@ -389,7 +389,7 @@ abi0(struct function *fn) * (return value optimization (RVO)) */ blk = fn->entry; do { - union ref arg = blk->jmp.arg[0]; + Ref arg = blk->jmp.arg[0]; if (blk->jmp.t != Jret) continue; if (!arg.bits) continue; if (arg.t != RTMP || !oisalloca(instrtab[arg.i].op)) { @@ -412,7 +412,7 @@ abi0(struct function *fn) do { /* adjust vaargs and calls */ for (int iinstr = 0; iinstr < blk->ins.n; ++iinstr) { - struct instr *ins = &instrtab[blk->ins.p[iinstr]]; + Instr *ins = &instrtab[blk->ins.p[iinstr]]; if (ins->op == Ovastart) mctarg->vastart(fn, blk, &iinstr); else if (ins->op == Ovaarg) mctarg->vaarg(fn, blk, &iinstr); else if (ins->op == Ocall) abi0_call(fn, ins, blk, &iinstr); @@ -422,7 +422,7 @@ abi0(struct function *fn) if (isagg(fn->retty) && blk->jmp.t == Jret && blk->jmp.arg[0].bits) { assert(!blk->jmp.arg[1].bits); if (fn->nabiret) { /* aggregate return in register(s) */ - union ref r[2]; + Ref r[2]; int curi = blk->ins.n; load2regs(r, mkirtype(fn->retty), blk->jmp.arg[0], fn->nabiret, fn->abiret, r2off, blk, &curi); for (int i = 0; i < fn->nabiret; ++i) { @@ -433,7 +433,7 @@ abi0(struct function *fn) /* aggregate return (arg[0] is pointer to return value) */ if (rvovar == -1) { /* blit %sret, %arg */ - union irtype typ = mkirtype(fn->retty); + IRType typ = mkirtype(fn->retty); insertinstr(blk, blk->ins.n, mkarginstr(typ, sret)); insertinstr(blk, blk->ins.n, mkarginstr(typ, blk->jmp.arg[0])); insertinstr(blk, blk->ins.n, mkintrin(INstructcopy, 0, 2)); diff --git a/src/ir_builder.c b/src/ir_builder.c index 206e66d..0c4b910 100644 --- a/src/ir_builder.c +++ b/src/ir_builder.c @@ -1,12 +1,12 @@ #include "ir.h" /* binary arithmetic builder with peephole optimizations */ -union ref -irbinop(struct function *fn, enum op op, enum irclass k, union ref l, union ref r) +Ref +irbinop(Function *fn, enum op op, enum irclass k, Ref l, Ref r) { - static const union ref ONE = {.t=RICON, .i=1}; + static const Ref ONE = {.t=RICON, .i=1}; vlong iv; - union ref c; + Ref c; if (foldbinop(&c, op, k, l, r)) return c; @@ -104,13 +104,13 @@ irbinop(struct function *fn, enum op op, enum irclass k, union ref l, union ref } /* implements f32/f64 -> u64 conversion */ -static union ref -cvtfu64(struct function *fn, enum irclass from, union ref x) +static Ref +cvtfu64(Function *fn, enum irclass from, Ref x) { - struct block *t, *f, *merge; - union ref tmp, phiarg[2]; + Block *t, *f, *merge; + Ref tmp, phiarg[2]; /* if (x < 2p63) cvtfXs(x) else (cvtfXs(x - 2p63) | (1<<63)) */ - union ref max = mkfltcon(from, 0x1.0p63); + Ref max = mkfltcon(from, 0x1.0p63); enum op cvt = from == KF32 ? Ocvtf32s : Ocvtf64s; putcondbranch(fn, irbinop(fn, Olth, from, x, max), t = newblk(fn), f = newblk(fn)); @@ -129,11 +129,11 @@ cvtfu64(struct function *fn, enum irclass from, union ref x) } /* implements u64 -> f32/f64 conversion */ -static union ref -cvtu64f(struct function *fn, enum irclass to, union ref x) +static Ref +cvtu64f(Function *fn, enum irclass to, Ref x) { - struct block *t, *f, *merge; - union ref t1, t2, phiarg[2]; + Block *t, *f, *merge; + Ref t1, t2, phiarg[2]; /* if ((s64)x >= 0) cvts64f(x) else cvts64f((x>>1)|(x&1))*2 */ @@ -155,11 +155,11 @@ cvtu64f(struct function *fn, enum irclass to, union ref x) return addphi(fn, to, phiarg); } -union ref -irunop(struct function *fn, enum op op, enum irclass k, union ref a) +Ref +irunop(Function *fn, enum op op, enum irclass k, Ref a) { - union ref c; - struct instr *ins = NULL; + Ref c; + Instr *ins = NULL; if (foldunop(&c, op, k, a)) return c; if (a.t == RTMP) ins = &instrtab[a.i]; @@ -200,8 +200,8 @@ irunop(struct function *fn, enum op op, enum irclass k, union ref a) int allocinstr(void); -union ref -addinstr(struct function *fn, struct instr ins) +Ref +addinstr(Function *fn, Instr ins) { int new = allocinstr(); assert(fn->curblk != NULL); @@ -213,7 +213,7 @@ addinstr(struct function *fn, struct instr ins) } void -useblk(struct function *fn, struct block *blk) +useblk(Function *fn, Block *blk) { extern int nerror; if (fn->curblk && nerror == 0) assert(fn->curblk->jmp.t && "never finished block"); @@ -228,15 +228,15 @@ useblk(struct function *fn, struct block *blk) fn->curblk = blk; } -union ref -addphi(struct function *fn, enum irclass cls, union ref *r) +Ref +addphi(Function *fn, enum irclass cls, Ref *r) { assert(fn->curblk); if (fn->curblk->npred == 0) return UNDREF; if (fn->curblk->npred == 1) /* 1-argument phi is identity */ return *r; - union ref *refs = NULL; + Ref *refs = NULL; xbgrow(&refs, fn->curblk->npred); memcpy(refs, r, fn->curblk->npred * sizeof *r); vpush(&phitab, refs); @@ -259,7 +259,7 @@ addphi(struct function *fn, enum irclass cls, union ref *r) fn->curblk = NULL; void -putbranch(struct function *fn, struct block *blk) +putbranch(Function *fn, Block *blk) { assert(fn->curblk && blk); addpred(blk, fn->curblk); @@ -267,7 +267,7 @@ putbranch(struct function *fn, struct block *blk) } void -putcondbranch(struct function *fn, union ref arg, struct block *t, struct block *f) +putcondbranch(Function *fn, Ref arg, Block *t, Block *f) { assert(fn->curblk && t && f); if (iscon(arg)) { @@ -287,7 +287,7 @@ putcondbranch(struct function *fn, union ref arg, struct block *t, struct block } void -putreturn(struct function *fn, union ref r0, union ref r1) +putreturn(Function *fn, Ref r0, Ref r1) { assert(fn->curblk); adduse(fn->curblk, USERJUMP, r0); @@ -296,7 +296,7 @@ putreturn(struct function *fn, union ref r0, union ref r1) } void -puttrap(struct function *fn) +puttrap(Function *fn) { assert(fn->curblk); putjump(fn, Jtrap, NOREF, NOREF, NULL, NULL); diff --git a/src/ir_cfg.c b/src/ir_cfg.c index 86af3f3..dad7b1a 100644 --- a/src/ir_cfg.c +++ b/src/ir_cfg.c @@ -1,7 +1,7 @@ #include "ir.h" static void -porec(int *nblk, struct block ***rpo, struct block *b) +porec(int *nblk, Block ***rpo, Block *b) { if (wasvisited(b)) return; assert(*nblk > 0 && "nblk bad"); @@ -14,10 +14,10 @@ porec(int *nblk, struct block ***rpo, struct block *b) /* also blkid */ void -sortrpo(struct function *fn) +sortrpo(Function *fn) { - static struct block **rpobuf; - struct block **rpoend, **rpo, *blk, *next; + static Block **rpobuf; + Block **rpoend, **rpo, *blk, *next; int i, ndead; xbgrow(&rpobuf, fn->nblk); @@ -57,9 +57,9 @@ sortrpo(struct function *fn) /* also blkid */ void -filldom(struct function *fn) +filldom(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; int i = 0; FREQUIRE(FNRPO); @@ -71,14 +71,14 @@ filldom(struct function *fn) changed = 0; do { int j; - struct block *new = NULL; + Block *new = NULL; if (blk->npred == 0) continue; for (j = 0; j < blk->npred; ++j) if ((new = blkpred(blk, j))->id < blk->id) break; assert(new); for (int i = 0; i < blk->npred; ++i) { if (i == j) continue; - struct block *p = blkpred(blk, i); + Block *p = blkpred(blk, i); if (p->idom) { /* new = intersect(p, new) */ while (p != new) { while (p->id > new->id) p = p->idom; @@ -96,7 +96,7 @@ filldom(struct function *fn) } static void -loopmark(struct block *head, struct block *blk) +loopmark(Block *head, Block *blk) { if (blk->id < head->id || blk->visit == head->id) return; blk->visit = head->id; @@ -106,9 +106,9 @@ loopmark(struct block *head, struct block *blk) } void -fillloop(struct function *fn) +fillloop(Function *fn) { - struct block *b = fn->entry; + Block *b = fn->entry; int id = 0; FREQUIRE(FNRPO); do { @@ -118,7 +118,7 @@ fillloop(struct function *fn) } while ((b = b->lnext) != fn->entry); do { for (int i = 0; i < b->npred; ++i) { - struct block *p = blkpred(b, i); + Block *p = blkpred(b, i); if (p->id > b->id) { /* b is loop header */ loopmark(b, p); } diff --git a/src/ir_cse.c b/src/ir_cse.c index 62976a2..6007f8c 100644 --- a/src/ir_cse.c +++ b/src/ir_cse.c @@ -2,13 +2,13 @@ #include "u_hash.h" static inline bool -pure(const struct instr *ins) +pure(const Instr *ins) { return oisarith(ins->op) || (oisload(ins->op) && !ins->keep); } static inline bool -insequ(const struct instr *a, const struct instr *b) +insequ(const Instr *a, const Instr *b) { if (a->op != b->op) return 0; enum op op = a->op; @@ -20,21 +20,21 @@ insequ(const struct instr *a, const struct instr *b) return 1; } -static struct ht { +static struct Ht { uint t; ushort memno, cutoff; - struct block *b; + Block *b; } *insht; static uint ninsht; static inline size_t -hashins(const struct instr *ins) +hashins(const Instr *ins) { return hashb(0, ins, sizeof *ins); } static bool -doms(struct block *blk, struct block *b) +doms(Block *blk, Block *b) { for (;; b = b->idom) { if (blk == b) return 1; @@ -44,13 +44,13 @@ doms(struct block *blk, struct block *b) } static int -uniq(int t, struct block *blk, int cutoff, int memno) +uniq(int t, Block *blk, int cutoff, int memno) { assert((uint)t < MAXINSTR); - struct instr *ins = &instrtab[t]; + Instr *ins = &instrtab[t]; if (!pure(ins)) return t; for (size_t h = hashins(&instrtab[t]), i = h;; ++i) { - struct ht *p = &insht[i &= (ninsht-1)]; + struct Ht *p = &insht[i &= (ninsht-1)]; if (!p->b) Put: { p->b = blk; p->memno = memno; @@ -65,14 +65,14 @@ uniq(int t, struct block *blk, int cutoff, int memno) } void -cselim(struct function *fn) +cselim(Function *fn) { FREQUIRE(FNUSE | FNRPO | FNDOM | FNBLKID); extern int ninstr; for (ninsht = 32; ninsht <= ninstr; ninsht *= 2) ; insht = allocz(fn->passarena, ninsht * sizeof *insht, 0); int memno = 0, cutoff = 0; - struct block *blk = fn->entry; + Block *blk = fn->entry; do { ++memno; for (int i = 0; i < blk->ins.n; ++i) { diff --git a/src/ir_dump.c b/src/ir_dump.c index 1036135..a3f59f7 100644 --- a/src/ir_dump.c +++ b/src/ir_dump.c @@ -4,10 +4,10 @@ static int nextdat; -static struct wbuf *out = &bstdout; +static WriteBuf *out = &bstdout; static bool -prilitdat(const struct irdat *dat, const char *prefix) +prilitdat(const IRDat *dat, const char *prefix) { uchar *p; switch (dat->section) { @@ -29,7 +29,7 @@ prilitdat(const struct irdat *dat, const char *prefix) } static void -pridat(const struct irdat *dat) +pridat(const IRDat *dat) { static const char *snames[] = { [Sdata] = ".data", [Srodata] = ".rodata", [Stext] = ".text" }; uchar *p; @@ -74,12 +74,12 @@ static const char *clsname[] = { }; static void -prityp(union irtype typ) +prityp(IRType typ) { if (!typ.isagg) bfmt(out, clsname[typ.cls]); else { - const struct typedata *td = &typedata[typ.dat]; + const TypeData *td = &typedata[typ.dat]; const char *tag = td->t == TYSTRUCT ? "struct" : "union"; if (ttypenames[td->id]) bfmt(out, "%s.%s.%d", tag, ttypenames[td->id], td->id); @@ -96,9 +96,9 @@ static const char *intrinname[] = { }; static void -dumpref(enum op o, union ref ref) +dumpref(enum op o, Ref ref) { - struct xcon *con; + IRCon *con; switch (ref.t) { case RXXX: if (ref.bits == UNDREF.bits) @@ -124,10 +124,10 @@ dumpref(enum op o, union ref ref) if (con->issym || con->isdat) { bfmt(out, "$%y", xcon2sym(ref.i)); if (con->isdat) { - struct irdat *dat = &dattab.p[con->dat]; + IRDat *dat = &dattab.p[con->dat]; if (prilitdat(dat, " (= ")) { if (isscalar(dat->ctype)) { - struct wbuf tmp = MEMBUF((char [1]){0}, 1); + WriteBuf tmp = MEMBUF((char [1]){0}, 1); bfmt(&tmp, "%ty", dat->ctype); ioputc(out, *tmp.buf); } @@ -149,7 +149,7 @@ dumpref(enum op o, union ref ref) break; case RADDR: { - const struct addr *addr = &addrtab.p[ref.i]; + const IRAddr *addr = &addrtab.p[ref.i]; bool k = 0; bfmt(out, "addr ["); if ((k = addr->base.bits)) dumpref(0, addr->base); @@ -175,7 +175,7 @@ dumpref(enum op o, union ref ref) } static void -dumpcall(struct call *call) +dumpcall(IRCall *call) { if (call->ret.isagg) { bfmt(out, "sret "); @@ -191,7 +191,7 @@ dumpcall(struct call *call) } static void -dumpinst(const struct instr *ins) +dumpinst(const Instr *ins) { int i; if (ins->op == Omove) { @@ -225,7 +225,7 @@ dumpinst(const struct instr *ins) } void -dumpblk(struct function *fn, struct block *blk) +dumpblk(Function *fn, Block *blk) { static const char *jnames[] = { 0, "b", "ret", "trap" }; int i; @@ -243,8 +243,8 @@ dumpblk(struct function *fn, struct block *blk) bfmt(out, "\t; loop depth: %d", blk->loop); ioputc(out, '\n'); for (i = 0; i < blk->phi.n; ++i) { - struct instr *phi = &instrtab[blk->phi.p[i]]; - union ref *refs = phitab.p[phi->l.i]; + Instr *phi = &instrtab[blk->phi.p[i]]; + Ref *refs = phitab.p[phi->l.i]; if (i == 0) bfmt(out, "%-4d", blk->inumstart); else bfmt(out, " |> "); bfmt(out, " %s ", clsname[phi->cls]); @@ -279,9 +279,9 @@ dumpblk(struct function *fn, struct block *blk) } void -irdump(struct function *fn) +irdump(Function *fn) { - struct block *blk; + Block *blk; /* print datas that have never been printed before */ while (nextdat < dattab.n) pridat(&dattab.p[nextdat++]); diff --git a/src/ir_fold.c b/src/ir_fold.c index a7b5c6e..9f0ff72 100644 --- a/src/ir_fold.c +++ b/src/ir_fold.c @@ -5,8 +5,8 @@ #ifdef __clang__ __attribute__((no_sanitize("float-cast-overflow"))) /* silence UBsan for float->int overflow */ #endif -static union ref -foldint(enum op op, enum irclass k, union ref lr, union ref rr) +static Ref +foldint(enum op op, enum irclass k, Ref lr, Ref rr) { vlong x; union { @@ -71,8 +71,8 @@ foldint(enum op op, enum irclass k, union ref lr, union ref rr) return mkintcon(k, x); } -static union ref -foldflt(enum op op, enum irclass k, union ref lr, union ref rr) +static Ref +foldflt(enum op op, enum irclass k, Ref lr, Ref rr) { int xi; double x, l = fltconval(lr), r = fltconval(rr); @@ -103,7 +103,7 @@ foldflt(enum op op, enum irclass k, union ref lr, union ref rr) } bool -foldbinop(union ref *to, enum op op, enum irclass k, union ref l, union ref r) +foldbinop(Ref *to, enum op op, enum irclass k, Ref l, Ref r) { if (!oisarith(op)) return 0; @@ -118,7 +118,7 @@ foldbinop(union ref *to, enum op op, enum irclass k, union ref l, union ref r) } bool -foldunop(union ref *to, enum op op, enum irclass k, union ref a) +foldunop(Ref *to, enum op op, enum irclass k, Ref a) { if (!isnumcon(a)) return 0; if (op != Ocopy && !oisarith(op)) diff --git a/src/ir_inliner.c b/src/ir_inliner.c index b99d3dc..c3e0777 100644 --- a/src/ir_inliner.c +++ b/src/ir_inliner.c @@ -1,27 +1,27 @@ #include "ir.h" -struct savedfunc { +typedef struct SavedFunc { uint ninstr; - struct instr *instrtab; - struct xcon *contab; - struct call *calltab; - union ref **phitab; - struct block *entry; - union type fnty, retty; - struct abiarg *abiarg, abiret[2]; + Instr *instrtab; + IRCon *contab; + IRCall *calltab; + Ref **phitab; + Block *entry; + Type fnty, retty; + ABIArg *abiarg, abiret[2]; ushort nabiarg, nabiret; ushort nretpoints; -}; +} SavedFunc; enum { MAX_INLINED_FN_NINS = 50, MAX_INLINED_FN_NBLK = 16, }; -static pmap_of(struct savedfunc *) savedfns; +static pmap_of(SavedFunc *) savedfns; bool -maybeinlinee(struct function *fn) +maybeinlinee(Function *fn) { - static struct arena *savearena; + static Arena *savearena; extern int ninstr; // TODO better heuristics @@ -37,7 +37,7 @@ maybeinlinee(struct function *fn) if (!savearena) { enum { N = 1<<12 }; - static union { char m[sizeof(struct arena) + N]; struct arena *_align; } amem; + static union { char m[sizeof(Arena) + N]; Arena *_align; } amem; savearena = (void *)amem.m; savearena->cap = N; } @@ -45,19 +45,19 @@ maybeinlinee(struct function *fn) if (ccopt.dbg.y) { bfmt(ccopt.dbgout, "> stashing '%s' for inlining\n", fn->name); } - struct savedfunc *sv = allocz(&savearena, sizeof *sv, 0); + SavedFunc *sv = allocz(&savearena, sizeof *sv, 0); sv->fnty = fn->fnty, sv->retty = fn->retty; if (fn->abiarg) sv->abiarg = alloccopy(&savearena, fn->abiarg, sizeof *sv->abiarg * fn->nabiarg, 0); sv->nabiarg = fn->nabiarg; if ((sv->nabiret = fn->nabiret) > 0) memcpy(sv->abiret, fn->abiret, sizeof sv->abiret); - struct block *bmap[MAX_INLINED_FN_NBLK]; - struct block *b = fn->entry; + Block *bmap[MAX_INLINED_FN_NBLK]; + Block *b = fn->entry; int id = 0; do { b->id = id++; - struct block *q = alloccopy(&savearena, b, sizeof *b, 0); + Block *q = alloccopy(&savearena, b, sizeof *b, 0); if (q->phi.n) q->phi.p = alloccopy(&savearena, q->phi.p, sizeof *q->phi.p * q->phi.n, 0); if (q->ins.n) @@ -100,8 +100,8 @@ maybeinlinee(struct function *fn) return 1; } -static union ref -mapref(short *instrmap, struct savedfunc *sv, union ref r) +static Ref +mapref(short *instrmap, SavedFunc *sv, Ref r) { assert(r.bits); if (r.t == RTMP) return r.i = instrmap[r.i], r; @@ -111,18 +111,18 @@ mapref(short *instrmap, struct savedfunc *sv, union ref r) return r; } -static struct block * -inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) +static Block * +inlcall(Function *fn, Block *blk, int curi, SavedFunc *sv) { int res = blk->ins.p[curi], res2; - struct instr *ins = &instrtab[res]; - struct call *call = &calltab.p[ins->r.i]; - union ref retvals[64]; - union ref args[64]; + Instr *ins = &instrtab[res]; + IRCall *call = &calltab.p[ins->r.i]; + Ref retvals[64]; + Ref args[64]; assert(sv->nabiret < 2 && sv->nretpoints < countof(retvals)); for (int n = call->narg, i = curi-1; n > 0; --i) { assert(i >= 0); - struct instr *ins = &instrtab[blk->ins.p[i]]; + Instr *ins = &instrtab[blk->ins.p[i]]; if (ins->op == Oarg) { args[--n] = ins->r; *ins = mkinstr(Onop,0,); @@ -133,7 +133,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) res2 = blk->ins.p[curi+1]; assert(instrtab[res2].op == Ocall2r); } - struct block *exit = blksplitafter(fn, blk, curi-1); + Block *exit = blksplitafter(fn, blk, curi-1); if (!ins->cls) { *ins = mkinstr(Onop,0,); } else { @@ -147,9 +147,9 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) } } - struct block *bmap[MAX_INLINED_FN_NBLK]; + Block *bmap[MAX_INLINED_FN_NBLK]; short instrmap[MAX_INLINED_FN_NINS]; - for (struct block *b = sv->entry; b; b = b->lnext) { + for (Block *b = sv->entry; b; b = b->lnext) { bmap[b->id] = newblk(fn); } for (int i = 0; i < sv->ninstr; ++i) { @@ -164,7 +164,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) exit->_pred0 = NULL; blk->s1 = bmap[0]; int iret = 0; - for (struct block *b = sv->entry, *prev = blk, *new; b; prev = new, b = b->lnext) { + for (Block *b = sv->entry, *prev = blk, *new; b; prev = new, b = b->lnext) { new = bmap[b->id]; new->id = fn->nblk++; new->lprev = prev; @@ -181,7 +181,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) vresize(&new->phi, b->phi.n); for (int i = 0; i < b->phi.n; ++i) { int t = b->phi.p[i]; - union ref *refs = NULL, + Ref *refs = NULL, *src = sv->phitab[sv->instrtab[t].l.i]; xbgrow(&refs, b->npred); for (int i = 0; i < b->npred; ++i) @@ -193,7 +193,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) vresize(&new->ins, b->ins.n); for (int i = 0; i < b->ins.n; ++i) { int t = b->ins.p[i]; - struct instr *ins = &instrtab[instrmap[t]]; + Instr *ins = &instrtab[instrmap[t]]; *ins = sv->instrtab[t]; if (ins->op == Oparam) { ins->op = Ocopy; @@ -201,7 +201,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) ins->l = args[ins->l.i]; } else if (ins->op == Ocall || ins->op == Ointrin) { ins->l = mapref(instrmap, sv, ins->l); - for (struct instr *ins2; + for (Instr *ins2; ins->l.t == RTMP && (ins2 = &instrtab[ins->l.i])->op == Ocopy && (isaddrcon(ins2->l,0) || (ins2->l.t == RTMP && instrtab[ins->l.i].cls == KPTR));) { /* for an indirect function call, eagerly copy-propagate the callee. this allows @@ -242,7 +242,7 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) ins->l = retvals[0]; } else { /* fill phi */ - union ref *refs = NULL; + Ref *refs = NULL; xbgrow(&refs, sv->nretpoints); memcpy(refs, retvals, sizeof *refs * sv->nretpoints); vpush(&phitab, refs); @@ -256,13 +256,13 @@ inlcall(struct function *fn, struct block *blk, int curi, struct savedfunc *sv) enum { MAX_REC_INLINE = 16 }; void -doinline(struct function *fn) +doinline(Function *fn) { if (calltab.n == 0) return; - struct block *b = fn->entry; - struct stack { /* stack of callees being inline expanded */ - struct block *b; /* block after the end of expansion */ - struct savedfunc *sv; + Block *b = fn->entry; + struct Stack { /* stack of callees being inline expanded */ + Block *b; /* block after the end of expansion */ + SavedFunc *sv; } stkbuf[MAX_REC_INLINE], *stk = stkbuf + MAX_REC_INLINE, *stkend = stk; bool dumpbefore = 0; do { @@ -271,18 +271,18 @@ doinline(struct function *fn) else if (stk == stkbuf) /* stack full? */ continue; for (int i = 0; i < b->ins.n; ++i) { - struct instr *ins = &instrtab[b->ins.p[i]]; + Instr *ins = &instrtab[b->ins.p[i]]; if (ins->op != Ocall) continue; if (!isaddrcon(ins->l,0)) continue; - struct call *call = &calltab.p[ins->r.i]; + IRCall *call = &calltab.p[ins->r.i]; internstr fname = xcon2sym(ins->l.i); - struct savedfunc **pcallee, *sv; + SavedFunc **pcallee, *sv; if ((pcallee = pmap_get(&savedfns, fname)) && (sv = *pcallee)->nabiarg == call->narg && call->vararg == -1 && call->narg == sv->nabiarg && (!call->narg || !memcmp(sv->abiarg, call->abiarg, sizeof *sv->abiarg * sv->nabiarg)) && !memcmp(sv->abiret, call->abiret, sizeof sv->abiret)) { - for (struct stack *s = stk; s != stkend; ++s) { + for (struct Stack *s = stk; s != stkend; ++s) { if (s->sv == sv) goto Skip; /* recursion encountered */ } if (ccopt.dbg.y) { diff --git a/src/ir_intrin.c b/src/ir_intrin.c index ca49341..6c73784 100644 --- a/src/ir_intrin.c +++ b/src/ir_intrin.c @@ -1,13 +1,13 @@ #include "ir.h" -struct arg { union ref *arg, *ty; }; +typedef struct { Ref *arg, *ty; } Arg; static int -intrin(struct block *blk, int *curi, enum intrin in, struct arg *args, int narg, union irtype ret) +intrin(Block *blk, int *curi, enum intrin in, Arg *args, int narg, IRType ret) { - struct instr *this = &instrtab[blk->ins.p[*curi]]; - const struct typedata *td; - union irtype ty; + Instr *this = &instrtab[blk->ins.p[*curi]]; + const TypeData *td; + IRType ty; uint ncopy, step; switch (in) { @@ -31,7 +31,7 @@ intrin(struct block *blk, int *curi, enum intrin in, struct arg *args, int narg, } else { delinstr(blk, (*curi)--); for (int off = 0; off < td->siz; off += step) { - union ref psrc = *args[1].arg, pdst = *args[0].arg, src; + Ref psrc = *args[1].arg, pdst = *args[0].arg, src; if (off) { pdst = insertinstr(blk, ++*curi, mkinstr(Oadd, KPTR, *args[0].arg, mkref(RICON, off))); psrc = insertinstr(blk, ++*curi, mkinstr(Oadd, KPTR, *args[1].arg, mkref(RICON, off))); @@ -46,17 +46,17 @@ intrin(struct block *blk, int *curi, enum intrin in, struct arg *args, int narg, } void -lowerintrin(struct function *fn) +lowerintrin(Function *fn) { - struct block *blk = fn->entry; - struct arg argsbuf[32]; - vec_of(struct arg) args = VINIT(argsbuf, countof(argsbuf)); + Block *blk = fn->entry; + Arg argsbuf[32]; + vec_of(Arg) args = VINIT(argsbuf, countof(argsbuf)); do { for (int i = 0; i < blk->ins.n; ++i) { - struct instr *ins = &instrtab[blk->ins.p[i]]; + Instr *ins = &instrtab[blk->ins.p[i]]; if (ins->op == Oarg) - vpush(&args, ((struct arg){ &ins->r, &ins->l })); + vpush(&args, ((Arg){&ins->r, &ins->l})); else if (ins->op == Ocall) vinit(&args, argsbuf, countof(argsbuf)); else if (ins->op == Ointrin) { diff --git a/src/ir_mem2reg.c b/src/ir_mem2reg.c index e684e62..8890439 100644 --- a/src/ir_mem2reg.c +++ b/src/ir_mem2reg.c @@ -31,23 +31,23 @@ static const uchar storesz[] = { /* Implements algorithm in 'Simple and Efficient Construction of Static Single Assignment' (Braun et al) */ -struct ssabuilder { - struct arena **arena; - imap_of(union ref *) curdefs; /* map of var to (map of block to def of var) */ - struct bitset *sealed, /* set of sealed blocks */ - *marked; /* blocks marked, for 'Marker Algorithm' in the paper */ +typedef struct SSABuilder { + Arena **arena; + imap_of(Ref *) curdefs; /* map of var to (map of block to def of var) */ + BitSet *sealed, /* set of sealed blocks */ + *marked; /* blocks marked, for 'Marker Algorithm' in the paper */ int lastvisit; int nblk; -}; +} SSABuilder; -static union ref readvar(struct ssabuilder *, int var, enum irclass, struct block *); +static Ref readvar(SSABuilder *, int var, enum irclass, Block *); -static union ref -deltrivialphis(struct ssabuilder *sb, int var, struct block *blk, union ref phiref) +static Ref +deltrivialphis(SSABuilder *sb, int var, Block *blk, Ref phiref) { assert(instrtab[phiref.i].op == Ophi); - union ref *args = phitab.p[instrtab[phiref.i].l.i]; - union ref same = {0}; + Ref *args = phitab.p[instrtab[phiref.i].l.i]; + Ref same = {0}; for (int i = 0; i < blk->npred; ++i) { if (args[i].bits == same.bits || args[i].bits == phiref.bits) { continue; /* unique value or self-reference */ @@ -62,7 +62,7 @@ deltrivialphis(struct ssabuilder *sb, int var, struct block *blk, union ref phir /* replace uses */ replcuses(phiref, same); - union ref **pcurdefs = imap_get(&sb->curdefs, var); + Ref **pcurdefs = imap_get(&sb->curdefs, var); assert (pcurdefs); for (int i = blk->id; i < sb->nblk; ++i) { if ((*pcurdefs)[i].bits == phiref.bits) @@ -74,10 +74,10 @@ deltrivialphis(struct ssabuilder *sb, int var, struct block *blk, union ref phir /* recursively try to remove all phi users as they might have become trivial */ Redo: - for (struct use *use = instruse[phiref.i]; use; use = use->next) { + for (IRUse *use = instruse[phiref.i]; use; use = use->next) { if (use->u != USERJUMP && instrtab[use->u].op == Ophi && use->u != phiref.i) { - union ref it = mkref(RTMP, use->u); - union ref vphi2 = deltrivialphis(sb, var, use->blk, it); + Ref it = mkref(RTMP, use->u); + Ref vphi2 = deltrivialphis(sb, var, use->blk, it); if (vphi2.bits != it.bits) { same = vphi2; /* deletion happened so phiref use may have changed */ @@ -90,12 +90,12 @@ Redo: return same; } -static union ref -addphiargs(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk, union ref phiref) +static Ref +addphiargs(SSABuilder *sb, int var, enum irclass cls, Block *blk, Ref phiref) { - union ref *args = phitab.p[instrtab[phiref.i].l.i]; + Ref *args = phitab.p[instrtab[phiref.i].l.i]; for (int i = 0; i < blk->npred; ++i) { - struct block *pred = blkpred(blk, i); + Block *pred = blkpred(blk, i); args[i] = readvar(sb, var, cls, pred); adduse(blk, phiref.i, args[i]); } @@ -104,21 +104,21 @@ addphiargs(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk, } static void -writevar(struct ssabuilder *sb, int var, struct block *blk, union ref val) +writevar(SSABuilder *sb, int var, Block *blk, Ref val) { - union ref **pcurdefs; + Ref **pcurdefs; if (!(pcurdefs = imap_get(&sb->curdefs, var))) { - pcurdefs = imap_set(&sb->curdefs, var, allocz(sb->arena, sb->nblk * sizeof(union ref), 0)); + pcurdefs = imap_set(&sb->curdefs, var, allocz(sb->arena, sb->nblk * sizeof(Ref), 0)); } if (val.t == RTMP) assert(instrtab[val.i].op != Onop); (*pcurdefs)[blk->id] = val; } enum { RPENDINGPHI = 7 }; -static union ref -readvarrec(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk) +static Ref +readvarrec(SSABuilder *sb, int var, enum irclass cls, Block *blk) { - union ref val; + Ref val; assert(blk->npred > 0); if (!bstest(sb->sealed, blk->id)) { /* unsealed block */ /* add pending phi */ @@ -134,11 +134,11 @@ readvarrec(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk) */ bsset(sb->marked, blk->id); for (int i = 0; i < blk->npred; ++i) { - struct block *pred = blkpred(blk, i); - union ref it = readvar(sb, var, cls, pred); + Block *pred = blkpred(blk, i); + Ref it = readvar(sb, var, cls, pred); if (!bstest(sb->marked, blk->id)) { /* recursion reached this blk again, use its phi */ - union ref **pcurdefs = imap_get(&sb->curdefs, var); + Ref **pcurdefs = imap_get(&sb->curdefs, var); /* must have called writevar */ assert(*pcurdefs && (*pcurdefs)[blk->id].bits); return (*pcurdefs)[blk->id]; @@ -160,10 +160,10 @@ readvarrec(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk) return val; } -static union ref -readvar(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk) +static Ref +readvar(SSABuilder *sb, int var, enum irclass cls, Block *blk) { - union ref **pcurdefs; + Ref **pcurdefs; if ((pcurdefs = imap_get(&sb->curdefs, var)) && (*pcurdefs)[blk->id].bits) return (*pcurdefs)[blk->id]; if (blk->npred == 0) /* entry block, var is read before being written to */ @@ -172,21 +172,21 @@ readvar(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk) } static bool -trysealrec(struct ssabuilder *sb, struct block *blk) +trysealrec(SSABuilder *sb, Block *blk) { Recur: if (bstest(sb->sealed, blk->id)) return 1; if (blk->id > sb->lastvisit) return 0; markvisited(blk); for (int i = 0; i < blk->npred; ++i) { - struct block *p = blkpred(blk, i); + Block *p = blkpred(blk, i); if (wasvisited(p)) continue; if (p->id > sb->lastvisit) return 0; } bsset(sb->sealed, blk->id); for (int i = 0; i < blk->phi.n; ++i) { - struct instr *ins = &instrtab[blk->phi.p[i]]; + Instr *ins = &instrtab[blk->phi.p[i]]; if (ins->r.t == RPENDINGPHI) addphiargs(sb, ins->r.i, ins->cls, blk, mkref(RTMP, blk->phi.p[i])); } @@ -202,14 +202,14 @@ Recur: } static void -tryseal(struct ssabuilder *sb, struct block *blk) +tryseal(SSABuilder *sb, Block *blk) { startbbvisit(); trysealrec(sb, blk); } static int -blkfindins(struct block *blk, int ins) +blkfindins(Block *blk, int ins) { for (int i = 0; i < blk->phi.n; ++i) if (blk->phi.p[i] == ins) return -1; @@ -222,17 +222,17 @@ static int rcmpuse(const void *a, const void *b) { /* postorder sort */ - const struct use *ua = *(struct use **)a, *ub = *(struct use **)b; - struct block *blk = ua->blk; + const IRUse *ua = *(IRUse **)a, *ub = *(IRUse **)b; + Block *blk = ua->blk; if (ua->blk != ub->blk) return ub->blk->id - ua->blk->id; assert(ua->u != USERJUMP && ub->u != USERJUMP); return blkfindins(blk, ub->u) - blkfindins(blk, ua->u); } void -mem2reg(struct function *fn) +mem2reg(Function *fn) { - struct ssabuilder sb = { fn->passarena, .nblk = fn->nblk }; + SSABuilder sb = { fn->passarena, .nblk = fn->nblk }; FREQUIRE(FNUSE); @@ -240,23 +240,23 @@ mem2reg(struct function *fn) sb.marked = allocz(sb.arena, BSSIZE(fn->nblk) * sizeof *sb.sealed, 0); sortrpo(fn); - struct block *blk = fn->entry; + Block *blk = fn->entry; do { for (int i = 0; i < blk->ins.n; ++i) { enum irclass k = 0; int sz = 0; enum op ext = Ocopy; int var = blk->ins.p[i]; - struct instr *ins = &instrtab[var]; - struct use *use; + Instr *ins = &instrtab[var]; + IRUse *use; /* find allocas only used in loads/stores of uniform size */ if (!oisalloca(ins->op) || !(use = instruse[var])) continue; - struct use *usesbuf[64]; - vec_of(struct use *) uses = VINIT(usesbuf, countof(usesbuf)); + IRUse *usesbuf[64]; + vec_of(IRUse *) uses = VINIT(usesbuf, countof(usesbuf)); do { if (use->u == USERJUMP) goto Skip; - struct instr *m = &instrtab[use->u]; + Instr *m = &instrtab[use->u]; if (oisload(m->op) && (!sz || sz == loadsz(m->op))) { sz = loadsz(m->op); k = loadcls(m->op); @@ -278,7 +278,7 @@ mem2reg(struct function *fn) writevar(&sb, var, use->blk, ins->r); *ins = mkinstr(Onop,0,); } else if (oisload(ins->op)) { - union ref val = readvar(&sb, var, k = ins->cls, use->blk); + Ref val = readvar(&sb, var, k = ins->cls, use->blk); adduse(use->blk, use->u, val); if (ext != Ocopy && isintcon(val)) { /* fold constant int extension */ val = irunop(NULL, ext, k, val); diff --git a/src/ir_regalloc.c b/src/ir_regalloc.c index fbe80ca..3a6e912 100644 --- a/src/ir_regalloc.c +++ b/src/ir_regalloc.c @@ -20,25 +20,25 @@ * appear later than some of its uses is very similar to that in mem2reg() */ static int livelastblk; -struct pendingphi { ushort var, phi; }; -static vec_of(struct pendingphi) *pendingphis; +typedef struct { ushort var, phi; } PendingPhi; +static vec_of(PendingPhi) *pendingphis; static int npendingphi; static ushort **curdefs; -static union ref readvar(struct bitset *defined, enum irclass cls, int var, struct block *blk); +static Ref readvar(BitSet *defined, enum irclass cls, int var, Block *blk); static void -fillphi(struct bitset *defined, union ref phi, enum irclass cls, int var, struct block *blk) +fillphi(BitSet *defined, Ref phi, enum irclass cls, int var, Block *blk) { - union ref *args = phitab.p[instrtab[phi.i].l.i]; + Ref *args = phitab.p[instrtab[phi.i].l.i]; assert(blk->npred > 0); for (int i = 0; i < blk->npred; ++i) args[i] = readvar(defined, cls, var, blk); } -static union ref -readvar(struct bitset *defined, enum irclass cls, int var, struct block *blk) +static Ref +readvar(BitSet *defined, enum irclass cls, int var, Block *blk) { - union ref val; + Ref val; if (bstest(defined, var)) return mkref(RTMP, var); assert(cls && "?"); @@ -52,7 +52,7 @@ readvar(struct bitset *defined, enum irclass cls, int var, struct block *blk) ++npendingphi; val = insertphi(blk, cls); xbgrowz(&pendingphis, blk->id + 1); - vpush(&pendingphis[blk->id], ((struct pendingphi) { var, val.i })); + vpush(&pendingphis[blk->id], ((PendingPhi) { var, val.i })); } else if (blk->npred == 1) { val = readvar(defined, cls, var, blkpred(blk, 0)); } else { @@ -66,7 +66,7 @@ readvar(struct bitset *defined, enum irclass cls, int var, struct block *blk) } static void -liveuse(struct bitset *defined, struct instr *ins, union ref *r, struct block *blk) +liveuse(BitSet *defined, Instr *ins, Ref *r, Block *blk) { int var; if (r->t == RADDR) { @@ -83,12 +83,12 @@ liveuse(struct bitset *defined, struct instr *ins, union ref *r, struct block *b /* regalloc() assumes every use of a temporary is visited before its definition * so this pass fixes cases where that would not apply by introducing phi functions */ static void -fixlive(struct function *fn) +fixlive(Function *fn) { extern int ninstr; - struct block *blk = fn->entry; - struct bitset definedbuf[4] = {0}; - struct bitset *defined = definedbuf; + Block *blk = fn->entry; + BitSet definedbuf[4] = {0}; + BitSet *defined = definedbuf; if (BSSIZE(ninstr) >= countof(definedbuf)) defined = xcalloc(sizeof *defined * BSSIZE(ninstr)); @@ -102,7 +102,7 @@ fixlive(struct function *fn) for (int i = 0; i < blk->ins.n; ++i) { int var = blk->ins.p[i]; - struct instr *ins = &instrtab[var]; + Instr *ins = &instrtab[var]; if (ins->l.t) liveuse(defined, ins, &ins->l, blk); if (ins->r.t) liveuse(defined, ins, &ins->r, blk); bsset(defined, var); @@ -110,7 +110,7 @@ fixlive(struct function *fn) } while ((blk = blk->lnext) != fn->entry); do { - vec_of(struct pendingphi) *pphi; + vec_of(PendingPhi) *pphi; if (!npendingphi) break; if (xbcap(pendingphis) <= blk->id) break; @@ -137,21 +137,20 @@ static regset gpregset, fpregset; /* an allocated physical register or stack slot */ enum { ADEAD, AREG, ASTACK }; -union alloc { struct { ushort t : 2, a : 14; }; ushort bits; }; -#define afree() ((union alloc) { .t=ADEAD }) -#define areg(r) ((union alloc) { .t=AREG, .a=(r) }) -#define astack(s) ((union alloc) { .t=ASTACK, .a=(s) }) +typedef union { struct { ushort t : 2, a : 14; }; ushort bits; } Alloc; +#define afree() ((Alloc) { .t=ADEAD }) +#define areg(r) ((Alloc) { .t=AREG, .a=(r) }) +#define astack(s) ((Alloc) { .t=ASTACK, .a=(s) }) enum { MAXSPILL = 512 }; /* half-closed instr range [from, to) */ -struct range { ushort from, to; }; -#define mkrange(f,t) ((struct range){(f), (t)}) +typedef struct { ushort from, to; } Range; /* a temporary's lifetime interval */ -struct interval { - struct interval *next; /* for linked list of active,inactive,handled sets in linear scan */ - union alloc alloc; +typedef struct Interval { + struct Interval *next; /* for linked list of active,inactive,handled sets in linear scan */ + Alloc alloc; schar rhint : 7; /* register hint */ bool fpr : 1; /* needs float register? */ uint cost; /* spilling cost estimate */ @@ -159,58 +158,66 @@ struct interval { /* sorted ranges array */ ushort nrange; union { - struct range _rinl[2]; - struct range *_rdyn; + Range _rinl[2]; + Range *_rdyn; }; -}; - -struct rega { - struct function *fn; - struct arena **arena; +} Interval; + +/* fixed intervals represent register constraints */ +typedef struct FixInterval { + struct FixInterval *next; + regset rs; + Range range; + /* unlike Interval, there is only one range because it's unnecessary to take + * gaps into account, since they are already "allocated"; the same + * register(s) can appear in different disjoint FixIntervals */ +} FixInterval; + +typedef struct RegAlloc { + Function *fn; + Arena **arena; int intercount; /* number of actual intervals */ int ninter; /* size of inter */ - struct interval *inter; /* map of tmp -> interval */ - struct fixinterval { - struct fixinterval *next; - regset rs; - struct range range; - } *fixed; /* linked list of fixed intervals, always sorted */ - - struct bitset freestk[BSSIZE(MAXSPILL)]; /* free stack slots */ + Interval *inter; /* map of tmp -> interval */ + FixInterval *fixed; /* linked list of fixed intervals, always sorted */ + + BitSet freestk[BSSIZE(MAXSPILL)]; /* free stack slots */ int maxstk, /* highest stack slot used */ stktop; -}; +} RegAlloc; -#define stkslotref(fn, off) \ - (mkaddr((struct addr){.base = mkref(RREG, mctarg->bpr), .disp = -(fn)->stksiz - 8 - (off)})) +#define stkslotref(fn, off) \ + mkaddr((IRAddr){.base = mkref(RREG, mctarg->bpr), \ + .disp = -(fn)->stksiz - 8 - (off)}) -/* Parallel moves algorithm from QBE: https://c9x.me/git/qbe.git/tree/rega.c?id=e493a7f23352f51acc0a1e12284ab19d7894488a#n201 */ +/* Parallel moves algorithm from QBE + * <https://c9x.me/git/qbe.git/tree/rega.c?id=e493a7f23352f51acc0a1e12284ab19d7894488a#n201> */ -enum { PMTOMOVE, PMMOVING, PMDONE }; -struct pmstate { - struct function *fn; +enum pmstat { PMTOMOVE, PMMOVING, PMDONE }; +typedef struct { + Function *fn; int npmove; - struct pmove { + struct PMove { uchar k; /* enum irclass */ - uchar stat; /* PMTOMOVE|MOVING|DONE */ - union alloc dst, src; + uchar stat; /* enum pmstat */ + Alloc dst, src; } pmove[MAXREGS]; -}; +} PMState; static void -pmadd(struct pmstate *pms, enum irclass k, union alloc dst, union alloc src) +pmadd(PMState *pms, enum irclass k, Alloc dst, Alloc src) { if (!memcmp(&dst, &src, sizeof dst)) return; assert(pms->npmove < MAXREGS); - pms->pmove[pms->npmove++] = (struct pmove) { k, PMTOMOVE, dst, src }; + pms->pmove[pms->npmove++] = (struct PMove) { k, PMTOMOVE, dst, src }; } #define mkmove(k, rd, rs) mkinstr(Omove, k, mkref(RREG, rd), mkref(RREG, rs)) static void -emitmove(struct function *fn, enum irclass k, union alloc dst, union alloc src, struct block *blk, int curi) +emitmove(Function *fn, enum irclass k, Alloc dst, Alloc src, Block *blk, int curi) { - struct instr mv = {.keep = 1}; + Instr mv = {.keep = 1}; int reg; if (dst.t == AREG && src.t == AREG) { insertinstr(blk, curi, mkmove(k, dst.a, src.a)); @@ -240,9 +247,9 @@ emitmove(struct function *fn, enum irclass k, union alloc dst, union alloc src, } static int -pmrec(struct pmstate *pms, int i, struct block *blk, int curi, enum irclass *k) +pmrec(PMState *pms, int i, Block *blk, int curi, enum irclass *k) { - struct pmove *pm = &pms->pmove[i]; + struct PMove *pm = &pms->pmove[i]; if (pm->dst.bits == pm->src.bits) { pm->stat = PMDONE; return -1; @@ -268,7 +275,7 @@ pmrec(struct pmstate *pms, int i, struct block *blk, int curi, enum irclass *k) insertinstr(blk, curi, mkinstr(Oswap, *k, mkref(RREG, pm->dst.a), mkref(RREG, pm->src.a), .keep = 1)); } else if (pm->src.t != pm->dst.t) { - union alloc reg, stk, regtmp; + Alloc reg, stk, regtmp; if (pm->src.t == AREG) reg = pm->src, stk = pm->dst; else @@ -312,7 +319,7 @@ pmrec(struct pmstate *pms, int i, struct block *blk, int curi, enum irclass *k) } static void -emitpm(struct pmstate *pms, struct block *blk) +emitpm(PMState *pms, Block *blk) { int curi = blk->ins.n; for (int i = 0; i < pms->npmove; ++i) { @@ -324,10 +331,10 @@ emitpm(struct pmstate *pms, struct block *blk) /* remove phis by inserting parallel moves */ static void -lowerphis(struct rega *ra, struct block *blk, struct block *suc) +lowerphis(RegAlloc *ra, Block *blk, Block *suc) { int predno; - struct block *n = NULL; + Block *n = NULL; if (!blk->npred && blk != ra->fn->entry) { assert(!blk->phi.n); @@ -341,14 +348,14 @@ lowerphis(struct rega *ra, struct block *blk, struct block *suc) break; assert(predno < suc->npred); - struct pmstate pms; + PMState pms; pms.fn = ra->fn; pms.npmove = 0; /* ensure phi args go to the same slot as phi with parallel copies */ for (int i = 0; i < suc->phi.n; ++i) { - struct instr *phi = &instrtab[suc->phi.p[i]]; - union ref *arg = &phitab.p[phi->l.i][predno]; - union alloc from, to; + Instr *phi = &instrtab[suc->phi.p[i]]; + Ref *arg = &phitab.p[phi->l.i][predno]; + Alloc from, to; if (arg->t == RREG) continue; assert(arg->t == RTMP); @@ -385,13 +392,13 @@ lowerphis(struct rega *ra, struct block *blk, struct block *suc) /* generate copies for phi operands to transform into conventional-SSA */ static void -fixcssa(struct function *fn) +fixcssa(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; do { if (!blk->phi.n) continue; for (int p = 0; p < blk->npred; ++p) { - struct block *n, *pred = blkpred(blk, p); + Block *n, *pred = blkpred(blk, p); if (!pred->s2) { /* pred only has 1 successor (blk), so insert move directly in it */ n = pred; @@ -401,7 +408,7 @@ fixcssa(struct function *fn) } for (int i = 0; i < blk->phi.n; ++i) { int phi = blk->phi.p[i]; - union ref *args = phitab.p[instrtab[phi].l.i]; + Ref *args = phitab.p[instrtab[phi].l.i]; args[p] = insertinstr(n, n->ins.n, mkinstr(Ocopy, instrtab[phi].cls, args[p])); } } @@ -411,18 +418,18 @@ fixcssa(struct function *fn) } static inline bool -rangeoverlap(struct range a, struct range b) +rangeoverlap(Range a, Range b) { return a.from < b.to && b.from < a.to; } static void -pushrange(struct interval *it, struct range r) +pushrange(Interval *it, Range r) { if (it->nrange < 2) it->_rinl[it->nrange++] = r; else if (it->nrange > 2) xbpush(&it->_rdyn, &it->nrange, r); else { - struct range *d = NULL; + Range *d = NULL; xbgrow(&d, 4); memcpy(d, it->_rinl, 2*sizeof *d); d[it->nrange++] = r; @@ -432,24 +439,24 @@ pushrange(struct interval *it, struct range r) #define itrange(it, i) ((it)->nrange <= 2 ? (it)->_rinl : (it)->_rdyn)[i] static inline int -intervalbeg(struct interval *it) +intervalbeg(Interval *it) { assert(it->nrange); return itrange(it, 0).from; } static inline int -intervalend(struct interval *it) +intervalend(Interval *it) { assert(it->nrange); return itrange(it, it->nrange-1).to; } static bool -intersoverlap(struct interval *a, struct interval *b) +intersoverlap(Interval *a, Interval *b) { for (int i = 0, j = 0; i < a->nrange && j < b->nrange; ) { - struct range r1 = itrange(a, i), r2 = itrange(b, j); + Range r1 = itrange(a, i), r2 = itrange(b, j); if (rangeoverlap(r1, r2)) return 1; if (r1.to <= r2.from) ++i; else ++j; @@ -458,16 +465,16 @@ intersoverlap(struct interval *a, struct interval *b) } static inline void -incrcost(struct interval *it, struct block *blk) +incrcost(Interval *it, Block *blk) { /* treat each loop as executing instr 8 times */ it->cost += 1 << (blk->loop * 3); } static bool -intervaldef(struct rega *ra, int t, struct block *blk, int pos, int reghint) +intervaldef(RegAlloc *ra, int t, Block *blk, int pos, int reghint) { - struct interval *it = &ra->inter[t]; + Interval *it = &ra->inter[t]; if (it->nrange) { ushort *beg = &itrange(it, 0).from; assert(*beg <= pos); @@ -479,10 +486,10 @@ intervaldef(struct rega *ra, int t, struct block *blk, int pos, int reghint) } static void -addrange(struct rega *ra, int t, struct range new, int reghint) +addrange(RegAlloc *ra, int t, Range new, int reghint) { - struct interval *it = &ra->inter[t]; - struct range *fst; + Interval *it = &ra->inter[t]; + Range *fst; int n; if (!it->nrange) { @@ -502,7 +509,7 @@ addrange(struct rega *ra, int t, struct range new, int reghint) } else { /* put new range at the start */ pushrange(it, new); - memmove(&itrange(it, 1), &itrange(it, 0), sizeof(struct range) * (it->nrange - 1)); + memmove(&itrange(it, 1), &itrange(it, 0), sizeof(Range) * (it->nrange - 1)); itrange(it, 0) = new; } @@ -511,7 +518,7 @@ addrange(struct rega *ra, int t, struct range new, int reghint) fst = &itrange(it, 0); n = 0; for (int i = 1; i < it->nrange; ++i) { - struct range other = itrange(it, i); + Range other = itrange(it, i); if (fst->to >= other.from) { fst->to = fst->to > other.to ? fst->to : other.to; ++n; @@ -522,7 +529,7 @@ addrange(struct rega *ra, int t, struct range new, int reghint) for (int i = 1; i + n < it->nrange; ++i) itrange(it, i) = itrange(it, i+n); if (it->nrange > 2 && it->nrange - n <= 2) { - struct range *dyn = it->_rdyn; + Range *dyn = it->_rdyn; memcpy(it->_rinl, dyn, (it->nrange - n) * sizeof *dyn); xbfree(dyn); } @@ -531,11 +538,11 @@ addrange(struct rega *ra, int t, struct range new, int reghint) } static void -usereg(struct rega *ra, int reg, struct block *blk, int pos) +usereg(RegAlloc *ra, int reg, Block *blk, int pos) { - struct fixinterval *fxit; + FixInterval *fxit; if (rstest(mctarg->rglob, reg)) return; /* regalloc never allocates globally live regs, so don't need intervals for those */ - for (struct fixinterval *prev = NULL, *fxit = ra->fixed; fxit; prev = fxit, fxit = fxit->next) { + for (FixInterval *prev = NULL, *fxit = ra->fixed; fxit; prev = fxit, fxit = fxit->next) { if (fxit->range.from > pos) break; if (fxit->rs == BIT(reg) && fxit->range.from <= pos && pos < fxit->range.to) { /* contained by existing interval */ @@ -552,19 +559,19 @@ usereg(struct rega *ra, int reg, struct block *blk, int pos) } fxit = alloc(ra->arena, sizeof *fxit, 0); fxit->next = ra->fixed; - fxit->range = (struct range) {blk->inumstart, pos}; + fxit->range = (Range) {blk->inumstart, pos}; fxit->rs = BIT(reg); ra->fixed = fxit; } static bool -defreg(struct rega *ra, int reg, int pos) { +defreg(RegAlloc *ra, int reg, int pos) { if (rstest(mctarg->rglob, reg)) return 1; - for (struct fixinterval *prev = NULL, *fxit = ra->fixed; fxit; prev = fxit, fxit = fxit->next) { + for (FixInterval *prev = NULL, *fxit = ra->fixed; fxit; prev = fxit, fxit = fxit->next) { if (fxit->rs == BIT(reg)) { if (fxit->range.from <= pos) { fxit->range.from = pos; - struct fixinterval **at = &ra->fixed; + FixInterval **at = &ra->fixed; if ((*at)->range.from > pos) { /* keep sorted */ //DBG("moved %s\n", mctarg->rnames[reg]); @@ -584,15 +591,16 @@ defreg(struct rega *ra, int reg, int pos) { /* lifetime interval construction */ static void -buildintervals(struct rega *ra) +buildintervals(RegAlloc *ra) { extern int ninstr; - struct block *blk, *last; - struct bitset **livein = alloc(ra->arena, ra->fn->nblk * sizeof *livein, 0); + Block *blk, *last; + BitSet **livein = alloc(ra->arena, ra->fn->nblk * sizeof *livein, 0); size_t bssize = BSSIZE(ninstr); - struct loops { /* list of loops */ - struct loops *next; - struct block *hdr, *end; + struct Loop { + struct Loop *next; + Block *hdr, *end; + /* list of loops */ } *loops = NULL; for (int i = 0; i < ra->fn->nblk; ++i) livein[i] = allocz(ra->arena, bssize * sizeof *livein[i], 0); @@ -600,11 +608,11 @@ buildintervals(struct rega *ra) ra->ninter = ninstr; uint n = numberinstrs(ra->fn); - assert((ushort)n == n && "too many instrs for struct range"); + assert((ushort)n == n && "too many instrs for Range"); /* visit blocks in reverse, to build lifetime intervals */ blk = last = ra->fn->entry->lprev; do { - struct bitset *live = livein[blk->id]; + BitSet *live = livein[blk->id]; /* live = union of successor.liveIn for each successor of b */ if (blk->s1) bsunion(live, livein[blk->s1->id], bssize); if (blk->s2) bsunion(live, livein[blk->s2->id], bssize); @@ -612,12 +620,12 @@ buildintervals(struct rega *ra) /* for each phi function phi of successors of b do * live.add(phi.inputOf(b)) */ - for (struct block *suc = blk->s1; suc; suc = blk->s2) { + for (Block *suc = blk->s1; suc; suc = blk->s2) { int predno; for (predno = 0; blkpred(suc, predno) != blk; ++predno) ; for (int i = 0; i < suc->phi.n; ++i) { - struct instr *phi = &instrtab[suc->phi.p[i]]; - union ref *arg = &phitab.p[phi->l.i][predno]; + Instr *phi = &instrtab[suc->phi.p[i]]; + Ref *arg = &phitab.p[phi->l.i][predno]; assert(arg->t == RTMP); bsset(live, arg->i); incrcost(&ra->inter[arg->i], blk); @@ -629,12 +637,12 @@ buildintervals(struct rega *ra) * intervals[opd].addRange(b.from, b.to) */ for (uint i = 0; bsiter(&i, live, bssize); ++i) { - addrange(ra, i, mkrange(blk->inumstart, blk->inumstart + blk->ins.n + 2), -1); + addrange(ra, i, (Range){blk->inumstart, blk->inumstart + blk->ins.n + 2}, -1); } /* for each operation op of b in reverse order do */ - struct instr *ins = NULL; - union ref queue[8] = { blk->jmp.arg[0], blk->jmp.arg[1] }; + Instr *ins = NULL; + Ref queue[8] = { blk->jmp.arg[0], blk->jmp.arg[1] }; goto Branchopd; for (int curi, pos ; curi >= 0; --curi) { int out = blk->ins.p[curi], reghint; @@ -676,7 +684,7 @@ buildintervals(struct rega *ra) if (ins->l.bits == ins->r.bits) continue; } else if (ins->op == Ocall) { - struct call *call = &calltab.p[ins->r.i]; + IRCall *call = &calltab.p[ins->r.i]; regset rclob = (gpregset | fpregset) &~ (mctarg->rglob | mctarg->rcallee); ra->fn->isleaf = 0; @@ -688,14 +696,14 @@ buildintervals(struct rega *ra) } } if (rclob) { - struct fixinterval *fxit = alloc(ra->arena, sizeof *fxit, 0); + FixInterval *fxit = alloc(ra->arena, sizeof *fxit, 0); fxit->next = ra->fixed; - fxit->range = mkrange(pos, pos); + fxit->range = (Range){pos, pos}; fxit->rs = rclob; ra->fixed = fxit; } for (int j = call->narg - 1; j >= 0; --j) { - struct abiarg abi = call->abiarg[j]; + ABIArg abi = call->abiarg[j]; if (!abi.isstk) { usereg(ra, abi.reg, blk, pos); } @@ -715,7 +723,7 @@ buildintervals(struct rega *ra) pos = blk->inumstart + blk->ins.n + 1; } for (int nqueue = ins && ins->op == Omove ? 1 : 2; nqueue > 0;) { - union ref r = queue[--nqueue]; + Ref r = queue[--nqueue]; /* do not allocate a reg for a cmp op used as branch argument, since it's a pseudo op */ if (curi == blk->ins.n && blk->jmp.t == Jb && r.t == RTMP && instrtab[r.i].keep) @@ -724,7 +732,7 @@ buildintervals(struct rega *ra) if (r.t == RTMP) { assert(instrtab[r.i].op != Onop); incrcost(&ra->inter[r.i], blk); - addrange(ra, r.i, mkrange(blk->inumstart, pos), reghint); + addrange(ra, r.i, (Range){blk->inumstart, pos}, reghint); bsset(live, r.i); } else if (r.t == RREG) { usereg(ra, r.i, blk, pos); @@ -751,15 +759,15 @@ buildintervals(struct rega *ra) * for each opd in live do * intervals[opd].addRange(b.from, loopEnd.to) */ - struct block *loopend = NULL; + Block *loopend = NULL; for (int i = 0; i < blk->npred; ++i) { - struct block *pred = blkpred(blk, i); + Block *pred = blkpred(blk, i); if (pred->id > blk->id) loopend = loopend && loopend->id > pred->id ? loopend : pred; } if (loopend) { if (loops) DBG("@lp @%d\n", blk->id); - for (struct loops *l = loops; l; l = l->next) { + for (struct Loop *l = loops; l; l = l->next) { /* a nested loop might end later than loopend, which lengthens this outer loop. */ /* XXX is this correct? more loop analysis might be required? */ if (l->hdr->id > loopend->id) break; @@ -769,26 +777,26 @@ buildintervals(struct rega *ra) } DBG("loop header @%d (to @%d)\n", blk->id, loopend->id); /* append to loop list */ - loops = alloccopy(ra->arena, &(struct loops){loops, blk, loopend}, sizeof *loops, 0); + loops = alloccopy(ra->arena, &(struct Loop){loops, blk, loopend}, sizeof *loops, 0); for (uint opd = 0; bsiter(&opd, live, bssize); ++opd) { // DBG(" i have live %%%d\n", opd); - addrange(ra, opd, mkrange(blk->inumstart, loopend->inumstart + loopend->ins.n+1), -1); + addrange(ra, opd, (Range){blk->inumstart, loopend->inumstart + loopend->ins.n+1}, -1); } } } while ((blk = blk->lprev) != last); if (ccopt.dbg.r) { for (int var = 0; var < ninstr; ++var) { - struct interval *it = &ra->inter[var]; + Interval *it = &ra->inter[var]; if (!it->nrange) continue; DBG("lifetime of %%%d: ", var); for (int i = 0; i < it->nrange; ++i) { - struct range r = itrange(it, i); + Range r = itrange(it, i); DBG("[%d,%d)%s", r.from, r.to, i < it->nrange-1 ? ", " : ""); } DBG(" spill cost: %d\n", it->cost); } - for (struct fixinterval *fx = ra->fixed; fx; fx = fx->next) { + for (FixInterval *fx = ra->fixed; fx; fx = fx->next) { DBG("fixed {"); for (int r = 0, f=1; rsiter(&r, fx->rs); ++r, f=0) DBG(&" %s"[f], mctarg->rnames[r]); @@ -798,10 +806,10 @@ buildintervals(struct rega *ra) } static bool -itcontainspos(struct interval *it, int pos) +itcontainspos(Interval *it, int pos) { for (int i = 0; i < it->nrange; ++i) { - struct range r = itrange(it, i); + Range r = itrange(it, i); if (r.from > pos) return 0; if (pos < r.to) return 1; } @@ -810,7 +818,7 @@ itcontainspos(struct interval *it, int pos) /* quicksort */ static void -sortintervals(struct interval **xs, int lo, int hi) +sortintervals(Interval **xs, int lo, int hi) { assert(lo >= 0 && hi >= 0); while (lo < hi) { @@ -818,7 +826,7 @@ sortintervals(struct interval **xs, int lo, int hi) int i = lo - 1, p = hi + 1, pivot = intervalbeg(xs[lo]); for (;;) { - struct interval *tmp; + Interval *tmp; do ++i; while (intervalbeg(xs[i]) < pivot); do --p; while (intervalbeg(xs[p]) > pivot); if (i >= p) break; @@ -838,8 +846,8 @@ sortintervals(struct interval **xs, int lo, int hi) } } -static union alloc -allocstk(struct rega *ra) +static Alloc +allocstk(RegAlloc *ra) { uint s = 0; if (bsiter(&s, ra->freestk, BSSIZE(MAXSPILL))) { @@ -853,7 +861,7 @@ allocstk(struct rega *ra) } static void -freestk(struct rega *ra, int slot) +freestk(RegAlloc *ra, int slot) { DBG("FREE stk %d\n",slot); if (slot < MAXSPILL) @@ -865,12 +873,12 @@ freestk(struct rega *ra, int slot) #define interval2temp(it) (int)(it - ra->inter) static void -linearscan(struct rega *ra) +linearscan(RegAlloc *ra) { if (!ra->intercount) return; /* sort intervals */ - struct interval **unhandled = alloc(ra->arena, sizeof *unhandled * ra->intercount, 0); + Interval **unhandled = alloc(ra->arena, sizeof *unhandled * ra->intercount, 0); int nunhandled = 0; for (int i = 0; i < ra->ninter; ++i) { if (!ra->inter[i].nrange) continue; @@ -883,14 +891,14 @@ linearscan(struct rega *ra) memset(ra->freestk, 0xFF, sizeof ra->freestk); /* LINEAR SCAN */ - struct interval *actives[2] = {0}, /* gpr set and fpr set */ + Interval *actives[2] = {0}, /* gpr set and fpr set */ *inactives[2] = {0}, *spilled = NULL, **spilled_tail = &spilled; - for (struct interval **pcurrent = unhandled; nunhandled > 0; ++pcurrent, --nunhandled) { - struct interval *current = *pcurrent; + for (Interval **pcurrent = unhandled; nunhandled > 0; ++pcurrent, --nunhandled) { + Interval *current = *pcurrent; int pos = intervalbeg(current); - struct interval **active = &actives[current->fpr], **inactive = &inactives[current->fpr], + Interval **active = &actives[current->fpr], **inactive = &inactives[current->fpr], **lnk, *it, *next; /* Expire old intervals */ /* check for intervals in active that are handled or inactive */ @@ -935,12 +943,12 @@ linearscan(struct rega *ra) int this = interval2temp(current); regset avail = freeregs & (current->fpr ? fpregset : gpregset), fixexcl = 0, excl = 0; - struct instr *ins = &instrtab[this]; + Instr *ins = &instrtab[this]; int reg = 0; /* exclude regs from overlapping fixed intervals */ int end = intervalend(current); - for (struct fixinterval *last = NULL, *fxit = ra->fixed; fxit; + for (FixInterval *last = NULL, *fxit = ra->fixed; fxit; last = fxit, fxit = fxit->next) { if (last) assert(last->range.from <= fxit->range.from && "unsorted fixintervals"); if (fxit->range.to <= pos) { @@ -957,7 +965,7 @@ linearscan(struct rega *ra) } } /* exclude regs from overlapping inactive intervals */ - for (struct interval *it = *inactive; it; it = it->next) { + for (Interval *it = *inactive; it; it = it->next) { if (it->alloc.t == AREG && intersoverlap(it, current)) { rsset(&excl, it->alloc.a); } @@ -975,7 +983,7 @@ linearscan(struct rega *ra) avail &= ~excl; if (!avail) { /* no regs left, must spill */ - struct interval **ptospill = NULL, *tospill = current; + Interval **ptospill = NULL, *tospill = current; /* heuristic: look for longest-lived active interval with lower spill cost */ int curend = intervalend(current); for (lnk = active, it = *lnk; (next = it ? it->next : 0), it; it = next) { @@ -1037,7 +1045,7 @@ linearscan(struct rega *ra) goto GotReg; /* for phi, try to use reg of any arg */ } else if (ins->op == Ophi) { - union ref *arg = phitab.p[ins->l.i]; + Ref *arg = phitab.p[ins->l.i]; for (int i = 0; i < xbcap(arg); ++i) { if (arg->t == RREG && rstest(avail, reg = arg->i)) goto GotReg; if (arg->t == RTMP) @@ -1074,16 +1082,16 @@ linearscan(struct rega *ra) } /* allocate stack slots for spilled intervals * this is like another (simplified) linear scan pass */ - struct interval *active = NULL; + Interval *active = NULL; int prevpos = -1; if (spilled) DBG("spilled:\n"); - for (struct interval *current = spilled, *next; current; current = next) { + for (Interval *current = spilled, *next; current; current = next) { int pos = intervalbeg(current); DBG(" %%%zd: [%d,%d)\n", interval2temp(current), pos, intervalend(current)); assert(pos >= prevpos && "unsorted spilled?"); prevpos = pos; /* Expire old intervals */ - struct interval **lnk, *it, *lnext; + Interval **lnk, *it, *lnext; for (lnk = &active, it = *lnk; (lnext = it ? it->next : 0), it; it = lnext) { /* ends before position? */ if (intervalend(it) <= pos) { @@ -1102,7 +1110,7 @@ linearscan(struct rega *ra) } static bool -isstoreimm(union ref r) +isstoreimm(Ref r) { if (r.t == RTMP) return 1; /* register OK */ if (isintcon(r)) switch (target.arch) { @@ -1116,20 +1124,20 @@ isstoreimm(union ref r) /* replace temps with physical regs, add loads & stores for spilled temps */ static bool -devirt(struct rega *ra, struct block *blk) +devirt(RegAlloc *ra, Block *blk) { bool allnops = 1; - struct function *fn = ra->fn; - union alloc spillsave[4] = {0}; + Function *fn = ra->fn; + Alloc spillsave[4] = {0}; memset(ra->freestk, 0, BSSIZE(MAXSPILL) * sizeof *ra->freestk); for (int curi = 0; curi < blk->ins.n; ++curi) { int temp = blk->ins.p[curi]; - struct instr *ins = &instrtab[temp]; - struct interval *it; - union alloc *alloc; - struct addr newaddr; - union ref *argref[4]; + Instr *ins = &instrtab[temp]; + Interval *it; + Alloc *alloc; + IRAddr newaddr; + Ref *argref[4]; int curi0; int naddr = 0; int nargref = 0; @@ -1137,9 +1145,9 @@ devirt(struct rega *ra, struct block *blk) /** devirtualize operands **/ for (int i = 0; i < 2; ++i) { - union ref *r = &i[&ins->l]; + Ref *r = &i[&ins->l]; if (r->t == RADDR) { - struct addr *a = &addrtab.p[r->i]; + IRAddr *a = &addrtab.p[r->i]; ++naddr; newaddr = *a; argref[nargref++] = &newaddr.base; @@ -1149,7 +1157,7 @@ devirt(struct rega *ra, struct block *blk) } } for (int i = 0; i < nargref; ++i) { - union ref *r = argref[i]; + Ref *r = argref[i]; int tr; if (r->t == RTMP && (it = &ra->inter[r->i])->nrange > 0) { alloc = &it->alloc; @@ -1166,7 +1174,7 @@ devirt(struct rega *ra, struct block *blk) ins->l = stkslotref(fn, alloc->a*8); } else if (alloc->t == ASTACK) { /* ref was spilled, gen load to scratch register and use it */ - struct instr ld = {.cls = insrescls(instrtab[r->i])}; + Instr ld = {.cls = insrescls(instrtab[r->i])}; int reg = kisint(ld.cls) ? mctarg->gprscratch : mctarg->fprscratch; bool dosave = 0; /* pick scratch register, or any register that doesn't conflict with this instr's srcs/dst */ @@ -1174,7 +1182,7 @@ devirt(struct rega *ra, struct block *blk) regset avail = (kisflt(ld.cls) ? fpregset : gpregset) &~ mctarg->rglob; if (ins->reg) rsclr(&avail, ins->reg-1); for (int j = 0; j < nargref; ++j) { - struct interval *it; + Interval *it; if (argref[j]->t == RREG) rsclr(&avail, argref[j]->i); else if (argref[j]->t == RTMP) { it = &ra->inter[argref[j]->i]; @@ -1208,7 +1216,7 @@ devirt(struct rega *ra, struct block *blk) } if (nspill > 1) assert(ins->op != Ocall); if (naddr) { - union ref *r = ins->l.t == RADDR ? &ins->l : &ins->r; + Ref *r = ins->l.t == RADDR ? &ins->l : &ins->r; *r = mkaddr(newaddr); } @@ -1282,11 +1290,11 @@ devirt(struct rega *ra, struct block *blk) } static void -fini(struct rega *ra) +fini(RegAlloc *ra) { int id = 0; - struct function *fn = ra->fn; - struct block *blk = fn->entry; + Function *fn = ra->fn; + Block *blk = fn->entry; do { blk->id = id++; @@ -1294,12 +1302,12 @@ fini(struct rega *ra) if (allnops && !blk->s2 && blk->npred > 0) { /* remove no-op blocks */ bool delet = 1; for (int i = 0; i < blk->npred; ++i) { - struct block *p = blkpred(blk, i); + Block *p = blkpred(blk, i); if (p == blk || (p->s2 && !blk->s1)) delet = 0; } for (int i = 0; i < blk->npred; ++i) { - struct block *p = blkpred(blk, i); + Block *p = blkpred(blk, i); if (!p->s2 && !blk->s1) { /* simplify: * @@ -1323,7 +1331,7 @@ fini(struct rega *ra) * NOP * b @next */ - struct block *next = blk->s1; + Block *next = blk->s1; if (p->s1 == blk) p->s1 = next; else if (p->s2 == blk) p->s2 = next; else continue; @@ -1348,10 +1356,10 @@ fini(struct rega *ra) } void -regalloc(struct function *fn) +regalloc(Function *fn) { - struct rega ra = {fn, .arena = fn->passarena}; - struct block *blk, *last; + RegAlloc ra = {fn, .arena = fn->passarena}; + Block *blk, *last; /* setup */ if (!fpregset || !gpregset) { @@ -1404,7 +1412,7 @@ regalloc(struct function *fn) fn->stksiz += ra.maxstk*8; if (fn->stksiz > 1<<24) error(NULL, "'%s' stack frame too big", fn->name); - for (struct interval *it = ra.inter; ra.intercount > 0; ++it) { + for (Interval *it = ra.inter; ra.intercount > 0; ++it) { if (it->nrange > 2) xbfree(it->_rdyn); if (it->nrange > 0) --ra.intercount; } diff --git a/src/ir_simpl.c b/src/ir_simpl.c index a01879a..8bf8c98 100644 --- a/src/ir_simpl.c +++ b/src/ir_simpl.c @@ -1,7 +1,7 @@ #include "ir.h" static int -mulk(struct instr *ins, struct block *blk, int *curi) +mulk(Instr *ins, Block *blk, int *curi) { vlong iv = intconval(ins->r); enum irclass cls = ins->cls; @@ -37,7 +37,7 @@ mulk(struct instr *ins, struct block *blk, int *curi) } static int -divmodk(struct instr *ins, struct block *blk, int *curi) +divmodk(Instr *ins, Block *blk, int *curi) { enum op op = ins->op; enum irclass cls = ins->cls; @@ -45,7 +45,7 @@ divmodk(struct instr *ins, struct block *blk, int *curi) uint nbit = 8 * cls2siz[cls]; bool neg = (op == Odiv || op == Orem) && iv < 0; if (ispo2(iv) || (neg && ispo2(-iv))) { /* simple po2 cases */ - union ref temp; + Ref temp; uint s = ilog2(neg ? -iv : iv); switch (op) { default: assert(0); @@ -63,7 +63,7 @@ divmodk(struct instr *ins, struct block *blk, int *curi) temp = insertinstr(blk, (*curi)++, mkinstr(Oadd, cls, ins->l, temp)); if (op == Odiv) { /* (-) (x' >> s) */ - struct instr sar = mkinstr(Osar, cls, temp, mkref(RICON, s)); + Instr sar = mkinstr(Osar, cls, temp, mkref(RICON, s)); if (!neg) *ins = sar; else { temp = insertinstr(blk, (*curi)++, sar); @@ -82,11 +82,11 @@ divmodk(struct instr *ins, struct block *blk, int *curi) } static int -doins(struct instr *ins, struct block *blk, int *curi) +doins(Instr *ins, Block *blk, int *curi) { int narg = opnarg[ins->op]; if (oisarith(ins->op)) { - union ref r = narg == 1 ? irunop(NULL, ins->op, ins->cls, ins->l) + Ref r = narg == 1 ? irunop(NULL, ins->op, ins->cls, ins->l) : irbinop(NULL, ins->op, ins->cls, ins->l, ins->r); if (r.bits) { *ins = mkinstr(Onop,0,); @@ -100,7 +100,7 @@ doins(struct instr *ins, struct block *blk, int *curi) if ((ins->l.t == RTMP && k == instrtab[ins->l.i].cls) || (isnumcon(ins->l) && k == concls(ins->l)) || (kisint(k) && ins->l.t == RICON)) { - union ref it = ins->l; + Ref it = ins->l; *ins = mkinstr(Onop,0,); replcuses(mkref(RTMP, ins - instrtab), it); return 1; @@ -119,7 +119,7 @@ doins(struct instr *ins, struct block *blk, int *curi) if (ins->l.t == RTMP && isintcon(ins->r)) { /* optimize `x <op> C <cmp> Q` */ /* could apply with add/sub to lth/lte/gth/gte iff no signed wraparound, which isn't assumed */ - struct instr *lhs = &instrtab[ins->l.i]; + Instr *lhs = &instrtab[ins->l.i]; enum op o = lhs->op; if (ins->cls == lhs->cls && (o == Oadd || o == Osub || o == Oxor) && isintcon(lhs->r)) { uvlong c = intconval(ins->r), q = intconval(lhs->r); @@ -139,9 +139,9 @@ doins(struct instr *ins, struct block *blk, int *curi) } static void -jmpfind(struct block **final, struct block **pblk) +jmpfind(Block **final, Block **pblk) { - struct block **p2 = &final[(*pblk)->id]; + Block **p2 = &final[(*pblk)->id]; if (*p2 && !(*p2)->phi.n) { jmpfind(final, p2); *pblk = *p2; @@ -149,7 +149,7 @@ jmpfind(struct block **final, struct block **pblk) } static void -fillpredsrec(struct block *blk) +fillpredsrec(Block *blk) { while (blk && !wasvisited(blk)) { markvisited(blk); @@ -166,9 +166,9 @@ fillpredsrec(struct block *blk) } static void -fillpreds(struct function *fn) +fillpreds(Function *fn) { - struct block *blk = fn->entry, *next; + Block *blk = fn->entry, *next; do { if (blk->phi.n) continue; if (blk->npred > 1) @@ -186,13 +186,13 @@ fillpreds(struct function *fn) } static void -mergeblks(struct function *fn, struct block *p, struct block *s) +mergeblks(Function *fn, Block *p, Block *s) { assert(s->npred == 1 && !s->phi.n); vpushn(&p->ins, s->ins.p, s->ins.n); p->jmp = p->s1->jmp; for (int i = 0; i < 2; ++i) { - struct block *ss = (&s->s1)[i]; + Block *ss = (&s->s1)[i]; if (ss) for (int j = 0; j < ss->npred; ++j) { if (blkpred(ss, j) == s) { blkpred(ss, j) = p; @@ -209,13 +209,13 @@ mergeblks(struct function *fn, struct block *p, struct block *s) } void -simpl(struct function *fn) +simpl(Function *fn) { FREQUIRE(FNUSE); int inschange = 0, blkchange = 0; - struct block **jmpfinal = allocz(fn->passarena, fn->nblk * sizeof *jmpfinal, 0); - struct block *blk = fn->entry; + Block **jmpfinal = allocz(fn->passarena, fn->nblk * sizeof *jmpfinal, 0); + Block *blk = fn->entry; do { /* merge blocks: @@ -242,7 +242,7 @@ simpl(struct function *fn) for (int i = 0; i < blk->phi.n; ++i) { int phi = blk->phi.p[i]; /* delete trivial phis */ - union ref *args = phitab.p[instrtab[phi].l.i], + Ref *args = phitab.p[instrtab[phi].l.i], same = *args; if (same.t == RTMP && instrtab[same.i].op == Ophi) goto Next; if (blk->npred > 1) for (int j = 1; j < blk->npred; ++j) { @@ -257,7 +257,7 @@ simpl(struct function *fn) int curi = 0; DoIns: for (; curi < blk->ins.n; ++curi) { - struct instr *ins = &instrtab[blk->ins.p[curi]]; + Instr *ins = &instrtab[blk->ins.p[curi]]; if (ins->op != Onop) { if (!(fn->prop & FNUSE)) filluses(fn); inschange += doins(ins, blk, &curi); @@ -266,7 +266,7 @@ simpl(struct function *fn) if (blk->s2 && isintcon(blk->jmp.arg[0])) { /* simplify known conditional branch */ - struct block *s = intconval(blk->jmp.arg[0]) ? blk->s1 : blk->s2; + Block *s = intconval(blk->jmp.arg[0]) ? blk->s1 : blk->s2; delpred(s == blk->s1 ? blk->s2 : blk->s1, blk); blk->s1 = s, blk->s2 = NULL; blk->jmp.arg[0] = NOREF; diff --git a/src/ir_ssa.c b/src/ir_ssa.c index 6598fba..f3fcb5d 100644 --- a/src/ir_ssa.c +++ b/src/ir_ssa.c @@ -1,16 +1,16 @@ #include "ir.h" void -copyopt(struct function *fn) +copyopt(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; FREQUIRE(FNUSE); do { for (int i = 0; i < blk->phi.n; ++i) { /* simplify same-arg phi */ int phi = blk->phi.p[i]; - union ref *arg = phitab.p[instrtab[phi].l.i]; + Ref *arg = phitab.p[instrtab[phi].l.i]; for (int j = 1; j < blk->npred; ++j) { if (arg[j].bits != arg->bits) goto Next; } @@ -22,12 +22,12 @@ copyopt(struct function *fn) Next:; } for (int i = 0; i < blk->ins.n; ++i) { - union ref var = mkref(RTMP, blk->ins.p[i]); - struct instr *ins = &instrtab[var.i]; + Ref var = mkref(RTMP, blk->ins.p[i]); + Instr *ins = &instrtab[var.i]; enum irclass k; if (ins->op == Ocopy) { - union ref arg = ins->l; + Ref arg = ins->l; if (arg.t == RTMP) k = insrescls(instrtab[arg.i]); else if (arg.t == RICON) k = cls2siz[ins->cls] == 4 ? KI32 : KI64; else if (arg.t == RXCON) k = isnumcon(arg) ? concls(arg) : KPTR; diff --git a/src/ir_stack.c b/src/ir_stack.c index 40a7b1d..d15a764 100644 --- a/src/ir_stack.c +++ b/src/ir_stack.c @@ -1,16 +1,16 @@ #include "ir.h" void -lowerstack(struct function *fn) +lowerstack(Function *fn) { fn->stksiz = 0; FREQUIRE(FNUSE); - struct block *blk = fn->entry; + Block *blk = fn->entry; do { for (int i = 0; i < blk->ins.n; ++i) { int t = blk->ins.p[i]; - struct instr *ins = &instrtab[t]; + Instr *ins = &instrtab[t]; if (oisalloca(ins->op)) { uint alignlog2 = ins->op - Oalloca1; assert(ins->l.i > 0); diff --git a/src/o_elf.c b/src/o_elf.c index f652657..336aa81 100644 --- a/src/o_elf.c +++ b/src/o_elf.c @@ -7,22 +7,22 @@ static union { ELF_HDRIDENT; - struct elf32hdr h32; - struct elf64hdr h64; + Elf32Hdr h32; + Elf64Hdr h64; } hdr; static vec_of(uchar) strs; -struct sym { +typedef struct { uint name; uchar bind : 4, type : 4; ushort shndx; uvlong value, size; -}; -static vec_of(struct sym) symtab; +} Sym; +static vec_of(Sym) symtab; static pmap_of(ushort) symht; static uint ntextrel, nrodatarel, ndatarel; -struct reloc { +typedef struct { uchar section; ushort kind; uint off; @@ -31,8 +31,8 @@ struct reloc { uint symidx; internstr symname; }; -}; -static vec_of(struct reloc) relocs; +} Reloc; +static vec_of(Reloc) relocs; #define O objout @@ -53,15 +53,15 @@ elfinit(void) } hdr.h32.version = ELFVERSION; if (targ_64bit) { - hdr.h64.ehsize = sizeof(struct elf64hdr); - hdr.h64.shentsize = sizeof(struct elf64shdr); + hdr.h64.ehsize = sizeof(Elf64Hdr); + hdr.h64.shentsize = sizeof(Elf64Shdr); } else { - hdr.h32.ehsize = sizeof(struct elf32hdr); - hdr.h32.shentsize = sizeof(struct elf32shdr); + hdr.h32.ehsize = sizeof(Elf32Hdr); + hdr.h32.shentsize = sizeof(Elf32Shdr); } vinit(&strs, NULL, 4<<10); - vpush(&symtab, ((struct sym){0})); - vpush(&symtab, ((struct sym){ .type = STT_FILE, .shndx = SHN_ABS})); + vpush(&symtab, ((Sym){0})); + vpush(&symtab, ((Sym){ .type = STT_FILE, .shndx = SHN_ABS})); } uint @@ -79,7 +79,7 @@ str2idx(const char *s) return i; } -static struct sym * +static Sym * findsym(internstr s) { ushort *idx = pmap_get(&symht, s); @@ -106,7 +106,7 @@ static const char sect2ndx[] = { enum section elfhassym(internstr nam, uint *value) { - struct sym *sym = findsym(nam); + Sym *sym = findsym(nam); if (sym) { if (value) *value = sym->value; return shndx2sect[sym->shndx]; @@ -117,7 +117,7 @@ elfhassym(internstr nam, uint *value) void elfaddsym(internstr nam, int info, enum section sect, uvlong value, uvlong size) { - struct sym *sym = findsym(nam), sym0; + Sym *sym = findsym(nam), sym0; if (!sym) { sym = &sym0; sym->name = str2idx(&nam->c); @@ -169,11 +169,11 @@ elfreloc(internstr sym, enum relockind kind, enum section section, uint off, vlo case Sdata: ++ndatarel; break; } assert(kind < NRELOCKIND); - vpush(&relocs, ((struct reloc) { section, relktab[target.arch][kind], off, addend, .symname = sym })); + vpush(&relocs, ((Reloc) { section, relktab[target.arch][kind], off, addend, .symname = sym })); } static void -elf64puthdr(struct wbuf *out, struct elf64hdr *hdr) +elf64puthdr(WriteBuf *out, Elf64Hdr *hdr) { if (!hostntarg_sameendian()) { hdr->type = bswap16(hdr->type); @@ -194,7 +194,7 @@ elf64puthdr(struct wbuf *out, struct elf64hdr *hdr) } static void -elf32puthdr(struct wbuf *out, struct elf32hdr *hdr) +elf32puthdr(WriteBuf *out, Elf32Hdr *hdr) { if (!hostntarg_sameendian()) { hdr->type = bswap16(hdr->type); @@ -215,7 +215,7 @@ elf32puthdr(struct wbuf *out, struct elf32hdr *hdr) } static void -elf64putshdr(struct wbuf *out, struct elf64shdr *shdr) +elf64putshdr(WriteBuf *out, Elf64Shdr *shdr) { if (!hostntarg_sameendian()) { shdr->name = bswap32(shdr->name); @@ -233,7 +233,7 @@ elf64putshdr(struct wbuf *out, struct elf64shdr *shdr) } static void -elf32putshdr(struct wbuf *out, struct elf32shdr *shdr) +elf32putshdr(WriteBuf *out, Elf32Shdr *shdr) { if (!hostntarg_sameendian()) { shdr->name = bswap32(shdr->name); @@ -251,7 +251,7 @@ elf32putshdr(struct wbuf *out, struct elf32shdr *shdr) } static void -elf64putsym(struct wbuf *out, struct elf64sym *sym) +elf64putsym(WriteBuf *out, Elf64Sym *sym) { if (!hostntarg_sameendian()) { sym->name = bswap32(sym->name); @@ -263,7 +263,7 @@ elf64putsym(struct wbuf *out, struct elf64sym *sym) } static void -elf32putsym(struct wbuf *out, struct elf32sym *sym) +elf32putsym(WriteBuf *out, Elf32Sym *sym) { if (!hostntarg_sameendian()) { sym->name = bswap32(sym->name); @@ -275,21 +275,21 @@ elf32putsym(struct wbuf *out, struct elf32sym *sym) } static void -putsym(struct wbuf *out, const struct sym *sym) +putsym(WriteBuf *out, const Sym *sym) { if (targ_64bit) { - elf64putsym(out, &(struct elf64sym) { + elf64putsym(out, &(Elf64Sym) { sym->name, .info = ELF_S_INFO(sym->bind, sym->type), .shndx = sym->shndx, .value = sym->value, .size = sym->size }); } else { - elf32putsym(out, &(struct elf32sym) { + elf32putsym(out, &(Elf32Sym) { sym->name, .info = ELF_S_INFO(sym->bind, sym->type), .shndx = sym->shndx, .value = sym->value, .size = sym->size }); } } static void -elf64putrel(struct wbuf *out, struct elf64rel *rel) +elf64putrel(WriteBuf *out, Elf64Rel *rel) { if (!hostntarg_sameendian()) { rel->offset = bswap64(rel->offset); @@ -299,7 +299,7 @@ elf64putrel(struct wbuf *out, struct elf64rel *rel) } static void -elf32putrel(struct wbuf *out, struct elf32rel *rel) +elf32putrel(WriteBuf *out, Elf32Rel *rel) { if (!hostntarg_sameendian()) { rel->offset = bswap32(rel->offset); @@ -309,7 +309,7 @@ elf32putrel(struct wbuf *out, struct elf32rel *rel) } static void -elf64putrela(struct wbuf *out, struct elf64rela *rel) +elf64putrela(WriteBuf *out, Elf64Rela *rel) { if (!hostntarg_sameendian()) { rel->offset = bswap64(rel->offset); @@ -320,7 +320,7 @@ elf64putrela(struct wbuf *out, struct elf64rela *rel) } static void -elf32putrela(struct wbuf *out, struct elf32rela *rel) +elf32putrela(WriteBuf *out, Elf32Rela *rel) { if (!hostntarg_sameendian()) { rel->offset = bswap32(rel->offset); @@ -331,22 +331,22 @@ elf32putrela(struct wbuf *out, struct elf32rela *rel) } static void -putreloc(struct wbuf *out, const struct reloc *rel, bool userela) +putreloc(WriteBuf *out, const Reloc *rel, bool userela) { if (userela) { if (targ_64bit) { - elf64putrela(out, &(struct elf64rela) { + elf64putrela(out, &(Elf64Rela) { rel->off, ELF64_R_INFO(rel->symidx, rel->kind), rel->addend }); } else { - elf32putrela(out, &(struct elf32rela) { + elf32putrela(out, &(Elf32Rela) { rel->off, ELF32_R_INFO(rel->symidx, rel->kind), rel->addend }); } } else { if (targ_64bit) { - elf64putrel(out, &(struct elf64rel) { + elf64putrel(out, &(Elf64Rel) { rel->off, ELF64_R_INFO(rel->symidx, rel->kind) }); } else { - elf32putrel(out, &(struct elf32rel) { + elf32putrel(out, &(Elf32Rel) { rel->off, ELF32_R_INFO(rel->symidx, rel->kind) }); } } @@ -363,7 +363,7 @@ static int symcmp(const void *aa, const void *bb) { const ushort *a = aa, *b = bb; - const struct sym *l = &symtab.p[*a], *r = &symtab.p[*b]; + const Sym *l = &symtab.p[*a], *r = &symtab.p[*b]; int tmp; if ((tmp = l->bind - r->bind)) return tmp; /* locals prio */ if ((tmp = r->shndx - l->shndx)) return tmp; /* section prio (real sections > SHN_UND) */ @@ -371,7 +371,7 @@ symcmp(const void *aa, const void *bb) } static void -wordalign(struct wbuf *out, int align) +wordalign(WriteBuf *out, int align) { size_t off = out->len + lseek(out->fd, 0, SEEK_CUR); while (off++ & (align - 1)) ioputc(out, 0); @@ -380,7 +380,7 @@ wordalign(struct wbuf *out, int align) static const bool userelatab[] = { [ISx86_64] = 1, [ISaarch64] = 1 }; void -elffini(struct wbuf *out) +elffini(WriteBuf *out) { enum { shnam_text = 1, shnam_rodata = 7, shnam_data = 15, shnam_bss = 21, shnam_shstrtab = 26, @@ -409,14 +409,14 @@ elffini(struct wbuf *out) /* fixup relocs */ for (int i = 0; i < relocs.n; ++i) { - struct reloc *rel = &relocs.p[i]; - struct sym *sym = findsym(rel->symname); + Reloc *rel = &relocs.p[i]; + Sym *sym = findsym(rel->symname); if (sym) { uint idx = sym - symtab.p; rel->symidx = idx < ndefsym ? defsym2idx[idx] : idx; } else { assert(symtab.n < 1<<16); - vpush(&symtab, ((struct sym) { str2idx(&rel->symname->c), .bind = STB_GLOBAL, .type = STT_NOTYPE, .shndx = SHN_UND })); + vpush(&symtab, ((Sym) { str2idx(&rel->symname->c), .bind = STB_GLOBAL, .type = STT_NOTYPE, .shndx = SHN_UND })); pmap_set(&symht, rel->symname, symtab.n-1); rel->symidx = symtab.n-1; } @@ -492,7 +492,7 @@ elffini(struct wbuf *out) /* symtab */ wordalign(out, align); for (int i = 0; i < symtab.n; ++i) { - struct sym *sym = &symtab.p[i < ndefsym ? sortedsyms[i] : i]; + Sym *sym = &symtab.p[i < ndefsym ? sortedsyms[i] : i]; if (sym->bind == STB_LOCAL) ++nlocal; putsym(out, sym); } @@ -502,7 +502,7 @@ elffini(struct wbuf *out) assert(relocs.n == ntextrel + nrodatarel + ndatarel); for (enum section s = Stext; s <= Sbss; ++s) { for (int i = 0; i < relocs.n; ++i) { - struct reloc *rel = &relocs.p[i]; + Reloc *rel = &relocs.p[i]; if (rel->section != s) continue; putreloc(out, rel, userela); } @@ -510,8 +510,8 @@ elffini(struct wbuf *out) /** Section Headers **/ wordalign(out, align); -#define putshdr(...) if (targ_64bit) elf64putshdr(out, &(struct elf64shdr) { __VA_ARGS__ }); \ - else elf32putshdr(out, &(struct elf32shdr) { __VA_ARGS__ }); +#define putshdr(...) if (targ_64bit) elf64putshdr(out, &(Elf64Shdr) { __VA_ARGS__ }); \ + else elf32putshdr(out, &(Elf32Shdr) { __VA_ARGS__ }); /* §0 null section */ putshdr(0); /* §1 .text */ diff --git a/src/o_elf.h b/src/o_elf.h index c63a2be..68bc882 100644 --- a/src/o_elf.h +++ b/src/o_elf.h @@ -46,7 +46,7 @@ enum { }; \ } -struct elf64hdr { +typedef struct Elf64Hdr { ELF_HDRIDENT; u16int type, machine; @@ -61,10 +61,10 @@ struct elf64hdr { shentsize, shnum, shstrndx; -}; -_Static_assert(sizeof(struct elf64hdr) == 64, ""); +} Elf64Hdr; +_Static_assert(sizeof(Elf64Hdr) == 64, ""); -struct elf32hdr { +typedef struct Elf32Hdr { ELF_HDRIDENT; u16int type, machine; @@ -79,8 +79,8 @@ struct elf32hdr { shentsize, shnum, shstrndx; -}; -_Static_assert(sizeof(struct elf32hdr) == 52, ""); +} Elf32Hdr; +_Static_assert(sizeof(Elf32Hdr) == 52, ""); enum { SHT_NULL = 0x0, @@ -115,7 +115,7 @@ enum { SHF_TLS = 0x400, }; -struct elf64shdr { +typedef struct Elf64Shdr { u32int name, type; u64int flags, @@ -126,10 +126,10 @@ struct elf64shdr { info; u64int addralign, entsize; -}; -_Static_assert(sizeof(struct elf64shdr) == 64, ""); +} Elf64Shdr; +_Static_assert(sizeof(Elf64Shdr) == 64, ""); -struct elf32shdr { +typedef struct Elf32Shdr { u32int name, type, flags, @@ -140,8 +140,8 @@ struct elf32shdr { info, addralign, entsize; -}; -_Static_assert(sizeof(struct elf32shdr) == 40, ""); +} Elf32Shdr; +_Static_assert(sizeof(Elf32Shdr) == 40, ""); enum { STB_LOCAL, @@ -164,48 +164,48 @@ enum { #define ELF_S_INFO(b,t) ((b) << 4 | (t)) -struct elf64sym { +typedef struct Elf64Sym { u32int name; u8int info, other; u16int shndx; u64int value, size; -}; -_Static_assert(sizeof(struct elf64sym) == 24, ""); +} Elf64Sym; +_Static_assert(sizeof(Elf64Sym) == 24, ""); -struct elf32sym { +typedef struct Elf32Sym { u32int name, value, size; u8int info, other; u16int shndx; -}; -_Static_assert(sizeof(struct elf32sym) == 16, ""); +} Elf32Sym; +_Static_assert(sizeof(Elf32Sym) == 16, ""); #define ELF64_R_INFO(s,t) ((u64int) (s) << 32 | (u32int)(t)) -struct elf64rel { +typedef struct { u64int offset, info; -}; -_Static_assert(sizeof(struct elf64rel) == 16, ""); +} Elf64Rel; +_Static_assert(sizeof(Elf64Rel) == 16, ""); #define ELF32_R_INFO(s,t) ((s) << 8 | (u8int)(t)) -struct elf32rel { +typedef struct { u32int offset, info; -}; -_Static_assert(sizeof(struct elf32rel) == 8, ""); +} Elf32Rel; +_Static_assert(sizeof(Elf32Rel) == 8, ""); -struct elf64rela { +typedef struct { u64int offset, info; s64int addend; -}; -_Static_assert(sizeof(struct elf64rela) == 24, ""); +} Elf64Rela; +_Static_assert(sizeof(Elf64Rela) == 24, ""); -struct elf32rela { +typedef struct { u32int offset, info; s32int addend; -}; -_Static_assert(sizeof(struct elf32rela) == 12, ""); +} Elf32Rela; +_Static_assert(sizeof(Elf32Rela) == 12, ""); /* vim:set ts=3 sw=3 expandtab: */ @@ -9,9 +9,9 @@ void elfinit(void); enum section elfhassym(internstr , uint *value); void elfaddsym(internstr , int info, enum section, uvlong value, uvlong size); void elfreloc(internstr sym, enum relockind, enum section, uint off, vlong addend); -void elffini(struct wbuf *); +void elffini(WriteBuf *); -struct objfile objout; +ObjFile objout; enum { NTEXT = 4<<20 /* 4MiB */ }; @@ -48,7 +48,7 @@ objhassym(internstr name, uint *off) uint objnewdat(internstr name, enum section sec, bool globl, uint siz, uint align) { - struct objfile *o = &objout; + ObjFile *o = &objout; uint off; assert(siz && align && ispo2(align)); switch (sec) { @@ -103,7 +103,7 @@ void objfini(void) { static char buf[1<<12]; - struct wbuf out = FDBUF(buf, sizeof buf, open(objout.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666)); + WriteBuf out = FDBUF(buf, sizeof buf, open(objout.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666)); if (out.fd < 0) fatal(NULL, "could not open %'s for writing: %s", objout.outfile, strerror(errno)); switch (mctarg->objkind) { @@ -1,13 +1,13 @@ #include "antcc.h" -extern struct objfile { +typedef struct ObjFile { const char *infile, *outfile; uchar *textbegin, *textend; uchar *code; uchar dataalign, rodataalign, bssalign; uint nbss; vec_of(uchar) data, rodata; -} objout; +} ObjFile; enum relockind { REL_ABS64, @@ -26,6 +26,7 @@ enum relockind { }; enum section { Snone, Stext, Srodata, Sdata, Sbss }; +extern ObjFile objout; void objini(const char *infile, const char *outfile); void objdeffunc(internstr nam, bool globl, uint off, uint siz); enum section objhassym(internstr name, uint *off); diff --git a/src/t_aarch64.h b/src/t_aarch64.h index ad017c1..997a5fd 100644 --- a/src/t_aarch64.h +++ b/src/t_aarch64.h @@ -9,8 +9,8 @@ enum reg { }; bool aarch64_logimm(uint *enc, enum irclass, uvlong x); -void aarch64_isel(struct function *); -void aarch64_emit(struct function *); +void aarch64_isel(Function *); +void aarch64_emit(Function *); /* vim:set ts=3 sw=3 expandtab: */ diff --git a/src/t_aarch64_aapcs.c b/src/t_aarch64_aapcs.c index 8b90ff2..1bfd23e 100644 --- a/src/t_aarch64_aapcs.c +++ b/src/t_aarch64_aapcs.c @@ -1,7 +1,7 @@ #include "t_aarch64.h" static int -abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union irtype typ) +abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, IRType typ) { enum { NINT = 8, NFLT = 8 }; if (!typ.isagg) { @@ -19,7 +19,7 @@ abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union } static int -abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype typ) +abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, IRType typ) { if (!typ.isagg) { r[0] = kisflt(cls[0] = typ.cls) ? V(0) : R0; @@ -36,13 +36,13 @@ abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype typ) } static void -vastart(struct function *fn, struct block *blk, int *curi) +vastart(Function *fn, Block *blk, int *curi) { assert(!"nyi"); } static void -vaarg(struct function *fn, struct block *blk, int *curi) +vaarg(Function *fn, Block *blk, int *curi) { assert(!"nyi"); } @@ -54,7 +54,7 @@ static const char aarch64_rnames[][6] = { "V16","V17","V18","V19","V20","V21","V22","V23","V24","V25","V26","V27","V28","V29","V30","V31", }; -const struct mctarg t_aarch64_aapcs = { +const MCTarg t_aarch64_aapcs = { .gpr0 = R0, .ngpr = 31, .bpr = FP, .gprscratch = R(16), .fprscratch = V(31), diff --git a/src/t_aarch64_emit.c b/src/t_aarch64_emit.c index ac7004b..4679569 100644 --- a/src/t_aarch64_emit.c +++ b/src/t_aarch64_emit.c @@ -11,7 +11,7 @@ enum operkind { ONONE, OREGZR, OREG, OIMM, OMEM, OSYM }; enum shiftkind { SLSL, SLSR, SASR, SROR }; enum addrmode { AIMMIDX, AREGIDX, APREIDX, APOSTIDX }; enum addrregext { XUXTW = 2, XLSL = 3, XSXTW = 6, XSXTX = 7 }; -struct oper { +typedef struct Oper { uchar t; union { struct { /* OREG (opt. shifted) */ @@ -37,14 +37,14 @@ struct oper { int cdisp; }; }; -}; +} Oper; -#define REGZR ((struct oper){OREGZR, .reg=31}) -#define mkoper(t, ...) ((struct oper){(t), __VA_ARGS__}) +#define REGZR ((Oper){OREGZR, .reg=31}) +#define mkoper(t, ...) ((Oper){(t), __VA_ARGS__}) #define reg2oper(r) (assert((uint)(r) <= V(31)), mkoper(OREG, .reg = (r))) -static struct oper -mkmemoper(uint msiz, union ref r) +static Oper +mkmemoper(uint msiz, Ref r) { if (r.t == RTMP) { assert(in_range(instrtab[r.i].reg-1, R0, SP)); @@ -54,7 +54,7 @@ mkmemoper(uint msiz, union ref r) } else if (isaddrcon(r,1)) { return mkoper(OSYM, .con = r.i,); } else if (r.t == RADDR) { - const struct addr *addr = &addrtab.p[r.i]; + const IRAddr *addr = &addrtab.p[r.i]; assert(addr->shift <= 3 && (!addr->disp || !addr->index.bits)); if (isaddrcon(addr->base,0)) { assert(!addr->index.bits); @@ -78,8 +78,8 @@ mkmemoper(uint msiz, union ref r) assert(!"nyi"); } -static struct oper -ref2oper(union ref r) +static Oper +ref2oper(Ref r) { switch (r.t) { case RTMP: return instrtab[r.i].reg ? mkoper(ONONE,) : reg2oper(instrtab[r.i].reg-1); @@ -147,16 +147,16 @@ enum operenc { EN_FPCMPZ, /* float cmp with zero */ EN_FPCMP, /* float cmp-imm */ }; -struct desc { +typedef struct EncDesc { uchar psiz; /* subset of {4,8} */ uchar pt[3]; /* bitsets of enum operpat, up to 3 operands */ uint opc; uchar operenc; /* enum operenc */ -}; +} EncDesc; /* match operand against pattern */ static inline bool -opermatch(enum operpat pat, enum irclass k, struct oper o) +opermatch(enum operpat pat, enum irclass k, Oper o) { switch (pat) { case PNONE: return !o.t; @@ -206,9 +206,9 @@ static int rbpoff; /* Given an instruction description table, find the first entry that matches * the operands and encode it. */ static void -encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct oper o[3]) +encode(uchar **pcode, const EncDesc *tab, int ntab, enum irclass k, Oper o[3]) { - const struct desc *en = NULL; + const EncDesc *en = NULL; for (int i = 0; i < ntab; ++i) { if (!(tab[i].psiz & cls2siz[k])) continue; for (int j = 0; j < 3; ++j) @@ -298,27 +298,28 @@ encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct o } W32(ins); } -#define DEFINSTR1(X, ...) \ - static void \ - X(uchar **pcode, enum irclass k, struct oper a) \ - { \ - static const struct desc tab[] = { __VA_ARGS__ }; \ - encode(pcode, tab, countof(tab), k, ((struct oper [3]){a})); \ +#define DEFINSTR1(X, ...) \ + static void \ + X(uchar **pcode, enum irclass k, Oper a) \ + { \ + static const EncDesc tab[] = { __VA_ARGS__ }; \ + encode(pcode, tab, countof(tab), k, ((Oper [3]){a})); \ } -#define DEFINSTR2(X, ...) \ - static void \ - X(uchar **pcode, enum irclass k, struct oper op1, struct oper op2) \ - { \ - static const struct desc tab[] = { __VA_ARGS__ }; \ - encode(pcode, tab, countof(tab), k, ((struct oper [3]){op1,op2})); \ +#define DEFINSTR2(X, ...) \ + static void \ + X(uchar **pcode, enum irclass k, Oper op1, Oper op2) \ + { \ + static const EncDesc tab[] = { __VA_ARGS__ }; \ + encode(pcode, tab, countof(tab), k, ((Oper [3]){op1,op2})); \ } -#define DEFINSTR3(X, ...) \ - static void \ - X(uchar **pcode, enum irclass k, struct oper op1, struct oper op2, struct oper op3) \ - { \ - static const struct desc tab[] = { __VA_ARGS__ }; \ - encode(pcode, tab, countof(tab), k, ((struct oper [3]){op1,op2,op3})); \ + +#define DEFINSTR3(X, ...) \ + static void \ + X(uchar **pcode, enum irclass k, Oper op1, Oper op2, Oper op3) \ + { \ + static const EncDesc tab[] = { __VA_ARGS__ }; \ + encode(pcode, tab, countof(tab), k, ((Oper [3]){op1,op2,op3})); \ } DEFINSTR2(Xadrp, @@ -343,7 +344,7 @@ DEFINSTR3(Xsubs, ) static void -Xmadd(uchar **pcode, enum irclass k, struct oper d, struct oper n, struct oper m, struct oper a) +Xmadd(uchar **pcode, enum irclass k, Oper d, Oper n, Oper m, Oper a) { assert(opermatch(PGPRZ, k, d) && opermatch(PGPRZ, k, n) && opermatch(PGPRZ, k, a) && opermatch(PGPRZ, k, m)); @@ -371,7 +372,7 @@ DEFINSTR3(Xlslv, {4|8, {PGPRZ, PGPRZ, PGPRZ}, 0x1AC02000, EN_ARITH3R}) DEFINSTR3(Xlsrv, {4|8, {PGPRZ, PGPRZ, PGPRZ}, 0x1AC02400, EN_ARITH3R}) DEFINSTR3(Xasrv, {4|8, {PGPRZ, PGPRZ, PGPRZ}, 0x1AC02800, EN_ARITH3R}) static void -Xubfm(uchar **pcode, enum irclass k, struct oper rd, struct oper rn, uint immr, uint imms) +Xubfm(uchar **pcode, enum irclass k, Oper rd, Oper rn, uint immr, uint imms) { uint x = k != KI32; uint nbit = x ? 64 : 32; @@ -379,7 +380,7 @@ Xubfm(uchar **pcode, enum irclass k, struct oper rd, struct oper rn, uint immr, W32(x<<31 | 0x53000000 | x<<22 | immr<<16 | imms<<10 | rn.reg<<5 | rd.reg); } static void -Xsbfm(uchar **pcode, enum irclass k, struct oper rd, struct oper rn, uint immr, uint imms) +Xsbfm(uchar **pcode, enum irclass k, Oper rd, Oper rn, uint immr, uint imms) { uint x = k != KI32; uint nbit = x ? 64 : 32; @@ -478,7 +479,7 @@ DEFINSTR3(Xfstp, {8, {PFPR, PFPR, PMEMPREPOST}, 0x6C800000, EN_MEMPPREPOST} /* STP (immediate, (pre/postinc)) */ ) static void -Xcall(uchar **pcode, struct oper dst) +Xcall(uchar **pcode, Oper dst) { if (dst.t == OSYM) { objreloc(xcon2sym(dst.con), REL_CALL26, Stext, *pcode - objout.textbegin, 0); @@ -516,10 +517,10 @@ DEFINSTR2(Xfcmp, ) static void -gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct oper dst, union ref val) +gencopy(uchar **pcode, enum irclass cls, Block *blk, int curi, Oper dst, Ref val) { assert(dst.t == OREG); - struct oper src; + Oper src; if (val.bits == UNDREF.bits) return; if (isintcon(val)) { assert(dst.reg <= R(31)); @@ -564,7 +565,7 @@ gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct ope /* maps blk -> address when resolved; or to linked list of jump displacement * relocations */ -static struct blkaddr { +static struct BlkAddr { bool resolved; union { uint addr; @@ -579,7 +580,7 @@ enum cc { }; static void -Xbcc(uchar **pcode, enum cc cc, struct block *dst) +Xbcc(uchar **pcode, enum cc cc, Block *dst) { int disp, insaddr = *pcode - objout.textbegin; @@ -595,7 +596,7 @@ Xbcc(uchar **pcode, enum cc cc, struct block *dst) } static void -Xcbcc(uchar **pcode, enum irclass k, uint rt, enum cc cc, struct block *dst) +Xcbcc(uchar **pcode, enum irclass k, uint rt, enum cc cc, Block *dst) { int disp, insaddr = *pcode - objout.textbegin; if (blkaddr[dst->id].resolved) { @@ -621,17 +622,17 @@ static const schar icmpop2cc[] = { }; static void -emitbranch(uchar **pcode, struct block *blk) +emitbranch(uchar **pcode, Block *blk) { enum irclass cbk = 0; - struct oper cbopr; + Oper cbopr; enum cc cc = CCAL; assert(blk->s1); if (blk->s2) { /* conditional branch.. */ - union ref arg = blk->jmp.arg[0]; + Ref arg = blk->jmp.arg[0]; assert(arg.t == RTMP); - struct instr *ins = &instrtab[arg.i]; + Instr *ins = &instrtab[arg.i]; if (in_range(ins->op, Oequ, Oneq) && ins->r.bits == ZEROREF.bits) { cc = ins->op == Oequ ? CCEQ : CCNE; cbk = ins->cls; @@ -647,7 +648,7 @@ emitbranch(uchar **pcode, struct block *blk) if (blk->s1 == blk->lnext) { /* if s1 is next adjacent block, swap s1,s2 and flip condition to emit a * single jump */ - struct block *tmp = blk->s1; + Block *tmp = blk->s1; blk->s1 = blk->s2; blk->s2 = tmp; cc ^= 1; @@ -662,15 +663,15 @@ emitbranch(uchar **pcode, struct block *blk) Xbcc(pcode, CCAL, blk->s2); } -static struct instr *lastcmp; +static Instr *lastcmp; static void -emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struct instr *ins) +emitinstr(uchar **pcode, Function *fn, Block *blk, int curi, Instr *ins) { - struct oper dst, o1, o2; + Oper dst, o1, o2; enum irclass cls = ins->cls; - void (*X3)(uchar **, enum irclass, struct oper, struct oper, struct oper) = NULL; - void (*X2)(uchar **, enum irclass, struct oper, struct oper) = NULL; + void (*X3)(uchar **, enum irclass, Oper, Oper, Oper) = NULL; + void (*X2)(uchar **, enum irclass, Oper, Oper) = NULL; switch (ins->op) { default: fatal(NULL, "aarch64 unimplemented instr: %s", opnames[ins->op]); @@ -843,25 +844,25 @@ emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struc } } -struct frame { +typedef struct Frame { regset save; - struct rpair { uchar a,b; } pairs[10]; + struct RPair { uchar a,b; } pairs[10]; uchar single[2]; uint nfpairs, ngpairs; -}; +} Frame; static void -prologue(uchar **pcode, struct frame *frame, struct function *fn) +prologue(uchar **pcode, Frame *frame, Function *fn) { - *frame = (struct frame){0}; + *frame = (Frame){0}; regset save = frame->save = (fn->regusage & mctarg->rcallee) | (usefp * BIT(FP)) | (!fn->isleaf * BIT(LR)); if (save) { int prev = 0; - struct rpair *p = frame->pairs; + struct RPair *p = frame->pairs; for (uint reg = V(8); reg <= V(15); ++reg) { if (!rstest(save, reg)) continue; if (prev) { - *p++ = (struct rpair) {prev, reg}; + *p++ = (struct RPair) {prev, reg}; ++frame->nfpairs; prev = 0; } else prev = reg; @@ -873,7 +874,7 @@ prologue(uchar **pcode, struct frame *frame, struct function *fn) frame->single[1] = prev = lowestsetbit(save); rsclr(&save, prev); } else { - *p++ = (struct rpair) {prev, V(0)}; + *p++ = (struct RPair) {prev, V(0)}; ++frame->nfpairs; } prev = 0; @@ -883,7 +884,7 @@ prologue(uchar **pcode, struct frame *frame, struct function *fn) for (uint reg = R(19); reg <= LR; ++reg) { if (!rstest(save, reg)) continue; if (prev) { - *p++ = (struct rpair) {prev, reg}; + *p++ = (struct RPair) {prev, reg}; ++frame->ngpairs; prev = 0; } else prev = reg; @@ -891,7 +892,7 @@ prologue(uchar **pcode, struct frame *frame, struct function *fn) assert(!prev); p = frame->pairs; - struct oper adr = mkoper(OMEM, .m = {.mode = APREIDX, .base = SP, .disp = -16}); + Oper adr = mkoper(OMEM, .m = {.mode = APREIDX, .base = SP, .disp = -16}); for (int i = 0; i < frame->nfpairs; ++i, ++p) Xfstp(pcode, KF64, reg2oper(p->a), reg2oper(p->b), adr); adr.m.disp = -8; @@ -915,12 +916,12 @@ prologue(uchar **pcode, struct frame *frame, struct function *fn) } static void -epilogue(uchar **pcode, struct function *fn, struct frame *frame) +epilogue(uchar **pcode, Function *fn, Frame *frame) { if (fn->stksiz) Xadd(pcode, KPTR, reg2oper(SP), reg2oper(SP), mkoper(OIMM, .imm = fn->stksiz)); if (frame->save) { - struct rpair *p = frame->pairs + frame->nfpairs + frame->ngpairs - 1; - struct oper adr = mkoper(OMEM, .m = {.mode = APOSTIDX, .base = SP, .disp = 16}); + struct RPair *p = frame->pairs + frame->nfpairs + frame->ngpairs - 1; + Oper adr = mkoper(OMEM, .m = {.mode = APOSTIDX, .base = SP, .disp = 16}); for (int i = 0; i < frame->ngpairs; ++i, --p) Xldp(pcode, KPTR, reg2oper(p->a), reg2oper(p->b), adr); adr.m.disp = 8; @@ -933,9 +934,9 @@ epilogue(uchar **pcode, struct function *fn, struct frame *frame) } static void -emitbin(struct function *fn) +emitbin(Function *fn) { - struct block *blk; + Block *blk; uchar **pcode = &objout.code; while ((*pcode - objout.textbegin) % 4) ++*pcode; @@ -946,12 +947,12 @@ emitbin(struct function *fn) /* only use frame pointer in non-leaf functions and functions that use the stack */ usefp = !fn->isleaf || fn->stksiz; - struct frame frame; + Frame frame; prologue(pcode, &frame, fn); if (*pcode - fnstart > 8) { /* largue prologue -> largue epilogue -> transform to use single exit point */ - struct block *exit = NULL; + Block *exit = NULL; blk = fn->entry->lprev; do { if (blk->jmp.t == Jret) { @@ -984,7 +985,7 @@ emitbin(struct function *fn) blk = fn->entry; do { - struct blkaddr *bb = &blkaddr[blk->id]; + struct BlkAddr *bb = &blkaddr[blk->id]; uint bbaddr = *pcode - objout.textbegin; assert(!bb->resolved); while (bb->relreloc) { @@ -1013,7 +1014,7 @@ emitbin(struct function *fn) } void -aarch64_emit(struct function *fn) +aarch64_emit(Function *fn) { fn->stksiz = alignup(fn->stksiz, 8); if (fn->stksiz > 1<<24) error(NULL, "'%s' stack frame too big", fn->name); diff --git a/src/t_aarch64_isel.c b/src/t_aarch64_isel.c index 7e5057c..16eb022 100644 --- a/src/t_aarch64_isel.c +++ b/src/t_aarch64_isel.c @@ -69,9 +69,9 @@ aarch64_logimm(uint *enc, enum irclass k, uvlong x) } -static void fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi); +static void fixarg(Ref *r, Instr *ins, Block *blk, int *curi); static void -regarg(union ref *r, enum irclass k, struct block *blk, int *curi) +regarg(Ref *r, enum irclass k, Block *blk, int *curi) { if (r->t != RTMP) { *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, k, *r)); @@ -84,7 +84,7 @@ regarg(union ref *r, enum irclass k, struct block *blk, int *curi) } static void -fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) +fixarg(Ref *r, Instr *ins, Block *blk, int *curi) { enum op op = ins ? ins->op : 0; if (isintcon(*r)) { @@ -123,7 +123,7 @@ fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) pun.f64 = contab.p[r->i].f; i = pun.i64; } - union ref gpr = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ki, mkintcon(ki, i))); + Ref gpr = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ki, mkintcon(ki, i))); *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, k, gpr)); } else if (oiscmp(op)) { return; @@ -131,7 +131,7 @@ fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, k, *r)); } } else if (r->t == RSTACK) { - struct instr adr = mkinstr(Osub, KPTR, mkref(RREG, FP), mkintcon(KI32, r->i)); + Instr adr = mkinstr(Osub, KPTR, mkref(RREG, FP), mkintcon(KI32, r->i)); if (op == Ocopy) *ins = adr; else @@ -142,10 +142,10 @@ fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) } static bool -arithfold(struct instr *ins) +arithfold(Instr *ins) { if (isnumcon(ins->l) && (!ins->r.t || isnumcon(ins->r))) { - union ref r; + Ref r; bool ok = ins->r.t ? foldbinop(&r, ins->op, ins->cls, ins->l, ins->r) : foldunop(&r, ins->op, ins->cls, ins->l); assert(ok && "fold?"); *ins = mkinstr(Ocopy, insrescls(*ins), r); @@ -155,16 +155,16 @@ arithfold(struct instr *ins) } static void -selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) +selcall(Function *fn, Instr *ins, Block *blk, int *curi) { - const struct call *call = &calltab.p[ins->r.i]; + const IRCall *call = &calltab.p[ins->r.i]; int iarg = *curi - 1; enum irclass cls; uint argstksiz = alignup(call->argstksiz, 16); for (int i = call->narg - 1; i >= 0; --i) { - struct abiarg abi = call->abiarg[i]; - struct instr *arg; + ABIArg abi = call->abiarg[i]; + Instr *arg; for (;; --iarg) { assert(iarg >= 0 && i >= 0 && "arg?"); if ((arg = &instrtab[blk->ins.p[iarg]])->op == Oarg) @@ -175,7 +175,7 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) assert(!abi.ty.isagg); *arg = mkinstr(Omove, call->abiarg[i].ty.cls, mkref(RREG, abi.reg), arg->r); } else { - union ref adr = mkaddr((struct addr){mkref(RREG, SP), .disp = abi.stk}); + Ref adr = mkaddr((IRAddr){mkref(RREG, SP), .disp = abi.stk}); int iargsave = iarg; if (!abi.ty.isagg) { /* scalar arg in stack */ *arg = mkinstr(cls2store[abi.ty.cls], 0, adr, arg->r); @@ -190,13 +190,13 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) } } if (call->argstksiz) { - union ref disp = mkref(RICON, argstksiz); - insertinstr(blk, iarg--, (struct instr){Osub, KPTR, .keep=1, .reg = SP+1, .l=mkref(RREG,SP), disp}); + Ref disp = mkref(RICON, argstksiz); + insertinstr(blk, iarg--, (Instr){Osub, KPTR, .keep=1, .reg = SP+1, .l=mkref(RREG,SP), disp}); ++*curi; - insertinstr(blk, *curi+1, (struct instr){Oadd, KPTR, .keep=1, .reg = SP+1, .l=mkref(RREG,SP), disp}); + insertinstr(blk, *curi+1, (Instr){Oadd, KPTR, .keep=1, .reg = SP+1, .l=mkref(RREG,SP), disp}); } if (isimm32(ins->l)) - ins->l = mkaddr((struct addr){.base = ins->l}); + ins->l = mkaddr((IRAddr){.base = ins->l}); else if (isintcon(ins->l)) ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, ins->l)); @@ -218,7 +218,7 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) } static bool -aimm(struct addr *addr, int disp) +aimm(IRAddr *addr, int disp) { if (addr->index.bits) return 0; vlong a = addr->disp; @@ -231,7 +231,7 @@ aimm(struct addr *addr, int disp) } static bool -ascale(struct addr *addr, union ref a, union ref b, uint siz/*1,2,4,8*/) +ascale(IRAddr *addr, Ref a, Ref b, uint siz/*1,2,4,8*/) { if (b.t != RICON) return 0; if (addr->index.bits || (addr->disp && !isaddrcon(addr->base,1))) return 0; @@ -245,13 +245,13 @@ ascale(struct addr *addr, union ref a, union ref b, uint siz/*1,2,4,8*/) } static bool -aadd(struct addr *addr, struct block *blk, int *curi, union ref r, uint siz/*1,2,4,8*/) +aadd(IRAddr *addr, Block *blk, int *curi, Ref r, uint siz/*1,2,4,8*/) { if (r.t == RSTACK) { if (addr->base.bits || addr->index.bits || !aimm(addr, -r.i)) goto Ref; addr->base = mkref(RREG, FP); } else if (r.t == RTMP) { - struct instr *ins = &instrtab[r.i]; + Instr *ins = &instrtab[r.i]; if (ins->op == Oadd) { if (!aadd(addr, blk, curi, ins->l, siz)) goto Ref; if (!aadd(addr, blk, curi, ins->r, siz)) goto Ref; @@ -290,9 +290,9 @@ aadd(struct addr *addr, struct block *blk, int *curi, union ref r, uint siz/*1,2 } static bool -fuseaddr(union ref *r, struct block *blk, int *curi, uint siz/*1,2,4,8*/) +fuseaddr(Ref *r, Block *blk, int *curi, uint siz/*1,2,4,8*/) { - struct addr addr = {0}; + IRAddr addr = {0}; if (isaddrcon(*r,1)) return 1; @@ -307,7 +307,7 @@ fuseaddr(union ref *r, struct block *blk, int *curi, uint siz/*1,2,4,8*/) addr.base = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, .l = addr.base)); } else { addr.base = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, - mkaddr((struct addr){addr.base, .disp = addr.disp}))); + mkaddr((IRAddr){addr.base, .disp = addr.disp}))); addr.disp = 0; } } @@ -332,11 +332,11 @@ static const uchar storesz[] = { [Ostoref64 - Ostorei8] = 8, }; static void -loadstoreaddr(struct block *blk, union ref *r, int *curi, enum op op) +loadstoreaddr(Block *blk, Ref *r, int *curi, enum op op) { uint siz = oisload(op) ? loadsz[op-Oloads8] : storesz[op-Ostorei8]; if (isimm32(*r)) { - *r = mkaddr((struct addr){.base = *r}); + *r = mkaddr((IRAddr){.base = *r}); } else if (isaddrcon(*r, 0)) { bool pcrelok = in_range(op, Oloads32, Oloadi64); /* LDR-LDRSW have PC-relative literal form */ if (!pcrelok || !(contab.p[r->i].flag & SLOCAL)) @@ -349,7 +349,7 @@ loadstoreaddr(struct block *blk, union ref *r, int *curi, enum op op) } static void -sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) +sel(Function *fn, Instr *ins, Block *blk, int *curi) { enum op op = ins->op; enum irclass cls; @@ -393,7 +393,7 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) case Oadd: if (isnumcon(ins->l)) { /* swap to have const in rhs */ - union ref tmp = ins->l; + Ref tmp = ins->l; ins->l = ins->r; ins->r = tmp; } @@ -409,7 +409,7 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) case Oand: case Oior: case Oxor: if (isnumcon(ins->l)) { /* swap to have const in rhs */ - union ref tmp = ins->l; + Ref tmp = ins->l; ins->l = ins->r; ins->r = tmp; } @@ -446,12 +446,12 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) } static void -seljmp(struct function *fn, struct block *blk) +seljmp(Function *fn, Block *blk) { if (blk->jmp.t == Jb && blk->jmp.arg[0].bits) { int curi = blk->ins.n; fixarg(&blk->jmp.arg[0], NULL, blk, &curi); - union ref c = blk->jmp.arg[0]; + Ref c = blk->jmp.arg[0]; if (c.t != RTMP) { enum irclass cls = c.t == RICON ? KI32 : c.t == RXCON && contab.p[c.i].cls ? contab.p[c.i].cls : KPTR; int curi = blk->ins.n; @@ -462,15 +462,15 @@ seljmp(struct function *fn, struct block *blk) if (!oiscmp(instrtab[c.i].op)) { enum irclass k = insrescls(instrtab[c.i]); blk->jmp.arg[0] = insertinstr(blk, blk->ins.n, mkinstr(Oneq, k, c, kisint(k) ? ZEROREF : mkfltcon(k, 0))); - struct instr *ins = &instrtab[blk->jmp.arg[0].i]; + Instr *ins = &instrtab[blk->jmp.arg[0].i]; ins->keep = 1; } else { instrtab[c.i].keep = 1; } } else if (blk->jmp.t == Jret) { if (blk->jmp.arg[0].bits) { - union ref r = mkref(RREG, fn->abiret[0].reg); - struct instr *ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[0].ty.cls, r, blk->jmp.arg[0])).i]; + Ref r = mkref(RREG, fn->abiret[0].reg); + Instr *ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[0].ty.cls, r, blk->jmp.arg[0])).i]; int curi = blk->ins.n-1; fixarg(&ins->r, ins, blk, &curi); blk->jmp.arg[0] = r; @@ -483,22 +483,22 @@ seljmp(struct function *fn, struct block *blk) } void -aarch64_isel(struct function *fn) +aarch64_isel(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; do { int i; for (i = 0; i < blk->phi.n; ++i) { - struct instr *ins = &instrtab[blk->phi.p[i]]; - union ref *phi = phitab.p[ins->l.i]; + Instr *ins = &instrtab[blk->phi.p[i]]; + Ref *phi = phitab.p[ins->l.i]; for (int i = 0; i < blk->npred; ++i) { int curi = blkpred(blk, i)->ins.n; fixarg(&phi[i], ins, blkpred(blk, i), &curi); } } for (i = 0; i < blk->ins.n; ++i) { - struct instr *ins = &instrtab[blk->ins.p[i]]; + Instr *ins = &instrtab[blk->ins.p[i]]; sel(fn, ins, blk, &i); } seljmp(fn, blk); diff --git a/src/t_x86-64.h b/src/t_x86-64.h index 91021ed..c1f3c49 100644 --- a/src/t_x86-64.h +++ b/src/t_x86-64.h @@ -12,7 +12,7 @@ enum reg { #undef R }; -void x86_64_isel(struct function *); -void x86_64_emit(struct function *); +void x86_64_isel(Function *); +void x86_64_emit(Function *); /* vim:set ts=3 sw=3 expandtab: */ diff --git a/src/t_x86-64_emit.c b/src/t_x86-64_emit.c index 37a3f9c..e1df982 100644 --- a/src/t_x86-64_emit.c +++ b/src/t_x86-64_emit.c @@ -10,7 +10,7 @@ */ enum operkind { ONONE, OREG, OIMM, OMEM, OSYM, OSYMGOT }; enum { NOBASE = 63, NOINDEX = 63 }; -struct oper { +typedef struct Oper { uchar t; union { struct { uchar base; }; /* OMEM */ @@ -25,21 +25,21 @@ struct oper { int disp; /* OMEM, OSYM */ int imm; /* OIMM */ }; -}; -#define mkoper(t, ...) ((struct oper){(t), __VA_ARGS__}) +} Oper; +#define mkoper(t, ...) ((Oper){(t), __VA_ARGS__}) #define reg2oper(R) (assert((uint)(R) <= XMM15), mkoper(OREG, .reg = (R))) -static struct oper mkmemoper(union ref); +static Oper mkmemoper(Ref); -static struct oper +static Oper ioper(int i) { int reg = instrtab[i].reg - 1; return reg < 0 ? mkoper(ONONE,) : reg2oper(reg); } -static struct oper -ref2oper(union ref r) +static Oper +ref2oper(Ref r) { switch (r.t) { case RTMP: return ioper(r.i); @@ -62,7 +62,7 @@ ref2oper(union ref r) } static void -addmemoper(struct oper *mem, struct oper add) +addmemoper(Oper *mem, Oper add) { assert(mem->t == OMEM); if (add.t == OIMM) { @@ -80,15 +80,15 @@ addmemoper(struct oper *mem, struct oper add) /* helpers to convert a reference to an operand of a specific kind, * with assertions to make sure nothing went wrong */ -static inline struct oper -mkregoper(union ref r) +static inline Oper +mkregoper(Ref r) { assert(r.t == RREG || (r.t == RTMP && ioper(r.i).t == OREG)); return r.t == RREG ? reg2oper(r.i) : ioper(r.i); } -static inline struct oper -mkimmoper(union ref r) +static inline Oper +mkimmoper(Ref r) { assert(iscon(r) && concls(r) == KI32); return mkoper(OIMM, .imm = intconval(r)); @@ -97,37 +97,37 @@ mkimmoper(union ref r) #define ismemref(ref) ((ref).t == RTMP && ioper((ref).i).t == OMEM) #define isregref(ref) ((ref).t == RREG || ((ref).t == RTMP && ioper((ref).i).t == OREG)) -static inline struct oper -mkimmregoper(union ref r) +static inline Oper +mkimmregoper(Ref r) { assert(isregref(r) || (iscon(r) && concls(r) == KI32)); return ref2oper(r); } -static inline struct oper -mkdatregoper(union ref r) +static inline Oper +mkdatregoper(Ref r) { assert(isregref(r) || (r.t == RXCON && contab.p[r.i].deref)); return ref2oper(r); } -static inline struct oper -mkimmdatregoper(union ref r) +static inline Oper +mkimmdatregoper(Ref r) { assert(isregref(r) || r.t == RICON || (r.t == RXCON && (contab.p[r.i].cls == KI32 || contab.p[r.i].deref))); return ref2oper(r); } -static struct oper -mkmemoper(union ref r) +static Oper +mkmemoper(Ref r) { if (r.t == RTMP) { - struct oper wop = ioper(r.i); + Oper wop = ioper(r.i); if (wop.t == OMEM) return wop; assert(wop.t == OREG); return mkoper(OMEM, .base = wop.reg, .index = NOINDEX); } else if (r.t == RADDR) { - const struct addr *addr = &addrtab.p[r.i]; + const IRAddr *addr = &addrtab.p[r.i]; assert(addr->shift <= 3); if (isaddrcon(addr->base,0)) { return mkoper(OSYM, .con = addr->base.i, @@ -208,7 +208,7 @@ enum operenc { EN_R32, /* rel32 */ NOPERENC, }; -struct desc { +typedef struct EncDesc { uchar psiz; /* subset of {1,2,4,8} */ uchar ptd, pts; /* bitsets of enum operpat */ uchar nopc; /* countof opc */ @@ -217,11 +217,11 @@ struct desc { uchar ext; /* ModR/M.reg opc extension */ bool r8; /* uses 8bit register */ bool norexw; /* do not use REX.W even if size is 64 bits */ -}; +} EncDesc; /* match operand against pattern */ static inline bool -opermatch(enum operpat pat, struct oper oper) +opermatch(enum operpat pat, Oper oper) { switch (pat) { case PNONE: return !oper.t; @@ -258,13 +258,13 @@ static uchar *fnstart; /* Given an instruction description table, find the first entry that matches * the operands (where dst, src are the operands in intel syntax order) and encode it */ static void -encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct oper dst, struct oper src) +encode(uchar **pcode, const EncDesc *tab, int ntab, enum irclass k, Oper dst, Oper src) { const uchar *opc; int nopc; - struct oper mem; + Oper mem; enum reg reg; - const struct desc *en = NULL; + const EncDesc *en = NULL; for (int i = 0; i < ntab; ++i) { if ((tab[i].psiz & cls2siz[k]) && opermatch(tab[i].ptd, dst) && opermatch(tab[i].pts, src)) { en = &tab[i]; @@ -448,18 +448,18 @@ encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct o #define DEFINSTR1(X, ...) \ static void \ - X(uchar **pcode, enum irclass k, struct oper oper) \ + X(uchar **pcode, enum irclass k, Oper oper) \ { \ - static const struct desc tab[] = { __VA_ARGS__ }; \ + static const EncDesc tab[] = { __VA_ARGS__ }; \ encode(pcode, tab, countof(tab), k, oper, mkoper(0,)); \ } -#define DEFINSTR2(X, ...) \ - static void \ - X(uchar **pcode, enum irclass k, struct oper dst, struct oper src) \ - { \ - static const struct desc tab[] = { __VA_ARGS__ }; \ - encode(pcode, tab, countof(tab), k, dst, src); \ +#define DEFINSTR2(X, ...) \ + static void \ + X(uchar **pcode, enum irclass k, Oper dst, Oper src) \ + { \ + static const EncDesc tab[] = { __VA_ARGS__ }; \ + encode(pcode, tab, countof(tab), k, dst, src); \ } #define O(s) (sizeof s)-1,s @@ -471,9 +471,9 @@ DEFINSTR2(Xmovw, {-1, PMEM, PGPR, O("\x66\x89"), EN_MR}, /* MOV m16, r16 */ {-1, PMEM, PI32, O("\x66\xC7"), EN_MI16}, /* MOV m16, imm16 */ ) -static void Xmov(uchar **pcode, enum irclass k, struct oper dst, struct oper src) +static void Xmov(uchar **pcode, enum irclass k, Oper dst, Oper src) { - static const struct desc all[] = { + static const EncDesc all[] = { {4 , PGPR, PI32, O("\xB8"), EN_OI}, /* MOV r32, imm */ {4|8, PGPR, PGPR, O("\x8B"), EN_RR}, /* MOV r32/64, r32/64 */ {4|8, PMEM, PGPR, O("\x89"), EN_MR}, /* MOV m32/64, r32/64 */ @@ -692,7 +692,7 @@ DEFINSTR2(Ximul2, {4|8, PGPR, PGPR, O("\x0F\xAF"), EN_RR}, /* IMUL r32/64, r32/64 */ {4|8, PGPR, PMEM, O("\x0F\xAF"), EN_RM}, /* IMUL r32/64, m32/64 */ ) -static const struct desc imul3_imm8tab[] = { +static const EncDesc imul3_imm8tab[] = { {4|8, PGPR, PGPR, O("\x6B"), EN_RR}, /* IMUL r32/64, r32/64, (imm8) */ {4|8, PGPR, PMEM, O("\x6B"), EN_RM}, /* IMUL r32/64, m32/64, (imm8) */ }, imul3_imm32tab[] = { @@ -701,7 +701,7 @@ static const struct desc imul3_imm8tab[] = { }; #undef O static void -Ximul(uchar **pcode, enum irclass k, struct oper dst, struct oper s1, struct oper s2) +Ximul(uchar **pcode, enum irclass k, Oper dst, Oper s1, Oper s2) { if (!memcmp(&dst, &s1, sizeof dst) && s2.t != OIMM) { Ximul2(pcode, k, dst, s2); @@ -739,7 +739,7 @@ enum cc { /* maps blk -> address when resolved; or to linked list of jump displacement * relocations */ -static struct blkaddr { +static struct BlkAddr { bool resolved; union { uint addr; @@ -748,7 +748,7 @@ static struct blkaddr { } *blkaddr; static void -Xjcc(uchar **pcode, enum cc cc, struct block *dst) +Xjcc(uchar **pcode, enum cc cc, Block *dst) { int disp, insaddr = *pcode - objout.textbegin; bool rel8 = 0; @@ -817,7 +817,7 @@ Xpop(uchar **pcode, enum reg reg) /* are flags live at given instruction? */ static bool -flagslivep(struct block *blk, int curi) +flagslivep(Block *blk, int curi) { int cmpi; /* conditional branch that references a previous comparison instruction? */ @@ -836,7 +836,7 @@ flagslivep(struct block *blk, int curi) /* Copy dst = val, with some peephole optimizations */ static void -gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct oper dst, union ref val) +gencopy(uchar **pcode, enum irclass cls, Block *blk, int curi, Oper dst, Ref val) { assert(dst.t == OREG); if (val.bits == UNDREF.bits) { @@ -846,7 +846,7 @@ gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct ope if (val.t == RADDR) { /* this is a LEA, but maybe it can be lowered to a 2-address instruction, * which may clobber flags */ - const struct addr *addr = &addrtab.p[val.i]; + const IRAddr *addr = &addrtab.p[val.i]; if (flagslivep(blk, curi)) goto Lea; if (addr->base.t != RREG) goto Lea; if (addr->base.bits && dst.reg == mkregoper(addr->base).reg) { /* base = dst */ @@ -907,18 +907,18 @@ gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct ope wr64le(*pcode, intconval(val)); /* imm64 */ *pcode += 8; } else { - struct oper src = mkimmdatregoper(val); + Oper src = mkimmdatregoper(val); if (memcmp(&dst, &src, sizeof dst) != 0) Xmov(pcode, cls == KF64 && src.t == OREG && src.reg < XMM0 ? KI64 : cls, dst, src); } } static void -Xvaprologue(uchar **pcode, struct function *fn, struct oper sav) +Xvaprologue(uchar **pcode, Function *fn, Oper sav) { uint gpr0 = 0, fpr0 = 0, jmpaddr; for (int i = 0; i < fn->nabiarg; ++i) { - struct abiarg abi = fn->abiarg[i]; + ABIArg abi = fn->abiarg[i]; if (!abi.isstk) { if (abi.reg < XMM0) ++gpr0; else ++fpr0; @@ -970,13 +970,13 @@ static const uchar icmpzero2cc[] = { }; static void -emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struct instr *ins) +emitinstr(uchar **pcode, Function *fn, Block *blk, int curi, Instr *ins) { - struct oper dst, src; + Oper dst, src; bool regzeroed; enum irclass cls = ins->cls; - void (*X)(uchar **, enum irclass, struct oper, struct oper) = NULL; - void (*X1)(uchar **, enum irclass, struct oper) = NULL; + void (*X)(uchar **, enum irclass, Oper, Oper) = NULL; + void (*X1)(uchar **, enum irclass, Oper) = NULL; switch (ins->op) { default: @@ -1037,7 +1037,7 @@ emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struc /* also two-address after swapping operands */ Xadd(pcode, cls, reg2oper(ins->reg-1), mkimmdatregoper(ins->l)); } else { /* three-address add (lea) */ - struct oper mem = { OMEM, .base = NOBASE, .index = NOINDEX }; + Oper mem = { OMEM, .base = NOBASE, .index = NOINDEX }; dst = reg2oper(ins->reg-1); addmemoper(&mem, ref2oper(ins->l)); addmemoper(&mem, ref2oper(ins->r)); @@ -1141,7 +1141,7 @@ emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struc if (ins->reg && dst.reg != ins->reg-1 && (src.t != OREG || src.reg != ins->reg-1)) { /* can zero output reg before test instruction (differs from both inputs) */ /* XXX this doesn't check if a source operand is an addr containing the register */ - struct oper dst = reg2oper(ins->reg-1); + Oper dst = reg2oper(ins->reg-1); Xxor(pcode, KI32, dst, dst); regzeroed = 1; } @@ -1189,7 +1189,7 @@ emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struc if (kisint(cls)) Xxchg(pcode, cls, ref2oper(ins->l), mkregoper(ins->r)); else { - struct oper l = mkregoper(ins->l), r = mkregoper(ins->r); + Oper l = mkregoper(ins->l), r = mkregoper(ins->r); Xxor(pcode, cls, l, r); Xxor(pcode, cls, r, l); Xxor(pcode, cls, l, r); @@ -1205,16 +1205,16 @@ emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struc } static void -emitbranch(uchar **pcode, struct block *blk) +emitbranch(uchar **pcode, Block *blk) { enum cc cc = ALWAYS; assert(blk->s1); if (blk->s2) { /* conditional branch.. */ - union ref arg = blk->jmp.arg[0]; - struct block *unord = NULL; + Ref arg = blk->jmp.arg[0]; + Block *unord = NULL; assert(arg.t == RTMP); - struct instr *ins = &instrtab[arg.i]; + Instr *ins = &instrtab[arg.i]; if ((oiscmp(ins->op) || ins->op == Oand || ins->op == Osub)) { if (ins->r.bits != ZEROREF.bits) { /* for CMP instr */ @@ -1236,7 +1236,7 @@ emitbranch(uchar **pcode, struct block *blk) if (blk->s1 == blk->lnext) { /* if s1 is next adjacent block, swap s1,s2 and flip condition to emit a * single jump */ - struct block *tmp = blk->s1; + Block *tmp = blk->s1; blk->s1 = blk->s2; blk->s2 = tmp; cc ^= 1; @@ -1250,7 +1250,7 @@ emitbranch(uchar **pcode, struct block *blk) } static bool -calleesave(int *npush, uchar **pcode, struct function *fn) +calleesave(int *npush, uchar **pcode, Function *fn) { bool any = 0; if (rstest(fn->regusage, RBX)) { @@ -1268,7 +1268,7 @@ calleesave(int *npush, uchar **pcode, struct function *fn) } static void -calleerestore(uchar **pcode, struct function *fn) +calleerestore(uchar **pcode, Function *fn) { for (int r = R15; r >= R12; --r) if (rstest(fn->regusage, r)) @@ -1298,9 +1298,9 @@ nops(uchar **pcode, int align) } static void -emitbin(struct function *fn) +emitbin(Function *fn) { - struct block *blk; + Block *blk; uchar **pcode = &objout.code; int npush = 0; bool saverestore; @@ -1345,7 +1345,7 @@ emitbin(struct function *fn) if (*pcode - fnstart > 6) { /* largue prologue -> largue epilogue -> transform to use single exit point */ - struct block *exit = NULL; + Block *exit = NULL; blk = fn->entry->lprev; do { if (blk->jmp.t == Jret) { @@ -1378,7 +1378,7 @@ emitbin(struct function *fn) blk = fn->entry; do { - struct blkaddr *bb = &blkaddr[blk->id]; + struct BlkAddr *bb = &blkaddr[blk->id]; uint bbaddr = *pcode - objout.textbegin; assert(!bb->resolved); while (bb->relreloc) { @@ -1412,7 +1412,7 @@ emitbin(struct function *fn) } void -x86_64_emit(struct function *fn) +x86_64_emit(Function *fn) { fn->stksiz = alignup(fn->stksiz, 8); if (fn->stksiz > 1<<24) error(NULL, "'%s' stack frame too big", fn->name); diff --git a/src/t_x86-64_isel.c b/src/t_x86-64_isel.c index a2c41be..ac3950c 100644 --- a/src/t_x86-64_isel.c +++ b/src/t_x86-64_isel.c @@ -42,14 +42,14 @@ static int iflagsrc = -1; #define inscopy(blk, pcuri, k, r) insertinstr((blk), (*(pcuri))++, mkinstr(Ocopy, k, .l = (r))) static void -picfixsym(union ref *r, struct block *blk, int *curi) +picfixsym(Ref *r, Block *blk, int *curi) { if (!ccopt.pic || !isaddrcon(*r,0)) return; *r = inscopy(blk, curi, KPTR, *r); } static void -fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) +fixarg(Ref *r, Instr *ins, Block *blk, int *curi) { int sh; enum op op = ins ? ins->op : 0; @@ -57,7 +57,7 @@ fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) Begin: if (r->t == RXCON) { - struct xcon *con = &contab.p[r->i]; + IRCon *con = &contab.p[r->i]; if (in_range(op, Oshl, Oslr) && r == &ins->r) { sh = con->i; goto ShiftImm; @@ -79,7 +79,7 @@ Begin: /* float immediates & 64bit immediates are loaded from memory */ uchar data[8]; uint ksiz = cls2siz[con->cls]; - union type ctype; + Type ctype; /* can't use memory arg in rhs if lhs is memory */ bool docopy = ins && &ins->l != r && (oisstore(ins->op) || ins->l.t == RADDR); if (con->cls <= KPTR && (in_range(op, Ocopy, Omove) || op == Ophi)) @@ -110,7 +110,7 @@ Begin: ShiftImm: /* shift immediate is always 8bit */ *r = mkref(RICON, sh & 255); } else if (r->t == RSTACK) { - struct instr adr = mkinstr(Oadd, KPTR, mkref(RREG, RBP), mkintcon(KI32, -r->i)); + Instr adr = mkinstr(Oadd, KPTR, mkref(RREG, RBP), mkintcon(KI32, -r->i)); if (op == Ocopy) *ins = adr; else @@ -124,17 +124,17 @@ Begin: #define isimm32(r) (iscon(r) && concls(r) == KI32) static void -selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) +selcall(Function *fn, Instr *ins, Block *blk, int *curi) { - const struct call *call = &calltab.p[ins->r.i]; + const IRCall *call = &calltab.p[ins->r.i]; int iarg = *curi - 1; enum irclass cls; uint argstksiz = alignup(call->argstksiz, 16); int nsse = 0; for (int i = call->narg - 1; i >= 0; --i) { - struct abiarg abi = call->abiarg[i]; - struct instr *arg; + ABIArg abi = call->abiarg[i]; + Instr *arg; for (;; --iarg) { assert(iarg >= 0 && i >= 0 && "arg?"); if ((arg = &instrtab[blk->ins.p[iarg]])->op == Oarg) @@ -146,7 +146,7 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) *arg = mkinstr(Omove, call->abiarg[i].ty.cls, mkref(RREG, abi.reg), arg->r); if (abi.reg >= XMM0) ++nsse; } else { - union ref adr = mkaddr((struct addr){mkref(RREG, RSP), .disp = abi.stk}); + Ref adr = mkaddr((IRAddr){mkref(RREG, RSP), .disp = abi.stk}); int iargsave = iarg; if (!abi.ty.isagg) { /* scalar arg in stack */ *arg = mkinstr(cls2store[abi.ty.cls], 0, adr, arg->r); @@ -161,13 +161,13 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) } } if (call->argstksiz) { - union ref disp = mkref(RICON, argstksiz); - insertinstr(blk, iarg--, (struct instr){Osub, KPTR, .keep=1, .reg = RSP+1, .l=mkref(RREG,RSP), disp}); + Ref disp = mkref(RICON, argstksiz); + insertinstr(blk, iarg--, (Instr){Osub, KPTR, .keep=1, .reg = RSP+1, .l=mkref(RREG,RSP), disp}); ++*curi; - insertinstr(blk, *curi+1, (struct instr){Oadd, KPTR, .keep=1, .reg = RSP+1, .l=mkref(RREG,RSP), disp}); + insertinstr(blk, *curi+1, (Instr){Oadd, KPTR, .keep=1, .reg = RSP+1, .l=mkref(RREG,RSP), disp}); } if (isimm32(ins->l)) - ins->l = mkaddr((struct addr){.base = ins->l}); + ins->l = mkaddr((IRAddr){.base = ins->l}); else if (isintcon(ins->l)) ins->l = inscopy(blk, curi, KPTR, ins->l); @@ -193,7 +193,7 @@ selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) } static bool -aimm(struct addr *addr, vlong disp) +aimm(IRAddr *addr, vlong disp) { vlong a = addr->disp; a += disp; @@ -205,7 +205,7 @@ aimm(struct addr *addr, vlong disp) } static bool -ascale(struct addr *addr, union ref a, union ref b) +ascale(IRAddr *addr, Ref a, Ref b) { if (b.t != RICON) return 0; if (addr->index.bits) return 0; @@ -216,7 +216,7 @@ ascale(struct addr *addr, union ref a, union ref b) addr->shift = b.i; return 1; } else if (a.t == RTMP) { - struct instr *ins = &instrtab[a.i]; + Instr *ins = &instrtab[a.i]; /* factor out shifted immediate from 'shl {add %x, imm}, s' */ /* XXX maybe we shouldn't do this here because it should be done by a generic * arithemetic optimization pass ? */ @@ -235,7 +235,7 @@ ascale(struct addr *addr, union ref a, union ref b) } static bool -aadd(struct addr *out, struct block *blk, int *curi, union ref r, bool recurring) +aadd(IRAddr *out, Block *blk, int *curi, Ref r, bool recurring) { if (r.t == RSTACK) { if (out->base.bits || !aimm(out, -r.i)) { @@ -244,8 +244,8 @@ aadd(struct addr *out, struct block *blk, int *curi, union ref r, bool recurring } out->base = mkref(RREG, RBP); } else if (r.t == RTMP) { - struct instr *ins = &instrtab[r.i]; - struct addr adr = {0}; + Instr *ins = &instrtab[r.i]; + IRAddr adr = {0}; if (ins->op == Oadd) { if (recurring) goto Ref; if (aadd(&adr, blk, curi, ins->l, 1) && aadd(&adr, blk, curi, ins->r, 1)) { @@ -269,7 +269,7 @@ aadd(struct addr *out, struct block *blk, int *curi, union ref r, bool recurring ins->skip = 1; } else return 0; } else if (ins->op == Ocopy && ins->l.t == RADDR) { - const struct addr *adr2 = &addrtab.p[ins->l.i]; + const IRAddr *adr2 = &addrtab.p[ins->l.i]; adr = *adr2; goto Add2; } else if (ins->op == Oshl) { @@ -295,9 +295,9 @@ aadd(struct addr *out, struct block *blk, int *curi, union ref r, bool recurring } static bool -fuseaddr(union ref *r, struct block *blk, int *curi) +fuseaddr(Ref *r, Block *blk, int *curi) { - struct addr addr = { 0 }; + IRAddr addr = { 0 }; if (isaddrcon(*r,1)) return 1; if (!aadd(&addr, blk, curi, *r, 0)) return 0; @@ -306,7 +306,7 @@ fuseaddr(union ref *r, struct block *blk, int *curi) /* pic needs to load from GOT */ /* pie cannot encode RIP-relative address with index register */ /* first load symbol address into a temp register */ - union ref temp = mkaddr((struct addr){.base = addr.base, .disp = ccopt.pic ? 0 : addr.disp}); + Ref temp = mkaddr((IRAddr){.base = addr.base, .disp = ccopt.pic ? 0 : addr.disp}); addr.base = inscopy(blk, curi, KPTR, temp); if (!ccopt.pic) addr.disp = 0; } @@ -324,9 +324,9 @@ fuseaddr(union ref *r, struct block *blk, int *curi) /* is add instruction with this arg a candidate to transform into efective addr? */ static bool -addarg4addrp(union ref r) +addarg4addrp(Ref r) { - struct instr *ins; + Instr *ins; if (isaddrcon(r, 0)) return 1; if (r.t == RSTACK) return 1; if (r.t != RTMP) return 0; @@ -335,10 +335,10 @@ addarg4addrp(union ref r) } static void -loadstoreaddr(struct block *blk, union ref *r, int *curi) +loadstoreaddr(Block *blk, Ref *r, int *curi) { if (isimm32(*r)) { - *r = mkaddr((struct addr){.base = *r}); + *r = mkaddr((IRAddr){.base = *r}); } else if (isaddrcon(*r, 0)) { picfixsym(r, blk, curi); } else if (r->t == RSTACK || (r->t == RTMP && addarg4addrp(*r))) { @@ -349,10 +349,10 @@ loadstoreaddr(struct block *blk, union ref *r, int *curi) } static bool -arithfold(struct instr *ins) +arithfold(Instr *ins) { if (isnumcon(ins->l) && (!ins->r.t || isnumcon(ins->r))) { - union ref r; + Ref r; bool ok = ins->r.t ? foldbinop(&r, ins->op, ins->cls, ins->l, ins->r) : foldunop(&r, ins->op, ins->cls, ins->l); if (ok) { *ins = mkinstr(Ocopy, insrescls(*ins), r); @@ -363,10 +363,10 @@ arithfold(struct instr *ins) } static void -sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) +sel(Function *fn, Instr *ins, Block *blk, int *curi) { int t = ins - instrtab; - struct instr temp = {0}; + Instr temp = {0}; enum op op = ins->op; if (oisarith(ins->op) && arithfold(ins)) { @@ -444,7 +444,7 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) /* sub imm, x -> sub x, imm; neg x */ fixarg(&ins->l, ins, blk, curi); ins->inplace = 1; - struct instr sub = *ins; + Instr sub = *ins; rswap(sub.l, sub.r); ins->op = op = Oneg; ins->l = insertinstr(blk, (*curi)++, sub); @@ -459,7 +459,7 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) /* fallthru */ case Oadd: if (kisint(ins->cls) && (addarg4addrp(ins->l) || addarg4addrp(ins->r))) { - union ref it = mkref(RTMP, ins - instrtab); + Ref it = mkref(RTMP, ins - instrtab); if (fuseaddr(&it, blk, curi)) { *ins = mkinstr(Ocopy, ins->cls, it); break; @@ -533,7 +533,7 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) case Ocvtu64f: case Oexts8: case Oextu8: case Oexts16: case Oextu16: case Oexts32: if (isnumcon(ins->l)) { - union ref it; + Ref it; bool ok = foldunop(&it, ins->op, ins->cls, ins->l); assert(ok); ins->op = Ocopy; @@ -565,12 +565,12 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) } static void -seljmp(struct function *fn, struct block *blk) +seljmp(Function *fn, Block *blk) { if (blk->jmp.t == Jb && blk->jmp.arg[0].bits) { int curi = blk->ins.n; fixarg(&blk->jmp.arg[0], NULL, blk, &curi); - union ref c = blk->jmp.arg[0]; + Ref c = blk->jmp.arg[0]; if (c.t != RTMP) { enum irclass cls = c.t == RICON ? KI32 : c.t == RXCON && contab.p[c.i].cls ? contab.p[c.i].cls : KPTR; int curi = blk->ins.n; @@ -583,7 +583,7 @@ seljmp(struct function *fn, struct block *blk) instrtab[c.i].keep = 1; } else { if (kisflt(instrtab[c.i].cls) || !(opflags[instrtab[c.i].op] & ZF) || blk->ins.n == 0 || c.i != blk->ins.p[blk->ins.n - 1]) { - struct instr *ins; + Instr *ins; int curi = blk->ins.n; blk->jmp.arg[0] = insertinstr(blk, blk->ins.n, mkinstr(Oneq, insrescls(instrtab[c.i]), c, ZEROREF)); ins = &instrtab[blk->jmp.arg[0].i]; @@ -599,8 +599,8 @@ seljmp(struct function *fn, struct block *blk) } else if (blk->jmp.t == Jret) { if (blk->jmp.arg[0].bits) { int curi; - union ref r = mkref(RREG, fn->abiret[0].reg); - struct instr *ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[0].ty.cls, r, blk->jmp.arg[0])).i]; + Ref r = mkref(RREG, fn->abiret[0].reg); + Instr *ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[0].ty.cls, r, blk->jmp.arg[0])).i]; curi = blk->ins.n-1; fixarg(&ins->r, ins, blk, &curi); blk->jmp.arg[0] = r; @@ -616,14 +616,14 @@ seljmp(struct function *fn, struct block *blk) } void -x86_64_isel(struct function *fn) +x86_64_isel(Function *fn) { - struct block *blk = fn->entry; + Block *blk = fn->entry; do { for (int i = 0; i < blk->phi.n; ++i) { - struct instr *ins = &instrtab[blk->phi.p[i]]; - union ref *phi = phitab.p[ins->l.i]; + Instr *ins = &instrtab[blk->phi.p[i]]; + Ref *phi = phitab.p[ins->l.i]; for (int i = 0; i < blk->npred; ++i) { int curi = blkpred(blk, i)->ins.n; fixarg(&phi[i], ins, blkpred(blk, i), &curi); @@ -631,7 +631,7 @@ x86_64_isel(struct function *fn) } iflagsrc = -1; for (int i = 0; i < blk->ins.n; ++i) { - struct instr *ins = &instrtab[blk->ins.p[i]]; + Instr *ins = &instrtab[blk->ins.p[i]]; sel(fn, ins, blk, &i); if (ins->op < countof(opflags) && kisint(insrescls(*ins))) { if (opflags[ins->op] & ZF) iflagsrc = ins - instrtab; diff --git a/src/t_x86-64_sysv.c b/src/t_x86-64_sysv.c index b0cf204..6477af2 100644 --- a/src/t_x86-64_sysv.c +++ b/src/t_x86-64_sysv.c @@ -1,9 +1,9 @@ #include "t_x86-64.h" -static int classify(uchar cls[2], const struct typedata *td, uint off); +static int classify(uchar cls[2], const TypeData *td, uint off); static void -clsscalar(uchar cls[2], uint off, union type ty) +clsscalar(uchar cls[2], uint off, Type ty) { enum irclass k = type2cls[scalartypet(ty)]; uchar *fcls = &cls[off/8]; @@ -20,9 +20,9 @@ clsscalar(uchar cls[2], uint off, union type ty) } static int -classifyarr(uchar cls[2], union type ty, uint off) +classifyarr(uchar cls[2], Type ty, uint off) { - union type chld = typechild(ty); + Type chld = typechild(ty); uint n = typearrlen(ty), siz = typesize(chld); assert(n > 0); for (uint i = 0; i < n; ++i) { @@ -41,13 +41,13 @@ classifyarr(uchar cls[2], union type ty, uint off) } static int -classify(uchar cls[2], const struct typedata *td, uint off) +classify(uchar cls[2], const TypeData *td, uint off) { uint siz = alignup(td->siz, 4); if (siz > 16) /* MEMORY */ return 0; for (int i = 0; i < td->nmemb; ++i) { - struct fielddata *fld = &td->fld[i].f; + FieldData *fld = &td->fld[i].f; uint align = typealign(fld->t); if (alignup(fld->off, align) != fld->off) /* unaligned field -> MEMORY */ return cls[0] = cls[1] = 0; @@ -66,7 +66,7 @@ classify(uchar cls[2], const struct typedata *td, uint off) } static int -abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union irtype typ) +abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, IRType typ) { static const uchar intregs[] = { RDI, RSI, RDX, RCX, R8, R9 }; enum { NINT = countof(intregs), NFLT = 8 }; @@ -111,7 +111,7 @@ abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union } static int -abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype typ) +abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, IRType typ) { if (!typ.isagg) { r[0] = kisflt(cls[0] = typ.cls) ? XMM0 : RAX; @@ -165,12 +165,12 @@ abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype typ) */ static void -vastart(struct function *fn, struct block *blk, int *curi) +vastart(Function *fn, Block *blk, int *curi) { - union ref rsave; /* register save area */ + Ref rsave; /* register save area */ int gpr0 = 0, fpr0 = 0, stk0 = 0; - struct instr *ins = &instrtab[blk->ins.p[*curi]]; - union ref ap = ins->l, src, dst; + Instr *ins = &instrtab[blk->ins.p[*curi]]; + Ref ap = ins->l, src, dst; assert(ins->op == Ovastart); /* add xvaprologue if not there yet, which must be the first * real instruction in the function (following alloca) */ @@ -183,7 +183,7 @@ vastart(struct function *fn, struct block *blk, int *curi) } /* find first unnamed gpr and fpr */ for (int i = 0; i < fn->nabiarg; ++i) { - struct abiarg abi = fn->abiarg[i]; + ABIArg abi = fn->abiarg[i]; if (!abi.isstk){ if (abi.reg < XMM0) ++gpr0; else ++fpr0; @@ -209,26 +209,26 @@ vastart(struct function *fn, struct block *blk, int *curi) } static void -vaarg(struct function *fn, struct block *blk, int *curi) +vaarg(Function *fn, Block *blk, int *curi) { short r[2]; uchar cls[2]; - union ref tmp; + Ref tmp; int ni = 0, nf = 0, ns = 0; uchar r2off; int var = blk->ins.p[*curi]; - union ref ap = instrtab[var].l; - union irtype ty = ref2type(instrtab[var].r); + Ref ap = instrtab[var].l; + IRType ty = ref2type(instrtab[var].r); assert(instrtab[var].op == Ovaarg); - blk->ins.p[*curi] = newinstr(blk, (struct instr){Onop}); + blk->ins.p[*curi] = newinstr(blk, (Instr){Onop}); int ret = abiarg(r, cls, &r2off, &ni, &nf, &ns, ty); if (ret == 2) assert(!"nyi"); else if (ret == 1) { - struct block *merge; - union ref phi, phiargs[2]; + Block *merge; + Ref phi, phiargs[2]; /* int: l->gp_offset < 48 - num_gp * 8 */ /* sse: l->fp_offset < 304 - num_gp * 16 (why 304? ... 176) */ tmp = ni ? ap : insertinstr(blk, (*curi)++, mkinstr(Oadd, KPTR, ap, mkref(RICON, 4))); @@ -241,8 +241,8 @@ vaarg(struct function *fn, struct block *blk, int *curi) useblk(fn, blk->s1); { /* phi0: &l->reg_save_area[l->gp/fp_offset] */ - union ref sav = addinstr(fn, mkinstr(Oloadi64, KPTR, irbinop(fn, Oadd, KPTR, ap, mkref(RICON, 16)))); - union ref roff = addinstr(fn, mkinstr(Oloadu32, KI32, irbinop(fn, Oadd, KPTR, ap, mkref(RICON, ni ? 0 : 4)))); + Ref sav = addinstr(fn, mkinstr(Oloadi64, KPTR, irbinop(fn, Oadd, KPTR, ap, mkref(RICON, 16)))); + Ref roff = addinstr(fn, mkinstr(Oloadu32, KI32, irbinop(fn, Oadd, KPTR, ap, mkref(RICON, ni ? 0 : 4)))); phiargs[0] = irbinop(fn, Oadd, KPTR, sav, roff); /* l->gp/fp_offset += num_gp/fp * 8(16) */ roff = irbinop(fn, Oadd, KI32, roff, mkref(RICON, ni ? ni * 8 : nf * 16)); @@ -255,8 +255,8 @@ vaarg(struct function *fn, struct block *blk, int *curi) useblk(fn, blk->s2); { /* phi1: l->overflow_arg_area */ - union ref adr = irbinop(fn, Oadd, KPTR, ap, mkref(RICON, 8)); - union ref ovf = addinstr(fn, mkinstr(Oloadi64, KPTR, adr)); + Ref adr = irbinop(fn, Oadd, KPTR, ap, mkref(RICON, 8)); + Ref ovf = addinstr(fn, mkinstr(Oloadi64, KPTR, adr)); /* align no-op */ phiargs[1] = ovf; @@ -290,7 +290,7 @@ static const char x86_64_rnames[][6] = { #undef R }; -const struct mctarg t_x86_64_sysv = { +const MCTarg t_x86_64_sysv = { .gpr0 = RAX, .ngpr = R15 - RAX + 1, .bpr = RBP, .gprscratch = R11, .fprscratch = XMM15, diff --git a/src/u_bits.h b/src/u_bits.h index a4cef85..6810398 100644 --- a/src/u_bits.h +++ b/src/u_bits.h @@ -3,43 +3,43 @@ #include "antcc.h" static inline bool -bstest(const struct bitset *bs, uint i) +bstest(const BitSet *bs, uint i) { return bs[i/BSNBIT].u >> i%BSNBIT & 1; } static inline void -bsset(struct bitset *bs, uint i) +bsset(BitSet *bs, uint i) { bs[i/BSNBIT].u |= 1ull << i%BSNBIT; } static inline void -bsclr(struct bitset *bs, uint i) +bsclr(BitSet *bs, uint i) { bs[i/BSNBIT].u &= ~(1ull << i%BSNBIT); } static inline void -bszero(struct bitset bs[/*siz*/], uint siz) +bszero(BitSet bs[/*siz*/], uint siz) { memset(bs, 0, siz * sizeof *bs); } static inline void -bscopy(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz) +bscopy(BitSet dst[/*siz*/], const BitSet src[/*siz*/], uint siz) { while (siz--) dst++->u = src++->u; } static inline void -bsunion(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz) +bsunion(BitSet dst[/*siz*/], const BitSet src[/*siz*/], uint siz) { while (siz--) dst++->u |= src++->u; } static inline uint -bscount(struct bitset bs[/*siz*/], uint siz) +bscount(BitSet bs[/*siz*/], uint siz) { uint n = 0; while (siz--) n += popcnt(bs++->u); @@ -47,7 +47,7 @@ bscount(struct bitset bs[/*siz*/], uint siz) } static inline bool -bsiter(uint *i, struct bitset bs[/*siz*/], uint siz) +bsiter(uint *i, BitSet bs[/*siz*/], uint siz) { uint k = *i/BSNBIT, j = *i%BSNBIT; if (k >= siz) return 0; @@ -62,7 +62,7 @@ bsiter(uint *i, struct bitset bs[/*siz*/], uint siz) #define bs_each(T, var, bs, siz) for (T (var) = 0; bsiter(&(var), (bs), (siz)); ++(var)) static inline bool -bsiterzr(uint *i, struct bitset bs[/*siz*/], uint siz) +bsiterzr(uint *i, BitSet bs[/*siz*/], uint siz) { uint k = *i/BSNBIT, j = *i%BSNBIT; if (k >= siz) return 0; @@ -10,7 +10,7 @@ #include <sys/stat.h> #include <unistd.h> -struct wbuf bstdout, bstderr; +WriteBuf bstdout, bstderr; void ioinit(void) @@ -22,7 +22,7 @@ ioinit(void) } void -iowrite(struct wbuf *buf, const void *Src, int n) +iowrite(WriteBuf *buf, const void *Src, int n) { const uchar *src = Src; @@ -50,7 +50,7 @@ iowrite(struct wbuf *buf, const void *Src, int n) } void -ioflush(struct wbuf *buf) +ioflush(WriteBuf *buf) { int i, ret; @@ -80,7 +80,7 @@ ioflush(struct wbuf *buf) } void -ioputc(struct wbuf *buf, uchar c) +ioputc(WriteBuf *buf, uchar c) { if (buf->isfp) { buf->err = fputc(c, buf->fp) != EOF; @@ -97,7 +97,7 @@ ioputc(struct wbuf *buf, uchar c) } static int -putquoted(struct wbuf *buf, uchar c, uchar qchar, int next) +putquoted(WriteBuf *buf, uchar c, uchar qchar, int next) { if (c == qchar || c == '\\' || !aisprint(c)) { int n = (ioputc(buf, '\\'), 1); @@ -133,7 +133,7 @@ putquoted(struct wbuf *buf, uchar c, uchar qchar, int next) } static int -putuint(struct wbuf *buf, uvlong x, int base, bool lower) +putuint(WriteBuf *buf, uvlong x, int base, bool lower) { uchar tmp[64]; uchar *end = tmp + sizeof(tmp); @@ -177,7 +177,7 @@ fmterr(const char *fmt, ...) #define bputc(B, C) (ioputc(B, C), 1) static int -priquals(struct wbuf *buf, int q) +priquals(WriteBuf *buf, int q) { const char s[] = " const volatile", *p = s; int m = sizeof s - 1; @@ -189,10 +189,10 @@ priquals(struct wbuf *buf, int q) return m; } static int -pritypebefore(struct wbuf *buf, union type ty, int qual) +pritypebefore(WriteBuf *buf, Type ty, int qual) { const char *s, *s2; - union type chld; + Type chld; int n; switch (ty.t) { case TYVOID: s = "void"; Prim: n = bfmt(buf, "%s", s); return n + priquals(buf, qual); @@ -245,9 +245,9 @@ pritypebefore(struct wbuf *buf, union type ty, int qual) } static int -pritypeafter(struct wbuf *buf, union type ty, int qual) +pritypeafter(WriteBuf *buf, Type ty, int qual) { - const struct typedata *td; + const TypeData *td; int n = 0; switch (ty.t) { case TYPTR: @@ -280,7 +280,7 @@ pritypeafter(struct wbuf *buf, union type ty, int qual) } static int -fmttype(struct wbuf *buf, union type ty, int qual) +fmttype(WriteBuf *buf, Type ty, int qual) { int n = pritypebefore(buf, ty, qual); n += pritypeafter(buf, ty, qual); @@ -288,7 +288,7 @@ fmttype(struct wbuf *buf, union type ty, int qual) } static int -putdouble(struct wbuf *buf, double x) +putdouble(WriteBuf *buf, double x) { char tmp[200]; int n = snprintf(tmp, sizeof tmp, "%f", x); @@ -299,7 +299,7 @@ putdouble(struct wbuf *buf, double x) } int -vbfmt(struct wbuf *out, const char *fmt, va_list ap) +vbfmt(WriteBuf *out, const char *fmt, va_list ap) { bool quote, umod, lmod, zmod, lower, possign; int base; @@ -307,13 +307,13 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) int pad, prec, q; const char *s; void *p; - struct token *tok; - union type ty; + Token *tok; + Type ty; double f; char tmpbuf1[70], tmpbuf2[70]; - struct wbuf tmp1 = MEMBUF(tmpbuf1, sizeof tmpbuf1); - struct wbuf tmp2 = MEMBUF(tmpbuf2, sizeof tmpbuf2); - struct wbuf *buf = out; + WriteBuf tmp1 = MEMBUF(tmpbuf1, sizeof tmpbuf1); + WriteBuf tmp2 = MEMBUF(tmpbuf2, sizeof tmpbuf2); + WriteBuf *buf = out; int n = 0, prevn; while (*fmt) { @@ -475,7 +475,7 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) case 't': /* token/tokentag/type */ switch (*fmt++) { case 'k': /* tk token */ - tok = va_arg(ap, struct token *); + tok = va_arg(ap, Token *); Tok: switch (tok->t) { case TKXXX: @@ -583,7 +583,7 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) } break; case 't': /* tt token tag */ - tok = &(struct token) { va_arg(ap, int) }; + tok = &(Token) { va_arg(ap, int) }; switch (tok->t) { case TKNUMLIT: n += bwriteS(buf, "numeric literal"); @@ -611,11 +611,11 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) } break; case 'y': /* ty type */ - ty = va_arg(ap, union type); + ty = va_arg(ap, Type); n += fmttype(buf, ty, 0); break; case 'q': /* tq qualified type */ - ty = va_arg(ap, union type); + ty = va_arg(ap, Type); q = va_arg(ap, int); n += fmttype(buf, ty, q); break; @@ -666,7 +666,7 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) } int -bfmt(struct wbuf *buf, const char *fmt, ...) +bfmt(WriteBuf *buf, const char *fmt, ...) { va_list ap; int ret; @@ -678,7 +678,7 @@ bfmt(struct wbuf *buf, const char *fmt, ...) } void -gpritype(union type ty) +gpritype(Type ty) { efmt("%ty\n", ty); ioflush(&bstderr); @@ -686,15 +686,20 @@ gpritype(union type ty) static uint pagesiz; -extern struct embedfile embedfilesdir[]; +typedef struct EmbedFile { + const char *name; + const char *s; + size_t len; +} EmbedFile; +extern EmbedFile embedfilesdir[]; -struct memfile +MemFile mapopen(const char **err, const char *path) { struct stat stat; int fd = -1; void *p = NULL; - struct memfile f = {0}; + MemFile f = {0}; uint mapsiz; assert("nullp" && err && path); @@ -703,9 +708,9 @@ mapopen(const char **err, const char *path) *err = NULL; if (*path == '@' && path[1] == ':') { - for (struct embedfile *e = embedfilesdir; e->name; ++e) { + for (EmbedFile *e = embedfilesdir; e->name; ++e) { if (!strcmp(e->name, path+2)) { - return (struct memfile) { (const uchar *)e->s, e->len, .statik = 1 }; + return (MemFile) { (const uchar *)e->s, e->len, .statik = 1 }; } } } @@ -776,7 +781,7 @@ Err: } void -mapclose(struct memfile *f) +mapclose(MemFile *f) { assert(f->p); if (!f->statik) @@ -799,38 +804,37 @@ _assertfmt(const char *file, int line, const char *func, const char *expr) ioflush(&bstderr); } -struct fileuid { - long dev; - union { - long ino; - const char *str; - }; -}; - -/* one entry per #line */ -struct linemap { - int phys; - int toline; +typedef struct { + int phys; /* pre-preprocessing line number */ + int toline; /* line number in directive */ const char *tofile; -}; - -static struct file { - struct fileuid uid; +} LineMap; + +typedef struct FileUID FileUID; +typedef struct File { + struct FileUID { + long dev; + union { + long ino; + const char *str; + }; + } uid; const char *path; - struct memfile f; + MemFile f; vec_of(uint) lineoffs; - vec_of(struct linemap) linemap; - bool once; + vec_of(LineMap) linemap; /* one entry per #line directive */ bool seen; + bool once; /* uses guard macro or #pragma once */ internstr guardmac; -} *fileht[1<<SPANFILEBITS]; +} File; +static File *fileht[1<<SPANFILEBITS]; static int nfiles; int -getpredeffile(struct memfile **pf, const char *name) +getpredeffile(MemFile **pf, const char *name) { - struct file *f; - struct fileuid uid; + File *f; + FileUID uid; uint h, id, n = countof(fileht); uid.dev = -11; @@ -844,7 +848,7 @@ getpredeffile(struct memfile **pf, const char *name) f = allocz(&globarena, sizeof *f, 0); f->uid = uid; f->path = name; - f->f = (struct memfile) { .statik = 1 }; + f->f = (MemFile) { .statik = 1 }; fileht[id] = f; vinit(&f->lineoffs, NULL, 10); vpush(&f->lineoffs, 0); @@ -858,11 +862,11 @@ getpredeffile(struct memfile **pf, const char *name) } int -openfile(const char **err, struct memfile **pf, const char *path) +openfile(const char **err, MemFile **pf, const char *path) { struct stat st; - struct file *f; - struct fileuid uid; + File *f; + FileUID uid; size_t h, id, n = countof(fileht); if (*path == '@' && path[1] == ':') { @@ -887,7 +891,7 @@ openfile(const char **err, struct memfile **pf, const char *path) if (f && f->uid.dev == uid.dev && (uid.dev >= 0 ? f->uid.ino == uid.ino : !strcmp(f->uid.str, uid.str))) { break; } else if (!f) { - struct memfile m; + MemFile m; m = mapopen(err, path); if (*err) return -1; f = allocz(&globarena, sizeof *f, 0); @@ -915,7 +919,7 @@ getfilename(int id, uint atoff) return getfilepos(NULL, NULL, id, atoff); } -struct memfile * +MemFile * getfile(int id) { assert((uint)id < countof(fileht) && fileht[id]); @@ -935,7 +939,7 @@ void setfileline(int id, uint off, int line, const char *file) { assert((uint)id < countof(fileht) && fileht[id]); - vec_of(struct linemap) *linemap = (void *)&fileht[id]->linemap; + vec_of(LineMap) *linemap = (void *)&fileht[id]->linemap; vec_of(uint) *lineoffs = (void *)&fileht[id]->lineoffs; int phys = 2; for (int i = lineoffs->n-1; i >= 0; --i) { @@ -948,7 +952,7 @@ setfileline(int id, uint off, int line, const char *file) assert(linemap->p[linemap->n-1].phys < phys); if (!file) file = linemap->p[linemap->n-1].tofile; } - vpush(linemap, ((struct linemap){ phys, line, file })); + vpush(linemap, ((LineMap){ phys, line, file })); } const char * @@ -968,7 +972,7 @@ getfilepos(int *pline, int *pcol, int id, uint off) i -= offs[i] > off; int line = i + 1, col = off - offs[i] + 1; const char *file = fileht[id]->path; - vec_of(struct linemap) *linemap = (void *)&fileht[id]->linemap; + vec_of(LineMap) *linemap = (void *)&fileht[id]->linemap; if (linemap->n) { /* binary search over linemap array */ l = 0, h = linemap->n - 1, i = 0; @@ -1027,20 +1031,20 @@ closefile(int id) } void -vdiag(const struct span *span, enum diagkind kind, const char *fmt, va_list ap) +vdiag(const Span *span, enum diagkind kind, const char *fmt, va_list ap) { /* to avoid concurrent invocations of the compiler mixing up the diagnostics * in the unbuffered stderr output, use a separate buffer here and write() * it all out bypassing stdio */ static char ebuf[4096]; - static struct wbuf out = FDBUF(ebuf, sizeof ebuf, STDERR_FILENO); + static WriteBuf out = FDBUF(ebuf, sizeof ebuf, STDERR_FILENO); static int depth = 0; /* needed for nested note() calls */ static const char *label[] = { "error", "warning", "note" }; static const char *color[] = { "%g1;31.", "%g1;35.", "%g1;36." }; int line, col; - struct memfile *f; - const struct span0 *loc; + MemFile *f; + const Span0 *loc; ++depth; if (span) { @@ -1107,13 +1111,13 @@ vdiag(const struct span *span, enum diagkind kind, const char *fmt, va_list ap) if (span && loc == &span->ex && span->sl.len) if (span->ex.file != span->sl.file || !((uint) span->sl.off - span->ex.off < span->ex.len)) - note(&(struct span){ span->sl }, "expanded from here"); + note(&(Span){ span->sl }, "expanded from here"); if (--depth == 0) ioflush(&out); } void _Noreturn -fatal(const struct span *span, const char *fmt, ...) +fatal(const Span *span, const char *fmt, ...) { if (fmt) { va_list ap; @@ -1129,7 +1133,7 @@ int nerror, nwarn; enum { MAXERROR = 20 }; void -error(const struct span *span, const char *fmt, ...) +error(const Span *span, const char *fmt, ...) { va_list ap; @@ -1144,7 +1148,7 @@ error(const struct span *span, const char *fmt, ...) } void -warn(const struct span *span, const char *fmt, ...) +warn(const Span *span, const char *fmt, ...) { va_list ap; @@ -1161,7 +1165,7 @@ warn(const struct span *span, const char *fmt, ...) } void -note(const struct span *span, const char *fmt, ...) +note(const Span *span, const char *fmt, ...) { va_list ap; @@ -1173,13 +1177,13 @@ note(const struct span *span, const char *fmt, ...) /*** UTF util ***/ ushort * -utf8to16(uint *ulen, struct arena **arena, const uchar *s, size_t len) +utf8to16(uint *ulen, Arena **arena, const uchar *s, size_t len) { assert(0 && "nyi"); } uint * -utf8to32(uint *ulen, struct arena **arena, const uchar *s, size_t len) +utf8to32(uint *ulen, Arena **arena, const uchar *s, size_t len) { uint *ret, *w; const uchar *p, *end; diff --git a/src/u_mem.c b/src/u_mem.c index b1abd2d..ec01433 100644 --- a/src/u_mem.c +++ b/src/u_mem.c @@ -43,16 +43,16 @@ internstr intern_(const char *s, uint len) { static uint N, n; - static struct ht { + static struct Ht { internstr s; size_t h; } *ht; - static struct { char m[sizeof(struct arena) + (2<<10)]; struct arena *_a; } amem; - static struct arena *arena; + static struct { char m[sizeof(Arena) + (2<<10)]; Arena *_a; } amem; + static Arena *arena; if (!N) { ht = xcalloc((sizeof *ht) * (N = 1<<10)); - arena = (void *)amem.m, arena->cap = sizeof amem.m - sizeof(struct arena); + arena = (void *)amem.m, arena->cap = sizeof amem.m - sizeof(Arena); } for (size_t h = len ? hashb(0, s, len) : hashs(0, s), i = h;;) { @@ -67,7 +67,7 @@ intern_(const char *s, uint len) } /* resize */ size_t nnew = N * 2; - struct ht *new = xcalloc(sizeof *new * nnew); + struct Ht *new = xcalloc(sizeof *new * nnew); for (uint i = 0; i < N; ++i) { /* rehash */ if (!ht[i].s) continue; uint j = ht[i].h; @@ -149,10 +149,10 @@ vresize_(struct vecbase *v, uint siz, uint N) } /** arena **/ -struct arena * +Arena * newarena(uint chunksiz) { - struct arena *ar = xmalloc(offsetof(struct arena, mem) + chunksiz); + Arena *ar = xmalloc(offsetof(Arena, mem) + chunksiz); assert(chunksiz < 1u<<31 && "toobig"); ar->prev = NULL; ar->cap = chunksiz; @@ -162,10 +162,10 @@ newarena(uint chunksiz) } void * -alloc(struct arena **par, uint siz, uint align) +alloc(Arena **par, uint siz, uint align) { uint idx; - struct arena *new; + Arena *new; if (siz > (*par)->cap) { new = newarena(siz); @@ -188,15 +188,15 @@ alloc(struct arena **par, uint siz, uint align) } void * -allocz(struct arena **par, uint siz, uint align) +allocz(Arena **par, uint siz, uint align) { return memset(alloc(par, siz, align), 0, siz); } void -freearena(struct arena **par) +freearena(Arena **par) { - struct arena *prev; + Arena *prev; for (; *par; *par = prev) { prev = (*par)->prev; if ((*par)->dyn) @@ -216,12 +216,12 @@ imap_init_(struct imapbase *m, void **v, uint vsiz, uint N) { uint sizk = N*sizeof*m->k, sizv = N*vsiz, - sizbs = BSSIZE(N)*sizeof(struct bitset); + sizbs = BSSIZE(N)*sizeof(BitSet); assert(ispo2(N)); m->k = xcalloc(sizk + sizv + sizbs); *v = (char *)m->k + sizk; - m->bs = (struct bitset *)((char *)*v + sizv); + m->bs = (BitSet *)((char *)*v + sizv); m->N = N; } @@ -242,17 +242,17 @@ imap_rehash(struct imapbase *m, void **v, uint vsiz) short *newk, k; int i, j; void *newv; - struct bitset *newbs; + BitSet *newbs; uint N2 = m->N ? m->N << 1 : 16, sizk = N2*sizeof*m->k, sizv = N2*vsiz, - sizbs = BSSIZE(N2)*sizeof(struct bitset); + sizbs = BSSIZE(N2)*sizeof(BitSet); assert(N2); newk = xcalloc(sizk + sizv + sizbs); memset(newk, 0, sizk + sizv + sizbs); newv = (char *)newk + sizk; - newbs = (struct bitset *)((char *)newv + sizv); + newbs = (BitSet *)((char *)newv + sizv); for (i = 0; i < m->N; ++i) { if (!bstest(m->bs, i)) continue; |