diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 7da987f5..b9e3d344 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -687,10 +687,10 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { case a.KAssign: n := n.Assign() - if err := g.writeSuspendibles(n.LHS()); err != nil { + if err := g.writeSuspendibles(n.LHS(), depth); err != nil { return err } - if err := g.writeSuspendibles(n.RHS()); err != nil { + if err := g.writeSuspendibles(n.RHS(), depth); err != nil { return err } if err := g.writeExpr(n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { @@ -704,6 +704,16 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { g.writes(";\n") return nil + case a.KExpr: + n := n.Expr() + if err := g.writeSuspendibles(n, depth); err != nil { + return err + } + if n.CallSuspendible() { + return nil + } + return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible") + case a.KIf: // TODO: consider suspendible calls. n := n.If() @@ -814,27 +824,32 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind()) } -func (g *gen) writeSuspendibles(n *a.Expr) error { +func (g *gen) writeSuspendibles(n *a.Expr, depth uint32) error { if !n.Suspendible() { return nil } - return g.writeCallSuspendibles(n) + return g.writeCallSuspendibles(n, depth) } -func (g *gen) writeCallSuspendibles(n *a.Expr) error { +func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { // The evaluation order for suspendible calls (which can have side effects) // is important here: LHS, MHS, RHS, Args and finally the node itself. if !n.CallSuspendible() { + if depth > a.MaxExprDepth { + return fmt.Errorf("expression recursion depth too large") + } + depth++ + for _, o := range n.Node().Raw().SubNodes() { if o != nil && o.Kind() == a.KExpr { - if err := g.writeCallSuspendibles(o.Expr()); err != nil { + if err := g.writeCallSuspendibles(o.Expr(), depth); err != nil { return err } } } for _, o := range n.Args() { if o != nil && o.Kind() == a.KExpr { - if err := g.writeCallSuspendibles(o.Expr()); err != nil { + if err := g.writeCallSuspendibles(o.Expr(), depth); err != nil { return err } } @@ -866,6 +881,26 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error { return err } g.printf(" %s%d = %ssrc->ptr[%ssrc->ri++];\n", tPrefix, temp, aPrefix, aPrefix) + + // TODO: delete this hack that only matches "in.dst.write_u8?()". + } else if isInDstWriteU8(g.tm, n) { + // TODO: suspend coroutine state. + g.printf("if (%sdst->wi >= %sdst->len) { status = puffs_%s_status_short_write;", + aPrefix, aPrefix, g.pkgName) + if g.perFunc.public && g.perFunc.suspendible { + g.writes("goto cleanup0;") + } else { + g.writes("return status;") + } + g.writes("}\n") + g.printf("%sdst->ptr[%sdst->wi++] = ", aPrefix, aPrefix) + x := n.Args()[0].Arg().Value() + if err := g.writeExpr(x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + g.writes(";\n") + return nil + } else { // TODO: fix this. // @@ -1000,6 +1035,23 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { return n.ID0() == 0 && n.ID1().Key() == t.KeyIn } +func isInDstWriteU8(tm *t.Map, n *a.Expr) bool { + // TODO: check that n.Args() is "(x:bar)". + if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("dst") { + return false + } + n = n.LHS().Expr() + return n.ID0() == 0 && n.ID1().Key() == t.KeyIn +} + func isLowBits(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(n:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/gen/c/gif.c b/gen/c/gif.c index b8b8dde7..b77201c6 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -246,6 +246,11 @@ label_0_continue:; v_bits >>= v_width; v_n_bits -= v_width; if (v_code < v_clear_code) { + if (a_dst->wi >= a_dst->len) { + status = puffs_gif_status_short_write; + goto cleanup0; + } + a_dst->ptr[a_dst->wi++] = ((uint8_t)(v_code)); } else if (v_code == v_clear_code) { v_use_save_code = false; v_save_code = v_end_code; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index d8826fc2..c629bc34 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -509,6 +509,7 @@ const MaxBodyDepth = 255 // Statement means one of: // - Assert // - Assign +// - Expr // - If // - Jump // - Return diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 1c2d6036..efb5a225 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -169,6 +169,10 @@ func (q *checker) bcheckStatement(n *a.Node) error { n := n.Assign() return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS()) + case a.KExpr: + _, _, err := q.bcheckExpr(n.Expr(), 0) + return err + case a.KIf: return q.bcheckIf(n.If()) @@ -589,8 +593,20 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, // No-op. case t.KeyOpenParen: - // TODO: delete this hack that only matches "in.src.read_u8?()". - if isInSrcReadU8(q.tm, n) { + // TODO: delete this hack that only matches "in.src.read_u8?()" and + // "in.dst.write_u8?(x:bar)". + if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) { + for _, o := range n.Args() { + // TODO: check that the arg range at the caller and the + // signature are compatible. + oMin, oMax, err := q.bcheckExpr(o.Arg().Value(), depth) + if err != nil { + return nil, nil, err + } + // TODO: check that oMin and oMax is within the function's + // declared arg bounds. + _, _ = oMin, oMax + } break } // TODO: delete this hack that only matches "foo.low_bits(etc)". diff --git a/lang/check/type.go b/lang/check/type.go index 8c814177..d684dd4f 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -50,6 +50,9 @@ func (q *checker) tcheckStatement(n *a.Node) error { return err } + case a.KExpr: + return q.tcheckExpr(n.Expr(), 0) + case a.KIf: for n := n.If(); n != nil; n = n.ElseIf() { cond := n.Condition() @@ -313,11 +316,17 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyOpenParen: // n is a function call. - // TODO: delete this hack that only matches "in.src.read_u8?()". - if isInSrcReadU8(q.tm, n) { + // TODO: delete this hack that only matches "in.src.read_u8?()" and + // "in.dst.write_u8?(x:bar)". + if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) { if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { return err } + for _, o := range n.Args() { + if err := q.tcheckArg(o.Arg(), depth); err != nil { + return err + } + } n.SetMType(TypeExprU8) // HACK. return nil } @@ -368,6 +377,23 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { return n.ID0() == 0 && n.ID1().Key() == t.KeyIn } +func isInDstWriteU8(tm *t.Map, n *a.Expr) bool { + // TODO: check that n.Args() is "(x:bar)". + if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("dst") { + return false + } + n = n.LHS().Expr() + return n.ID0() == 0 && n.ID1().Key() == t.KeyIn +} + func isLowBits(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(n:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 1737a972..e36240d2 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -43,6 +43,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { n_bits -= width if code < clear_code { + in.dst.write_u8?(x:code as u8) // TODO: if use_save_code { etc } } else if code == clear_code { diff --git a/test/c/gif/lzw.c b/test/c/gif/lzw.c index b1f91b33..b358f04d 100644 --- a/test/c/gif/lzw.c +++ b/test/c/gif/lzw.c @@ -30,7 +30,7 @@ void test_lzw_decode() { } // That .giflzw file should be 13382 bytes long. if (src.wi != 13382) { - FAIL("test_lzw_decode: file size: got %d, want %d", (int)(src.wi), 13382); + FAIL("test_lzw_decode: src size: got %d, want %d", (int)(src.wi), 13382); goto cleanup0; } // The first byte in that file, the LZW literal width, should be 0x08. @@ -51,6 +51,21 @@ void test_lzw_decode() { puffs_gif_status_ok); goto cleanup1; } + + // The decoded per-pixel indexes should be 3982 bytes long. + // + // TODO: s/3982/19200/ as 19200 = 160 * 120. + if (dst.wi != 3982) { + FAIL("test_lzw_decode: dst size: got %d, want %d", (int)(dst.wi), 3982); + goto cleanup1; + } + // The first decoded byte should be 0xDC. + if (dst.ptr[0] != 0xDC) { + FAIL("test_lzw_decode: first decoded byte: got 0x%02x, want 0x%02x", + (int)(dst.ptr[0]), 0xDC); + goto cleanup1; + } + cleanup1: puffs_gif_lzw_decoder_destructor(&dec); cleanup0:;