From ed8a91ef51d8f35c8fd0d324ebbbdc827db46005 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 22 Sep 2017 16:30:30 +1000 Subject: [PATCH] Add an array.copy_from method. --- cmd/puffs-c/internal/cgen/base-impl.h | 7 +++++ cmd/puffs-c/internal/cgen/data.go | 2 +- cmd/puffs-c/internal/cgen/statement.go | 17 +++++++++++ gen/c/std/flate.c | 15 ++++++++++ gen/c/std/gif.c | 7 +++++ gen/h/std/flate.h | 2 ++ lang/check/bounds.go | 30 ++++++++++++++++---- lang/check/type.go | 20 ++----------- lang/token/list.go | 39 ++++++++++++++------------ std/flate/decode_flate.puffs | 20 ++++++++++++- 10 files changed, 116 insertions(+), 43 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 7745ab89..279b880e 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -90,4 +90,11 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( return s; } +// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is +// the minimum of dst.len and src.len. +static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { + memmove(dst.ptr, src.ptr, dst.len < src.len ? dst.len : src.len); +} + #endif // PUFFS_BASE_IMPL_H diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 7c49c75a..a0ffb6a8 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -15,7 +15,7 @@ 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\n// TODO: for puffs_u16be and similar functions, look for (ifdef) the x86\n// architecture and cast the pointer" + "? Only do so if a benchmark justifies the\n// additional code path. Modern compilers might already generate good code.\n\nstatic inline uint16_t puffs_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t puffs_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_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_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#endif // PUFFS_BASE_IMPL_H\n" + + "_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.\nstatic inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n memmove(dst.ptr, src.ptr, dst.len < src.len ? dst.len : src.len);\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" type template_args_short_read struct { diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 577dc27c..1c4d716e 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -58,6 +58,23 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { b.writes(");\n") return nil } + // TODO: delete this hack that only matches "foo.copy_from(etc)". + if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) { + b.writes("puffs_base_slice_u8_copy_from(") + // TODO: don't hard-code this.history (a [32768]u8) as the + // receiver. In any case, the receiver should probably be a slice + // instead of an array. + b.writes("((puffs_base_slice_u8){") + b.writes(".ptr=self->private_impl.f_history,") + b.writes(".len=sizeof(self->private_impl.f_history)})") + b.writeb(',') + a := n.Args()[0].Arg().Value() + if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + b.writes(");\n") + return nil + } return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible") case a.KIf: diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index e6a95502..ab1047dd 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -165,6 +165,8 @@ typedef struct { uint32_t f_n_bits; uint32_t f_huffs[2][1234]; uint32_t f_n_huffs_bits[2]; + uint8_t f_history[32768]; + uint32_t f_history_index; uint8_t f_code_lengths[320]; struct { @@ -374,6 +376,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( return s; } +// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is +// the minimum of dst.len and src.len. +static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { + memmove(dst.ptr, src.ptr, dst.len < src.len ? dst.len : src.len); +} + #endif // PUFFS_BASE_IMPL_H // ---------------- Status Codes Implementations @@ -640,6 +649,12 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, .len = b_wptr_dst - a_dst.buf->ptr}); 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 = sizeof(self->private_impl.f_history)}), + v_written); + self->private_impl.f_history_index = 32768; } } status = v_z; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 55409a3d..2fd86477 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -347,6 +347,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( return s; } +// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is +// the minimum of dst.len and src.len. +static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { + memmove(dst.ptr, src.ptr, dst.len < src.len ? dst.len : src.len); +} + #endif // PUFFS_BASE_IMPL_H // ---------------- Status Codes Implementations diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index ba2ca7a2..b4a00d03 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -165,6 +165,8 @@ typedef struct { uint32_t f_n_bits; uint32_t f_huffs[2][1234]; uint32_t f_n_huffs_bits[2]; + uint8_t f_history[32768]; + uint32_t f_history_index; uint8_t f_code_lengths[320]; struct { diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 44a7e623..7f10dec9 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -658,15 +658,16 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, isThisMethod(q.tm, n, "init_huff", 4) { for _, o := range n.Args() { + a := o.Arg().Value() // TODO: check that the arg range at the caller and the // signature are compatible. - oMin, oMax, err := q.bcheckExpr(o.Arg().Value(), depth) + aMin, aMax, err := q.bcheckExpr(a, depth) if err != nil { return nil, nil, err } - // TODO: check that oMin and oMax is within the function's + // TODO: check that aMin and aMax is within the function's // declared arg bounds. - _, _ = oMin, oMax + _, _ = aMin, aMax } break } @@ -706,12 +707,29 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } // TODO: delete this hack that only matches "foo.decode(etc)". if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) { - for _, aNode := range n.Args() { - a := aNode.Arg().Value() - _, _, err := q.bcheckExpr(a, depth) + for _, o := range n.Args() { + a := o.Arg().Value() + aMin, aMax, err := q.bcheckExpr(a, depth) if err != nil { return nil, nil, err } + // TODO: check that aMin and aMax is within the function's + // declared arg bounds. + _, _ = aMin, aMax + } + return nil, nil, nil + } + // TODO: delete this hack that only matches "foo.copy_from(etc)". + if isThatMethod(q.tm, n, t.KeyCopyFrom, 1) { + for _, o := range n.Args() { + a := o.Arg().Value() + aMin, aMax, err := q.bcheckExpr(a, depth) + if err != nil { + return nil, nil, err + } + // TODO: check that aMin and aMax is within the function's + // declared arg bounds. + _, _ = aMin, aMax } return nil, nil, nil } diff --git a/lang/check/type.go b/lang/check/type.go index 092ca906..135ddfc0 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -428,23 +428,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return nil } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". - if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) { - foo := n.LHS().Expr().LHS().Expr() - if err := q.tcheckExpr(foo, depth); err != nil { - return err - } - n.LHS().SetTypeChecked() - n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK. - for _, o := range n.Args() { - if err := q.tcheckArg(o.Arg(), depth); err != nil { - return err - } - } - n.SetMType(typeExprPlaceholder) // HACK. - return nil - } - // TODO: delete this hack that only matches "foo.decode(etc)". - if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) { + if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) || + isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) || + isThatMethod(q.tm, n, t.KeyCopyFrom, 1) { foo := n.LHS().Expr().LHS().Expr() if err := q.tcheckExpr(foo, depth); err != nil { return err diff --git a/lang/token/list.go b/lang/token/list.go index 309400fb..21654ace 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -261,14 +261,15 @@ const ( KeyWriteU64BE = Key(IDWriteU64BE >> KeyShift) KeyWriteU64LE = Key(IDWriteU64LE >> KeyShift) + KeyIsError = Key(IDIsError >> KeyShift) + KeyIsOK = Key(IDIsOK >> KeyShift) + KeyIsSuspension = Key(IDIsSuspension >> KeyShift) + KeyCopyFrom = Key(IDCopyFrom >> KeyShift) KeyCopyFrom32 = Key(IDCopyFrom32 >> KeyShift) KeyCopyFrom64 = Key(IDCopyFrom64 >> KeyShift) KeySkip32 = Key(IDSkip32 >> KeyShift) KeySkip64 = Key(IDSkip64 >> KeyShift) KeyCopyHistory32 = Key(IDCopyHistory32 >> KeyShift) - KeyIsError = Key(IDIsError >> KeyShift) - KeyIsOK = Key(IDIsOK >> KeyShift) - KeyIsSuspension = Key(IDIsSuspension >> KeyShift) KeySlice = Key(IDSlice >> KeyShift) KeyLength = Key(IDLength >> KeyShift) KeyPrefix = Key(IDPrefix >> KeyShift) @@ -438,18 +439,19 @@ const ( IDWriteU64BE = ID(0xBE<= 0x8000. + // + // history[history_index & 0x7FFF] is where the next byte of decoded output + // will be written. + history[32 * 1024] u8, + history_index u32, + // code_lengths is used to pass out-of-band data to init_huff. // // code_lengths[in.n_codes0 + i] holds the number of bits in the i'th code. @@ -90,7 +104,6 @@ pub struct decoder?( ) 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. @@ -101,6 +114,11 @@ pub func decoder.decode?(dst writer1, src reader1)() { var written[] u8 = in.dst.slice() if written.length() >= (32 * 1024) { written = written.suffix(up_to:32 * 1024) + // TODO: change this.history to this.history[:]. + this.history.copy_from(s:written) + this.history_index = 32 * 1024 + } else { + // TODO: fill part of the history. } } return z