diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 0489cd58..e95e8447 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -238,29 +238,31 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { return err } b.writes(";\n") - b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name) + b.printf("wuffs_base__slice_u8 %s%s = %sslice_%s;\n", vPrefix, name, iPrefix, name) // TODO: look at n.HasContinue() and n.HasBreak(). unroll := n.Unroll().SmallPowerOf2Value() step := n.Step().SmallPowerOf2Value() if uStep := unroll * step; uStep != 1 { + b.printf("%s%s.len = %d;\n", vPrefix, name, step) b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n", iPrefix, name, iPrefix, name, iPrefix, name, uStep, uStep) - b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name) + b.printf("while (%s%s.ptr < %send0_%s) {\n", vPrefix, name, iPrefix, name) for i := 0; i < unroll; i++ { for _, o := range n.Body() { if err := g.writeStatement(b, o, depth); err != nil { return err } } - b.printf("%s%s += %d;\n", vPrefix, name, step) + b.printf("%s%s.ptr += %d;\n", vPrefix, name, step) } b.writes("}\n") } + b.printf("%s%s.len = %d;\n", vPrefix, name, 1) b.printf("uint8_t* %send1_%s = %sslice_%s.ptr + %sslice_%s.len;\n", iPrefix, name, iPrefix, name, iPrefix, name) - b.printf("while (%s%s < %send1_%s) {\n", vPrefix, name, iPrefix, name) + b.printf("while (%s%s.ptr < %send1_%s) {\n", vPrefix, name, iPrefix, name) tail := n.Tail() if step == 1 { tail = n.Body() @@ -270,7 +272,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { return err } } - b.printf("%s%s++;\n", vPrefix, name) + b.printf("%s%s.ptr++;\n", vPrefix, name) b.writes("}\n") b.writes("}\n") diff --git a/doc/changelog.md b/doc/changelog.md index 9618add0..4fae72ca 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -16,6 +16,7 @@ - Removed `&^`. - Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`. - Renamed `iterate.U` to `iterate (step:S, unroll:U)`. +- Changed iterate variable types from ptr T to slice T. - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. - Renamed `u32`, `io_reader`, etc to `base.u32`, `base.io_reader`, etc. - Added a `base.image_config` type. diff --git a/gen/c/std/adler32.c b/gen/c/std/adler32.c index 76441bdd..a01be88d 100644 --- a/gen/c/std/adler32.c +++ b/gen/c/std/adler32.c @@ -1663,39 +1663,41 @@ uint32_t wuffs_adler32__hasher__update(wuffs_adler32__hasher* self, } { wuffs_base__slice_u8 i_slice_p = a_x; - uint8_t* v_p = i_slice_p.ptr; + wuffs_base__slice_u8 v_p = i_slice_p; + v_p.len = 1; uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; - while (v_p < i_end0_p) { - v_s1 += ((uint32_t)(v_p[0])); + while (v_p.ptr < i_end0_p) { + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; - v_s1 += ((uint32_t)(v_p[0])); + v_p.ptr += 1; + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p += 1; + v_p.ptr += 1; } + v_p.len = 1; uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; - while (v_p < i_end1_p) { - v_s1 += ((uint32_t)(v_p[0])); + while (v_p.ptr < i_end1_p) { + v_s1 += ((uint32_t)(v_p.ptr[0])); v_s2 += v_s1; - v_p++; + v_p.ptr++; } } v_s1 %= 65521; diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index ae2150c5..eb779b6b 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -2015,26 +2015,29 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, v_s = (4294967295 ^ self->private_impl.f_state); { wuffs_base__slice_u8 i_slice_p = a_x; - uint8_t* v_p = i_slice_p.ptr; + wuffs_base__slice_u8 v_p = i_slice_p; + v_p.len = 8; uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; - while (v_p < i_end0_p) { - v_s ^= ((((uint32_t)(v_p[0])) << 0) | (((uint32_t)(v_p[1])) << 8) | - (((uint32_t)(v_p[2])) << 16) | (((uint32_t)(v_p[3])) << 24)); - v_s = (wuffs_crc32__ieee_table[0][v_p[7]] ^ - wuffs_crc32__ieee_table[1][v_p[6]] ^ - wuffs_crc32__ieee_table[2][v_p[5]] ^ - wuffs_crc32__ieee_table[3][v_p[4]] ^ + while (v_p.ptr < i_end0_p) { + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); - v_p += 8; + v_p.ptr += 8; } + v_p.len = 1; uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; - while (v_p < i_end1_p) { - v_s = (wuffs_crc32__ieee_table[0][((uint8_t)((v_s & 255))) ^ v_p[0]] ^ + while (v_p.ptr < i_end1_p) { + v_s = (wuffs_crc32__ieee_table[0][((uint8_t)((v_s & 255))) ^ v_p.ptr[0]] ^ (v_s >> 8)); - v_p++; + v_p.ptr++; } } self->private_impl.f_state = (4294967295 ^ v_s); diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 700b90c2..023eddb9 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -304,8 +304,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { q.facts = q.facts[:0] for _, o := range n.Variables() { v := o.Var() - // TODO: use the XType(), not the Value().MType(). - q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.Value().MType(), n.Step())) + q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.XType(), n.Step())) } for _, o := range n.Body() { if err := q.bcheckStatement(o); err != nil { @@ -319,8 +318,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { q.facts = q.facts[:0] for _, o := range n.Variables() { v := o.Var() - // TODO: use the XType(), not the Value().MType(). - q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.Value().MType(), t.ID1)) + q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.XType(), t.ID1)) } for _, o := range n.Tail() { if err := q.bcheckStatement(o); err != nil { diff --git a/lang/check/type.go b/lang/check/type.go index bd102ac8..810d7e75 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -206,17 +206,13 @@ func (q *checker) tcheckStatement(n *a.Node) error { lTyp := n.XType() rTyp := value.MType() if n.IterateVariable() { - if lTyp.Decorator() != t.IDPtr { - return fmt.Errorf("check: iterate variable %q, of type %q, does not have a pointer type", + if !lTyp.IsSliceType() { + return fmt.Errorf("check: iterate variable %q, of type %q, does not have a slice type", n.Name().Str(q.tm), lTyp.Str(q.tm)) } - if !rTyp.IsSliceType() { - return fmt.Errorf("check: iterate range %q, of type %q, does not have a slice type", - value.Str(q.tm), rTyp.Str(q.tm)) - } - if !lTyp.Inner().Eq(rTyp.Inner()) { + if !lTyp.Eq(rTyp) { return fmt.Errorf("check: cannot iterate %q of type %q over %q of type %q "+ - "as their inner types don't match", + "as their types don't match", n.Name().Str(q.tm), lTyp.Str(q.tm), value.Str(q.tm), rTyp.Str(q.tm)) } } else if err := q.tcheckEq(n.Name(), nil, lTyp, value, rTyp); err != nil { diff --git a/std/adler32/common_adler32.wuffs b/std/adler32/common_adler32.wuffs index 98786336..f0961404 100644 --- a/std/adler32/common_adler32.wuffs +++ b/std/adler32/common_adler32.wuffs @@ -34,7 +34,7 @@ pub func hasher.update(x slice base.u8)(checksum base.u32) { in.x = in.x[:5552] } - iterate (step:1, unroll:8)(p ptr base.u8:in.x) { + iterate (step:1, unroll:8)(p slice base.u8:in.x) { s1 ~mod+= p[0] as base.u32 s2 ~mod+= s1 } diff --git a/std/crc32/common_crc32.wuffs b/std/crc32/common_crc32.wuffs index 22a4f52e..fa242d8f 100644 --- a/std/crc32/common_crc32.wuffs +++ b/std/crc32/common_crc32.wuffs @@ -27,7 +27,7 @@ pub struct ieee_hasher?( 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) { + iterate (step:8, unroll:1)(p slice base.u8:in.x) { // TODO: explain the slicing-by-8 algorithm. // // TODO: bounds checking for the index expressions "p[7]" etc.