From 2d3b3b1dae8a8c528b0984d02ca29e818e6bb2b8 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 13 May 2018 10:40:44 +1000 Subject: [PATCH] Add an io_writer var to gif's decoder.decode_id --- cmd/wuffs-c/internal/cgen/statement.go | 26 +++++++++++++++++++++----- cmd/wuffs-c/internal/cgen/var.go | 2 ++ gen/c/std/gif.c | 7 +++++++ lang/ast/ast.go | 6 ++++++ lang/check/type.go | 10 ++-------- lang/parse/parse.go | 5 ++++- std/gif/decode_gif.wuffs | 6 +++++- 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index fcc4c568..4aebc0ef 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -165,10 +165,16 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error { b.writes("{\n") for i := 0; i < len(inFields); i++ { e := inFields[i].Expr() + prefix := vPrefix + if e.Operator() != 0 { + prefix = aPrefix + } name := e.Ident().Str(g.tm) + // TODO: stash the whole io_reader, not just its bounds. for j := 0; j < 2; j++ { + // TODO: insert the prefix in %d_%s. b.printf("uint8_t* %s%d_bounds%d_%s = %s%s.private_impl.bounds[%d];\n", - oPrefix, ioBindNum, j, name, aPrefix, name, j) + oPrefix, ioBindNum, j, name, prefix, name, j) } } @@ -180,10 +186,14 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error { for i := len(inFields) - 1; i >= 0; i-- { e := inFields[i].Expr() + prefix := vPrefix + if e.Operator() != 0 { + prefix = aPrefix + } 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) + prefix, name, j, oPrefix, ioBindNum, j, name) } } b.writes("}\n") @@ -352,10 +362,10 @@ func (g *gen) writeStatementVar(b *buffer, n *a.Var, depth uint32) error { return err } } - if n.XType().IsArrayType() { + if nTyp := n.XType(); nTyp.IsArrayType() { if n.Value() != nil { // TODO: something like: - // cv := n.XType().ArrayLength().ConstValue() + // cv := nTyp.ArrayLength().ConstValue() // // TODO: check that cv is within size_t's range. // g.printf("{ size_t i; for (i = 0; i < %d; i++) { %s%s[i] = $DEFAULT_VALUE; }}\n", // cv, vPrefix, n.Name().Str(g.tm)) @@ -371,9 +381,15 @@ func (g *gen) writeStatementVar(b *buffer, n *a.Var, depth uint32) error { if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { return err } - } else if n.XType().IsSliceType() { + } else if nTyp.IsSliceType() { // TODO: don't assume that the slice is a slice of base.u8. b.printf("((wuffs_base__slice_u8){})") + } else if nTyp.IsIOType() { + s := "reader" + if nTyp.QID()[1] == t.IDIOWriter { + s = "writer" + } + b.printf("((wuffs_base__io_%s){})", s) } else { b.writeb('0') } diff --git a/cmd/wuffs-c/internal/cgen/var.go b/cmd/wuffs-c/internal/cgen/var.go index 4bdfd454..6bc0224a 100644 --- a/cmd/wuffs-c/internal/cgen/var.go +++ b/cmd/wuffs-c/internal/cgen/var.go @@ -282,11 +282,13 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp local := fmt.Sprintf("%s%s", vPrefix, n.Name().Str(g.tm)) if typ := n.XType(); typ.HasPointers() { + // TODO: drop the initBoolTypedVars. if suspend || initBoolTypedVars { return nil } rhs := "" + // TODO: move this initialization to writeVars? switch typ.Decorator() { case 0: if qid := typ.QID(); qid[0] == t.IDBase { diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 6a06a9e0..77fa097d 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -2659,6 +2659,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( uint32_t v_i; uint8_t v_lw; uint64_t v_block_size; + wuffs_base__io_writer v_w; wuffs_gif__status v_z; uint8_t* b_rptr_src = NULL; @@ -2682,6 +2683,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( v_i = self->private_impl.c_decode_id[0].v_i; v_lw = self->private_impl.c_decode_id[0].v_lw; v_block_size = self->private_impl.c_decode_id[0].v_block_size; + v_w = ((wuffs_base__io_writer){}); v_z = self->private_impl.c_decode_id[0].v_z; } else { } @@ -2874,9 +2876,12 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( } self->private_impl.f_previous_lzw_decode_ended_abruptly = true; while (true) { + v_w = ((wuffs_base__io_writer){}); { uint8_t* o_0_bounds0_src = a_src.private_impl.bounds[0]; uint8_t* o_0_bounds1_src = a_src.private_impl.bounds[1]; + uint8_t* o_0_bounds0_w = v_w.private_impl.bounds[0]; + uint8_t* o_0_bounds1_w = v_w.private_impl.bounds[1]; wuffs_base__io_reader__set_limit(&a_src, b_rptr_src, v_block_size); wuffs_base__io_reader__set_mark(&a_src, b_rptr_src); { @@ -2900,6 +2905,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id( a_src.private_impl.bounds[0]), }) .len))); + v_w.private_impl.bounds[1] = o_0_bounds1_w; + v_w.private_impl.bounds[0] = o_0_bounds0_w; a_src.private_impl.bounds[1] = o_0_bounds1_src; a_src.private_impl.bounds[0] = o_0_bounds0_src; } diff --git a/lang/ast/ast.go b/lang/ast/ast.go index a9898281..26cc6ac0 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -495,6 +495,8 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field { // IOBind is "io_bind (in_fields) { List2 }": // - List0: in.something fields // - List2: body +// +// TODO: in_fields and "in.something" isn't right. type IOBind Node func (n *IOBind) Node() *Node { return (*Node)(n) } @@ -705,6 +707,10 @@ func (n *TypeExpr) IsIdeal() bool { return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDDoubleZ } +func (n *TypeExpr) IsIOType() bool { + return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDIOReader || n.id2 == t.IDIOWriter) +} + func (n *TypeExpr) IsNumType() bool { return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType() } diff --git a/lang/check/type.go b/lang/check/type.go index fa014d7e..a6bafb2a 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -123,15 +123,9 @@ func (q *checker) tcheckStatement(n *a.Node) error { if err := q.tcheckExpr(o.Expr(), 0); err != nil { return err } - isIOType := false - if typ := o.Expr().MType(); typ.Decorator() == 0 { - if qid := typ.QID(); qid[0] == t.IDBase && (qid[1] == t.IDIOReader || qid[1] == t.IDIOWriter) { - isIOType = true - } - } - if !isIOType { + if typ := o.Expr().MType(); !typ.IsIOType() { return fmt.Errorf("check: io_bind expression %q, of type %q, does not have an I/O type", - o.Expr().Str(q.tm), o.Expr().MType().Str(q.tm)) + o.Expr().Str(q.tm), typ.Str(q.tm)) } } for _, o := range n.Body() { diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 6ed241c2..5d92f586 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -845,7 +845,10 @@ func (p *parser) parseIOBindExprNode() (*a.Node, error) { if err != nil { return nil, err } - if e.Operator() == t.IDDot { + switch e.Operator() { + case 0: + return e.Node(), nil + case t.IDDot: if lhs := e.LHS().Expr(); lhs.Operator() == 0 && lhs.Ident() == t.IDIn { return e.Node(), nil } diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 6522b216..a0edcd4e 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -279,6 +279,8 @@ pri func decoder.decode_ae?(src base.io_reader)() { // been read. // // See the spec section 20 "Image Descriptor" on page 11. +// +// TODO: dst should be a ptr base.image_buffer, not a base.io_writer. pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() { // TODO: check that the frame rect is inside the image rect?? this.frame_left = in.src.read_u16le?() as base.u32 @@ -338,7 +340,8 @@ pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() { this.previous_lzw_decode_ended_abruptly = true while true { - io_bind (in.src) { + var w base.io_writer + io_bind (in.src, w) { // TODO: enforce that calling r.set_limit has a precondition // that r.is_bound(), and that you can't suspend inside an // io_bind? Otherwise, the cgen implementation becomes more @@ -347,6 +350,7 @@ pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() { // Should calling r.set_mark also require r.is_bound()? in.src.set_limit!(l:block_size) in.src.set_mark!() + // TODO: in.dst should be w. var z base.status = try this.lzw.decode?(dst:in.dst, src:in.src) block_size ~sat-= in.src.since_mark().length() }