From 3d1bb5e2a966f4f928d56178c9f2e1c1efbeec2c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 28 Apr 2018 12:22:15 +1000 Subject: [PATCH] Generate temporary io_bind variables --- cmd/wuffs-c/internal/cgen/cgen.go | 7 ++++++- cmd/wuffs-c/internal/cgen/func.go | 1 + cmd/wuffs-c/internal/cgen/statement.go | 29 ++++++++++++++++++++++++++ gen/c/std/gif.c | 4 ++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 2aa8e545..4ce36efa 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -52,6 +52,7 @@ const ( cPrefix = "c_" // Coroutine state. fPrefix = "f_" // Struct field. iPrefix = "i_" // Iterate variable. + oPrefix = "o_" // Temporary io_bind variable. tPrefix = "t_" // Temporary local variable. vPrefix = "v_" // Local variable. ) @@ -145,7 +146,11 @@ const ( priOnly ) -const maxTemp = 10000 +const ( + maxIOBinds = 100 + maxIOBindInFields = 100 + maxTemp = 10000 +) type status struct { name string diff --git a/cmd/wuffs-c/internal/cgen/func.go b/cmd/wuffs-c/internal/cgen/func.go index 677788d9..4ae1628a 100644 --- a/cmd/wuffs-c/internal/cgen/func.go +++ b/cmd/wuffs-c/internal/cgen/func.go @@ -34,6 +34,7 @@ type funk struct { derivedVars map[t.ID]struct{} jumpTargets map[a.Loop]uint32 coroSuspPoint uint32 + ioBinds uint32 tempW uint32 tempR uint32 public bool diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 7c0847fa..b347ab79 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -130,12 +130,41 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { case a.KIOBind: n := n.IOBind() + inFields := n.InFields() + + if g.currFunk.ioBinds > maxIOBinds || len(inFields) > maxIOBindInFields { + return fmt.Errorf("too many temporary variables required") + } + ioBindNum := g.currFunk.ioBinds + g.currFunk.ioBinds++ + + // TODO: do these variables need to be func-scoped (bigger scope) + // instead of block-scoped (smaller scope) if the coro_susp_point + // switch can jump past this initialization?? b.writes("{\n") + for i := 0; i < len(inFields); i++ { + e := inFields[i].Expr() + name := e.Ident().Str(g.tm) + for j := 0; j < 2; j++ { + b.printf("uint8_t* %s%d_bounds%d_%s = %s%s.private_impl.bounds[%d];\n", + oPrefix, ioBindNum, j, name, aPrefix, name, j) + } + } + for _, o := range n.Body() { if err := g.writeStatement(b, o, depth); err != nil { return err } } + + for i := len(inFields) - 1; i >= 0; i-- { + e := inFields[i].Expr() + name := e.Ident().Str(g.tm) + for j := 1; j >= 0; j-- { + b.printf("%s%s.private_impl.bounds[%d] = %s%d_bounds%d_%s;\n", + aPrefix, name, j, oPrefix, ioBindNum, j, name) + } + } b.writes("}\n") return nil diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index cdf02ca4..2ae0a41a 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -2956,6 +2956,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( while (true) { wuffs_base__io_reader__mark(&a_src, b_rptr_src); { + uint8_t* o_0_bounds0_src = a_src.private_impl.bounds[0]; + uint8_t* o_0_bounds1_src = a_src.private_impl.bounds[1]; v_r = a_src; { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15); @@ -2980,6 +2982,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( } v_z = t_14; } + a_src.private_impl.bounds[1] = o_0_bounds1_src; + a_src.private_impl.bounds[0] = o_0_bounds0_src; } if (v_z == 0) { goto label_1_break;