diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index 65cb3c05..84dfb78b 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -374,25 +374,29 @@ func (g *gen) writeBuiltinSliceCopyFromSlice8(b *buffer, recv *a.Expr, method t. if err := g.writeExpr(b, foo, rp, depth); err != nil { return err } - b.writes(")+(") - if err := g.writeExpr(b, fIndex, rp, depth); err != nil { - return err + if fIndex != nil { + b.writes(")+(") + if err := g.writeExpr(b, fIndex, rp, depth); err != nil { + return err + } } b.writes("),(") if err := g.writeExpr(b, bar, rp, depth); err != nil { return err } - b.writes(")+(") - if err := g.writeExpr(b, bIndex, rp, depth); err != nil { - return err + if bIndex != nil { + b.writes(")+(") + if err := g.writeExpr(b, bIndex, rp, depth); err != nil { + return err + } } // TODO: don't assume that the slice is a slice of base.u8. b.writes("), 8)") return nil } -// matchFooIndexIndexPlus8 matches n with "foo[index:index + 8]". It returns -// nil values if there isn't a match. +// matchFooIndexIndexPlus8 matches n with "foo[index:index + 8]" or "foo[:8]". +// It returns a nil foo if there isn't a match. func matchFooIndexIndexPlus8(n *a.Expr) (foo *a.Expr, index *a.Expr) { if n.Operator() != t.IDColon { return nil, nil @@ -400,10 +404,19 @@ func matchFooIndexIndexPlus8(n *a.Expr) (foo *a.Expr, index *a.Expr) { foo = n.LHS().AsExpr() index = n.MHS().AsExpr() rhs := n.RHS().AsExpr() - if index == nil || rhs == nil || rhs.Operator() != t.IDXBinaryPlus || !rhs.LHS().AsExpr().Eq(index) { + if rhs == nil { return nil, nil } - if cv := rhs.RHS().AsExpr().ConstValue(); cv == nil || cv.Cmp(eight) != 0 { + + if index == nil { + // No-op. + } else if rhs.Operator() != t.IDXBinaryPlus || !rhs.LHS().AsExpr().Eq(index) { + return nil, nil + } else { + rhs = rhs.RHS().AsExpr() + } + + if cv := rhs.ConstValue(); cv == nil || cv.Cmp(eight) != 0 { return nil, nil } return foo, index diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index d798e8bf..157062d3 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -9656,7 +9656,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, v_steps = (((uint32_t)(self->private_impl.f_lm1s[v_c])) >> 3); while (true) { memcpy((self->private_impl.f_output) + (v_o), - (self->private_impl.f_suffixes[v_c]) + (0), 8); + (self->private_impl.f_suffixes[v_c]), 8); if (v_steps <= 0) { goto label_2_break; } diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 939b1e99..85b52235 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -160,7 +160,12 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { var steps base.u32 = (this.lm1s[c] as base.u32) >> 3 while true { assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b:8) - this.output[o:o + 8].copy_from_slice!(s:this.suffixes[c][0:0 + 8]) + + // The final "8" is redundant semantically, but helps the + // wuffs-c code generator recognize that both slices have the + // same constant length, and hence produce efficient C code. + this.output[o:o + 8].copy_from_slice!(s:this.suffixes[c][:8]) + if steps <= 0 { break }