From 587bc2a55de26f283cc7df04d673ab7d500d145f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 9 Jun 2017 18:00:33 +1000 Subject: [PATCH] Generate C code for "a[i] = x". --- cmd/puffs-gen-c/main.go | 12 ++++++++++-- gen/c/gif.c | 8 ++++++++ lang/check/bounds.go | 25 +++++++++++++++++++++++-- lang/check/type.go | 21 ++++++++++++++++++++- std/gif/decode_lzw.puffs | 10 ++++++++-- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index b9e3d344..07d90e33 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -997,8 +997,16 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro // TODO. case t.KeyOpenBracket: - // n is an index. - // TODO. + // n is an index. + if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { + return err + } + g.writeb('[') + if err := g.writeExpr(n.RHS().Expr(), rp, parenthesesOptional, depth); err != nil { + return err + } + g.writeb(']') + return nil case t.KeyColon: // n is a slice. diff --git a/gen/c/gif.c b/gen/c/gif.c index b77201c6..bd5072d9 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -219,6 +219,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, uint32_t v_end_code; bool v_use_save_code; uint32_t v_save_code; + uint32_t v_prev_code; uint32_t v_width; uint32_t v_bits; uint32_t v_n_bits; @@ -228,6 +229,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_end_code = (v_clear_code + 1); v_use_save_code = 0; v_save_code = v_end_code; + v_prev_code = 0; v_width = (self->f_literal_width + 1); v_bits = 0; v_n_bits = 0; @@ -251,9 +253,14 @@ label_0_continue:; goto cleanup0; } a_dst->ptr[a_dst->wi++] = ((uint8_t)(v_code)); + if (v_use_save_code) { + self->f_suffixes[v_save_code] = ((uint8_t)(v_code)); + self->f_prefixes[v_save_code] = ((uint16_t)(v_prev_code)); + } } else if (v_code == v_clear_code) { v_use_save_code = false; v_save_code = v_end_code; + v_prev_code = 0; v_width = (self->f_literal_width + 1); goto label_0_continue; } else if (v_code == v_end_code) { @@ -271,6 +278,7 @@ label_0_continue:; v_width += 1; } } + v_prev_code = v_code; } cleanup0: diff --git a/lang/check/bounds.go b/lang/check/bounds.go index efb5a225..49404552 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -304,6 +304,10 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { return nil } + _, _, err := q.bcheckExpr(lhs, 0) + if err != nil { + return err + } lMin, lMax, err := q.bcheckTypeExpr(lhs.MType()) if err != nil { return err @@ -630,8 +634,25 @@ 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.KeyOpenBracket: - // TODO. - return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) + lhs := n.LHS().Expr() + _, _, err := q.bcheckExpr(lhs, depth) + if err != nil { + return nil, nil, err + } + rhs := n.RHS().Expr() + rMin, rMax, err := q.bcheckExpr(rhs, depth) + if err != nil { + return nil, nil, err + } + cv := lhs.MType().ArrayLength().ConstValue() + if cv == nil { + return nil, nil, fmt.Errorf("check: cannot determine constant array length for %q", + lhs.MType().String(q.tm)) + } + if rMin.Cmp(zero) < 0 || rMax.Cmp(cv) >= 0 { + return nil, nil, fmt.Errorf("check: array index %q (of range %v..%v) out of range", + rhs.String(q.tm), rMin, rMax) + } case t.KeyColon: // TODO. diff --git a/lang/check/type.go b/lang/check/type.go index d684dd4f..da1f981f 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -349,7 +349,26 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyOpenBracket: // n is an index. - // TODO. + lhs := n.LHS().Expr() + if err := q.tcheckExpr(lhs, depth); err != nil { + return err + } + rhs := n.RHS().Expr() + if err := q.tcheckExpr(rhs, depth); err != nil { + return err + } + lTyp := lhs.MType() + if lTyp.PackageOrDecorator().Key() != t.KeyOpenBracket { + return fmt.Errorf("%s is an array-index expression but %s has type %s, not an array type", + n.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm)) + } + rTyp := rhs.MType() + if !rTyp.IsNumType() { + return fmt.Errorf("%s is an array-index expression but %s has type %s, not a numeric type", + n.String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) + } + n.SetMType(lTyp.Inner()) + return nil case t.KeyColon: // n is a slice. diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index e36240d2..39931cbf 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -9,7 +9,7 @@ pub struct lzw_decoder?( literal_width u32[2..8] = 8, stack[4096] u8, suffixes[4096] u8, - prefixes[4096] u16, + prefixes[4096] u16[..4095], ) pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { @@ -20,6 +20,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { // These variables do change. var use_save_code bool var save_code u32[..4095] = end_code + var prev_code u32[..4095] var width u32[..12] = this.literal_width + 1 // These variables yield src's bits in Least Significant Bits order. @@ -44,11 +45,15 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { if code < clear_code { in.dst.write_u8?(x:code as u8) - // TODO: if use_save_code { etc } + if use_save_code { + this.suffixes[save_code] = code as u8 + this.prefixes[save_code] = prev_code as u16 + } } else if code == clear_code { use_save_code = false save_code = end_code + prev_code = 0 width = this.literal_width + 1 continue @@ -69,5 +74,6 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { width += 1 } } + prev_code = code } }