Add an array.copy_from method.

This commit is contained in:
Nigel Tao committed 2017-09-22 16:32:11 +10:00
1 parent 569e800bac
commit ed8a91ef51
10 files changed
+116 -43

No files matched your search

+7
View File
@@ -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
+1 -1
View File
@@ -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 {
+17
View File
@@ -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:
+15
View File
@@ -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;
+7
View File
@@ -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
+2
View File
@@ -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 {
+24 -6
View File
@@ -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
}
+3 -17
View File
@@ -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
+21 -18
View File
@@ -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<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDWriteU64LE = ID(0xBF<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyFrom32 = ID(0xC0<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyFrom64 = ID(0xC1<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip32 = ID(0xC2<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip64 = ID(0xC3<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyHistory32 = ID(0xC4<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsError = ID(0xC5<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsOK = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsSuspension = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDPrefix = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSuffix = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsError = ID(0xC0<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsOK = ID(0xC1<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsSuspension = ID(0xC2<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyFrom = ID(0xC3<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyFrom32 = ID(0xC4<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyFrom64 = ID(0xC5<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip32 = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip64 = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDCopyHistory32 = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSlice = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDLength = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDPrefix = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSuffix = ID(0xCC<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
)
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
@@ -637,14 +639,15 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyWriteU64BE: {"write_u64be", IDWriteU64BE},
KeyWriteU64LE: {"write_u64le", IDWriteU64LE},
KeyIsError: {"is_error", IDIsError},
KeyIsOK: {"is_ok", IDIsOK},
KeyIsSuspension: {"is_suspension", IDIsSuspension},
KeyCopyFrom: {"copy_from", IDCopyFrom},
KeyCopyFrom32: {"copy_from32", IDCopyFrom32},
KeyCopyFrom64: {"copy_from64", IDCopyFrom64},
KeySkip32: {"skip32", IDSkip32},
KeySkip64: {"skip64", IDSkip64},
KeyCopyHistory32: {"copy_history32", IDCopyHistory32},
KeyIsError: {"is_error", IDIsError},
KeyIsOK: {"is_ok", IDIsOK},
KeyIsSuspension: {"is_suspension", IDIsSuspension},
KeySlice: {"slice", IDSlice},
KeyLength: {"length", IDLength},
KeyPrefix: {"prefix", IDPrefix},
+19 -1
View File
@@ -83,6 +83,20 @@ pub struct decoder?(
huffs[2][1234] u32,
n_huffs_bits[2] u32[..9],
// history holds up to the last 32KiB of decoded output, if the decoding
// was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE)
// gives the maximum distance in a length-distance back-reference as 32768,
// or 0x8000.
//
// It is a ringbuffer, so that the most distant byte in the decoding isn't
// necessarily history[0]. The ringbuffer is full (i.e. it holds 32KiB of
// history) if and only if history_index >= 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