From 519ec373f29b1849a37cae8e4a123039265f6bc2 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 8 Sep 2017 16:53:59 +1000 Subject: [PATCH] First cut at a "[] u8" slice type. --- cmd/puffs-c/internal/cgen/base-header.h | 12 ++++++++++-- cmd/puffs-c/internal/cgen/data.go | 4 ++-- cmd/puffs-c/internal/cgen/expr.go | 14 ++++++++++++++ cmd/puffs-c/internal/cgen/statement.go | 17 +++++++++++++++-- cmd/puffs-c/internal/cgen/var.go | 5 +++++ gen/c/std/flate.c | 20 ++++++++++++++++++-- gen/c/std/gif.c | 12 ++++++++++-- gen/h/std/flate.h | 13 +++++++++++-- gen/h/std/gif.h | 12 ++++++++++-- lang/ast/ast.go | 8 +++++--- lang/ast/string.go | 3 +++ lang/check/bounds.go | 4 ++-- lang/check/type.go | 8 +++----- lang/parse/parse.go | 21 +++++++++++++-------- std/flate/decode_flate.puffs | 2 ++ 15 files changed, 123 insertions(+), 32 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-header.h b/cmd/puffs-c/internal/cgen/base-header.h index 729d89e7..e3fd8cd1 100644 --- a/cmd/puffs-c/internal/cgen/base-header.h +++ b/cmd/puffs-c/internal/cgen/base-header.h @@ -34,8 +34,16 @@ // TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus -// additional indexes into that buffer. +// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length). +// +// A value with all fields NULL or zero is a valid, empty slice. +typedef struct { + uint8_t* ptr; + size_t len; +} puffs_base_slice_u8; + +// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus +// additional indexes into that buffer, plus an opened / closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct { diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 75ded283..2836af21 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -7,8 +7,8 @@ package cgen const baseHeader = "" + "#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include \n#include \n#include \n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n//\n// Similarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Puffs requires a word size of at most 64 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API " + - "/ ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus\n// additional indexes into that buffer.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\n// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len" + - "; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" + + "/ ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base_slice_u8;\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\n// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's curr" + + "ent read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" + "" const baseImpl = "" + diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index e4a03822..d677711c 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -250,6 +250,20 @@ 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. + if n.Decorator().Key() == t.KeyColon { + o := n.Inner() + if o.Decorator() == 0 && o.Name().Key() == t.KeyU8 && !o.IsRefined() { + b.writes("puffs_base_slice_u8") + b.writeb(' ') + b.writes(varNamePrefix) + b.writes(varName) + return nil + } + return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm)) + } + // maxNumPointers is an arbitrary implementation restriction. const maxNumPointers = 16 diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index f6071f14..11af2ba2 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -169,7 +169,8 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { b.writes(";\n") } } - if n.XType().Decorator().Key() == t.KeyOpenBracket { + switch n.XType().Decorator().Key() { + case t.KeyOpenBracket: if n.Value() != nil { // TODO: something like: // cv := n.XType().ArrayLength().ConstValue() @@ -181,7 +182,19 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { // TODO: arrays of arrays. name := n.Name().String(g.tm) b.printf("memset(%s%s, 0, sizeof(%s%s));\n", vPrefix, name, vPrefix, name) - } else { + + case t.KeyColon: + if n.Value() != nil { + return fmt.Errorf("TODO: slice initializers for non-zero default values") + } + // TODO: don't assume that the slice is a slice of u8. + b.printf("%s%s = ((puffs_base_slice_u8){});", vPrefix, n.Name().String(g.tm)) + + // TODO: remove this. + b.printf("\n/* Avoid the \"unused variable\" warning. */\n") + b.printf("if (%s%s.ptr) {}\n", vPrefix, n.Name().String(g.tm)) + + default: b.printf("%s%s = ", vPrefix, n.Name().String(g.tm)) if v := n.Value(); v != nil { if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { diff --git a/cmd/puffs-c/internal/cgen/var.go b/cmd/puffs-c/internal/cgen/var.go index 36c70d48..68313d61 100644 --- a/cmd/puffs-c/internal/cgen/var.go +++ b/cmd/puffs-c/internal/cgen/var.go @@ -242,6 +242,11 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, prefix string, suspend bo b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local) return nil } + case t.KeyColon: + // TODO: should we be able to resume and suspend slices? + // TODO: don't assume that the slice is a slice of u8. + b.printf("%s = ((puffs_base_slice_u8){});\n", lhs) + return nil } return fmt.Errorf("cannot resume or suspend a local variable %q of type %q", n.Name().String(g.tm), n.XType().String(g.tm)) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 48e852dc..ba7d1079 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -37,8 +37,16 @@ // TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus -// additional indexes into that buffer. +// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length). +// +// A value with all fields NULL or zero is a valid, empty slice. +typedef struct { + uint8_t* ptr; + size_t len; +} puffs_base_slice_u8; + +// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus +// additional indexes into that buffer, plus an opened / closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct { @@ -162,6 +170,7 @@ typedef struct { struct { uint32_t coro_susp_point; puffs_flate_status v_z; + puffs_base_slice_u8 v_written; } c_decode[1]; struct { uint32_t coro_susp_point; @@ -524,10 +533,12 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, puffs_flate_status status = PUFFS_FLATE_STATUS_OK; puffs_flate_status v_z; + puffs_base_slice_u8 v_written; uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; if (coro_susp_point) { v_z = self->private_impl.c_decode[0].v_z; + v_written = ((puffs_base_slice_u8){}); } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); @@ -537,6 +548,10 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, puffs_flate_decoder_decode_blocks(self, a_dst, a_src); v_z = t_0; if (v_z > 0) { + v_written = ((puffs_base_slice_u8){}); + /* Avoid the "unused variable" warning. */ + if (v_written.ptr) { + } } status = v_z; goto suspend; @@ -548,6 +563,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, suspend: self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode[0].v_z = v_z; + self->private_impl.c_decode[0].v_written = ((puffs_base_slice_u8){}); exit: self->private_impl.status = status; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 5003605e..15a23bc2 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -37,8 +37,16 @@ // TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus -// additional indexes into that buffer. +// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length). +// +// A value with all fields NULL or zero is a valid, empty slice. +typedef struct { + uint8_t* ptr; + size_t len; +} puffs_base_slice_u8; + +// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus +// additional indexes into that buffer, plus an opened / closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct { diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 9513f886..ba2ca7a2 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -37,8 +37,16 @@ // TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus -// additional indexes into that buffer. +// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length). +// +// A value with all fields NULL or zero is a valid, empty slice. +typedef struct { + uint8_t* ptr; + size_t len; +} puffs_base_slice_u8; + +// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus +// additional indexes into that buffer, plus an opened / closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct { @@ -162,6 +170,7 @@ typedef struct { struct { uint32_t coro_susp_point; puffs_flate_status v_z; + puffs_base_slice_u8 v_written; } c_decode[1]; struct { uint32_t coro_susp_point; diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 9f33037f..51661e13 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -37,8 +37,16 @@ // TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus -// additional indexes into that buffer. +// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length). +// +// A value with all fields NULL or zero is a valid, empty slice. +typedef struct { + uint8_t* ptr; + size_t len; +} puffs_base_slice_u8; + +// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus +// additional indexes into that buffer, plus an opened / closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct { diff --git a/lang/ast/ast.go b/lang/ast/ast.go index f0a279a8..cce7b534 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -456,9 +456,9 @@ 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" -// or "[8] T": -// - ID0: <0|package name|IDPtr|IDOpenBracket> +// TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr +// T", "[8] T" or "[] T": +// - ID0: <0|package name|IDPtr|IDOpenBracket|IDColon> // - ID1: <0|type name> // - LHS: // - MHS: @@ -468,6 +468,8 @@ const MaxTypeExprDepth = 63 // // An IDOpenBracket ID0 means "[LHS] RHS". RHS is the inner type. // +// An IDColon ID0 means "[] RHS". RHS is the inner type. +// // Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or // "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be // refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For diff --git a/lang/ast/string.go b/lang/ast/string.go index cc53f264..1d1f2743 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -188,6 +188,9 @@ func (n *TypeExpr) appendString(buf []byte, tm *t.Map, depth uint32) []byte { buf = n.ArrayLength().appendString(buf, tm, false, 0) buf = append(buf, "] "...) return n.Inner().appendString(buf, tm, depth) + case t.KeyColon: + buf = append(buf, "[] "...) + return n.Inner().appendString(buf, tm, depth) default: buf = append(buf, tm.ByID(n.Decorator())...) buf = append(buf, '.') diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 6db7953d..f8e000f9 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -353,7 +353,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { rMin, rMax := (*big.Int)(nil), (*big.Int)(nil) if op == t.IDEq { if cv := rhs.ConstValue(); cv != nil { - if cv.Cmp(lMin) < 0 || cv.Cmp(lMax) > 0 { + if (lMin != nil && cv.Cmp(lMin) < 0) || (lMax != nil && cv.Cmp(lMax) > 0) { return fmt.Errorf("check: constant %v is not within bounds [%v..%v]", cv, lMin, lMax) } return nil @@ -951,7 +951,7 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { } switch n.Decorator().Key() { - case t.KeyPtr, t.KeyOpenBracket: + case t.KeyPtr, t.KeyOpenBracket, t.KeyColon: return nil, nil, nil } switch n.Name().Key() { diff --git a/lang/check/type.go b/lang/check/type.go index d041ef0e..54d75b92 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -925,11 +925,6 @@ swtch: } return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm)) - case t.KeyPtr: - if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil { - return err - } - case t.KeyOpenBracket: aLen := n.ArrayLength() if err := q.tcheckExpr(aLen, 0); err != nil { @@ -938,6 +933,9 @@ swtch: if aLen.ConstValue() == nil { return fmt.Errorf("check: %q is not constant", aLen.String(q.tm)) } + fallthrough + + case t.KeyPtr, t.KeyColon: if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil { return err } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 198e274e..055fa27f 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -346,20 +346,25 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { if p.peek1().Key() == t.KeyOpenBracket { p.src = p.src[1:] - lhs, err := p.parseExpr() - if err != nil { - return nil, err - } - if x := p.peek1().Key(); x != t.KeyCloseBracket { - got := p.tm.ByKey(x) - return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line()) + decorator, lhs := t.IDColon, (*a.Expr)(nil) + if p.peek1().Key() != t.KeyCloseBracket { + decorator = t.IDOpenBracket + var err error + lhs, err = p.parseExpr() + if err != nil { + return nil, err + } + if x := p.peek1().Key(); x != t.KeyCloseBracket { + got := p.tm.ByKey(x) + return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line()) + } } p.src = p.src[1:] rhs, err := p.parseTypeExpr() if err != nil { return nil, err } - return a.NewTypeExpr(t.IDOpenBracket, 0, lhs, nil, rhs), nil + return a.NewTypeExpr(decorator, 0, lhs, nil, rhs), nil } pkg, name, err := p.parseQualifiedIdent() diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 853e7a4e..1d749c73 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -93,6 +93,8 @@ pub func decoder.decode?(dst writer1, src reader1)() { // TODO: copy from dst to history, if we aren't finished. var z status = try this.decode_blocks?(dst:in.dst, src:in.src) if z.is_suspension() { + // TODO: add some way to specify the bounds of the slice. + var written[] u8 // = in.dst.slice() } return z }