diff --git a/cmd/puffs-c/internal/cgen/base-header.h b/cmd/puffs-c/internal/cgen/base-header.h index e3fd8cd1..e33bc8c9 100644 --- a/cmd/puffs-c/internal/cgen/base-header.h +++ b/cmd/puffs-c/internal/cgen/base-header.h @@ -54,6 +54,12 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base_buf1; +// puffs_base_buf1_mark marks a position (pointer + offset) in a +// puffs_base_buf1. +typedef struct { + uint8_t* ptr; +} puffs_base_buf1_mark; + // puffs_base_limit1 provides a limited view of a 1-dimensional byte stream: // its first N bytes. That N can be greater than a buffer's current read or // write capacity. N decreases naturally over time as bytes are read from or diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index cab7ea7a..439d94dc 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_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" + + "/ 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_buf1_mark marks a position (pointer + offset) in a\n// puffs_base_buf1.\ntypedef struct {\n uint8_t* ptr;\n} puffs_base_buf1_mark;" + + "\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" + "" const baseImpl = "" + diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 29b2c2f1..86bd247c 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -148,6 +148,16 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare b.writes(")") return nil } + if isInDst(g.tm, n, t.KeyMark, 0) { + if pp == parenthesesMandatory { + b.writeb('(') + } + b.writes("(puffs_base_buf1_mark){.ptr= a_dst.buf->ptr + a_dst.buf->wi}") + if pp == parenthesesMandatory { + b.writeb(')') + } + return nil + } if isInDst(g.tm, n, t.KeySlice, 0) { if pp == parenthesesMandatory { b.writeb('(') @@ -374,20 +384,21 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var } var cTypeNames = [...]string{ - t.KeyI8: "int8_t", - t.KeyI16: "int16_t", - t.KeyI32: "int32_t", - t.KeyI64: "int64_t", - t.KeyU8: "uint8_t", - t.KeyU16: "uint16_t", - t.KeyU32: "uint32_t", - t.KeyU64: "uint64_t", - t.KeyUsize: "size_t", - t.KeyBool: "bool", - t.KeyBuf1: "puffs_base_buf1", - t.KeyBuf2: "puffs_base_buf2", - t.KeyReader1: "puffs_base_reader1", - t.KeyWriter1: "puffs_base_writer1", + t.KeyI8: "int8_t", + t.KeyI16: "int16_t", + t.KeyI32: "int32_t", + t.KeyI64: "int64_t", + t.KeyU8: "uint8_t", + t.KeyU16: "uint16_t", + t.KeyU32: "uint32_t", + t.KeyU64: "uint64_t", + t.KeyUsize: "size_t", + t.KeyBool: "bool", + t.KeyBuf1: "puffs_base_buf1", + t.KeyBuf1Mark: "puffs_base_buf1_mark", + t.KeyReader1: "puffs_base_reader1", + t.KeyWriter1: "puffs_base_writer1", + t.KeyBuf2: "puffs_base_buf2", } var cOpNames = [256]string{ diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 69311f99..fca59e8c 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -725,7 +725,7 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { } func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { - callSuspendible := methodName != t.KeySlice + callSuspendible := methodName != t.KeyMark && methodName != t.KeySlice // TODO: check that n.Args() is "(x:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs { return false diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index b6ca47ad..3b70c781 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -57,6 +57,12 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base_buf1; +// puffs_base_buf1_mark marks a position (pointer + offset) in a +// puffs_base_buf1. +typedef struct { + uint8_t* ptr; +} puffs_base_buf1_mark; + // puffs_base_limit1 provides a limited view of a 1-dimensional byte stream: // its first N bytes. That N can be greater than a buffer's current read or // write capacity. N decreases naturally over time as bytes are read from or @@ -171,7 +177,9 @@ typedef struct { struct { uint32_t coro_susp_point; + puffs_base_buf1_mark v_m0; puffs_flate_status v_z; + puffs_base_buf1_mark v_m1; puffs_base_slice_u8 v_written; } c_decode[1]; struct { @@ -595,7 +603,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, } puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + puffs_base_buf1_mark v_m0; puffs_flate_status v_z; + puffs_base_buf1_mark v_m1; puffs_base_slice_u8 v_written; uint8_t* b_wptr_dst = NULL; @@ -614,12 +624,15 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; if (coro_susp_point) { + v_m0 = self->private_impl.c_decode[0].v_m0; v_z = self->private_impl.c_decode[0].v_z; + v_m1 = self->private_impl.c_decode[0].v_m1; v_written = ((puffs_base_slice_u8){}); } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); + v_m0 = ((puffs_base_buf1_mark){.ptr = a_dst.buf->ptr + a_dst.buf->wi}); PUFFS_COROUTINE_SUSPENSION_POINT(1); if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); @@ -647,6 +660,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, } v_z = t_0; if (v_z > 0) { + v_m1 = ((puffs_base_buf1_mark){.ptr = a_dst.buf->ptr + a_dst.buf->wi}); v_written = ((puffs_base_slice_u8){.ptr = a_dst.buf->ptr, .len = b_wptr_dst - a_dst.buf->ptr}); if (((uint64_t)(v_written.len)) >= 32768) { @@ -667,7 +681,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, goto suspend; suspend: self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; + self->private_impl.c_decode[0].v_m0 = v_m0; self->private_impl.c_decode[0].v_z = v_z; + self->private_impl.c_decode[0].v_m1 = v_m1; self->private_impl.c_decode[0].v_written = ((puffs_base_slice_u8){}); exit: diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 77c4c074..bf33cab6 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -57,6 +57,12 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base_buf1; +// puffs_base_buf1_mark marks a position (pointer + offset) in a +// puffs_base_buf1. +typedef struct { + uint8_t* ptr; +} puffs_base_buf1_mark; + // puffs_base_limit1 provides a limited view of a 1-dimensional byte stream: // its first N bytes. That N can be greater than a buffer's current read or // write capacity. N decreases naturally over time as bytes are read from or diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index b4a00d03..bd22297e 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -57,6 +57,12 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base_buf1; +// puffs_base_buf1_mark marks a position (pointer + offset) in a +// puffs_base_buf1. +typedef struct { + uint8_t* ptr; +} puffs_base_buf1_mark; + // puffs_base_limit1 provides a limited view of a 1-dimensional byte stream: // its first N bytes. That N can be greater than a buffer's current read or // write capacity. N decreases naturally over time as bytes are read from or @@ -171,7 +177,9 @@ typedef struct { struct { uint32_t coro_susp_point; + puffs_base_buf1_mark v_m0; puffs_flate_status v_z; + puffs_base_buf1_mark v_m1; puffs_base_slice_u8 v_written; } c_decode[1]; struct { diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 51661e13..e20fda9c 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -57,6 +57,12 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base_buf1; +// puffs_base_buf1_mark marks a position (pointer + offset) in a +// puffs_base_buf1. +typedef struct { + uint8_t* ptr; +} puffs_base_buf1_mark; + // puffs_base_limit1 provides a limited view of a 1-dimensional byte stream: // its first N bytes. That N can be greater than a buffer's current read or // write capacity. N decreases naturally over time as bytes are read from or diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 0c0c89a8..8c9be99c 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -649,7 +649,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, isInSrc(q.tm, n, t.KeySkip32, 1) || isInDst(q.tm, n, t.KeyWrite, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeyCopyFrom32, 2) || isInDst(q.tm, n, t.KeyCopyHistory32, 2) || - isInDst(q.tm, n, t.KeySlice, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 0) || isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) || isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) || isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_huffman", 2) || diff --git a/lang/check/check.go b/lang/check/check.go index d411e3e0..dbade953 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -56,13 +56,14 @@ func trimQuotes(s string) string { // typeExprFoo is an *ast.Node MType (implicit type). var ( - typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) - typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) - typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil) - typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil) - typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) - typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil) - typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8) + typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) + typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) + typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil) + typeExprBuf1Mark = a.NewTypeExpr(0, t.IDBuf1Mark, nil, nil, nil) + typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil) + typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) + typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil) + typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8) // TODO: delete this. typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) diff --git a/lang/check/type.go b/lang/check/type.go index c2300c9a..17d302ef 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -349,7 +349,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { isInSrc(q.tm, n, t.KeySkip32, 1) || isInDst(q.tm, n, t.KeyWrite, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeyCopyFrom32, 2) || isInDst(q.tm, n, t.KeyCopyHistory32, 2) || - isInDst(q.tm, n, t.KeySlice, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 0) || isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) || isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) || isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_huffman", 2) || @@ -371,6 +371,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(typeExprPlaceholder32) // HACK. } else if isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU16LE, 0) { n.SetMType(typeExprPlaceholder16) // HACK. + } else if isInDst(q.tm, n, t.KeyMark, 0) { + n.SetMType(typeExprBuf1Mark) } else if isInDst(q.tm, n, t.KeySlice, 0) { n.SetMType(typeExprSliceU8) // HACK. } else { @@ -585,7 +587,7 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { } func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { - callSuspendible := methodName != t.KeySlice + callSuspendible := methodName != t.KeyMark && methodName != t.KeySlice // TODO: check that n.Args() is "(x:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs { return false @@ -950,7 +952,7 @@ swtch: // TODO: reject. You can only refine numeric types. } if name := n.Name().Key(); name == t.KeyBool || name == t.KeyStatus || - name == t.KeyReader1 || name == t.KeyWriter1 { + name == t.KeyBuf1Mark || name == t.KeyReader1 || name == t.KeyWriter1 { break } for _, s := range q.c.structs { diff --git a/lang/token/list.go b/lang/token/list.go index 21654ace..7311f8eb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -221,21 +221,22 @@ const ( KeyTrue = Key(IDTrue >> KeyShift) KeyZero = Key(IDZero >> KeyShift) - KeyI8 = Key(IDI8 >> KeyShift) - KeyI16 = Key(IDI16 >> KeyShift) - KeyI32 = Key(IDI32 >> KeyShift) - KeyI64 = Key(IDI64 >> KeyShift) - KeyU8 = Key(IDU8 >> KeyShift) - KeyU16 = Key(IDU16 >> KeyShift) - KeyU32 = Key(IDU32 >> KeyShift) - KeyU64 = Key(IDU64 >> KeyShift) - KeyUsize = Key(IDUsize >> KeyShift) - KeyBool = Key(IDBool >> KeyShift) - KeyBuf1 = Key(IDBuf1 >> KeyShift) - KeyBuf2 = Key(IDBuf2 >> KeyShift) - KeyReader1 = Key(IDReader1 >> KeyShift) - KeyWriter1 = Key(IDWriter1 >> KeyShift) - KeyStatus = Key(IDStatus >> KeyShift) + KeyI8 = Key(IDI8 >> KeyShift) + KeyI16 = Key(IDI16 >> KeyShift) + KeyI32 = Key(IDI32 >> KeyShift) + KeyI64 = Key(IDI64 >> KeyShift) + KeyU8 = Key(IDU8 >> KeyShift) + KeyU16 = Key(IDU16 >> KeyShift) + KeyU32 = Key(IDU32 >> KeyShift) + KeyU64 = Key(IDU64 >> KeyShift) + KeyUsize = Key(IDUsize >> KeyShift) + KeyBool = Key(IDBool >> KeyShift) + KeyBuf1 = Key(IDBuf1 >> KeyShift) + KeyBuf1Mark = Key(IDBuf1Mark >> KeyShift) + KeyReader1 = Key(IDReader1 >> KeyShift) + KeyWriter1 = Key(IDWriter1 >> KeyShift) + KeyBuf2 = Key(IDBuf2 >> KeyShift) + KeyStatus = Key(IDStatus >> KeyShift) KeyUnderscore = Key(IDUnderscore >> KeyShift) KeyThis = Key(IDThis >> KeyShift) @@ -270,6 +271,7 @@ const ( KeySkip32 = Key(IDSkip32 >> KeyShift) KeySkip64 = Key(IDSkip64 >> KeyShift) KeyCopyHistory32 = Key(IDCopyHistory32 >> KeyShift) + KeyMark = Key(IDMark >> KeyShift) KeySlice = Key(IDSlice >> KeyShift) KeyLength = Key(IDLength >> KeyShift) KeyPrefix = Key(IDPrefix >> KeyShift) @@ -399,21 +401,22 @@ const ( IDTrue = ID(0x81<