diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 7a7e4b9c..287561c2 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -814,6 +814,19 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro case t.KeyOpenParen: // n is a function call. + // TODO: delete this hack that only matches "foo.low_bits(etc)". + if isLowBits(g.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 { + g.printf("PUFFS_LOW_BITS(") + if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, depth); err != nil { + return err + } + g.writes(",") + if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, depth); err != nil { + return err + } + g.writes(")") + return nil + } // TODO. case t.KeyOpenBracket: @@ -851,6 +864,11 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { 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)". + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits +} + func (g *gen) writeExprUnaryOp(n *a.Expr, rp replacementPolicy, depth uint32) error { // TODO. return nil diff --git a/gen/c/gif.c b/gen/c/gif.c index f881af21..269b10ad 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -211,6 +211,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, uint32_t v_n_bits; uint32_t v_clear_code; uint32_t v_width; + uint32_t v_code; v_bits = 0; v_n_bits = 0; @@ -226,9 +227,10 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_bits |= (((uint32_t)(t_0)) << v_n_bits); v_n_bits += 8; } + v_code = PUFFS_LOW_BITS(v_bits, v_width); v_bits >>= v_width; v_n_bits -= v_width; - if ((v_bits < v_clear_code)) { + if ((v_code < v_clear_code)) { goto label_0_break; } goto label_0_break; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index a3d0e2d3..d5318105 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -24,10 +24,11 @@ var numTypeBounds = [256][2]*big.Int{ } var ( - minusOne = big.NewInt(-1) - zero = big.NewInt(+0) - one = big.NewInt(+1) - ffff = big.NewInt(0xFFFF) + minusOne = big.NewInt(-1) + zero = big.NewInt(+0) + one = big.NewInt(+1) + sixtyFour = big.NewInt(+64) + ffff = big.NewInt(0xFFFF) maxIntBits = big.NewInt(t.MaxIntBits) @@ -580,6 +581,24 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 { break } + // TODO: delete this hack that only matches "foo.low_bits(etc)". + if isLowBits(q.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 { + a := n.Args()[0].Arg().Value() + aMin, aMax, err := q.bcheckExpr(a, depth) + if err != nil { + return nil, nil, err + } + if aMin.Cmp(zero) < 0 { + // TODO: error, but a better check than aMin < 0 is that + // a.MType() is u32. Checking this properly should fall out + // when a *a.TypeExpr can express function types. + } + // TODO: sixtyFour should actually be sizeof(n.LHS().Expr()). + if aMax.Cmp(sixtyFour) > 0 { + return nil, nil, fmt.Errorf("check: low_bits argument %q is possibly too large", a.String(q.tm)) + } + return zero, bitMask(int(aMax.Int64())), 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 27187091..19092f96 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -219,6 +219,14 @@ func (q *checker) tcheckAssign(n *a.Assign) error { ) } +func (q *checker) tcheckArg(n *a.Arg, depth uint32) error { + if err := q.tcheckExpr(n.Value(), depth); err != nil { + return err + } + n.Node().SetTypeChecked() + return nil +} + func (q *checker) tcheckExpr(n *a.Expr, depth uint32) error { if depth > a.MaxExprDepth { return fmt.Errorf("check: expression recursion depth too large") @@ -298,7 +306,23 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { return err } - n.SetMType(TypeExprU8) + n.SetMType(TypeExprU8) // HACK. + return nil + } + // TODO: delete this hack that only matches "foo.low_bits(etc)". + if isLowBits(q.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 { + foo := n.LHS().Expr().LHS().Expr() + if err := q.tcheckExpr(foo, depth); err != nil { + return err + } + n.LHS().SetTypeChecked() + n.LHS().Expr().SetMType(TypeExprU8) // HACK. + for _, o := range n.Args() { + if err := q.tcheckArg(o.Arg(), depth); err != nil { + return err + } + } + n.SetMType(foo.MType()) return nil } @@ -328,6 +352,11 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { 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)". + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits +} + 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/lang/token/list.go b/lang/token/list.go index a3423f5f..3c689241 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -230,6 +230,8 @@ const ( KeyIn = Key(IDIn >> KeyShift) KeyOut = Key(IDOut >> KeyShift) + KeyLowBits = Key(IDLowBits >> KeyShift) + KeyReadU8 = Key(IDReadU8 >> KeyShift) KeyReadU16BE = Key(IDReadU16BE >> KeyShift) KeyReadU16LE = Key(IDReadU16LE >> KeyShift) @@ -377,6 +379,8 @@ const ( IDIn = ID(0xA2<>= width n_bits -= width - // TODO: s/bits/code/ - if bits < clear_code { + if code < clear_code { break }