Have "return ok" set coro_susp_point = 0.

This commit is contained in:
Nigel Tao committed 2017-10-07 14:43:25 +11:00
1 parent 585ea2024d
commit a12292d094
6 files changed
+60 -15

No files matched your search

+3 -1
View File
@@ -18,8 +18,10 @@
case n:;
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status <= 0) { \
if (status < 0) { \
goto exit; \
} else if (status == 0) { \
goto ok; \
} \
coro_susp_point = n; \
goto suspend; \
+7 -7
View File
@@ -12,13 +12,13 @@ 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#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status <= 0) { \\\n goto exit; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\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#define 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_slice_u8_subslice_i(\n puffs_base_slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.le" +
"n)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(\n puffs_base_slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8\npuffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\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 pu" +
"ffs_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 uint64_t 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 return n;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n " +
" uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n if (length > (wend - wptr)) {\n length = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (length > (rend - rptr)) {\n length = rend - rptr;\n }\n if (length > 0) {\n memmove(wptr, rptr, length);\n *ptr_wptr += length;\n *ptr_rptr += length;\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t l = end - ptr;\n if ((size_t)(length) > l) {\n length = l;\n } else {\n l = length;\n }\n // TODO: is manual unrolling actually helpful?\n f" +
"or (; l >= 8; l -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; l; l--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
"#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#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; " +
" \\\n goto suspend; \\\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#define 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 otherwis" +
"e 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_s" +
"lice_u8 puffs_base_slice_u8_subslice_i(\n puffs_base_slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(\n puffs_base_slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8\npuffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\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 uint64_t 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 return n;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,\n uint8_t* wend,\n " +
" uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n if (length > (wend - wptr)) {\n length = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (length > (rend - rptr)) {\n length = rend - rptr;\n }\n if (length > 0) {\n memmove(wptr, rptr, length);\n *ptr_wptr += length;\n *ptr_rptr += length;\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t l = end - ptr;\n if ((si" +
"ze_t)(length) > l) {\n length = l;\n } else {\n l = length;\n }\n // TODO: is manual unrolling actually helpful?\n for (; l >= 8; l -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; l; l--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
""
type template_args_short_read struct {
+2
View File
@@ -227,6 +227,8 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
// We've reached the end of the function body. Reset the coroutine
// suspension point so that the next call to this function starts at
// the top.
b.writes("\ngoto ok;\n") // Avoid the "unused label" warning.
b.writes("ok:\n")
b.printf("self->private_impl.%s%s[0].coro_susp_point = 0;\n",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
b.writes("goto exit; }\n\n") // Close the coroutine switch.
+1 -3
View File
@@ -151,9 +151,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
if retExpr == nil {
switch ret.keyword.Key() {
case 0:
// TODO: set csp=0, then goto ok.
// Also update the PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND macro.
b.writes("goto suspend;")
b.writes("goto ok;")
return nil
case t.KeyError:
b.writes("goto exit;")
+24 -1
View File
@@ -302,8 +302,10 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
case n:;
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status <= 0) { \
if (status < 0) { \
goto exit; \
} else if (status == 0) { \
goto ok; \
} \
coro_susp_point = n; \
goto suspend; \
@@ -779,6 +781,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
status = v_z;
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
}
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}
@@ -950,6 +955,9 @@ puffs_flate_status puffs_flate_decoder_decode_blocks(puffs_flate_decoder* self,
goto suspend;
}
}
goto ok;
ok:
self->private_impl.c_decode_blocks[0].coro_susp_point = 0;
goto exit;
}
@@ -1091,6 +1099,9 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
}
label_0_break:;
goto ok;
ok:
self->private_impl.c_decode_uncompressed[0].coro_susp_point = 0;
goto exit;
}
@@ -1405,6 +1416,9 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
label_0_break:;
self->private_impl.f_bits = v_bits;
self->private_impl.f_n_bits = v_n_bits;
goto ok;
ok:
self->private_impl.c_decode_huffman[0].coro_susp_point = 0;
goto exit;
}
@@ -1510,6 +1524,9 @@ puffs_flate_status puffs_flate_decoder_init_fixed_huffman(
if (status) {
goto suspend;
}
goto ok;
ok:
self->private_impl.c_init_fixed_huffman[0].coro_susp_point = 0;
goto exit;
}
@@ -1730,6 +1747,9 @@ puffs_flate_status puffs_flate_decoder_init_dynamic_huffman(
}
self->private_impl.f_bits = v_bits;
self->private_impl.f_n_bits = v_n_bits;
goto ok;
ok:
self->private_impl.c_init_dynamic_huffman[0].coro_susp_point = 0;
goto exit;
}
@@ -2196,6 +2216,9 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
}
}
v_checksum = t_3;
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}
+23 -3
View File
@@ -271,8 +271,10 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
case n:;
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status <= 0) { \
if (status < 0) { \
goto exit; \
} else if (status == 0) { \
goto ok; \
} \
coro_susp_point = n; \
goto suspend; \
@@ -724,12 +726,15 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
}
} else if (v_c == 59) {
status = PUFFS_GIF_STATUS_OK;
goto suspend;
goto ok;
} else {
status = PUFFS_GIF_ERROR_BAD_GIF_BLOCK;
goto exit;
}
}
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}
@@ -816,6 +821,9 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
status = PUFFS_GIF_ERROR_BAD_GIF_HEADER;
goto exit;
}
goto ok;
ok:
self->private_impl.c_decode_header[0].coro_susp_point = 0;
goto exit;
}
@@ -928,6 +936,9 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self,
v_i += 1;
}
}
goto ok;
ok:
self->private_impl.c_decode_lsd[0].coro_susp_point = 0;
goto exit;
}
@@ -1045,6 +1056,9 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self,
b_rptr_src += self->private_impl.c_decode_extension[0].scratch;
}
label_0_break:;
goto ok;
ok:
self->private_impl.c_decode_extension[0].coro_susp_point = 0;
goto exit;
}
@@ -1200,6 +1214,9 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
}
}
label_0_break:;
goto ok;
ok:
self->private_impl.c_decode_id[0].coro_susp_point = 0;
goto exit;
}
@@ -1374,7 +1391,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
goto label_0_continue;
} else if (v_code == v_end_code) {
status = PUFFS_GIF_STATUS_OK;
goto suspend;
goto ok;
} else if (v_code <= v_save_code) {
v_s = 4095;
v_c = v_code;
@@ -1425,6 +1442,9 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
}
v_prev_code = v_code;
}
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}