mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
Rename PUFFS to WUFFS, part 7 of N
This commit is contained in:
1 parent
eda3130628
commit
3c708dfe59
23 files changed
+1610
-1610
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// After editing this file, run "go generate" in this directory.
|
||||
|
||||
#ifndef PUFFS_BASE_HEADER_H
|
||||
#define PUFFS_BASE_HEADER_H
|
||||
#ifndef WUFFS_BASE_HEADER_H
|
||||
#define WUFFS_BASE_HEADER_H
|
||||
|
||||
// Copyright 2017 The Wuffs Authors.
|
||||
//
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// Puffs requires a word size of at least 32 bits because it assumes that
|
||||
// Wuffs requires a word size of at least 32 bits because it assumes that
|
||||
// converting a u32 to usize will never overflow. For example, the size of a
|
||||
// decoded image is often represented, explicitly or implicitly in an image
|
||||
// file, as a u32, and it is convenient to compare that to a buffer size.
|
||||
@@ -29,12 +29,12 @@
|
||||
// Similarly, the word size is at most 64 bits because it assumes that
|
||||
// converting a usize to u64 will never overflow.
|
||||
#if __WORDSIZE < 32
|
||||
#error "Puffs requires a word size of at least 32 bits"
|
||||
#error "Wuffs requires a word size of at least 32 bits"
|
||||
#elif __WORDSIZE > 64
|
||||
#error "Puffs requires a word size of at most 64 bits"
|
||||
#error "Wuffs requires a word size of at most 64 bits"
|
||||
#endif
|
||||
|
||||
// PUFFS_VERSION is the major.minor version number as a uint32. The major
|
||||
// WUFFS_VERSION is the major.minor version number as a uint32. The major
|
||||
// number is the high 16 bits. The minor number is the low 16 bits.
|
||||
//
|
||||
// The intention is to bump the version number at least on every API / ABI
|
||||
@@ -43,17 +43,17 @@
|
||||
// For now, the API and ABI are simply unstable and can change at any time.
|
||||
//
|
||||
// TODO: don't hard code this in base-header.h.
|
||||
#define PUFFS_VERSION (0x00001)
|
||||
#define WUFFS_VERSION (0x00001)
|
||||
|
||||
// puffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).
|
||||
// wuffs_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;
|
||||
} wuffs_base__slice_u8;
|
||||
|
||||
// puffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus
|
||||
// wuffs_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,43 +63,43 @@ 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;
|
||||
} wuffs_base__buf1;
|
||||
|
||||
// puffs_base__limit1 provides a limited view of a 1-dimensional byte stream:
|
||||
// wuffs_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 {
|
||||
typedef struct wuffs_base__limit1 {
|
||||
uint64_t* ptr_to_len; // Pointer to N.
|
||||
struct puffs_base__limit1* next; // Linked list of limits.
|
||||
} puffs_base__limit1;
|
||||
struct wuffs_base__limit1* next; // Linked list of limits.
|
||||
} wuffs_base__limit1;
|
||||
|
||||
typedef struct {
|
||||
// TODO: move buf into private_impl? As it is, it looks like users can modify
|
||||
// the buf field to point to a different buffer, which can turn the limit and
|
||||
// mark fields into dangling pointers.
|
||||
puffs_base__buf1* buf;
|
||||
wuffs_base__buf1* buf;
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
puffs_base__limit1 limit;
|
||||
wuffs_base__limit1 limit;
|
||||
uint8_t* mark;
|
||||
} private_impl;
|
||||
} puffs_base__reader1;
|
||||
} wuffs_base__reader1;
|
||||
|
||||
typedef struct {
|
||||
// TODO: move buf into private_impl? As it is, it looks like users can modify
|
||||
// the buf field to point to a different buffer, which can turn the limit and
|
||||
// mark fields into dangling pointers.
|
||||
puffs_base__buf1* buf;
|
||||
wuffs_base__buf1* buf;
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
puffs_base__limit1 limit;
|
||||
wuffs_base__limit1 limit;
|
||||
uint8_t* mark;
|
||||
} private_impl;
|
||||
} puffs_base__writer1;
|
||||
} wuffs_base__writer1;
|
||||
|
||||
#endif // PUFFS_BASE_HEADER_H
|
||||
#endif // WUFFS_BASE_HEADER_H
|
||||
@@ -1,7 +1,7 @@
|
||||
// After editing this file, run "go generate" in this directory.
|
||||
|
||||
#ifndef PUFFS_BASE_IMPL_H
|
||||
#define PUFFS_BASE_IMPL_H
|
||||
#ifndef WUFFS_BASE_IMPL_H
|
||||
#define WUFFS_BASE_IMPL_H
|
||||
|
||||
// Copyright 2017 The Wuffs Authors.
|
||||
//
|
||||
@@ -17,39 +17,39 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// puffs_base__empty_struct is used when a Puffs function returns an empty
|
||||
// wuffs_base__empty_struct is used when a Wuffs function returns an empty
|
||||
// struct. In C, if a function f returns void, you can't say "x = f()", but in
|
||||
// Puffs, if a function g returns empty, you can say "y = g()".
|
||||
// Wuffs, if a function g returns empty, you can say "y = g()".
|
||||
typedef struct {
|
||||
} puffs_base__empty_struct;
|
||||
} wuffs_base__empty_struct;
|
||||
|
||||
#define PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
|
||||
#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
|
||||
|
||||
// PUFFS_BASE__MAGIC is a magic number to check that initializers are called.
|
||||
// WUFFS_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_BASE__MAGIC (0xCB3699CCU)
|
||||
#define WUFFS_BASE__MAGIC (0xCB3699CCU)
|
||||
|
||||
// PUFFS_BASE__ALREADY_ZEROED is passed from a container struct's initializer
|
||||
// WUFFS_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_BASE__ALREADY_ZEROED (0x68602EF1U)
|
||||
#define WUFFS_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 trivial macros instead of an explicit assignment and case statement
|
||||
// so that clang-format doesn't get confused by the unusual "case"s.
|
||||
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;
|
||||
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \
|
||||
#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;
|
||||
#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \
|
||||
coro_susp_point = n; \
|
||||
case n:;
|
||||
|
||||
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
||||
#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
||||
if (status < 0) { \
|
||||
goto exit; \
|
||||
} else if (status == 0) { \
|
||||
@@ -61,11 +61,11 @@ typedef struct {
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
#define PUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))
|
||||
#define PUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
|
||||
#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))
|
||||
#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
|
||||
#else
|
||||
#define PUFFS_BASE__LIKELY(expr) (expr)
|
||||
#define PUFFS_BASE__UNLIKELY(expr) (expr)
|
||||
#define WUFFS_BASE__LIKELY(expr) (expr)
|
||||
#define WUFFS_BASE__UNLIKELY(expr) (expr)
|
||||
#endif
|
||||
|
||||
// Uncomment this #include for printf-debugging.
|
||||
@@ -82,68 +82,68 @@ typedef struct {
|
||||
// "gif.c", without having to additionally include or otherwise build and link
|
||||
// a "base.c" file.
|
||||
//
|
||||
// They are static, so that linking multiple puffs .o files won't complain about
|
||||
// They are static, so that linking multiple wuffs .o files won't complain about
|
||||
// duplicate function definitions.
|
||||
//
|
||||
// They are explicitly marked inline, even if modern compilers don't use the
|
||||
// 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 wuffs_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 wuffs_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 wuffs_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 wuffs_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 wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_i(
|
||||
wuffs_base__slice_u8 s,
|
||||
uint64_t i) {
|
||||
if ((i <= SIZE_MAX) && (i <= s.len)) {
|
||||
return ((puffs_base__slice_u8){
|
||||
return ((wuffs_base__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = s.len - i,
|
||||
});
|
||||
}
|
||||
return ((puffs_base__slice_u8){});
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
static inline puffs_base__slice_u8 puffs_base__slice_u8__subslice_j(
|
||||
puffs_base__slice_u8 s,
|
||||
static inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_j(
|
||||
wuffs_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 ((wuffs_base__slice_u8){.ptr = s.ptr, .len = j});
|
||||
}
|
||||
return ((puffs_base__slice_u8){});
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
static inline puffs_base__slice_u8 puffs_base__slice_u8__subslice_ij(
|
||||
puffs_base__slice_u8 s,
|
||||
static inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_ij(
|
||||
wuffs_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 ((wuffs_base__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = j - i,
|
||||
});
|
||||
}
|
||||
return ((puffs_base__slice_u8){});
|
||||
return ((wuffs_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,
|
||||
// wuffs_base__slice_u8__prefix returns up to the first up_to bytes of s.
|
||||
static inline wuffs_base__slice_u8 wuffs_base__slice_u8__prefix(
|
||||
wuffs_base__slice_u8 s,
|
||||
uint64_t up_to) {
|
||||
if ((uint64_t)(s.len) > up_to) {
|
||||
s.len = up_to;
|
||||
@@ -151,9 +151,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,
|
||||
// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.
|
||||
static inline wuffs_base__slice_u8 wuffs_base__slice_u8_suffix(
|
||||
wuffs_base__slice_u8 s,
|
||||
uint64_t up_to) {
|
||||
if ((uint64_t)(s.len) > up_to) {
|
||||
s.ptr += (uint64_t)(s.len) - up_to;
|
||||
@@ -162,14 +162,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,
|
||||
// wuffs_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 wuffs_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 wuffs_base__slice_u8__copy_from_slice(
|
||||
wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src) {
|
||||
size_t length = dst.len < src.len ? dst.len : src.len;
|
||||
if (length > 0) {
|
||||
memmove(dst.ptr, src.ptr, length);
|
||||
@@ -177,7 +177,7 @@ static inline uint64_t puffs_base__slice_u8__copy_from_slice(
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base__writer1__copy_from_history32(
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32(
|
||||
uint8_t** ptr_ptr,
|
||||
uint8_t* start, // May be NULL, meaning an unmarked writer1.
|
||||
uint8_t* end,
|
||||
@@ -200,10 +200,10 @@ static inline uint32_t puffs_base__writer1__copy_from_history32(
|
||||
// TODO: unrolling by 3 seems best for the std/flate benchmarks, but that is
|
||||
// mostly because 3 is the minimum length for the flate format. This function
|
||||
// implementation shouldn't overfit to that one format. Perhaps the
|
||||
// copy_from_history32 Puffs method should also take an unroll hint argument,
|
||||
// copy_from_history32 Wuffs method should also take an unroll hint argument,
|
||||
// and the cgen can look if that argument is the constant expression '3'.
|
||||
//
|
||||
// See also puffs_base__writer1__copy_from_history32__bco below.
|
||||
// See also wuffs_base__writer1__copy_from_history32__bco below.
|
||||
//
|
||||
// Alternatively, or additionally, have a sloppy_copy_from_history32 method
|
||||
// that copies 8 bytes at a time, possibly writing more than length bytes?
|
||||
@@ -219,14 +219,14 @@ static inline uint32_t puffs_base__writer1__copy_from_history32(
|
||||
return length;
|
||||
}
|
||||
|
||||
// puffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized
|
||||
// version of the puffs_base__writer1__copy_from_history32 function above. The
|
||||
// wuffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized
|
||||
// version of the wuffs_base__writer1__copy_from_history32 function above. The
|
||||
// caller needs to prove that:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t puffs_base__writer1__copy_from_history32__bco(
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32__bco(
|
||||
uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
@@ -247,7 +247,7 @@ static inline uint32_t puffs_base__writer1__copy_from_history32__bco(
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base__writer1__copy_from_reader32(
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_reader32(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
uint8_t** ptr_rptr,
|
||||
@@ -270,10 +270,10 @@ static inline uint32_t puffs_base__writer1__copy_from_reader32(
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t puffs_base__writer1__copy_from_slice(
|
||||
static inline uint64_t wuffs_base__writer1__copy_from_slice(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
puffs_base__slice_u8 src) {
|
||||
wuffs_base__slice_u8 src) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t n = src.len;
|
||||
if (n > wend - wptr) {
|
||||
@@ -286,10 +286,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 wuffs_base__writer1__copy_from_slice32(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
puffs_base__slice_u8 src,
|
||||
wuffs_base__slice_u8 src,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t n = src.len;
|
||||
@@ -308,39 +308,39 @@ static inline uint32_t puffs_base__writer1__copy_from_slice32(
|
||||
|
||||
// Note that the *__limit and *__mark methods are private (in base-impl.h) not
|
||||
// public (in base-header.h). We assume that, at the boundary between user code
|
||||
// and Puffs code, the reader1 and writer1's private_impl fields (including
|
||||
// and Wuffs code, the reader1 and writer1's private_impl fields (including
|
||||
// limit and mark) are NULL. Otherwise, some internal assumptions break down.
|
||||
// For example, limits could be represented as pointers, even though
|
||||
// conceptually they are counts, but that pointer-to-count correspondence
|
||||
// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).
|
||||
//
|
||||
// Admittedly, some of the Puffs test code calls these methods, but that test
|
||||
// code is still Puffs code, not user code. Other Puffs test code modifies
|
||||
// Admittedly, some of the Wuffs test code calls these methods, but that test
|
||||
// code is still Wuffs code, not user code. Other Wuffs test code modifies
|
||||
// private_impl fields directly.
|
||||
|
||||
static inline puffs_base__reader1 puffs_base__reader1__limit(
|
||||
puffs_base__reader1* o,
|
||||
static inline wuffs_base__reader1 wuffs_base__reader1__limit(
|
||||
wuffs_base__reader1* o,
|
||||
uint64_t* ptr_to_len) {
|
||||
puffs_base__reader1 ret = *o;
|
||||
wuffs_base__reader1 ret = *o;
|
||||
ret.private_impl.limit.ptr_to_len = ptr_to_len;
|
||||
ret.private_impl.limit.next = &o->private_impl.limit;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline puffs_base__empty_struct puffs_base__reader1__mark(
|
||||
puffs_base__reader1* o,
|
||||
static inline wuffs_base__empty_struct wuffs_base__reader1__mark(
|
||||
wuffs_base__reader1* o,
|
||||
uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((puffs_base__empty_struct){});
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit()
|
||||
// TODO: static inline wuffs_base__writer1 wuffs_base__writer1__limit()
|
||||
|
||||
static inline puffs_base__empty_struct puffs_base__writer1__mark(
|
||||
puffs_base__writer1* o,
|
||||
static inline wuffs_base__empty_struct wuffs_base__writer1__mark(
|
||||
wuffs_base__writer1* o,
|
||||
uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((puffs_base__empty_struct){});
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
#endif // WUFFS_BASE_IMPL_H
|
||||
@@ -39,7 +39,7 @@ var (
|
||||
)
|
||||
|
||||
// Prefixes are prepended to names to form a namespace and to avoid e.g.
|
||||
// "double" being a valid Puffs variable name but not a valid C one.
|
||||
// "double" being a valid Wuffs variable name but not a valid C one.
|
||||
const (
|
||||
aPrefix = "a_" // Function argument.
|
||||
bPrefix = "b_" // Derived from a function argument.
|
||||
@@ -50,17 +50,17 @@ const (
|
||||
vPrefix = "v_" // Local variable.
|
||||
)
|
||||
|
||||
// Do transpiles a Puffs program to a C program.
|
||||
// Do transpiles a Wuffs program to a C program.
|
||||
//
|
||||
// The arguments list the source Puffs files. If no arguments are given, it
|
||||
// The arguments list the source Wuffs files. If no arguments are given, it
|
||||
// reads from stdin.
|
||||
//
|
||||
// The generated program is written to stdout.
|
||||
func Do(args []string) error {
|
||||
return generate.Do(args, func(pkgName string, tm *t.Map, c *check.Checker, files []*a.File) ([]byte, error) {
|
||||
g := &gen{
|
||||
PKGPREFIX: "PUFFS_" + strings.ToUpper(pkgName) + "__",
|
||||
pkgPrefix: "puffs_" + pkgName + "__",
|
||||
PKGPREFIX: "WUFFS_" + strings.ToUpper(pkgName) + "__",
|
||||
pkgPrefix: "wuffs_" + pkgName + "__",
|
||||
pkgName: pkgName,
|
||||
tm: tm,
|
||||
checker: c,
|
||||
@@ -153,8 +153,8 @@ func (b *buffer) writes(s string) { *b = append(*b, s.
|
||||
func (b *buffer) writex(s []byte) { *b = append(*b, s...) }
|
||||
|
||||
type gen struct {
|
||||
PKGPREFIX string // e.g. "PUFFS_JPEG__"
|
||||
pkgPrefix string // e.g. "puffs_jpeg__"
|
||||
PKGPREFIX string // e.g. "WUFFS_JPEG__"
|
||||
pkgPrefix string // e.g. "wuffs_jpeg__"
|
||||
pkgName string // e.g. "jpeg"
|
||||
|
||||
tm *t.Map
|
||||
@@ -211,7 +211,7 @@ func (g *gen) generate() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (g *gen) genHeader(b *buffer) error {
|
||||
includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
||||
includeGuard := "WUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
||||
b.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
|
||||
|
||||
b.printf("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
|
||||
@@ -492,7 +492,7 @@ func (g *gen) writeConstList(b *buffer, n *a.Expr) error {
|
||||
func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
|
||||
// For API/ABI compatibility, the very first field in the struct's
|
||||
// private_impl must be the status code. This lets the initializer callee
|
||||
// set "self->private_impl.status = PUFFS_ETC__ERROR_BAD_PUFFS_VERSION;"
|
||||
// set "self->private_impl.status = WUFFS_ETC__ERROR_BAD_WUFFS_VERSION;"
|
||||
// regardless of the sizeof(*self) struct reserved by the caller and even
|
||||
// if the caller and callee were built with different versions.
|
||||
structName := n.Name().String(g.tm)
|
||||
@@ -563,10 +563,10 @@ func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) err
|
||||
b.printf("// It should be called before any other %s%s__* function.\n",
|
||||
g.pkgPrefix, structName)
|
||||
b.printf("//\n")
|
||||
b.printf("// Pass PUFFS_VERSION and 0 for puffs_version and for_internal_use_only.\n")
|
||||
b.printf("// Pass WUFFS_VERSION and 0 for wuffs_version and for_internal_use_only.\n")
|
||||
}
|
||||
b.printf("void %s%s__initialize(%s%s *self", g.pkgPrefix, structName, g.pkgPrefix, structName)
|
||||
b.printf(", uint32_t puffs_version, uint32_t for_internal_use_only")
|
||||
b.printf(", uint32_t wuffs_version, uint32_t for_internal_use_only")
|
||||
b.printf(")")
|
||||
return nil
|
||||
}
|
||||
@@ -592,14 +592,14 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
|
||||
b.printf("{\n")
|
||||
b.printf("if (!self) { return; }\n")
|
||||
|
||||
b.printf("if (puffs_version != PUFFS_VERSION) {\n")
|
||||
b.printf("self->private_impl.status = %sERROR_BAD_PUFFS_VERSION;\n", g.PKGPREFIX)
|
||||
b.printf("if (wuffs_version != WUFFS_VERSION) {\n")
|
||||
b.printf("self->private_impl.status = %sERROR_BAD_WUFFS_VERSION;\n", g.PKGPREFIX)
|
||||
b.printf("return;\n")
|
||||
b.printf("}\n")
|
||||
|
||||
b.writes("if (for_internal_use_only != PUFFS_BASE__ALREADY_ZEROED) {" +
|
||||
b.writes("if (for_internal_use_only != WUFFS_BASE__ALREADY_ZEROED) {" +
|
||||
"memset(self, 0, sizeof(*self)); }\n")
|
||||
b.writes("self->private_impl.magic = PUFFS_BASE__MAGIC;\n")
|
||||
b.writes("self->private_impl.magic = WUFFS_BASE__MAGIC;\n")
|
||||
|
||||
for _, f := range n.Fields() {
|
||||
f := f.Field()
|
||||
@@ -621,7 +621,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_BASE__ALREADY_ZEROED);\n",
|
||||
"WUFFS_VERSION, WUFFS_BASE__ALREADY_ZEROED);\n",
|
||||
g.pkgPrefix, x.Name().String(g.tm), fPrefix, f.Name().String(g.tm))
|
||||
}
|
||||
|
||||
|
||||
@@ -17,24 +17,24 @@
|
||||
package cgen
|
||||
|
||||
const baseHeader = "" +
|
||||
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Wuffs 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, 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 // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the l" +
|
||||
"imit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n puffs_base__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} puffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n puffs_base__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} puffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
||||
"#ifndef WUFFS_BASE_HEADER_H\n#define WUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Wuffs 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// Wuffs 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 \"Wuffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Wuffs requires a word size of at most 64 bits\"\n#endif\n\n// WUFFS_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 WUFFS_VERSION (0x00001)\n\n// wuffs_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} wuffs_base__slice_u8;\n\n// wuffs_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} wuffs_base__buf1;\n\n// wuffs_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 wuffs_base__limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct wuffs_base__limit1* next; // Linked list of limits.\n} wuffs_base__limit1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the l" +
|
||||
"imit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__writer1;\n\n#endif // WUFFS_BASE_HEADER_H\n" +
|
||||
""
|
||||
|
||||
const baseImpl = "" +
|
||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Copyright 2017 The Wuffs 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// puffs_base__empty_struct is used when a Puffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Puffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n} puffs_base__empty_struct;\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 trivial macros 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_0 case 0:;\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#else\n#define PUFFS_BASE__LIKELY(expr) (expr)\n#define PUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// Uncomment this #include for printf-debugging.\n// #include <stdio.h>\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their argume" +
|
||||
"nts\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 puf" +
|
||||
"fs_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 puffs_base__slice_u8__subslice_ij(\n puffs_base__slice_u8 s,\n uint64_t i,\n uin" +
|
||||
"t64_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.\ns" +
|
||||
"tatic 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(\n uint8_t** ptr_ptr,\n uint8_t* start, // May be NULL, meaning an unmarked writer1.\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n if (!start || !distance) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n if ((size_t)(ptr - start) < (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: unrolling by 3 seems best for the std/flate benchmarks, but that is\n // mostly because 3 is the minimum length for the flate format. This function\n // implementation shouldn't overfit to that one format. Perhaps the\n // copy_from_history32 Puffs method shoul" +
|
||||
"d also take an unroll hint argument,\n // and the cgen can look if that argument is the constant expression '3'.\n //\n // See also puffs_base__writer1__copy_from_history32__bco below.\n //\n // Alternatively, or additionally, have a sloppy_copy_from_history32 method\n // that copies 8 bytes at a time, possibly writing more than length bytes?\n for (; n >= 3; n -= 3) {\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\n// puffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized\n// version of the puffs_base__writer1__copy_from_history32 function above. The\n// caller needs to prove that:\n// - start != NULL\n// - distance != 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t puffs_base__writer1__copy_from_history32__bco(\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 start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\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 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 = *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// Note that the *__limit and *__mark methods are private (in base-impl.h) not\n// public (in base-header.h). We assume that, at the boundary between user code\n// and Puffs code, the reader1 and writer1's private_impl fields (including\n// limit and mark) are NULL. Otherwise, some internal assumptions break down.\n// For example, limits could be represented as pointers, even though\n// conceptually they are counts, but that pointer-to-count correspondence\n// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).\n//\n// Admittedly, some of the Puffs test code calls these methods, but that test\n// code is still Puffs code" +
|
||||
", not user code. Other Puffs test code modifies\n// private_impl fields directly.\n\nstatic inline puffs_base__reader1 puffs_base__reader1__limit(\n puffs_base__reader1* o,\n uint64_t* ptr_to_len) {\n puffs_base__reader1 ret = *o;\n ret.private_impl.limit.ptr_to_len = ptr_to_len;\n ret.private_impl.limit.next = &o->private_impl.limit;\n return ret;\n}\n\nstatic inline puffs_base__empty_struct puffs_base__reader1__mark(\n puffs_base__reader1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\n// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit()\n\nstatic inline puffs_base__empty_struct puffs_base__writer1__mark(\n puffs_base__writer1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
"#ifndef WUFFS_BASE_IMPL_H\n#define WUFFS_BASE_IMPL_H\n\n// Copyright 2017 The Wuffs 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// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n} wuffs_base__empty_struct;\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// WUFFS_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 WUFFS_BASE__MAGIC (0xCB3699CCU)\n\n// WUFFS_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 WUFFS_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 trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case" +
|
||||
" n:;\n\n#define WUFFS_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 WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// Uncomment this #include for printf-debugging.\n// #include <stdio.h>\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their argume" +
|
||||
"nts\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 wuffs .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 wuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t wuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t wuf" +
|
||||
"fs_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 wuffs_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 wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_i(\n wuffs_base__slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_j(\n wuffs_base__slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_ij(\n wuffs_base__slice_u8 s,\n uint64_t i,\n uin" +
|
||||
"t64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n// wuffs_base__slice_u8__prefix returns up to the first up_to bytes of s.\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__prefix(\n wuffs_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// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8_suffix(\n wuffs_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// wuffs_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 wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\ns" +
|
||||
"tatic inline uint64_t wuffs_base__slice_u8__copy_from_slice(\n wuffs_base__slice_u8 dst,\n wuffs_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 wuffs_base__writer1__copy_from_history32(\n uint8_t** ptr_ptr,\n uint8_t* start, // May be NULL, meaning an unmarked writer1.\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n if (!start || !distance) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n if ((size_t)(ptr - start) < (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: unrolling by 3 seems best for the std/flate benchmarks, but that is\n // mostly because 3 is the minimum length for the flate format. This function\n // implementation shouldn't overfit to that one format. Perhaps the\n // copy_from_history32 Wuffs method shoul" +
|
||||
"d also take an unroll hint argument,\n // and the cgen can look if that argument is the constant expression '3'.\n //\n // See also wuffs_base__writer1__copy_from_history32__bco below.\n //\n // Alternatively, or additionally, have a sloppy_copy_from_history32 method\n // that copies 8 bytes at a time, possibly writing more than length bytes?\n for (; n >= 3; n -= 3) {\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\n// wuffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized\n// version of the wuffs_base__writer1__copy_from_history32 function above. The\n// caller needs to prove that:\n// - start != NULL\n// - distance != 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t wuffs_base__writer1__copy_from_history32__bco(\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 start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\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 wuffs_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 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 wuffs_base__writer1__copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n wuffs_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 wuffs_base__writer1__copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n wuffs_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// Note that the *__limit and *__mark methods are private (in base-impl.h) not\n// public (in base-header.h). We assume that, at the boundary between user code\n// and Wuffs code, the reader1 and writer1's private_impl fields (including\n// limit and mark) are NULL. Otherwise, some internal assumptions break down.\n// For example, limits could be represented as pointers, even though\n// conceptually they are counts, but that pointer-to-count correspondence\n// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).\n//\n// Admittedly, some of the Wuffs test code calls these methods, but that test\n// code is still Wuffs code" +
|
||||
", not user code. Other Wuffs test code modifies\n// private_impl fields directly.\n\nstatic inline wuffs_base__reader1 wuffs_base__reader1__limit(\n wuffs_base__reader1* o,\n uint64_t* ptr_to_len) {\n wuffs_base__reader1 ret = *o;\n ret.private_impl.limit.ptr_to_len = ptr_to_len;\n ret.private_impl.limit.next = &o->private_impl.limit;\n return ret;\n}\n\nstatic inline wuffs_base__empty_struct wuffs_base__reader1__mark(\n wuffs_base__reader1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n// TODO: static inline wuffs_base__writer1 wuffs_base__writer1__limit()\n\nstatic inline wuffs_base__empty_struct wuffs_base__writer1__mark(\n wuffs_base__writer1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#endif // WUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
@@ -159,7 +159,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("wuffs_base__slice_u8_suffix(")
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
@@ -175,11 +175,11 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
return fmt.Errorf(`TODO: cgen an "in.src.limit" expression`)
|
||||
}
|
||||
if isInSrc(g.tm, n, t.KeyMark, 0) {
|
||||
b.printf("puffs_base__reader1__mark(&%ssrc, %srptr_src)", aPrefix, bPrefix)
|
||||
b.printf("wuffs_base__reader1__mark(&%ssrc, %srptr_src)", aPrefix, bPrefix)
|
||||
return nil
|
||||
}
|
||||
if isInSrc(g.tm, n, t.KeySinceMark, 0) {
|
||||
b.printf("((puffs_base__slice_u8){ "+
|
||||
b.printf("((wuffs_base__slice_u8){ "+
|
||||
".ptr = %ssrc.private_impl.mark, "+
|
||||
".len = %ssrc.private_impl.mark ? (size_t)(%srptr_src - %ssrc.private_impl.mark) : 0, })",
|
||||
aPrefix, aPrefix, bPrefix, aPrefix)
|
||||
@@ -194,7 +194,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
// the function is passed a (ptr writer1) instead of a (writer1)?
|
||||
// Do we still want to have that mark live outside of the function
|
||||
// scope?
|
||||
b.printf("puffs_base__writer1__mark(&%sdst, %swptr_dst)", aPrefix, bPrefix)
|
||||
b.printf("wuffs_base__writer1__mark(&%sdst, %swptr_dst)", aPrefix, bPrefix)
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeySinceMark, 0) {
|
||||
@@ -207,7 +207,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
len0 = aPrefix + "dst.private_impl.mark ?"
|
||||
len1 = ": 0"
|
||||
}
|
||||
b.printf("((puffs_base__slice_u8){ "+
|
||||
b.printf("((wuffs_base__slice_u8){ "+
|
||||
".ptr = %sdst.private_impl.mark, "+
|
||||
".len = %s (size_t)(%swptr_dst - %sdst.private_impl.mark) %s, })",
|
||||
aPrefix, len0, bPrefix, aPrefix, len1)
|
||||
@@ -224,7 +224,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
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("wuffs_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()
|
||||
@@ -239,7 +239,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
if n.BoundsCheckOptimized() {
|
||||
bco = "__bco"
|
||||
}
|
||||
b.printf("puffs_base__writer1__copy_from_history32%s(&%swptr_dst, %sdst.private_impl.mark , %swend_dst",
|
||||
b.printf("wuffs_base__writer1__copy_from_history32%s(&%swptr_dst, %sdst.private_impl.mark , %swend_dst",
|
||||
bco, bPrefix, aPrefix, bPrefix)
|
||||
for _, o := range n.Args() {
|
||||
b.writeb(',')
|
||||
@@ -251,7 +251,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("wuffs_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 {
|
||||
@@ -262,7 +262,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("wuffs_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
|
||||
@@ -271,7 +271,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("wuffs_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
|
||||
@@ -348,12 +348,12 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
}
|
||||
if isThatMethod(g.tm, n, t.KeyMark, 0) {
|
||||
// TODO: don't hard-code v_r or b_rptr_src.
|
||||
b.printf("puffs_base__reader1__mark(&v_r, b_rptr_src)")
|
||||
b.printf("wuffs_base__reader1__mark(&v_r, b_rptr_src)")
|
||||
return nil
|
||||
}
|
||||
if isThatMethod(g.tm, n, t.KeySinceMark, 0) {
|
||||
// TODO: don't hard-code v_r or b_rptr_src.
|
||||
b.printf("((puffs_base__slice_u8){ " +
|
||||
b.printf("((wuffs_base__slice_u8){ " +
|
||||
".ptr = v_r.private_impl.mark, " +
|
||||
".len = v_r.private_impl.mark ? (size_t)(b_rptr_src - v_r.private_impl.mark) : 0, })")
|
||||
return nil
|
||||
@@ -383,17 +383,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("wuffs_base__slice_u8__subslice_i(")
|
||||
case mhs == nil && rhs != nil:
|
||||
b.writes("puffs_base__slice_u8__subslice_j(")
|
||||
b.writes("wuffs_base__slice_u8__subslice_j(")
|
||||
case mhs != nil && rhs != nil:
|
||||
b.writes("puffs_base__slice_u8__subslice_ij(")
|
||||
b.writes("wuffs_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("((wuffs_base__slice_u8){.ptr=")
|
||||
}
|
||||
if err := g.writeExpr(b, lhs, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
@@ -524,13 +524,13 @@ 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("wuffs_base__slice_u8")
|
||||
b.writeb(' ')
|
||||
b.writes(varNamePrefix)
|
||||
b.writes(varName)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
||||
return fmt.Errorf("cannot convert Wuffs type %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
// maxNumPointers is an arbitrary implementation restriction.
|
||||
@@ -545,13 +545,13 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
|
||||
// TODO: "nptr T", not just "ptr T".
|
||||
if p := innermost.Decorator().Key(); p == t.KeyPtr {
|
||||
if numPointers == maxNumPointers {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.String(g.tm))
|
||||
return fmt.Errorf("cannot convert Wuffs type %q to C: too many ptr's", n.String(g.tm))
|
||||
}
|
||||
numPointers++
|
||||
continue
|
||||
}
|
||||
// TODO: fix this.
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
||||
return fmt.Errorf("cannot convert Wuffs type %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
fallback := true
|
||||
@@ -594,10 +594,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: "wuffs_base__buf1",
|
||||
t.KeyReader1: "wuffs_base__reader1",
|
||||
t.KeyWriter1: "wuffs_base__writer1",
|
||||
t.KeyBuf2: "wuffs_base__buf2",
|
||||
}
|
||||
|
||||
var cOpNames = [256]string{
|
||||
|
||||
@@ -176,7 +176,7 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
|
||||
}
|
||||
b.writes("}")
|
||||
|
||||
b.printf("if (self->private_impl.magic != PUFFS_BASE__MAGIC) {"+
|
||||
b.printf("if (self->private_impl.magic != WUFFS_BASE__MAGIC) {"+
|
||||
"self->private_impl.status = %sERROR_INITIALIZER_NOT_CALLED; }", g.PKGPREFIX)
|
||||
|
||||
b.writes("if (self->private_impl.status < 0) {")
|
||||
@@ -234,7 +234,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_BASE__COROUTINE_SUSPENSION_POINT_0;\n\n")
|
||||
b.writes("switch (coro_susp_point) {\nWUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;\n\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -23,10 +23,10 @@ import (
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
// genFilenameLineComments is whether to print "// foo.puffs:123\n" comments in
|
||||
// genFilenameLineComments is whether to print "// foo.wuffs:123\n" comments in
|
||||
// the generated code. This can be useful for debugging, although it is not
|
||||
// enabled by default as it can lead to many spurious changes in the generated
|
||||
// C code (due to line numbers changing) when editing Puffs code.
|
||||
// C code (due to line numbers changing) when editing Wuffs code.
|
||||
const genFilenameLineComments = false
|
||||
|
||||
func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
@@ -168,7 +168,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("wuffs_base__slice_u8 %sslice_%s =", iPrefix, name)
|
||||
if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -292,7 +292,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("((wuffs_base__slice_u8){})")
|
||||
} else {
|
||||
b.writeb('0')
|
||||
}
|
||||
@@ -346,7 +346,7 @@ func (g *gen) writeCoroSuspPoint(b *buffer, maybeSuspend bool) error {
|
||||
if maybeSuspend {
|
||||
macro = "_MAYBE_SUSPEND"
|
||||
}
|
||||
b.printf("PUFFS_BASE__COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
|
||||
b.printf("WUFFS_BASE__COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -447,7 +447,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
g.currFunk.tempW++
|
||||
|
||||
if !n.ProvenNotToSuspend() {
|
||||
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }",
|
||||
b.printf("if (WUFFS_BASE__UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }",
|
||||
bPrefix, bPrefix)
|
||||
g.currFunk.shortReads = append(g.currFunk.shortReads, "src")
|
||||
}
|
||||
@@ -658,7 +658,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
// TODO: don't hard-code a_dst or v_r or l_rlimit or v_block_size.
|
||||
b.writes("uint64_t l_rlimit0 = v_block_size;\n")
|
||||
b.printf("%sstatus %s%d = %slzw_decoder__decode(&self->private_impl.f_lzw, %sdst,"+
|
||||
"puffs_base__reader1__limit(&%sr, &l_rlimit0));\n",
|
||||
"wuffs_base__reader1__limit(&%sr, &l_rlimit0));\n",
|
||||
g.pkgPrefix, tPrefix, temp,
|
||||
g.pkgPrefix, aPrefix, vPrefix)
|
||||
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
|
||||
@@ -667,14 +667,14 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
// TODO: check if tPrefix_temp is an error, and return?
|
||||
|
||||
default:
|
||||
return fmt.Errorf("cannot convert Puffs call %q to C", n.String(g.tm))
|
||||
return fmt.Errorf("cannot convert Wuffs call %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
} else {
|
||||
// TODO: fix this.
|
||||
//
|
||||
// This might involve calling g.writeExpr with replaceNothing??
|
||||
return fmt.Errorf("cannot convert Puffs call %q to C", n.String(g.tm))
|
||||
return fmt.Errorf("cannot convert Wuffs call %q to C", n.String(g.tm))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -708,8 +708,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_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("if (WUFFS_BASE__LIKELY(%srend_src - %srptr_src >= %d)) {", bPrefix, bPrefix, size/8)
|
||||
b.printf("%s%d = wuffs_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)
|
||||
@@ -718,7 +718,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
|
||||
}
|
||||
b.printf("while (true) {")
|
||||
|
||||
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_%s == %srend_%s)) { goto short_read_%s; }",
|
||||
b.printf("if (WUFFS_BASE__UNLIKELY(%srptr_%s == %srend_%s)) { goto short_read_%s; }",
|
||||
bPrefix, name, bPrefix, name, name)
|
||||
g.currFunk.shortReads = append(g.currFunk.shortReads, name)
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
|
||||
|
||||
b.printf("uint64_t len = %s%s.buf->wi - %s%s.buf->ri;", aPrefix, nameStr, aPrefix, nameStr)
|
||||
|
||||
b.printf("puffs_base__limit1* lim;\n")
|
||||
b.printf("wuffs_base__limit1* lim;\n")
|
||||
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
|
||||
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n")
|
||||
b.printf("len = *lim->ptr_to_len;\n")
|
||||
@@ -137,7 +137,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
|
||||
b.printf("if (!%s%s.buf->closed) {", aPrefix, nameStr)
|
||||
b.printf("uint64_t len = %s%s.buf->len - %s%s.buf->wi;", aPrefix, nameStr, aPrefix, nameStr)
|
||||
|
||||
b.printf("puffs_base__limit1* lim;\n")
|
||||
b.printf("wuffs_base__limit1* lim;\n")
|
||||
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
|
||||
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n")
|
||||
b.printf("len = *lim->ptr_to_len;\n")
|
||||
@@ -175,7 +175,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
|
||||
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
|
||||
b.printf("%s%s.buf->ri += n;", aPrefix, nameStr)
|
||||
|
||||
b.printf("puffs_base__limit1* lim;\n")
|
||||
b.printf("wuffs_base__limit1* lim;\n")
|
||||
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
|
||||
b.printf("if (lim->ptr_to_len) {\n")
|
||||
b.printf("*lim->ptr_to_len -= n;\n")
|
||||
@@ -183,8 +183,8 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
|
||||
b.printf("}\n")
|
||||
|
||||
if footer {
|
||||
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("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srstart_%s);", bPrefix, nameStr)
|
||||
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srend_%s);", bPrefix, nameStr)
|
||||
}
|
||||
|
||||
b.printf("}\n")
|
||||
@@ -196,7 +196,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
|
||||
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
|
||||
b.printf("%s%s.buf->wi += n;", aPrefix, nameStr)
|
||||
|
||||
b.printf("puffs_base__limit1* lim;\n")
|
||||
b.printf("wuffs_base__limit1* lim;\n")
|
||||
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
|
||||
b.printf("if (lim->ptr_to_len) {\n")
|
||||
b.printf("*lim->ptr_to_len -= n;\n")
|
||||
@@ -204,8 +204,8 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer
|
||||
b.printf("}\n")
|
||||
|
||||
if footer {
|
||||
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("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swstart_%s);", bPrefix, nameStr)
|
||||
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr)
|
||||
}
|
||||
|
||||
b.printf("}\n")
|
||||
@@ -309,7 +309,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool) error {
|
||||
}
|
||||
case t.KeyColon:
|
||||
// TODO: don't assume that the slice is a slice of u8.
|
||||
rhs = "puffs_base__slice_u8"
|
||||
rhs = "wuffs_base__slice_u8"
|
||||
}
|
||||
if rhs != "" {
|
||||
b.printf("%s = ((%s){});\n", local, rhs)
|
||||
|
||||
+1
-1
@@ -142,7 +142,7 @@ func findPuffsMimicCflags(filename string) ([]string, error) {
|
||||
s := bufio.NewScanner(f)
|
||||
for s.Scan() {
|
||||
t := s.Text()
|
||||
const prefix = "// !! puffs mimic cflags:"
|
||||
const prefix = "// !! wuffs mimic cflags:"
|
||||
if strings.HasPrefix(t, prefix) {
|
||||
t = strings.TrimSpace(t[len(prefix):])
|
||||
return strings.Split(t, " "), nil
|
||||
|
||||
Reference in new issue
Block a user