diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 17c068d6..28b6d081 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -297,6 +297,26 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare } return nil } + // TODO: All of the (ptr base.u8).ReadUxx methods, not just U32LE. + if isThatMethod(g.tm, n, t.IDReadU32LE, 1) { + if pp == parenthesesMandatory { + b.writeb('(') + } + b.writes("wuffs_base__load_u32le(") + if err := g.writeExpr(b, n.LHS().Expr().LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { + return err + } + b.writeb('+') + a := n.Args()[0].Arg().Value() + if err := g.writeExpr(b, a, rp, parenthesesMandatory, depth); err != nil { + return err + } + b.writeb(')') + if pp == parenthesesMandatory { + b.writeb(')') + } + return nil + } if isThatMethod(g.tm, n, g.tm.ByName("update"), 1) { // TODO: don't hard-code the class name or this.checksum. class := "wuffs_crc32__ieee_hasher" diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index ed416eb8..c78035ea 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -2018,7 +2018,7 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, uint8_t* v_p = i_slice_p.ptr; uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 0; while (v_p < i_end0_p) { - v_s ^= 1234; + v_s ^= (wuffs_base__load_u32le(v_p + 0)); v_p += 8; } uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index e7a1fef7..e4330221 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -669,6 +669,8 @@ const MaxTypeExprDepth = 63 // type. LHS is the receiver type, which may be nil. If non-nil, it will be a // pointee type: "T" instead of "ptr T", "ptr ptr T", etc. // +// TODO: is "pointee type" correct for the built-in ptr base.u8 methods?? +// // TODO: method effects: "foo" vs "foo!" vs "foo?". // // A zero ID0 means a (possibly package-qualified) type like "pkg.foo" or diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 52662365..eb10eb94 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -134,16 +134,28 @@ var Funcs = []string{ "image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()", } -// The "T" types here are generic placeholders for every "slice U" type. After -// parsing these SliceFunc strings (e.g. in the lang/check package), replace -// "T" with the "◊" diamond to denote a generic slice method, to avoid -// ambiguity with a user-defined, non-generic "T" type. +// The "T" types here are placeholders for "ptr base.u8" or generic "slice U" +// types. After tokenizing (but before parsing) these XxxFunc strings (e.g. in +// the lang/check package), replace the "T" receiver type with the "◊" diamond, +// to avoid collision with a user-defined "T" type. const ( PlaceholderOldName = t.IDCapitalT PlaceholderNewName = t.IDDiamond ) +var PtrU8Funcs = []string{ + // TODO: these should really have preconditions like "this.length() >= 4". + // Should this be explicit in the Wuffs syntax here, or enforced as a + // special case in the compiler (during bounds checking)? + "T.read_u16be(at u32)(ret u16)", + "T.read_u16le(at u32)(ret u16)", + "T.read_u32be(at u32)(ret u32)", + "T.read_u32le(at u32)(ret u32)", + "T.read_u64be(at u32)(ret u64)", + "T.read_u64le(at u32)(ret u64)", +} + var SliceFuncs = []string{ "T.copy_from_slice(s T)(ret u64)", "T.length()(ret u64)", diff --git a/lang/check/check.go b/lang/check/check.go index 9bf07376..9ec61773 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -160,6 +160,7 @@ type Checker struct { useBaseNames map[t.ID]struct{} builtInFuncs map[t.QQID]*a.Func + builtInPtrU8Funcs map[t.QQID]*a.Func builtInSliceFuncs map[t.QQID]*a.Func unsortedStructs []*a.Struct } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index e4026ead..0375b0f1 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -80,6 +80,17 @@ func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) { return c.builtInFuncs[qqid], nil } +func (c *Checker) builtInPtrU8Func(qqid t.QQID) (*a.Func, error) { + if c.builtInPtrU8Funcs == nil { + err := error(nil) + c.builtInPtrU8Funcs, err = parseBuiltInFuncs(c.tm, builtin.PtrU8Funcs, true) + if err != nil { + return nil, err + } + } + return c.builtInPtrU8Funcs[qqid], nil +} + func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) { if c.builtInSliceFuncs == nil { err := error(nil) @@ -91,7 +102,7 @@ func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) { return c.builtInSliceFuncs[qqid], nil } -func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func, error) { +func parseBuiltInFuncs(tm *t.Map, ss []string, placeholder bool) (map[t.QQID]*a.Func, error) { m := map[t.QQID]*a.Func{} buf := []byte(nil) opts := parse.Options{ @@ -108,7 +119,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func if err != nil { return nil, fmt.Errorf("check: could not tokenize built-in funcs: %v", err) } - if generic { + if placeholder { for i := range tokens { if tokens[i].ID == builtin.PlaceholderOldName { tokens[i].ID = builtin.PlaceholderNewName @@ -146,6 +157,16 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { } else if f != nil { return f, nil } + + } else if lTyp.Decorator() == t.IDPtr && (lTyp.Inner().QID() == t.QID{t.IDBase, t.IDU8}) { + qqid[0] = t.IDBase + qqid[1] = t.IDDiamond + if f, err := c.builtInPtrU8Func(qqid); err != nil { + return nil, err + } else if f != nil { + return f, nil + } + } else { if f, err := c.builtInFunc(qqid); err != nil { return nil, err diff --git a/lang/check/type.go b/lang/check/type.go index 15f47795..5f00c369 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -660,6 +660,18 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { } n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.Node(), nil, nil)) return nil + + } else if ptrLTyp := lhs.MType(); ptrLTyp.Decorator() == t.IDPtr && (lQID == t.QID{t.IDBase, t.IDU8}) { + qqid[0] = t.IDBase + qqid[1] = t.IDDiamond + if f, err := q.c.builtInPtrU8Func(qqid); err != nil { + return err + } else if f == nil { + return fmt.Errorf("check: no ptr base.u8 method %q", n.Ident().Str(q.tm)) + } + n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), ptrLTyp.Node(), nil, nil)) + return nil + } else if lTyp.Decorator() != 0 { return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm)) } diff --git a/std/crc32/common_crc32.wuffs b/std/crc32/common_crc32.wuffs index b2198dc8..e2701d67 100644 --- a/std/crc32/common_crc32.wuffs +++ b/std/crc32/common_crc32.wuffs @@ -28,8 +28,8 @@ pub func ieee_hasher.update(x slice base.u8)(checksum base.u32) { var s base.u32 = 0xFFFFFFFF ^ this.state iterate (step:8, unroll:1)(p ptr base.u8:in.x) { - // TODO. - s ^= 1234 + // TODO: bounds checking for the ptr base.u8 methods. + s ^= p.read_u32le(at:0) } else { s = ieee_table[0][((s & 0xFF) as base.u8) ^ deref p] ^ (s >> 8) }