From 497a8776c29e0860ea398ed529ff8528ff35bd30 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 22 Jun 2017 14:56:29 +1000 Subject: [PATCH] Generate C code for "this.lzw.set_literal_width". --- cmd/puffs-gen-c/main.go | 19 +++++++++++++++++++ gen/c/std/gif.c | 8 +++++--- lang/check/bounds.go | 12 ++++++++++++ lang/check/type.go | 25 +++++++++++++++++++++++++ std/gif/decode_gif.puffs | 6 +++--- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 23864532..d9a97fa1 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -774,6 +774,16 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { if n.CallSuspendible() { return nil } + // TODO: delete this hack that only matches "foo.set_literal_width(etc)". + if isSetLiteralWidth(g.tm, n) { + g.printf("puffs_%s_lzw_decoder_set_literal_width(&self->private_impl.f_lzw, ", g.pkgName) + a := n.Args()[0].Arg().Value() + if err := g.writeExpr(a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + g.writes(");\n") + return nil + } return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible") case a.KIf: @@ -1261,6 +1271,15 @@ func isLowBits(tm *t.Map, n *a.Expr) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits } +func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { + // TODO: check that n.Args() is "(lw:bar)". + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width") +} + func (g *gen) writeExprUnaryOp(n *a.Expr, rp replacementPolicy, depth uint32) error { // TODO. return nil diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 21170eba..ff5d2eed 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -504,7 +504,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, uint8_t v_c[9]; uint32_t v_i; bool v_interlace; - uint8_t v_literal_width; + uint8_t v_lw; uint8_t v_block_size; for (size_t i = 0; i < 9; i++) { @@ -533,10 +533,12 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, return status; } uint8_t t_1 = a_src.buf->ptr[a_src.buf->ri++]; - v_literal_width = t_1; - if ((v_literal_width < 2) || (8 < v_literal_width)) { + v_lw = t_1; + if ((v_lw < 2) || (8 < v_lw)) { return puffs_gif_error_bad_lzw_literal_width; } + puffs_gif_lzw_decoder_set_literal_width(&self->private_impl.f_lzw, + ((uint32_t)(v_lw))); while (true) { if (a_src.buf->ri >= a_src.buf->wi) { status = a_src.buf->closed ? puffs_gif_error_unexpected_eof diff --git a/lang/check/bounds.go b/lang/check/bounds.go index fa9d3626..32d0fa58 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -651,6 +651,18 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } return zero, bitMask(int(aMax.Int64())), nil } + // TODO: delete this hack that only matches "foo.set_literal_width(etc)". + if isSetLiteralWidth(q.tm, n) { + a := n.Args()[0].Arg().Value() + aMin, aMax, err := q.bcheckExpr(a, depth) + if err != nil { + return nil, nil, err + } + if aMin.Cmp(big.NewInt(2)) < 0 || aMax.Cmp(big.NewInt(8)) > 0 { + return nil, nil, fmt.Errorf("check: %q not in range [2..8]", a.String(q.tm)) + } + return nil, nil, nil + } return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) case t.KeyOpenBracket: diff --git a/lang/check/type.go b/lang/check/type.go index a1037b17..9c75d254 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -359,6 +359,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(foo.MType().Unrefined()) return nil } + // TODO: delete this hack that only matches "foo.set_literal_width(etc)". + if isSetLiteralWidth(q.tm, n) { + foo := n.LHS().Expr().LHS().Expr() + if err := q.tcheckExpr(foo, depth); err != nil { + return err + } + n.LHS().SetTypeChecked() + n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK. + for _, o := range n.Args() { + if err := q.tcheckArg(o.Arg(), depth); err != nil { + return err + } + } + n.SetMType(typeExprPlaceholder) // HACK. + return nil + } case t.KeyOpenBracket: // n is an index. @@ -448,6 +464,15 @@ func isLowBits(tm *t.Map, n *a.Expr) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits } +func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { + // TODO: check that n.Args() is "(lw:bar)". + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width") +} + func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { lhs := n.LHS().Expr() if err := q.tcheckExpr(lhs, depth); err != nil { diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs index f4870940..18dae5c7 100644 --- a/std/gif/decode_gif.puffs +++ b/std/gif/decode_gif.puffs @@ -137,11 +137,11 @@ pri func decoder.decode_id?(src reader1)() { return error "TODO: unsupported Local Color Table" } - var literal_width u8 = in.src.read_u8?() - // TODO: call this.lzw.set_literal_width. - if (literal_width < 2) or (8 < literal_width) { + var lw u8 = in.src.read_u8?() + if (lw < 2) or (8 < lw) { return error "bad LZW literal width" } + this.lzw.set_literal_width(lw:lw as u32) // TODO: pass the data blocks' payload to this.lzw. while true {