diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 6ea777d1..e3ae6cca 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -362,14 +362,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare } func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { - switch n.ID0().Key() { - case t.KeyXUnaryPlus: - b.writeb('+') - case t.KeyXUnaryMinus: - b.writeb('-') - case t.KeyXUnaryNot: - b.writeb('!') - } + b.writes(cOpNames[0xFF&n.ID0().Key()]) return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth) } @@ -528,9 +521,11 @@ var cOpNames = [256]string{ t.KeyPercentEq: " %= ", t.KeyTildePlusEq: " += ", - t.KeyXUnaryPlus: "+", - t.KeyXUnaryMinus: "-", - t.KeyXUnaryNot: "!", + t.KeyXUnaryPlus: " + ", + t.KeyXUnaryMinus: " - ", + t.KeyXUnaryNot: " ! ", + t.KeyXUnaryRef: " & ", + t.KeyXUnaryDeref: " * ", t.KeyXBinaryPlus: " + ", t.KeyXBinaryMinus: " - ", diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 9f4eaf54..a360b001 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -247,22 +247,24 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { name := v.Name().String(g.tm) b.writes("{\n") - // TODO: don't assume that the slice is a slice of u8. + // TODO: don't assume that the slice is a slice of u8. In particular, + // the code gen can be subtle if the slice element type has zero size, + // such as the empty struct. b.printf("puffs_base_slice_u8 %sslice_%s =", iPrefix, name) if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil { return err } b.writes(";\n") - b.printf("uint8_t* %sptr_%s = %sslice_%s.ptr;\n", iPrefix, name, iPrefix, name) + b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name) b.printf("uint8_t* %send_%s = %sslice_%s.ptr + %sslice_%s.len;\n", iPrefix, name, iPrefix, name, iPrefix, name) - b.printf("while (%sptr_%s < %send_%s) {\n", iPrefix, name, iPrefix, name) + b.printf("while (%s%s < %send_%s) {\n", vPrefix, name, iPrefix, name) for _, o := range n.Body() { if err := g.writeStatement(b, o, depth); err != nil { return err } } - b.printf("%sptr_%s++;\n}\n", iPrefix, name) + b.printf("%s%s++;\n}\n", vPrefix, name) b.writes("}\n") return nil diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 90dd5f06..1255d3c1 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2425,7 +2425,6 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self, uint32_t v_s1; uint32_t v_s2; puffs_base_slice_u8 v_remaining; - uint64_t v_i; v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1)); v_s2 = ((self->private_impl.f_state) >> (32 - (16))); @@ -2434,24 +2433,17 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self, if (((uint64_t)(a_x.len)) > 5552) { v_remaining = puffs_base_slice_u8_subslice_i(a_x, 5552); a_x = puffs_base_slice_u8_subslice_j(a_x, 5552); - if (((uint64_t)(a_x.len)) > 5552) { - return 0; - } } { puffs_base_slice_u8 i_slice_p = a_x; - uint8_t* i_ptr_p = i_slice_p.ptr; + uint8_t* v_p = i_slice_p.ptr; uint8_t* i_end_p = i_slice_p.ptr + i_slice_p.len; - while (i_ptr_p < i_end_p) { - i_ptr_p++; + while (v_p < i_end_p) { + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; } } - v_i = 0; - while (v_i < ((uint64_t)(a_x.len))) { - v_s1 += ((uint32_t)(a_x.ptr[v_i])); - v_s2 += v_s1; - v_i += 1; - } v_s1 %= 65521; v_s2 %= 65521; a_x = v_remaining; diff --git a/lang/ast/string.go b/lang/ast/string.go index f6f182cd..59140047 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -138,6 +138,8 @@ var opStrings = [256]string{ t.KeyXUnaryPlus: "+", t.KeyXUnaryMinus: "-", t.KeyXUnaryNot: "not ", + t.KeyXUnaryRef: "ref ", + t.KeyXUnaryDeref: "deref ", t.KeyXBinaryPlus: " + ", t.KeyXBinaryMinus: " - ", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 108ca63f..bc42460a 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -920,6 +920,8 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int return neg(rMax), neg(rMin), nil case t.KeyXUnaryNot: return zero, one, nil + case t.KeyXUnaryRef, t.KeyXUnaryDeref: + return q.bcheckTypeExpr(n.MType()) } return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key()) diff --git a/lang/check/type.go b/lang/check/type.go index 5cdfd8b9..c171b3c8 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -796,6 +796,17 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error { } n.SetMType(typeExprBool) return nil + + case t.KeyXUnaryRef: + // TODO. + + case t.KeyXUnaryDeref: + if rTyp.Decorator().Key() != t.KeyPtr { // TODO: t.KeyNptr? + return fmt.Errorf("check: %q is a dereference of a non-pointer type %q", + n.String(q.tm), rTyp.String(q.tm)) + } + n.SetMType(rTyp.Inner()) + return nil } return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key()) } diff --git a/lang/token/list.go b/lang/token/list.go index dc662b01..a11499dc 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -208,10 +208,12 @@ const ( KeyGreaterThan = Key(IDGreaterThan >> KeyShift) // TODO: sort these by name, when the list has stabilized. - KeyAnd = Key(IDAnd >> KeyShift) - KeyOr = Key(IDOr >> KeyShift) - KeyNot = Key(IDNot >> KeyShift) - KeyAs = Key(IDAs >> KeyShift) + KeyAnd = Key(IDAnd >> KeyShift) + KeyOr = Key(IDOr >> KeyShift) + KeyNot = Key(IDNot >> KeyShift) + KeyAs = Key(IDAs >> KeyShift) + KeyRef = Key(IDRef >> KeyShift) + KeyDeref = Key(IDDeref >> KeyShift) // TODO: sort these by name, when the list has stabilized. KeyFunc = Key(IDFunc >> KeyShift) @@ -302,6 +304,8 @@ const ( KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) KeyXUnaryNot = Key(IDXUnaryNot >> KeyShift) + KeyXUnaryRef = Key(IDXUnaryRef >> KeyShift) + KeyXUnaryDeref = Key(IDXUnaryDeref >> KeyShift) KeyXBinaryPlus = Key(IDXBinaryPlus >> KeyShift) KeyXBinaryMinus = Key(IDXBinaryMinus >> KeyShift) @@ -390,10 +394,12 @@ const ( IDGreaterThan = ID(0x55< 5552 { remaining = in.x[5552:] in.x = in.x[:5552] - // TODO: this redundant if should be unnecessary. - if in.x.length() > 5552 { - return 0 // Unreachable. - } } iterate (p ptr u8:in.x) { - // TODO: uncomment this loop body, and delete the while loop below. - // s1 ~+= deref p as u32 - // s2 ~+= s1 - } - - // TODO: can u64 be u32? Does it matter? - var i u64 - while i < in.x.length(), - inv in.x.length() <= 5552, - { - assert i < 5552 via "a < b: a < c; c <= b"(c:in.x.length()) - s1 ~+= in.x[i] as u32 + s1 ~+= deref p as u32 s2 ~+= s1 - i += 1 } s1 %= 65521