diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index 3a4246d6..131e9e0e 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -116,6 +116,10 @@ func (g *gen) writeBuiltinIO(b *buffer, recv *a.Expr, method t.ID, args []*a.Nod func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { // TODO: don't hard-code the recv being a_src. switch method { + case t.IDPeekU8: + b.writes("(*ioptr_src)") + return nil + case t.IDSetLimit: b.printf("wuffs_base__io_reader__set_limit(&%ssrc, ioptr_src,", aPrefix) // TODO: update the ioptr variables? @@ -132,8 +136,16 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args [] aPrefix, aPrefix) return nil - case t.IDPeekU8: - b.writes("(*ioptr_src)") + case t.IDSkip32Fast: + // Generate a two part expression using the comma operator: "(pointer + // increment, return_empty_struct call)". The final part is a function + // call (to a static inline function) instead of a struct literal, to + // avoid a "expression result unused" compiler error. + b.writes("(ioptr_src += ") + if err := g.writeExpr(b, args[1].Arg().Value(), rp, depth); err != nil { + return err + } + b.writes(", wuffs_base__return_empty_struct())") return nil } diff --git a/doc/changelog.md b/doc/changelog.md index 1c32f57d..d6604179 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -21,7 +21,7 @@ - Redesigned iterate blocks. - Added a `base.image_config` type. - Added a `reset` method. -- Added `peek_uxx` methods. +- Added `peek_uxx` and `skip32_fast` methods. - Tweaked how marks and limits work. - Supported animated (not just single frame) GIFs. - Marked the `std/gif` LZW decoder as private. diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index ee38e698..23ff6295 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -157,8 +157,8 @@ var Funcs = []string{ "io_reader.read_u64be?()(ret u64)", "io_reader.read_u64le?()(ret u64)", - // TODO: these methods should have an explicit precondition on "available() - // >= N". For now, that's implicitly checked (i.e. hard coded). + // TODO: these should have an explicit precondition "available() >= N". For + // now, that's implicitly checked (i.e. hard coded). "io_reader.peek_u8()(ret u8)", "io_reader.peek_u16be()(ret u16)", "io_reader.peek_u16le()(ret u16)", @@ -184,6 +184,12 @@ var Funcs = []string{ "io_reader.skip32?(n u32)()", "io_reader.skip64?(n u64)()", + // TODO: this should have explicit preconditions "actual <= worst_case" and + // "worst_case <= available()". As an implementation restriction, we also + // require that worst_case has a constant value. For now, that's all + // implicitly checked (i.e. hard coded). + "io_reader.skip32_fast!(actual u32, worst_case u32)()", + // ---- io_writer "io_writer.write_u8?(x u8)()", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 77417c69..8d561b09 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -969,16 +969,38 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (*big.Int, } } else if recvTyp.IsIOType() { - if method >= t.IDPeekU8 { + advance, update := (*big.Int)(nil), false + + if method == t.IDSkip32Fast { + args := n.Args() + if len(args) != 2 { + return nil, nil, fmt.Errorf("check: internal error: bad skip32_fast arguments") + } + actual := args[0].Arg().Value() + worstCase := args[1].Arg().Value() + if err := q.proveBinaryOp(t.IDXBinaryLessEq, actual, worstCase); err == errFailed { + return nil, nil, fmt.Errorf("check: could not prove skip32_fast precondition: %s <= %s", + actual.Str(q.tm), worstCase.Str(q.tm)) + } else if err != nil { + return nil, nil, err + } + advance, update = worstCase.ConstValue(), true + if advance == nil { + return nil, nil, fmt.Errorf("check: skip32_fast worst_case is not a constant value") + } + + } else if method >= t.IDPeekU8 { if m := method - t.IDPeekU8; m < t.ID(len(ioMethodAdvances)) { - if advance := ioMethodAdvances[m]; advance != nil { - if ok, err := q.optimizeIOMethodAdvance(recv, advance, false); err != nil { - return nil, nil, err - } else if !ok { - return nil, nil, fmt.Errorf("check: could not prove %s precondition: %s.available() >= %v", - method.Str(q.tm), recv.Str(q.tm), advance) - } - } + advance, update = ioMethodAdvances[m], false + } + } + + if advance != nil { + if ok, err := q.optimizeIOMethodAdvance(recv, advance, update); err != nil { + return nil, nil, err + } else if !ok { + return nil, nil, fmt.Errorf("check: could not prove %s precondition: %s.available() >= %v", + method.Str(q.tm), recv.Str(q.tm), advance) } } } diff --git a/lang/check/optimize.go b/lang/check/optimize.go index 6fa0868e..8b9ca4d5 100644 --- a/lang/check/optimize.go +++ b/lang/check/optimize.go @@ -224,6 +224,10 @@ func (q *checker) optimizeSuspendible(n *a.Expr, depth uint32) error { } func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, update bool) (retOK bool, retErr error) { + // TODO: do two passes? The first one to non-destructively check retOK. The + // second one to drop facts if indeed retOK? Otherwise, an advance + // precondition failure will lose some of the facts in its error message. + retErr = q.facts.update(func(x *a.Expr) (*a.Expr, error) { // TODO: update (discard?) any facts that merely mention // receiver.available(), even if they aren't an exact match. @@ -271,6 +275,11 @@ func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, up return x, nil } + if rcv.Cmp(advance) == 0 { + // TODO: delete the (adjusted) fact, as newRCV will be zero, and + // "foo.advance() >= 0" is redundant. + } + // Create a new a.Expr to hold the adjusted RHS constant value, newRCV. newRCV := big.NewInt(0).Sub(rcv, advance) id, err := q.tm.Insert(newRCV.String()) diff --git a/lang/token/list.go b/lang/token/list.go index 2b82fb27..a21b0aa7 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -494,11 +494,12 @@ const ( IDWriteU64BE = ID(0x16E) IDWriteU64LE = ID(0x16F) - IDSetLimit = ID(0x180) - IDSetMark = ID(0x181) - IDSinceMark = ID(0x182) - IDSkip32 = ID(0x183) - IDSkip64 = ID(0x184) + IDSetLimit = ID(0x180) + IDSetMark = ID(0x181) + IDSinceMark = ID(0x182) + IDSkip32 = ID(0x183) + IDSkip32Fast = ID(0x184) + IDSkip64 = ID(0x185) IDCopyFromHistory32 = ID(0x190) IDCopyFromReader32 = ID(0x191) @@ -751,11 +752,12 @@ var builtInsByID = [nBuiltInIDs]string{ IDWriteU64BE: "write_u64be", IDWriteU64LE: "write_u64le", - IDSetLimit: "set_limit", - IDSetMark: "set_mark", - IDSinceMark: "since_mark", - IDSkip32: "skip32", - IDSkip64: "skip64", + IDSetLimit: "set_limit", + IDSetMark: "set_mark", + IDSinceMark: "since_mark", + IDSkip32: "skip32", + IDSkip32Fast: "skip32_fast", + IDSkip64: "skip64", IDCopyFromHistory32: "copy_from_history32", IDCopyFromReader32: "copy_from_reader32",