mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Add an iterate step argument
This commit is contained in:
1 parent
92682e6ccc
commit
a73e56712d
9 files changed
+120
-57
No files matched your search
@@ -241,18 +241,20 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name)
|
||||
// TODO: look at n.HasContinue() and n.HasBreak().
|
||||
|
||||
unrollCount := int(n.UnrollCount().ConstValue().Int64())
|
||||
if unrollCount != 1 {
|
||||
unroll := n.Unroll().SmallPowerOf2Value()
|
||||
step := n.Step().SmallPowerOf2Value()
|
||||
// TODO: remove the "step == 1" hack.
|
||||
if uStep := unroll * step; uStep != 1 && step == 1 {
|
||||
b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n",
|
||||
iPrefix, name, iPrefix, name, iPrefix, name, unrollCount, unrollCount)
|
||||
iPrefix, name, iPrefix, name, iPrefix, name, uStep, uStep)
|
||||
b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name)
|
||||
for i := 0; i < unrollCount; i++ {
|
||||
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++;\n", vPrefix, name)
|
||||
b.printf("%s%s += %d;\n", vPrefix, name, step)
|
||||
}
|
||||
b.writes("}\n")
|
||||
}
|
||||
@@ -260,7 +262,11 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
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)
|
||||
for _, o := range n.Body() {
|
||||
tail := n.Tail()
|
||||
if step == 1 {
|
||||
tail = n.Body()
|
||||
}
|
||||
for _, o := range tail {
|
||||
if err := g.writeStatement(b, o, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
- Renamed `~+` to `~mod+`; added `~mod-`, `~sat+` and `~sat-`.
|
||||
- Removed `&^`.
|
||||
- Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`.
|
||||
- Renamed `iterate.N` to `iterate[N]`.
|
||||
- Renamed `iterate.N` to `iterate[N]`; added step argument.
|
||||
- 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.
|
||||
|
||||
+8
-8
@@ -1668,28 +1668,28 @@ uint32_t wuffs_adler32__hasher__update(wuffs_adler32__hasher* self,
|
||||
while (v_p < i_end0_p) {
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_p += 1;
|
||||
}
|
||||
uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len;
|
||||
while (v_p < i_end1_p) {
|
||||
|
||||
+29
-17
@@ -166,7 +166,7 @@ type Node struct {
|
||||
// Func funcName receiverPkg receiverName Func
|
||||
// IOBind . . . IOBind
|
||||
// If . . . If
|
||||
// Iterate . label . Iterate
|
||||
// Iterate unroll label step Iterate
|
||||
// Jump keyword label . Jump
|
||||
// PackageID . . lit(pkgID) PackageID
|
||||
// Ret keyword . . Ret
|
||||
@@ -187,6 +187,7 @@ type Node struct {
|
||||
list0 []*Node
|
||||
list1 []*Node
|
||||
list2 []*Node
|
||||
list3 []*Node
|
||||
}
|
||||
|
||||
func (n *Node) Kind() Kind { return n.kind }
|
||||
@@ -247,13 +248,18 @@ type Loop interface {
|
||||
SetHasContinue()
|
||||
}
|
||||
|
||||
type TailedLoop interface {
|
||||
Loop
|
||||
Tail() []*Node
|
||||
}
|
||||
|
||||
type Raw Node
|
||||
|
||||
func (n *Raw) Node() *Node { return (*Node)(n) }
|
||||
func (n *Raw) Flags() Flags { return n.flags }
|
||||
func (n *Raw) FilenameLine() (string, uint32) { return n.filename, n.line }
|
||||
func (n *Raw) SubNodes() [3]*Node { return [3]*Node{n.lhs, n.mhs, n.rhs} }
|
||||
func (n *Raw) SubLists() [3][]*Node { return [3][]*Node{n.list0, n.list1, n.list2} }
|
||||
func (n *Raw) SubLists() [4][]*Node { return [4][]*Node{n.list0, n.list1, n.list2, n.list3} }
|
||||
|
||||
func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
||||
|
||||
@@ -494,7 +500,7 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
|
||||
|
||||
// IOBind is "io_bind (in_fields) { List2 }":
|
||||
// - List0: <Expr> in.something fields
|
||||
// - List2: <Statement> loop body
|
||||
// - List2: <Statement> body
|
||||
type IOBind Node
|
||||
|
||||
func (n *IOBind) Node() *Node { return (*Node)(n) }
|
||||
@@ -509,36 +515,42 @@ func NewIOBind(in_fields []*Node, body []*Node) *IOBind {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate is "iterate[LHS]:ID1 (vars), List1 { List2 }":
|
||||
// Iterate is "iterate[ID0, ID2]:ID1 (vars), List1 { List2 }":
|
||||
// - FlagsHasBreak is the iterate has an explicit break
|
||||
// - FlagsHasContinue is the iterate has an explicit continue
|
||||
// - ID0: unroll
|
||||
// - ID1: <0|label>
|
||||
// - LHS: <Expr> unroll count
|
||||
// - ID2: step
|
||||
// - List0: <Var> variables
|
||||
// - List1: <Assert> asserts
|
||||
// - List2: <Statement> loop body
|
||||
// - List2: <Statement> body
|
||||
// - List3: <Statement> tail
|
||||
type Iterate Node
|
||||
|
||||
func (n *Iterate) Node() *Node { return (*Node)(n) }
|
||||
func (n *Iterate) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
|
||||
func (n *Iterate) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
|
||||
func (n *Iterate) Unroll() t.ID { return n.id0 }
|
||||
func (n *Iterate) Label() t.ID { return n.id1 }
|
||||
func (n *Iterate) UnrollCount() *Expr { return n.lhs.Expr() }
|
||||
func (n *Iterate) Step() t.ID { return n.id2 }
|
||||
func (n *Iterate) Variables() []*Node { return n.list0 }
|
||||
func (n *Iterate) Asserts() []*Node { return n.list1 }
|
||||
func (n *Iterate) Body() []*Node { return n.list2 }
|
||||
func (n *Iterate) Tail() []*Node { return n.list3 }
|
||||
|
||||
func (n *Iterate) SetHasBreak() { n.flags |= FlagsHasBreak }
|
||||
func (n *Iterate) SetHasContinue() { n.flags |= FlagsHasContinue }
|
||||
|
||||
func NewIterate(label t.ID, unrollCount *Expr, variables []*Node, asserts []*Node, body []*Node) *Iterate {
|
||||
func NewIterate(unroll t.ID, step t.ID, label t.ID, variables []*Node, asserts []*Node, body []*Node, tail []*Node) *Iterate {
|
||||
return &Iterate{
|
||||
kind: KIterate,
|
||||
id0: unroll,
|
||||
id1: label,
|
||||
lhs: unrollCount.Node(),
|
||||
id2: step,
|
||||
list0: variables,
|
||||
list1: asserts,
|
||||
list2: body,
|
||||
list3: tail,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -548,7 +560,7 @@ func NewIterate(label t.ID, unrollCount *Expr, variables []*Node, asserts []*Nod
|
||||
// - ID1: <0|label>
|
||||
// - MHS: <Expr>
|
||||
// - List1: <Assert> asserts
|
||||
// - List2: <Statement> loop body
|
||||
// - List2: <Statement> body
|
||||
//
|
||||
// TODO: should we be able to unroll while loops too?
|
||||
type While Node
|
||||
@@ -577,23 +589,23 @@ func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While
|
||||
// If is "if MHS { List0 } else RHS" or "if MHS { List0 } else { List1 }":
|
||||
// - MHS: <Expr>
|
||||
// - RHS: <nil|If>
|
||||
// - List0: <Statement> if-true body
|
||||
// - List1: <Statement> if-false body
|
||||
// - List2: <Statement> if-true body
|
||||
// - List3: <Statement> if-false body
|
||||
type If Node
|
||||
|
||||
func (n *If) Node() *Node { return (*Node)(n) }
|
||||
func (n *If) Condition() *Expr { return n.mhs.Expr() }
|
||||
func (n *If) ElseIf() *If { return n.rhs.If() }
|
||||
func (n *If) BodyIfTrue() []*Node { return n.list0 }
|
||||
func (n *If) BodyIfFalse() []*Node { return n.list1 }
|
||||
func (n *If) BodyIfTrue() []*Node { return n.list2 }
|
||||
func (n *If) BodyIfFalse() []*Node { return n.list3 }
|
||||
|
||||
func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node) *If {
|
||||
return &If{
|
||||
kind: KIf,
|
||||
mhs: condition.Node(),
|
||||
rhs: elseIf.Node(),
|
||||
list0: bodyIfTrue,
|
||||
list1: bodyIfFalse,
|
||||
list2: bodyIfTrue,
|
||||
list3: bodyIfFalse,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -775,7 +787,7 @@ const MaxBodyDepth = 255
|
||||
// - LHS: <Struct> in-parameters
|
||||
// - RHS: <Struct> out-parameters
|
||||
// - List1: <Assert> asserts
|
||||
// - List2: <Statement> function body
|
||||
// - List2: <Statement> body
|
||||
//
|
||||
// Statement means one of:
|
||||
// - Assert
|
||||
|
||||
+16
-3
@@ -291,18 +291,26 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
||||
case a.KIterate:
|
||||
n := n.Iterate()
|
||||
for _, o := range n.Variables() {
|
||||
if err := q.bcheckVar(o.Var()); err != nil {
|
||||
if err := q.bcheckVar(o.Var(), true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO: this isn't right, as the body is a loop, not an
|
||||
// execute-exactly-once block. We should have pre / inv / post
|
||||
// conditions, a la bcheckWhile.
|
||||
q.facts = q.facts[:0]
|
||||
for _, o := range n.Body() {
|
||||
if err := q.bcheckStatement(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
q.facts = q.facts[:0]
|
||||
for _, o := range n.Tail() {
|
||||
if err := q.bcheckStatement(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
q.facts = q.facts[:0]
|
||||
return nil
|
||||
|
||||
case a.KJump:
|
||||
@@ -326,7 +334,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
||||
// TODO.
|
||||
|
||||
case a.KVar:
|
||||
return q.bcheckVar(n.Var())
|
||||
return q.bcheckVar(n.Var(), false)
|
||||
|
||||
case a.KWhile:
|
||||
return q.bcheckWhile(n.While())
|
||||
@@ -715,12 +723,17 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *checker) bcheckVar(n *a.Var) error {
|
||||
func (q *checker) bcheckVar(n *a.Var, iterateVariable bool) error {
|
||||
if innTyp := n.XType().Innermost(); innTyp.IsRefined() {
|
||||
if _, _, err := typeBounds(q.tm, innTyp); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if iterateVariable {
|
||||
// TODO.
|
||||
return nil
|
||||
}
|
||||
|
||||
lhs := a.NewExpr(a.FlagsTypeChecked, 0, 0, n.Name(), nil, nil, nil, nil)
|
||||
lhs.SetMType(n.XType())
|
||||
rhs := n.Value()
|
||||
|
||||
+12
-7
@@ -51,6 +51,9 @@ func (q *checker) tcheckVars(block []*a.Node) error {
|
||||
if err := q.tcheckVars(o.Iterate().Body()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.tcheckVars(o.Iterate().Tail()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KVar:
|
||||
o := o.Var()
|
||||
@@ -142,18 +145,13 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
|
||||
case a.KIterate:
|
||||
n := n.Iterate()
|
||||
unroll := n.UnrollCount()
|
||||
if err := q.tcheckExpr(unroll, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if cv := unroll.ConstValue(); cv == nil {
|
||||
return fmt.Errorf("check: unroll count %q is not constant", unroll.Str(q.tm))
|
||||
}
|
||||
for _, o := range n.Variables() {
|
||||
if err := q.tcheckStatement(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO: prohibit jumps (breaks, continues), rets (returns, yields) and
|
||||
// retry-calling ? methods while inside an iterate body.
|
||||
if err := q.tcheckLoop(n); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -370,6 +368,13 @@ func (q *checker) tcheckLoop(n a.Loop) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if n, ok := n.(a.TailedLoop); ok {
|
||||
for _, o := range n.Tail() {
|
||||
if err := q.tcheckStatement(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+38
-13
@@ -633,21 +633,27 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
return nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
// TODO: allow expressions, not just literals, as unroll counts?
|
||||
unrollID := p.peek1()
|
||||
unrollStr := p.tm.ByID(unrollID)
|
||||
if !unrollID.IsLiteral(p.tm) {
|
||||
return nil, fmt.Errorf(`parse: expected literal unroll count, got %q at %s:%d`,
|
||||
unrollStr, p.filename, p.line())
|
||||
|
||||
unroll := p.peek1()
|
||||
if unroll.SmallPowerOf2Value() == 0 {
|
||||
return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`,
|
||||
p.tm.ByID(unroll), p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
switch unrollStr {
|
||||
default:
|
||||
return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`,
|
||||
unrollStr, p.filename, p.line())
|
||||
case "1", "2", "4", "8", "16", "32", "64", "128", "256":
|
||||
|
||||
if x := p.peek1(); x != t.IDComma {
|
||||
got := p.tm.ByID(x)
|
||||
return nil, fmt.Errorf(`parse: expected ",", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
unroll := a.NewExpr(0, 0, 0, unrollID, nil, nil, nil, nil)
|
||||
p.src = p.src[1:]
|
||||
|
||||
step := p.peek1()
|
||||
if step.SmallPowerOf2Value() == 0 {
|
||||
return nil, fmt.Errorf(`parse: expected power-of-2 step count in [1..256], got %q at %s:%d`,
|
||||
p.tm.ByID(step), p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
|
||||
if x := p.peek1(); x != t.IDCloseBracket {
|
||||
got := p.tm.ByID(x)
|
||||
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
|
||||
@@ -666,7 +672,26 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewIterate(label, unroll, vars, asserts, body).Node(), nil
|
||||
|
||||
tail := ([]*a.Node)(nil)
|
||||
if step == t.ID1 {
|
||||
if x := p.peek1(); x == t.IDElse {
|
||||
got := p.tm.ByID(x)
|
||||
return nil, fmt.Errorf(`parse: expected non-"else", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
} else {
|
||||
if x := p.peek1(); x != t.IDElse {
|
||||
got := p.tm.ByID(x)
|
||||
return nil, fmt.Errorf(`parse: expected "else", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
tail, err = p.parseBlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return a.NewIterate(unroll, step, label, vars, asserts, body, tail).Node(), nil
|
||||
|
||||
case t.IDReturn, t.IDYield:
|
||||
p.src = p.src[1:]
|
||||
|
||||
@@ -34,7 +34,7 @@ pub func hasher.update(x slice base.u8)(checksum base.u32) {
|
||||
in.x = in.x[:5552]
|
||||
}
|
||||
|
||||
iterate[8](p ptr base.u8:in.x) {
|
||||
iterate[8, 1](p ptr base.u8:in.x) {
|
||||
s1 ~mod+= deref p as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ pub struct ieee_hasher?(
|
||||
pub func ieee_hasher.update(x slice base.u8)(checksum base.u32) {
|
||||
var s base.u32 = 0xFFFFFFFF ^ this.state
|
||||
|
||||
iterate[1](p ptr base.u8:in.x) {
|
||||
iterate[1, 8](p ptr base.u8:in.x) {
|
||||
// TODO.
|
||||
} else {
|
||||
s = ieee_table[0][((s & 0xFF) as base.u8) ^ deref p] ^ (s >> 8)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user