Follow the pkg__type__method convention for base.

This commit is contained in:
Nigel Tao committed 2017-10-21 15:59:08 +11:00
1 parent 362bb94e68
commit f29c4e278f
19 files changed
+747 -734

No files matched your search

+15 -15
View File
@@ -45,15 +45,15 @@
// TODO: don't hard code this in base-header.h.
#define PUFFS_VERSION (0x00001)
// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length).
// puffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).
//
// A value with all fields NULL or zero is a valid, empty slice.
typedef struct {
uint8_t* ptr;
size_t len;
} puffs_base_slice_u8;
} puffs_base__slice_u8;
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus
// puffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus
// additional indexes into that buffer, plus an opened / closed flag.
//
// A value with all fields NULL or zero is a valid, empty buffer.
@@ -63,27 +63,27 @@ typedef struct {
size_t wi; // Write index. Invariant: wi <= len.
size_t ri; // Read index. Invariant: ri <= wi.
bool closed; // No further writes are expected.
} puffs_base_buf1;
} puffs_base__buf1;
// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:
// puffs_base__limit1 provides a limited view of a 1-dimensional byte stream:
// its first N bytes. That N can be greater than a buffer's current read or
// write capacity. N decreases naturally over time as bytes are read from or
// written to the stream.
//
// A value with all fields NULL or zero is a valid, unlimited view.
typedef struct puffs_base_limit1 {
uint64_t* ptr_to_len; // Pointer to N.
struct puffs_base_limit1* next; // Linked list of limits.
} puffs_base_limit1;
typedef struct puffs_base__limit1 {
uint64_t* ptr_to_len; // Pointer to N.
struct puffs_base__limit1* next; // Linked list of limits.
} puffs_base__limit1;
typedef struct {
puffs_base_buf1* buf;
puffs_base_limit1 limit;
} puffs_base_reader1;
puffs_base__buf1* buf;
puffs_base__limit1 limit;
} puffs_base__reader1;
typedef struct {
puffs_base_buf1* buf;
puffs_base_limit1 limit;
} puffs_base_writer1;
puffs_base__buf1* buf;
puffs_base__limit1 limit;
} puffs_base__writer1;
#endif // PUFFS_BASE_HEADER_H
+69 -65
View File
@@ -17,51 +17,51 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
#define PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
// PUFFS_MAGIC is a magic number to check that initializers are called. It's
// not foolproof, given C doesn't automatically zero memory before use, but it
// should catch 99.99% of cases.
// PUFFS_BASE__MAGIC is a magic number to check that initializers are called.
// It's not foolproof, given C doesn't automatically zero memory before use,
// but it should catch 99.99% of cases.
//
// Its (non-zero) value is arbitrary, based on md5sum("puffs").
#define PUFFS_MAGIC (0xCB3699CCU)
#define PUFFS_BASE__MAGIC (0xCB3699CCU)
// PUFFS_ALREADY_ZEROED is passed from a container struct's initializer to a
// containee struct's initializer when the container has already zeroed the
// containee's memory.
// PUFFS_BASE__ALREADY_ZEROED is passed from a container struct's initializer
// to a containee struct's initializer when the container has already zeroed
// the containee's memory.
//
// Its (non-zero) value is arbitrary, based on md5sum("zeroed").
#define PUFFS_ALREADY_ZEROED (0x68602EF1U)
#define PUFFS_BASE__ALREADY_ZEROED (0x68602EF1U)
// Use switch cases for coroutine suspension points, similar to the technique
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
//
// We use a trivial macro instead of an explicit assignment and case statement
// so that clang-format doesn't get confused by the unusual "case"s.
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
coro_susp_point = n; \
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \
coro_susp_point = n; \
case n:;
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status < 0) { \
goto exit; \
} else if (status == 0) { \
goto ok; \
} \
coro_susp_point = n; \
goto suspend; \
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status < 0) { \
goto exit; \
} else if (status == 0) { \
goto ok; \
} \
coro_susp_point = n; \
goto suspend; \
case n:;
// Clang also defines "__GNUC__".
#if defined(__GNUC__)
#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))
#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
#define PUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))
#define PUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
// Declare the printf prototype. The generated code shouldn't need this at all,
// but it's useful for manual printf debugging.
extern int printf(const char* __restrict __format, ...);
#else
#define PUFFS_LIKELY(expr) (expr)
#define PUFFS_UNLIKELY(expr) (expr)
#define PUFFS_BASE__LIKELY(expr) (expr)
#define PUFFS_BASE__UNLIKELY(expr) (expr)
#endif
// ---------------- Static Inline Functions
@@ -82,59 +82,61 @@ extern int printf(const char* __restrict __format, ...);
// inline attribute to guide optimizations such as inlining, to avoid the
// -Wunused-function warning, and we like to compile with -Wall -Werror.
static inline uint16_t puffs_base_load_u16be(uint8_t* p) {
static inline uint16_t puffs_base__load_u16be(uint8_t* p) {
return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);
}
static inline uint16_t puffs_base_load_u16le(uint8_t* p) {
static inline uint16_t puffs_base__load_u16le(uint8_t* p) {
return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);
}
static inline uint32_t puffs_base_load_u32be(uint8_t* p) {
static inline uint32_t puffs_base__load_u32be(uint8_t* p) {
return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |
((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);
}
static inline uint32_t puffs_base_load_u32le(uint8_t* p) {
static inline uint32_t puffs_base__load_u32le(uint8_t* p) {
return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |
((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
}
static inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_i(
puffs_base_slice_u8 s,
static inline puffs_base__slice_u8 puffs_base__slice_u8__subslice_i(
puffs_base__slice_u8 s,
uint64_t i) {
if ((i <= SIZE_MAX) && (i <= s.len)) {
return ((puffs_base_slice_u8){
return ((puffs_base__slice_u8){
.ptr = s.ptr + i,
.len = s.len - i,
});
}
return ((puffs_base_slice_u8){});
return ((puffs_base__slice_u8){});
}
static inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(
puffs_base_slice_u8 s,
static inline puffs_base__slice_u8 puffs_base__slice_u8__subslice_j(
puffs_base__slice_u8 s,
uint64_t j) {
if ((j <= SIZE_MAX) && (j <= s.len)) {
return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});
return ((puffs_base__slice_u8){.ptr = s.ptr, .len = j});
}
return ((puffs_base_slice_u8){});
return ((puffs_base__slice_u8){});
}
static inline puffs_base_slice_u8
puffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {
static inline puffs_base__slice_u8 puffs_base__slice_u8__subslice_ij(
puffs_base__slice_u8 s,
uint64_t i,
uint64_t j) {
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
return ((puffs_base_slice_u8){
return ((puffs_base__slice_u8){
.ptr = s.ptr + i,
.len = j - i,
});
}
return ((puffs_base_slice_u8){});
return ((puffs_base__slice_u8){});
}
// puffs_base_slice_u8_prefix returns up to the first up_to bytes of s.
static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
puffs_base_slice_u8 s,
// puffs_base__slice_u8__prefix returns up to the first up_to bytes of s.
static inline puffs_base__slice_u8 puffs_base__slice_u8__prefix(
puffs_base__slice_u8 s,
uint64_t up_to) {
if ((uint64_t)(s.len) > up_to) {
s.len = up_to;
@@ -142,9 +144,9 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
return s;
}
// puffs_base_slice_u8_suffix returns up to the last up_to bytes of s.
static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
puffs_base_slice_u8 s,
// puffs_base__slice_u8__suffix returns up to the last up_to bytes of s.
static inline puffs_base__slice_u8 puffs_base__slice_u8_suffix(
puffs_base__slice_u8 s,
uint64_t up_to) {
if ((uint64_t)(s.len) > up_to) {
s.ptr += (uint64_t)(s.len) - up_to;
@@ -153,14 +155,14 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
return s;
}
// puffs_base_slice_u8_copy_from_slice calls memmove(dst.ptr, src.ptr, length)
// where length is the minimum of dst.len and src.len.
// puffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,
// length) where length is the minimum of dst.len and src.len.
//
// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty
// Passing a puffs_base__slice_u8 with all fields NULL or zero (a valid, empty
// slice) is valid and results in a no-op.
static inline uint64_t puffs_base_slice_u8_copy_from_slice(
puffs_base_slice_u8 dst,
puffs_base_slice_u8 src) {
static inline uint64_t puffs_base__slice_u8__copy_from_slice(
puffs_base__slice_u8 dst,
puffs_base__slice_u8 src) {
size_t length = dst.len < src.len ? dst.len : src.len;
if (length > 0) {
memmove(dst.ptr, src.ptr, length);
@@ -168,11 +170,12 @@ static inline uint64_t puffs_base_slice_u8_copy_from_slice(
return length;
}
static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
uint8_t* start,
uint8_t* end,
uint32_t distance,
uint32_t length) {
static inline uint32_t puffs_base__writer1__copy_from_history32(
uint8_t** ptr_ptr,
uint8_t* start,
uint8_t* end,
uint32_t distance,
uint32_t length) {
uint8_t* ptr = *ptr_ptr;
size_t d = ptr - start;
if ((d == 0) || (d < (size_t)(distance))) {
@@ -203,11 +206,12 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
return length;
}
static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
uint8_t* wend,
uint8_t** ptr_rptr,
uint8_t* rend,
uint32_t length) {
static inline uint32_t puffs_base__writer1__copy_from_reader32(
uint8_t** ptr_wptr,
uint8_t* wend,
uint8_t** ptr_rptr,
uint8_t* rend,
uint32_t length) {
uint8_t* wptr = *ptr_wptr;
size_t n = length;
if (n > wend - wptr) {
@@ -225,10 +229,10 @@ static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
return n;
}
static inline uint64_t puffs_base_writer1_copy_from_slice(
static inline uint64_t puffs_base__writer1__copy_from_slice(
uint8_t** ptr_wptr,
uint8_t* wend,
puffs_base_slice_u8 src) {
puffs_base__slice_u8 src) {
uint8_t* wptr = *ptr_wptr;
size_t n = src.len;
if (n > wend - wptr) {
@@ -241,10 +245,10 @@ static inline uint64_t puffs_base_writer1_copy_from_slice(
return n;
}
static inline uint32_t puffs_base_writer1_copy_from_slice32(
static inline uint32_t puffs_base__writer1__copy_from_slice32(
uint8_t** ptr_wptr,
uint8_t* wend,
puffs_base_slice_u8 src,
puffs_base__slice_u8 src,
uint32_t length) {
uint8_t* wptr = *ptr_wptr;
size_t n = src.len;
+3 -3
View File
@@ -598,9 +598,9 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
b.printf("return;\n")
b.printf("}\n")
b.writes("if (for_internal_use_only != PUFFS_ALREADY_ZEROED) {" +
b.writes("if (for_internal_use_only != PUFFS_BASE__ALREADY_ZEROED) {" +
"memset(self, 0, sizeof(*self)); }\n")
b.writes("self->private_impl.magic = PUFFS_MAGIC;\n")
b.writes("self->private_impl.magic = PUFFS_BASE__MAGIC;\n")
for _, f := range n.Fields() {
f := f.Field()
@@ -622,7 +622,7 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
continue
}
b.printf("%s%s__initialize(&self->private_impl.%s%s,"+
"PUFFS_VERSION, PUFFS_ALREADY_ZEROED);\n",
"PUFFS_VERSION, PUFFS_BASE__ALREADY_ZEROED);\n",
g.pkgPrefix, x.Name().String(g.tm), fPrefix, f.Name().String(g.tm))
}
+11 -12
View File
@@ -18,21 +18,20 @@ package cgen
const baseHeader = "" +
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Puffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n//\n// Si" +
"milarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Puffs requires a word size of at most 64 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base_slice_u8;\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus" +
" an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\n// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_write" +
"r1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
"milarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Puffs requires a word size of at most 64 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base__slice_u8;\n\n// puffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, p" +
"lus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base__buf1;\n\n// puffs_base__limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base__limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct puffs_base__limit1* next; // Linked list of limits.\n} puffs_base__limit1;\n\ntypedef struct {\n puffs_base__buf1* buf;\n puffs_base__limit1 limit;\n} puffs_base__reader1;\n\ntypedef struct {\n puffs_base__buf1* buf;\n puffs_base__limit1 limit;\n} pu" +
"ffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Copyright 2017 The Puffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// PUFFS_MAGIC is a magic number to check that initializers are called. It's\n// not foolproof, given C doesn't automatically zero memory before use, but it\n// should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"puffs\").\n#define PUFFS_MAGIC (0xCB3699CCU)\n\n// PUFFS_ALREADY_ZERO" +
"ED is passed from a container struct's initializer to a\n// containee struct's initializer when the container has already zeroed the\n// containee's memory.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"zeroed\").\n#define PUFFS_ALREADY_ZEROED (0x68602EF1U)\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\", with" +
"out 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.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 up_to bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8" +
" s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last up_to bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from_slice calls memmove(dst.ptr, src.ptr, length)\n// where length is 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_slice(\n puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_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 n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: is manual unrolling actually helpful?\n for (; n >= 8; n -= 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 (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_reader32(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 size_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n memmove(wptr, rptr, n);\n *ptr_wptr += n;\n *ptr_rptr += n;\n }\n return n;\n}\n\nstatic inline uint64_t puffs_base_writer1_copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base_slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base_slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *pt" +
"r_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Copyright 2017 The Puffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#define PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// PUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"puffs\").\n#define PUFFS_BASE__MAGIC (0xCB3699CCU)\n\n// " +
"PUFFS_BASE__ALREADY_ZEROED is passed from a container struct's initializer\n// to a containee struct's initializer when the container has already zeroed\n// the containee's memory.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"zeroed\").\n#define PUFFS_BASE__ALREADY_ZEROED (0x68602EF1U)\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_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:;\n\n#define PUFFS_BASE__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_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_BASE__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_BASE__LIKELY(expr) (expr)\n#define PUFFS_BASE__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 desig" +
"n 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(ui" +
"nt8_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.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 puffs_base__slice_u8__subslice_ij(\n puffs_base__slice_u8 s,\n uint64_t i,\n 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 up_to bytes of s.\nstatic inline puffs_base__slice_u8 puffs_base__slice_u8__prefix(\n puffs_base__slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline puffs_base__slice_u8 puffs_base__slice_u8_suffix(\n puffs_base__slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is 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_slice(\n puffs_base__slice_u8 dst,\n puffs_base__slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memm" +
"ove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_history32(\n 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 n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: is manual unrolling actually helpful?\n for (; n >= 8; n -= 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 (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_reader32(\n 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 s" +
"ize_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n memmove(wptr, rptr, n);\n *ptr_wptr += n;\n *ptr_rptr += n;\n }\n return n;\n}\n\nstatic inline uint64_t puffs_base__writer1__copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base__slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base__slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
""
type template_args_short_read struct {
+18 -18
View File
@@ -156,7 +156,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
}
if isThatMethod(g.tm, n, t.KeySuffix, 1) {
// TODO: don't assume that the slice is a slice of u8.
b.writes("puffs_base_slice_u8_suffix(")
b.writes("puffs_base__slice_u8_suffix(")
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
@@ -169,12 +169,12 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeySlice, 0) {
b.printf("((puffs_base_slice_u8){ .ptr = %swstart_dst, .len = %swptr_dst - %swstart_dst, })",
b.printf("((puffs_base__slice_u8){ .ptr = %swstart_dst, .len = %swptr_dst - %swstart_dst, })",
bPrefix, bPrefix, bPrefix)
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromReader32, 2) {
b.printf("puffs_base_writer1_copy_from_reader32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
b.printf("puffs_base__writer1__copy_from_reader32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
// TODO: don't assume that the first argument is "in.src".
b.printf(", &%srptr_src, %srend_src,", bPrefix, bPrefix)
a := n.Args()[1].Arg().Value()
@@ -185,7 +185,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromHistory32, 2) {
b.printf("puffs_base_writer1_copy_from_history32(&%swptr_dst, %swstart_dst, %swend_dst",
b.printf("puffs_base__writer1__copy_from_history32(&%swptr_dst, %swstart_dst, %swend_dst",
bPrefix, bPrefix, bPrefix)
for _, o := range n.Args() {
b.writeb(',')
@@ -197,7 +197,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromSlice32, 2) {
b.printf("puffs_base_writer1_copy_from_slice32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
b.printf("puffs_base__writer1__copy_from_slice32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
for _, o := range n.Args() {
b.writeb(',')
if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil {
@@ -208,7 +208,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromSlice, 1) {
b.printf("puffs_base_writer1_copy_from_slice(&%swptr_dst, %swend_dst,", bPrefix, bPrefix)
b.printf("puffs_base__writer1__copy_from_slice(&%swptr_dst, %swend_dst,", bPrefix, bPrefix)
a := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, a, rp, parenthesesOptional, depth); err != nil {
return err
@@ -217,7 +217,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isThatMethod(g.tm, n, t.KeyCopyFromSlice, 1) {
b.writes("puffs_base_slice_u8_copy_from_slice(")
b.writes("puffs_base__slice_u8__copy_from_slice(")
receiver := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, receiver, rp, parenthesesOptional, depth); err != nil {
return err
@@ -310,17 +310,17 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
rhs := n.RHS().Expr()
switch {
case mhs != nil && rhs == nil:
b.writes("puffs_base_slice_u8_subslice_i(")
b.writes("puffs_base__slice_u8__subslice_i(")
case mhs == nil && rhs != nil:
b.writes("puffs_base_slice_u8_subslice_j(")
b.writes("puffs_base__slice_u8__subslice_j(")
case mhs != nil && rhs != nil:
b.writes("puffs_base_slice_u8_subslice_ij(")
b.writes("puffs_base__slice_u8__subslice_ij(")
}
lhsIsArray := lhs.MType().Decorator().Key() == t.KeyOpenBracket
if lhsIsArray {
// TODO: don't assume that the slice is a slice of u8.
b.writes("((puffs_base_slice_u8){.ptr=")
b.writes("((puffs_base__slice_u8){.ptr=")
}
if err := g.writeExpr(b, lhs, rp, parenthesesOptional, depth); err != nil {
return err
@@ -368,9 +368,9 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
case t.KeyLimit:
// TODO: don't hard code so much detail.
b.writes("(puffs_base_reader1) {")
b.writes("(puffs_base__reader1) {")
b.writes(".buf = a_src.buf,")
b.writes(".limit = (puffs_base_limit1) {")
b.writes(".limit = (puffs_base__limit1) {")
b.printf(".ptr_to_len = &%s%s,", lPrefix, g.currFunk.limitVarName)
b.writes(".next = &a_src.limit,")
b.writes("}}")
@@ -449,7 +449,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
if n.Decorator().Key() == t.KeyColon {
o := n.Inner()
if o.Decorator() == 0 && o.Name().Key() == t.KeyU8 && !o.IsRefined() {
b.writes("puffs_base_slice_u8")
b.writes("puffs_base__slice_u8")
b.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
@@ -519,10 +519,10 @@ var cTypeNames = [...]string{
t.KeyU64: "uint64_t",
t.KeyUsize: "size_t",
t.KeyBool: "bool",
t.KeyBuf1: "puffs_base_buf1",
t.KeyReader1: "puffs_base_reader1",
t.KeyWriter1: "puffs_base_writer1",
t.KeyBuf2: "puffs_base_buf2",
t.KeyBuf1: "puffs_base__buf1",
t.KeyReader1: "puffs_base__reader1",
t.KeyWriter1: "puffs_base__writer1",
t.KeyBuf2: "puffs_base__buf2",
}
var cOpNames = [256]string{
+2 -2
View File
@@ -168,7 +168,7 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
}
b.writes("}")
b.printf("if (self->private_impl.magic != PUFFS_MAGIC) {"+
b.printf("if (self->private_impl.magic != PUFFS_BASE__MAGIC) {"+
"self->private_impl.status = %sERROR_INITIALIZER_NOT_CALLED; }", g.PKGPREFIX)
b.writes("if (self->private_impl.status < 0) {")
@@ -226,7 +226,7 @@ func (g *gen) writeFuncImplBodyResume(b *buffer) error {
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
//
// The matching } is written below. See "Close the coroutine switch".
b.writes("switch (coro_susp_point) {\nPUFFS_COROUTINE_SUSPENSION_POINT(0);\n\n")
b.writes("switch (coro_susp_point) {\nPUFFS_BASE__COROUTINE_SUSPENSION_POINT(0);\n\n")
}
return nil
}
+8 -7
View File
@@ -226,7 +226,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
}
} else if n.XType().Decorator().Key() == t.KeyColon {
// TODO: don't assume that the slice is a slice of u8.
b.printf("((puffs_base_slice_u8){})")
b.printf("((puffs_base__slice_u8){})")
} else {
b.writeb('0')
}
@@ -250,7 +250,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
// TODO: don't assume that the slice is a slice of u8. In particular,
// the code gen can be subtle if the slice element type has zero size,
// such as the empty struct.
b.printf("puffs_base_slice_u8 %sslice_%s =", iPrefix, name)
b.printf("puffs_base__slice_u8 %sslice_%s =", iPrefix, name)
if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
@@ -334,7 +334,7 @@ func (g *gen) writeCoroSuspPoint(b *buffer, maybeSuspend bool) error {
if maybeSuspend {
macro = "_MAYBE_SUSPEND"
}
b.printf("PUFFS_COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
b.printf("PUFFS_BASE__COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
return nil
}
@@ -388,7 +388,8 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("if (PUFFS_UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }", bPrefix, bPrefix)
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }",
bPrefix, bPrefix)
g.currFunk.shortReads = append(g.currFunk.shortReads, "src")
// TODO: watch for passing an array type to writeCTypeName? In C, an
// array type can decay into a pointer.
@@ -612,8 +613,8 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
b.printf("if (PUFFS_LIKELY(%srend_src - %srptr_src >= %d)) {", bPrefix, bPrefix, size/8)
b.printf("%s%d = puffs_base_load_u%d%s(%srptr_src);\n", tPrefix, temp1, size, endianness, bPrefix)
b.printf("if (PUFFS_BASE__LIKELY(%srend_src - %srptr_src >= %d)) {", bPrefix, bPrefix, size/8)
b.printf("%s%d = puffs_base__load_u%d%s(%srptr_src);\n", tPrefix, temp1, size, endianness, bPrefix)
b.printf("%srptr_src += %d;\n", bPrefix, size/8)
b.printf("} else {")
b.printf("%s = 0;\n", scratchName)
@@ -622,7 +623,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
}
b.printf("while (true) {")
b.printf("if (PUFFS_UNLIKELY(%srptr_%s == %srend_%s)) { goto short_read_%s; }",
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_%s == %srend_%s)) { goto short_read_%s; }",
bPrefix, name, bPrefix, name, name)
g.currFunk.shortReads = append(g.currFunk.shortReads, name)
+9 -9
View File
@@ -99,7 +99,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
b.printf("%srstart_%s = %srptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
}
b.printf("size_t len = %s%s.buf->wi - %s%s.buf->ri;", aPrefix, nameStr, aPrefix, nameStr)
b.printf("puffs_base_limit1* lim;")
b.printf("puffs_base__limit1* lim;")
b.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { len = *lim->ptr_to_len; }")
b.printf("}")
@@ -123,7 +123,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
b.printf("%swend_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
b.printf("if (!%s%s.buf->closed) {", aPrefix, nameStr)
b.printf("size_t len = %s%s.buf->len - %s%s.buf->wi;", aPrefix, nameStr, aPrefix, nameStr)
b.printf("puffs_base_limit1* lim;")
b.printf("puffs_base__limit1* lim;")
b.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { len = *lim->ptr_to_len; }")
b.printf("}")
@@ -150,14 +150,14 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
b.printf("size_t n = %srptr_%s - (%s%s.buf->ptr + %s%s.buf->ri);",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
b.printf("%s%s.buf->ri += n;", aPrefix, nameStr)
b.printf("puffs_base_limit1* lim;")
b.printf("puffs_base__limit1* lim;")
b.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len) { *lim->ptr_to_len -= n; }")
b.printf("}")
if footer {
b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srstart_%s);", bPrefix, nameStr)
b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srend_%s);", bPrefix, nameStr)
b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srstart_%s);", bPrefix, nameStr)
b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srend_%s);", bPrefix, nameStr)
}
b.printf("}\n")
@@ -168,14 +168,14 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
b.printf("size_t n = %swptr_%s - (%s%s.buf->ptr + %s%s.buf->wi);",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
b.printf("%s%s.buf->wi += n;", aPrefix, nameStr)
b.printf("puffs_base_limit1* lim;")
b.printf("puffs_base__limit1* lim;")
b.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len) { *lim->ptr_to_len -= n; }")
b.printf("}")
if footer {
b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swstart_%s);", bPrefix, nameStr)
b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr)
b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swstart_%s);", bPrefix, nameStr)
b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr)
}
b.printf("}\n")
@@ -280,7 +280,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, prefix string, suspend bo
}
case t.KeyColon:
// TODO: don't assume that the slice is a slice of u8.
rhs = "puffs_base_slice_u8"
rhs = "puffs_base__slice_u8"
}
if rhs != "" {
b.printf("%s = ((%s){});\n", local, rhs)