From a8d6f8bf30c07edb775e56889f568ca20240bedf Mon Sep 17 00:00:00 2001 From: lemon Date: Tue, 17 Mar 2026 13:22:00 +0100 Subject: REFACTOR: move sources to src/ --- .gitignore | 11 +- Makefile | 16 +- aarch64/aapcs.c | 77 - aarch64/all.h | 16 - aarch64/emit.c | 1023 ----------- aarch64/isel.c | 515 ------ c/builtin.c | 177 -- c/c.c | 4772 ------------------------------------------------- c/c.h | 139 -- c/eval.c | 437 ----- c/keywords.def | 76 - c/lex.c | 2496 -------------------------- c/lex.h | 126 -- common.h | 465 ----- configure | 4 +- embedfilesdir.c | 106 -- endian.h | 189 -- io.c | 1255 ------------- ir/abi0.c | 462 ----- ir/builder.c | 307 ---- ir/cfg.c | 130 -- ir/cse.c | 92 - ir/dump.c | 319 ---- ir/fold.c | 133 -- ir/inliner.c | 309 ---- ir/intrin.c | 77 - ir/intrin.def | 2 - ir/ir.c | 689 ------- ir/ir.h | 353 ---- ir/mem2reg.c | 317 ---- ir/op.def | 79 - ir/regalloc.c | 1417 --------------- ir/simpl.c | 308 ---- ir/ssa.c | 46 - ir/stack.c | 33 - main.c | 710 -------- mem.c | 390 ---- obj/elf.c | 572 ------ obj/elf.h | 206 --- obj/obj.c | 118 -- obj/obj.h | 36 - src/a_common.h | 465 +++++ src/a_main.c | 710 ++++++++ src/a_targ.c | 121 ++ src/c.c | 4772 +++++++++++++++++++++++++++++++++++++++++++++++++ src/c.h | 139 ++ src/c_builtin.c | 177 ++ src/c_embedfilesdir.c | 106 ++ src/c_eval.c | 437 +++++ src/c_keywords.def | 76 + src/c_lex.c | 2496 ++++++++++++++++++++++++++ src/c_lex.h | 126 ++ src/c_type.c | 311 ++++ src/c_type.h | 177 ++ src/io.c | 1255 +++++++++++++ src/ir.c | 689 +++++++ src/ir.h | 353 ++++ src/ir_abi0.c | 462 +++++ src/ir_builder.c | 307 ++++ src/ir_cfg.c | 130 ++ src/ir_cse.c | 92 + src/ir_dump.c | 319 ++++ src/ir_fold.c | 133 ++ src/ir_inliner.c | 309 ++++ src/ir_intrin.c | 77 + src/ir_intrin.def | 2 + src/ir_mem2reg.c | 317 ++++ src/ir_op.def | 79 + src/ir_regalloc.c | 1417 +++++++++++++++ src/ir_simpl.c | 308 ++++ src/ir_ssa.c | 46 + src/ir_stack.c | 33 + src/o_elf.c | 572 ++++++ src/o_elf.h | 206 +++ src/obj.c | 118 ++ src/obj.h | 36 + src/t_aarch64.h | 16 + src/t_aarch64_aapcs.c | 77 + src/t_aarch64_emit.c | 1023 +++++++++++ src/t_aarch64_isel.c | 515 ++++++ src/t_x86-64.h | 18 + src/t_x86-64_emit.c | 1422 +++++++++++++++ src/t_x86-64_isel.c | 652 +++++++ src/t_x86-64_sysv.c | 310 ++++ src/u_endian.h | 189 ++ src/u_mem.c | 390 ++++ src/version.h | 13 + targ.c | 121 -- type.c | 311 ---- type.h | 177 -- version.h | 13 - x86_64/all.h | 18 - x86_64/emit.c | 1422 --------------- x86_64/isel.c | 652 ------- x86_64/sysv.c | 310 ---- 95 files changed, 22008 insertions(+), 22019 deletions(-) delete mode 100644 aarch64/aapcs.c delete mode 100644 aarch64/all.h delete mode 100644 aarch64/emit.c delete mode 100644 aarch64/isel.c delete mode 100644 c/builtin.c delete mode 100644 c/c.c delete mode 100644 c/c.h delete mode 100644 c/eval.c delete mode 100644 c/keywords.def delete mode 100644 c/lex.c delete mode 100644 c/lex.h delete mode 100644 common.h delete mode 100644 embedfilesdir.c delete mode 100644 endian.h delete mode 100644 io.c delete mode 100644 ir/abi0.c delete mode 100644 ir/builder.c delete mode 100644 ir/cfg.c delete mode 100644 ir/cse.c delete mode 100644 ir/dump.c delete mode 100644 ir/fold.c delete mode 100644 ir/inliner.c delete mode 100644 ir/intrin.c delete mode 100644 ir/intrin.def delete mode 100644 ir/ir.c delete mode 100644 ir/ir.h delete mode 100644 ir/mem2reg.c delete mode 100644 ir/op.def delete mode 100644 ir/regalloc.c delete mode 100644 ir/simpl.c delete mode 100644 ir/ssa.c delete mode 100644 ir/stack.c delete mode 100644 main.c delete mode 100644 mem.c delete mode 100644 obj/elf.c delete mode 100644 obj/elf.h delete mode 100644 obj/obj.c delete mode 100644 obj/obj.h create mode 100644 src/a_common.h create mode 100644 src/a_main.c create mode 100644 src/a_targ.c create mode 100644 src/c.c create mode 100644 src/c.h create mode 100644 src/c_builtin.c create mode 100644 src/c_embedfilesdir.c create mode 100644 src/c_eval.c create mode 100644 src/c_keywords.def create mode 100644 src/c_lex.c create mode 100644 src/c_lex.h create mode 100644 src/c_type.c create mode 100644 src/c_type.h create mode 100644 src/io.c create mode 100644 src/ir.c create mode 100644 src/ir.h create mode 100644 src/ir_abi0.c create mode 100644 src/ir_builder.c create mode 100644 src/ir_cfg.c create mode 100644 src/ir_cse.c create mode 100644 src/ir_dump.c create mode 100644 src/ir_fold.c create mode 100644 src/ir_inliner.c create mode 100644 src/ir_intrin.c create mode 100644 src/ir_intrin.def create mode 100644 src/ir_mem2reg.c create mode 100644 src/ir_op.def create mode 100644 src/ir_regalloc.c create mode 100644 src/ir_simpl.c create mode 100644 src/ir_ssa.c create mode 100644 src/ir_stack.c create mode 100644 src/o_elf.c create mode 100644 src/o_elf.h create mode 100644 src/obj.c create mode 100644 src/obj.h create mode 100644 src/t_aarch64.h create mode 100644 src/t_aarch64_aapcs.c create mode 100644 src/t_aarch64_emit.c create mode 100644 src/t_aarch64_isel.c create mode 100644 src/t_x86-64.h create mode 100644 src/t_x86-64_emit.c create mode 100644 src/t_x86-64_isel.c create mode 100644 src/t_x86-64_sysv.c create mode 100644 src/u_endian.h create mode 100644 src/u_mem.c create mode 100644 src/version.h delete mode 100644 targ.c delete mode 100644 type.c delete mode 100644 type.h delete mode 100644 version.h delete mode 100644 x86_64/all.h delete mode 100644 x86_64/emit.c delete mode 100644 x86_64/isel.c delete mode 100644 x86_64/sysv.c diff --git a/.gitignore b/.gitignore index 3a4757d..22ef4d1 100644 --- a/.gitignore +++ b/.gitignore @@ -16,14 +16,9 @@ !Makefile !bootstrap.sh -!/*.h -hostconfig.h -!/*.c -!/c/ -!/ir/ -!/x86_64/ -!/aarch64/ -!/obj/ +src/hostconfig.h +!/src/ +!/src/* !/tool/ !/test/ diff --git a/Makefile b/Makefile index 2a1f1b7..69317af 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,7 @@ OUT?=antcc BUILDDIR?=build -SRC=main.c io.c mem.c c/c.c c/lex.c c/eval.c c/builtin.c type.c targ.c \ - ir/ir.c ir/builder.c ir/fold.c ir/dump.c ir/ssa.c ir/cfg.c ir/intrin.c ir/abi0.c ir/mem2reg.c ir/regalloc.c ir/simpl.c ir/stack.c \ - ir/cse.c ir/inliner.c \ - x86_64/sysv.c x86_64/isel.c x86_64/emit.c \ - aarch64/aapcs.c aarch64/isel.c aarch64/emit.c \ - obj/obj.c obj/elf.c \ - embedfilesdir.c -OBJ=$(patsubst %.c,build/%.o,$(SRC)) +SRC=$(wildcard src/*.c) +OBJ=$(patsubst src/%.c,build/%.o,$(SRC)) DEP=$(OBJ:.o=.d) CFLAGS=-Wall -std=c11 -pedantic @@ -40,10 +34,10 @@ tool/depgen: tool/depgen.c $(OUT): tool/depgen $(OBJ) $(CC) $(CFLAGS) -o $@ $(OBJ) -hostconfig.h: +src/hostconfig.h: ./configure -$(BUILDDIR)/%.o: %.c hostconfig.h +$(BUILDDIR)/%.o: src/%.c src/hostconfig.h $Vmkdir -p `dirname $@` $Vtool/depgen -MP -MF $(BUILDDIR)/$*.d -MT $@ $< $(CC) $(CFLAGS) -c -o $@ $< @@ -55,7 +49,7 @@ clean-tool: $(RM) -r tool/depgen clean-config: clean - $(RM) -r config.mk hostconfig.h + $(RM) -r config.mk src/hostconfig.h install: all @mkdir -p "$(DESTDIR)$(BINDIR)" diff --git a/aarch64/aapcs.c b/aarch64/aapcs.c deleted file mode 100644 index fc08da1..0000000 --- a/aarch64/aapcs.c +++ /dev/null @@ -1,77 +0,0 @@ -#include "all.h" - -static int -abiarg(short r[2], uchar cls[2], uchar *r2off, int *ni, int *nf, int *ns, union irtype typ) -{ - enum { NINT = 8, NFLT = 8 }; - if (!typ.isagg) { - if (kisflt(cls[0] = typ.cls) && *nf < 8) { - r[0] = V(0) + (*nf)++; - } else if (kisint(cls[0]) && *ni < NINT) { - r[0] = R0 + (*ni)++; - } else { - r[0] = *ns; - *ns += 8; - return 0; /* MEMORY */ - } - return 1; - } else assert(!"nyi"); -} - -static int -abiret(short r[2], uchar cls[2], uchar *r2off, int *ni, union irtype typ) -{ - if (!typ.isagg) { - r[0] = kisflt(cls[0] = typ.cls) ? V(0) : R0; - return 1; - } - int nf = 0, ns = 0; - int ret = abiarg(r, cls, r2off, ni, &nf, &ns, typ); - if (ret) return ret; - /* caller-allocated result address in x8 */ - assert(*ni == 0); - r[0] = -1; - r[1] = R(8); - return 0; -} - -static void -vastart(struct function *fn, struct block *blk, int *curi) -{ - assert(!"nyi"); -} - -static void -vaarg(struct function *fn, struct block *blk, int *curi) -{ - assert(!"nyi"); -} - -static const char aarch64_rnames[][6] = { - "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9","R10","R11","R12","R13","R14","R15", - "R16","R17","R18","R19","R20","R21","R22","R23","R24","R25","R26","R27","R28", "FP", "LR", "SP", - "V0", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9","V10","V11","V12","V13","V14","V15", - "V16","V17","V18","V19","V20","V21","V22","V23","V24","V25","V26","V27","V28","V29","V30","V31", -}; - -const struct mctarg t_aarch64_aapcs = { - .gpr0 = R0, .ngpr = 31, - .bpr = FP, - .gprscratch = R(16), .fprscratch = V(31), - .fpr0 = V0, .nfpr = 32, - .rcallee = BIT(R(19)) | BIT(R(20)) | BIT(R(21)) | BIT(R(22)) | BIT(R(23)) - | BIT(R(24)) | BIT(R(25)) | BIT(R(26)) | BIT(R(27)) | BIT(R(28)) - | BIT( V(8)) | BIT( V(9)) | BIT(V(10)) | BIT(V(11)) | BIT(V(12)) - | BIT(V(13)) | BIT(V(14)) | BIT(V(15)), - .rglob = BIT(FP) | BIT(LR) | BIT(SP), - .rnames = aarch64_rnames, - .objkind = OBJELF, - .abiret = abiret, - .abiarg = abiarg, - .vastart = vastart, - .vaarg = vaarg, - .isel = aarch64_isel, - .emit = aarch64_emit, -}; - -/* vim:set ts=3 sw=3 expandtab: */ diff --git a/aarch64/all.h b/aarch64/all.h deleted file mode 100644 index 828909e..0000000 --- a/aarch64/all.h +++ /dev/null @@ -1,16 +0,0 @@ -#include "../ir/ir.h" - -enum reg { - R0 = 0, -#define R(n) (R0+n) - FP = R(29), LR = R(30), SP = R(31), - V0, -#define V(n) (V0+n) -}; - -bool aarch64_logimm(uint *enc, enum irclass, uvlong x); -void aarch64_isel(struct function *); -void aarch64_emit(struct function *); - -/* vim:set ts=3 sw=3 expandtab: */ - diff --git a/aarch64/emit.c b/aarch64/emit.c deleted file mode 100644 index 9fdcd83..0000000 --- a/aarch64/emit.c +++ /dev/null @@ -1,1023 +0,0 @@ -#include "all.h" -#include "../obj/obj.h" -#include "../endian.h" - -/* References: - * ARM ARM https://developer.arm.com/documentation/ddi0628/aa/?lang=en - * AAELF ABI https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst - */ - -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 { - uchar t; - union { - struct { /* OREG (opt. shifted) */ - uchar reg; - uchar shft : 2, /* enum shiftkind */ - shamt : 6; - }; - struct { /* OMEM */ - uchar mode : 3; /* enum addrmode */ - uchar base : 5; /* reg */ - union { - struct { - uchar index : 5; /* reg */ - uchar ext : 3; /* enum addrregext */ - uchar shamt; - }; - short disp; - }; - } m; - vlong imm; uvlong uimm; /* OIMM */ - struct { /* OSYM */ - ushort con; - int cdisp; - }; - }; -}; - -#define REGZR ((struct oper){OREGZR, .reg=31}) -#define mkoper(t, ...) ((struct 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) -{ - if (r.t == RTMP) { - assert(in_range(instrtab[r.i].reg-1, R0, SP)); - return mkoper(OMEM, .m = {AIMMIDX, .base = instrtab[r.i].reg-1}); - } else if (r.t == RREG) { - return mkoper(OMEM, .m = {AIMMIDX, .base = r.i}); - } else if (isaddrcon(r,1)) { - return mkoper(OSYM, .con = r.i,); - } else if (r.t == RADDR) { - const struct addr *addr = &addrtab.p[r.i]; - assert(addr->shift <= 3 && (!addr->disp || !addr->index.bits)); - if (isaddrcon(addr->base,0)) { - assert(!addr->index.bits); - return mkoper(OSYM, .con = addr->base.i, .cdisp = addr->disp); - } - assert(addr->base.t == RREG); - if (!addr->index.bits) { - return mkoper(OMEM, .m = {.mode = AIMMIDX, .base = addr->base.i, .disp = addr->disp}); - } else { - assert(addr->index.t == RREG); - assert(addr->shift == 0 || 1<shift == msiz); - return mkoper(OMEM, .m = { - .mode = AREGIDX, - .base = addr->base.i, - .index = addr->index.i, - .ext = XLSL, - .shamt = !!addr->shift, - }); - } - } - assert(!"nyi"); -} - -static struct oper -ref2oper(union ref r) -{ - switch (r.t) { - case RTMP: return instrtab[r.i].reg ? mkoper(ONONE,) : reg2oper(instrtab[r.i].reg-1); - case RREG: return reg2oper(r.i); - case RICON: return mkoper(OIMM, .imm = r.i); - case RXCON: - if (kisint(contab.p[r.i].cls)) - return mkoper(OIMM, .imm = contab.p[r.i].i); - else if (kisflt(contab.p[r.i].cls)) { - assert(contab.p[r.i].f == 0.0); - return mkoper(OIMM, .imm = 0); - } else if (!contab.p[r.i].cls) { - return mkoper(OSYM, .con = r.i); - } - assert(0); - case RADDR: return mkmemoper(0, r); - default: assert(0); - } -} - -enum operpat { - PNONE, - PGPRZ, /* R0-R30,ZR */ - PGPRSP, /* R0-R30,SP */ - PSP, /* SP */ - PGPRZSHFT, /* R0-30,ZR SFHT #n */ - PFPR, /* V0 - V31 */ - PZERO, /* zero immediate */ - PU6, /* 6-bit uimm */ - PU12SL12, /* 12 bit uimm, optionally left shifted by 12 */ - PU16SL16, /* 16 bit uimm, left shift by 0/16/32/48 */ - PLOGIMM, /* immediate for logical instrs */ - PMEMAIMM, /* addr 12bit immediate byte offset */ - PMEMAIMMH, /* addr 12bit immediate halfword offset (multiple of 2) */ - PMEMAIMMW, /* addr 12bit immediate word offset (multiple of 4) */ - PMEMAIMMX, /* addr 12bit immediate doubleword offset (multiple of 8) */ - PMEMPREPOST, /* addr signed 9bit immediate byte offset */ - PMEMAREG, /* addr reg offset, optionally left shifted */ - PSYM, /* symbol */ -}; -enum operenc { - EN_ADDSUBEXT3R, /* add/sub-ext-reg */ - EN_ADDSUBSHFT3R, /* add/sub-shift-reg */ - EN_LOGSHFT3R, /* logical/shifted-reg */ - EN_ARITH2R, /* data-processing/1src */ - EN_ARITH3R, /* data-processing/2src */ - EN_ADDSUBIMM, /* add/subtract-imm */ - EN_LOGIMM, /* logical-imm */ - EN_MOVEIMM, /* move/wide-imm */ - EN_MEMAIMM, /* load/store/unsigned-imm */ - EN_MEMAIMMH, /* load/store/unsigned-imm (halfword) */ - EN_MEMAIMMW, /* load/store/unsigned-imm (word) */ - EN_MEMAIMMX, /* load/store/unsigned-imm (doubleword) */ - EN_MEMAPREPOST, /* load/store/pre/postidx-imm */ - EN_MEMAREG, /* load/store/reg-offset */ - EN_MEMPPREPOST, /* load/store-pair/pre/postidx-imm */ - EN_ADRSYMLO21, /* for ADR */ - EN_ADRSYMPGHI21, /* for ADRP */ - EN_ADDSYMLO12, /* for ADD x,x, */ - EN_LDSYMLO19, /* for LDR (literal) */ - EN_FP2R, /* float 1src */ - EN_FP1GPR1, /* fpr + gpr */ - EN_FP3R, /* float 2src */ - EN_FPIMM, /* float-imm */ - EN_FPCMPZ, /* float cmp with zero */ - EN_FPCMP, /* float cmp-imm */ -}; -struct desc { - uchar psiz; /* subset of {4,8} */ - uchar pt[3]; /* bitsets of enum operpat, up to 3 operands */ - uint opc; - uchar operenc; /* enum operenc */ -}; - -/* match operand against pattern */ -static inline bool -opermatch(enum operpat pat, enum irclass k, struct oper o) -{ - switch (pat) { - case PNONE: return !o.t; - case PGPRZ: - return o.t == OREGZR || (o.t == OREG && in_range(o.reg, R0, R(30)) && !o.shamt); - case PGPRSP: - return o.t == OREG && in_range(o.reg, R0, R(31)) && !o.shamt; - case PGPRZSHFT: - return o.t == OREGZR || (o.t == OREG && in_range(o.reg, R0, R(30))); - case PSP: return o.t == OREG && o.reg == SP; - case PFPR: return o.t == OREG && in_range(o.reg, V0, V(31)); - case PZERO: return o.t == OIMM && o.imm == 0; - case PU6: return o.t == OIMM && (uint)o.imm < 63; - case PSYM: return o.t == OSYM; - case PU12SL12: - return o.t == OIMM && ((o.imm &~ 0xFFF) == 0 || (o.imm &~ 0xFFF000) == 0); - case PU16SL16: - return o.t == OIMM - && ((o.imm &~ 0xFFFF) == 0 || (o.imm &~ 0xFFFF0000) == 0 - || (o.imm &~ (0xFFFFull<<32)) == 0 || (o.imm &~ (0xFFFFull<<48)) == 0); - case PLOGIMM: return o.t == OIMM && aarch64_logimm(NULL, k, o.imm); - case PMEMAIMM: - return o.t == OMEM && o.m.mode == AIMMIDX && (uint)o.m.disp < (1<<12); - case PMEMAIMMH: - return o.t == OMEM && o.m.mode == AIMMIDX && (uint)o.m.disp < (1<<13) && !(o.m.disp % 2); - case PMEMAIMMW: - return o.t == OMEM && o.m.mode == AIMMIDX && (uint)o.m.disp < (1<<14) && !(o.m.disp % 4); - case PMEMAIMMX: - return o.t == OMEM && o.m.mode == AIMMIDX && (uint)o.m.disp < (1<<15) && !(o.m.disp % 8); - case PMEMAREG: - return o.t == OMEM && o.m.mode == AREGIDX; - case PMEMPREPOST: - return o.t == OMEM && (o.m.mode == APREIDX || o.m.mode == APOSTIDX - || (o.m.mode == AIMMIDX && o.m.disp >= -256 && o.m.disp < 256)); - } - assert(0); -} - -/* code output helpers */ -#define W32(w) (wr32targ(*pcode, (w)), *pcode += 4) - -static uchar *fnstart; -static internstr curfnsym; -static bool usefp; -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]) -{ - const struct desc *en = NULL; - for (int i = 0; i < ntab; ++i) { - if (!(tab[i].psiz & cls2siz[k])) continue; - for (int j = 0; j < 3; ++j) - if (!opermatch(tab[i].pt[j], k, o[j])) - goto Skip; - en = &tab[i]; - break; - Skip:; - } - assert(en && "no match for instr"); - - uint sf = cls2siz[k] >> 3; - uint ins = en->opc, sh, nimmrs; - switch (en->operenc) { - default: assert(!"nyi enc"); - case EN_ADDSUBSHFT3R: case EN_LOGSHFT3R: - ins |= sf<<31 | o[2].shft<<22 | o[2].reg<<16 | o[2].shamt<<10 | o[1].reg<<5 | o[0].reg; - break; - case EN_ARITH3R: - ins |= sf<<31 | o[2].reg<<16 | o[1].reg<<5 | o[0].reg; - break; - case EN_ADDSUBIMM: - sh = o[2].imm > 0xFFF; - ins |= sf<<31 | sh<<22 | (o[2].uimm >> 12*sh)<<10 | o[1].reg<<5 | o[0].reg; - break; - case EN_LOGIMM: - assert(aarch64_logimm(&nimmrs, k, o[2].uimm)); - ins |= sf<<31 | nimmrs<<10 | o[1].reg<<5 | o[0].reg; - break; - case EN_MOVEIMM: - sh = o[1].imm ? lowestsetbit(o[1].imm) / 16 : 0; - ins |= sf<<31 | sh<<21 | (o[1].uimm >> 16*sh)<<5 | o[0].reg; - break; - case EN_MEMAIMM: AImm: - ins |= o[1].m.disp<<10 | o[1].m.base<<5 | (o[0].reg&31); - break; - case EN_MEMAIMMH: o[1].m.disp >>= 1; goto AImm; - case EN_MEMAIMMW: o[1].m.disp >>= 2; goto AImm; - case EN_MEMAIMMX: o[1].m.disp >>= 3; goto AImm; - case EN_MEMAPREPOST: - ins |= (o[1].m.disp&0x1FF)<<12 | o[1].m.base<<5 | (o[0].reg&31); - if (o[1].m.mode == APREIDX) ins |= 3<<10; - else if (o[1].m.mode == APOSTIDX) ins |= 1<<10; - break; - case EN_MEMAREG: - assert(o[1].m.shamt <= 1); - ins |= o[1].m.index<<16 | o[1].m.ext<<13 | o[1].m.shamt<<12 | o[1].m.base<<5 | (o[0].reg&31); - break; - case EN_MEMPPREPOST: - assert(o[2].m.disp % 8 == 0); - ins |= (o[2].m.disp/8&0x7F)<<15 | (o[1].reg&31)<<10 | o[2].m.base<<5 | (o[0].reg&31); - if (o[2].m.mode == APREIDX) ins |= 3<<23; - else if (o[2].m.mode == APOSTIDX) ins |= 1<<23; - else ins |= 2<<23; - break; - case EN_ADRSYMLO21: - ins |= o[0].reg; - objreloc(xcon2sym(o[1].con), REL_ADR_PREL_LO21, Stext, *pcode - objout.textbegin, o[1].cdisp); - break; - case EN_ADRSYMPGHI21: - ins |= o[0].reg; - objreloc(xcon2sym(o[1].con), REL_ADR_PREL_PG_HI21, Stext, *pcode - objout.textbegin, o[1].cdisp); - break; - case EN_ADDSYMLO12: - ins |= sf<<31 | o[1].reg<<5 | o[0].reg; - objreloc(xcon2sym(o[2].con), REL_ADD_ABS_LO12_NC, Stext, *pcode - objout.textbegin, o[1].cdisp); - break; - case EN_LDSYMLO19: - ins |= o[0].reg; - objreloc(xcon2sym(o[1].con), REL_LD_PREL_LO19, Stext, *pcode - objout.textbegin, o[1].cdisp); - break; - case EN_FP2R: - ins |= sf<<22 | (o[1].reg&31)<<5 | (o[0].reg&31); - break; - case EN_FP1GPR1: - ins |= (o[1].reg&31)<<5 | (o[0].reg&31); - break; - case EN_FP3R: - ins |= sf<<22 | (o[2].reg&31)<<16 | (o[1].reg&31)<<5 | (o[0].reg&31); - break; - case EN_FPCMPZ: - ins |= sf<<22 | (o[0].reg&31)<<5; - break; - case EN_FPCMP: - ins |= sf<<22 | (o[1].reg&31)<<16 | (o[0].reg&31)<<5; - break; - } - 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 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 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})); \ - } - -DEFINSTR2(Xadrp, - {8, {PGPRZ, PSYM}, 0x90000000, EN_ADRSYMPGHI21} /* ADR (sym pg hi21) */ -) -DEFINSTR2(Xadr, - {8, {PGPRZ, PSYM}, 0x10000000, EN_ADRSYMLO21} /* ADR (sym pg hi21) */ -) - -DEFINSTR3(Xadd, - {4|8, {PGPRSP, PGPRSP, PU12SL12}, 0x11000000, EN_ADDSUBIMM}, /* ADD (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x0B000000, EN_ADDSUBSHFT3R}, /* ADD (shifted register) */ - { 8, {PGPRZ, PGPRZ, PSYM}, 0x11000000, EN_ADDSYMLO12}, /* ADD (sym lo12) */ -) -DEFINSTR3(Xsub, - {4|8, {PGPRSP, PGPRSP, PU12SL12}, 0x51000000, EN_ADDSUBIMM}, /* SUB (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x4B000000, EN_ADDSUBSHFT3R}, /* SUB (shifted register) */ -) -DEFINSTR3(Xsubs, - {4|8, {PGPRZ, PGPRSP, PU12SL12}, 0x71000000, EN_ADDSUBIMM}, /* SUBS (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x6B000000, EN_ADDSUBSHFT3R}, /* SUBS (shifted register) */ -) - -static void -Xmadd(uchar **pcode, enum irclass k, struct oper d, struct oper n, struct oper m, struct oper a) -{ - assert(opermatch(PGPRZ, k, d) && opermatch(PGPRZ, k, n) - && opermatch(PGPRZ, k, a) && opermatch(PGPRZ, k, m)); - uint sf = k > KI32; - W32(0x1B000000 | sf<<31 | m.reg<<16 | a.reg<<10 | n.reg<<5 | d.reg); -} - -DEFINSTR3(Xsdiv, {4|8, {PGPRZ, PGPRZ, PGPRZ}, 0x1AC00C00, EN_ARITH3R}) -DEFINSTR3(Xudiv, {4|8, {PGPRZ, PGPRZ, PGPRZ}, 0x1AC00800, EN_ARITH3R}) - -DEFINSTR3(Xand, - {4|8, {PGPRSP, PGPRZ, PLOGIMM}, 0x12000000, EN_LOGIMM}, /* AND (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x0A000000, EN_LOGSHFT3R}, /* AND (shifted register) */ -) -DEFINSTR3(Xorr, - {4|8, {PGPRSP, PGPRZ, PLOGIMM}, 0x32000000, EN_LOGIMM}, /* ORR (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x2A000000, EN_LOGSHFT3R}, /* ORR (shifted register) */ -) -DEFINSTR3(Xorn, {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x2A200000, EN_LOGSHFT3R}) -DEFINSTR3(Xeor, - {4|8, {PGPRSP, PGPRZ, PLOGIMM}, 0x52000000, EN_LOGIMM}, /* EOR (immediate) */ - {4|8, {PGPRZ, PGPRZ, PGPRZSHFT}, 0x4A000000, EN_LOGSHFT3R}, /* EOR (shifted register) */ -) -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) -{ - uint x = k != KI32; - uint nbit = x ? 64 : 32; - assert(opermatch(PGPRZ, k, rd) && opermatch(PGPRZ, k, rn) && immr < nbit && imms < nbit); - 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) -{ - uint x = k != KI32; - uint nbit = x ? 64 : 32; - assert(opermatch(PGPRZ, k, rd) && opermatch(PGPRZ, k, rn) && immr < nbit && imms < nbit); - W32(x<<31 | 0x13000000 | x<<22 | immr<<16 | imms<<10 | rn.reg<<5 | rd.reg); -} - -DEFINSTR2(Xmovz, {4|8, {PGPRZ, PU16SL16}, 0x52800000, EN_MOVEIMM}, /* MOVZ */) -DEFINSTR2(Xmovn, {4|8, {PGPRZ, PU16SL16}, 0x12800000, EN_MOVEIMM}, /* MOVN */) -DEFINSTR2(Xmovk, {4|8, {PGPRZ, PU16SL16}, 0x72800000, EN_MOVEIMM}, /* MOVK */) -DEFINSTR2(Xldr, - {4, {PGPRZ, PMEMAIMMW}, 0xB9400000, EN_MEMAIMMW}, /* LDR (immediate) */ - {8, {PGPRZ, PMEMAIMMX}, 0xF9400000, EN_MEMAIMMX}, - {4, {PGPRZ, PMEMAREG}, 0xB8600800, EN_MEMAREG}, /* LDR (register) */ - {8, {PGPRZ, PMEMAREG}, 0xF8600800, EN_MEMAREG}, - {4, {PGPRZ, PSYM}, 0x18000000, EN_LDSYMLO19}, /* LDR (literal) */ - {8, {PGPRZ, PSYM}, 0x58000000, EN_LDSYMLO19}, - {4, {PGPRZ, PMEMPREPOST}, 0xB8400000, EN_MEMAPREPOST}, /* LDR (immediate, (pre/postinc)) */ - {8, {PGPRZ, PMEMPREPOST}, 0xF8400000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xfldr, - {4, {PFPR, PMEMAIMMW}, 0xBD400000, EN_MEMAIMMW}, /* LDR (immediate) */ - {8, {PFPR, PMEMAIMMX}, 0xFD400000, EN_MEMAIMMX}, - {4, {PFPR, PMEMAREG}, 0xBC600800, EN_MEMAREG}, /* LDR (register) */ - {8, {PFPR, PMEMAREG}, 0xFC600800, EN_MEMAREG}, - {4, {PFPR, PMEMPREPOST}, 0xBC400000, EN_MEMAPREPOST}, /* LDR (immediate, (pre/postinc)) */ - {8, {PFPR, PMEMPREPOST}, 0xFC400000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xldrsw, - {8, {PGPRZ, PMEMAIMMW}, 0xB9800000, EN_MEMAIMMW}, /* LDRSW (immediate) */ -// {8, {PGPRZ, PMEMAREG}, 0xB8A00800, EN_MEMAREG}, /* LDRSW (register) */ - {8, {PGPRZ, PMEMPREPOST}, 0xB8800000, EN_MEMAPREPOST}, /* LDRSW (immediate, (pre/postinc)) */ -) -DEFINSTR2(Xldrh, - {4|8, {PGPRZ, PMEMAIMMH}, 0x79400000, EN_MEMAIMMH}, /* LDRH (immediate) */ - {4|8, {PGPRZ, PMEMAREG}, 0x78600800, EN_MEMAREG}, /* LDRH (register) */ - {4|8, {PGPRZ, PMEMPREPOST}, 0x78400000, EN_MEMAPREPOST}, /* LDRH (immediate, (pre/postinc)) */ -) -DEFINSTR2(Xldrsh, - {4, {PGPRZ, PMEMAIMMH}, 0x79C00000, EN_MEMAIMMH}, /* LDRSH (immediate) */ - {8, {PGPRZ, PMEMAIMMH}, 0x79800000, EN_MEMAIMMH}, - {4, {PGPRZ, PMEMAREG}, 0x78E00800, EN_MEMAREG}, /* LDRSH (register) */ - {8, {PGPRZ, PMEMAREG}, 0x78A00800, EN_MEMAREG}, - {4, {PGPRZ, PMEMPREPOST}, 0x78C00000, EN_MEMAPREPOST}, /* LDRSH (immediate, (pre/postinc)) */ - {8, {PGPRZ, PMEMPREPOST}, 0x78800000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xldrb, - {4|8, {PGPRZ, PMEMAIMM}, 0x39400000, EN_MEMAIMM}, /* LDRB (immediate) */ - {4|8, {PGPRZ, PMEMAREG}, 0x38600800, EN_MEMAREG}, /* LDRB (register) */ - {4|8, {PGPRZ, PMEMPREPOST}, 0x38400000, EN_MEMAPREPOST}, /* LDRB (immediate, (pre/postinc)) */ -) -DEFINSTR2(Xldrsb, - {4, {PGPRZ, PMEMAIMM}, 0x39C00000, EN_MEMAIMM}, /* LDRSB (immediate) */ - {8, {PGPRZ, PMEMAIMM}, 0x39800000, EN_MEMAIMM}, - {4, {PGPRZ, PMEMAREG}, 0x38E00800, EN_MEMAREG}, /* LDRSB (register) */ - {8, {PGPRZ, PMEMAREG}, 0x38A00800, EN_MEMAREG}, - {4, {PGPRZ, PMEMPREPOST}, 0x38C00000, EN_MEMAPREPOST}, /* LDRSB (immediate, (pre/postinc)) */ - {8, {PGPRZ, PMEMPREPOST}, 0x38800000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xstr, - {4, {PGPRZ, PMEMAIMMW}, 0xB9000000, EN_MEMAIMMW}, /* STR (immediate) */ - {8, {PGPRZ, PMEMAIMMX}, 0xF9000000, EN_MEMAIMMX}, - {4, {PGPRZ, PMEMAREG}, 0xB8200800, EN_MEMAREG}, /* STR (register) */ - {8, {PGPRZ, PMEMAREG}, 0xF8200800, EN_MEMAREG}, - {4, {PGPRZ, PMEMPREPOST}, 0xB8000000, EN_MEMAPREPOST}, /* STR (immediate, (pre/postinc)) */ - {8, {PGPRZ, PMEMPREPOST}, 0xF8000000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xfstr, - {4, {PFPR, PMEMAIMMW}, 0xBD000000, EN_MEMAIMMW}, /* LDR (immediate) */ - {8, {PFPR, PMEMAIMMX}, 0xFD000000, EN_MEMAIMMX}, - {4, {PFPR, PMEMAREG}, 0xBC200800, EN_MEMAREG}, /* LDR (register) */ - {8, {PFPR, PMEMAREG}, 0xFC200800, EN_MEMAREG}, - {4, {PFPR, PMEMPREPOST}, 0xBC000000, EN_MEMAPREPOST}, /* LDR (immediate, (pre/postinc)) */ - {8, {PFPR, PMEMPREPOST}, 0xFC000000, EN_MEMAPREPOST}, -) -DEFINSTR2(Xstrh, - {4|8, {PGPRZ, PMEMAIMMH}, 0x79000000, EN_MEMAIMMH}, /* STRH (immediate) */ - {4|8, {PGPRZ, PMEMAREG}, 0x78200800, EN_MEMAREG}, /* STRH (register) */ - {4|8, {PGPRZ, PMEMPREPOST}, 0x78000000, EN_MEMAPREPOST}, /* STRH (immediate, (pre/postinc)) */ -) -DEFINSTR2(Xstrb, - {4|8, {PGPRZ, PMEMAIMM}, 0x39000000, EN_MEMAIMM}, /* STRB (immediate) */ - {4|8, {PGPRZ, PMEMAREG}, 0x38200800, EN_MEMAREG}, /* STRB (register) */ - {4|8, {PGPRZ, PMEMPREPOST}, 0x38000000, EN_MEMAPREPOST}, /* STRB (immediate, (pre/postinc)) */ -) -DEFINSTR3(Xldp, - {8, {PGPRZ, PGPRZ, PMEMPREPOST}, 0xA8400000, EN_MEMPPREPOST} /* LDP (immediate, (pre/postinc)) */ -) -DEFINSTR3(Xstp, - {8, {PGPRZ, PGPRZ, PMEMPREPOST}, 0xA8000000, EN_MEMPPREPOST} /* STP (immediate, (pre/postinc)) */ -) -DEFINSTR3(Xfldp, - {8, {PFPR, PFPR, PMEMPREPOST}, 0x6CC00000, EN_MEMPPREPOST} /* LDP (immediate, (pre/postinc)) */ -) -DEFINSTR3(Xfstp, - {8, {PFPR, PFPR, PMEMPREPOST}, 0x6C800000, EN_MEMPPREPOST} /* STP (immediate, (pre/postinc)) */ -) -static void -Xcall(uchar **pcode, struct oper dst) -{ - if (dst.t == OSYM) { - objreloc(xcon2sym(dst.con), REL_CALL26, Stext, *pcode - objout.textbegin, 0); - W32(0x94000000); /* BL */ - } else { - assert(opermatch(PGPRZ, KPTR, dst)); - W32(0xD63F0000 | dst.reg<<5); /* BLR Xn */ - } -} -DEFINSTR2(Xfmov, - {4|8, {PFPR, PFPR}, 0x1E204000, EN_FP2R}, - {4, {PFPR, PGPRZ}, 0x1E270000, EN_FP1GPR1}, - { 8, {PFPR, PGPRZ}, 0x9E670000, EN_FP1GPR1}, - {4, {PGPRZ, PFPR}, 0x1E260000, EN_FP1GPR1}, - { 8, {PGPRZ, PFPR}, 0x9E660000, EN_FP1GPR1}, -) -DEFINSTR2(Xfneg, {4|8, {PFPR, PFPR}, 0x1E214000, EN_FP2R}) -DEFINSTR2(Xscvtfw, {4|8, {PFPR, PGPRZ}, 0x1E220000, EN_FP2R}) -DEFINSTR2(Xscvtfx, {4|8, {PFPR, PGPRZ}, 0x9E220000, EN_FP2R}) -DEFINSTR2(Xfcvtzsw, {4|8, {PGPRZ, PFPR}, 0x1E380000, EN_FP2R}) -DEFINSTR2(Xfcvtzsx, {4|8, {PGPRZ, PFPR}, 0x9E380000, EN_FP2R}) -DEFINSTR2(Xucvtfw, {4|8, {PFPR, PGPRZ}, 0x1E230000, EN_FP2R}) -DEFINSTR2(Xucvtfx, {4|8, {PFPR, PGPRZ}, 0x9E230000, EN_FP2R}) -DEFINSTR2(Xfcvtzuw, {4|8, {PGPRZ, PFPR}, 0x1E390000, EN_FP2R}) -DEFINSTR2(Xfcvtzux, {4|8, {PGPRZ, PFPR}, 0x9E390000, EN_FP2R}) -DEFINSTR2(Xfcvtds, {4, {PFPR, PFPR}, 0x1E624000, EN_FP2R}) -DEFINSTR2(Xfcvtsd, {4, {PFPR, PFPR}, 0x1E22C000, EN_FP2R}) -DEFINSTR3(Xfadd, {4|8, {PFPR, PFPR, PFPR}, 0x1E202800, EN_FP3R}) -DEFINSTR3(Xfsub, {4|8, {PFPR, PFPR, PFPR}, 0x1E203800, EN_FP3R}) -DEFINSTR3(Xfmul, {4|8, {PFPR, PFPR, PFPR}, 0x1E200800, EN_FP3R}) -DEFINSTR3(Xfdiv, {4|8, {PFPR, PFPR, PFPR}, 0x1E201800, EN_FP3R}) -DEFINSTR2(Xfcmp, - {4|8, {PFPR, PZERO}, 0x1E602008, EN_FPCMPZ}, - {4|8, {PFPR, PFPR}, 0x1E602000, EN_FPCMP}, -) - -static void -gencopy(uchar **pcode, enum irclass cls, struct block *blk, int curi, struct oper dst, union ref val) -{ - assert(dst.t == OREG); - struct oper src; - if (val.bits == UNDREF.bits) return; - if (isintcon(val)) { - assert(dst.reg <= R(31)); - /* MOV r, #imm */ - uvlong u = intconval(val); - if (~u <= 0xFFFF) { - /* immediate can be encoded with 1 MOVN instruction */ - Xmovn(pcode, cls, dst, mkoper(OIMM, .imm = ~u)); - } else { - /* generate MOV (+ MOVKs) */ - if (cls == KI32) u = (uint)u; - int s = 0; - while (s < 48 && (u >> s & 0xFFFF) == 0) s += 16; - if ((u &~ (0xFFFFull << s)) != 0 && aarch64_logimm(NULL, cls, u)) { - /* can be encoded as a logical immediate in 1 instr */ - Xorr(pcode, cls, dst, REGZR, mkoper(OIMM, .uimm = u)); - } else { - Xmovz(pcode, cls, dst, mkoper(OIMM, .imm = u & (0xFFFFull << s))); - for (s += 16; s <= 48; s += 16) { - if ((u >> s) & 0xFFFF) - Xmovk(pcode, cls, dst, mkoper(OIMM, .imm = u & (0xFFFFull << s))); - } - } - } - } else if (opermatch(PGPRZ, cls, (src = ref2oper(val))) && kisint(cls)) { - Xorr(pcode, cls, dst, REGZR, src); /* MOV Rd, Rn ==> ORR Rd, zr, Rn */ - } else if (kisflt(cls) || opermatch(PFPR, 0, src)) { - if (src.t == OREG) - Xfmov(pcode, cls, dst, src); - else if (src.t == OIMM && src.imm == 0) - Xfmov(pcode, cls, dst, REGZR); - else assert(0); - } else if (isaddrcon(val,0) || (val.t == RADDR && isaddrcon(addrtab.p[val.i].base,0))) { - if ((ccopt.pic || (contab.p[val.i].flag & SFUNC)) && !(contab.p[val.i].flag & SLOCAL)) { - Xadrp(pcode, KPTR, dst, src); - Xadd(pcode, KPTR, dst, dst, src); - } else { - Xadr(pcode, KPTR, dst, src); - } - } else assert(0); -} - -/* maps blk -> address when resolved; or to linked list of jump displacement - * relocations */ -static struct blkaddr { - bool resolved; - union { - uint addr; - uint relreloc; - }; -} *blkaddr; - -enum cc { - CCEQ, CCNE, CCCS, CCCC, CCMI, CCPL, CCVS, CCVC, - CCHI, CCLS, CCGE, CCLT, CCGT, CCLE, CCAL, CCNV, - CCHS = CCCS, CCLO = CCCC, -}; - -static void -Xbcc(uchar **pcode, enum cc cc, struct block *dst) -{ - int disp, insaddr = *pcode - objout.textbegin; - - if (blkaddr[dst->id].resolved) { - disp = (int)(blkaddr[dst->id].addr - insaddr)/4; - assert(disp >= -(1<<18) && disp < (1<<18)); - } else { - disp = blkaddr[dst->id].relreloc; - blkaddr[dst->id].relreloc = insaddr; - } - assert(in_range(cc, 0, 0xF)); - W32(0x54000000 | (disp & 0x7FFFF)<<5 | cc); -} - -static void -Xcbcc(uchar **pcode, enum irclass k, uint rt, enum cc cc, struct block *dst) -{ - int disp, insaddr = *pcode - objout.textbegin; - if (blkaddr[dst->id].resolved) { - disp = (int)(blkaddr[dst->id].addr - insaddr)/4; - assert(disp >= -(1<<18) && disp < (1<<18)); - } else { - disp = blkaddr[dst->id].relreloc; - blkaddr[dst->id].relreloc = insaddr; - } - assert(in_range(cc, CCEQ, CCNE)); - assert(in_range(rt, 0, 31)); - W32(0x34000000 | (uint)(k > KI32)<<31 | cc<<24 | (disp & 0x7FFFF)<<5 | rt); -} - -/* condition code for CMP */ -static const schar icmpop2cc[] = { - [Oequ] = CCEQ, [Oneq] = CCNE, - [Olth] = CCLT, [Ogth] = CCGT, [Olte] = CCLE, [Ogte] = CCGE, - [Oulth] = CCLO, [Ougth] = CCHI, [Oulte] = CCLS, [Ougte] = CCHS, -}, fcmpop2cc[] = { - [Oequ] = CCEQ, [Oneq] = CCNE, - [Olth] = CCLO, [Ogth] = CCGT, [Olte] = CCLS, [Ogte] = CCGE, -}; - -static void -emitbranch(uchar **pcode, struct block *blk) -{ - enum irclass cbk = 0; - struct oper cbopr; - enum cc cc = CCAL; - assert(blk->s1); - if (blk->s2) { - /* conditional branch.. */ - union ref arg = blk->jmp.arg[0]; - assert(arg.t == RTMP); - struct 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; - cbopr = ref2oper(ins->l); - assert(opermatch(PGPRZ, ins->cls, cbopr)); - } else if (oiscmp(ins->op)) { - /* for CMP instr */ - cc = (kisint(ins->cls) ? icmpop2cc : fcmpop2cc)[ins->op]; - } else { - /* implicit by ZF */ - cc = CCNE; - } - 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; - blk->s1 = blk->s2; - blk->s2 = tmp; - cc ^= 1; - } - } - /* make sure to fallthru if jumping to next adjacent block */ - if (blk->s2 || blk->s1 != blk->lnext) { - if (cbk) Xcbcc(pcode, cbk, cbopr.reg, cc, blk->s1); - else Xbcc(pcode, cc, blk->s1); - } - if (blk->s2 && blk->s2 != blk->lnext) - Xbcc(pcode, CCAL, blk->s2); -} - -static struct instr *lastcmp; - -static void -emitinstr(uchar **pcode, struct function *fn, struct block *blk, int curi, struct instr *ins) -{ - struct 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; - - switch (ins->op) { - default: fatal(NULL, "aarch64 unimplemented instr: %s", opnames[ins->op]); - case Onop: break; - case Omove: - dst = ref2oper(ins->l); - gencopy(pcode, cls, blk, curi, dst, ins->r); - break; - case Oextu32: cls = KI32; - /* fallthru */ - case Ocopy: - dst = reg2oper(ins->reg-1); - gencopy(pcode, cls, blk, curi, dst, ins->l); - break; - case Oswap: - o1 = ref2oper(ins->l), o2 = ref2oper(ins->r); - if (kisflt(ins->cls) && ins->l.i != mctarg->fprscratch && ins->r.i != mctarg->fprscratch) { - dst = reg2oper(mctarg->fprscratch); - Xfmov(pcode, cls, dst, o1); - Xfmov(pcode, cls, o1, o2); - Xfmov(pcode, cls, o2, dst); - } else if (ins->l.i != mctarg->gprscratch && ins->r.i != mctarg->gprscratch) { - dst = reg2oper(mctarg->gprscratch); - Xorr(pcode, cls, dst, REGZR, o1); - Xorr(pcode, cls, o1, REGZR, o2); - Xorr(pcode, cls, o2, REGZR, dst); - } else { - Xeor(pcode, cls, o1, o1, o2); - Xeor(pcode, cls, o2, o1, o2); - Xeor(pcode, cls, o1, o1, o2); - } - break; - case Onot: /* MVN Rd, Rn ==> ORN Rd, zr, Rn */ - Xorn(pcode, cls, reg2oper(ins->reg-1), REGZR, ref2oper(ins->l)); - break; - case Oneg: - if (kisint(ins->cls)) /* NEG Rd, Rn ==> SUB Rd, zr, Rn */ - Xsub(pcode, cls, reg2oper(ins->reg-1), REGZR, ref2oper(ins->l)); - else - Xfneg(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l)); - break; - case Oexts8: case Oexts16: case Oexts32: /* SXTB/H/W Rd, Rn ==> SBFM Rd, Rn, #0, #7/15/31 */ - Xsbfm(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), 0, (8<<(ins->op-Oexts8)/2)-1); - break; - case Oextu8: case Oextu16: /* UXTB/H Rd, Rn ==> UBFM Rd, Rn, #0, #7/15 */ - Xubfm(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), 0, (8<<(ins->op-Oexts8)/2)-1); - break; - case Ocvts32f: X2 = Xscvtfw; goto Cvt; - case Ocvts64f: X2 = Xscvtfx; goto Cvt; - case Ocvtf32s: - X2 = cls == KI32 ? Xfcvtzsw : Xfcvtzsx; - cls = KF32; - goto Cvt; - case Ocvtf64s: - X2 = cls == KI32 ? Xfcvtzsw : Xfcvtzsx; - cls = KF64; - goto Cvt; - case Ocvtu32f: X2 = Xucvtfw; goto Cvt; - case Ocvtu64f: X2 = Xucvtfx; goto Cvt; - case Ocvtf32u: - X2 = cls == KI32 ? Xfcvtzuw : Xfcvtzux; - cls = KF32; - goto Cvt; - case Ocvtf64u: - X2 = cls == KI32 ? Xfcvtzuw : Xfcvtzux; - cls = KF64; - goto Cvt; - case Ocvtf32f64: cls = KF32; X2 = Xfcvtsd; goto Cvt; - case Ocvtf64f32: cls = KF32; X2 = Xfcvtds; goto Cvt; - Cvt: - X2(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l)); - break; - case Oadd: X3 = kisint(cls) ? Xadd : Xfadd; goto ALU3; - case Osub: X3 = kisint(cls) ? Xsub : Xfsub; goto ALU3; - case Omul: if (kisflt(cls)) { X3 = Xfmul; goto ALU3; } - /* MUL Rd,Rn,Rm ==> MADD Rd,Rn,Rm,zr */ - Xmadd(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), ref2oper(ins->r), REGZR); - break; - case Odiv: X3 = kisint(cls) ? Xsdiv : Xfdiv; goto ALU3; - case Oudiv: X3 = Xudiv; goto ALU3; - case Oand: X3 = Xand; goto ALU3; - case Oior: X3 = Xorr; goto ALU3; - case Oxor: X3 = Xeor; goto ALU3; - ALU3: - X3(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), ref2oper(ins->r)); - break; - case Oshl: - if (ins->r.t == RICON) { - uint nbit = cls == KI32 ? 32 : 64, s = ins->r.i & (nbit-1); - assert(s > 0); - Xubfm(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), nbit-s, nbit-s-1); - } else { - X3 = Xlslv; - goto ALU3; - } - break; - case Oslr: - if (ins->r.t == RICON) { - uint nbit = cls == KI32 ? 32 : 64, s = ins->r.i & (nbit-1); - assert(s > 0); - Xubfm(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), s, nbit-1); - } else { - X3 = Xlsrv; - goto ALU3; - } - break; - case Osar: - if (ins->r.t == RICON) { - uint nbit = cls == KI32 ? 32 : 64, s = ins->r.i & (nbit-1); - assert(s > 0); - Xsbfm(pcode, cls, reg2oper(ins->reg-1), ref2oper(ins->l), s, nbit-1); - } else { - X3 = Xasrv; - goto ALU3; - } - break; - case Oequ: case Oneq: - if (!ins->reg && kisint(cls) && ins->r.bits == ZEROREF.bits) /* handled by emitbranch for CBZ/CBNZ */ - break; - case Olth: case Ogth: case Olte: case Ogte: - case Oulth: case Ougth: case Oulte: case Ougte: - if (lastcmp && lastcmp->cls == cls - && lastcmp->l.bits == ins->l.bits && lastcmp->r.bits == ins->r.bits) - /* reuse flags from previous identical cmp */ ; - else if (kisflt(cls)) - Xfcmp(pcode, cls, ref2oper(ins->l), ref2oper(ins->r)); - else /* CMP ... ==> SUBS zr, ... */ - Xsubs(pcode, cls, REGZR, ref2oper(ins->l), ref2oper(ins->r)); - lastcmp = ins; - if (ins->reg) { - enum cc cc = (kisflt(cls) ? fcmpop2cc : icmpop2cc)[ins->op]; - dst = reg2oper(ins->reg-1); - assert(dst.reg < R(31)); - W32(0x1A9F07E0 | (cc^1)<<12 | dst.reg); /* CSET Wd, */ - } - break; - case Oloadu8: X2 = Xldrb; goto Load; - case Oloads8: X2 = Xldrsb; goto Load; - case Oloadu16: X2 = Xldrh; goto Load; - case Oloads16: X2 = Xldrsh; goto Load; - case Oloads32: - if (cls != KI32) { - X2 = Xldrsw; - goto Load; - } - case Oloadu32: - cls = KI32; - /* fallthru */ - case Oloadi64: X2 = Xldr; - Load: - X2(pcode, cls, reg2oper(ins->reg-1), mkmemoper(1<<(ins->op - Oloads8)/2, ins->l)); - break; - case Oloadf32: case Oloadf64: - Xfldr(pcode, cls, reg2oper(ins->reg-1), mkmemoper(ins->op == Oloadf32 ? 4 : 8, ins->l)); - break; - case Ostorei8: cls = KI32; X2 = Xstrb; goto Store; - case Ostorei16: cls = KI32; X2 = Xstrh; goto Store; - case Ostorei32: cls = KI32; X2 = Xstr; goto Store; - case Ostorei64: cls = KI64; X2 = Xstr; - Store: - X2(pcode, cls, ins->r.bits == ZEROREF.bits ? REGZR : ref2oper(ins->r), - mkmemoper(1<<(ins->op-Ostorei8), ins->l)); - break; - case Ostoref32: case Ostoref64: - Xfstr(pcode, KF32 + ins->op-Ostoref32, ref2oper(ins->r), mkmemoper(ins->op == Oloadf32 ? 4 : 8, ins->l)); - break; - case Ocall: - Xcall(pcode, ref2oper(ins->l)); - break; - } -} - -struct frame { - regset save; - struct rpair { uchar a,b; } pairs[10]; - uchar single[2]; - uint nfpairs, ngpairs; -}; - -static void -prologue(uchar **pcode, struct frame *frame, struct function *fn) -{ - *frame = (struct 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; - for (uint reg = V(8); reg <= V(15); ++reg) { - if (!rstest(save, reg)) continue; - if (prev) { - *p++ = (struct rpair) {prev, reg}; - ++frame->nfpairs; - prev = 0; - } else prev = reg; - } - uint ngpr = popcnt(save & (BIT(32)-1)); - if (prev) { - if (ngpr & 1) { - frame->single[0] = prev; - frame->single[1] = prev = lowestsetbit(save); - rsclr(&save, prev); - } else { - *p++ = (struct rpair) {prev, V(0)}; - ++frame->nfpairs; - } - prev = 0; - } else if (ngpr & 1) { - prev = 0x100; - } - for (uint reg = R(19); reg <= LR; ++reg) { - if (!rstest(save, reg)) continue; - if (prev) { - *p++ = (struct rpair) {prev, reg}; - ++frame->ngpairs; - prev = 0; - } else prev = reg; - } - assert(!prev); - - p = frame->pairs; - struct 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; - if (frame->single[0]) Xfstr(pcode, KF64, reg2oper(frame->single[0]), adr); - if (frame->single[1]) Xstr(pcode, KPTR, reg2oper(frame->single[1]), adr); - adr.m.disp = -16; - for (int i = 0; i < frame->ngpairs; ++i, ++p) - Xstp(pcode, KPTR, reg2oper(p->a), reg2oper(p->b), adr); - } - - if (usefp) /* MOV x29, sp */ - Xadd(pcode, KPTR, reg2oper(FP), reg2oper(SP), mkoper(OIMM,)); - - /* ensure stack is 16-byte aligned for function calls */ - if (!fn->isleaf && ((fn->stksiz) & 0xF) != 0) { - assert(usefp); - rbpoff -= 8; - fn->stksiz += 8; - } - if (fn->stksiz) Xsub(pcode, KPTR, reg2oper(SP), reg2oper(SP), mkoper(OIMM, .imm = fn->stksiz)); -} - -static void -epilogue(uchar **pcode, struct function *fn, struct 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}); - for (int i = 0; i < frame->ngpairs; ++i, --p) - Xldp(pcode, KPTR, reg2oper(p->a), reg2oper(p->b), adr); - adr.m.disp = 8; - if (frame->single[1]) Xldr(pcode, KPTR, reg2oper(frame->single[1]), adr); - if (frame->single[0]) Xfldr(pcode, KF64, reg2oper(frame->single[0]), adr); - adr.m.disp = 16; - for (int i = 0; i < frame->nfpairs; ++i, --p) - Xfldp(pcode, KF64, reg2oper(p->a), reg2oper(p->b), adr); - } -} - -static void -emitbin(struct function *fn) -{ - struct block *blk; - uchar **pcode = &objout.code; - - while ((*pcode - objout.textbegin) % 4) ++*pcode; - fnstart = *pcode; - curfnsym = fn->name; - - /** prologue **/ - - /* only use frame pointer in non-leaf functions and functions that use the stack */ - usefp = !fn->isleaf || fn->stksiz; - struct frame frame; - prologue(pcode, &frame, fn); - - if (*pcode - fnstart > 8) { - /* largue prologue -> largue epilogue -> transform to use single exit point */ - struct block *exit = NULL; - blk = fn->entry->lprev; - do { - if (blk->jmp.t == Jret) { - if (!exit) { - if (blk->ins.n == 0) { - exit = blk; - continue; - } else { - exit = newblk(fn); - exit->lnext = blk->lnext; - exit->lprev = blk; - blk->lnext = exit; - exit->lnext->lprev = exit; - exit->id = fn->nblk++; - exit->jmp.t = Jret; - } - } - blk->jmp.t = Jb; - memset(blk->jmp.arg, 0, sizeof blk->jmp.arg); - blk->s1 = exit; - } else if (exit) { - /* thread jumps to the exit block */ - if (blk->s1 && !blk->s1->ins.n && blk->s1->s1 == exit && !blk->s1->s2) blk->s1 = exit; - if (blk->s2 && !blk->s2->ins.n && blk->s2->s1 == exit && !blk->s2->s2) blk->s2 = exit; - } - } while ((blk = blk->lprev) != fn->entry); - } - - blkaddr = allocz(fn->passarena, fn->nblk * sizeof *blkaddr, 0); - - blk = fn->entry; - do { - struct blkaddr *bb = &blkaddr[blk->id]; - uint bbaddr = *pcode - objout.textbegin; - assert(!bb->resolved); - while (bb->relreloc) { - int disp = (bbaddr - bb->relreloc)/4; - assert(disp >= -(1<<18) && disp < (1<<18)); - uint tmp = rd32targ(objout.textbegin + bb->relreloc); - wr32le(objout.textbegin + bb->relreloc, (tmp &~ (0x7FFFFu<<5)) | (disp & 0x7FFFF)<<5); - bb->relreloc = tmp>>5 & 0x7FFFF; - } - bb->resolved = 1; - bb->addr = bbaddr; - - lastcmp = NULL; - for (int i = 0; i < blk->ins.n; ++i) - emitinstr(pcode, fn, blk, i, &instrtab[blk->ins.p[i]]); - if (blk->jmp.t == Jret) { - if (blk->lnext != fn->entry && blk->lnext->jmp.t == Jret && blk->lnext->ins.n == 0) - continue; /* fallthru to next blk's RET */ - epilogue(pcode, fn, &frame); - W32(0xD65F03C0); /* RET */ - } else if (blk->jmp.t == Jtrap) { - W32(0xD4200020); /* BRK #0x1 */ - } else emitbranch(pcode, blk); - } while ((blk = blk->lnext) != fn->entry); - objdeffunc(fn->name, fn->globl, fnstart - objout.textbegin, *pcode - fnstart); -} - -void -aarch64_emit(struct function *fn) -{ - fn->stksiz = alignup(fn->stksiz, 8); - if (fn->stksiz > 1<<24) error(NULL, "'%s' stack frame too big", fn->name); - emitbin(fn); -} - -/* vim:set ts=3 sw=3 expandtab: */ diff --git a/aarch64/isel.c b/aarch64/isel.c deleted file mode 100644 index 398ea28..0000000 --- a/aarch64/isel.c +++ /dev/null @@ -1,515 +0,0 @@ -#include "all.h" - -#define isimm32(r) (iscon(r) && concls(r) == KI32) - -static inline uint -clz(uvlong x) -{ -#if HAS_BUILTIN(clzll) - return __builtin_clzll(x); -#else - int i = 0; - for (uvlong mask = BIT(63);; ++i, mask >>= 1) - if (x & mask) - break; - return i; -#endif -} - -/* Encode logical immediate */ -bool -aarch64_logimm(uint *enc, enum irclass k, uvlong x) -{ - /* https://github.com/v8/v8/blob/927ccc6076e25a614787c7011315468e40fe39a4/src/codegen/arm64/assembler-arm64.cc#L4409 */ - if (k == KI32) x = (uint)x | x << 32; - bool neg; - if ((neg = x & 1)) x = ~x; - if (x == 0) return 0; - uvlong a = x & (~x + 1), - xa = x + a, - b = xa & (~xa + 1), - xa_b = xa - b, - c = xa_b & (~xa_b + 1), - mask; - uint clza = clz(a), - d, outn; - if (c != 0) { - d = clza - clz(c); - mask = BIT(d) - 1; - outn = 0; - } else { - assert(a != 0); - d = 64; - mask = ~0ull; - outn = 1; - } - if (!ispo2(d)) return 0; - if (((b - a) & ~mask) != 0) return 0; - static const uvlong M[] = { - 0x0000000000000001, 0x0000000100000001, 0x0001000100010001, - 0x0101010101010101, 0x1111111111111111, 0x5555555555555555, - }; - int i = clz(d) - 57; - assert((uint)i < countof(M)); - uvlong m = M[i]; - uvlong y = (b - a) * m; - if (y != x) return 0; - if (enc) { - int clzb = b == 0 ? -1 : clz(b), - s = clza - clzb, r; - if (neg) { - s = d - s; - r = (clzb + 1) & (d - 1); - } else { - r = (clza + 1) & (d - 1); - } - *enc = outn<<12 | r<<6 | (((-d * 2) | (s - 1)) & 0x3F); - } - return 1; -} - - -static void fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi); -static void -regarg(union ref *r, enum irclass k, struct block *blk, int *curi) -{ - if (r->t != RTMP) { - *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, k, *r)); - if (kisflt(k) || instrtab[r->i].l.t == RSTACK) { - int iprev = *curi-1; - fixarg(&instrtab[r->i].l, &instrtab[r->i], blk, &iprev); - *curi = iprev+1; - } - } -} - -static void -fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi) -{ - enum op op = ins ? ins->op : 0; - if (isintcon(*r)) { - vlong x = intconval(*r); - switch (op) { - case Ocopy: return; - default: - if (oiscmp(op)) { - case Oadd: case Osub: - /* imm12 (lsl 12) */ - if ((x &~ 0xFFF) == 0 || (x &~ 0xFFF000) == 0) return; - break; - case Oshl: case Osar: case Oslr: - if ((uvlong)x < (ins->cls == KI32 ? 32 : 64)) return; - break; - case Oand: case Oior: case Oxor: - if (aarch64_logimm(NULL, ins->cls, x)) return; - break; - } - } - goto Reg; - } else if (isfltcon(*r)) { - enum irclass k = concls(*r), ki = KI32 + k-KF32; - if (contab.p[r->i].f != 0.0) { - union { - vlong i64; - int i32; - float f32; - double f64; - } pun; - vlong i; - if (k == KF32) { - pun.f32 = contab.p[r->i].f; - i = pun.i32; - } else { - pun.f64 = contab.p[r->i].f; - i = pun.i64; - } - union ref gpr = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ki, mkintcon(ki, i))); - *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, k, gpr)); - } else if (oiscmp(op)) { - return; - } else { - *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)); - if (op == Ocopy) - *ins = adr; - else - *r = insertinstr(blk, (*curi)++, adr); - } else if (r->t != RTMP) Reg: { - regarg(r, r->t == RTMP ? instrtab[r->i].cls : ins->cls ? ins->cls : KI32, blk, curi); - } -} - -static bool -arithfold(struct instr *ins) -{ - if (isnumcon(ins->l) && (!ins->r.t || isnumcon(ins->r))) { - union 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); - return 1; - } - return 0; -} - -static void -selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi) -{ - const struct call *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; - for (;; --iarg) { - assert(iarg >= 0 && i >= 0 && "arg?"); - if ((arg = &instrtab[blk->ins.p[iarg]])->op == Oarg) - break; - } - - if (!abi.isstk) { - 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}); - int iargsave = iarg; - if (!abi.ty.isagg) { /* scalar arg in stack */ - *arg = mkinstr(cls2store[abi.ty.cls], 0, adr, arg->r); - if (isaddrcon(arg->r,1) || arg->r.t == RADDR) - arg->r = insertinstr(blk, iarg++, mkinstr(Ocopy, abi.ty.cls, arg->r)); - else - fixarg(&ins->r, ins, blk, &iarg); - } else { /* aggregate arg in stack, callee stack frame destination address */ - *arg = mkinstr(Ocopy, KPTR, adr); - } - *curi += iarg - iargsave; - } - } - 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}); - ++*curi; - insertinstr(blk, *curi+1, (struct 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}); - else if (isintcon(ins->l)) - ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, ins->l)); - - cls = ins->cls; - ins->cls = 0; - if (cls) { - /* duplicate to reuse same TMP ref */ - insertinstr(blk, (*curi)++, *ins); - *ins = mkinstr(Ocopy, cls, mkref(RREG, call->abiret[0].reg)); - for (int i = 1; i <= 2; ++i) { - if (*curi + i >= blk->ins.n) break; - if (instrtab[blk->ins.p[*curi + i]].op == Ocall2r) { - ins = &instrtab[blk->ins.p[*curi += i]]; - *ins = mkinstr(Ocopy, ins->cls, mkref(RREG, call->abiret[1].reg)); - break; - } - } - } -} - -static bool -aimm(struct addr *addr, int disp) -{ - if (addr->index.bits) return 0; - vlong a = addr->disp; - a += disp; - if ((int)a == a) { - addr->disp = a; - return 1; - } - return 0; -} - -static bool -ascale(struct addr *addr, union ref a, union 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; - if ((unsigned)b.i > 3 || 1<index = a; - addr->shift = b.i; - return 1; - } - return 0; -} - -static bool -aadd(struct addr *addr, struct block *blk, int *curi, union 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]; - if (ins->op == Oadd) { - if (!aadd(addr, blk, curi, ins->l, siz)) goto Ref; - if (!aadd(addr, blk, curi, ins->r, siz)) goto Ref; - ins->skip = 1; - } else if (ins->op == Osub) { - if (!aadd(addr, blk, curi, ins->l, siz)) goto Ref; - if (!isintcon(ins->r)) goto Ref; - if (!aimm(addr, -intconval(ins->r))) goto Ref; - ins->skip = 1; - } else if (ins->op == Oshl) { - if (!ascale(addr, ins->l, ins->r, siz)) goto Ref; - ins->skip = 1; - } else if (ins->op == Ocopy) { - if (!aadd(addr, blk, curi, ins->l, siz)) goto Ref; - ins->skip = 1; - } else goto Ref; - } else if (isnumcon(r)) { - assert(isintcon(r)); - return aimm(addr, intconval(r)); - } else if (isaddrcon(r,1)) { - if (!addr->base.bits && !isaddrcon(addr->index,1)) addr->base = r; - else return 0; - } else if (r.t == RREG) { - /* temporaries are single assignment, but register aren't, so they can't be * - * safely hoisted into an address value, unless they have global lifetime */ - if (!rstest(mctarg->rglob, r.i)) return 0; - Ref: - if (r.t == RSTACK && (addr->base.bits || addr->index.bits)) { - r = insertinstr(blk, (*curi)++, mkinstr(Oadd, KPTR, mkref(RREG, FP), mkref(RICON, -r.i))); - } - if (!addr->base.bits) addr->base = r; - else if (!addr->index.bits) addr->index = r; - else return 0; - } else return 0; - return 1; -} - -static bool -fuseaddr(union ref *r, struct block *blk, int *curi, uint siz/*1,2,4,8*/) -{ - struct addr addr = {0}; - - if (isaddrcon(*r,1)) return 1; - - if (r->t != RSTACK && r->t != RTMP) return 0; - if (!aadd(&addr, blk, curi, *r, siz)) return 0; - if (!(addr.disp >= -256 && addr.disp < 256) /* for 9-bit signed unscaled offset */ - && !(!(addr.disp & (siz-1)) && (uvlong)addr.disp < (1<<12)*siz)) /* 12-bit unsigned scaled offset */ - return 0; - if (isaddrcon(addr.base,0) && (!(contab.p[addr.base.i].flag & SLOCAL) || addr.index.bits)) { - /* first load symbol address into a temp register */ - if (addr.disp && (ccopt.pic || (contab.p[addr.base.i].flag & SFUNC)) && !addr.index.bits) { - 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}))); - addr.disp = 0; - } - } - *r = mkaddr(addr); - return 1; -} - -static const uchar loadsz[] = { - [Oloads8 - Oloads8] = 1, [Oloadu8 - Oloads8] = 1, - [Oloads16 - Oloads8] = 2, [Oloadu16 - Oloads8] = 2, - [Oloads32 - Oloads8] = 4, [Oloadu32 - Oloads8] = 4, - [Oloadi64 - Oloads8] = 8, - [Oloadf32 - Oloads8] = 4, - [Oloadf64 - Oloads8] = 8, -}; -static const uchar storesz[] = { - [Ostorei8 - Ostorei8] = 1, - [Ostorei16 - Ostorei8] = 2, - [Ostorei32 - Ostorei8] = 4, - [Ostorei64 - Ostorei8] = 8, - [Ostoref32 - Ostorei8] = 4, - [Ostoref64 - Ostorei8] = 8, -}; -static void -loadstoreaddr(struct block *blk, union 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}); - } 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)) - regarg(r, KPTR, blk, curi); - } else if (r->t == RTMP || r->t == RSTACK) { - fuseaddr(r, blk, curi, siz); - } else if (r->t != RREG) { - *r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, *r)); - } -} - -static void -sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) -{ - enum op op = ins->op; - enum irclass cls; - - if (oisarith(ins->op) && arithfold(ins)) { - fixarg(&ins->l, ins, blk, curi); - return; - } - - switch (op) { - //default: assert(0); - case Onop: break; - case Oalloca1: case Oalloca2: case Oalloca4: case Oalloca8: case Oalloca16: - assert(!"unlowered alloca"); - break; - case Ocopy: - fixarg(&ins->l, ins, blk, curi); - break; - case Oparam: - assert(ins->l.t == RICON && ins->l.i < fn->nabiarg); - if (!fn->abiarg[ins->l.i].isstk) - *ins = mkinstr(Ocopy, ins->cls, mkref(RREG, fn->abiarg[ins->l.i].reg)); - else /* stack */ - *ins = mkinstr(Oadd, KPTR, mkref(RREG, FP), mkref(RICON, 16+fn->abiarg[ins->l.i].stk)); - break; - case Oneg: case Onot: - case Ocvtf32s: case Ocvtf32u: - case Ocvtf32f64: case Ocvtf64s: - case Ocvtf64u: case Ocvtf64f32: - case Ocvts32f: case Ocvtu32f: - case Ocvts64f: case Ocvtu64f: - case Oexts8: case Oextu8: - case Oexts16: case Oextu16: - case Oexts32: - regarg(&ins->l, ins->cls, blk, curi); - break; - case Oextu32: - regarg(&ins->l, ins->cls, blk, curi); - ins->op = Ocopy; - break; - case Oadd: - if (isnumcon(ins->l)) { - /* swap to have const in rhs */ - union ref tmp = ins->l; - ins->l = ins->r; - ins->r = tmp; - } - case Osub: - if (ins->r.t == RICON && ins->r.i < 0) { - op = ins->op ^= 1; - ins->r.i = -ins->r.i; - } - if (!(isaddrcon(ins->l,0) && (contab.p[ins->l.i].flag & SLOCAL))) - regarg(&ins->l, ins->cls, blk, curi); - fixarg(&ins->r, ins, blk, curi); - break; - case Oand: case Oior: case Oxor: - if (isnumcon(ins->l)) { - /* swap to have const in rhs */ - union ref tmp = ins->l; - ins->l = ins->r; - ins->r = tmp; - } - case Oshl: case Osar: case Oslr: - case Oequ: case Oneq: - case Olth: case Ogth: case Olte: case Ogte: - case Oulth: case Ougth: case Oulte: case Ougte: - case Omove: - regarg(&ins->l, ins->cls, blk, curi); - fixarg(&ins->r, ins, blk, curi); - break; - case Omul: case Odiv: case Oudiv: case Ourem: - regarg(&ins->l, ins->cls, blk, curi); - regarg(&ins->r, ins->cls, blk, curi); - break; - case Oarg: - fixarg(&ins->r, ins, blk, curi); - break; - case Ocall: - selcall(fn, ins, blk, curi); - break; - case Oloads8: case Oloadu8: case Oloads16: case Oloadu16: - case Oloads32: case Oloadu32: case Oloadi64: case Oloadf32: case Oloadf64: - loadstoreaddr(blk, &ins->l, curi, op); - break; - case Ostorei8: case Ostorei16: case Ostorei32: cls = KI32; goto Store; - case Ostorei64: cls = KI64; goto Store; - case Ostoref32: cls = KF32; goto Store; - case Ostoref64: cls = KF64; Store: - loadstoreaddr(blk, &ins->l, curi, op); - regarg(&ins->r, cls, blk, curi); - break; - } -} - -static void -seljmp(struct function *fn, struct 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]; - 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; - - c = insertinstr(blk, blk->ins.n, mkinstr(Ocopy, cls, c)); - sel(fn, &instrtab[c.i], blk, &curi); - } - 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]; - 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]; - int curi = blk->ins.n-1; - fixarg(&ins->r, ins, blk, &curi); - blk->jmp.arg[0] = r; - if (blk->jmp.arg[1].bits) { - r = mkref(RREG, fn->abiret[1].reg); - ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[1].ty.cls, r, blk->jmp.arg[1])).i]; - } - } - } -} - -void -aarch64_isel(struct function *fn) -{ - struct 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]; - 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]]; - sel(fn, ins, blk, &i); - } - seljmp(fn, blk); - } while ((blk = blk->lnext) != fn->entry); - - if (ccopt.dbg.i) { - bfmt(ccopt.dbgout, "<< After isel >>\n"); - irdump(fn); - } - - fn->prop = 0; -} - -/* vim:set ts=3 sw=3 expandtab: */ diff --git a/c/builtin.c b/c/builtin.c deleted file mode 100644 index 5c59857..0000000 --- a/c/builtin.c +++ /dev/null @@ -1,177 +0,0 @@ -#include "c.h" -#include "../ir/ir.h" - -static bool -callcheck(const struct span *span, int nparam, const union type *param, int narg, struct expr *args) -{ - bool ok = 1; - for (int i = 0, n = narg < nparam ? narg : nparam; i < n; ++i) { - if (!assigncheck(typedecay(param[i]), &args[i])) { - ok = 0; - error(&args[i].span, "arg #%d of type '%ty' is incompatible with '%ty'", - i, args[i].ty, param[i]); - } - } - - if (narg > nparam) { - error(&args[nparam].span, "too many args to builtin function taking %d params", nparam); - ok = 0; - } else if (narg < nparam) { - error(span, "not enough args to builtin function taking %d param%s", nparam, - nparam != 1 ? "s" : ""); - ok = 0; - } - return ok; -} - -#define DEF_FNLIKE_SEMA(name, retty, ...) \ - static bool \ - name##_sema(struct comp *cm, struct expr *ex) { \ - union 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) -{ - 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) -{ - assert(ex->t == ECALL && ex->narg == 1); - assert(typedecay(ex->sub[1].ty).bits == typedecay(cvalistty).bits); - if (!typedata[fn->fnty.dat].variadic) - error(&ex->span, "va_start used in non-variadic function"); - addinstr(fn, mkinstr(Ovastart, 0, compileexpr(fn, &ex->sub[1], 0))); - return NOREF; -} - -/* __builtin_va_end */ -static bool -va_end_sema(struct comp *cm, struct 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) -{ - 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) -{ - union 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); - addinstr(fn, mkarginstr(typ, dst)); - addinstr(fn, mkarginstr(typ, src)); - addinstr(fn, mkintrin(INstructcopy, 0, 2)); - return NOREF; -} - -/* __builtin_trap */ -DEF_FNLIKE_SEMA(trap, mktype(TYVOID), ) -static union ref -trap_comp(struct function *fn, struct 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) -{ - if (src.t == RTMP) { - if (insrescls(instrtab[src.i]) != dst) - return addinstr(fn, mkinstr(Ocopy, dst, src)); - return src; - } else if (isintcon(src)) { - vlong x = intconval(src); - return mkintcon(dst, cls2siz[dst] == 4 ? (int)x : x); - } - assert(!"int ref?"); -} - -/* __builtin_bswap16 */ -DEF_FNLIKE_SEMA(bswap16, mktype(TYUSHORT), mktype(TYUSHORT)) -static union ref -bswap16_comp(struct function *fn, struct expr *ex, bool discard) -{ - assert(isint(ex->ty)); - return irunop(fn, Obswap16, KI32, scalarcvt(fn, ex->ty, ex->sub[1].ty, - compileexpr(fn, &ex->sub[1], 0))); -} -/* __builtin_bswap32 */ -DEF_FNLIKE_SEMA(bswap32, mktype(TYUINT), mktype(TYUINT)) -static union ref -bswap32_comp(struct function *fn, struct expr *ex, bool discard) -{ - assert(isint(ex->ty)); - return irunop(fn, Obswap32, KI32, scalarcvt(fn, ex->ty, ex->sub[1].ty, - compileexpr(fn, &ex->sub[1], 0))); -} -/* __builtin_bswap64 */ -DEF_FNLIKE_SEMA(bswap64, mktype(TYUVLONG), mktype(TYUVLONG)) -static union ref -bswap64_comp(struct function *fn, struct expr *ex, bool discard) -{ - assert(isint(ex->ty)); - return irunop(fn, Obswap64, KI64, scalarcvt(fn, ex->ty, ex->sub[1].ty, - compileexpr(fn, &ex->sub[1], 0))); -} - -#define LIST_BUILTINS(_) \ - _(va_start) _(va_copy) _(va_end) \ - _(trap) _(bswap16) _(bswap32) _(bswap64) - -static const struct { - const char *name; - struct builtin b; -} tab[] = { -#define FNS(x) { "__builtin_" #x, { x##_sema, x##_comp } }, - LIST_BUILTINS(FNS) -#undef FNS -}; - -void -putbuiltins(struct env *env) -{ - for (int i = 0; i < countof(tab); ++i) { - envadddecl(env, &(struct decl) { - .name = intern(tab[i].name), - .isbuiltin = 1, - .builtin = &tab[i].b, - }); - } -} - -/* this is separate because it's a keyword */ -union ref -builtin_va_arg_comp(struct function *fn, const struct expr *ex, bool discard) -{ - assert(ex->t == EVAARG && ex->ty.t); - enum irclass k = isagg(ex->ty) ? KPTR : type2cls[scalartypet(ex->ty)]; - return addinstr(fn, mkinstr(Ovaarg, k, compileexpr(fn, ex->sub, 0), mktyperef(mkirtype(ex->ty)))); -} - -bool -hasbuiltin(const char *name, uint len) -{ - for (int i = 0; i < countof(tab); ++i) - if (!strncmp(name, tab[i].name, len)) - return 1; - return 0; -} - -/* vim:set ts=3 sw=3 expandtab: */ diff --git a/c/c.c b/c/c.c deleted file mode 100644 index 63c3f7f..0000000 --- a/c/c.c +++ /dev/null @@ -1,4772 +0,0 @@ -#include "c.h" -#include "lex.h" -#include "../endian.h" -#include "../ir/ir.h" -#include "../obj/obj.h" - -/** Parsing helper functions **/ -#define peek(Cm,Tk) lexpeek((Cm)->lx,Tk) -static int -lexc(struct comp *cm, struct token *tk) -{ - struct 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 */ - static char buf[200]; - vec_of(char) rest = VINIT(buf, sizeof buf); - do { - lex(cm->lx, NULL); - if (tk) { - joinspan(&tk->span.ex, tk2.span.ex); - if (!tk->wide) - vpushn(&rest, tk2.s, tk2.len); - else if (tk->wide && targ_primsizes[targ_wchartype] == 2) - vpushn(&rest, tk2.ws16, tk2.len*2); - else - vpushn(&rest, tk2.ws32, tk2.len*4); - } - } while (peek(cm, &tk2) == TKSTRLIT && tk2.wide == tk->wide); - if (tk) { - if (!tk->wide) { - tk->s = memcpy(alloc(&cm->exarena, tk->len + rest.n, 1), tk->s, tk->len); - memcpy((char *)tk->s + tk->len, rest.p, rest.n); - tk->len += rest.n; - } else if (tk->wide == 1) { - tk->ws16 = memcpy(alloc(&cm->exarena, tk->len + rest.n*2, 2), tk->ws16, tk->len*2); - memcpy((short *)tk->s + tk->len, rest.p, rest.n); - tk->len += rest.n * 2; - } else { - tk->ws32 = memcpy(alloc(&cm->exarena, tk->len + rest.n*4, 4), tk->ws32, tk->len*4); - memcpy((int *)tk->s + tk->len, rest.p, rest.n); - tk->len += rest.n * 4; - } - } - vfree(&rest); - } - if (ccopt.pedant && in_range(t, TKWBEGIN_, TKWEND_) && (tk = tk ? tk : tk_)->extwarn) { - static struct 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); - } - } - return t; -} -#define lex(Cm,Tk) lexc(Cm,Tk) -static bool -match(struct comp *cm, struct token *tk, enum toktag t) -{ - if (peek(cm, NULL) == t) { - lex(cm, tk); - return 1; - } - return 0; -} -static bool -expect(struct comp *cm, enum toktag t, const char *s) -{ - struct token tk; - if (!match(cm, &tk, t)) { - peek(cm, &tk); - if (aisprint(t)) tk.span.ex.len = tk.span.sl.len = 1; - error(&tk.span, "expected %'tt%s%s", t, s?" ":"",s ? s : ""); - return 0; - } - return 1; -} - -/******************************************/ -/* Data structures for declaration parser */ -/******************************************/ - -enum declkind { - DTOPLEVEL, - DFUNCPARAM, - DFUNCVAR, - DFIELD, - DCASTEXPR, -}; - -/* Since a declaration can have multiple declarators, and we need to process - * each one individually, the declaration parser is a state machine - * (conceptually a generator coroutine); the state is zero-initialized (except - * for the .kind field), each call to pdecl yields the next individual decl, - * 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 { - enum declkind kind; - union type base; - uchar scls; - uchar qual; - bool fnnoreturn : 1, - fninline : 1; - uint align; - bool base0, /* caller set initial base type, but there may be declspecs to parse */ - more, /* caller should keep calling pdecl to get next decl */ - varini, /* caller should parse an initializer ('=' ) and - call pdecl() to advance state before checking .more */ - funcdef, /* caller should parse an func definition ('{' '}'). - the declaration list is finished. */ - bitf, /* caller should parse a bitfield size and - call pdecl() to advance state before checking .more */ - tagdecl, /* declarator is a tagged type */ - empty; /* nothing decl (';') */ - internstr *pnames; /* param names for function definition */ - struct span *pspans; /* param spans ditto */ - uchar *pqual; /* param quals ditto */ - int attr; -}; -static struct decl pdecl(struct declstate *st, struct comp *cm); - -static struct decl *finddecl(struct comp *cm, internstr name); - -/* next token starts a decl? */ -static bool -isdecltok(struct comp *cm) -{ - struct token tk; - if (peek(cm, &tk) == TKIDENT) { - struct decl *decl = finddecl(cm, tk.name); - return decl && decl->scls == SCTYPEDEF; - } else { - static const bool kws[] = { -#define kw(x) [TKW##x-TKWBEGIN_] = 1 - kw(auto), kw(extern), kw(static), kw(register), kw(typedef), - kw(_Thread_local), kw(thread_local), kw(_Static_assert), - kw(inline), kw(_Noreturn), - kw(const), kw(volatile), kw(restrict), kw(_Atomic), - kw(void), kw(float), kw(double), kw(_Complex), - kw(signed), kw(unsigned), kw(short), kw(long), - kw(int), kw(char), kw(_Bool), kw(bool), - kw(struct), kw(union), kw(enum), - kw(__typeof__), kw(typeof), kw(typeof_unqual), - kw(__attribute__) -#undef kw - }; - return ((uint)tk.t-TKWBEGIN_) < countof(kws) && kws[tk.t-TKWBEGIN_]; - } -} - -/* next token starts an expr? */ -static bool -isexprtok(struct comp *cm) -{ - struct token tk; - if (peek(cm, &tk) == TKIDENT) { - struct decl *decl = finddecl(cm, tk.name); - return !decl || decl->scls != SCTYPEDEF; - } else { - static const bool tks[] = { -#define tk(x) [x] = 1 - tk('+'), tk('-'), tk('*'), tk('&'), tk('~'), tk('!'), tk(TKINC), tk(TKDEC), - tk(TKWsizeof), tk(TKW_Alignof), tk(TKWalignof), tk(TKWtrue), tk(TKWfalse), - tk('('), tk(TKNUMLIT), tk(TKCHRLIT), tk(TKSTRLIT), tk(TKW_Generic) -#undef tk - }; - return tk.t < countof(tks) && tks[tk.t]; - } -} - -/**********************************/ -/* 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; - /* list of decls is implicitly envdecls[decl..ndecl] */ - ushort decl, ndecl; - /* ditto for envtagged[] */ - ushort tagged, ntagged; -}; -/* use a hashmap for lookups of top-level declarations, since there's usually many of those */ -static pmap_of(ushort) tldeclmap; - -static void -envdown(struct comp *cm, struct env *e) -{ - assert(cm->env->decl + cm->env->ndecl == declsbuf.n); - assert(cm->env->tagged + cm->env->ntagged == envtagged.n); - e->decl = declsbuf.n; - e->tagged = envtagged.n; - e->ndecl = e->ntagged = 0; - e->up = cm->env; - cm->env = e; -} - -static void -envup(struct comp *cm) -{ - struct env *env = cm->env; - assert(env->decl + env->ndecl == declsbuf.n); - declsbuf.n -= env->ndecl; - envtagged.n -= env->ntagged; - assert(env->up); - cm->env = env->up; -} - -int -envadddecl(struct env *env, const struct decl *d) -{ - assert(env->decl + env->ndecl == declsbuf.n); - vpush(&declsbuf, *d); - assert(declsbuf.n < 1<<16); - ++env->ndecl; - if (!env->up) pmap_set(&tldeclmap, d->name, declsbuf.n-1); - return declsbuf.n - 1; -} - -/* 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) -{ - if (!env->ndecl) return 0; - if (!*d) *d = &declsbuf.p[env->decl + env->ndecl - 1]; - else if (*d == &declsbuf.p[env->decl]) return 0; - else --*d; - return 1; -} - -static struct tagged * -envaddtagged(struct env *env, union type ty, const struct span *span) -{ - struct tagged tagged = { ty, *span }; - assert(env->tagged + env->ntagged == envtagged.n); - vpush(&envtagged, tagged); - assert(envtagged.n < 1<<16); - ++env->ntagged; - return &envtagged.p[envtagged.n - 1]; -} - -/* like enviterdecl */ -static inline bool -envitertagged(struct tagged **l, struct env *env) -{ - if (!env->ntagged) return 0; - if (!*l) *l = &envtagged.p[env->tagged + env->ntagged - 1]; - else if (*l == &envtagged.p[env->tagged]) return 0; - else --*l; - return 1; -} - -static bool -redeclarationok(const struct decl *old, const struct decl *new) -{ - bool takeoldscls = 0; - if (old->scls != new->scls) { - if (old->scls == SCSTATIC && (new->scls &~ SCEXTERN) == SCNONE && old->ty.t == TYFUNC && new->ty.t == TYFUNC) - takeoldscls = 1; - else - return 0; - } - switch (old->scls) { - case SCSTATIC: - case SCEXTERN: - if (old->ty.bits == new->ty.bits) goto OkFuncs; - if (old->ty.t != new->ty.t) return 0; - if (old->ty.t == TYARRAY /* allow 'int x[]; int x[100];' */ - && typechild(old->ty).bits == typechild(new->ty).bits - && (isincomplete(old->ty) || isincomplete(new->ty))) - { - return 1; - } - if (old->ty.t == TYFUNC /* allow 'int f(); int f(int);' (some K&R) */ - && 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; - return 1; - } - return 0; - case SCTYPEDEF: - return old->ty.bits == new->ty.bits; - } - return 0; -} - -static int -putdecl(struct comp *cm, const struct decl *decl) -{ - for (struct env *env = cm->env; env; env = env->up) { - struct decl *l; - if (!env->up) { - ushort *pi = pmap_get(&tldeclmap, decl->name); - if (pi) { - l = &declsbuf.p[*pi]; - goto Match; - } - } else for (l = NULL; enviterdecl(&l, env);) { - if (decl->name == l->name) { - Match: - if ((cm->env->up != NULL && decl->scls == SCSTATIC) || (l->isdef && decl->isdef)) { - error(&decl->span, "redefinition of '%s'", decl->name); - note(&l->span, "previously defined here"); - break; - } else if (!redeclarationok(l, decl)) { - error(&decl->span, "incompatible redeclaration of '%s'", decl->name); - note(&l->span, "previously declared here"); - break; - } - if (l->isdef && !decl->isdef) return l - declsbuf.p; - break; - } - } - if (decl->scls != SCEXTERN) break; - } - return envadddecl(cm->env, decl); -} - -static struct decl * -finddecl(struct comp *cm, internstr name) -{ - assert(name); - for (struct 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);) { - if (name == l->name) - return l; - } - } - return NULL; -} - -static union type -gettagged(struct comp *cm, struct span *span, enum typetag tt, internstr name, bool dodef) -{ - struct typedata td = {0}; - assert(name); - for (struct env *e = cm->env; e; e = e->up) { - for (struct tagged *l = NULL; envitertagged(&l, e);) { - if (name == ttypenames[typedata[l->ty.dat].id]) { - if (dodef && e != cm->env) - goto Break2; - *span = l->span; - return l->ty; - } - } - } - if (tt == TYENUM && ccopt.pedant) { - warn(span, "forward-declared enum is an extension"); - } -Break2: - td.t = tt; - 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) -{ - struct typedata td = {0}; - assert(name); - for (struct tagged *l = NULL; envitertagged(&l, cm->env);) { - if (name == ttypenames[typedata[l->ty.dat].id]) { - *span = l->span; - return l->ty; - } - } - td.t = tt; - return envaddtagged(cm->env, ty.t ? ty : mktagtype(name, &td), span)->ty; -} - -/*********************/ -/* Expr Typechecking */ -/*********************/ - -#define iszero(ex) ((ex).t == ENUMLIT && isint((ex).ty) && (ex).u == 0) - -static bool -islvalue(const struct 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) -{ - if (isint(t)) t.t = intpromote(t.t); - else if (t.t == TYFLOAT) t.t = TYDOUBLE; - else if (t.t == TYARRAY) return mkptrtype(typechild(t), t.flag & TFCHLDQUAL); - else if (t.t == TYFUNC) return mkptrtype(t, 0); - return t; -} - -bool -assigncheck(union type t, const struct expr *src) -{ - union 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)) { - warn(&src->span, "usage of '%ty' discards pointer qualifiers", src->ty); - } - return 1; - } else if (t.t == TYPTR && srcty.t == TYPTR) { - warn(&src->span, "converting between incompatible pointer types ('%ty' -> '%ty')", srcty, t); - return 1; - } else if (t.t == TYPTR && iszero(*src)) return 1; - return 0; -} - -static bool -initcheck(union type t, const struct expr *src) -{ - if (assigncheck(t, src)) return 1; - if (t.bits == src->ty.bits && (src->t == EINIT || src->t == ESTRLIT)) return 1; - return 0; -} - -static void -incdeccheck(enum toktag tt, const struct expr *ex, const struct span *span) -{ - if (!isscalar(ex->ty)) - error(&ex->span, "invalid operand to %tt '%ty'", tt, ex->ty); - else if (!islvalue(ex)) - error(&ex->span, "operand to %tt is not an lvalue", tt); - else if (ex->ty.t == TYPTR && isincomplete(typechild(ex->ty))) - error(span, "arithmetic on pointer to incomplete type '%ty'", ex->ty); - else if (ex->ty.t == TYPTR && typechild(ex->ty).t == TYFUNC) - error(span, "arithmetic on function pointer '%ty'", ex->ty); -} - -static bool /* 6.5.4 Cast operators */ -castcheck(union type to, const struct expr *ex) -{ - union type src = ex->ty; - if (to.t == TYVOID) return 1; - if (isagg(to)) return 0; - if (to.bits == src.bits) return 1; - if (isarith(to) && isarith(src)) return 1; - if (isint(to) && isptrcvt(src)) return 1; - if (to.t == TYPTR && isint(src)) return 1; - if (to.t == TYPTR && isptrcvt(src)) return 1; - return 0; -} - -static union type /* 6.5.2.1 Array subscripting */ -subscriptcheck(const struct expr *ex, const struct expr *rhs, const struct span *span) -{ - union 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); - ty = mktype(TYINT); - } else if (ty.t == TYFUNC) { - error(span, "subscripted value is pointer to function"); - ty = mktype(TYINT); - } - } else { - error(&ex->span, "subscripted value is not pointer-convertible '%ty'", ex->ty); - ty = mktype(TYINT); - } - if (!isint(rhs->ty)) - error(&rhs->span, "array subscript is not integer ('%ty')", rhs->ty); - return ty; -} - -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) -{ - uint r = (tt == TKWsizeof ? typesize : typealign)(ty); - if (ty.t == TYVOID) { - if (ccopt.pedant) warn(span, "applying %'tt to void type", tt); - r = 1; - } else if (isincomplete(ty)) { - error(span, "cannot apply %'tt to incomplete type '%ty'", tt, ty); - } else if (ty.t == TYFUNC) { - error(span, "cannot apply %'tt to function type '%ty'", tt, ty); - } else if (tt == TKWsizeof && ex && ex->t == EGETF && ex->fld.bitsiz) { - error(span, "cannot apply %'tt to bitfield", tt); - } - if (tt != TKWsizeof && ex && ccopt.pedant) - warn(span, "%'tt applied to an expression is a GNU extension", tt); - return r; -} - -static bool /* 6.5.8 Relational operators */ -relationalcheck(const struct expr *a, const struct expr *b) -{ - union 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); - return t1.dat == t2.dat; - } - return 0; -} - -static bool -isnullpo(const struct expr *ex) /* match '0' or '(void *) 0' */ -{ - static const union 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? */ - && iszero(*ex); -} - -static bool /* 6.5.9 Equality operators */ -equalitycheck(const struct expr *a, const struct expr *b) -{ - union 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); - /* comparing .dat works for both TFCHLDPRIM and not, (checks equal child types) - * quals are ignored either way */ - return t1.dat == t2.dat || typechild(t1).t == TYVOID || typechild(t2).t == TYVOID; - } - 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) -{ - union 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; - if (isnullpo(a) && t2.t == TYPTR) return t2; - if (t1.t == TYPTR && t2.t == TYPTR) { - s1 = typechild(t1), s2 = typechild(t2); - if (s1.bits == s2.bits || s2.t == TYVOID || s1.t == TYVOID) { - return mkptrtype(s1.t == TYVOID ? s1 : s2, (t1.flag | t2.flag) & TFCHLDQUAL); - } - } - return mktype(0); -} - -static void -bintypeerr(const struct span *span, enum toktag tt, union type lhs, union type rhs) -{ - error(span, "bad operands to %tt ('%ty', '%ty')", tt, lhs, rhs); -} - -enum binopclass { /* binary operator type-checking classes */ - BCSET = 1<<7, /* is a (compound) assignment operator? */ - BCSEQ = 1, BCADDITIVE, BCARITH, BCINT, BCSHFT, BCEQL, BCCMP, BCLOG, -}; - -/* table indexed by binary op token; - * containing precedence level, expression kind and type-checking class */ -static const struct { uchar prec, t, k; } bintab[] = { - ['*'] = {13, EMUL, BCARITH}, - ['/'] = {13, EDIV, BCARITH}, - ['%'] = {13, EREM, BCINT}, - ['+'] = {12, EADD, BCADDITIVE}, - ['-'] = {12, ESUB, BCADDITIVE}, - [TKSHL] = {11, ESHL, BCSHFT}, - [TKSHR] = {11, ESHR, BCSHFT}, - ['<'] = {10, ELTH, BCCMP}, - ['>'] = {10, EGTH, BCCMP}, - [TKLTE] = {10, ELTE, BCCMP}, - [TKGTE] = {10, EGTE, BCCMP}, - [TKEQU] = {9, EEQU, BCEQL}, - [TKNEQ] = {9, ENEQ, BCEQL}, - ['&'] = {8, EBAND, BCINT}, - ['^'] = {7, EXOR, BCINT}, - ['|'] = {6, EBIOR, BCINT}, - [TKLOGAND] = {5, ELOGAND, BCLOG}, - [TKLOGIOR] = {4, ELOGIOR, BCLOG}, - ['?'] = {3, ECOND}, /* not actually a binop (special cased) */ - ['='] = {2, ESET, BCSET}, - [TKSETADD] = {2, ESETADD, BCSET|BCADDITIVE}, [TKSETSUB] = {2, ESETSUB, BCSET|BCADDITIVE}, - [TKSETMUL] = {2, ESETMUL, BCSET|BCARITH}, [TKSETDIV] = {2, ESETDIV, BCSET|BCARITH}, - [TKSETREM] = {2, ESETREM, BCSET|BCINT}, [TKSETAND] = {2, ESETAND, BCSET|BCINT}, - [TKSETIOR] = {2, ESETIOR, BCSET|BCINT}, [TKSETXOR] = {2, ESETXOR, BCSET|BCINT}, - [TKSETSHL] = {2, ESETSHL, BCSET|BCSHFT}, [TKSETSHR] = {2, ESETSHR, BCSET|BCSHFT}, - [','] = {1, ESEQ, BCSEQ} -}; - -static union type -bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct expr *rhs) -{ - enum binopclass k = bintab[tt].k; - union type ty = lhs->ty; - - assert(k); - if (k & BCSET) { - if (!islvalue(lhs)) - error(&lhs->span, "left-hand-side of assignment is not an lvalue"); - else if (lhs->qual & QCONST) - error(&lhs->span, "cannot assign to const-qualified lvalue (%tq)", ty, lhs->qual); - else if (isincomplete(ty)) - error(&lhs->span, "cannot assign to incomplete type '%ty'", ty); - else if (ty.t == TYARRAY) - error(&lhs->span, "cannot assign to array type '%ty'", ty); - else if (ty.t == TYFUNC) - error(&lhs->span, "cannot assign to function designator '%ty'", lhs->ty); - } - switch (k &~ BCSET) { - case 0: - if (isagg(ty) && !(lhs->qual & QCONST) && typedata[ty.dat].anyconst) - error(&lhs->span, "cannot assign to aggregate with const-qualified member"); - if (!assigncheck(ty, rhs)) - goto Error; - break; - case BCSEQ: - ty = rhs->ty; - break; - case BCADDITIVE: - if (tt == '+' && isptrcvt(rhs->ty)) { - /* int + ptr -> ptr + int (for convenience) */ - const struct expr swaptmp = *lhs; - *lhs = *rhs; - *rhs = swaptmp; - ty = lhs->ty; - } - if (isarith(ty) && isarith(rhs->ty)) { - /* num +/- num */ - ty = cvtarith(ty, rhs->ty); - assert(ty.t); - } else if ((ty.t == TYPTR || ty.t == TYARRAY) && isint(rhs->ty)) { - /* ptr +/- int */ - union type pointee = typechild(ty); - if (isincomplete(pointee)) - error(span, "arithmetic on pointer to incomplete type '%ty'", ty); - else if (pointee.t == TYFUNC) - error(span, "arithmetic on function pointer '%ty'", ty); - ty = typedecay(ty); - } else if (tt == '-' && isptrcvt(ty) && isptrcvt(rhs->ty)) { - /* ptr - ptr */ - union type pointee1 = typechild(typedecay(ty)), - pointee2 = typechild(typedecay(rhs->ty)); - if (isincomplete(pointee1)) - error(span, "arithmetic on pointer to incomplete type '%ty'", ty); - else if (pointee1.t == TYFUNC) - error(span, "arithmetic on function pointer '%ty'", lhs->ty); - else if (pointee1.bits != pointee2.bits) { - error(span, "arithmetic on incompatible pointer types: '%ty', '%ty'", - ty, rhs->ty); - } - ty = mktype(targ_ptrdifftype); - } else goto Error; - break; - case BCARITH: - ty = cvtarith(ty, rhs->ty); - if (!ty.t) { - ty.t = TYINT; - Error: - bintypeerr(span, tt, lhs->ty, rhs->ty); - } - break; - case BCINT: - if (!isint(ty) || !isint(rhs->ty)) - goto Error; - ty = cvtarith(ty, rhs->ty); - assert(ty.t); - break; - case BCSHFT: /* 6.5.7 Bitwise shift operators */ - if (!isint(ty) || !isint(rhs->ty)) { - ty = mktype(TYINT); - goto Error; - } - ty.t = intpromote(ty.t); - assert(ty.t); - break; - case BCEQL: - ty = mktype(TYINT); - if (!equalitycheck(lhs, rhs)) { - if (isptrcvt(lhs->ty) && isptrcvt(rhs->ty)) - warn(span, "comparison of distinct pointer types ('%ty' and '%ty')", lhs->ty, rhs->ty); - else - goto Error; - } - break; - case BCCMP: - ty = mktype(TYINT); - if (!relationalcheck(lhs, rhs)) { - if (isptrcvt(lhs->ty) && isptrcvt(rhs->ty)) - warn(span, "comparison of distinct pointer types ('%ty' and '%ty')", lhs->ty, rhs->ty); - else - goto Error; - } - break; - case BCLOG: /* 6.5.13-14 Logical AND/OR operator */ - ty = mktype(TYINT); - if (!isscalar(typedecay(ty)) || !isscalar(typedecay(rhs->ty))) - goto Error; - break; - } - return (k & BCSET) || !ty.t ? lhs->ty : ty; -} - -/****************/ -/* Expr Parsing */ -/****************/ - -#define mkexpr(t_,span_,ty_,...) ((struct expr){.t=(t_), .ty=(ty_), .span=(span_), __VA_ARGS__}) - -static struct expr * -exprdup(struct comp *cm, const struct 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) -{ - struct 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); - -enum { IMPLICITSYMTY = 0xFF, }; - -static struct expr /* 6.5.2.2 Function calls */ -callexpr(struct comp *cm, const struct span *span_, const struct 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)); - bool spanok = joinspan(&span.ex, span_->ex); - bool printsig = 0; - const struct builtin *builtin = NULL; - - if (callee->t == ESYM && !callee->ty.t && declsbuf.p[callee->decl].isbuiltin) { - builtin = declsbuf.p[callee->decl].builtin; - assert(!ty.t); - } - - if (callee->t == ESYM && ty.t == IMPLICITSYMTY) { /* implicit function decl.. */ - internstr name = callee->implicitsym; - struct 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); - } - - if (!builtin) { - if (ty.t == TYPTR) /* auto-deref when calling a function pointer */ - ty = typechild(ty); - if (ty.t != TYFUNC) - error(&span, "calling a value of type '%ty'", callee->ty); - else - td = &typedata[ty.dat]; - if (ty.t == TYFUNC && td->ret.t != TYVOID && isincomplete(td->ret)) - error(&span, "cannot call function with incomplete return type '%ty'", td->ret); - } - - if (!match(cm, &tk, ')')) for (;;) { - arg = expr(cm); - spanok = spanok && joinspan(&span.ex, callee->span.ex); - if (td && args.n == td->nmemb && !td->variadic && !td->kandr) { - error(&arg.span, "too many args to function taking %d params", td->nmemb); - printsig = 1; - } - if (arg.ty.t == TYVOID) { - error(&arg.span, "invalid use of void expression"); - } else if (td && args.n < td->nmemb && !td->kandr) { - if (!assigncheck(td->param[args.n], &arg)) { - error(&arg.span, "arg #%d of type '%ty' is incompatible with '%ty'", - args.n+1, arg.ty, td->param[args.n]); - printsig = 1; - } - } - vpush(&args, arg); - peek(cm, &tk); - if (match(cm, &tk, ',')) { - spanok = spanok && joinspan(&span.ex, tk.span.ex); - } else if (expect(cm, ')', "or ',' after arg")) { - break; - } - } - if (!spanok || !joinspan(&span.ex, tk.span.ex)) span = *span_; - - if (td && !td->variadic && !td->kandr && args.n < td->nmemb) { - error(&tk.span, "not enough args to function taking %d param%s", - td->nmemb, td->nmemb != 1 ? "s" : ""); - printsig = 1; - } - 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)); - ex.sub[0] = *callee; - memcpy(ex.sub+1, args.p, args.n*sizeof(struct expr)); - vfree(&args); - if (builtin) { - builtin->sema(cm, &ex); - } - return ex; -} - -static void -ppostfixopers(struct comp *cm, struct expr *ex) -{ - struct expr tmp, rhs; - struct token tk, tk2; - struct span span; - union type ty; - - for (;;) switch (peek(cm, &tk)) { - default: return; - case TKINC: - case TKDEC: - lex(cm, &tk); - span = ex->span; - if (!joinspan(&span.ex, tk.span.ex)) span = tk.span; - incdeccheck(tk.t, ex, &span); - *ex = mkexpr(tk.t == TKINC ? EPOSTINC : EPOSTDEC, span, ex->ty, .sub = exprdup(cm, ex)); - continue; - case '[': /* a[subscript] */ - lex(cm, NULL); - rhs = commaexpr(cm); - span = ex->span; - if (!joinspan(&span.ex, tk.span.ex) || !joinspan(&span.ex, ex->span.ex) - || (peek(cm, &tk), !joinspan(&span.ex, tk.span.ex))) - span = tk.span; - expect(cm, ']', NULL); - - if (isint(ex->ty) && isptrcvt(rhs.ty)) { - /* swap idx[ptr] -> ptr[idx] */ - tmp = *ex; - *ex = rhs; - rhs = tmp; - } - - ty = subscriptcheck(ex, &rhs, &span); - assert(ty.t); - if (!iszero(rhs)) { - tmp.sub = exprdup2(cm, ex, &rhs); - tmp.t = EADD; - tmp.span = span; - tmp.ty = typedecay(ex->ty); - } - tmp.sub = exprdup(cm, iszero(rhs) ? ex : &tmp); - tmp.span = span; - tmp.t = EDEREF; - tmp.qual = ex->ty.flag & TFCHLDQUAL; - tmp.ty = ty; - *ex = tmp; - continue; - case '(': /* call(args) */ - lex(cm, &tk); - span = ex->span; - *ex = callexpr(cm, &span, ex); - continue; - case TKARROW: - if (ex->ty.t != TYPTR && ex->ty.t != TYARRAY) - error(&ex->span, "operand to -> is not a pointer: '%ty'", ex->ty); - else - *ex = mkexpr(EDEREF, ex->span, typechild(ex->ty), .qual = ex->ty.flag & TFCHLDQUAL, - .sub = exprdup(cm, ex)); - /* fallthru */ - case '.': - lex(cm, &tk); - span = ex->span; - peek(cm, &tk2); /* field name */ - if (!expect(cm, TKIDENT, NULL)) tk2.s = ""; - if (!joinspan(&span.ex, tk.span.ex) || !joinspan(&span.ex, tk2.span.ex)) - span = tk.span; - if (!isagg(ex->ty)) { - 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)}; - 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 */ - ex->span = span; - ex->ty = fld.t; - ex->fld.off += fld.off; - ex->fld.bitoff = fld.bitoff; - ex->fld.bitsiz = fld.bitsiz; - } else { - *ex = mkexpr(EGETF, span, fld.t, .qual = ex->qual | fld.qual, .sub = exprdup(cm, ex), - .fld = { fld.off, fld.bitsiz, fld.bitoff }); - } - } - continue; - } -} - -static struct expr -vaargexpr(struct comp *cm, struct span *span) -{ - struct token tk; - struct 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; - expect(cm, ',', NULL); - decl = pdecl(&(struct declstate){DCASTEXPR}, cm); - ty = decl.ty; - peek(cm, &tk); - if (expect(cm, ')', NULL)) - joinspan(&span->ex, tk.span.ex); - if (ty.t == TYARRAY) - warn(&decl.span, "va_arg type argument is array type '%ty', which is undefined behavior", decl.ty); - else if (ty.t == TYFUNC) - error(&decl.span, "va_arg type argument is function type '%ty'", decl.ty); - else { - ty = argpromote(ty); - if (ty.bits != decl.ty.bits) { - warn(&decl.span, - "va_arg type argument is promotable type '%ty', which has undefined behavior" - " (it will be promoted to '%ty')", decl.ty, ty); - } - } - ex = mkexpr(EVAARG, *span, decl.ty, .sub = exprdup(cm, &arg)); - } - return ex; -} - -static struct expr -genericexpr(struct comp *cm, struct span *span) -{ - struct token tk; - if (expect(cm, '(', "after _Generic")) { - struct expr control = expr(cm), dfault = {0}, ex = {0}; - expect(cm, ',', NULL); - for (;;) { - if (match(cm, &tk, TKWdefault)) { - expect(cm, ':', NULL); - if (dfault.t) { - error(&tk.span, "duplicate 'default' specifier in generic selection expression"); - (void)expr(cm); - } else { - dfault = expr(cm); - } - } else { - struct decl decl = pdecl(&(struct declstate){DCASTEXPR}, cm); - union type ty = decl.ty; - expect(cm, ':', NULL); - if (!ex.t && - (typedecay(ty).bits == typedecay(control.ty).bits - || ((ty.t == TYENUM || control.ty.t == TYENUM) && scalartypet(ty) == scalartypet(control.ty)))) - ex = expr(cm); - else - (void)expr(cm); - } - if (match(cm, &tk, ')')) break; - else if (!expect(cm, ',', "or `)'")) { - if (!isdecltok(cm)) { - peek(cm, &tk); /* want the span */ - break; - } - } - } - if (!ex.t) ex = dfault; - if (!ex.t) { - error(&control.span, - "controlling type '%ty' not compatible with any generic association type", - control.ty); - ex.ty.t = TYINT; - } - ex.span = *span; - joinspan(&ex.span.ex, tk.span.ex); - return ex; - } - return mkexpr(ENUMLIT,*span,mktype(TYINT),); -} - -static inline int -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, - bool globl, enum qualifier qual, internstr name); - -static internstr istr__func__, istr_main, istr_memset; - -static internstr mkhiddensym(const char *fnname, const char *name, int id); - -/* parse an expression with the given operator precedence */ -/* 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) -{ - struct token tk; - struct span span; - struct expr ex; - union type ty; - struct { - struct span span; - union { - union type ty; /* cast type */ - struct { - uchar t0; /* t == 0 */ - short tt; /* token */ - }; - }; - } unops[4]; - int nunop = 0; - - if (ident) { - assert(ident->t == TKIDENT); - tk = *ident; - ident = NULL; - goto Ident; - } - -Unary: - switch (lex(cm, &tk)) { - /* unary operators (gather) */ - case '*': - if (ctx == EARRAYCOUNT && peek(cm, NULL) == ']') { - /* kludge for C99 `int x[*]` (unk VLA size) */ - return (struct expr) { 0, .span = tk.span }; - } - /* fallthru */ - case '+': case '-': case '~': case '!': - case '&': case TKINC: case TKDEC: - Unops: - unops[nunop].span = tk.span; - unops[nunop].t0 = 0; - unops[nunop].tt = tk.t; - if (++nunop >= countof(unops)) { - ex = exprparse(cm, 999, NULL, 0); - break; - } - goto Unary; - - /* might be unary op (cast) or primary expr */ - case '(': - if (!isdecltok(cm)) { /* (expr) */ - struct 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; - assert(decl.ty.t); - peek(cm, &tk); - if (expect(cm, ')', NULL)) - joinspan(&span.ex, tk.span.ex); - if (peek(cm, NULL) == '{') { - if (ccopt.cstd < STDC99) - warn(&tk.span, "compound literals are a c99 feature"); - ex = initializer(cm, &decl.ty, (decl.scls & SCSTATIC) ? EVSTATICINI : EVFOLD, - /* globl */ 0, decl.qual, NULL); - break; - } - unops[nunop].span = span; - unops[nunop].ty = decl.ty; - if (++nunop >= countof(unops)) { - ex = exprparse(cm, 999, NULL, 0); - break; - } - goto Unary; - } - break; - /* base exprs */ - case TKNUMLIT: - case TKCHRLIT: - ex = mkexpr(ENUMLIT, tk.span, mktype(0), ); - if (!(ty.t = parsenumlit(&ex.u, &ex.f, &tk, 0))) - error(&tk.span, "bad %s literal %'tk", tk.t == TKNUMLIT ? "number" : "character", &tk); - ex.ty.t = ty.t ? ty.t : TYINT; - break; - case TKWtrue: case TKWfalse: - ex = mkexpr(ENUMLIT, tk.span, mktype(TYBOOL), .u = tk.t == TKWtrue); - break; - case TKSTRLIT: - ty = mktype(((const char []){TYCHAR, TYSHORT, TYINT})[tk.wide]); - 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); - if (!decl) { - if (cm->env->up && tk.name->c == '_' - && (!strcmp(&tk.name->c, "__FUNCTION__") || !strcmp(&tk.name->c, "__PRETTY_FUNCTION__"))) { - /* hack: treat these identifiers as __func__ synonym to support the GNU extension */ - warn(&tk.span, "%'tk is a GNU extension", &tk); - decl = finddecl(cm, istr__func__); - assert(decl && decl->scls == SCSTATIC); - goto Sym; - } else if (ctx == EATTRARG && nunop == 0 && (peek(cm, NULL) == ',' || peek(cm, NULL) == ')')) { - return ex = mkexpr(ESYM, tk.span, mktype(IMPLICITSYMTY), .implicitsym = tk.name); - } else if (peek(cm, NULL) == '(') { /* implicit function decl? */ - ex = mkexpr(ESYM, tk.span, mktype(IMPLICITSYMTY), .implicitsym = tk.name); - } else { - error(&tk.span, "undeclared identifier %'tk", &tk); - ex = mkexpr(ESYM, tk.span, mktype(TYINT), .implicitsym = NULL); - } - } else if (decl->scls == SCTYPEDEF) { - error(&tk.span, "unexpected typename %'tk (expected expression)", &tk); - ex = mkexpr(ESYM, tk.span, decl->ty, .implicitsym = NULL); - } else if (decl->isenum) { - ex = mkexpr(ENUMLIT, tk.span, decl->ty, .i = decl->value); - } else Sym: { - if (decl->name == istr__func__ && decl->isbuiltin) { /* lazy __func__ */ - internstr fnname = decl->sym; - decl->isbuiltin = 0; - decl->sym = mkhiddensym(&fnname->c, "__func__", 1); - uint off = objnewdat(decl->sym, objout.code ? Stext : Srodata, 0, typesize(decl->ty), typealign(decl->ty)); - uchar *p = objout.code ? objout.textbegin + off : objout.rodata.p + off; - memcpy(p, fnname, typearrlen(decl->ty)-1); - } - ex = mkexpr(ESYM, tk.span, decl->ty, .qual = decl->qual, .decl = decl - declsbuf.p); - } - break; } - case TKWsizeof: case TKW_Alignof: case TKWalignof: { - enum toktag tt = tk.t; - uint res; - span = tk.span; - if (!match(cm, NULL, '(')) /* sizeof/alignof expr */ - goto Unops; - else if (isdecltok(cm)) { /* sizeof/alignof (type) */ - struct 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); - peek(cm, &tk); - if (expect(cm, ')', NULL)) - joinspan(&span.ex, tk.span.ex); - ppostfixopers(cm, &tmp); - ty = tmp.ty; - res = sizeofalignofcheck(&span, tt, ty, &tmp); - } - ex = mkexpr(ENUMLIT, span, mktype(targ_sizetype), .u = res); - break; } - case TKW__builtin_va_arg: - span = tk.span; - ex = vaargexpr(cm, &span); - break; - case TKW_Generic: - span = tk.span; - ex = genericexpr(cm, &span); - break; - default: - fatal(&tk.span, "expected %s (near %'tk)", ctx == EFROMSTMT ? "statement" : "expression", &tk); - } - - ppostfixopers(cm, &ex); - - /* unary operators (process) */ - while (nunop-- > 0) { - enum exprkind ek; - span = unops[nunop].span; - joinspan(&span.ex, ex.span.ex); - if (unops[nunop].t0 == 0) { - switch (unops[nunop].tt) { - case '+': - ek = EPLUS; - goto Alu; - case '-': - ek = ENEG; - goto Alu; - case '~': - ek = ECOMPL; - goto Alu; - case '!': - ek = ELOGNOT; - Alu: - ty = ek == ELOGNOT ? mktype(TYINT) : cvtarith(ex.ty, ex.ty); - if (!ty.t || (ek == ECOMPL && !isint(ty))) { - error(&tk.span, "invalid operand to %'tk '%ty'", &tk, ex.ty); - ty = mktype(TYINT); - } - ex = mkexpr(ek, span, ty, .sub = exprdup(cm, &ex)); - break; - case TKINC: case TKDEC: - ty = ex.ty; - incdeccheck(tk.t, &ex, &span); - ex = mkexpr(unops[nunop].tt == TKINC ? EPREINC : EPREDEC, span, ty, - .sub = exprdup(cm, &ex)); - break; - case '*': - if (ex.ty.t == TYPTR || ex.ty.t == TYARRAY) { - ty = typechild(ex.ty); - if (ty.t != TYVOID && isincomplete(typedecay(ty))) { - error(&span, "cannot dereference pointer to incomplete type '%ty'", ty); - ty = mktype(TYINT); - } - } else { - error(&span, "invalid operand to unary * '%ty'", ex.ty); - ty = mktype(TYINT); - } - ex = mkexpr(EDEREF, span, ty, .qual = ex.ty.flag & TFCHLDQUAL, - .sub = exprdup(cm, &ex)); - break; - case '&': - if (!islvalue(&ex)) - error(&span, "operand to unary & is not an lvalue"); - if (ex.t == EGETF && ex.fld.bitsiz) - error(&span, "cannot take address of bitfield"); - ex = mkexpr(EADDROF, span, mkptrtype(ex.ty, ex.qual), .sub = exprdup(cm, &ex)); - break; - case TKWsizeof: case TKW_Alignof: case TKWalignof: - ex = mkexpr(ENUMLIT, span, mktype(targ_sizetype), - .u = sizeofalignofcheck(&span, unops[nunop].tt, ex.ty, &ex)); - break; - default: assert(0); - } - } else { /* cast */ - ty = unops[nunop].ty; - if (!castcheck(ty, &ex)) - error(&span, "cannot cast value of type '%ty' to '%ty'", ex.ty, ty); - if (ex.t == ENUMLIT && isint(ex.ty) && ty.t == TYPTR) - ex.ty = ty; - else - ex = mkexpr(ECAST, span, ty, .sub = exprdup(cm, &ex)); - } - } - - /* binary operators */ - for (int opprec; (opprec = tkprec(peek(cm, &tk))) >= prec;) { - enum exprkind ek = bintab[tk.t].t; - struct expr rhs, tmp; - lex(cm, NULL); - if (ek != ECOND) { - /* only the assignment operators are right-associative */ - bool leftassoc = (bintab[tk.t].k & BCSET) == 0; - /* ex OP rhs */ - span.sl = tk.span.sl; - span.ex = ex.span.ex; - rhs = exprparse(cm, opprec + leftassoc, NULL, 0); - if (!joinspan(&span.ex, tk.span.ex) || !joinspan(&span.ex, rhs.span.ex)) - span.ex = tk.span.ex; - ty = bintypecheck(&span, tk.t, &ex, &rhs); - assert(ty.t); - ex = mkexpr(ek, span, ty, .sub = exprdup2(cm, &ex, &rhs)); - } else { - /* logical-OR-expression ? expression : conditional-expression */ - struct expr *sub; - span.sl = tk.span.sl; - span.ex = ex.span.ex; - if (!isscalar(ex.ty) && !isptrcvt(ex.ty)) - error(&ex.span, "?: condition is not a scalar type: '%ty'", ex.ty); - tmp = commaexpr(cm); - joinspan(&tk.span.ex, tmp.span.ex); - expect(cm, ':', NULL); - rhs = exprparse(cm, opprec, NULL, 0); - if (!joinspan(&span.ex, tk.span.ex) || !joinspan(&span.ex, tmp.span.ex) - || !joinspan(&span.ex, rhs.span.ex)) - span.ex = tk.span.ex; - ty = condtype(&tmp, &rhs); - if (!ty.t) { - error(&span, "incompatible types in conditional expression: '%ty', '%ty'", tmp.ty, rhs.ty); - ty = tmp.ty; - } - sub = alloc(&cm->exarena, 3 * sizeof*sub, 0); - sub[0] = ex, sub[1] = tmp, sub[2] = rhs; - ex = mkexpr(ECOND, span, ty, .sub = sub); - } - } - - return ex; -} - -static struct expr -expr(struct comp *cm) -{ - return exprparse(cm, bintab['='].prec, NULL, 0); /* non-comma expr */ -} - -static struct expr -arraycountexpr(struct comp *cm) -{ - return exprparse(cm, bintab['='].prec, NULL, EARRAYCOUNT); /* non-comma expr, or lone '*' */ -} - -static struct expr -constantexpr(struct comp *cm) -{ - return exprparse(cm, bintab['?'].prec, NULL, 0); /* conditional-expr */ -} - -static struct expr -commaexpr(struct comp *cm) -{ - return exprparse(cm, 1, NULL, 0); -} - -/****************/ -/* Initializers */ -/****************/ - -static uint -nmemb(union type ty) -{ - switch (ty.t) { - case TYARRAY: return typearrlen(ty) ? typearrlen(ty) : -1u; - case TYUNION: case TYSTRUCT: return typedata[ty.dat].nmemb; - default: return 1; - } -} - -static bool -objectp(union type ty) -{ - return isagg(ty) || ty.t == TYARRAY; -} - -static bool -chrarrayof(union type ty, union 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) -{ - *bitsiz = *bitoff = 0; - if (!objectp(ty)) { - *off = 0; - return ty; - } else if (ty.t == TYARRAY) { - *off = typesize(typechild(ty)) * idx; - return typechild(ty); - } else if (idx < typedata[ty.dat].nmemb) { - struct fielddata fld = typedata[ty.dat].fld[idx].f; - *off = fld.off; - *bitsiz = fld.bitsiz, *bitoff = fld.bitoff; - return fld.t; - } - *off = ~0u; - return mktype(0); -} - -struct initparser { - struct initcur { - union type ty; - uint idx; - uint off; - short prev; - } buf[32], *cur, *sub; - struct arena **arena; - uint arrlen; - enum evalmode ev; - bool dyn; /* when set, data is written to a temporary buffer first, because either: - - size is not known until parsing done (implicit array size) - - 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 */ - 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; - internstr sym; - vlong addend; - uint off; - } *drel; - }; - }; -}; - -static void -excesscheck(struct initparser *ip, const struct span *span) -{ - union type sub = ip->sub->ty; - uint n = nmemb(sub); - if (ip->sub->idx == n) { - if (sub.t == TYARRAY) - warn(span, "excess elements in array initializer for '%ty'", sub); - else if (sub.t == TYSTRUCT) - warn(span, "excess elements in initializer; '%ty' has %u member%s", sub, n, &"s"[n==1]); - else if (sub.t == TYUNION) - warn(span, "excess elements in union initializer"); - else - warn(span, "excess elements in scalar initializer"); - } -} - -#if 1 -#define dumpini(_) -#else -/* debugging */ -static void -dumpini(struct initparser *ip) -{ - efmt(">>>\n"); - for (struct 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"); - ioputc(&bstderr, '\n'); - } - efmt("<<<\n"); -} -#endif - -static vlong /* -> returns addend */ -expr2reloc(internstr *psym, const struct expr *ex) -{ - if (ex->t == ESSYMREF) { - *psym = ex->ssym.sym; - return ex->ssym.off; - } else if (ex->t == ESTRLIT || ex->t == EINIT) { - if (ex->t == ESTRLIT) assert(ex->ty.t == TYARRAY); - *psym = xcon2sym(expraddr(NULL, ex).i); - return 0; - } - fatal(&ex->span, "internal bug: non static reloc?"); -} - -static bool -rodatarelocok(void) -{ - return !(ccopt.pie | ccopt.pic); -} - -static void -iniwrite(struct comp *cm, struct initparser *ip, uint off, uint bitsiz, uint bitoff, union type ty, struct 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); - iniwrite(cm, ip, off + suboff, bitsiz, bitoff, sub, exprdup(cm, &mkexpr(EGETF, ex->span, sub, .sub = ex))); - } - } else if (ip->ev == EVSTATICINI) { - uchar *p; - uint siz = typesize(ty); - if (nerror) return; - if (ip->dyn) { - if (ip->ddat.n < off + siz) { - uint old = ip->ddat.n; - vresize(&ip->ddat, off + siz); - memset(ip->ddat.p + old, 0, ip->ddat.n - old); - assert(off + siz == ip->ddat.n); - } - p = ip->ddat.p + off; - } else { - p = (ip->sec == Sdata ? objout.data.p : objout.rodata.p) + ip->off + off; - } - - if (ex->t == ENUMLIT) { - struct expr *e = ex, tmp; - if (ex->ty.bits != ty.bits && ty.t != TYPTR) { - tmp = mkexpr(ECAST, ex->span, ty, .sub = ex); - e = &tmp; - assert(eval(e, EVSTATICINI)); - assert(e->t == ENUMLIT); - } - if (!bitsiz) switch (siz) { - default: assert(0); - case 1: *p = e->u; break; - case 2: wr16targ(p, e->u); break; - case 4: isint(ty) ? wr32targ(p, e->u) : wrf32targ(p, e->f); break; - case 8: isint(ty) ? wr64targ(p, e->u) : wrf64targ(p, e->f); break; - } else { - uvlong mask = (bitsiz == 64 ? -1ull : (1ull << bitsiz) - 1) << bitoff; - if (bitoff + bitsiz > siz*8) siz <<= 1; /* straddles an allocation boundary */ - switch (siz) { - default: assert(0); - case 1: *p = (*p &~ mask) | (e->u << bitoff & mask); break; - case 2: wr16targ(p, (rd16targ(p) &~ mask) | (e->u << bitoff & mask)); break; - case 4: wr32targ(p, (rd32targ(p) &~ mask) | (e->u << bitoff & mask)); break; - case 8: wr64targ(p, (rd64targ(p) &~ mask) | (e->u << bitoff & mask)); break; - } - } - } else if (ty.t == TYARRAY && ex->t == ESTRLIT) { - uint n = ex->s.n * typesize(typechild(ty)); - if (siz < n) n = siz; - /* XXX endian for wide strs */ - memcpy(p, ex->s.p, n); - } else { - internstr sym; - vlong addend = expr2reloc(&sym, ex); - if (!ip->dyn) { - assert(ip->sec != Srodata || rodatarelocok()); - objreloc(sym, targ_64bit ? REL_ABS64 : REL_ABS32, - ip->sec, ip->off + off, addend); - } else { - struct dreloc *rel = alloc(ip->arena, sizeof *rel, 0); - rel->link = ip->drel; - rel->sym = sym; - rel->off = off; - rel->addend = addend; - ip->drel = rel; - } - } - } else { - assert(cm != NULL); - struct init *init = ip->init; - struct initval val = { - .off = off, - .bitsiz = bitsiz, - .bitoff = bitoff, - .ex = *ex - }, *new = alloccopy(&cm->exarena, &val, sizeof val, 0); - *init->tail = new; - init->tail = &new->next; - if (!bitsiz) for (uint i = off, end = i + typesize(ex->ty); i < end; ++i) { - if (BSSIZE(end) > countof(init->zero)) break; - bsclr(init->zero, i); - } - } -} - -static bool -iniwriterec(struct comp *cm, struct initparser *ip, uint off, struct expr *ex) -{ - assert(ex->t == EINIT); - for (struct initval *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); - } - return 1; -} - -static struct initcur * -iniadvance(struct initparser *ip, struct initcur *c, const struct span *span) -{ - if (c - ip->buf >= countof(ip->buf) - 1) - fatal(span, "too many nested initializers"); - return c + 1; -} - -/* set the initializer cursor object */ -static void -inifocus(struct initparser *ip, struct comp *cm, const struct 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); - assert(!bitsiz); - - if (isagg(ip->sub->ty) && targ.t == TYARRAY && !typearrlen(targ)) - error(span, "cannot initialize flexible array member"); - excesscheck(ip, span); - - next->ty = targ; - next->idx = 0; - next->off = ip->sub->off + off; - next->prev = ip->cur - ip->buf; - ++ip->cur->idx; - ip->sub = ip->cur = next; -} - -/* initialize a character array with a string literal */ -static void -inistrlit(struct comp *cm, struct expr *ex, union type *ty) -{ - if (isincomplete(*ty)) { - *ty = mkarrtype(typechild(*ty), ty->flag & TFCHLDQUAL, ex->s.n + 1); - } else if (typearrlen(*ty) < ex->s.n) { - warn(&ex->span, "string literal in initializer is truncated from %u to %u bytes", - (ex->s.n+1)*typesize(typechild(*ty)), typesize(*ty)); - } - ex->ty = *ty; -} - -/* read scalar initializer into initializer list and avance */ -static void -ininext(struct initparser *ip, struct comp *cm) -{ - uint off, bitsiz, bitoff; - union type targ; - struct expr ex = expr(cm); - -Retry: - targ = membertype(&off, &bitsiz, &bitoff, ip->sub->ty, ip->sub->idx); - - if (isagg(ip->sub->ty) && targ.t == TYARRAY && !typearrlen(targ)) { - error(&ex.span, "cannot initialize flexible array member"); - ++ip->sub->idx; - return; - } - if (ex.t == ESTRLIT && chrarrayof(targ, typechild(ex.ty))) { - assert(!isincomplete(targ)); - inistrlit(cm, &ex, &targ); - iniwrite(cm, ip, ip->sub->off + off, 0,0, targ, &ex); - ++ip->sub->idx; - return; - } else if (ex.t == ESTRLIT && ip->sub->idx == 0 && chrarrayof(ip->sub->ty, typechild(ex.ty))) { - /* handle e.g. (char []){"foo"} */ - assert(off == 0); - targ = ip->sub->ty; - inistrlit(cm, &ex, &targ); - iniwrite(cm, ip, ip->sub->off, 0,0, targ, &ex); - if (ip->sub == ip->buf && ip->arrlen < ex.s.n+1) - ip->arrlen = ex.s.n+1; - --ip->sub; - return; - } else if (ip->sub->idx >= nmemb(ip->sub->ty) && ip->sub != ip->cur) { - --ip->sub; - goto Retry; - } else if (objectp(targ) && targ.bits != ex.ty.bits) { - struct 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 }; - ip->sub = next; - goto Retry; - } - excesscheck(ip, &ex.span); - - if (targ.t) { - if (!initcheck(targ, &ex)) - error(&ex.span, "cannot initialize '%ty' with expression of type '%ty'", targ, ex.ty); - else { - if (targ.bits == ex.ty.bits && ex.t == EINIT) { - if (!iniwriterec(cm, ip, ip->sub->off + off, &ex)) - goto CannotEval; - } else if (ip->ev && !eval(&ex, ip->ev) && ip->ev != EVFOLD) { - CannotEval: - error(&ex.span, "cannot evaluate expression statically"); - } else { - struct expr *pex = &ex; - if (ip->ev != EVSTATICINI) { - if (ex.ty.bits != targ.bits) - ex = mkexpr(ECAST, ex.span, targ, .sub = exprdup(cm, &ex)); - pex = exprdup(cm, &ex); - } - iniwrite(cm, ip, ip->sub->off + off, bitsiz, bitoff, targ, pex); - } - } - } - if (ip->sub == ip->buf && ip->arrlen < ip->sub->idx+1) - ip->arrlen = ip->sub->idx+1; - - if (++ip->sub->idx == 0) { - error(&ex.span, "element makes object too large"); - --ip->sub->idx; - } -} - -static int -aggdesignator(struct initparser *ip, union type ty, internstr name, const struct span *span) -{ - const struct typedata *td = &typedata[ty.dat]; - for (int i = 0; i < td->nmemb; ++i) { - struct 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); - save = ip->sub->idx; - ip->sub->idx = i+1; - *next = (struct 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) { - --ip->sub; - ip->sub->idx = save; - } else return sub; - } - } - return -1; -} - -static bool -designators(struct initparser *ip, struct comp *cm) -{ - struct token tk; - struct span span; - bool some = 0; - - for (;;) { - uint off, bitsiz, bitoff; - uvlong idx = ~0ull; - if (match(cm, &tk, '[')) { - struct 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); - assert(!bitsiz); - *next = (struct initcur) { ty, .off = ip->sub->off + off }; - ip->sub = next; - dumpini(ip); - } - if (expect(cm, ']', NULL)) joinspan(&span.ex, tk.span.ex); - if (ip->sub->ty.t != TYARRAY) - error(&ex.span, "array designator used with non-array type '%ty'", ip->sub->ty); - if (!eval(&ex, EVINTCONST)) - error(&ex.span, "array designator index is not an integer constant"); - else if (issigned(ex.ty) && ex.i < 0) - error(&ex.span, "negative array designator index"); - else if (ex.i > ~0u - 1) - error(&ex.span, "index too large"); - else { - idx = ex.u; - ip->sub->idx = idx; - if (ip->sub == ip->buf && ip->arrlen < idx+1) - ip->arrlen = idx+1; - dumpini(ip); - } - some = 1; - } else if (match(cm, &tk, '.')) { - 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 }; - ip->sub = next; - dumpini(ip); - } - if (expect(cm, TKIDENT, NULL)) joinspan(&span.ex, tk.span.ex); - if (!isagg(ip->sub->ty)) - error(&span, "member designator used with non-aggregate type '%ty'", ip->sub->ty); - else if (tk.t == TKIDENT) { - int idx; - for (;;) { - idx = aggdesignator(ip, ip->sub->ty, tk.name, &span); - if (idx >= 0 || ip->sub == ip->cur) break; - --ip->sub; - } - ip->sub->idx = idx; - if (idx < 0) - error(&span, "%ty has no such field: '%s'", ip->cur->ty, tk.name); - dumpini(ip); - } - some = 1; - } else { - if (some) { - expect(cm, '=', NULL); - } - return some; - } - } -} - -static struct expr -initializer(struct comp *cm, union 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}; - - ip->arena = &cm->exarena; - ip->ev = ev; - if (ev == EVSTATICINI) { - if (ty->t == TYARRAY && isincomplete(*ty)) { - ip->dyn = 1; - } else if (qual & QCONST && !rodatarelocok()) { - ip->dyn = 1; - vresize(&ip->ddat, typesize(*ty)); - memset(ip->ddat.p, 0, typesize(*ty)); - } else { - ip->sec = qual & QCONST ? Srodata : Sdata; - if (!nerror) - ip->off = objnewdat(sym, ip->sec, globl, typesize(*ty), typealign(*ty)); - } - } else { - ip->init = &res; - res.tail = &res.vals; - } - - if (!match(cm, &tk, '{')) { - struct 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); - } else if (!initcheck(*ty, &ex)) { - error(&ex.span, "cannot initialize '%ty' with expression of type '%ty'", *ty, ex.ty); - } else { - if (ev && !eval(&ex, ev) && ev != EVFOLD) - error(&ex.span, "cannot evaluate expression statically"); - else - iniwrite(cm, ip, 0, 0, 0, *ty, &ex); - } - if (ip->dyn) - goto Dynfix; - return ex; - } - - assert(countof(res.zero) == BSSIZE(64)); - if (ev != EVSTATICINI) { - memset(res.zero, 0xFF, sizeof res.zero); - } - - span = tk.span; - ip->sub = ip->cur = ip->buf; - ip->cur->ty = *ty; - for (;;) { - peek(cm, &tk); - joinspan(&span.ex, tk.span.ex); - if (tk.t == '[' || tk.t == '.') { - designators(ip, cm); - } - if (match(cm, &tk, '}')) { - if (ip->cur == ip->buf) break; - ip->sub = ip->cur = ip->buf + ip->cur->prev; - dumpini(ip); - } else if (match(cm, &tk, '{')) { - struct 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; - if (!objectp(ip->sub->ty)) { - error(&span, "scalar initializer cannot be empty"); - } else if (ccopt.cstd < STDC23 && ccopt.pedant) { - warn(&span, "empty initializer in %M is an extension"); - } - } else if (ip->sub->ty.t && !objectp(ip->sub->ty)) { - warn(&span, "brace initializer for scalar object '%ty'", ip->sub->ty); - } - continue; - } else { - dumpini(ip); - ininext(ip, cm); - } - match(cm, NULL, ','); - if (peek(cm, &tk) != '}' && ip->sub->ty.t == TYUNION) { - if (ip->sub == ip->cur) { - warn(&tk.span, "excess elements in union initializer"); - } else while (ip->sub != ip->cur && ip->sub->ty.t == TYUNION) { - --ip->sub; - } - } - } - if (ip->dyn) { - enum section sec; - uint off, siz, align; - uchar *p; - - if (isincomplete(*ty)) { - uint len = ip->arrlen > ip->cur->idx ? ip->arrlen : ip->cur->idx; - if (len == 0) - error(&span, "array cannot have zero length"); - *ty = mkarrtype(typechild(*ty), ty->flag & TFCHLDQUAL, len); - } - Dynfix: - if (qual & QCONST && (ip->drel == NULL || rodatarelocok())) - sec = Srodata; - else - sec = Sdata; - if (!nerror) { - off = objnewdat(sym, sec, globl, siz = typesize(*ty), align = typealign(*ty)); - 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) { - objreloc(rel->sym, targ_64bit ? REL_ABS64 : REL_ABS32, sec, off + rel->off, rel->addend); - } - } - vfree(&ip->ddat); - } - dumpini(ip); - - if (ev == EVSTATICINI) { - return (struct expr){.span = span}; - } else { - uint siz; - if (isincomplete(*ty)) { - uint len = ip->arrlen > ip->cur->idx ? ip->arrlen : ip->cur->idx; - if (!len) - error(&span, "initializer creates a zero-sized array"); - *ty = mkarrtype(typechild(*ty), ty->flag & TFCHLDQUAL, len); - } - - assert(countof(res.zero) == 1); - siz = typesize(*ty); - if (siz && siz <= 64) - res.zero->u &= ~0ull >> (64 - siz); - - return mkexpr(EINIT, span, *ty, .init = alloccopy(&cm->exarena, &res, sizeof res, 0)); - } -} - -/* debugging */ -void -dumpexpr(const struct expr *ex, bool prity) -{ - static const char *name[] = { - [EXXX] = "xxx", [ENUMLIT] = "numlit", [ESTRLIT] = "strlit", - [ESSYMREF] = "ssymref", [ESYM] = "sym", [EVAARG] = "vaarg", - [EINIT] = "init", [EGETF] = "getf", [ECALL] = "call", - [ECOND] = "cond", [EPLUS] = "plus", [ENEG] = "neg", - [ECOMPL] = "compl", [ELOGNOT] = "lognot", [EDEREF] = "deref", - [EADDROF] = "addrof", [ECAST] = "cast", [EPREINC] = "preinc", - [EPOSTINC] = "postinc", [EPREDEC] = "predec", [EPOSTDEC] = "postdec", - [EADD] = "add", [ESUB] = "sub", [EMUL] = "mul", - [EDIV] = "div", [EREM] = "rem", [EBAND] = "band", - [EBIOR] = "bior", [EXOR] = "xor", [ESHL] = "shl", - [ESHR] = "shr", [ELOGAND] = "logand", [ELOGIOR] = "logior", - [EEQU] = "equ", [ENEQ] = "neq", [ELTH] = "lth", - [EGTH] = "gth", [ELTE] = "lte", [EGTE] = "gte", - [ESET] = "set", [ESETADD] = "setadd", [ESETSUB] = "setsub", - [ESETMUL] = "setmul", [ESETDIV] = "setdiv", [ESETREM] = "setrem", - [ESETAND] = "setand", [ESETIOR] = "setior", [ESETXOR] = "setxor", - [ESETSHL] = "setshl", [ESETSHR] = "setshr", [ESEQ] = "seq", - }; - ioputc(&bstderr, '('); - efmt("%s ", name[ex->t]); - if (ex->ty.t && (prity || ex->t == EVAARG || ex->t == ECAST)) - efmt("<%ty> ", ex->ty); - int nsub = 0; - switch (ex->t) { - case ENUMLIT: if (!isflt(ex->ty)) efmt(isunsigned(ex->ty) ? "%lu" : "%ld", ex->u); - else efmt("%f", ex->f); - break; - case ESTRLIT: efmt("%'S", ex->s.p, ex->s.n); break; /* XXX widestr */ - case ESYM: efmt("%y", declsbuf.p[ex->decl].name); break; - case ESSYMREF: efmt("%y%+d", ex->ssym.sym, ex->ssym.off); - if (ex->ssym.func) efmt(" @func"); - if (ex->ssym.local) efmt(" @local"); - break; - case EVAARG: dumpexpr(ex->sub, prity); break; - case EGETF: dumpexpr(ex->sub, prity); efmt(" #+%u", ex->fld.off); - if (ex->fld.bitsiz) efmt("[%d:%d]", ex->fld.bitoff, ex->fld.bitsiz); - break; - case ECALL: nsub = ex->narg+1; goto Sub; - case ECOND: nsub = ex->narg+1; goto Sub; - default: - if (in_range(ex->t, EPLUS, EPOSTDEC)) nsub = 1; - else nsub = 2; - Sub: - for (int i = 0; i < nsub; ++i) { - if (i) ioputc(&bstderr, ' '); - dumpexpr(&ex->sub[i], prity); - } - break; - case EINIT: assert(!"nyi"); - } - ioputc(&bstderr, ')'); -} - -/*****************/ -/* Decls Parsing */ -/*****************/ - -static union type -buildagg(struct comp *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}; - 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 }; - do { - struct decl decl = pdecl(&st, cm); - uint tysize; - if (st.empty) { - if (ccopt.pedant) - warn(&decl.span, "extra semicolon in aggregate"); - continue; - } - tysize = typesize(decl.ty); - if (fld.n && td.flexi) { - td.flexi = 0; - error(&flexspan, "flexible array member is not at end of struct"); - } - if (!isunion && decl.ty.t == TYARRAY && !typearrlen(decl.ty)) { - td.flexi = 1; - flexspan = decl.span; - } else if (isincomplete(decl.ty)) { - error(&decl.span, "field has incomplete type '%ty'", decl.ty); - } else if (decl.ty.t == TYFUNC) { - error(&decl.span, "field has function type '%ty'", decl.ty); - } - bitsiz = 0; - if (st.bitf) { - struct expr ex = constantexpr(cm); - const char *name = decl.name ? &decl.name->c : ""; - if (!isint(decl.ty)) { - error(&decl.span, "bit-field '%s' has non-integer type '%ty'", name, decl.ty); - } else if (!isint(ex.ty)) { - error(&ex.span, "integer constant expression has non-integer type '%ty'", decl.ty); - } else if (!eval(&ex, EVINTCONST)) { - error(&ex.span, "cannot evaluate integer constant expression"); - } else if (ex.i < 0) { - error(&ex.span, "bit-field '%s' has negative width '%ld'", name, ex.i); - } else if (ex.i > 8*tysize) { - error(&ex.span, "width of bit-field '%s' (%ld) exceeds width of type (%d)", - name, ex.i, 8*tysize); - } else if (ex.i == 0 && decl.name) { - error(&ex.span, "named bit-field '%s' has zero width", name); - } else { - bitsiz = ex.i; - if (bitsiz == 0) { - if (bitftypesiz) { - bitfbyteoff += bitftypesiz; - bitfbyteoff = alignup(bitfbyteoff, typealign(decl.ty)); - } - bitoff = 0; - } else if (bitftypesiz && bitftypesiz < tysize) { - /* end of previous bitfield */ - bitoff = 0; - bitfbyteoff += bitftypesiz; - } else if (!bitftypesiz) { - bitoff = 0; - bitfbyteoff = alignup(td.siz, typealign(decl.ty)); - } else if (bitoff + bitsiz > 8*bitftypesiz) { - /* no straddling boundaries */ - bitoff = 0; - bitfbyteoff += bitftypesiz; - } - if (tysize > bitftypesiz) bitftypesiz = tysize; - } - pdecl(&st, cm); - } else { - bitftypesiz = bitoff = bitsiz = 0; - } - if (decl.ty.t) { - 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 }}; - if (bitftypesiz && siz != bitftypesiz) while (f.f.bitoff + f.f.bitsiz > 8*siz) { - /* adjust bitfields narrower than container type */ - f.f.off += siz; - f.f.bitoff -= 8*siz; - } - if (!decl.name && !bitftypesiz) { - if (!isagg(decl.ty) || ttypenames[typedata[decl.ty.dat].id]) { - warn(&decl.span, "declaration does not declare anything"); - continue; - } else if (ccopt.cstd < STDC11 && ccopt.pedant) { - warn(&decl.span, "anonymous %s in %M is an extension", - decl.ty.t == TYUNION ? "union" : "struct"); - } - } - if (decl.name || !bitftypesiz) - vpush(&fld, f); - td.anyconst |= decl.qual & QCONST; - if (isagg(decl.ty)) { - td.anyconst |= typedata[decl.ty.dat].anyconst; - if (typedata[decl.ty.dat].flexi && !isunion) - error(&decl.span, "nested aggregate has flexible array member"); - } - if (isunion) - td.siz = td.siz < siz ? siz : td.siz; - else - td.siz = off + siz; - td.align = td.align < align ? align : td.align; - bitoff += bitsiz; - } - } while (st.more); - } - if (td.flexi && fld.n == 1) - error(&flexspan, "flexible array member in otherwise empty aggregate"); - 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 }}; - error(&tk.span, "%s cannot have zero members", tag); - vpush(&fld, dummy); - td.siz = td.align = 1; - } - td.siz = alignup(td.siz, td.align); - td.fld = fld.p; - td.nmemb = fld.n; - if (id != -1) - t = completetype(name, id, &td); - else - t = mktagtype(name, &td); - vfree(&fld); - return t; -} - -static inline void -inttyminmax(vlong *min, uvlong *max, enum typetag tt) -{ - uint bits = 8*targ_primsizes[tt]; - *min = isunsignedt(tt) ? 0 : -(1ull << (bits - 1)); - *max = isunsignedt(tt) ? ~0ull >> (64 - bits) : bits == 64 ? ~0ull>>1 : (1ll << (bits - 1)) - 1; -} - -/* the backing type of enum (without a C23 fixed backing type) is int or the - * smallest-rank type that all the enumerators fit in, or if it doesn't exist, - * then the biggest signed type. the type of enumeration constants is the type of - * its defining expression when present or the type of the previous enumerator - * or in case of overflow the smallest type that fits (previous value + 1) - * this isn't strictly conforming since pre C23 enums are pretty loosely defined, - * 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) -{ - struct 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; - vlong iota = 0; - bool somelonglong = 0; - - inttyminmax(&tymin, &tymax, td.backing); - while (!match(cm, &tk, '}')) { - struct 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); - if (eval(&ex, EVINTCONST)) { - iota = ex.i; - if (ex.ty.t != ty.t) - inttyminmax(&tymin, &tymax, ex.ty.t); - ty = ex.ty; - } else { - error(&ex.span, "enum value is not an integer constant"); - } - } else if (tk.t != TKIDENT) { - lex(cm, NULL); - continue; - } - while (issigned(ty) ? (iota > (vlong)tymax || iota < tymin) : iota > tymax) - inttyminmax(&tymin, &tymax, ++ty.t); - somelonglong |= ty.t >= TYVLONG; - if ((isunsigned(ty) || iota > 0) && iota > maxv) - maxv = iota, maxvspan = tk.span; - else if (issigned(ty) && iota < minv) - minv = iota; - - decl.name = tk.name; - decl.ty = ty; - decl.isenum = 1; - decl.value = iota++; - putdecl(cm, &decl); - if (!match(cm, &tk, ',')) { - if (expect(cm, '}', "or `,'")) - break; - else lex(cm, NULL); - } - } - - td.backing = 0; - if (minv >= 0 && maxv <= ~0u) { - td.backing = TYUINT; - } else for (int t = TYINT; t <= TYUVLONG; ++t) { - inttyminmax(&tymin, &tymax, t); - if (minv >= tymin && maxv <= tymax) { - td.backing = t; - break; - } - } - if (!td.backing) { - td.backing = !somelonglong && ccopt.cstd == STDC89 && ccopt.pedant ? TYLONG : TYVLONG; - warn(&maxvspan, "enumerators exceed range of enum's backing type '%ty'", mktype(td.backing)); - } - if (td.backing >= TYVLONG && !somelonglong && ccopt.cstd == STDC89 && ccopt.pedant) - warn(span, "enum backing type is '%ty' in %M", mktype(td.backing)); - - if (id != -1) - ty = completetype(name, id, &td); - else - ty = mktagtype(name, &td); - ty.backing = td.backing; - return ty; -} - -static union type -tagtype(struct comp *cm, enum toktag kind) -{ - struct token tk; - union type t; - struct span span; - enum typetag tt = kind == TKWenum ? TYENUM : kind == TKWstruct ? TYSTRUCT : TYUNION; - internstr tag = NULL; - - peek(cm, &tk); - if (match(cm, &tk, TKIDENT)) - tag = tk.name; - span = tk.span; - if (!match(cm, NULL, '{')) { - if (!tag) { - error(&tk.span, "expected %tt name or '{'", kind); - return mktype(0); - } - t = gettagged(cm, &span, tt, tag, /* def? */ peek(cm, NULL) == ';'); - } else { - if (tag) { - t = deftagged(cm, &span, tt, tag, mktype(0)); - if (t.t != tt || !isincomplete(t)) { - if (t.t != tt) - error(&tk.span, - "defining tagged type %'tk as %tt clashes with previous definition", - &tk, kind); - else - error(&tk.span, "redefinition of '%tt %s'", kind, tag); - note(&span, "previous definition:"); - } - } - if (tt == TYENUM) - t = buildenum(cm, tag, &span, tag ? typedata[t.dat].id : -1); - else - t = buildagg(cm, tt, tag, tag ? typedata[t.dat].id : -1); - } - - if (t.t != tt) { - error(&tk.span, "declaring tagged type %'tk as %tt clashes with previous definition", - &tk, kind); - note(&span, "previous definition:"); - } - return t; -} - -static bool -attrspec(struct comp *cm, int *attr) -{ /* __attribute__ (( attribute-list )) */ - if (!match(cm, NULL, TKW__attribute__)) return 0; - if (!expect(cm, '(', "after __attribute__") || !expect(cm, '(', "after __attribute__")) { - Bad: - fatal(NULL, NULL); - } - while (!match(cm, NULL, ')')) { - struct token tk; - lex(cm, &tk); - if (tk.t != TKIDENT && !in_range(tk.t, TKWBEGIN_, TKWEND_)) { - fatal(&tk.span, "expected attribute name"); - } - internstr name = tk.name; - int ltrim = name[0].c == '_' && name[1].c == '_', - rtrim = tk.len > 2 && name[tk.len-1].c == '_' && name[tk.len-2].c == '_'; - if (ltrim || rtrim) { /* trim surrounding '__' */ - name = intern_(&name->c + ltrim*2, tk.len - ltrim*2 - rtrim*2); - } - if (match(cm, NULL, '(')) { - while (!match(cm, NULL, ')')) { - (void)exprparse(cm, bintab['='].prec, NULL, EATTRARG); - if (!match(cm, NULL, ',')) { - if (expect(cm, ')', NULL)) break; - else goto Bad; - } - } - } - (void)name; - if (!match(cm, NULL, ',')) { - if (expect(cm, ')', NULL)) break; - else goto Bad; - } - } - if (!expect(cm, ')', NULL)) - goto Bad; - return 1; -} - -static union type -ptypeof(struct comp *cm) -{ - union type ty; - expect(cm, '(', NULL); - if (isdecltok(cm)) { /* typeof (type) */ - struct declstate st = { DCASTEXPR }; - ty = pdecl(&st, cm).ty; - } else { /* typeof (expr) */ - ty = commaexpr(cm).ty; - } - expect(cm, ')', NULL); - return ty; -} - -static void -declspec(struct declstate *st, struct comp *cm, struct span *pspan) -{ - struct token tk; - struct decl *decl; - enum arith { - KSIGNED = 1<<0, - KUNSIGNED = 1<<1, - KBOOL = 1<<2, - KCHAR = 1<<3, - KSHORT = 1<<4, - KLONG = 1<<5, - KLONGLONG = 1<<6, - KINT = 1<<7, - KFLOAT = 1<<8, - KDOUBLE = 1<<9, - KCOMPLEX = 1<<10, - } arith = 0; - struct span span = {0}; - union type ty = st->base; - bool properdecl = st->kind == DFUNCVAR || st->kind == DTOPLEVEL; - - for (bool first = 1;; first = 0) { - enum storageclass scls = 0; - peek(cm, &tk); - if (first) span = tk.span; - switch (tk.t) { - /* storage-class-specifier */ - case TKWtypedef: scls = SCTYPEDEF; break; - case TKWextern: scls = SCEXTERN; break; - case TKWstatic: scls = SCSTATIC; break; - case TKWauto: scls = SCAUTO; break; - case TKWregister: scls = SCREGISTER; break; - case TKWthread_local: - case TKW_Thread_local: scls = SCTHREADLOCAL; break; - - /* function-specifier */ - case TKWinline: - if (!properdecl) BadFnSpec: { - error(&tk.span, "function specifier %'tk is not allowed here", &tk); - break; - } - st->fninline = 1; - break; - case TKW_Noreturn: - if (!properdecl) goto BadFnSpec; - st->fnnoreturn = 1; - break; - - /* alignment-specifier */ - case TKW_Alignas: assert(!"nyi alignas"); break; - - /* type-qualifier */ - case TKWconst: st->qual |= QCONST; break; - case TKWvolatile: st->qual |= QVOLATILE; break; - case TKWrestrict: /* unimplemented */ break; - case TKW_Atomic: /* unimplemented */ break; - - /* type-specifier */ - case TKWvoid: - if (st->base.t) { - DupBase: - error(&tk.span, "more than one data type in declaration specifier"); - } else { - st->base = mktype(TYVOID); - } - break; - case TKWsigned: - arith |= KSIGNED; - break; - case TKWunsigned: - arith |= KUNSIGNED; - break; - case TKW_Bool: case TKWbool: - if (arith & KBOOL) goto DupArith; - arith |= KBOOL; - break; - case TKWchar: - if (arith & KCHAR) { - DupArith: - error(&tk.span, "duplicate %tk specifier", &tk); - } - arith |= KCHAR; - break; - case TKWshort: - arith |= KSHORT; - break; - case TKWlong: - if ((arith & (KLONG | KLONGLONG)) == KLONG) - arith = (arith &~ KLONG) | KLONGLONG; - else if ((arith & (KLONG | KLONGLONG)) == 0) - arith |= KLONG; - else - error(&tk.span, "too long"); - break; - case TKWint: - if (arith & KINT) goto DupArith; - arith |= KINT; - break; - case TKWfloat: - if (arith & KFLOAT) goto DupArith; - arith |= KFLOAT; - break; - case TKWdouble: - if (arith & KDOUBLE) goto DupArith; - arith |= KDOUBLE; - break; - case TKW_Complex: - if (arith & KCOMPLEX) goto DupArith; - arith |= KCOMPLEX; - break; - case TKWenum: - case TKWstruct: - case TKWunion: - lex(cm, &tk); - ty = tagtype(cm, tk.t); - st->tagdecl = 1; - joinspan(&span.ex, tk.span.ex); - if (st->base.t) goto DupBase; - st->base = ty; - continue; - case TKW__typeof__: case TKWtypeof: - lex(cm, &tk); - ty = ptypeof(cm); - joinspan(&span.ex, tk.span.ex); - if (st->base.t) goto DupBase; - st->base = ty; - continue; - case TKIDENT: - if (!st->base.t && !arith && (decl = finddecl(cm, tk.name)) - && decl->scls == SCTYPEDEF) { - lex(cm, &tk); - st->base = decl->ty; - continue; - } - /* fallthru */ - default: - goto End; - case TKW_BitInt: - case TKW_Decimal128: case TKW_Decimal32: - case TKW_Decimal64: case TKW_Imaginary: - error(&tk.span, "%'tk is unsupported", &tk); - arith = arith ? arith : KINT; - } - - if ((!properdecl && scls && !(st->kind == DFUNCPARAM && scls == SCREGISTER)) || (scls == SCAUTO && st->kind == DTOPLEVEL)) - error(&tk.span, "storage class specifier %'tk is not allowed here", &tk); - else - st->scls |= scls; - - joinspan(&span.ex, tk.span.ex); - lex(cm, &tk); - } -End: - if (pspan) *pspan = span; - - if (st->scls && properdecl) { - if (popcnt(st->scls) > 1) - error(&span, "invalid combination of storage class specifiers"); - } - if (st->base.t && arith) { - /* combining arith type specifiers and other types */ - Bad: - error(&span, "invalid type specifiers"); - st->base = mktype(TYINT); - } else if (!st->base.t && arith) { - enum typetag t; - if (arith == KFLOAT) - t = TYFLOAT; - else if (arith == KDOUBLE) - t = TYDOUBLE; - else if (arith == (KLONG | KDOUBLE)) - t = TYLDOUBLE; - else if (arith == KBOOL) - t = TYBOOL; - else if (arith == KCHAR) - t = TYCHAR; - else if (arith == (KSIGNED | KCHAR)) - t = TYSCHAR; - else if (arith == (KUNSIGNED | KCHAR)) - t = TYUCHAR; - else if ((arith & ~KINT & ~KSIGNED) == KSHORT) - t = TYSHORT; - else if ((arith & ~KINT) == (KUNSIGNED | KSHORT)) - t = TYUSHORT; - else if ((arith & ~KINT & ~KSIGNED) == 0) - t = TYINT; - else if ((arith & ~KINT) == KUNSIGNED) - t = TYUINT; - else if ((arith & ~KINT & ~KSIGNED) == KLONG) - t = TYLONG; - else if ((arith & ~KINT) == (KUNSIGNED | KLONG)) - t = TYULONG; - else if ((arith & ~KINT & ~KSIGNED) == KLONGLONG) - t = TYVLONG; - else if ((arith & ~KINT) == (KUNSIGNED | KLONGLONG)) - t = TYUVLONG; - else if (arith == (KCOMPLEX | KFLOAT)) - t = TYCOMPLEXF; - else if (arith == (KCOMPLEX | KDOUBLE)) - t = TYCOMPLEX; - else if (arith == (KCOMPLEX | KLONG | KDOUBLE)) - t = TYCOMPLEXL; - else - goto Bad; - st->base = mktype(t ? t : TYINT); - } else if (!st->base.t) { - if (ccopt.cstd < STDC23 && peek(cm, NULL) == TKIDENT) { - if (ccopt.cstd > STDC89) - warn(&tk.span, "type implicitly declared as int"); - st->base = mktype(TYINT); - } else { - error(&tk.span, "type specifier missing"); - } - } -} - -/* circular doubly linked list used to parse declarators */ -enum { EARRAYUNSIZED = 0xFF /* for count.t */ }; -static struct decllist { - struct decllist *prev, *next; - uchar t; /* TYPTR, TYARRAY or TYFUNC */ - union { - uchar qual; /* TYPTR */ - struct expr count; /* TYARRAY */ - struct { /* TYFUNC */ - union type *param; - internstr *pnames; - struct span *pspans; - uchar *pqual; - short npar; - bool kandr : 1, variadic : 1; - }; - }; - struct span span; -} decltmp[64], *declfreelist; -static bool usingdeclparamtmp; -static union type declparamtmp[16]; -static internstr declpnamestmp[16]; -static struct span declpspanstmp[16]; -static uchar declpqualtmp[16]; - -static void -declinsert(struct decllist *list, const struct decllist *node) -{ - struct decllist *pnode = declfreelist; - if (!pnode) fatal(NULL, "too many nested declarators"); - declfreelist = declfreelist->next; - *pnode = *node; - pnode->next = list->next; - pnode->prev = list; - list->next->prev = pnode; - list->next = pnode; -} - -static int -cvqual(struct comp *cm) -{ - struct 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; - return q; -} - -static void -decltypes(struct comp *cm, struct decllist *list, internstr *name, struct span *span, struct span *namespan) -{ - struct token tk; - struct decllist *ptr, node; - - while (match(cm, &tk, '*')) { - node.t = TYPTR; - node.qual = cvqual(cm); - node.span = tk.span; - declinsert(list, &node); - joinspan(&span->ex, tk.span.ex); - } - ptr = list->next; - switch (peek(cm, &tk)) { - case '(': - lex(cm, &tk); - if (isdecltok(cm)) { - goto Func; - } else if (match(cm, &tk, ')')) { - /* T () is K&R func proto */ - node.span = tk.span; - node.t = TYFUNC; - node.param = NULL; - node.pqual = NULL; - node.pnames = NULL; - node.pspans = NULL; - node.variadic = 0; - node.kandr = 1; - node.npar = 0; - declinsert(ptr->prev, &node); - joinspan(&span->ex, tk.span.ex); - break; - } else { - decltypes(cm, list, name, span, namespan); - expect(cm, ')', NULL); - joinspan(&span->ex, tk.span.ex); - } - break; - case TKIDENT: - if (!name) - error(&tk.span, "unexpected identifier in type name"); - else { - *name = tk.name; - *namespan = tk.span; - } - lex(cm, &tk); - joinspan(&span->ex, tk.span.ex); - break; - default: - if (name) - *name = NULL; - } - for (;;) { - if (match(cm, &tk, '[')) { - node.span = tk.span; - int q = 0; - bool statik = 0; - if (in_range(peek(cm, &tk), TKWBEGIN_, TKWEND_)) { - q = cvqual(cm); - statik = match(cm, NULL, TKWstatic); - q |= cvqual(cm); - } - (void)q, (void)statik; /* stub */ - - if (match(cm, &tk, ']')) { - node.count.t = EARRAYUNSIZED; - } else { - node.count = arraycountexpr(cm); - peek(cm, &tk); - joinspan(&node.span.ex, tk.span.ex); - expect(cm, ']', NULL); - } - node.t = TYARRAY; - declinsert(ptr->prev, &node); - joinspan(&span->ex, node.span.ex); - } else if (match(cm, &tk, '(')) Func: { - vec_of(union type) params = {0}; - vec_of(uchar) qual = {0}; - vec_of(internstr) names = {0}; - vec_of(struct span) spans = {0}; - - if (!usingdeclparamtmp) { - usingdeclparamtmp = 1; - vinit(¶ms, declparamtmp, countof(declparamtmp)); - vinit(&qual, declpqualtmp, countof(declpqualtmp)); - vinit(&names, declpnamestmp, countof(declpnamestmp)); - vinit(&spans, declpspanstmp, countof(declpspanstmp)); - } - - node.span = tk.span; - node.kandr = 0; - node.variadic = 0; - if (ccopt.cstd < STDC23 && !isdecltok(cm)) { - node.kandr = 1; - } - - if (!match(cm, &tk, ')')) for (;;) { - if (match(cm, &tk, TKDOTS)) { - node.variadic = 1; - expect(cm, ')', NULL); - break; - } - if (node.kandr) { - if (match(cm, &tk, TKIDENT)) { - vpush(¶ms, mktype(TYINT)); - vpush(&names, tk.name); - vpush(&spans, tk.span); - } else error(&tk.span, "expected identifier"); - } else if (!isdecltok(cm) && peek(cm, &tk) != TKIDENT) { - error(&tk.span, "expected parameter declarator"); - } else { - struct declstate st = { DFUNCPARAM }; - struct decl decl; - decl = pdecl(&st, cm); - decl.ty = typedecay(decl.ty); - vpush(¶ms, decl.ty); - vpush(&names, decl.name); - vpush(&spans, decl.span); - vpush(&qual, decl.qual); - if (decl.ty.t == TYVOID) { - if (params.n > 1 || decl.qual || decl.name || peek(cm, &tk) != ')') { - error(&decl.span, "function parameter #%d has void type", - params.n, decl.ty, qual.p[params.n-1]); - } - } - } - peek(cm, &tk); - joinspan(&node.span.ex, tk.span.ex); - if (!match(cm, &tk, ',')) { - expect(cm, ')', "or `,'"); - break; - } - } else { - if (ccopt.cstd < STDC23) node.kandr = 1; - } - if (node.kandr && ccopt.cstd != STDC89 && params.n > 0) { - warn(&node.span, "K&R function prototype is deprecated"); - } else if (params.n == 1 && params.p[0].t == TYVOID && !qual.p[0] && !names.p[0]) { /* (void) */ - vfree(¶ms); - vfree(&names); - vfree(&spans); - vfree(&qual); - } - node.t = TYFUNC; - node.param = params.n ? params.p : NULL; - node.pqual = qual.n ? qual.p : NULL; - node.pnames = params.n ? names.p : NULL; - node.pspans = params.n ? spans.p : NULL; - node.npar = params.n; - declinsert(ptr->prev, &node); - joinspan(&span->ex, node.span.ex); - } else break; - } -} - -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}; - if (!inidecltmp) { - inidecltmp = 1; - for (int i = 0; i < countof(decltmp); ++i) { - decltmp[i].next = declfreelist; - declfreelist = &decltmp[i]; - } - } - - decltypes(cm, &list, st->kind == DCASTEXPR ? NULL : &decl.name, &decl.span, &namespan); - if (!decl.name && st->kind != DCASTEXPR && st->kind != DFUNCPARAM) { - if (list.prev == &list) lex(cm, NULL); - error(&decl.span, "expected `(', `*' or identifier"); - } - for (l = list.prev; l != &list; l = l->prev) { - switch (l->t) { - case TYPTR: - decl.ty = mkptrtype(decl.ty, decl.qual); - decl.qual = l->qual; - break; - case TYARRAY: - if (isincomplete(decl.ty)) - error(&l->span, "array has incomplete element type '%ty'", decl.ty); - else if (decl.ty.t == TYFUNC) - error(&l->span, "array has element has function type '%ty'", decl.ty); - if (l->count.t == EARRAYUNSIZED) /* unsized '[]' */ - decl.ty = mkarrtype(decl.ty, decl.qual, 0); - else { - uint n = 0; - struct 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)) { - error(&ex->span, "array length is not an integer constant"); - } else if (issigned(ex->ty) && ex->i < 0) { - error(&ex->span, "array length is negative"); - } else if (ex->u > (1ull << (8*sizeof n)) - 1) { - error(&ex->span, "array too long (%ul)", ex->u); - } else if (ex->u == 0) { - /* when struct field, silently accept zero-length as synonym of '[]' for flexible array member */ - if (l->prev != &list || st->kind != DFIELD) { - warn(&ex->span, "array cannot have zero length"); - } - } else { - n = ex->u; - } - decl.ty = mkarrtype(decl.ty, decl.qual, n); - } - break; - case TYFUNC: - if (decl.ty.t == TYFUNC) - error(&decl.span, "function cannot return function type '%ty'", decl.ty); - else if (decl.ty.t == TYARRAY) - error(&decl.span, "function cannot return array type '%ty'", decl.ty); - if (l->kandr && ccopt.cstd > STDC89 && (ccopt.cstd >= STDC23 || ccopt.pedant)) - warn(&l->span, "function declaration without a prototype is deprecated"); - decl.ty = mkfntype(decl.ty, l->npar, l->param, l->kandr, l->variadic); - 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->pqual = l->pqual ? alloccopy(&cm->fnarena, l->pqual, l->npar, 1) : NULL; - decl.inlin = st->fninline; - decl.noret = st->fnnoreturn; - } - if (l->pqual != declpqualtmp) free(l->pqual); - if (l->pnames != declpnamestmp) free(l->pnames); - if (l->pspans != declpspanstmp) free(l->pspans); - if (l->param == declparamtmp) usingdeclparamtmp = 0; - decl.qual = 0; - break; - } - - l->next = declfreelist; - declfreelist = l; - } - if (st->kind != DCASTEXPR && decl.name) - decl.span = namespan; - return decl; -} - -static void -pstaticassert(struct comp *cm, struct span *span) -{ - struct expr ex; - struct token tk, msg = {0}; - - /* _Static_assert '(' [ ',' ] ')' ';' */ - expect(cm, '(', NULL); - ex = expr(cm); - peek(cm, &tk); - if (match(cm, &tk, ',')) { - peek(cm, &msg); - expect(cm, TKSTRLIT, NULL); - } - peek(cm, &tk); - expect(cm, ')', NULL); - expect(cm, ';', NULL); - - joinspan(&span->ex, tk.span.ex); - if (!msg.t && ccopt.cstd == STDC11) - warn(span, "static assert without message is a C23 extension"); - if (!eval(&ex, EVINTCONST)) { - error(&ex.span, "static assert expression is not an integer constant"); - } else if (iszero(ex)) { - if (msg.t) - error(&ex.span, "static assertion failed: %'S", msg.s, msg.len); - else - error(&ex.span, "static assertion failed"); - } -} - -static struct decl -pdecl(struct declstate *st, struct comp *cm) { - struct token tk; - struct decl decl; - bool properdecl = st->kind == DTOPLEVEL || st->kind == DFUNCVAR; - bool first = 0; - - assert(!st->funcdef); - - if (st->varini || st->bitf) { - memset(&decl, 0, sizeof decl); - goto AfterIniBitf; - } - decl.sym = NULL; - - if (st->base0) goto DeclSpec; - 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}; - } - first = 1; - if (match(cm, &tk, ';')) { - st->empty = 1; - return (struct decl){.span = tk.span}; - } - - DeclSpec: - st->base0 = 0; - while (attrspec(cm, &st->attr)) ; - declspec(st, cm, &decl.span); - } else { - peek(cm, &tk); - decl.span = tk.span; - } - 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 }; - 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 }; - st->bitf = 1; - return decl; - } - decl = declarator(st, cm, decl.span); - if (decl.ty.t != TYFUNC && st->fninline) - error(&decl.span, "`inline' used on non-function declaration"); - if (decl.ty.t != TYFUNC && st->fnnoreturn) - error(&decl.span, "`_Noreturn' used on non-function declaration"); - /* trailing attributes */ - if (st->kind == DTOPLEVEL || st->kind == DFUNCVAR) { - while (attrspec(cm, &st->attr)) ; - if (match(cm, NULL, TKW__asm__) && expect(cm, '(', NULL)) { - peek(cm, &tk); - if (expect(cm, TKSTRLIT, "asm symbol name")) { - decl.sym = intern_(tk.s, tk.len); - } - expect(cm, ')', NULL); - } - while (attrspec(cm, &st->attr)) ; - } - - if (properdecl && match(cm, &tk, '=')) { - st->varini = 1; - return decl; - } else if (first && decl.ty.t == TYFUNC && match(cm, &tk, '{')) { - st->funcdef = 1; - return decl; - } else if (st->kind == DFIELD && match(cm, &tk, ':')) { - st->bitf = 1; - return decl; - } - -AfterIniBitf: - st->varini = st->bitf = 0; - st->more = 0; - if (st->kind != DCASTEXPR && st->kind != DFUNCPARAM) { - if (match(cm, &tk, ',')) - st->more = 1; - else expect(cm, st->kind == DFUNCPARAM ? ')' : ';', "or `,'"); - } - - return decl; -} - -/*****************/ -/* IR Generation */ -/*****************/ - -static inline union ref -exprvalue(struct function *fn, const struct expr *ex) -{ - return compileexpr(fn, ex, /*discard*/ 0); -} -static inline void -expreffects(struct function *fn, const struct expr *ex) -{ - compileexpr(fn, ex, /*discard*/ 1); -} - -static void -structcopy(struct function *fn, union type ty, union ref dst, union ref src) -{ - union 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) -{ - return expraddr(fn, src); -} - -static union ref compilecall(struct function *fn, const struct expr *ex); - -static internstr -mkhiddensym(const char *fnname, const char *name, int id) -{ - char buf[200]; - struct wbuf wbuf = MEMBUF(buf, sizeof buf); - assert(id > 0); - if (fnname) - bfmt(&wbuf, "%s.%s.%d", fnname, name, id-1); - else - bfmt(&wbuf, "%s.%d", name, id-1); - ioputc(&wbuf, 0); - assert(!wbuf.err); - 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); - -union ref -expraddr(struct function *fn, const struct expr *ex) -{ - struct decl *decl; - union ref r; - - switch (ex->t) { - case ESYM: - decl = &declsbuf.p[ex->decl]; - assert(decl != NULL); - switch (decl->scls) { - case SCAUTO: case SCREGISTER: - assert(decl->id >= 0); - return mkref(RTMP, decl->id); - case SCEXTERN: case SCNONE: case SCSTATIC: - return mksymref(decl->sym, (SFUNC & -(decl->ty.t == TYFUNC)) | (SLOCAL & -(decl->scls == SCSTATIC || decl->isdef))); - default: - assert(0); - } - break; - case ESSYMREF: - return irbinop(fn, Oadd, KPTR, - mksymref(ex->ssym.sym, (SFUNC & -ex->ssym.func) | (SLOCAL & -ex->ssym.local)), - mkintcon(KPTR, ex->ssym.off)); - case ESTRLIT: - /* XXX endian for wide strs */ - return mkdatref(NULL, ex->ty, typesize(ex->ty), typealign(ex->ty), ex->s.p, ex->s.n * typesize(typechild(ex->ty)), /*deref*/0, fn != NULL); - case EDEREF: - return exprvalue(fn, ex->sub); - case EGETF: - r = expraddr(fn, ex->sub); - assert(ex->fld.bitsiz == 0); - return irbinop(fn, Oadd, KPTR, r, mkintcon(KI32, ex->fld.off)); - case ESET: - assert(isagg(ex->ty)); - r = expraddr(fn, &ex->sub[1]); - structcopy(fn, ex->ty, expraddr(fn, &ex->sub[0]), r); - return r; - case ECALL: - assert(isagg(ex->ty)); - return compilecall(fn, ex); - case EVAARG: - assert(isagg(ex->ty)); - return builtin_va_arg_comp(fn, ex, 0); - case EINIT: - if (fn) { - /* compound literal, allocate temp */ - r = addinstr(fn, mkalloca(typesize(ex->ty), typealign(ex->ty))); - geninit(fn, ex->ty, r, ex); - return r; - } else { - /* emit static dat */ - static int id; - struct initparser ip[1] = {0}; - union 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)) - error(&ex->span, "cannot not evaluate expression statically"); - return mksymref(sym, 0); - } - case ESEQ: - expreffects(fn, &ex->sub[0]); - return expraddr(fn, &ex->sub[1]); - case ECOND: - assert(isagg(ex->ty)); - return condexprvalue(fn, ex, 0); - default: - assert(!"lvalue?>"); - } - -} - -static union ref -genload(struct function *fn, union type t, union ref ref, bool volatyl) -{ - struct instr ins = {0}; - - assert(isscalar(t)); - ins.cls = type2cls[scalartypet(t)]; - assert(ins.cls); - switch (typesize(t)) { - case 1: ins.op = issigned(t) ? Oloads8 : Oloadu8; break; - case 2: ins.op = issigned(t) ? Oloads16 : Oloadu16; break; - case 4: ins.op = isflt(t) ? Oloadf32 : issigned(t) ? Oloads32 : Oloadu32; break; - case 8: ins.op = isflt(t) ? Oloadf64 : Oloadi64; break; - default: assert(0); - } - ins.l = ref; - ins.keep = volatyl; - return addinstr(fn, ins); -} - -static union ref -genstore(struct function *fn, union type t, union ref ptr, union ref val) -{ - struct instr ins = {0}; - - assert(isscalar(t)); - switch (typesize(t)) { - case 1: ins.op = Ostorei8; break; - case 2: ins.op = Ostorei16; break; - case 4: ins.op = isflt(t) ? Ostoref32 : Ostorei32; break; - case 8: ins.op = isflt(t) ? Ostoref64 : Ostorei64; break; - default: assert(0); - } - ins.l = ptr; - ins.r = 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 -geninit(struct function *fn, union type t, union ref dst, const struct expr *src) -{ - union ref adr; - if (src->t == EINIT) { - struct init *ini = src->init; - uint siz = typesize(t); - uint align = typealign(t); - struct bitset azero[1] = {0}; - - if (BSSIZE(siz) <= countof(ini->zero)) { - for (int i = 0; i < siz; i += align) { - for (int j = 0; j < align; ++j) { - if (bstest(ini->zero, i + j)) { - bsset(azero, i); - break; - } - } - } - if (bscount(azero, countof(azero)) < 32) { - /* write individual zeros at non initialized gaps */ - for (uint i = 0; bsiter(&i, azero, countof(azero)) && i < siz; i += align) { - adr = irbinop(fn, Oadd, KPTR, dst, mkref(RICON, i)); - addinstr(fn, mkinstr(Ostorei8 + ilog2(align), 0, .l = adr, .r = ZEROREF)); - } - } else { - goto Memset0; - } - } else Memset0: { - /* memset(dst,0,siz) */ - /* TODO make it into an intrinsic */ - struct 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))); - call.l = mksymref(istr_memset, 1); - call.r = mkcallarg(cls2type(KPTR), 3, -1); - addinstr(fn, call); - } - for (struct initval *val = ini->vals; val; val = val->next) { - uint off = val->off; - struct 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); - } else if (isagg(ex->ty)) { - structcopy(fn, ex->ty, adr, expraddr(fn, ex)); - } 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); - } - } - } else if (src->t == ESTRLIT) { - union type ctyp = typechild(src->ty); - uint csiz = typesize(ctyp); - adr = dst; - for (uint i = 0; i < src->s.n; ++i) { - if (csiz == 1) - genstore(fn, ctyp, adr, mkref(RICON, src->s.p[i])); - else if (csiz == 2) - genstore(fn, ctyp, adr, mkref(RICON, src->s.w16[i])); - else - genstore(fn, ctyp, adr, mkintcon(KI32, src->s.w32[i])); - adr = irbinop(fn, Oadd, KPTR, dst, mkref(RICON, (i+1)*csiz)); - } - genstore(fn, ctyp, adr, ZEROREF); /* null term */ - } else assert(0); -} - -static bool -isboollike(struct function *fn, union ref r) -{ - struct 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; - for (blk = fn->curblk; phi == NULL; blk = blk->lprev) { - /* find blk that defines phi */ - assert(blk != fn->entry); - for (int i = 0; i < blk->phi.n; ++i){ - if (blk->phi.p[i] == r.i) { - phi = phitab.p[ins->l.i]; - break; - } - } - } - for (int i = 0; i < blk->npred; ++i) { - if (!isboollike(fn, phi[i])) { - return 0; - } - } - return 1; - } - if (in_range(ins->op, Oand, Oxor)) - return isboollike(fn, ins->l) && isboollike(fn, ins->r); - if (ins->op == Ocopy || in_range(ins->op, Oexts8, Oextu32)) - return isboollike(fn, ins->l); - if (ins->op == Oparam) - return typedata[fn->fnty.dat].param[ins->l.i].t == TYBOOL; - return 0; -} - -union ref -scalarcvt(struct function *fn, union type to, union type from, union ref ref) -{ - enum irclass kto = type2cls[scalartypet(to)], kfrom = type2cls[scalartypet(from)]; - enum op op; - if (to.bits == from.bits) return ref; - assert(kto && kfrom); - if (kto == kfrom && to.t != TYBOOL) return ref; - - if (kisflt(kto) || kisflt(kfrom)) { - if (ref.t == RICON) { - assert(kisflt(kto) && kisint(kfrom)); - return mkfltcon(kto, kto == KF32 ? (float)ref.i : (double)ref.i); - } - if (kisflt(kto) && kfrom == KI32) op = issigned(from) ? Ocvts32f : Ocvtu32f; - else if (to.t == TYBOOL && kisflt(kfrom)) return irbinop(fn, Oneq, kfrom, ref, mkfltcon(kfrom, 0.0)); - else if (kisflt(kto) && kfrom == KI64) op = issigned(from) ? Ocvts64f : Ocvtu64f; - else if (kto == KF64 && kfrom == KF32) op = Ocvtf32f64; - else if (kto == KF32 && kfrom == KF64) op = Ocvtf64f32; - else if (kfrom == KF32) op = issigned(to) ? Ocvtf32s : Ocvtf32u; - else if (kfrom == KF64) op = issigned(to) ? Ocvtf64s : Ocvtf64u; - else assert(0); - } else { - if (to.t == TYBOOL) { - if (from.t == TYBOOL) return ref; - if (isboollike(fn, ref)) - return kfrom == KI32 ? ref : scalarcvt(fn, mktype(TYINT), from, ref); - return irbinop(fn, Oneq, kfrom, ref, ZEROREF); - } - else if (kfrom == KI32 && issigned(from)) op = Oexts32; - else if (kfrom == KI32) op = Oextu32; - else if (kto == KI32 && isintcon(ref)) - return issigned(to) ? mkintcon(kto, (int)intconval(ref)) : mkintcon(kto, (uint)intconval(ref)); - else op = Ocopy; - } - return irunop(fn, op, kto, ref); -} - -static union ref -narrow(struct function *fn, enum irclass to, union type t, union ref ref, uint bitsiz) -{ - enum typetag tt = scalartypet(t); - assert(isscalar(t)); - if (targ_primsizes[tt] < cls2siz[to]) { - enum op op; - if (isfltt(tt)) { - assert(to == KF32 && tt >= TYDOUBLE); - op = Ocvtf64f32; - } else { - static const enum op ext[5][2] = { - [1] = {Oextu8, Oexts8}, [2] = {Oextu16, Oexts16}, [4] = {Oextu32, Oexts32} - }; - op = ext[targ_primsizes[tt]][issignedt(tt)]; - } - ref = irunop(fn, op, to, ref); - } - if (bitsiz) { - assert(kisint(to) && isintt(tt) && bitsiz < 8*targ_primsizes[tt]); - if (!issignedt(tt)) { - ref = irbinop(fn, Oand, to, ref, mkintcon(to, (1ull< 0); - a = irbinop(fn, Osub, cls, a, b); - if (siz == 1) return a; - else if (ispo2(siz)) - return irbinop(fn, Osar, cls, a, mkintcon(cls, ilog2(siz))); - else - return irbinop(fn, Odiv, cls, a, mkintcon(cls, siz)); -} - -/* 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) -{ - struct block *next, *next2; -Recur: - for (; ex->t == ESEQ; ex = &ex->sub[1]) - expreffects(fn, &ex->sub[0]); - if (ex->t == ELOGAND) { - next = newblk(fn); - condjump(fn, &ex->sub[0], next, fl); - useblk(fn, next); - ex = &ex->sub[1]; - goto Recur; - } else if (ex->t == ELOGIOR) { - next = newblk(fn); - condjump(fn, &ex->sub[0], tr, next); - useblk(fn, next); - ex = &ex->sub[1]; - goto Recur; - } else if (ex->t == ECOND) { - next = newblk(fn); - next2 = newblk(fn); - condjump(fn, &ex->sub[0], next, next2); - useblk(fn, next); - condjump(fn, &ex->sub[1], tr, fl); - useblk(fn, next2); - condjump(fn, &ex->sub[2], tr, fl); - } else if (ex->t == ELOGNOT) { - Negate: - /* swap tr,fl */ - next = tr; - tr = fl; - fl = next; - ex = &ex->sub[0]; - goto Recur; - } else if (ex->t == EEQU && isnullpo(&ex->sub[1])) { /* == 0 */ - goto Negate; - } else if (ex->t == ENEQ && isnullpo(&ex->sub[1])) { /* != 0 */ - ex = &ex->sub[0]; - goto Recur; - } else { - putcondbranch(fn, exprvalue(fn, ex), tr, fl); - } -} - -struct condphis { - union type typ; - vec_of(union ref) ref; -}; - -static void -condexprrec(struct function *fn, const struct expr *ex, struct condphis *phis, struct 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); - 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)); - } - useblk(fn, tr); - ex = &ex->sub[1]; - goto Recur; - } else if (ex->t == ELOGIOR) { - struct 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)); - } - useblk(fn, fl); - ex = &ex->sub[1]; - goto Recur; - } else if (ex->t == ECOND) { - struct block *tr = newblk(fn), *fl = newblk(fn); - condjump(fn, &ex->sub[0], tr, fl); - useblk(fn, tr); - condexprrec(fn, &ex->sub[1], phis, end); - useblk(fn, fl); - ex = &ex->sub[2]; - goto Recur; - } else { - if (!phis) { - 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); - } - putbranch(fn, end); - } -} - -/* 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) -{ - 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); - useblk(fn, dst); - if (discard) return NOREF; - enum irclass k; - if (isscalar(ex->ty)) { - k = type2cls[scalartypet(ex->ty)]; - assert(k); - } else { - assert(isagg(ex->ty) || isptrcvt(ex->ty)); - k = KPTR; - } - union ref r = addphi(fn, k, phis.ref.p); - vfree(&phis.ref); - return r; -} - -static union ref -compilecall(struct function *fn, const struct 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)); - - if (sub[0].t == ESYM && declsbuf.p[sub[0].decl].isbuiltin) { - return declsbuf.p[sub[0].decl].builtin->comp(fn, (struct expr *)ex, 0); - } - ins.op = Ocall; - if (isagg(ex->ty)) { - ins.cls = KPTR; - } else { - assert(isscalar(ex->ty) || ex->ty.t == TYVOID); - ins.cls = type2cls[scalartypet(ex->ty)]; - assert(ins.cls || ex->ty.t == TYVOID); - } - 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)); - 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); - 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) -{ - enum irclass k = type2cls[scalartypet(ty)]; - uint off = fld->off, bitsiz = fld->bitsiz, bitoff = fld->bitoff; - union ref tmp; - uvlong mask; - - assert(k); - *addr = irbinop(fn, Oadd, KPTR, *addr, mkintcon(KI32, off)); - tmp = genload(fn, ty, *addr, volatyl); - if (tmpval) *tmpval = tmp; - if (!issigned(ty)) { - /* shift right and mask */ - tmp = irbinop(fn, Oslr, k, tmp, mkref(RICON, bitoff)); - if (bitsiz < 8*typesize(ty)) { - mask = bitsiz == 64 ? -1ull : (1ull << bitsiz) - 1; - tmp = irbinop(fn, Oand, k, tmp, mkintcon(k, mask)); - } - } else { - /* shift left and shift right arithmetic to propagate sign bit */ - int sh = 8*cls2siz[k] - bitsiz - bitoff; - tmp = irbinop(fn, Oshl, k, tmp, mkref(RICON, sh)); - sh += bitoff; - tmp = irbinop(fn, Osar, k, tmp, mkref(RICON, sh)); - } - return tmp; -} - -static void -genbitfstore(struct function *fn, const union type ty, union ref addr, - const struct exgetfld *fld, union ref tmp, union ref val) -{ - enum irclass k = type2cls[scalartypet(ty)]; - uint off = fld->off, bitsiz = fld->bitsiz, bitoff = fld->bitoff; - uint bittypesize = 8*typesize(ty); - uvlong mask; - - assert(k); - if (!tmp.bits) { - addr = irbinop(fn, Oadd, KPTR, addr, mkintcon(KPTR, off)); - tmp = genload(fn, ty, addr, 0); - } - mask = (bitsiz == 64 ? -1ull : (1ull << bitsiz) - 1) << bitoff; - - /* mask out bits in existing container */ - tmp = irbinop(fn, Oand, k, tmp, mkintcon(k, ~mask)); - - /* shift and mask source value */ - if (isintcon(val)) { - val = mkintcon(k, ((uvlong)intconval(val) << bitoff) & mask); - } else { - val = irbinop(fn, Oshl, k, val, mkref(RICON, bitoff)); - if (bitsiz < bittypesize) - val = irbinop(fn, Oand, k, val, mkintcon(k, mask)); - } - /* combine and write */ - if (bitsiz < bittypesize) - val = irbinop(fn, Oior, k, tmp, val); - genstore(fn, ty, addr, val); -} - -static bool -knowntruthy(bool *t, struct expr *ex) -{ - if (!eval(ex, EVFOLD)) return 0; - - switch (ex->t) { - default: assert(0 && "!scalar?"); - case ENUMLIT: - *t = isflt(ex->ty) ? ex->f != 0.0 : ex->u != 0; - break; - case ESTRLIT: case ESSYMREF: - /* string literals & symbol addresses are always truthy */ - *t = 1; - break; - } - return 1; -} - -union ref -compileexpr(struct function *fn, const struct expr *ex, bool discard) -{ - union type ty; - union ref l, r, q, adr; - uint bitsiz; - enum op op; - enum irclass cls = type2cls[scalartypet(ex->ty)]; - int swp = 0; - struct expr *sub; - - //eval((struct expr *)ex, EVFOLD); - sub = ex->sub; - - if (ex->ty.t != TYVOID && !isscalar(ex->ty)) { - /* fn & array designators evaluate to their address; - * so do aggregates for the purpose of code generation */ - if (isagg(ex->ty) && isincomplete(ex->ty)) - error(&ex->span, "use of incomplete type '%ty'", ex->ty); - return expraddr(fn, ex); - } - switch (ex->t) { - case ENUMLIT: - if (discard) return NOREF; - if (isflt(ex->ty)) - return mkfltcon(cls, ex->f); - return mkintcon(cls, ex->i); - case ESYM: - if (discard && !(ex->qual & QVOLATILE)) return NOREF; - return genload(fn, ex->ty, expraddr(fn, ex), ex->qual & QVOLATILE); - case ESSYMREF: - return expraddr(fn, ex); - case EVAARG: - return builtin_va_arg_comp(fn, ex, discard); - case EGETF: - if (discard && !(ex->qual & QVOLATILE)) return NOREF; - if (ex->fld.bitsiz) { - /* bit-field */ - r = expraddr(fn, ex->sub); - return genbitfload(fn, NULL, ex->ty, &r, &ex->fld, ex->qual & QVOLATILE); - } - return genload(fn, ex->ty, expraddr(fn, ex), ex->qual & QVOLATILE); - case ECAST: - if (ex->ty.t == TYVOID) { - expreffects(fn, sub); - return NOREF; - } - /* fallthru */ - case EPLUS: - r = compileexpr(fn, sub, discard); - if (discard) return NOREF; - r = scalarcvt(fn, ex->ty, sub->ty, r); - if (isint(ex->ty) && (typesize(ex->ty) < typesize(sub->ty) || issigned(ex->ty) != issigned(sub->ty))) - return narrow(fn, type2cls[scalartypet(ex->ty)], ex->ty, r, 0); - return r; - case ENEG: - op = Oneg; - goto Unary; - case ECOMPL: - op = Onot; - Unary: - l = compileexpr(fn, sub, discard); - if (discard) return NOREF; - l = scalarcvt(fn, ex->ty, sub->ty, l); - return irunop(fn, op, cls, l); - case ELOGNOT: - for (; sub->t == ELOGNOT; ex = sub, sub = sub->sub) - swp ^= 1; - op = Oequ + swp; - l = compileexpr(fn, sub, discard); - if (discard) return NOREF; - l = scalarcvt(fn, ex->ty, sub->ty, l); - r = mkintcon(cls, 0); - return irbinop(fn, op, cls, l, r); - case EDEREF: - discard &= (ex->qual & QVOLATILE) == 0; - r = compileexpr(fn, sub, discard); - if (discard) return NOREF; - return genload(fn, ex->ty, r, ex->qual & QVOLATILE); - case EADDROF: - return expraddr(fn, sub); - case EMUL: - op = Omul; - goto BinArith; - case EDIV: - op = isunsigned(ex->ty) ? Oudiv : Odiv; - goto BinArith; - case EREM: - op = issigned(ex->ty) ? Orem : Ourem; - goto BinArith; - case EBAND: - op = Oand; - goto BinArith; - case EXOR: - op = Oxor; - goto BinArith; - case EBIOR: - op = Oior; - goto BinArith; - case ESHL: - op = Oshl; - goto BinArith; - case ESHR: - op = issigned(ex->ty) ? Osar : Oslr; - goto BinArith; - case ESUB: - op = Osub; - goto BinArith; - case EADD: - op = Oadd; - BinArith: - l = compileexpr(fn, &sub[0], discard); - r = compileexpr(fn, &sub[1], discard); - if (discard) return NOREF; - if (op == Osub && isptrcvt(sub[0].ty) && isptrcvt(sub[1].ty)) { - /* ptr - ptr */ - return genptrdiff(fn, typesize(typechild(sub[0].ty)), l, r); - } else if ((op != Oadd && op != Osub) || cls != KPTR) { - /* num OP num */ - l = scalarcvt(fn, ex->ty, sub[0].ty, l); - r = scalarcvt(fn, ex->ty, sub[1].ty, r); - } else { - assert(isptrcvt(sub[0].ty)); - /* ptr +/- num */ - return genptroff(fn, op, typesize(typechild(sub[0].ty)), l, sub[1].ty, r); - } - return irbinop(fn, op, cls, l, r); - case EPOSTINC: - case EPOSTDEC: - op = ex->t == EPOSTINC ? Oadd : Osub; - if (ex->ty.t == TYPTR) - r = mkintcon(type2cls[targ_sizetype], typesize(typechild(ex->ty))); - else - 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; - 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); - genbitfstore(fn, sub[0].ty, adr, &sub[0].fld, tmp, q); - } else { - adr = expraddr(fn, sub); - l = genload(fn, sub->ty, adr, sub->qual & QVOLATILE); - q = irbinop(fn, op, cls, l, r); - genstore(fn, sub->ty, adr, q); - } - return discard ? NOREF : l; - case EPREINC: - case EPREDEC: - op = ex->t == EPREINC ? Oadd : Osub; - if (ex->ty.t == TYPTR) - r = mkintcon(type2cls[targ_sizetype], typesize(typechild(ex->ty))); - else - r = isflt(ex->ty) ? mkfltcon(type2cls[ex->ty.t], 1.0) : mkref(RICON, 1); - if (sub[0].t == EGETF && (bitsiz = sub->fld.bitsiz)) { - ty = ex->ty; - goto CompoundBitf; - } - adr = expraddr(fn, sub); - l = genload(fn, sub->ty, adr, sub->qual & QVOLATILE); - q = irbinop(fn, op, cls, l, r); - genstore(fn, sub->ty, adr, q); - if (discard) return NOREF; - return narrow(fn, cls, ex->ty, q, 0); - case EEQU: - op = Oequ; - goto Cmp; - case ENEQ: - op = Oneq; - goto Cmp; - case ELTH: - op = Olth; - goto Cmp; - case ELTE: - op = Olte; - goto Cmp; - case EGTH: - op = Ogth; - goto Cmp; - case EGTE: - op = Ogte; - Cmp: - ty = cvtarith(sub[0].ty, sub[1].ty); - if (!ty.t) ty.t = TYPTR; - if (isunsigned(ty) && in_range(op, Olth, Ogte)) - op += Oulth - Olth; - l = compileexpr(fn, &sub[0], discard); - r = compileexpr(fn, &sub[1], discard); - if (discard) return NOREF; - l = scalarcvt(fn, ty, sub[0].ty, l); - r = scalarcvt(fn, ty, sub[1].ty, r); - cls = type2cls[ty.t]; - return irbinop(fn, op, cls, l, r); - case ESET: - assert(isscalar(ex->ty)); - q = scalarcvt(fn, sub[0].ty, sub[1].ty, exprvalue(fn, &sub[1])); - if (sub[0].t == EGETF && (bitsiz = sub[0].fld.bitsiz)) { - /* bit-field */ - adr = expraddr(fn, &sub[0].sub[0]); - genbitfstore(fn, ex->ty, adr, &sub[0].fld, NOREF, q); - } else { - bitsiz = 0; - adr = expraddr(fn, &sub[0]); - genstore(fn, ex->ty, adr, q); - } - if (discard) return NOREF; - return bitsiz ? narrow(fn, cls, sub[0].ty, q, bitsiz) : q; - case ESETMUL: - op = Omul; - goto Compound; - case ESETDIV: - op = isunsigned(ex->ty) ? Oudiv : Odiv; - goto Compound; - case ESETREM: - op = issigned(ex->ty) ? Orem : Ourem; - goto Compound; - case ESETAND: - op = Oand; - goto Compound; - case ESETXOR: - op = Oxor; - goto Compound; - case ESETIOR: - op = Oior; - goto Compound; - case ESETSHL: - op = Oshl; - goto Compound; - case ESETSHR: - op = issigned(ex->ty) ? Osar : Oslr; - goto Compound; - case ESETSUB: - op = Osub; - goto Compound; - case ESETADD: - op = Oadd; - Compound: - ty = in_range(ex->t, ESETSHL, ESETSHR) ? mktype(intpromote(ex->ty.t)) - : cvtarith(sub[0].ty, sub[1].ty); - r = exprvalue(fn, &sub[1]); - if (sub[0].t == EGETF && (bitsiz = sub[0].fld.bitsiz)) { - /* bit-field */ - union 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); - q = irbinop(fn, op, cls, l, r); - genbitfstore(fn, sub[0].ty, adr, &sub[0].fld, tmp, q); - } else { - bitsiz = 0; - adr = expraddr(fn, &sub[0]); - l = genload(fn, ex->ty, adr, ex->qual & QVOLATILE); - if ((op != Oadd && op != Osub) || cls != KPTR) { - l = scalarcvt(fn, ty, sub[0].ty, l); - r = scalarcvt(fn, ty, sub[1].ty, r); - q = irbinop(fn, op, type2cls[ty.t], l, r); - q = scalarcvt(fn, ex->ty, ty, q); - } else { - q = genptroff(fn, op, typesize(typechild(ex->ty)), l, sub[1].ty, r); - } - genstore(fn, ex->ty, adr, q); - } - if (discard) return NOREF; - return bitsiz ? narrow(fn, cls, ex->ty, q, bitsiz) : q; - case ECALL: - r = compilecall(fn, ex); - if (isint(ex->ty)) - return narrow(fn, cls, ex->ty, r, 0); - return r; - case ECOND: - for (bool c; knowntruthy(&c, &ex->sub[0]);) { - r = compileexpr(fn, &ex->sub[2-c], discard); - if (discard) return NOREF; - return scalarcvt(fn, ex->ty, ex->sub[2-c].ty, r); - } - - if (ex->ty.t == TYVOID || discard) { - struct block *tr, *fl, *end; - condjump(fn, &sub[0], tr = newblk(fn), fl = newblk(fn)); - useblk(fn, tr); - expreffects(fn, &sub[1]); - end = newblk(fn); - if (fn->curblk) - putbranch(fn, end); - useblk(fn, fl); - expreffects(fn, &sub[2]); - if (fn->curblk) - putbranch(fn, end); - useblk(fn, end); - return NOREF; - } - return condexprvalue(fn, ex, discard); - case ELOGAND: - case ELOGIOR: - for (bool c; knowntruthy(&c, &ex->sub[0]);) { - c ^= ex->t == ELOGIOR; - r = compileexpr(fn, &ex->sub[c], discard); - if (discard) return NOREF; - return scalarcvt(fn, mktype(TYBOOL), ex->sub[c].ty, r); - } - return condexprvalue(fn, ex, discard); - case ESEQ: - expreffects(fn, &sub[0]); - return compileexpr(fn, &sub[1], discard); - default: assert(!"nyi expr"); - } -} - -/************************************/ -/* Statements parsing & compilation */ -/************************************/ - -static void -stmtterm(struct comp *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); - -struct label { - struct label *link; - internstr name; - struct block *blk; - struct 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) -{ - for (struct 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) -{ - struct 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; - if (!nerror) { - new = newblk(fn); - if (fn->curblk) putbranch(fn, new); - } - /* fix up relocations */ - for (struct 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->blk = new; - if (!nerror) useblk(fn, new); - } else { - struct label l = { cm->labels, name }; - if (!nerror) { - struct block *new = newblk(fn); - if (fn->curblk) putbranch(fn, new); - useblk(fn, new); - } - l.blk = fn->curblk; - cm->labels = alloccopy(fn->arena, &l, sizeof l, 0); - } -} - -static bool -loopbody(struct comp *cm, struct function *fn, struct block *brk, struct block *cont) -{ - struct block *save[2]; - bool terminates = 0; - - save[0] = cm->breakto, save[1] = cm->loopcont; - cm->breakto = brk, cm->loopcont = cont; - ++cm->loopdepth; - - terminates = stmt(cm, fn); - - --cm->loopdepth; - cm->breakto = save[0], cm->loopcont = save[1]; - - return terminates; -} - -#define EMITS if (doemit && !nerror) - -struct swcase { - vlong val; - struct block *blk; - struct span span; -}; -struct switchstmt { - struct block *bdefault; - union type condtype; - vec_of(struct swcase) cases; -}; - -static int -cmpswcase(const void *aa, const void *bb) -{ - const struct swcase *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) -{ - 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) -{ - union ref sel; - bool doemit = fn->curblk; - struct block *begin = NULL, *end = NULL, *breaksave = cm->breakto; - struct switchstmt *stsave = cm->switchstmt, st = {.condtype = ex->ty}; - enum irclass k = type2cls[scalartypet(ex->ty)]; - struct swcase casebuf[8]; - vinit(&st.cases, casebuf, countof(casebuf)); - - assert(k); - end = newblk(fn); - EMITS { - sel = exprvalue(fn, ex); - assert(isint(ex->ty)); - } - cm->switchstmt = &st; - cm->breakto = end; - begin = fn->curblk; - fn->curblk = NULL; - ++cm->switchdepth; - stmt(cm, fn); - --cm->switchdepth; - doemit = fn->curblk; - cm->switchstmt = stsave; - cm->breakto = breaksave; - - EMITS putbranch(fn, end); - useblk(fn, begin); - swsortcases(st.cases.p, st.cases.n); - doemit = 1; - if (!st.bdefault) st.bdefault = end; - /* TODO: optimize instead of generating the equivalent of if == .. else if .. chain - * XX 1. sort by case values (also for easy duplicates checking) - * 2. contiguous ranges (case a..b: -> x >= && x <= b) - * 3. binary search - * 4. jump tables? (harder, backend refactoring) - */ - vlong prev; - for (int i = 0; i < st.cases.n; ++i) { - const struct swcase *c = &st.cases.p[i]; - if (i > 0) { - assert(c->val >= prev); - if (c->val == prev) { - error(&c->span, "duplicate case value"); - note(&c[-1].span, "previously defined here"); - } - } - EMITS { - struct 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); - } - prev = c->val; - } - vfree(&st.cases); - if (fn->curblk != end) { - if (fn->curblk) EMITS putbranch(fn, end); - if (end->npred > 0) { - useblk(fn, end); - } else { - fn->curblk = NULL; - freeblk(fn, end); - } - } - - return fn->curblk == NULL; -} - -static bool /* return 1 if stmt is terminating (ends with a jump) */ -stmt(struct comp *cm, struct function *fn) -{ - struct 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; - bool terminates = 0; - bool doemit = fn->curblk; - - while (match(cm, &tk, TKIDENT) || match(cm, &tk, TKWcase) || match(cm, &tk, TKWdefault)) { - if (tk.t == TKWcase) { - /* case ':' */ - if (!cm->switchstmt) error(&tk.span, "'case' outside of switch statement"); - ex = constantexpr(cm); - 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; - ex = mkexpr(ECAST, ex.span, cm->switchstmt->condtype, .sub = &tmp); - bool ok = eval(&ex, EVINTCONST); - assert(ok && "cast const int?"); - if (ex.i != tmp.i) - warn(&ex.span, "overflow converting case value to switch condition type"); - } - expect(cm, ':', NULL); - if (!fn->curblk || (fn->curblk->phi.n > 0 || fn->curblk->ins.n > 0)) { - begin = newblk(fn); - EMITS putbranch(fn, begin); - useblk(fn, begin); - } - if (cm->switchstmt) - vpush(&cm->switchstmt->cases, ((struct swcase) {ex.i, fn->curblk, ex.span})); - } else if (tk.t == TKWdefault) { - /* default ':' */ - if (!cm->switchstmt) error(&tk.span, "'default' outside of switch statement"); - expect(cm, ':', NULL); - if (!fn->curblk || (fn->curblk->phi.n > 0 || fn->curblk->ins.n > 0)) { - begin = newblk(fn); - EMITS putbranch(fn, begin); - useblk(fn, begin); - } - if (cm->switchstmt) { - if (cm->switchstmt->bdefault) error(&tk.span, "multiple 'default' labels in one switch"); - cm->switchstmt->bdefault = fn->curblk; - } - } else if (tk.t == TKIDENT && match(cm, NULL, ':')) { - /*