diff --git a/internal/cgen/func.go b/internal/cgen/func.go index ac5c6f0f..88ea9132 100644 --- a/internal/cgen/func.go +++ b/internal/cgen/func.go @@ -39,6 +39,7 @@ type funk struct { varResumables map[t.ID]bool derivedVars map[t.ID]struct{} jumpTargets map[a.Loop]string + activeLoops a.LoopStack coroSuspPoint uint32 ioManips uint32 tempW uint32 diff --git a/internal/cgen/statement.go b/internal/cgen/statement.go index 41074a1d..08effef7 100644 --- a/internal/cgen/statement.go +++ b/internal/cgen/statement.go @@ -473,6 +473,7 @@ func (g *gen) writeStatementIterate(b *buffer, n *a.Iterate, depth uint32) error return nil } name0 := assigns[0].AsAssign().LHS().Ident().Str(g.tm) + g.currFunk.activeLoops.Push(n) b.writes("{\n") // TODO: don't assume that the slice is a slice of base.u8. In @@ -526,6 +527,7 @@ func (g *gen) writeStatementIterate(b *buffer, n *a.Iterate, depth uint32) error } b.writes("}\n") + g.currFunk.activeLoops.Pop() return nil } @@ -538,6 +540,13 @@ func (g *gen) writeStatementJump(b *buffer, n *a.Jump, depth uint32) error { if n.Keyword() == t.IDBreak { keyword = "break" } + // TODO: apply to all generated code, not just + // wuffs_jpeg__decoder__fill_bitstream. + if (n.JumpTarget() == g.currFunk.activeLoops.Top()) && + (g.currFunk.cName == "wuffs_jpeg__decoder__fill_bitstream") { + b.printf("%s;\n", keyword) + return nil + } b.printf("goto label__%s__%s;\n", jt, keyword) return nil } @@ -624,13 +633,19 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error { } func (g *gen) writeStatementWhile(b *buffer, n *a.While, depth uint32) error { - if n.HasContinue() { + // TODO: apply to all generated code, not just + // wuffs_jpeg__decoder__fill_bitstream. + stagedRollout := g.currFunk.cName != "wuffs_jpeg__decoder__fill_bitstream" + + if n.HasDeepContinue() || (stagedRollout && n.HasContinue()) { jt, err := g.currFunk.jumpTarget(g.tm, n) if err != nil { return err } b.printf("label__%s__continue:;\n", jt) } + g.currFunk.activeLoops.Push(n) + condition := buffer(nil) if err := g.writeExpr(&condition, n.Condition(), false, 0); err != nil { return err @@ -643,7 +658,9 @@ func (g *gen) writeStatementWhile(b *buffer, n *a.While, depth uint32) error { } } b.writes("}\n") - if n.HasBreak() { + + g.currFunk.activeLoops.Pop() + if n.HasDeepBreak() || (stagedRollout && n.HasBreak()) { jt, err := g.currFunk.jumpTarget(g.tm, n) if err != nil { return err diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 85b73ebb..59f7dc42 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -100,15 +100,31 @@ const ( FlagsPublic = Flags(0x00000100) FlagsHasBreak = Flags(0x00000200) FlagsHasContinue = Flags(0x00000400) - FlagsGlobalIdent = Flags(0x00000800) - FlagsClassy = Flags(0x00001000) - FlagsSubExprHasEffect = Flags(0x00002000) - FlagsRetsError = Flags(0x00004000) - FlagsPrivateData = Flags(0x00008000) - FlagsChoosy = Flags(0x00010000) - FlagsHasChooseCPUArch = Flags(0x00020000) + FlagsHasDeepBreak = Flags(0x00000800) + FlagsHasDeepContinue = Flags(0x00001000) + FlagsGlobalIdent = Flags(0x00002000) + FlagsClassy = Flags(0x00004000) + FlagsSubExprHasEffect = Flags(0x00008000) + FlagsRetsError = Flags(0x00010000) + FlagsPrivateData = Flags(0x00020000) + FlagsChoosy = Flags(0x00040000) + FlagsHasChooseCPUArch = Flags(0x00080000) ) +func breakFlags(deep bool) Flags { + if deep { + return FlagsHasBreak | FlagsHasDeepBreak + } + return FlagsHasBreak +} + +func continueFlags(deep bool) Flags { + if deep { + return FlagsHasContinue | FlagsHasDeepContinue + } + return FlagsHasContinue +} + func (f Flags) AsEffect() Effect { return Effect(f) } type Effect uint8 @@ -253,12 +269,14 @@ type Loop interface { AsNode() *Node HasBreak() bool HasContinue() bool + HasDeepBreak() bool + HasDeepContinue() bool Keyword() t.ID Label() t.ID Asserts() []*Node Body() []*Node - SetHasBreak() - SetHasContinue() + SetHasBreak(deep bool) + SetHasContinue(deep bool) } type LoopStack []Loop @@ -282,6 +300,10 @@ func (s *LoopStack) Pop() Loop { return l } +func (s *LoopStack) Top() Loop { + return (*s)[len(*s)-1] +} + type Raw Node func (n *Raw) AsNode() *Node { return (*Node)(n) } @@ -632,6 +654,8 @@ type Iterate Node func (n *Iterate) AsNode() *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) HasDeepBreak() bool { return n.flags&FlagsHasDeepBreak != 0 } +func (n *Iterate) HasDeepContinue() bool { return n.flags&FlagsHasDeepContinue != 0 } func (n *Iterate) Keyword() t.ID { return t.IDIterate } func (n *Iterate) Advance() t.ID { return n.id0 } func (n *Iterate) Label() t.ID { return n.id1 } @@ -645,8 +669,8 @@ func (n *Iterate) Body() []*Node { return n.list2 } func (n *Iterate) SetBody(body []*Node) { n.list2 = body } func (n *Iterate) SetElseIterate(o *Iterate) { n.rhs = o.AsNode() } -func (n *Iterate) SetHasBreak() { n.flags |= FlagsHasBreak } -func (n *Iterate) SetHasContinue() { n.flags |= FlagsHasContinue } +func (n *Iterate) SetHasBreak(deep bool) { n.flags |= breakFlags(deep) } +func (n *Iterate) SetHasContinue(deep bool) { n.flags |= continueFlags(deep) } func NewIterate(label t.ID, assigns []*Node, length t.ID, advance t.ID, unroll t.ID, asserts []*Node) *Iterate { return &Iterate{ @@ -671,18 +695,20 @@ func NewIterate(label t.ID, assigns []*Node, length t.ID, advance t.ID, unroll t // TODO: should we be able to unroll while loops too? type While Node -func (n *While) AsNode() *Node { return (*Node)(n) } -func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 } -func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 } -func (n *While) Keyword() t.ID { return t.IDWhile } -func (n *While) Label() t.ID { return n.id1 } -func (n *While) Condition() *Expr { return n.mhs.AsExpr() } -func (n *While) Asserts() []*Node { return n.list1 } -func (n *While) Body() []*Node { return n.list2 } +func (n *While) AsNode() *Node { return (*Node)(n) } +func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 } +func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 } +func (n *While) HasDeepBreak() bool { return n.flags&FlagsHasDeepBreak != 0 } +func (n *While) HasDeepContinue() bool { return n.flags&FlagsHasDeepContinue != 0 } +func (n *While) Keyword() t.ID { return t.IDWhile } +func (n *While) Label() t.ID { return n.id1 } +func (n *While) Condition() *Expr { return n.mhs.AsExpr() } +func (n *While) Asserts() []*Node { return n.list1 } +func (n *While) Body() []*Node { return n.list2 } -func (n *While) SetBody(body []*Node) { n.list2 = body } -func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak } -func (n *While) SetHasContinue() { n.flags |= FlagsHasContinue } +func (n *While) SetBody(body []*Node) { n.list2 = body } +func (n *While) SetHasBreak(deep bool) { n.flags |= breakFlags(deep) } +func (n *While) SetHasContinue(deep bool) { n.flags |= continueFlags(deep) } func (n *While) IsWhileTrue() bool { condition := n.mhs.AsExpr() diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 0d9bb813..fae47e0d 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -805,7 +805,7 @@ func (p *parser) parseStatement1() (*a.Node, error) { if len(p.loops) == 0 { // No-op. } else if label == 0 { - loop = p.loops[len(p.loops)-1] + loop = p.loops.Top() if loop.Label() != 0 { return nil, fmt.Errorf(`parse: unlabeled %s for labeled %s.%s at %s:%d`, x.Str(p.tm), loop.Keyword().Str(p.tm), loop.Label().Str(p.tm), p.filename, p.line()) @@ -827,10 +827,11 @@ func (p *parser) parseStatement1() (*a.Node, error) { x.Str(p.tm), sepStr, labelStr, p.filename, p.line()) } + deep := loop != p.loops.Top() if x == t.IDBreak { - loop.SetHasBreak() + loop.SetHasBreak(deep) } else { - loop.SetHasContinue() + loop.SetHasContinue(deep) } n := a.NewJump(x, label) n.SetJumpTarget(loop) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 85fa5b23..4aec3143 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -41415,25 +41415,23 @@ wuffs_jpeg__decoder__fill_bitstream( self->private_impl.f_bitstream_wi = v_wi; } v_wi = self->private_impl.f_bitstream_wi; - label__0__continue:; while ((v_wi < 2048u) && (((uint64_t)(io2_a_src - iop_a_src)) > 0u)) { v_c = wuffs_base__peek_u8be__no_bounds_check(iop_a_src); if (v_c < 255u) { self->private_data.f_bitstream_buffer[v_wi] = v_c; v_wi += 1u; iop_a_src += 1u; - goto label__0__continue; + continue; } else if (((uint64_t)(io2_a_src - iop_a_src)) <= 1u) { - goto label__0__break; + break; } else if ((wuffs_base__peek_u16le__no_bounds_check(iop_a_src) >> 8u) > 0u) { - goto label__0__break; + break; } else { self->private_data.f_bitstream_buffer[v_wi] = 255u; v_wi += 1u; iop_a_src += 2u; } } - label__0__break:; if (((uint64_t)(io2_a_src - iop_a_src)) > 1u) { if ((wuffs_base__peek_u8be__no_bounds_check(iop_a_src) >= 255u) && ((wuffs_base__peek_u16le__no_bounds_check(iop_a_src) >> 8u) > 0u)) { v_new_wi = (wuffs_base__u32__min(v_wi, 1784u) + 264u);