From 019e83f19056c7dc8ba257a88119fc7b50a8aa6b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 11 Jun 2017 13:58:12 +1000 Subject: [PATCH] Add lzw_decoder.set_literal_width. --- cmd/puffs-gen-c/main.go | 105 ++++++++++++++++++++++++++++++++------- gen/c/gif.c | 23 +++++++++ gen/h/gif.h | 3 ++ lang/ast/ast.go | 2 +- lang/check/bounds.go | 13 +++++ std/gif/decode_lzw.puffs | 4 ++ test/c/gif/basic.c | 46 +++++++++++++---- test/c/gif/gif.c | 5 +- 8 files changed, 170 insertions(+), 31 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 6d224297..deb61eb2 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -523,20 +523,27 @@ func (g *gen) writeFuncImpl(n *a.Func) error { } g.writes("{\n") - // Check the previous status and the args. + // Check the previous status and the "self" arg. if n.Public() { g.perFunc.public = true if n.Receiver() != 0 { - g.printf("if (!self) { return puffs_%s_error_bad_receiver; }\n", g.pkgName) + g.writes("if (!self) {\n") + if n.Suspendible() { + g.printf("return puffs_%s_error_bad_receiver;", g.pkgName) + } else { + g.printf("return;") + } + g.writes("}\n") } } + if n.Suspendible() { g.perFunc.suspendible = true g.printf("puffs_%s_status status = ", g.pkgName) if n.Receiver() != 0 { - g.printf("self->private_impl.status;\n") + g.writes("self->private_impl.status;\n") if n.Public() { - g.printf("if (status & 1) { return status; }") + g.writes("if (status & 1) { return status; }") } } else { g.printf("puffs_%s_status_ok;\n", g.pkgName) @@ -545,35 +552,76 @@ func (g *gen) writeFuncImpl(n *a.Func) error { g.printf("if (self->private_impl.magic != PUFFS_MAGIC) {"+ "status = puffs_%s_error_constructor_not_called; goto cleanup0; }\n", g.pkgName) } - } else if r := n.Receiver(); r != 0 { - // TODO: fix this. - return fmt.Errorf(`cannot convert Puffs function "%s.%s" to C`, - r.String(g.tm), n.Name().String(g.tm)) + } else if n.Receiver() != 0 && n.Public() { + g.writes("if (self->private_impl.status & 1) { return; }") + g.printf("if (self->private_impl.magic != PUFFS_MAGIC) {"+ + "self->private_impl.status = puffs_%s_error_constructor_not_called; return; }\n", g.pkgName) } + + // For public functions, check the other args for bounds and null-ness. if n.Public() { badArg := false for _, o := range n.In().Fields() { o := o.Field() - if o.XType().Decorator().Key() != t.KeyPtr { - // TODO: check for type refinements: u32[..4095] instead of - // u32. Also check for types, for array-typed arguments. + oTyp := o.XType() + if oTyp.Decorator().Key() != t.KeyPtr && !oTyp.IsRefined() { + // TODO: Also check elements, for array-typed arguments. continue } - if badArg { - g.writes(" || ") - } else { - g.writes("if (") + switch { + case oTyp.Decorator().Key() == t.KeyPtr: + if badArg { + g.writes(" || ") + } else { + g.writes("if (") + } + g.printf("!%s%s", aPrefix, o.Name().String(g.tm)) + badArg = true + case oTyp.IsRefined(): + bounds := [2]*big.Int{} + for i, b := range oTyp.Bounds() { + if b != nil { + if cv := b.ConstValue(); cv != nil { + bounds[i] = cv + } + } + } + if key := oTyp.Name().Key(); key < t.Key(len(numTypeBounds)) { + ntb := numTypeBounds[key] + for i := 0; i < 2; i++ { + if bounds[i] != nil && ntb[i] != nil && bounds[i].Cmp(ntb[i]) == 0 { + bounds[i] = nil + continue + } + } + } + for i, b := range bounds { + if badArg { + g.writes(" || ") + } else { + g.writes("if (") + } + g.printf("%s%s", aPrefix, o.Name().String(g.tm)) + if i == 0 { + g.writeb('<') + } else { + g.writeb('>') + } + g.printf("%s", b) + badArg = true + } } - g.printf("!%s%s", aPrefix, o.Name().String(g.tm)) - badArg = true } if badArg { g.writes(") {") if n.Suspendible() { - g.printf("status = puffs_%s_error_bad_argument; goto cleanup0; }\n", g.pkgName) + g.printf("status = puffs_%s_error_bad_argument; goto cleanup0;", g.pkgName) + } else if n.Receiver() != 0 { + g.printf("self->private_impl.status = puffs_%s_error_bad_argument; return;", g.pkgName) } else { - g.printf("return puffs_%s_error_bad_argument; }\n", g.pkgName) + g.printf("return;") } + g.writes("}\n") } } g.writes("\n") @@ -1051,6 +1099,12 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro // TODO. case t.KeyDot: + if n.LHS().Expr().ID1().Key() == t.KeyIn { + g.writes(aPrefix) + g.writes(n.ID1().String(g.tm)) + return nil + } + if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { return err } @@ -1165,6 +1219,19 @@ func (g *gen) writeCTypeName(n *a.TypeExpr) error { return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm)) } +var numTypeBounds = [256][2]*big.Int{ + t.KeyI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)}, + t.KeyI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)}, + t.KeyI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)}, + t.KeyI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)}, + t.KeyU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)}, + t.KeyU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)}, + t.KeyU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)}, + t.KeyU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)}, + t.KeyUsize: {zero, zero}, + t.KeyBool: {zero, one}, +} + var cTypeNames = [...]string{ t.KeyI8: "int8_t", t.KeyI16: "int16_t", diff --git a/gen/c/gif.c b/gen/c/gif.c index 256fb93f..0fc5f450 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -106,6 +106,9 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); // ---------------- Public Function Prototypes +void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, + uint32_t a_lw); + puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, puffs_base_buf1* a_dst, puffs_base_buf1* a_src, @@ -206,6 +209,26 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) { // ---------------- Function Implementations +void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, + uint32_t a_lw) { + if (!self) { + return; + } + if (self->private_impl.status & 1) { + return; + } + if (self->private_impl.magic != PUFFS_MAGIC) { + self->private_impl.status = puffs_gif_error_constructor_not_called; + return; + } + if (a_lw < 2 || a_lw > 8) { + self->private_impl.status = puffs_gif_error_bad_argument; + return; + } + + self->private_impl.f_literal_width = a_lw; +} + puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, puffs_base_buf1* a_dst, puffs_base_buf1* a_src, diff --git a/gen/h/gif.h b/gen/h/gif.h index def5298d..525874fa 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -106,6 +106,9 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); // ---------------- Public Function Prototypes +void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, + uint32_t a_lw); + puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, puffs_base_buf1* a_dst, puffs_base_buf1* a_src, diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 595317be..fb702c76 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -457,7 +457,7 @@ const MaxTypeExprDepth = 63 // refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For // example, the LHS for "u32[..4095]" is nil. // -// TODO: function / method types. +// TODO: function / method types, struct types. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 65ae1606..69f24d4c 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -659,6 +659,19 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) case t.KeyDot: + // TODO: delete this hack that only matches "in". + if n.LHS().Expr().ID1().Key() == t.KeyIn { + for _, o := range q.f.Func.In().Fields() { + o := o.Field() + if o.Name() == n.ID1() { + return q.bcheckTypeExpr(o.XType()) + } + } + lTyp := n.LHS().Expr().MType() + return nil, nil, fmt.Errorf("check: no field named %q found in struct type %q for expression %q", + n.ID1().String(q.tm), lTyp.Name().String(q.tm), n.String(q.tm)) + } + if _, _, err := q.bcheckExpr(n.LHS().Expr(), depth); err != nil { return nil, nil, err } diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 76018579..f878c5ea 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -13,6 +13,10 @@ pub struct lzw_decoder?( prefixes[4096] u16[..4095], ) +pub func lzw_decoder.set_literal_width(lw u32[2..8] = 8)() { + this.literal_width = in.lw +} + pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { // These variables don't change over the lifetime of this func. var clear_code u32[4..256] = (1 as u32) << this.literal_width diff --git a/test/c/gif/basic.c b/test/c/gif/basic.c index 23438eba..d7e47e0e 100644 --- a/test/c/gif/basic.c +++ b/test/c/gif/basic.c @@ -30,7 +30,7 @@ void test_constructor_not_called() { } } -void test_bad_argument() { +void test_bad_argument_null() { test_funcname = __func__; puffs_gif_lzw_decoder dec; puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0); @@ -41,6 +41,33 @@ void test_bad_argument() { } } +void test_bad_argument_out_of_range() { + test_funcname = __func__; + puffs_gif_lzw_decoder dec; + puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0); + + // Setting to 8 is in the [2..8] range. + puffs_gif_lzw_decoder_set_literal_width(&dec, 8); + if (dec.private_impl.status != puffs_gif_status_ok) { + FAIL("status: got %d, want %d", dec.private_impl.status, + puffs_gif_status_ok); + } + + // Setting to 999 is out of range. + puffs_gif_lzw_decoder_set_literal_width(&dec, 999); + if (dec.private_impl.status != puffs_gif_error_bad_argument) { + FAIL("status: got %d, want %d", dec.private_impl.status, + puffs_gif_error_bad_argument); + } + + // That error status code should be sticky. + puffs_gif_lzw_decoder_set_literal_width(&dec, 8); + if (dec.private_impl.status != puffs_gif_error_bad_argument) { + FAIL("status: got %d, want %d", dec.private_impl.status, + puffs_gif_error_bad_argument); + } +} + void test_bad_receiver() { test_funcname = __func__; puffs_base_buf1 dst = {0}; @@ -139,12 +166,13 @@ void test_status_strings() { // The empty comments forces clang-format to place one element per line. test tests[] = { - test_constructor_not_called, // - test_bad_argument, // - test_bad_receiver, // - test_puffs_version_bad, // - test_puffs_version_good, // - test_status_is_error, // - test_status_strings, // - NULL, // + test_constructor_not_called, // + test_bad_argument_null, // + test_bad_argument_out_of_range, // + test_bad_receiver, // + test_puffs_version_bad, // + test_puffs_version_good, // + test_status_is_error, // + test_status_strings, // + NULL, // }; diff --git a/test/c/gif/gif.c b/test/c/gif/gif.c index faf61803..d0668238 100644 --- a/test/c/gif/gif.c +++ b/test/c/gif/gif.c @@ -48,7 +48,8 @@ void test_lzw_decode() { goto cleanup0; } // The first byte in that file, the LZW literal width, should be 0x08. - if (src.ptr[0] != 0x08) { + uint8_t literal_width = src.ptr[0]; + if (literal_width != 0x08) { FAIL("LZW literal width: got %d, want %d", (int)(src.ptr[0]), 0x08); goto cleanup0; } @@ -56,7 +57,7 @@ void test_lzw_decode() { puffs_gif_lzw_decoder dec; puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0); - // TODO: call puffs_gif_lzw_decoder_set_literal_width. + puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width); puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, &got, &src, false); if (status != puffs_gif_status_ok) {