First cut at a "[] u8" slice type.

This commit is contained in:
Nigel Tao committed 2017-09-08 16:53:59 +10:00
1 parent ceedd8c3b9
commit 519ec373f2
15 files changed
+123 -32

No files matched your search

+10 -2
View File
@@ -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 {
+2 -2
View File
@@ -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 <stdbool.h>\n#include <stdint.h>\n#include <string.h>\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 = "" +
+14
View File
@@ -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
+15 -2
View File
@@ -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 {
+5
View File
@@ -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))
+18 -2
View File
@@ -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;
+10 -2
View File
@@ -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 {
+11 -2
View File
@@ -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;
+10 -2
View File
@@ -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 {
+5 -3
View File
@@ -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: <nil|Expr>
// - MHS: <nil|Expr>
@@ -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
+3
View File
@@ -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, '.')
+2 -2
View File
@@ -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() {
+3 -5
View File
@@ -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
}
+13 -8
View File
@@ -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()
+2
View File
@@ -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
}