From a9bdeee7c18706143a9629d3db51eb82f2955432 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 28 Sep 2017 15:57:25 +1000 Subject: [PATCH] Generate C code for a[i:j] with explicit i or j. --- cmd/puffs-c/internal/cgen/base-impl.h | 23 ++++++++++--- cmd/puffs-c/internal/cgen/data.go | 6 ++-- cmd/puffs-c/internal/cgen/expr.go | 49 +++++++++++++++++---------- gen/c/std/flate.c | 39 ++++++++++++++++----- gen/c/std/gif.c | 23 ++++++++++--- lang/check/bounds.go | 49 ++++++++++++++++++++++++++- lang/check/type.go | 7 ++++ std/flate/decode_flate.puffs | 1 + 8 files changed, 159 insertions(+), 38 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 23dea840..84575f41 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -65,10 +65,25 @@ 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) { +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ints( + uint8_t* start, + uint64_t i, + uint64_t j, + uint64_t k) { + if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) { + return ((puffs_base_slice_u8){ + .ptr = start + i, + .len = j - i, + }); + } + return ((puffs_base_slice_u8){}); +} + +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs( + 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, diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index ce2bcce0..8eaa6d16 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -14,9 +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\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" + + "]) << 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_from_ints(\n uint8_t* start,\n uint64_t i,\n uint64_t j,\n uint64_t k) {\n if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) {\n return ((puffs_base_slice_u8){\n .ptr = start + i,\n .len = j - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs(\n 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_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// 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 be36cbd8..58213456 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -159,7 +159,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInDst(g.tm, n, t.KeySlice, 2) { - b.printf("puffs_base_make_slice_u8(%sdst.buf ? %sdst.buf->ptr : NULL,", aPrefix, aPrefix) + b.printf("puffs_base_make_slice_u8_from_ptrs(%sdst.buf ? %sdst.buf->ptr : NULL,", aPrefix, aPrefix) for _, o := range n.Args() { if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil { return err @@ -203,24 +203,39 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare lhs := n.LHS().Expr() mhs := n.MHS().Expr() rhs := n.RHS().Expr() - if mhs == nil && rhs == nil { - if lhs.MType().Decorator().Key() == t.KeyOpenBracket { - // Slice of an array. - // TODO: don't assume that the slice is a slice of u8. - b.writes("((puffs_base_slice_u8){.ptr = ") - if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil { - return err - } - b.printf(", .len=%s})", lhs.MType().ArrayLength().ConstValue()) - } else { - // Slice of a slice. - if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil { - return err - } + if lhs.MType().Decorator().Key() == t.KeyOpenBracket { + // Slice of an array. + // TODO: don't assume that the slice is a slice of u8. + length := lhs.MType().ArrayLength().ConstValue().String() + b.writes("puffs_base_make_slice_u8_from_ints(") + if err := g.writeExpr(b, lhs, rp, parenthesesOptional, depth); err != nil { + return err + } + b.writeb(',') + if mhs == nil { + b.writes("0") + } else if err := g.writeExpr(b, mhs, rp, parenthesesOptional, depth); err != nil { + return err + } + b.writeb(',') + if rhs == nil { + b.writes(length) + } else if err := g.writeExpr(b, rhs, rp, parenthesesOptional, depth); err != nil { + return err + } + b.writeb(',') + b.writes(length) + b.writes(")") + } else { + // Slice of a slice. + if mhs != nil || rhs != nil { + return fmt.Errorf("TODO: writeExprOther for a non-trivial slice of a slice") + } + if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil { + return err } - return nil } - return fmt.Errorf("TODO: writeExprOther for a non-trivial slice") + return nil case t.KeyDot: lhs := n.LHS().Expr() diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index fab67dca..1f2f466e 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -356,10 +356,25 @@ 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) { +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ints( + uint8_t* start, + uint64_t i, + uint64_t j, + uint64_t k) { + if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) { + return ((puffs_base_slice_u8){ + .ptr = start + i, + .len = j - i, + }); + } + return ((puffs_base_slice_u8){}); +} + +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs( + 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, @@ -671,16 +686,22 @@ 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 = b_wptr_dst}); - v_written = - puffs_base_make_slice_u8(a_dst.buf ? a_dst.buf->ptr : NULL, v_m0.ptr, - v_m1.ptr, a_dst.buf ? b_wptr_dst : NULL); + v_written = puffs_base_make_slice_u8_from_ptrs( + a_dst.buf ? a_dst.buf->ptr : NULL, v_m0.ptr, v_m1.ptr, + a_dst.buf ? b_wptr_dst : NULL); if (((uint64_t)(v_written.len)) >= 32768) { v_written = puffs_base_slice_u8_suffix(v_written, 32768); puffs_base_slice_u8_copy_from( - ((puffs_base_slice_u8){.ptr = self->private_impl.f_history, - .len = 32768}), + puffs_base_make_slice_u8_from_ints(self->private_impl.f_history, 0, + 32768, 32768), v_written); self->private_impl.f_history_index = 32768; + } else { + puffs_base_slice_u8_copy_from( + puffs_base_make_slice_u8_from_ints( + self->private_impl.f_history, + self->private_impl.f_history_index & 32767, 32768, 32768), + v_written); } } status = v_z; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 1b8952de..c506729b 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -328,10 +328,25 @@ 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) { +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ints( + uint8_t* start, + uint64_t i, + uint64_t j, + uint64_t k) { + if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) { + return ((puffs_base_slice_u8){ + .ptr = start + i, + .len = j - i, + }); + } + return ((puffs_base_slice_u8){}); +} + +static inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs( + 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, diff --git a/lang/check/bounds.go b/lang/check/bounds.go index c3360e0d..51cf7c0f 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -642,6 +642,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, // No-op. case t.KeyOpenParen, t.KeyTry: + _, _, err := q.bcheckExpr(n.LHS().Expr(), depth) + if err != nil { + return nil, nil, err + } + // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || @@ -766,7 +771,49 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, if mhs == nil && rhs == nil { return nil, nil, nil } - return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice") + lTyp := n.LHS().Expr().MType() + + mMin, mMax := (*big.Int)(nil), (*big.Int)(nil) + if mhs != nil { + var err error + mMin, mMax, err = q.bcheckExpr(mhs, depth) + if err != nil { + return nil, nil, err + } + } + rMin, rMax := (*big.Int)(nil), (*big.Int)(nil) + if rhs != nil { + var err error + rMin, rMax, err = q.bcheckExpr(rhs, depth) + if err != nil { + return nil, nil, err + } + } + + if lTyp.Decorator().Key() == t.KeyOpenBracket { + // Slice of an array. + cv := lTyp.ArrayLength().ConstValue() + if mhs != nil && (mMin.Cmp(zero) < 0 || mMax.Cmp(cv) > 0) { + return nil, nil, fmt.Errorf("check: slice index %q (of range %v..%v) out of range", + mhs.String(q.tm), mMin, mMax) + } + if rhs != nil && (rMin.Cmp(zero) < 0 || rMax.Cmp(cv) > 0) { + return nil, nil, fmt.Errorf("check: slice index %q (of range %v..%v) out of range", + rhs.String(q.tm), rMin, rMax) + } + if mhs != nil && rhs != nil && mMax.Cmp(rMin) > 0 { + return nil, nil, fmt.Errorf("check: slice indexes "+ + "%q (of range %v..%v) and %q (of range %v..%v) overlap", + mhs.String(q.tm), mMin, mMax, + rhs.String(q.tm), rMin, rMax, + ) + } + + } else { + // Slice of a slice. + return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice of a slice") + } + return nil, nil, nil case t.KeyDot: // TODO: delete this hack that only matches "in". diff --git a/lang/check/type.go b/lang/check/type.go index f06cd67b..f24b60f1 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -342,6 +342,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyOpenParen, t.KeyTry: // n is a function call. + + // TODO: be consistent about type-checking n.LHS().Expr() or + // n.LHS().Expr().LHS().Expr(). Doing this properly will probably + // require a TypeExpr being able to express function and method types. + // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || @@ -489,6 +494,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyColon: // n is a slice. + // TODO: require that the i and j in a[i:j] are *unsigned* (or + // non-negative constants)? if mhs := n.MHS().Expr(); mhs != nil { if err := q.tcheckExpr(mhs, depth); err != nil { return err diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index d9d08eb0..f253d574 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -118,6 +118,7 @@ pub func decoder.decode?(dst writer1, src reader1)() { this.history_index = 32 * 1024 } else { // TODO: fill part of the history. + this.history[this.history_index & 0x7FFF:].copy_from(s:written) } } return z