Add (ptr base.u8).read_u32le method

This commit is contained in:
Nigel Tao committed 2018-05-04 10:42:05 +10:00
1 parent 4584022d1a
commit e72f957eca
8 files changed
+77 -9

No files matched your search

+20
View File
@@ -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"
+1 -1
View File
@@ -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;
+2
View File
@@ -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
+16 -4
View File
@@ -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)",
+1
View File
@@ -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
}
+23 -2
View File
@@ -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
+12
View File
@@ -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))
}
+2 -2
View File
@@ -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)
}