Rename u32, io_reader, etc to base.u32, etc

This commit is contained in:
Nigel Tao committed 2018-04-12 21:49:17 +10:00
1 parent fee97df698
commit f95239f6e0
24 files changed
+394 -392

No files matched your search

+5 -3
View File
@@ -456,7 +456,7 @@ func (g *gen) cName(name string) string {
func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) {
if typ.Decorator() == 0 {
if qid := typ.QID(); qid[0] == 0 {
if qid := typ.QID(); qid[0] == t.IDBase {
switch qid[1].Key() {
case t.KeyU8:
return 1, nil
@@ -710,12 +710,14 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
prefix := g.pkgPrefix
qid := x.QID()
if qid[0] != 0 {
if qid[0] == t.IDBase {
// Base types don't need further initialization.
continue
} else if qid[0] != 0 {
// See gen.writeCTypeName for a related TODO with otherPkg.
otherPkg := g.tm.ByID(qid[0])
prefix = "wuffs_" + otherPkg + "__"
} else if g.structMap[qid] == nil {
// Skip field types like u32 and bool.
continue
}
+13 -13
View File
@@ -158,7 +158,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isThatMethod(g.tm, n, t.KeySuffix, 1) {
// TODO: don't assume that the slice is a slice of u8.
// TODO: don't assume that the slice is a slice of base.u8.
b.writes("wuffs_base__slice_u8_suffix(")
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
@@ -385,7 +385,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return err
}
if lTyp := n.LHS().Expr().MType(); lTyp.IsSliceType() {
// TODO: don't assume that the slice is a slice of u8.
// TODO: don't assume that the slice is a slice of base.u8.
b.writes(".ptr")
}
b.writeb('[')
@@ -411,7 +411,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
lhsIsArray := lhs.MType().IsArrayType()
if lhsIsArray {
// TODO: don't assume that the slice is a slice of u8.
// TODO: don't assume that the slice is a slice of base.u8.
b.writes("((wuffs_base__slice_u8){.ptr=")
}
if err := g.writeExpr(b, lhs, rp, parenthesesOptional, depth); err != nil {
@@ -538,11 +538,11 @@ func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy,
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
// TODO: fix this, allow slices of all types, not just of u8's. Also allow
// arrays of slices, slices of pointers, etc.
// TODO: fix this, allow slices of all types, not just of base.u8's. Also
// allow arrays of slices, slices of pointers, etc.
if n.IsSliceType() {
o := n.Inner()
if o.Decorator() == 0 && o.QID() == (t.QID{0, t.IDU8}) && !o.IsRefined() {
if o.Decorator() == 0 && o.QID() == (t.QID{t.IDBase, t.IDU8}) && !o.IsRefined() {
b.writes("wuffs_base__slice_u8")
b.writeb(' ')
b.writes(varNamePrefix)
@@ -574,7 +574,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
}
fallback := true
if qid := innermost.QID(); qid[0] == 0 {
if qid := innermost.QID(); qid[0] == t.IDBase {
if key := qid[1].Key(); key < t.Key(len(cTypeNames)) {
if s := cTypeNames[key]; s != "" {
b.writes(s)
@@ -585,7 +585,12 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
if fallback {
prefix := g.pkgPrefix
qid := innermost.QID()
if qid[0] != 0 {
if qid == (t.QID{t.IDBase, t.IDStatus}) {
// No-op: special case "base.status" as being inside this package.
//
// TODO: change "base.status" in Wuffs code to just "status"? Or
// change the C code's "wuffs_foo__status" to "wuffs_base__status"?
} else if qid[0] != 0 {
otherPkg := g.tm.ByID(qid[0])
// TODO: map the "deflate" in "deflate.decoder" to the "deflate" in
// `use "std/deflate"`, and use the latter "deflate".
@@ -600,11 +605,6 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
// See gen.writeInitializerImpl for a similar use of otherPkg.
prefix = "wuffs_" + otherPkg + "__"
}
// TODO: remove this hack when "image_config" in Wuffs code becomes
// "base.image_config".
if qid[1] == t.IDImageConfig {
prefix = "wuffs_base__"
}
b.printf("%s%s", prefix, qid[1].Str(g.tm))
}
+1 -1
View File
@@ -350,7 +350,7 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error {
}
}
}
if qid := oTyp.QID(); qid[0] == 0 {
if qid := oTyp.QID(); qid[0] == t.IDBase {
if key := qid[1].Key(); key < t.Key(len(numTypeBounds)) {
ntb := numTypeBounds[key]
for i := 0; i < 2; i++ {
+4 -4
View File
@@ -165,9 +165,9 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
name := v.Name().Str(g.tm)
b.writes("{\n")
// TODO: don't assume that the slice is a slice of u8. In particular,
// the code gen can be subtle if the slice element type has zero size,
// such as the empty struct.
// TODO: don't assume that the slice is a slice of base.u8. In
// particular, the code gen can be subtle if the slice element type has
// zero size, such as the empty struct.
b.printf("wuffs_base__slice_u8 %sslice_%s =", iPrefix, name)
if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
@@ -295,7 +295,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
return err
}
} else if n.XType().IsSliceType() {
// TODO: don't assume that the slice is a slice of u8.
// TODO: don't assume that the slice is a slice of base.u8.
b.printf("((wuffs_base__slice_u8){})")
} else {
b.writeb('0')
+9 -9
View File
@@ -66,7 +66,7 @@ func (g *gen) findDerivedVars() {
switch oTyp.QID() {
default:
continue
case t.QID{0, t.IDIOReader}, t.QID{0, t.IDIOWriter}:
case t.QID{t.IDBase, t.IDIOReader}, t.QID{t.IDBase, t.IDIOWriter}:
// No-op.
}
if !g.needDerivedVar(o.Name()) {
@@ -94,7 +94,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
}
nameStr := name.Str(g.tm)
switch typ.QID() {
case t.QID{0, t.IDIOReader}:
case t.QID{t.IDBase, t.IDIOReader}:
if header {
b.printf("uint8_t* %srptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr)
@@ -122,7 +122,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
b.printf("}\n")
case t.QID{0, t.IDIOWriter}:
case t.QID{t.IDBase, t.IDIOWriter}:
if header {
b.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr)
@@ -171,7 +171,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
}
nameStr := name.Str(g.tm)
switch typ.QID() {
case t.QID{0, t.IDIOReader}:
case t.QID{t.IDBase, t.IDIOReader}:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("size_t n = %srptr_%s - (%s%s.buf->ptr + %s%s.buf->ri);",
@@ -192,7 +192,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
b.printf("}\n")
case t.QID{0, t.IDIOWriter}:
case t.QID{t.IDBase, t.IDIOWriter}:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("size_t n = %swptr_%s - (%s%s.buf->ptr + %s%s.buf->wi);",
@@ -307,13 +307,13 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp
switch typ.Decorator().Key() {
case 0:
if qid := typ.QID(); qid[0] == 0 {
if qid := typ.QID(); qid[0] == t.IDBase {
if key := qid[1].Key(); key < t.Key(len(cTypeNames)) {
rhs = cTypeNames[key]
}
}
case t.KeySlice:
// TODO: don't assume that the slice is a slice of u8.
// TODO: don't assume that the slice is a slice of base.u8.
rhs = "wuffs_base__slice_u8"
}
if rhs != "" {
@@ -327,7 +327,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp
// TODO: don't hard-code [0], and allow recursive coroutines.
if !initBoolTypedVars {
rhs = fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs)
} else if typ.QID() != (t.QID{0, t.IDBool}) {
} else if typ.QID() != (t.QID{t.IDBase, t.IDBool}) {
return nil
} else if typ.Decorator() != 0 {
goto fail
@@ -368,7 +368,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp
break
}
qid := inner.QID()
if qid[0] != 0 {
if qid[0] != t.IDBase {
break
}
switch qid[1].Key() {
+4 -3
View File
@@ -7,14 +7,15 @@
- Added a `skipgendeps` flag.
- Added a `use` keyword.
- Added a `yield` keyword.
- Added an image\_config built-in concept.
- Added `std/crc32` and `std/gzip`.
- Spun `std/zlib` out of `std/flate`.
- Let the `std/zlib` decoder ignore checksums.
- Renamed `std/flate` to `std/deflate`.
- Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc.
- Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`.
- Renamed `iterate.N` to `iterate[N]`.
- Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc.
- Renamed `u32`, `io_reader`, etc to `base.u32`, `base.io_reader`, etc.
- Added a `base.image_config` type.
- Supported animated (not just single frame) GIFs.
- Marked the `std/gif` LZW decoder as private.
- Marked some internal status codes as private.
@@ -37,4 +38,4 @@
---
Updated on March 2018.
Updated on April 2018.
+6 -11
View File
@@ -274,16 +274,11 @@ func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error {
if o.id0.Key() != 0 {
return nil
}
// TODO: Add signed integer types, as per the builtin.Types var?
//
// Or, don't hard code these, and instead require built-in types to
// have qualified names, such as "builtin.u8" or "base.io_reader"?
switch o.id2.Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64, t.KeyBool, t.KeyStatus, t.KeyIOReader, t.KeyIOWriter:
return nil
}
}
if o.id1 == t.IDBase {
return nil
}
if o.id1 != 0 {
return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`,
pkg.Str(tm), o.id1.Str(tm))
@@ -621,8 +616,8 @@ func NewJump(keyword t.ID, label t.ID) *Jump {
// MaxTypeExprDepth is an advisory limit for a TypeExpr's recursion depth.
const MaxTypeExprDepth = 63
// TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr
// T", "array[8] T" or "slice T":
// TypeExpr is a type expression, such as "base.u32", "base.u32[..8]", "foo",
// "pkg.bar", "ptr T", "array[8] T" or "slice T":
// - ID0: <0|IDPtr|IDArray|IDSlice|IDOpenParen>
// - ID1: <0|pkg>
// - ID2: <0|type name>
@@ -646,7 +641,7 @@ const MaxTypeExprDepth = 63
// "foo". ID1 is the "pkg" or zero, ID2 is the "foo".
//
// Numeric types can be refined as "foo[LHS..MHS]". LHS and MHS are Expr's,
// possibly nil. For example, the LHS for "u32[..4095]" is nil.
// possibly nil. For example, the LHS for "base.u32[..4095]" is nil.
//
// TODO: struct types, list types, nptr vs ptr, table types.
type TypeExpr Node
+1 -1
View File
@@ -89,7 +89,7 @@ func (n *TypeExpr) Eq(o *TypeExpr) bool {
}
// EqIgnoringRefinements returns whether n and o are equal, ignoring the
// "[i:j]" in "u32[i:j]".
// "[i:j]" in "base.u32[i:j]".
func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool {
return n.eq(o, true)
}
+3 -3
View File
@@ -52,14 +52,14 @@ func TestString(tt *testing.T) {
"x + 42",
"x and (y < z)",
"x & (y as u8)",
"x & (y as base.u8)",
"x * ((a / b) - (i / j))",
"x + y + z",
"x + (i * j.k[l] * (-m << 4) * (n & o(o0:p, o1:q[:r.s + 5]))) + z",
"x as bool",
"x as u32",
"x as base.bool",
"x as base.u32",
"x as T",
"x as T",
"x as T[i..]",
+6 -6
View File
@@ -170,7 +170,7 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
func typeBounds(tm *t.Map, typ *a.TypeExpr) (*big.Int, *big.Int, error) {
b := [2]*big.Int{}
if typ.Decorator() == 0 {
if qid := typ.QID(); qid[0] == 0 && qid[1].Key() < t.Key(len(numTypeBounds)) {
if qid := typ.QID(); qid[0] == t.IDBase && qid[1].Key() < t.Key(len(numTypeBounds)) {
b = numTypeBounds[qid[1].Key()]
}
}
@@ -827,8 +827,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
if aMin.Sign() < 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.
// a.MType() is base.u32. Checking this properly should fall
// out when a *a.TypeExpr can express function types.
}
// TODO: sixtyFour should actually be 8 * sizeof(n.LHS().Expr()).
if aMax.Cmp(sixtyFour) > 0 {
@@ -1145,7 +1145,7 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
if typ.IsIdeal() {
typ = rhs.MType()
}
if qid := typ.QID(); qid[0] == 0 {
if qid := typ.QID(); qid[0] == t.IDBase {
b := numTypeBounds[qid[1].Key()]
return b[0], b[1], nil
}
@@ -1194,7 +1194,7 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) {
// TODO: is the special cases for io_reader and io_writer superfluous with
// the general purpose code for built-ins below?
if qid := typ.QID(); qid[0] == 0 {
if qid := typ.QID(); qid[0] == t.IDBase {
switch qid[1].Key() {
case t.KeyIOReader, t.KeyIOWriter:
return nil, nil, nil
@@ -1202,7 +1202,7 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) {
}
b := [2]*big.Int{}
if qid := typ.QID(); qid[0] == 0 && qid[1].Key() < t.Key(len(numTypeBounds)) {
if qid := typ.QID(); qid[0] == t.IDBase && qid[1].Key() < t.Key(len(numTypeBounds)) {
b = numTypeBounds[qid[1].Key()]
}
// TODO: should || be && instead (see also func typeBounds)? Is this if
+40 -40
View File
@@ -62,15 +62,15 @@ func TestCheck(tt *testing.T) {
packageid "test"
pri struct foo(
i i32,
i base.i32,
)
pri func foo.bar()() {
var x u8
var y i32 = +2
var z u64[..123]
var a array[4] u8
var b bool
var x base.u8
var y base.i32 = +2
var z base.u64[..123]
var a array[4] base.u8
var b base.bool
x = 0
x = 1 + (x * 0)
@@ -78,10 +78,10 @@ func TestCheck(tt *testing.T) {
y = this.i
b = not true
y = x as i32
y = x as base.i32
var p i32
var q i32[0..8]
var p base.i32
var q base.i32[0..8]
assert true
@@ -147,16 +147,16 @@ func TestCheck(tt *testing.T) {
})
want := [][2]string{
{"a", "array[4] u8"},
{"b", "bool"},
{"a", "array[4] base.u8"},
{"b", "base.bool"},
{"in", "in"},
{"out", "out"},
{"p", "i32"},
{"q", "i32[0..8]"},
{"p", "base.i32"},
{"q", "base.i32[0..8]"},
{"this", "ptr foo"},
{"x", "u8"},
{"y", "i32"},
{"z", "u64[..123]"},
{"x", "base.u8"},
{"y", "base.i32"},
{"z", "base.u64[..123]"},
}
if !reflect.DeepEqual(got, want) {
tt.Fatalf("\ngot %v\nwant %v", got, want)
@@ -166,35 +166,35 @@ func TestCheck(tt *testing.T) {
func TestConstValues(tt *testing.T) {
const filename = "test.wuffs"
testCases := map[string]int64{
"var i i32 = 42": 42,
"var i base.i32 = 42": 42,
"var i i32 = +7": +7,
"var i i32 = -7": -7,
"var i base.i32 = +7": +7,
"var i base.i32 = -7": -7,
"var b bool = false": 0,
"var b bool = not false": 1,
"var b bool = not not false": 0,
"var b base.bool = false": 0,
"var b base.bool = not false": 1,
"var b base.bool = not not false": 0,
"var i i32 = 10 + 3": 13,
"var i i32 = 10 - 3": 7,
"var i i32 = 10 * 3": 30,
"var i i32 = 10 / 3": 3,
"var i i32 = 10 << 3": 80,
"var i i32 = 10 >> 3": 1,
"var i i32 = 10 & 3": 2,
"var i i32 = 10 &^ 3": 8,
"var i i32 = 10 | 3": 11,
"var i i32 = 10 ^ 3": 9,
"var i base.i32 = 10 + 3": 13,
"var i base.i32 = 10 - 3": 7,
"var i base.i32 = 10 * 3": 30,
"var i base.i32 = 10 / 3": 3,
"var i base.i32 = 10 << 3": 80,
"var i base.i32 = 10 >> 3": 1,
"var i base.i32 = 10 & 3": 2,
"var i base.i32 = 10 &^ 3": 8,
"var i base.i32 = 10 | 3": 11,
"var i base.i32 = 10 ^ 3": 9,
"var b bool = 10 != 3": 1,
"var b bool = 10 < 3": 0,
"var b bool = 10 <= 3": 0,
"var b bool = 10 == 3": 0,
"var b bool = 10 >= 3": 1,
"var b bool = 10 > 3": 1,
"var b base.bool = 10 != 3": 1,
"var b base.bool = 10 < 3": 0,
"var b base.bool = 10 <= 3": 0,
"var b base.bool = 10 == 3": 0,
"var b base.bool = 10 >= 3": 1,
"var b base.bool = 10 > 3": 1,
"var b bool = false and true": 0,
"var b bool = false or true": 1,
"var b base.bool = false and true": 0,
"var b base.bool = false or true": 1,
}
tm := &t.Map{}
+2 -2
View File
@@ -132,7 +132,7 @@ check2:
continue
}
// Check that the LHS is "d as u64".
// Check that the LHS is "d as base.u64".
lhs := x.LHS().Expr()
if lhs.Operator().Key() != t.KeyXBinaryAs {
continue
@@ -168,7 +168,7 @@ check3:
continue
}
// Check that the LHS is "l as u64".
// Check that the LHS is "l as base.u64".
lhs := x.LHS().Expr()
if lhs.Operator().Key() != t.KeyXBinaryAs {
continue
+17 -16
View File
@@ -34,26 +34,26 @@ var (
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprGeneric = a.NewTypeExpr(0, 0, t.IDDiamond, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, 0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, 0, t.IDDollar, nil, nil, nil)
typeExprGeneric = a.NewTypeExpr(0, t.IDBase, t.IDDiamond, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
typeExprU16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, 0, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, 0, t.IDBool, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, 0, t.IDStatus, nil, nil, nil)
typeExprIOReader = a.NewTypeExpr(0, 0, t.IDIOReader, nil, nil, nil)
typeExprIOWriter = a.NewTypeExpr(0, 0, t.IDIOWriter, nil, nil, nil)
typeExprImageConfig = a.NewTypeExpr(0, 0, t.IDImageConfig, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, t.IDBase, t.IDU8, nil, nil, nil)
typeExprU16 = a.NewTypeExpr(0, t.IDBase, t.IDU16, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, t.IDBase, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDBase, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, t.IDBase, t.IDBool, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, t.IDBase, t.IDStatus, nil, nil, nil)
typeExprIOReader = a.NewTypeExpr(0, t.IDBase, t.IDIOReader, nil, nil, nil)
typeExprIOWriter = a.NewTypeExpr(0, t.IDBase, t.IDIOWriter, nil, nil, nil)
typeExprImageConfig = a.NewTypeExpr(0, t.IDBase, t.IDImageConfig, nil, nil, nil)
typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8)
// TODO: delete this.
typeExprPlaceholder = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDBase, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDBase, t.IDU32, nil, nil, nil)
)
// typeMap maps from variable names (as token IDs) to types.
@@ -127,6 +127,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func
return nil, fmt.Errorf("check: parsing %q: got %d top level decls, want %d", s, len(tlds), 1)
}
f := tlds[0].Func()
f.Node().Raw().SetPackage(tm, t.IDBase)
m[f.QQID()] = f
}
return m, nil
@@ -140,7 +141,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
lQID := lTyp.QID()
qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()}
if lTyp.IsSliceType() {
qqid[0] = 0
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := c.builtInSliceFunc(qqid); err != nil {
return nil, err
+13 -13
View File
@@ -570,7 +570,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
}
genericType := (*a.TypeExpr)(nil)
if f.Receiver() == (t.QID{0, t.IDDiamond}) {
if f.Receiver() == (t.QID{t.IDBase, t.IDDiamond}) {
genericType = lhs.MType().Receiver()
}
@@ -690,7 +690,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
qqid := t.QQID{lQID[0], lQID[1], n.Ident()}
if lTyp.IsSliceType() {
qqid[0] = 0
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := q.c.builtInSliceFunc(qqid); err != nil {
return err
@@ -725,18 +725,16 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
if s == nil {
s = q.c.structs[lQID]
if s == nil && builtInTypeMap[lQID[1]] == nil {
if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q", lTyp.Str(q.tm), lhs.Str(q.tm))
}
}
if s != nil {
for _, field := range s.Fields() {
f := field.Field()
if f.Name() == n.Ident() {
n.SetMType(f.XType())
return nil
}
for _, field := range s.Fields() {
f := field.Field()
if f.Name() == n.Ident() {
n.SetMType(f.XType())
return nil
}
}
@@ -1028,7 +1026,7 @@ swtch:
// TODO: also check t.KeyOpenParen.
case 0:
qid := typ.QID()
if qid[1].IsNumType() {
if qid[0] == t.IDBase && qid[1].IsNumType() {
for _, b := range typ.Bounds() {
if b == nil {
continue
@@ -1045,8 +1043,10 @@ swtch:
if typ.Min() != nil || typ.Max() != nil {
// TODO: reject. You can only refine numeric types.
}
if _, ok := builtInTypeMap[qid[1]]; ok {
break swtch
if qid[0] == t.IDBase {
if _, ok := builtInTypeMap[qid[1]]; ok {
break swtch
}
}
for _, s := range q.c.structs {
if s.QID() == qid {
+3
View File
@@ -277,6 +277,7 @@ const (
KeyIn = Key(IDIn >> KeyShift)
KeyOut = Key(IDOut >> KeyShift)
KeySLICE = Key(IDSLICE >> KeyShift)
KeyBase = Key(IDBase >> KeyShift)
KeyI8 = Key(IDI8 >> KeyShift)
KeyI16 = Key(IDI16 >> KeyShift)
@@ -469,6 +470,7 @@ const (
IDIn = ID(0x7A<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDOut = ID(0x7B<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSLICE = ID(0x7C<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDBase = ID(0x7D<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDI8 = ID(0x80<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
IDI16 = ID(0x81<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
@@ -681,6 +683,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyIn: {"in", IDIn},
KeyOut: {"out", IDOut},
KeySLICE: {"SLICE", IDSLICE},
KeyBase: {"base", IDBase},
// Change MaxIntBits if a future update adds an i128 or u128 type.
+6 -6
View File
@@ -16,7 +16,7 @@ packageid "crc3"
// TODO: drop the '?' but still generate wuffs_crc32__ieee__initialize?
pub struct ieee?(
state u32,
state base.u32,
)
// TODO: add a ! as this function is impure.
@@ -24,11 +24,11 @@ pub struct ieee?(
// TODO: should it be ? instead of ! because it's public, and might need a way
// to signal "initializer not called"? Should the return type, in the generated
// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"?
pub func ieee.update(x slice u8)(checksum u32) {
var s u32 = 0xFFFFFFFF ^ this.state
pub func ieee.update(x slice base.u8)(checksum base.u32) {
var s base.u32 = 0xFFFFFFFF ^ this.state
iterate[1](p ptr u8:in.x) {
s = ieee_table[((s & 0xFF) as u8) ^ deref p] ^ (s >> 8)
iterate[1](p ptr base.u8:in.x) {
s = ieee_table[((s & 0xFF) as base.u8) ^ deref p] ^ (s >> 8)
}
this.state = 0xFFFFFFFF ^ s
@@ -37,7 +37,7 @@ pub func ieee.update(x slice u8)(checksum u32) {
// The table below was created by script/print-crc32-magic-numbers.go.
pri const ieee_table array[256] u32 = $(
pri const ieee_table array[256] base.u32 = $(
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
+2 -2
View File
@@ -13,12 +13,12 @@
// limitations under the License.
// code_order is defined in the RFC section 3.2.7.
pri const code_order array[19] u8[..18] = $(
pri const code_order array[19] base.u8[..18] = $(
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
)
// reverse8 reverses the bits in a byte.
pri const reverse8 array[256] u8 = $(
pri const reverse8 array[256] base.u8 = $(
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
+86 -86
View File
@@ -41,14 +41,14 @@ pri error "internal error: inconsistent n_bits"
//
// Some trailing elements are 0x08000000. Bit 27 indicates an invalid value.
pri const lcode_magic_numbers array[32] u32 = $(
pri const lcode_magic_numbers array[32] base.u32 = $(
0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, 0x40000800, 0x40000900, 0x40000A00,
0x40000B10, 0x40000D10, 0x40000F10, 0x40001110, 0x40001320, 0x40001720, 0x40001B20, 0x40001F20,
0x40002330, 0x40002B30, 0x40003330, 0x40003B30, 0x40004340, 0x40005340, 0x40006340, 0x40007340,
0x40008350, 0x4000A350, 0x4000C350, 0x4000E350, 0x40010200, 0x08000000, 0x08000000, 0x08000000,
)
pri const dcode_magic_numbers array[32] u32 = $(
pri const dcode_magic_numbers array[32] base.u32 = $(
0x40000000, 0x40000100, 0x40000200, 0x40000300, 0x40000410, 0x40000610, 0x40000820, 0x40000C20,
0x40001030, 0x40001830, 0x40002040, 0x40003040, 0x40004050, 0x40006050, 0x40008060, 0x4000C060,
0x40010070, 0x40018070, 0x40020080, 0x40030080, 0x40040090, 0x40060090, 0x400800A0, 0x400C00A0,
@@ -57,8 +57,8 @@ pri const dcode_magic_numbers array[32] u32 = $(
pub struct decoder?(
// These fields yield src's bits in Least Significant Bits order.
bits u32,
n_bits u32,
bits base.u32,
n_bits base.u32,
// huffs and n_huffs_bits are the lookup tables for Huffman decodings.
//
@@ -93,8 +93,8 @@ pub struct decoder?(
//
// TODO: replace the magic "big enough" 1234 with something more
// principled, perhaps discovered via an exhaustive search.
huffs array[2] array[1234] u32,
n_huffs_bits array[2] u32[..9],
huffs array[2] array[1234] base.u32,
n_huffs_bits array[2] base.u32[..9],
// history holds up to the last 32KiB of decoded output, if the decoding
// was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE)
@@ -107,31 +107,31 @@ pub struct decoder?(
//
// history[history_index & 0x7FFF] is where the next byte of decoded output
// will be written.
history array[0x8000] u8, // 32 KiB.
history_index u32,
history array[0x8000] base.u8, // 32 KiB.
history_index base.u32,
// code_lengths is used to pass out-of-band data to init_huff.
//
// code_lengths[in.n_codes0 + i] holds the number of bits in the i'th code.
code_lengths array[320] u8[..15],
code_lengths array[320] base.u8[..15],
// end_of_block is whether decode_huffman_xxx saw an end-of-block code.
//
// TODO: can decode_huffman_xxx signal this in band instead of out of band?
end_of_block bool,
end_of_block base.bool,
)
pub func decoder.decode?(dst io_writer, src io_reader)() {
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
while true {
in.dst.mark()
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
var z base.status = try this.decode_blocks?(dst:in.dst, src:in.src)
if not z.is_suspension() {
return z
}
// TODO: should "since_mark" be "since_mark!", as the return value lets
// you modify the state of in.dst, so future mutations (via the slice)
// can change the veracity of any in.dst assertions?
var written slice u8 = in.dst.since_mark()
var written slice base.u8 = in.dst.since_mark()
// Append written, the decoded output, to the history ringbuffer.
if written.length() >= 0x8000 {
// If written is longer than the ringbuffer, we can ignore the
@@ -143,7 +143,7 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
} else {
// Otherwise, append written to the history ringbuffer starting at
// the previous history_index (modulo 0x8000).
var n_copied u64 = this.history[this.history_index & 0x7FFF:].copy_from_slice(s:written)
var n_copied base.u64 = this.history[this.history_index & 0x7FFF:].copy_from_slice(s:written)
if n_copied < written.length() {
// a_slice.copy_from(s:b_slice) returns the minimum of the two
// slice lengths. If that value is less than b_slice.length(),
@@ -159,31 +159,31 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
// compiler that the conversion to u32 will not overflow. The
// +0x8000 is to maintain that the history ringbuffer is full
// if and only if history_index >= 0x8000.
this.history_index = ((n_copied & 0x7FFF) as u32) + 0x8000
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
} else {
// We didn't need to wrap around.
var already_full u32[..0x8000]
var already_full base.u32[..0x8000]
if this.history_index >= 0x8000 {
already_full = 0x8000
}
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as u32) + already_full
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
}
}
yield z
}
}
pri func decoder.decode_blocks?(dst io_writer, src io_reader)() {
var final u32
pri func decoder.decode_blocks?(dst base.io_writer, src base.io_reader)() {
var final base.u32
while final == 0 {
while this.n_bits < 3,
post this.n_bits >= 3,
{
this.bits |= (in.src.read_u8?() as u32) << this.n_bits
this.bits |= (in.src.read_u8?() as base.u32) << this.n_bits
this.n_bits += 8
}
final = this.bits & 0x01
var type u32 = (this.bits >> 1) & 0x03
var type base.u32 = (this.bits >> 1) & 0x03
this.bits >>= 3
this.n_bits -= 3
@@ -213,7 +213,7 @@ pri func decoder.decode_blocks?(dst io_writer, src io_reader)() {
// decode_uncompressed decodes an uncompresed block as per the RFC section
// 3.2.4.
pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader)() {
// TODO: make this "if" into a function invariant?
//
// Ditto for decode_huffman_slow and decode_huffman_fast.
@@ -223,13 +223,13 @@ pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
this.n_bits = 0
this.bits = 0
var length u32 = in.src.read_u32le?()
var length base.u32 = in.src.read_u32le?()
if (length.low_bits(n:16) + length.high_bits(n:16)) != 0xFFFF {
return error "inconsistent stored block length"
}
length = length.low_bits(n:16)
while true {
var n_copied u32 = in.dst.copy_from_reader32(r:in.src, length:length)
var n_copied base.u32 = in.dst.copy_from_reader32(r:in.src, length:length)
if length <= n_copied {
length = 0
break
@@ -246,7 +246,7 @@ pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
// init_fixed_huffman initializes this.huffs as per the RFC section 3.2.6.
pri func decoder.init_fixed_huffman?()() {
var i u32
var i base.u32
while i < 144 {
this.code_lengths[i] = 8
i += 1
@@ -272,41 +272,41 @@ pri func decoder.init_fixed_huffman?()() {
}
// init_dynamic_huffman initializes this.huffs as per the RFC section 3.2.7.
pri func decoder.init_dynamic_huffman?(src io_reader)() {
var bits u32 = this.bits
var n_bits u32 = this.n_bits
pri func decoder.init_dynamic_huffman?(src base.io_reader)() {
var bits base.u32 = this.bits
var n_bits base.u32 = this.n_bits
while n_bits < 14,
post n_bits >= 14,
{
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
var n_lit u32[257..288] = bits.low_bits(n:5) + 257
var n_lit base.u32[257..288] = bits.low_bits(n:5) + 257
if n_lit > 286 {
return error "bad literal/length code count"
}
bits >>= 5
var n_dist u32[1..32] = bits.low_bits(n:5) + 1
var n_dist base.u32[1..32] = bits.low_bits(n:5) + 1
if n_dist > 30 {
return error "bad distance code count"
}
bits >>= 5
var n_clen u32[4..19] = bits.low_bits(n:4) + 4
var n_clen base.u32[4..19] = bits.low_bits(n:4) + 4
bits >>= 4
n_bits -= 14
// Read the clcode Huffman table: H-CL.
var i u32
var i base.u32
while i < n_clen {
while n_bits < 3,
inv i < n_clen,
post n_bits >= 3,
{
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
assert i < 19 via "a < b: a < c; c <= b"(c:n_clen)
this.code_lengths[code_order[i]] = (bits & 0x07) as u8
this.code_lengths[code_order[i]] = (bits & 0x07) as base.u8
bits >>= 3
n_bits -= 3
i += 1
@@ -318,25 +318,25 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
this.init_huff?(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF)
// Decode the code lengths for the next two Huffman tables.
var mask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
var mask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
i = 0
while i < (n_lit + n_dist) {
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0:n_lit, c0:n_dist)
// Decode a clcode symbol from H-CL.
var table_entry u32
var table_entry base.u32
while true,
inv i < 320,
{
table_entry = this.huffs[0][bits & mask]
var table_entry_n_bits u32[..15] = table_entry & 15
var table_entry_n_bits base.u32[..15] = table_entry & 15
if n_bits >= table_entry_n_bits {
bits >>= table_entry_n_bits
n_bits -= table_entry_n_bits
break
}
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
// For H-CL, there should be no redirections and all symbols should be
@@ -348,15 +348,15 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
// Write a literal code length.
if table_entry < 16 {
this.code_lengths[i] = table_entry as u8
this.code_lengths[i] = table_entry as base.u8
i += 1
continue
}
// Write a repeated code length.
var n_extra_bits u32[..7]
var rep_symbol u8[..15]
var rep_count u32
var n_extra_bits base.u32[..7]
var rep_symbol base.u8[..15]
var rep_count base.u32
if table_entry == 16 {
n_extra_bits = 2
if i <= 0 {
@@ -384,7 +384,7 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
post n_bits >= n_extra_bits,
{
assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
rep_count += bits.low_bits(n:n_extra_bits)
@@ -422,7 +422,7 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
// TODO: skip generating WUFFS_COROUTINE_SUSPENSION_POINTs for *callers* of
// this function, as it can only return errors, not suspensions.
pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..320], base_symbol u32)() {
pri func decoder.init_huff?(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32)() {
// For the clcode example in this package's README.md:
// - n_codes0 = 0
// - n_codes1 = 19
@@ -456,8 +456,8 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// - counts[4] = 1
// - counts[5] = 2
// - all other counts elements are 0.
var counts array[16] u16[..320]
var i u32 = in.n_codes0
var counts array[16] base.u16[..320]
var i base.u32 = in.n_codes0
while i < in.n_codes1 {
assert i < 320 via "a < b: a < c; c <= b"(c:in.n_codes1)
// TODO: this if should be unnecessary. Have some way to assert that,
@@ -468,12 +468,12 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
counts[this.code_lengths[i]] += 1
i += 1
}
if ((counts[0] as u32) + in.n_codes0) == in.n_codes1 {
if ((counts[0] as base.u32) + in.n_codes0) == in.n_codes1 {
return error "no Huffman codes"
}
// Check that the Huffman code completely covers all possible input bits.
var remaining u32 = 1 // There is 1 possible 0-bit code.
var remaining base.u32 = 1 // There is 1 possible 0-bit code.
i = 1
while i <= 15 {
if remaining > (1 << 30) {
@@ -481,10 +481,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
}
// Each iteration doubles the number of possible remaining codes.
remaining <<= 1
if remaining < (counts[i] as u32) {
if remaining < (counts[i] as base.u32) {
return error "bad Huffman code (over-subscribed)"
}
remaining -= counts[i] as u32
remaining -= counts[i] as base.u32
i += 1
}
if remaining != 0 {
@@ -504,12 +504,12 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// - offsets[6] = 10
// - all other offsets elements are 10.
// - n_symbols = 10
var offsets array[16] u16[..320]
var n_symbols u32[..320]
var offsets array[16] base.u16[..320]
var n_symbols base.u32[..320]
i = 1
while i <= 15 {
offsets[i] = n_symbols as u16
var count u32[..320] = counts[i] as u32
offsets[i] = n_symbols as base.u16
var count base.u32[..320] = counts[i] as base.u32
if n_symbols > (320 - count) {
return error "internal error: inconsistent Huffman decoder state"
}
@@ -541,7 +541,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// - offsets[3] = 7, formerly 0
// - offsets[4] = 8, formerly 7
// - offsets[5] = 10, formerly 8
var symbols array[320] u16[..319]
var symbols array[320] base.u16[..319]
i = in.n_codes0
while i < in.n_codes1,
inv n_symbols <= 288,
@@ -555,7 +555,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
if offsets[this.code_lengths[i]] >= 320 {
return error "internal error: inconsistent Huffman decoder state"
}
symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as u16
symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as base.u16
offsets[this.code_lengths[i]] += 1
}
i += 1
@@ -566,7 +566,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// For the clcode example in this package's README.md:
// - min_cl = 3
// - max_cl = 5
var min_cl u32[1..9] = 1
var min_cl base.u32[1..9] = 1
while true,
inv n_symbols <= 288,
{
@@ -578,7 +578,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
}
min_cl += 1
}
var max_cl u32[1..15] = 15
var max_cl base.u32[1..15] = 15
while true,
inv n_symbols <= 288,
{
@@ -611,33 +611,33 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// - this.huffs[0][0b01111] = 0x80000305 (literal, symbols[8]=0x03, code_length=5)
// - this.huffs[0][0b11111] = 0x80001805 (literal, symbols[9]=0x18, code_length=5)
i = 0
if (n_symbols != (offsets[max_cl] as u32)) or (n_symbols != (offsets[15] as u32)) {
if (n_symbols != (offsets[max_cl] as base.u32)) or (n_symbols != (offsets[15] as base.u32)) {
return error "internal error: inconsistent Huffman decoder state"
}
if (in.n_codes0 + (symbols[0] as u32)) >= 320 {
if (in.n_codes0 + (symbols[0] as base.u32)) >= 320 {
return error "internal error: inconsistent Huffman decoder state"
}
var initial_high_bits u32 = 1 << 9
var initial_high_bits base.u32 = 1 << 9
if max_cl < 9 {
initial_high_bits = (1 as u32) << max_cl
initial_high_bits = (1 as base.u32) << max_cl
}
var prev_cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[0] as u32)] as u32
var prev_redirect_key u32 = 0xFFFFFFFF
var top u32[..1234]
var next_top u32[..1234] = 512
var code u32
var key u32
var value u32
var prev_cl base.u32[..15] = this.code_lengths[in.n_codes0 + (symbols[0] as base.u32)] as base.u32
var prev_redirect_key base.u32 = 0xFFFFFFFF
var top base.u32[..1234]
var next_top base.u32[..1234] = 512
var code base.u32
var key base.u32
var value base.u32
while true,
pre code < (1 << 15),
pre i < 288,
inv n_symbols <= 288,
{
if (in.n_codes0 + (symbols[i] as u32)) >= 320 {
if (in.n_codes0 + (symbols[i] as base.u32)) >= 320 {
return error "internal error: inconsistent Huffman decoder state"
}
var cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[i] as u32)] as u32
var cl base.u32[..15] = this.code_lengths[in.n_codes0 + (symbols[i] as base.u32)] as base.u32
if cl > prev_cl {
code <<= cl - prev_cl
if code >= (1 << 15) {
@@ -651,11 +651,11 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
key = code
if cl > 9 {
// TODO: we shouldn't need a temporary variable.
var tmp u32[..6] = cl - 9
var tmp base.u32[..6] = cl - 9
cl = tmp
assert cl <= 9 via "a <= b: a == c; c <= b"(c:tmp)
var redirect_key u32[..511] = (key >> tmp) & 511
var redirect_key base.u32[..511] = (key >> tmp) & 511
key = key.low_bits(n:tmp)
if prev_redirect_key != redirect_key {
prev_redirect_key = redirect_key
@@ -663,18 +663,18 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// Calculate the number of bits needed for the 2nd level table.
// This computation is similar to "check that the Huffman code
// completely covers all possible input bits" above.
remaining = (1 as u32) << cl
var j u32 = prev_cl
remaining = (1 as base.u32) << cl
var j base.u32 = prev_cl
while j <= 15,
inv cl <= 9,
inv code < (1 << 15),
inv i < 288,
inv n_symbols <= 288,
{
if remaining <= (counts[j] as u32) {
if remaining <= (counts[j] as base.u32) {
break
}
remaining -= counts[j] as u32
remaining -= counts[j] as base.u32
if remaining > (1 << 30) {
return error "internal error: inconsistent Huffman decoder state"
}
@@ -685,15 +685,15 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
return error "internal error: inconsistent Huffman decoder state"
}
tmp = j - 9
initial_high_bits = (1 as u32) << tmp
initial_high_bits = (1 as base.u32) << tmp
top = next_top
if (top + ((1 as u32) << tmp)) > 1234 {
if (top + ((1 as base.u32) << tmp)) > 1234 {
return error "internal error: inconsistent Huffman decoder state"
}
next_top = top + ((1 as u32) << tmp)
next_top = top + ((1 as base.u32) << tmp)
redirect_key = (reverse8[redirect_key >> 1] as u32) | ((redirect_key & 1) << 8)
redirect_key = (reverse8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8)
this.huffs[in.which][redirect_key] = 0x10000009 | (top << 8) | (tmp << 4)
}
}
@@ -702,10 +702,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
}
counts[prev_cl] -= 1
var reversed_key u32[..511] = (reverse8[key >> 1] as u32) | ((key & 1) << 8)
var reversed_key base.u32[..511] = (reverse8[key >> 1] as base.u32) | ((key & 1) << 8)
reversed_key >>= 9 - cl
var symbol u32[..319] = symbols[i] as u32
var symbol base.u32[..319] = symbols[i] as base.u32
if symbol == 256 {
// End-of-block.
value = 0x20000000 | cl
@@ -727,8 +727,8 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// The table uses log2(initial_high_bits) bits, but reversed_key only
// has cl bits. We duplicate the key-value pair across all possible
// values of the high (log2(initial_high_bits) - cl) bits.
var high_bits u32 = initial_high_bits
var delta u32 = (1 as u32) << cl
var high_bits base.u32 = initial_high_bits
var delta base.u32 = (1 as base.u32) << cl
while high_bits >= delta,
inv code < (1 << 15),
inv i < 288,
+36 -36
View File
@@ -14,7 +14,7 @@
// TODO: describe how the xxx_fast version differs from the xxx_slow one, the
// assumptions that xxx_fast makes, and how that makes it fast.
pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
pri func decoder.decode_huffman_fast?(dst base.io_writer, src base.io_reader)() {
// When editing this function, consider making the equivalent change to the
// decode_huffman_slow function. Keep the diff between the two
// decode_huffman_*.wuffs files as small as possible, while retaining both
@@ -29,13 +29,13 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
return error "internal error: inconsistent n_bits"
}
var bits u32 = this.bits
var n_bits u32 = this.n_bits
var bits base.u32 = this.bits
var n_bits base.u32 = this.n_bits
var table_entry u32
var table_entry_n_bits u32[..15]
var lmask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
var dmask u32[..511] = ((1 as u32) << this.n_huffs_bits[1]) - 1
var table_entry base.u32
var table_entry_n_bits base.u32[..15]
var lmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
var dmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
// Check up front, on each iteration, that we have enough buffer space to
// both read (12 bytes) and write (258 bytes) as much as we need to. Doing
@@ -70,9 +70,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
} else {
@@ -92,7 +92,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -107,9 +107,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
} else {
@@ -119,8 +119,8 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
assert in.src.available() >= 8
assert n_bits >= 15
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
var redir_top base.u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
var redir_mask base.u32[..0x7FFF] = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
if (redir_top + (bits & redir_mask)) >= 1234 {
return error "internal error: inconsistent Huffman decoder state"
}
@@ -131,7 +131,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -157,15 +157,15 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
}
// length = base_number + extra_bits.
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
table_entry_n_bits = (table_entry >> 4) & 0x0F
if table_entry_n_bits > 0 {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
} else {
@@ -189,9 +189,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
} else {
@@ -212,9 +212,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
} else {
@@ -225,7 +225,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
assert n_bits >= 15
redir_top = (table_entry >> 8) & 0xFFFF
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
if (redir_top + (bits & redir_mask)) >= 1234 {
return error "internal error: inconsistent Huffman decoder state"
}
@@ -251,15 +251,15 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
table_entry_n_bits = (table_entry >> 4) & 0x0F
if table_entry_n_bits > 0 {
// Ensure that we have at least 15 bits of input.
if n_bits < 15 {
assert n_bits >= 0 // TODO: this shouldn't be necessary.
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
assert n_bits >= 15
}
@@ -271,7 +271,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
n_bits -= table_entry_n_bits
}
var n_copied u32
var n_copied base.u32
// The "while true { etc; break }" is a redundant version of "etc", but
// its presence minimizes the diff between decode_huffman_fast and
// decode_huffman_slow.
@@ -281,13 +281,13 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
inv in.dst.is_marked(),
{
// Copy from this.history.
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
// Set (hlen, hdist) to be the length-distance pair to copy
// from this.history, and (length, distance) to be the
// remaining length-distance pair to copy from in.dst.
var hlen u32[..0x7FFF]
var hdist u32 =
(((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32
var hlen base.u32[..0x7FFF]
var hdist base.u32 =
(((dist_minus_1 + 1) as base.u64) - in.dst.since_mark().length()) as base.u32
if length > hdist {
assert hdist < length via "a < b: b > a"()
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
@@ -342,20 +342,20 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
continue:loop
}
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
return error "internal error: inconsistent distance"
}
}
// Once again, redundant but explicit assertions.
assert length <= 258
assert ((dist_minus_1 + 1) as u64) <= in.dst.since_mark().length()
assert ((dist_minus_1 + 1) as base.u64) <= in.dst.since_mark().length()
assert in.dst.available() >= 258
assert in.dst.is_marked()
// We can therefore prove:
assert (dist_minus_1 + 1) > 0
assert (length as u64) <= 258
assert (length as u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258)
assert (length as base.u64) <= 258
assert (length as base.u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258)
// Copy from in.dst.
in.dst.copy_from_history32(distance:(dist_minus_1 + 1), length:length)
@@ -383,7 +383,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
in.src.unread_u8?()
}
this.bits = bits & (((1 as u32) << n_bits) - 1)
this.bits = bits & (((1 as base.u32) << n_bits) - 1)
this.n_bits = n_bits
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
+25 -25
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader)() {
// When editing this function, consider making the equivalent change to the
// decode_huffman_fast function. Keep the diff between the two
// decode_huffman_*.wuffs files as small as possible, while retaining both
@@ -22,13 +22,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
return error "internal error: inconsistent n_bits"
}
var bits u32 = this.bits
var n_bits u32 = this.n_bits
var bits base.u32 = this.bits
var n_bits base.u32 = this.n_bits
var table_entry u32
var table_entry_n_bits u32[..15]
var lmask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
var dmask u32[..511] = ((1 as u32) << this.n_huffs_bits[1]) - 1
var table_entry base.u32
var table_entry_n_bits base.u32[..15]
var lmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
var dmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
while:loop true {
// Decode an lcode symbol from H-L.
@@ -41,13 +41,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
break
}
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -57,8 +57,8 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
break:loop
} else if (table_entry >> 28) != 0 {
// Redirect.
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
var redir_top base.u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
var redir_mask base.u32[..0x7FFF] = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
while true {
if (redir_top + (bits & redir_mask)) >= 1234 {
return error "internal error: inconsistent Huffman decoder state"
@@ -71,13 +71,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
break
}
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -100,14 +100,14 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
}
// length = base_number + extra_bits.
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
table_entry_n_bits = (table_entry >> 4) & 0x0F
if table_entry_n_bits > 0 {
while n_bits < table_entry_n_bits,
post n_bits >= table_entry_n_bits,
{
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
@@ -125,13 +125,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
break
}
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
// Check for a redirect.
if (table_entry >> 28) == 1 {
redir_top = (table_entry >> 8) & 0xFFFF
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
while true {
if (redir_top + (bits & redir_mask)) >= 1234 {
return error "internal error: inconsistent Huffman decoder state"
@@ -144,7 +144,7 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
break
}
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
}
@@ -163,14 +163,14 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
table_entry_n_bits = (table_entry >> 4) & 0x0F
if table_entry_n_bits > 0 {
while n_bits < table_entry_n_bits,
post n_bits >= table_entry_n_bits,
{
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
dist_minus_1 = (dist_minus_1 + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
@@ -178,16 +178,16 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
n_bits -= table_entry_n_bits
}
var n_copied u32
var n_copied base.u32
while true {
// Copy from this.history.
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
// Set (hlen, hdist) to be the length-distance pair to copy
// from this.history, and (length, distance) to be the
// remaining length-distance pair to copy from in.dst.
var hlen u32[..0x7FFF]
var hdist u32 =
(((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32
var hlen base.u32[..0x7FFF]
var hdist base.u32 =
(((dist_minus_1 + 1) as base.u64) - in.dst.since_mark().length()) as base.u32
if length > hdist {
assert hdist < length via "a < b: b > a"()
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
+56 -56
View File
@@ -22,45 +22,45 @@ pub error "bad LZW literal width"
pri error "internal error: inconsistent limited read"
pub struct decoder?(
width u32[..65535],
height u32[..65535],
width base.u32[..65535],
height base.u32[..65535],
// Call sequence state transitions:
// - 0 -> 1: via decode_config.
// - 1 -> 1: via decode_frame.
call_sequence u8,
call_sequence base.u8,
background_color_index u8,
background_color_index base.u8,
block_type u8,
peek_block_type bool,
block_type base.u8,
peek_block_type base.bool,
have_gct bool,
have_lct bool,
have_gct base.bool,
have_lct base.bool,
interlace bool,
interlace base.bool,
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// animation loops is 1.
seen_num_loops bool,
num_loops u32 = 1,
seen_num_loops base.bool,
num_loops base.u32 = 1,
frame_top u32[..65535],
frame_left u32[..65535],
frame_width u32[..65535],
frame_height u32[..65535],
frame_top base.u32[..65535],
frame_left base.u32[..65535],
frame_width base.u32[..65535],
frame_height base.u32[..65535],
// gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries.
//
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
gct array[3 * 256] u8,
lct array[3 * 256] u8,
gct array[3 * 256] base.u8,
lct array[3 * 256] base.u8,
lzw lzw_decoder,
)
// TODO: should dst be an nptr instead of a ptr?
pub func decoder.decode_config?(dst ptr image_config, src io_reader)() {
pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() {
if this.call_sequence >= 1 {
return error "invalid call sequence"
}
@@ -85,7 +85,7 @@ pub func decoder.decode_config?(dst ptr image_config, src io_reader)() {
this.call_sequence = 1
}
pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() {
if this.call_sequence < 1 {
return error "invalid call sequence"
}
@@ -121,9 +121,9 @@ pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
// decode_header reads either "GIF87a" or "GIF89a".
//
// See the spec section 17 "Header" on page 7.
pri func decoder.decode_header?(src io_reader)() {
var c array[6] u8
var i u32
pri func decoder.decode_header?(src base.io_reader)() {
var c array[6] base.u8
var i base.u32
while i < 6 {
c[i] = in.src.read_u8?()
i += 1
@@ -137,22 +137,22 @@ pri func decoder.decode_header?(src io_reader)() {
// decode_lsd reads the Logical Screen Descriptor.
//
// See the spec section 18 "Logical Screen Descriptor" on page 8.
pri func decoder.decode_lsd?(src io_reader)() {
var c array[7] u8
var i u32
pri func decoder.decode_lsd?(src base.io_reader)() {
var c array[7] base.u8
var i base.u32
while i < 7 {
c[i] = in.src.read_u8?()
i += 1
}
// TODO: use read_u16le.
this.width = (c[0] as u32) | ((c[1] as u32) << 8)
this.height = (c[2] as u32) | ((c[3] as u32) << 8)
this.width = (c[0] as base.u32) | ((c[1] as base.u32) << 8)
this.height = (c[2] as base.u32) | ((c[3] as base.u32) << 8)
this.background_color_index = c[5]
// Read the optional Global Color Table.
this.have_gct = (c[4] & 0x80) != 0
if this.have_gct {
var gct_size u32[..256] = (1 as u32) << (1 + (c[4] & 0x07))
var gct_size base.u32[..256] = (1 as base.u32) << (1 + (c[4] & 0x07))
i = 0
while i < gct_size {
assert i < 256 via "a < b: a < c; c <= b"(c:gct_size)
@@ -172,8 +172,8 @@ pri func decoder.decode_lsd?(src io_reader)() {
// - section 24 "Comment Extension" on page 17.
// - section 25 "Plain Text Extension" on page 18.
// - section 26 "Application Extension" on page 21.
pri func decoder.decode_extension?(src io_reader)() {
var label u8 = in.src.read_u8?()
pri func decoder.decode_extension?(src base.io_reader)() {
var label base.u8 = in.src.read_u8?()
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
// TODO.
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
@@ -185,33 +185,33 @@ pri func decoder.decode_extension?(src io_reader)() {
this.skip_blocks?(src:in.src)
}
pri func decoder.skip_blocks?(src io_reader)() {
pri func decoder.skip_blocks?(src base.io_reader)() {
while true {
var block_size u8 = in.src.read_u8?()
var block_size base.u8 = in.src.read_u8?()
if block_size == 0 {
return
}
in.src.skip32?(n:block_size as u32)
in.src.skip32?(n:block_size as base.u32)
}
}
// animexts1dot0 is "ANIMEXTS1.0" as bytes.
pri const animexts1dot0 array[11] u8 = $(
pri const animexts1dot0 array[11] base.u8 = $(
0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30,
)
// netscape2dot0 is "NETSCAPE2.0" as bytes.
pri const netscape2dot0 array[11] u8 = $(
pri const netscape2dot0 array[11] base.u8 = $(
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
)
// decode_ae reads an Application Extension.
pri func decoder.decode_ae?(src io_reader)() {
pri func decoder.decode_ae?(src base.io_reader)() {
// This "while true" always executes exactly once, as it ends with a
// "break", but using "break"s throughout simplifies the control flow.
while true {
var c u8
var block_size u8 = in.src.read_u8?()
var c base.u8
var block_size base.u8 = in.src.read_u8?()
if block_size == 0 {
return
}
@@ -221,11 +221,11 @@ pri func decoder.decode_ae?(src io_reader)() {
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
if block_size != 11 {
in.src.skip32?(n:block_size as u32)
in.src.skip32?(n:block_size as base.u32)
break
}
var not_animexts bool
var not_netscape bool
var not_animexts base.bool
var not_netscape base.bool
block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 {
c = in.src.read_u8?()
@@ -241,7 +241,7 @@ pri func decoder.decode_ae?(src io_reader)() {
// count.
block_size = in.src.read_u8?()
if block_size != 3 {
in.src.skip32?(n:block_size as u32)
in.src.skip32?(n:block_size as base.u32)
break
}
c = in.src.read_u8?()
@@ -249,7 +249,7 @@ pri func decoder.decode_ae?(src io_reader)() {
in.src.skip32?(n:2)
break
}
this.num_loops = in.src.read_u16le?() as u32
this.num_loops = in.src.read_u16le?() as base.u32
this.seen_num_loops = true
// A loop count of N, in the wire format, actually means "repeat N
@@ -275,22 +275,22 @@ pri func decoder.decode_ae?(src io_reader)() {
// been read.
//
// See the spec section 20 "Image Descriptor" on page 11.
pri func decoder.decode_id?(dst io_writer, src io_reader)() {
pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() {
// TODO: check that the frame rect is inside the image rect??
this.frame_left = in.src.read_u16le?() as u32
this.frame_top = in.src.read_u16le?() as u32
this.frame_width = in.src.read_u16le?() as u32
this.frame_height = in.src.read_u16le?() as u32
this.frame_left = in.src.read_u16le?() as base.u32
this.frame_top = in.src.read_u16le?() as base.u32
this.frame_width = in.src.read_u16le?() as base.u32
this.frame_height = in.src.read_u16le?() as base.u32
// TODO: use this.interlace.
var flags u8 = in.src.read_u8?()
var flags base.u8 = in.src.read_u8?()
this.interlace = (flags & 0x40) != 0
// Read the optional Local Color Table.
this.have_lct = (flags & 0x80) != 0
if this.have_lct {
var lct_size u32[..256] = (1 as u32) << (1 + (flags & 0x07))
var i u32
var lct_size base.u32[..256] = (1 as base.u32) << (1 + (flags & 0x07))
var i base.u32
while i < lct_size {
assert i < 256 via "a < b: a < c; c <= b"(c:lct_size)
this.lct[(3 * i) + 0] = in.src.read_u8?()
@@ -300,19 +300,19 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
}
}
var lw u8 = in.src.read_u8?()
var lw base.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)
this.lzw.set_literal_width!(lw:lw as base.u32)
while true {
var block_size u64 = in.src.read_u8?() as u64
var block_size base.u64 = in.src.read_u8?() as base.u64
if block_size == 0 {
break
}
while true {
var r io_reader = in.src
var r base.io_reader = in.src
// TODO: should "mark" be "set_mark"? Unlike "limit", "mark" does
// not return a different io_reader.
r.mark()
@@ -321,7 +321,7 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
// variables.
//
// TODO: enforce that limit can only be called in a "foo?" call?
var z status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
var z base.status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
if z.is_ok() {
break
}
+29 -29
View File
@@ -18,33 +18,33 @@ pub error "LZW code is out of range"
pub error "LZW prefix chain is cyclical"
pri struct lzw_decoder?(
literal_width u32[2..8] = 8,
stack array[4096] u8,
suffixes array[4096] u8,
prefixes array[4096] u16[..4095],
literal_width base.u32[2..8] = 8,
stack array[4096] base.u8,
suffixes array[4096] base.u8,
prefixes array[4096] base.u16[..4095],
)
pri func lzw_decoder.set_literal_width!(lw u32[2..8] = 8)() {
pri func lzw_decoder.set_literal_width!(lw base.u32[2..8] = 8)() {
this.literal_width = in.lw
}
pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
// These variables don't change over the lifetime of this func.
var clear_code u32[4..256] = (1 as u32) << this.literal_width
var end_code u32[5..257] = clear_code + 1
var clear_code base.u32[4..256] = (1 as base.u32) << this.literal_width
var end_code base.u32[5..257] = clear_code + 1
// These variables do change.
//
// save_code is the code for which, after decoding a code, we save what the
// next back-reference expands to. The README.md file also calls this value
// `max`. 4096 means do not save.
var save_code u32[..4096] = end_code
var prev_code u32[..4095]
var width u32[..12] = this.literal_width + 1
var save_code base.u32[..4096] = end_code
var prev_code base.u32[..4095]
var width base.u32[..12] = this.literal_width + 1
// These variables yield src's bits in Least Significant Bits order.
var bits u32
var n_bits u32
var bits base.u32
var n_bits base.u32
while true,
pre n_bits < 8,
@@ -55,19 +55,19 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
bits |= (in.src.read_u8?() as u32) << n_bits
bits |= (in.src.read_u8?() as base.u32) << n_bits
n_bits += 8
}
var code u32[..4095] = bits.low_bits(n:width)
var code base.u32[..4095] = bits.low_bits(n:width)
bits >>= width
n_bits -= width
if code < clear_code {
assert code < 256 via "a < b: a < c; c <= b"(c:clear_code)
in.dst.write_u8?(x:code as u8)
in.dst.write_u8?(x:code as base.u8)
if save_code <= 4095 {
this.suffixes[save_code] = code as u8
this.prefixes[save_code] = prev_code as u16
this.suffixes[save_code] = code as base.u8
this.prefixes[save_code] = prev_code as base.u16
}
} else if code == clear_code {
@@ -80,8 +80,8 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
return
} else if code <= save_code {
var s u32[..4095] = 4095
var c u32[..4095] = code
var s base.u32[..4095] = 4095
var c base.u32[..4095] = code
if code == save_code {
s -= 1
@@ -97,31 +97,31 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
return error "LZW prefix chain is cyclical"
}
s -= 1
c = this.prefixes[c] as u32
c = this.prefixes[c] as base.u32
}
this.stack[s] = c as u8
this.stack[s] = c as base.u8
if code == save_code {
this.stack[4095] = c as u8
this.stack[4095] = c as base.u8
}
while true,
inv n_bits < 8,
inv c < 256,
{
var expansion slice u8 = this.stack[s:]
var n_copied u64 = in.dst.copy_from_slice(s:expansion)
var expansion slice base.u8 = this.stack[s:]
var n_copied base.u64 = in.dst.copy_from_slice(s:expansion)
if n_copied == expansion.length() {
break
}
s = (s + ((n_copied & 4095) as u32)) & 4095
s = (s + ((n_copied & 4095) as base.u32)) & 4095
// TODO: "closed for writes" instead?
yield suspension "short write"
}
if save_code <= 4095 {
this.suffixes[save_code] = c as u8
this.prefixes[save_code] = prev_code as u16
this.suffixes[save_code] = c as base.u8
this.prefixes[save_code] = prev_code as base.u16
}
} else {
@@ -130,7 +130,7 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
if save_code <= 4095 {
save_code += 1
if (save_code == ((1 as u32) << width)) and (width < 12) {
if (save_code == ((1 as base.u32) << width)) and (width < 12) {
width += 1
}
}
+13 -13
View File
@@ -25,14 +25,14 @@ pub error "invalid gzip encoding flags"
pub struct decoder?(
flate deflate.decoder,
checksum crc32.ieee,
ignore_checksum bool,
ignore_checksum base.bool,
)
pub func decoder.set_ignore_checksum!(ic bool)() {
pub func decoder.set_ignore_checksum!(ic base.bool)() {
this.ignore_checksum = in.ic
}
pub func decoder.decode?(dst io_writer, src io_reader)() {
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
// Read the header.
if in.src.read_u8?() != 0x1F {
return error "bad gzip header"
@@ -43,15 +43,15 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
if in.src.read_u8?() != 0x08 {
return error "invalid gzip compression method"
}
var flags u8 = in.src.read_u8?()
var flags base.u8 = in.src.read_u8?()
// TODO: API for returning the header's MTIME field.
in.src.skip32?(n:6)
var c u8
var c base.u8
// Handle FEXTRA.
if (flags & 0x04) != 0 {
var xlen u16 = in.src.read_u16le?()
in.src.skip32?(n:xlen as u32)
var xlen base.u16 = in.src.read_u16le?()
in.src.skip32?(n:xlen as base.u32)
}
// Handle FNAME.
@@ -89,23 +89,23 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
}
// Decode and checksum the DEFLATE-encoded payload.
var checksum_got u32
var decoded_length_got u32
var checksum_got base.u32
var decoded_length_got base.u32
while true {
in.dst.mark()
var z status = try this.flate.decode?(dst:in.dst, src:in.src)
var z base.status = try this.flate.decode?(dst:in.dst, src:in.src)
if not this.ignore_checksum {
checksum_got = this.checksum.update(x:in.dst.since_mark())
decoded_length_got = decoded_length_got ~+
((in.dst.since_mark().length() & 0xFFFFFFFF) as u32)
((in.dst.since_mark().length() & 0xFFFFFFFF) as base.u32)
}
if z.is_ok() {
break
}
yield z
}
var checksum_want u32 = in.src.read_u32le?()
var decoded_length_want u32 = in.src.read_u32le?()
var checksum_want base.u32 = in.src.read_u32le?()
var decoded_length_want base.u32 = in.src.read_u32le?()
if (not this.ignore_checksum) and
((checksum_got != checksum_want) or (decoded_length_got != decoded_length_want)) {
return error "checksum mismatch"
+14 -14
View File
@@ -26,15 +26,15 @@ pub error "TODO: unsupported zlib preset dictionary"
pub struct decoder?(
flate deflate.decoder,
checksum adler32,
ignore_checksum bool,
ignore_checksum base.bool,
)
pub func decoder.set_ignore_checksum!(ic bool)() {
pub func decoder.set_ignore_checksum!(ic base.bool)() {
this.ignore_checksum = in.ic
}
pub func decoder.decode?(dst io_writer, src io_reader)() {
var x u16 = in.src.read_u16be?()
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
var x base.u16 = in.src.read_u16be?()
if ((x >> 8) & 0x0F) != 0x08 {
return error "invalid zlib compression method"
}
@@ -49,10 +49,10 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
}
// Decode and checksum the DEFLATE-encoded payload.
var checksum_got u32
var checksum_got base.u32
while true {
in.dst.mark()
var z status = try this.flate.decode?(dst:in.dst, src:in.src)
var z base.status = try this.flate.decode?(dst:in.dst, src:in.src)
if not this.ignore_checksum {
checksum_got = this.checksum.update(x:in.dst.since_mark())
}
@@ -61,7 +61,7 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
}
yield z
}
var checksum_want u32 = in.src.read_u32be?()
var checksum_want base.u32 = in.src.read_u32be?()
if (not this.ignore_checksum) and (checksum_got != checksum_want) {
return error "checksum mismatch"
}
@@ -69,26 +69,26 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
// TODO: drop the '?' but still generate wuffs_zlib__adler32__initialize?
pri struct adler32?(
state u32 = 1,
state base.u32 = 1,
)
// TODO: add a ! as this function is impure.
pri func adler32.update(x slice u8)(checksum u32) {
pri func adler32.update(x slice base.u8)(checksum base.u32) {
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
// RFC 1950.
var s1 u32 = this.state.low_bits(n:16)
var s2 u32 = this.state.high_bits(n:16)
var s1 base.u32 = this.state.low_bits(n:16)
var s2 base.u32 = this.state.high_bits(n:16)
while in.x.length() > 0 {
var remaining slice u8
var remaining slice base.u8
if in.x.length() > 5552 {
remaining = in.x[5552:]
in.x = in.x[:5552]
}
iterate[8](p ptr u8:in.x) {
s1 ~+= deref p as u32
iterate[8](p ptr base.u8:in.x) {
s1 ~+= deref p as base.u32
s2 ~+= s1
}