diff options
| author | 2022-08-26 22:08:52 +0200 | |
|---|---|---|
| committer | 2022-08-26 22:11:30 +0200 | |
| commit | 4a7d610bb9a441f6fb7f88caaa803c1c85e4fcfd (patch) | |
| tree | 3ac01c9be375f30efd09f0a670da74f886ad44ca /src | |
| parent | 475125eaba2852f88a4ee8d9be64c8b4964df3a9 (diff) | |
bitfields
Diffstat (limited to 'src')
| -rw-r--r-- | src/cffc.hff | 18 | ||||
| -rw-r--r-- | src/fmt.cff | 6 | ||||
| -rw-r--r-- | src/llvm.cff | 92 | ||||
| -rw-r--r-- | src/parse.cff | 172 | ||||
| -rw-r--r-- | src/type.cff | 6 |
5 files changed, 242 insertions, 52 deletions
diff --git a/src/cffc.hff b/src/cffc.hff index d1d423f..066c1f7 100644 --- a/src/cffc.hff +++ b/src/cffc.hff @@ -19,8 +19,8 @@ struct Loc { #[lax] enum TokT : i32 { // !sorted - kw_alignof, kw_and, kw_as, kw_break, kw_case, kw_const, - kw_continue, kw_def, kw_defer, kw_defmacro, kw_do, + kw_alignof, kw_and, kw_as, kw_bitfield, kw_break, kw_case, + kw_const, kw_continue, kw_def, kw_defer, kw_defmacro, kw_do, kw_else, kw_enum, kw_extern, kw_fn, kw_for, kw_if, kw_import, kw_let, kw_offsetof, kw_or, kw_return, kw_sizeof, kw_static, @@ -71,6 +71,12 @@ struct AggField { struct EnumVal { name *const u8, i i64 } +struct BitFField { + name *const u8, + ty *const Type, + off u16, size u16 +} + enum union TeplArg; struct Type { @@ -108,6 +114,12 @@ struct Type { flds [#]AggField, decls *Env, }, + BitF struct { + intty *const Type, + name *const u8, + id int, + flds [#]BitFField, + }, VaList, }, @@ -178,9 +190,11 @@ struct Expr { Cond struct { test *Expr, t *Expr, f *Expr }, Cast *Expr, Dot struct { lhs *Expr, fld *const AggField }, + BitDot struct { lhs *Expr, fld *const BitFField }, SLen *Expr, SPtr *Expr, EUTag *Expr, + BitRaw *Expr, Index struct { lhs *Expr, rhs *Expr }, Slice struct { lhs *Expr, begin *Expr, end *Expr }, Call struct { lhs *Expr, args [#]Expr }, diff --git a/src/fmt.cff b/src/fmt.cff index 8c0f6a4..6707222 100644 --- a/src/fmt.cff +++ b/src/fmt.cff @@ -198,6 +198,12 @@ extern fn vpfmt(proc *fn(u8, *void) void, parg *void, fmt *const u8, ap va_list) } else { ps("(anonymous)"); } + case BitF b; + if b.name { + ps(b.name); + } else { + ps("(anonymous)"); + } } } diff --git a/src/llvm.cff b/src/llvm.cff index 8ff32ba..32b1066 100644 --- a/src/llvm.cff +++ b/src/llvm.cff @@ -96,6 +96,7 @@ fn gen(fmt *const u8, ...) void { case Agg; genagg(ty); case Enum enu; pritype(enu.intty); + case BitF bitf; pritype(bitf.intty); case Fn f; gen("%t(", f.ret); foreach(ty, i, f.params) { @@ -202,6 +203,16 @@ fn mktmp(ty *const Type) Value { return Value{ty, :Tmp(tmpid++)}; } +defmacro gentmp(type, ...rest) [ + (do + let $tmp = mktmp(type); + gen("\t%v = ", $tmp); + gen(rest); + gen("\n"); + $tmp; + ) +] + fn convert(f *Fn, to *const Type, ex *Expr) Value; fn genexpr(f *Fn, ex *Expr) Value; static arena Arena = {}; @@ -235,20 +246,18 @@ fn genaddr(f *Fn, ex *Expr) Value { switch idx.lhs.ty.u { case Arr arrty; let arr = genaddr(f, idx.lhs); - lhs = mktmp(mkptrtype(ex.ty)); - gen("\t%v = getelementptr %t, %t %v, i32 0, i32 0\n", lhs, idx.lhs.ty, arr.ty, arr); + lhs = gentmp(mkptrtype(ex.ty), "getelementptr %t, %t %v, i32 0, i32 0", idx.lhs.ty, arr.ty, arr); case Ptr; lhs = genexpr(f, idx.lhs); case Slice child; let tmp = genexpr(f, idx.lhs); - lhs = mktmp(mkptrtype(child)); - gen("\t%v = extractvalue %t %v, 0\n", lhs, tmp.ty, tmp); + lhs = gentmp(mkptrtype(child), "extractvalue %t %v, 0", tmp.ty, tmp); case else; assert(#f, "index"); } - let rhs = genexpr(f, idx.rhs), - addr = mktmp(mkptrtype(ex.ty)); - gen("\t%v = getelementptr %t, %t %v, %t %v\n", addr, ex.ty, lhs.ty, lhs, rhs.ty, rhs); + let rhs = genexpr(f, idx.rhs); + let addr = gentmp(mkptrtype(ex.ty), "getelementptr %t, %t %v, %t %v", + ex.ty, lhs.ty, lhs, rhs.ty, rhs); return addr; case Dot dot; @@ -262,27 +271,30 @@ fn genaddr(f *Fn, ex *Expr) Value { } else { idx = dot.fld - dot.lhs.ty.u.Agg.flds.#ptr; } - let addr = mktmp(mkptrtype(ex.ty)); + let addr Value #?; + let ptrty = mkptrtype(ex.ty); switch lhs.ty.u.Ptr.u.Agg.kind { case :Struct; - gen("\t%v = getelementptr %t, %t %v, i32 0, i32 %d\n", addr, lhs.ty.u.Ptr, lhs.ty, lhs, idx); + addr = gentmp(ptrty, "getelementptr %t, %t %v, i32 0, i32 %d", + lhs.ty.u.Ptr, lhs.ty, lhs, idx); case :Union; - gen("\t%v = bitcast %t %v to %t", addr, lhs.ty, lhs, addr.ty); + addr = gentmp(ptrty, "bitcast %t %v to %t", lhs.ty, lhs, ptrty); case else let off int = dot.fld.off; - gen("\t%v = getelementptr i8, %t %v, i32 %d\n", addr, lhs.ty, lhs, off); + addr = gentmp(ptrty, "getelementptr i8, %t %v, i32 %d", lhs.ty, lhs, off); } return addr; case EUTag eex; let lhs = eex.ty->is(:Ptr) ? genexpr(f, eex) : genaddr(f, eex); - let addr = mktmp(mkptrtype(ex.ty)); - gen("\t%v = bitcast %t %v to %t", addr, lhs.ty, lhs, addr.ty); + let addr = gentmp(mkptrtype(ex.ty), "bitcast %t %v to %t", lhs.ty, lhs, mkptrtype(ex.ty)); return addr; + case BitRaw ex; + return genaddr(f, ex); + case AggIni ini; - let tmp = mktmp(mkptrtype(ex.ty)); - gen("\t%v = alloca %t\n", tmp, ex.ty); + let tmp = gentmp(mkptrtype(ex.ty), "alloca %t", ex.ty); gen("\tstore %t zeroinitializer, %t %v\n", ex.ty, tmp.ty, tmp); foreach(fld, i, ini.flds) { let fex = &ini.exs[i]; @@ -300,8 +312,7 @@ fn genaddr(f *Fn, ex *Expr) Value { case ArrIni ini; let ty = mkarrtype(ini.maxn, #f, ex.ty.u.Arr.child); - let tmp = mktmp(mkptrtype(ty)); - gen("\t%v = alloca %t\n", tmp, ty); + let tmp = gentmp(mkptrtype(ty), "alloca %t", ty); gen("\tstore %t zeroinitializer, %t %v\n", ty, tmp.ty, tmp); foreach(idx, i, ini.idxs) { let fex = &ini.exs[i]; @@ -524,10 +535,28 @@ fn genexpr(f *Fn, ex *Expr) Value { case '>'; gencmp(ty2->is(:Flo) ? "ogt" : ty2.u.Int.sgn ? "sgt" : "ugt"); case '>='; gencmp(ty2->is(:Flo) ? "oge" : ty2.u.Int.sgn ? "sge" : "uge"); case '='; - let addr = genaddr(f, b.lhs); - let rhs = convert(f, b.lhs.ty, b.rhs); - gen("\tstore %t %v, %t %v\n", ex.ty, rhs, addr.ty, addr); - return rhs; + if b.lhs.u.#tag != :BitDot { + let addr = genaddr(f, b.lhs); + let rhs = convert(f, b.lhs.ty, b.rhs); + gen("\tstore %t %v, %t %v\n", ex.ty, rhs, addr.ty, addr); + return rhs; + } else { + let dot = &b.lhs.u.BitDot; + let intty = dot.lhs.ty.u.BitF.intty; + let fld = dot.fld; + let srcmask = fld.size == 64 ? ~0u64 : ((1u64 << fld.size) - 1); + let addr = genaddr(f, dot.lhs); + let src0 = convert(f, intty, b.rhs); + let src = gentmp(intty, "and %t %v, %I", src0.ty, src0, srcmask); + let src = gentmp(intty, "shl %t %v, %d", src.ty, src, fld.off); + let dstmask = ~(srcmask << fld.off); + let lhs = gentmp(intty, "load %t, %t %v", intty, addr.ty, addr); + let lhs = gentmp(lhs.ty, "and %t %v, %I", lhs.ty, lhs, dstmask); + let raw = gentmp(lhs.ty, "or %t %v, %v", lhs.ty, lhs, src); + gen("\tstore %t %v, %t %v\n", raw.ty, raw, addr.ty, addr); + let res = gentmp(intty, "load %t, %t %v", fld.ty, addr.ty, addr); + return res; + } case '+='; let addr = genaddr(f, b.lhs); let rhs = convert(f, b.lhs.ty, b.rhs); @@ -696,6 +725,27 @@ fn genexpr(f *Fn, ex *Expr) Value { gen("\t%v = load %t, %t %v\n", tmp, ex.ty, addr.ty, addr); return tmp; + case BitRaw ex; + return genexpr(f, ex); + + case BitDot dot; + let fld = dot.fld; + let lhs = genexpr(f, dot.lhs); + let shifted = fld.off == 0 ? lhs : gentmp(lhs.ty, "lshr %t %v, %d", lhs.ty, lhs, fld.off); + let truncd = gentmp(#null, "trunc %t %v to i%d", shifted.ty, shifted, fld.size); + let res = mktmp(fld.ty); + switch { + case dot.fld.ty->is(:Bool); + let tmp = gentmp(ty_bool, "icmp ne i%d %v, 0", fld.size, truncd); + gen("\t%v = zext i1 %v to %t\n", res, tmp, res.ty); + + case dot.fld.ty->is(:Int); + gen("\t%v = %s i%d %v to %t\n", res, dot.fld.ty.u.Int.sgn ? "sext" : "zext", truncd, fld.size, lhs.ty); + + case else assert(#f, ""); + } + return res; + case SPtr sl; let sl = genexpr(f, sl); let ptr = mktmp(mkptrtype(sl.ty.u.Slice)); diff --git a/src/parse.cff b/src/parse.cff index d4b8b7a..364b23b 100644 --- a/src/parse.cff +++ b/src/parse.cff @@ -141,8 +141,8 @@ fn eatspaces(P *Parser) void { // !sorted extern static keyword2str [NUM_KEYWORDS]*const u8 = { - "alignof", "and", "as", "break", "case", "const", - "continue", "def", "defer", "defmacro", "do", + "alignof", "and", "as", "bitfield", "break", "case", + "const", "continue", "def", "defer", "defmacro", "do", "else", "enum", "extern", "fn", "for", "if", "import", "let", "offsetof", "or", "return", "sizeof", "static", @@ -400,6 +400,8 @@ fn lex(P *Parser) Tok { tok.t = '#ptr'; case streq(s, "#tag"); tok.t = '#tag'; + case streq(s, "#raw"); + tok.t = '#raw'; case streq(s, "#FILE"); tok.t = '#FIL'; case streq(s, "#LINE"); @@ -693,7 +695,7 @@ fn parseenum(P *Parser, name *const u8, lax bool) *const Type { fn isdecltokt(t TokT) bool { switch t { case :kw_fn, :kw_static, :kw_def, :kw_defmacro, :kw_struct, :kw_union, :kw_enum, - :kw_extern; + :kw_extern, :kw_bitfield; return #t; } return #f; @@ -824,6 +826,79 @@ fn findaggfield(ty *const Type, name *const u8) *AggField { return #null; } +fn parsebitfield(P *Parser, name *const u8) *const Type { + let ty Type = { .u: :BitF{} }; + let bitf = &ty.u.BitF; + let tok = lexexpect(P, ':'); + bitf.name = name; + bitf.intty = parsetype(P); + if !bitf.intty->is(:Int) or bitf.intty.u.Int.sgn { + fatal(P, tok.loc, "bitfield backing type must be unsigned integer (got %t)", bitf.intty); + } + ty.size = bitf.intty.size; + ty.align = bitf.intty.align; + lexexpect(P, '{'); + static id int = {}; + bitf.id = id++; + let flds Vec<BitFField> = {}; + let iota = 0u16; + while !lexmatch(P, &tok, '}') { + let name = (tok = lexexpects(P, :ident, "field name")).u.ident; + let off u16 = 0, size u16 = 0; + let ty = bitf.intty; + + if lexmatch(P, &tok, '(') { + // `(offset, size)` + let off_ex = parseexpr(P); + lexexpect(P, ','); + let size_ex = parseexpr(P); + lexmatch(P, &tok, ','); + lexexpect(P, ')'); + if !fold(&off_ex) or off_ex.u.#tag != :IntLit or off_ex.u.IntLit.i < 0 { + fatal(P, off_ex.loc, "expected non-negative constant integer for field offset"); + } + if !fold(&size_ex) or size_ex.u.#tag != :IntLit or size_ex.u.IntLit.i <= 0 { + fatal(P, size_ex.loc, "expected positive constant integer for field size"); + } + off = off_ex.u.IntLit.i; + size = size_ex.u.IntLit.i; + } else { + // `size` (offset is right after previous field) + let size_ex = parseexpr(P); + if !fold(&size_ex) or size_ex.u.#tag != :IntLit or size_ex.u.IntLit.i <= 0 { + fatal(P, size_ex.loc, "expected positive constant integer for field size"); + } + off = iota; + size = size_ex.u.IntLit.i; + } + + if (tok = lexpeek(P)).t == :ident and streq(tok.u.ident, "bool") { + lex(P); + ty = ty_bool; + } else if (tok = lexpeek(P)).t == :ident and streq(tok.u.ident, "signed") { + lex(P); + let signedty = *bitf.intty; + signedty.u.Int.sgn = #t; + ty = interntype(signedty); + } + + iota += size; + if off + size > bitf.intty.size * 8 { + err(P, tok.loc, "field `%s' outside %t bit range", name, bitf.intty); + } + + flds->push({ name, ty, off, size }); + + if !lexmatch(P, #null, ',') { + lexexpect(P, '}'); + break; + } + } + + bitf.flds = flds->move(P.alloc); + return interntype(ty); +} + fn typematchestarg(targ *const Type, ty *const Type) bool { if targ == ty { return #t; @@ -1077,9 +1152,12 @@ fn parsetype(P *Parser) *const Type { if lexmatch(P, #null, :kw_union) { return parseagg(P, tok.loc, :EUnion, #null, &decl); } else { - return parseenum(P, #null, #{lax?} #f); + return parseenum(P, #{name} #null, #{lax?} #f); } + case lexmatch(P, &tok, :kw_bitfield); + return parsebitfield(P, #{name} #null); + case else; fatal(P, tok.loc, "expected type (near %qT)", tok); } @@ -1093,7 +1171,7 @@ fn exprdup(alloc *Allocator, ex Expr) *Expr { return memcpy(alloc->alloc(sizeof(ex), alignof(ex)), &ex, sizeof(ex)); } -fn islvalue(ex Expr) bool { +fn islvalue(ex *Expr) bool { switch ex.u { case Symbol decl; return decl.u.#tag == :Let or decl.u.#tag == :Static; @@ -1103,6 +1181,10 @@ fn islvalue(ex Expr) bool { return #t; case Dot; return #t; + case BitDot bdot; + return islvalue(bdot.lhs); + case BitRaw ex; + return islvalue(ex); case EUTag; return #t; } @@ -1588,12 +1670,16 @@ fn pexpostfix(P *Parser) Expr { if !isnumtype(ex.ty) and !ex.ty->is(:Ptr) { fatal(P, ex.loc, "invalid operand to unary operator %qT (%t)", tok, ex.ty); } - if !islvalue(ex) { + if !islvalue(&ex) { err(P, ex.loc, "left operand to prefix %qT operator is not lvalue", tok); } if ex.ty.konst { err(P, ex.loc, "left operand to prefix %qT operator is const", tok); } + if ex.u.#tag == :BitDot { + err(P, ex.loc, "cannot apply post-%srement operator to bitfield value", + tok.t == '++' ? "inc" : "dec"); + } ex = { tok.loc, ex.ty, .u: :UnOp { tok.t == '++' ? :postinc : :postdec, exprdup(P.alloc, ex) @@ -1607,26 +1693,32 @@ fn pexpostfix(P *Parser) Expr { if ty->is(:Ptr) { ty = ty.u.Ptr; } - if !ty->is(:Agg) { - fatal(P, tok.loc, "left-hand-side is not an aggregate (%t)", ty); - } let konst = ty.konst; - ty = unconstify(ty); - let agg = &ty.u.Agg; - let fld *AggField = #null; - foreach(f, i, agg.flds) { - if streq(name, f.name) { - fld = &agg.flds[i]; - break; + switch ty.u { + case Agg *agg; + let fld *AggField = #null; + foreach(f, i, agg.flds) { + if streq(name, f.name) { + fld = &agg.flds[i]; + break; + } } + if fld == #null { fatal(P, tok.loc, "%t has no such field %qT", ty, tok); } + if fld.ty == #null { fatal(P, tok.loc, "field %qT has no type", tok); } + ex = { tok.loc, konst ? constify(fld.ty) : fld.ty, :Dot { exprdup(P.alloc, ex), fld }}; + case BitF *bitf; + let fld *BitFField = #null; + foreach(f, i, bitf.flds) { + if streq(name, f.name) { + fld = &bitf.flds[i]; + break; + } + } + if fld == #null { fatal(P, tok.loc, "%t has no such field %qT", ty, tok); } + ex = { tok.loc, konst ? constify(fld.ty) : fld.ty, :BitDot { exprdup(P.alloc, ex), fld }}; + case else + fatal(P, tok.loc, "left-hand-side is not an aggregate (%t)", ty); } - if fld == #null { - fatal(P, tok.loc, "%t has no such field %qT", ty, tok); - } - if fld.ty == #null { - fatal(P, tok.loc, "field %qT has no type", tok); - } - ex = { tok.loc, konst ? constify(fld.ty) : fld.ty, :Dot { exprdup(P.alloc, ex), fld }}; case lexmatch(P, &tok, '#ptr'); let ty = ex.ty; @@ -1658,6 +1750,17 @@ fn pexpostfix(P *Parser) Expr { let enumty = ty.u.Agg.enumty; ex = { tok.loc, ty.konst ? constify(enumty) : enumty, :EUTag(exprdup(P.alloc, ex)) }; + case lexmatch(P, &tok, '#raw'); + let ty = ex.ty; + if ty->is(:Ptr) { + ty = ty.u.Ptr; + } + if !ty->is(:BitF) { + fatal(P, ex.loc, "invalid operand to #raw (%t)", ex.ty); + } + let intty = ty.u.BitF.intty; + ex = { tok.loc, ty.konst ? constify(intty) : intty, :BitRaw(exprdup(P.alloc, ex)) }; + case lexpeek(P).t == '['; // sugar: expr.[idx] -> (*expr)[idx] if !ex.ty->is(:Ptr) { @@ -1709,7 +1812,7 @@ fn pexpostfix(P *Parser) Expr { ex = { ex.loc, ty, :UnOp{:deref, exprdup(P.alloc, ex)}}; case !exptr and metptr; - if !islvalue(ex) { + if !islvalue(&ex) { err(P, tok.loc, "cannot call `->%s' by reference, lhs is not an lvalue", name); } else if ty.konst and !recv.konst { err(P, tok.loc, "constness mismatch: method takes %t but got %t", recv0, ex.ty); @@ -1773,12 +1876,16 @@ fn pexprefix(P *Parser) Expr { if !isnumtype(ex.ty) and !ex.ty->is(:Ptr) { fatal(P, ex.loc, "invalid operand to unary operator %qT (%t)", tok, ex.ty); } - if !islvalue(ex) { + if !islvalue(&ex) { err(P, ex.loc, "left operand to prefix %qT operator is not lvalue", tok); } if ex.ty.konst { err(P, ex.loc, "left operand to prefix %qT operator is const", tok); } + if ex.u.#tag == :BitDot { + err(P, ex.loc, "cannot apply pre-%srement operator to bitfield value", + tok.t == '++' ? "inc" : "dec"); + } return { tok.loc, ex.ty, .u: :UnOp { tok.t == '++' ? :preinc : :predec, exprdup(P.alloc, ex) @@ -1805,7 +1912,7 @@ fn pexprefix(P *Parser) Expr { case lexmatch(P, &tok, '&'); let ex = pexprefix(P); let ty2 = interntype({ g_targ.ptrsize, .u: :Ptr(ex.ty) }); - if !islvalue(ex) and !(ex.u.#tag == :Symbol and ex.u.Symbol.u.#tag == :Fn) + if !islvalue(&ex) and !(ex.u.#tag == :Symbol and ex.u.Symbol.u.#tag == :Fn) and !(ex.u.#tag == :ZeroIni or ex.u.#tag == :AggIni or ex.u.#tag == :ArrIni) { err(P, ex.loc, "invalid operand to `&': not an lvalue"); } @@ -2027,7 +2134,7 @@ fn pexassign(P *Parser) Expr { } let rhs = pexcond(P); switch { - case !islvalue(ex); + case !islvalue(&ex); err(P, ex.loc, "left operand to assignment is not lvalue"); case (typeof2(ex.ty, rhs.ty) == #null) and !((ex.ty->is(:Ptr) and rhs.ty->is(:Int) @@ -2035,6 +2142,8 @@ fn pexassign(P *Parser) Expr { err(P, ex.loc, "operands %t and %t to assignment operator %qT have incompatible types", ex.ty, rhs.ty, tok); + case okind != :Set and ex.u.#tag == :BitDot; + err(P, ex.loc, "cannot apply compound assignment operator to bitfield value"); case ex.ty.konst; err(P, ex.loc, "left operand to assignment is const"); } @@ -2289,7 +2398,7 @@ fn psteuswitch(P *Parser, loc Loc, test Expr) Stmt { seen->put(c.variant, tok.loc); if c.fld.ty != #null and lexmatch(P, &tok, '*') { - if !islvalue(test) { + if !islvalue(&test) { fatal(P, tok.loc, "cannot capture by pointer, test expression is not lvalue"); } c.captptr = #t; @@ -2320,7 +2429,7 @@ fn psteuswitch(P *Parser, loc Loc, test Expr) Stmt { } - return { loc, :EUSwitch { islvalue(test), test, cs->move(P.alloc), f }}; + return { loc, :EUSwitch { islvalue(&test), test, cs->move(P.alloc), f }}; } fn pstcswitch(P *Parser, loc Loc) Stmt { @@ -2914,6 +3023,11 @@ fn parsedecls(P *Parser, loc Loc, yield DeclYielder, yarg *void, toplevel bool) { name, tok.loc, .u: :Tepl(parsetepl(P, tok.loc, :Union, name)) }); } + case lexmatch(P, &tok, :kw_bitfield); + let name = lexexpect(P, :ident).u.ident; + let ty = parsebitfield(P, name); + decl = putdecl(P, loc, { name, loc, .u: :Ty(ty) }); + case lexmatch(P, &tok, :kw_static); struct Arg { P *Parser, diff --git a/src/type.cff b/src/type.cff index 214bda8..c7e76cc 100644 --- a/src/type.cff +++ b/src/type.cff @@ -43,6 +43,9 @@ struct TypeTraits { case Enum enu; h = fnv1a_i(h, enu.id); + + case BitF bitf; + h = fnv1a_i(h, bitf.id); } return h; @@ -97,6 +100,9 @@ struct TypeTraits { case Enum enu; return enu.id == b.u.Enum.id; + + case BitF bitf; + return bitf.id == b.u.BitF.id; } return #f; } |