diff --git a/gen/c/gif.c b/gen/c/gif.c index 521ac01a..71f13c5b 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -167,6 +167,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) { v_bits |= 0; v_n_bits += 8; } + v_bits >>= v_width; goto label_0_break; v_n_bits = 0; } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index e181ca32..fe0683b2 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -28,6 +28,8 @@ var ( one = big.NewInt(1) ffff = big.NewInt(0xFFFF) + maxIntBits = big.NewInt(t.MaxIntBits) + zeroExpr = a.NewExpr(a.FlagsTypeChecked, 0, t.IDZero, nil, nil, nil, nil) ) @@ -511,8 +513,25 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u return zero, zero, nil } case t.KeyXBinarySlash: + case t.KeyXBinaryShiftL: + case t.KeyXBinaryShiftR: + if lMin.Cmp(zero) < 0 { + return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap)) + } + if rMin.Cmp(zero) < 0 { + return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap)) + } + if rMin.Cmp(maxIntBits) >= 0 { + return zero, zero, nil + } + nMax := big.NewInt(0).Rsh(lMax, uint(rMin.Uint64())) + if rMax.Cmp(maxIntBits) >= 0 { + return zero, nMax, nil + } + return big.NewInt(0).Rsh(lMin, uint(rMax.Uint64())), nMax, nil + case t.KeyXBinaryAmp, t.KeyXBinaryPipe: // TODO: should type-checking ensure that bitwise ops only apply to // *unsigned* integer types? @@ -537,6 +556,7 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u // Return [0, z rounded up to the next power-of-2-minus-1]. This is // conservative, but works fine in practice. return zero, bitMask(z.BitLen()), nil + case t.KeyXBinaryAmpHat: case t.KeyXBinaryHat: diff --git a/lang/token/list.go b/lang/token/list.go index 402b0158..a9649359 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -3,6 +3,10 @@ package token +// MaxIntBits is the largest size (in bits) of the i8, u8, i16, u16, etc. +// integer types. +const MaxIntBits = 64 + // Flags are the low 16 bits of an ID. For example, they signify whether a // token is an operator, an identifier, etc. // @@ -471,6 +475,8 @@ var builtInsByKey = [nBuiltInKeys]struct { KeyTrue: {"true", IDTrue}, KeyZero: {"0", IDZero}, + // Change MaxIntBits if a future update adds an i128 or u128 type. + KeyI8: {"i8", IDI8}, KeyI16: {"i16", IDI16}, KeyI32: {"i32", IDI32}, diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index a77e23fe..e387b286 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -29,6 +29,8 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() bits |= 0 // TODO. n_bits += 8 } + // TODO: var code u32[..4095] = bits.low_bits(width) + bits >>= width // TODO: implement. break