diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 1cc0d86a..23dea840 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -65,6 +65,19 @@ static inline uint32_t puffs_base_load_u32le(uint8_t* p) { ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); } +static inline puffs_base_slice_u8 puffs_base_make_slice_u8(uint8_t* start, + uint8_t* mark0, + uint8_t* mark1, + uint8_t* end) { + if ((start <= mark0) && (mark0 <= mark1) && (mark1 <= end)) { + return ((puffs_base_slice_u8){ + .ptr = mark0, + .len = mark1 - mark0, + }); + } + return ((puffs_base_slice_u8){}); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s, diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 439d94dc..29c50338 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -14,8 +14,9 @@ const baseHeader = "" + const baseImpl = "" + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_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#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#de" + "fine PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline uint16_t puffs_base_load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1" + - "]) << 0);\n}\n\nstatic inline uint16_t puffs_base_load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_base_load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t puffs_base_load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// p" + - "uffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "]) << 0);\n}\n\nstatic inline uint16_t puffs_base_load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_base_load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t puffs_base_load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_make_slice_u8(uint8_t* start,\n uint8_t* mark0,\n uint8_t* mark1,\n uint8_t* end) {\n if ((start <= mark0) && (mark0 <= mark1) && (mark1 <= end)) {\n return ((puffs_base_slice_u8){\n .ptr = mark0,\n .len = mark1 - mark0,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_sli" + + "ce_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n}\n\n#endif // PUFFS_BASE_IMPL" + + "_H\n" + "" type template_args_short_read struct { diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 86bd247c..2c2aa175 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -152,20 +152,21 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare if pp == parenthesesMandatory { b.writeb('(') } - b.writes("(puffs_base_buf1_mark){.ptr= a_dst.buf->ptr + a_dst.buf->wi}") + b.printf("(puffs_base_buf1_mark){.ptr= %swptr_dst}", bPrefix) if pp == parenthesesMandatory { b.writeb(')') } return nil } - if isInDst(g.tm, n, t.KeySlice, 0) { - if pp == parenthesesMandatory { - b.writeb('(') - } - b.writes("(puffs_base_slice_u8){.ptr= a_dst.buf->ptr, .len=b_wptr_dst - a_dst.buf->ptr}") - if pp == parenthesesMandatory { - b.writeb(')') + if isInDst(g.tm, n, t.KeySlice, 2) { + b.printf("puffs_base_make_slice_u8(%sdst.buf->ptr,", aPrefix) + for _, o := range n.Args() { + if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil { + return err + } + b.writes(".ptr,") } + b.printf("%swptr_dst)", bPrefix) return nil } if isThatMethod(g.tm, n, t.KeyLength, 0) { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 3b70c781..25141b2d 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -359,6 +359,19 @@ static inline uint32_t puffs_base_load_u32le(uint8_t* p) { ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); } +static inline puffs_base_slice_u8 puffs_base_make_slice_u8(uint8_t* start, + uint8_t* mark0, + uint8_t* mark1, + uint8_t* end) { + if ((start <= mark0) && (mark0 <= mark1) && (mark1 <= end)) { + return ((puffs_base_slice_u8){ + .ptr = mark0, + .len = mark1 - mark0, + }); + } + return ((puffs_base_slice_u8){}); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s, @@ -632,7 +645,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); - v_m0 = ((puffs_base_buf1_mark){.ptr = a_dst.buf->ptr + a_dst.buf->wi}); + v_m0 = ((puffs_base_buf1_mark){.ptr = b_wptr_dst}); PUFFS_COROUTINE_SUSPENSION_POINT(1); if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); @@ -660,9 +673,9 @@ 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}); + v_m1 = ((puffs_base_buf1_mark){.ptr = b_wptr_dst}); + v_written = puffs_base_make_slice_u8(a_dst.buf->ptr, v_m0.ptr, v_m1.ptr, + b_wptr_dst); if (((uint64_t)(v_written.len)) >= 32768) { v_written = puffs_base_slice_u8_suffix(v_written, 32768); puffs_base_slice_u8_copy_from( diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index bf33cab6..365e0767 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -328,6 +328,19 @@ static inline uint32_t puffs_base_load_u32le(uint8_t* p) { ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); } +static inline puffs_base_slice_u8 puffs_base_make_slice_u8(uint8_t* start, + uint8_t* mark0, + uint8_t* mark1, + uint8_t* end) { + if ((start <= mark0) && (mark0 <= mark1) && (mark1 <= end)) { + return ((puffs_base_slice_u8){ + .ptr = mark0, + .len = mark1 - mark0, + }); + } + return ((puffs_base_slice_u8){}); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s, diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 8c9be99c..c3360e0d 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.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 2) || 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/type.go b/lang/check/type.go index 17d302ef..2a1c8536 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.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeySlice, 2) || 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) || @@ -373,7 +373,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { 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) { + } else if isInDst(q.tm, n, t.KeySlice, 2) { n.SetMType(typeExprSliceU8) // HACK. } else { n.SetMType(typeExprPlaceholder) // HACK. diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 31c62c6e..d9d08eb0 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -108,12 +108,10 @@ pub func decoder.decode?(dst writer1, src reader1)() { var z status = try this.decode_blocks?(dst:in.dst, src:in.src) if z.is_suspension() { var m1 buf1_mark = in.dst.mark() - // TODO: add some way to specify the bounds of the slice. - // // TODO: should "slice" be "slice!", 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[] u8 = in.dst.slice() + var written[] u8 = in.dst.slice(m0:m0, m1:m1) if written.length() >= (32 * 1024) { written = written.suffix(up_to:32 * 1024) this.history[:].copy_from(s:written)