Add io_bind history_position arg

This commit is contained in:
Nigel Tao committed 2021-08-15 08:36:14 +10:00
1 parent b60a2cacd9
commit 28a54baa19
11 files changed
+87 -24

No files matched your search

+6 -4
View File
@@ -117,11 +117,12 @@ wuffs_base__io_reader__set(wuffs_base__io_buffer* b,
const uint8_t** ptr_io0_r,
const uint8_t** ptr_io1_r,
const uint8_t** ptr_io2_r,
wuffs_base__slice_u8 data) {
wuffs_base__slice_u8 data,
uint64_t history_position) {
b->data = data;
b->meta.wi = data.len;
b->meta.ri = 0;
b->meta.pos = 0;
b->meta.pos = history_position;
b->meta.closed = false;
*ptr_iop_r = data.ptr;
@@ -355,11 +356,12 @@ wuffs_base__io_writer__set(wuffs_base__io_buffer* b,
uint8_t** ptr_io0_w,
uint8_t** ptr_io1_w,
uint8_t** ptr_io2_w,
wuffs_base__slice_u8 data) {
wuffs_base__slice_u8 data,
uint64_t history_position) {
b->data = data;
b->meta.wi = 0;
b->meta.ri = 0;
b->meta.pos = 0;
b->meta.pos = history_position;
b->meta.closed = false;
*ptr_iop_w = data.ptr;
+3 -3
View File
@@ -223,7 +223,7 @@ const BaseIOPrivateH = "" +
"" +
"// --------\n\nstatic inline void //\nwuffs_base__io_reader__limit(const uint8_t** ptr_io2_r,\n const uint8_t* iop_r,\n uint64_t limit) {\n if (((uint64_t)(*ptr_io2_r - iop_r)) > limit) {\n *ptr_io2_r = iop_r + limit;\n }\n}\n\nstatic inline uint32_t //\nwuffs_base__io_reader__limited_copy_u32_to_slice(const uint8_t** ptr_iop_r,\n const uint8_t* io2_r,\n uint32_t length,\n wuffs_base__slice_u8 dst) {\n const uint8_t* iop_r = *ptr_iop_r;\n size_t n = dst.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(io2_r - iop_r))) {\n n = (size_t)(io2_r - iop_r);\n }\n if (n > 0) {\n memmove(dst.ptr, iop_r, n);\n *ptr_iop_r += n;\n }\n return (uint32_t)(n);\n}\n\n// wuffs_base__io_reader__match7 returns whether the io_reader's upcoming bytes\n// start with the given prefix (up to 7 bytes long). It is peek-like, not\n" +
"// read-like, in that there are no side-effects.\n//\n// The low 3 bits of a hold the prefix length, n.\n//\n// The high 56 bits of a hold the prefix itself, in little-endian order. The\n// first prefix byte is in bits 8..=15, the second prefix byte is in bits\n// 16..=23, etc. The high (8 * (7 - n)) bits are ignored.\n//\n// There are three possible return values:\n// - 0 means success.\n// - 1 means inconclusive, equivalent to \"$short read\".\n// - 2 means failure.\nstatic inline uint32_t //\nwuffs_base__io_reader__match7(const uint8_t* iop_r,\n const uint8_t* io2_r,\n wuffs_base__io_buffer* r,\n uint64_t a) {\n uint32_t n = a & 7;\n a >>= 8;\n if ((io2_r - iop_r) >= 8) {\n uint64_t x = wuffs_base__peek_u64le__no_bounds_check(iop_r);\n uint32_t shift = 8 * (8 - n);\n return ((a << shift) == (x << shift)) ? 0 : 2;\n }\n for (; n > 0; n--) {\n if (iop_r >= io2_r) {\n return (r && r->meta.closed) ? 2 : 1;\n } else if (*iop_" +
"r != ((uint8_t)(a))) {\n return 2;\n }\n iop_r++;\n a >>= 8;\n }\n return 0;\n}\n\nstatic inline wuffs_base__io_buffer* //\nwuffs_base__io_reader__set(wuffs_base__io_buffer* b,\n const uint8_t** ptr_iop_r,\n const uint8_t** ptr_io0_r,\n const uint8_t** ptr_io1_r,\n const uint8_t** ptr_io2_r,\n wuffs_base__slice_u8 data) {\n b->data = data;\n b->meta.wi = data.len;\n b->meta.ri = 0;\n b->meta.pos = 0;\n b->meta.closed = false;\n\n *ptr_iop_r = data.ptr;\n *ptr_io0_r = data.ptr;\n *ptr_io1_r = data.ptr;\n *ptr_io2_r = data.ptr + data.len;\n\n return b;\n}\n\n" +
"r != ((uint8_t)(a))) {\n return 2;\n }\n iop_r++;\n a >>= 8;\n }\n return 0;\n}\n\nstatic inline wuffs_base__io_buffer* //\nwuffs_base__io_reader__set(wuffs_base__io_buffer* b,\n const uint8_t** ptr_iop_r,\n const uint8_t** ptr_io0_r,\n const uint8_t** ptr_io1_r,\n const uint8_t** ptr_io2_r,\n wuffs_base__slice_u8 data,\n uint64_t history_position) {\n b->data = data;\n b->meta.wi = data.len;\n b->meta.ri = 0;\n b->meta.pos = history_position;\n b->meta.closed = false;\n\n *ptr_iop_r = data.ptr;\n *ptr_io0_r = data.ptr;\n *ptr_io1_r = data.ptr;\n *ptr_io2_r = data.ptr + data.len;\n\n return b;\n}\n\n" +
"" +
"// --------\n\nstatic inline uint64_t //\nwuffs_base__io_writer__copy_from_slice(uint8_t** ptr_iop_w,\n uint8_t* io2_w,\n wuffs_base__slice_u8 src) {\n uint8_t* iop_w = *ptr_iop_w;\n size_t n = src.len;\n if (n > ((size_t)(io2_w - iop_w))) {\n n = (size_t)(io2_w - iop_w);\n }\n if (n > 0) {\n memmove(iop_w, src.ptr, n);\n *ptr_iop_w += n;\n }\n return (uint64_t)(n);\n}\n\nstatic inline void //\nwuffs_base__io_writer__limit(uint8_t** ptr_io2_w,\n uint8_t* iop_w,\n uint64_t limit) {\n if (((uint64_t)(*ptr_io2_w - iop_w)) > limit) {\n *ptr_io2_w = iop_w + limit;\n }\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__limited_copy_u32_from_history(uint8_t** ptr_iop_w,\n uint8_t* io1_w,\n uint8_t* io2_w,\n uint32_t length,\n " +
" uint32_t distance) {\n if (!distance) {\n return 0;\n }\n uint8_t* p = *ptr_iop_w;\n if ((size_t)(p - io1_w) < (size_t)(distance)) {\n return 0;\n }\n uint8_t* q = p - distance;\n size_t n = (size_t)(io2_w - p);\n if ((size_t)(length) > n) {\n length = (uint32_t)(n);\n } else {\n n = (size_t)(length);\n }\n // TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that\n // is mostly because 3 is the minimum length for the deflate format. This\n // function implementation shouldn't overfit to that one format. Perhaps the\n // limited_copy_u32_from_history Wuffs method should also take an unroll hint\n // argument, and the cgen can look if that argument is the constant\n // expression '3'.\n //\n // See also wuffs_base__io_writer__limited_copy_u32_from_history_fast below.\n for (; n >= 3; n -= 3) {\n *p++ = *q++;\n *p++ = *q++;\n *p++ = *q++;\n }\n for (; n; n--) {\n *p++ = *q++;\n }\n *ptr_iop_w = p;\n return length;\n}\n\n// wuffs_base__io_w" +
@@ -231,8 +231,8 @@ const BaseIOPrivateH = "" +
"ance_1_fast\n// copies the previous byte (the one immediately before *ptr_iop_w), copying 8\n// byte chunks at a time. Each chunk contains 8 repetitions of the same byte.\n//\n// In terms of number of bytes copied, length is rounded up to a multiple of 8.\n// As a special case, a zero length rounds up to 8 (even though 0 is already a\n// multiple of 8), since there is always at least one 8 byte chunk copied.\n//\n// In terms of advancing *ptr_iop_w, length is not rounded up.\n//\n// The caller needs to prove that:\n// - (length + 8) <= (io2_w - *ptr_iop_w)\n// - distance == 1\n// - distance <= (*ptr_iop_w - io1_w)\nstatic inline uint32_t //\nwuffs_base__io_writer__limited_copy_u32_from_history_8_byte_chunks_distance_1_fast(\n uint8_t** ptr_iop_w,\n uint8_t* io1_w,\n uint8_t* io2_w,\n uint32_t length,\n uint32_t distance) {\n uint8_t* p = *ptr_iop_w;\n uint64_t x = p[-1];\n x |= x << 8;\n x |= x << 16;\n x |= x << 32;\n uint32_t n = length;\n while (1) {\n wuffs_base__poke_u64le__no_bounds_check(" +
"p, x);\n if (n <= 8) {\n p += n;\n break;\n }\n p += 8;\n n -= 8;\n }\n *ptr_iop_w = p;\n return length;\n}\n\n// wuffs_base__io_writer__limited_copy_u32_from_history_8_byte_chunks_fast is\n// like the wuffs_base__io_writer__limited_copy_u32_from_history_fast function\n// above, but copies 8 byte chunks at a time.\n//\n// In terms of number of bytes copied, length is rounded up to a multiple of 8.\n// As a special case, a zero length rounds up to 8 (even though 0 is already a\n// multiple of 8), since there is always at least one 8 byte chunk copied.\n//\n// In terms of advancing *ptr_iop_w, length is not rounded up.\n//\n// The caller needs to prove that:\n// - (length + 8) <= (io2_w - *ptr_iop_w)\n// - distance >= 8\n// - distance <= (*ptr_iop_w - io1_w)\nstatic inline uint32_t //\nwuffs_base__io_writer__limited_copy_u32_from_history_8_byte_chunks_fast(\n uint8_t** ptr_iop_w,\n uint8_t* io1_w,\n uint8_t* io2_w,\n uint32_t length,\n uint32_t distance) {\n uint8_t* p = *ptr_iop_w;\n u" +
"int8_t* q = p - distance;\n uint32_t n = length;\n while (1) {\n memcpy(p, q, 8);\n if (n <= 8) {\n p += n;\n break;\n }\n p += 8;\n q += 8;\n n -= 8;\n }\n *ptr_iop_w = p;\n return length;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__limited_copy_u32_from_reader(uint8_t** ptr_iop_w,\n uint8_t* io2_w,\n uint32_t length,\n const uint8_t** ptr_iop_r,\n const uint8_t* io2_r) {\n uint8_t* iop_w = *ptr_iop_w;\n size_t n = length;\n if (n > ((size_t)(io2_w - iop_w))) {\n n = (size_t)(io2_w - iop_w);\n }\n const uint8_t* iop_r = *ptr_iop_r;\n if (n > ((size_t)(io2_r - iop_r))) {\n n = (size_t)(io2_r - iop_r);\n }\n if (n > 0) {\n memmove(iop_w, iop_r, n);\n *ptr_iop_w += n;\n *ptr_iop_r += n;\n }\n return (uint32_t)(n);\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__limited_co" +
"py_u32_from_slice(uint8_t** ptr_iop_w,\n uint8_t* io2_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* iop_w = *ptr_iop_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(io2_w - iop_w))) {\n n = (size_t)(io2_w - iop_w);\n }\n if (n > 0) {\n memmove(iop_w, src.ptr, n);\n *ptr_iop_w += n;\n }\n return (uint32_t)(n);\n}\n\nstatic inline wuffs_base__io_buffer* //\nwuffs_base__io_writer__set(wuffs_base__io_buffer* b,\n uint8_t** ptr_iop_w,\n uint8_t** ptr_io0_w,\n uint8_t** ptr_io1_w,\n uint8_t** ptr_io2_w,\n wuffs_base__slice_u8 data) {\n b->data = data;\n b->meta.wi = 0;\n b->meta.ri = 0;\n b->meta.pos = 0;\n b->meta.closed = false;\n\n *ptr_iop_w = data.ptr;\n *ptr_io0_w = data.ptr;\n *ptr_io1_" +
"w = data.ptr;\n *ptr_io2_w = data.ptr + data.len;\n\n return b;\n}\n\n" +
"py_u32_from_slice(uint8_t** ptr_iop_w,\n uint8_t* io2_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* iop_w = *ptr_iop_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(io2_w - iop_w))) {\n n = (size_t)(io2_w - iop_w);\n }\n if (n > 0) {\n memmove(iop_w, src.ptr, n);\n *ptr_iop_w += n;\n }\n return (uint32_t)(n);\n}\n\nstatic inline wuffs_base__io_buffer* //\nwuffs_base__io_writer__set(wuffs_base__io_buffer* b,\n uint8_t** ptr_iop_w,\n uint8_t** ptr_io0_w,\n uint8_t** ptr_io1_w,\n uint8_t** ptr_io2_w,\n wuffs_base__slice_u8 data,\n uint64_t history_position) {\n b->data = data;\n b->meta.wi = 0;\n b->meta.ri = 0;\n b->meta.pos = history_position;\n b->meta.closed = " +
"false;\n\n *ptr_iop_w = data.ptr;\n *ptr_io0_w = data.ptr;\n *ptr_io1_w = data.ptr;\n *ptr_io2_w = data.ptr + data.len;\n\n return b;\n}\n\n" +
"" +
"// ---------------- I/O (Utility)\n\n#define wuffs_base__utility__empty_io_reader wuffs_base__empty_io_reader\n#define wuffs_base__utility__empty_io_writer wuffs_base__empty_io_writer\n" +
""
+5
View File
@@ -331,6 +331,11 @@ func (h *livenessHelper) doIOBind(r livenesses, n *a.IOBind, depth uint32) error
if err := h.doExpr(r, n.Arg1()); err != nil {
return err
}
if histPos := n.HistoryPosition(); histPos != nil {
if err := h.doExpr(r, n.HistoryPosition()); err != nil {
return err
}
}
return h.doBlock(r, n.Body(), depth)
}
+4
View File
@@ -343,6 +343,10 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
if err := g.writeExpr(b, n.Arg1(), false, 0); err != nil {
return err
}
b.writes(",\n")
if err := g.writeExpr(b, n.HistoryPosition(), false, 0); err != nil {
return err
}
b.writes(");\n")
} else {
+11 -8
View File
@@ -583,26 +583,29 @@ func NewField(flags Flags, name t.ID, xType *TypeExpr) *Field {
}
}
// IOBind is "io_bind (io:LHS, data:MHS) { List2 }" or "io_limit (io:LHS,
// limit:MHS) { List2 }":
// IOBind is "io_bind (io:LHS, data:MHS, history_position:RHS) { List2 }" or
// "io_limit (io:LHS, limit:MHS) { List2 }":
// - ID0: <IDIOBind|IDIOLimit>
// - LHS: <Expr>
// - MHS: <Expr>
// - RHS: <Expr>
// - List2: <Statement> body
type IOBind Node
func (n *IOBind) AsNode() *Node { return (*Node)(n) }
func (n *IOBind) Keyword() t.ID { return n.id0 }
func (n *IOBind) IO() *Expr { return n.lhs.AsExpr() }
func (n *IOBind) Arg1() *Expr { return n.mhs.AsExpr() }
func (n *IOBind) Body() []*Node { return n.list2 }
func (n *IOBind) AsNode() *Node { return (*Node)(n) }
func (n *IOBind) Keyword() t.ID { return n.id0 }
func (n *IOBind) IO() *Expr { return n.lhs.AsExpr() }
func (n *IOBind) Arg1() *Expr { return n.mhs.AsExpr() }
func (n *IOBind) HistoryPosition() *Expr { return n.rhs.AsExpr() }
func (n *IOBind) Body() []*Node { return n.list2 }
func NewIOBind(keyword t.ID, io *Expr, arg1 *Expr, body []*Node) *IOBind {
func NewIOBind(keyword t.ID, io *Expr, arg1 *Expr, historyPosition *Expr, body []*Node) *IOBind {
return &IOBind{
kind: KIOBind,
id0: keyword,
lhs: io.AsNode(),
mhs: arg1.AsNode(),
rhs: historyPosition.AsNode(),
list2: body,
}
}
+5
View File
@@ -258,6 +258,11 @@ func (q *checker) bcheckStatement(n *a.Node) error {
if _, err := q.bcheckExpr(n.Arg1(), 0); err != nil {
return err
}
if histPos := n.HistoryPosition(); histPos != nil {
if _, err := q.bcheckExpr(histPos, 0); err != nil {
return err
}
}
if err := q.bcheckBlock(n.Body()); err != nil {
return err
}
+10
View File
@@ -171,6 +171,16 @@ func (q *checker) tcheckStatement(n *a.Node) error {
n.Keyword().Str(q.tm), n.Arg1().Str(q.tm), typ.Str(q.tm), arg1Typ.Str(q.tm))
}
if n.Keyword() == t.IDIOBind {
if err := q.tcheckExpr(n.HistoryPosition(), 0); err != nil {
return err
}
if typ := n.HistoryPosition().MType(); !typ.IsIdeal() && !typ.EqIgnoringRefinements(typeExprU64) {
return fmt.Errorf("check: %s expression %q, of type %q, does not have type %q",
n.Keyword().Str(q.tm), n.HistoryPosition().Str(q.tm), typ.Str(q.tm), typeExprU64.Str(q.tm))
}
}
for _, o := range n.Body() {
// TODO: prohibit jumps (breaks, continues), rets (returns, yields)
// and retry-calling ? methods while inside an io_bind body.
+31 -1
View File
@@ -1123,6 +1123,36 @@ func (p *parser) parseIOBindNode() (*a.Node, error) {
io.Str(p.tm), p.filename, p.line())
}
histPos := (*a.Expr)(nil)
if keyword == t.IDIOBind {
if x := p.peek1(); x != t.IDComma {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ",", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDHistoryPosition {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "history_position", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDColon {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ":", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
histPos, err = p.parseExpr()
if err != nil {
return nil, err
}
if histPos.Effect() != 0 {
return nil, fmt.Errorf(`parse: argument %q is not effect-free at %s:%d`,
io.Str(p.tm), p.filename, p.line())
}
}
if x := p.peek1(); x != t.IDCloseParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
@@ -1134,7 +1164,7 @@ func (p *parser) parseIOBindNode() (*a.Node, error) {
return nil, err
}
return a.NewIOBind(keyword, io, arg1, body).AsNode(), nil
return a.NewIOBind(keyword, io, arg1, histPos, body).AsNode(), nil
}
func (p *parser) parseIf() (*a.If, error) {
+10 -6
View File
@@ -10463,11 +10463,12 @@ wuffs_base__io_reader__set(wuffs_base__io_buffer* b,
const uint8_t** ptr_io0_r,
const uint8_t** ptr_io1_r,
const uint8_t** ptr_io2_r,
wuffs_base__slice_u8 data) {
wuffs_base__slice_u8 data,
uint64_t history_position) {
b->data = data;
b->meta.wi = data.len;
b->meta.ri = 0;
b->meta.pos = 0;
b->meta.pos = history_position;
b->meta.closed = false;
*ptr_iop_r = data.ptr;
@@ -10701,11 +10702,12 @@ wuffs_base__io_writer__set(wuffs_base__io_buffer* b,
uint8_t** ptr_io0_w,
uint8_t** ptr_io1_w,
uint8_t** ptr_io2_w,
wuffs_base__slice_u8 data) {
wuffs_base__slice_u8 data,
uint64_t history_position) {
b->data = data;
b->meta.wi = 0;
b->meta.ri = 0;
b->meta.pos = 0;
b->meta.pos = history_position;
b->meta.closed = false;
*ptr_iop_w = data.ptr;
@@ -30518,7 +30520,8 @@ wuffs_gif__decoder__decode_id_part2(
wuffs_base__slice_u8__subslice_ij(wuffs_base__make_slice_u8(self->private_data.f_compressed,
4096),
self->private_impl.f_compressed_ri,
self->private_impl.f_compressed_wi));
self->private_impl.f_compressed_wi),
0);
v_mark = ((uint64_t)(iop_v_r - io0_v_r));
{
u_r.meta.ri = ((size_t)(iop_v_r - u_r.data.ptr));
@@ -37792,7 +37795,8 @@ wuffs_png__decoder__decode_pass(
&io2_v_w,
wuffs_base__slice_u8__subslice_ij(a_workbuf,
self->private_impl.f_workbuf_wi,
self->private_impl.f_pass_workbuf_length));
self->private_impl.f_pass_workbuf_length),
self->private_impl.f_workbuf_wi);
{
const uint8_t *o_1_io2_a_src = io2_a_src;
wuffs_base__io_reader__limit(&io2_a_src, iop_a_src,
+1 -1
View File
@@ -929,7 +929,7 @@ pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reade
if (this.compressed_ri > this.compressed_wi) or (this.compressed_wi > 4096) {
return "#internal error: inconsistent ri/wi"
}
io_bind (io: r, data: this.compressed[this.compressed_ri .. this.compressed_wi]) {
io_bind (io: r, data: this.compressed[this.compressed_ri .. this.compressed_wi], history_position: 0) {
mark = r.mark()
lzw_status =? this.lzw.transform_io?(
dst: this.util.empty_io_writer(), src: r, workbuf: this.util.empty_slice_u8())
+1 -1
View File
@@ -574,7 +574,7 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
this.pass_workbuf_length > args.workbuf.length()) {
return base."#bad workbuf length"
}
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length]) {
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length], history_position: this.workbuf_wi) {
io_limit (io: args.src, limit: this.chunk_length) {
w_mark = w.mark()
r_mark = args.src.mark()