Rename PUFFS to WUFFS, part 3 of N

This commit is contained in:
Nigel Tao committed 2017-11-30 16:26:16 +11:00
1 parent 0cea46531b
commit 7a6a598d1e
17 files changed
+4 -4

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
cf "github.com/google/wuffs/cmd/commonflags"
)
func doGenlib(args []string) error {
flags := flag.FlagSet{}
ccompilersFlag := flags.String("ccompilers", cf.CcompilersDefault, cf.CcompilersUsage)
dstdirFlag := flags.String("dstdir", "", "directory containing the object files ")
srcdirFlag := flags.String("srcdir", "", "directory containing the C source files")
if err := flags.Parse(args); err != nil {
return err
}
args = flags.Args()
if *dstdirFlag == "" {
return fmt.Errorf("empty -dstdir flag")
}
if *srcdirFlag == "" {
return fmt.Errorf("empty -srcdir flag")
}
for _, cc := range strings.Split(*ccompilersFlag, ",") {
cc = strings.TrimSpace(cc)
if cc == "" {
continue
}
for _, dynamism := range []string{"static", "dynamic"} {
outDir := filepath.Join(*dstdirFlag, cc+"-"+dynamism)
if err := os.MkdirAll(outDir, 0755); err != nil {
return err
}
if err := genObj(outDir, *srcdirFlag, cc, dynamism, args); err != nil {
return err
}
if err := genLib(outDir, cc, dynamism, args); err != nil {
return err
}
}
}
return nil
}
// TODO: are these extensions correct for non-Linux?
var (
objExtensions = map[string]string{
"dynamic": ".lo",
"static": ".o",
}
libExtensions = map[string]string{
"dynamic": ".so",
"static": ".a",
}
)
func genObj(outDir string, inDir string, cc string, dynamism string, filenames []string) error {
for _, filename := range filenames {
in := filepath.Join(inDir, filename+".c")
out := genlibOutFilename(outDir, dynamism, filename)
args := []string(nil)
args = append(args, "-O3", "-std=c99")
if dynamism == "dynamic" {
args = append(args, "-fPIC", "-DPIC")
}
args = append(args, "-c", "-o", out, in)
cmd := exec.Command(cc, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
fmt.Printf("genlib: %s\n", out)
}
return nil
}
func genLib(outDir string, cc string, dynamism string, filenames []string) error {
args := []string(nil)
switch dynamism {
case "dynamic":
// TODO: add a "-Wl,-soname,libpuffs.so.1.2.3" argument?
args = append(args, "-shared", "-fPIC", "-o")
case "static":
cc = "ar"
args = append(args, "rc")
}
out := filepath.Join(outDir, "libpuffs"+libExtensions[dynamism])
args = append(args, out)
for _, filename := range filenames {
args = append(args, genlibOutFilename(outDir, dynamism, filename))
}
cmd := exec.Command(cc, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
fmt.Printf("genlib: %s\n", out)
return nil
}
func genlibOutFilename(outDir string, dynamism string, filename string) string {
filename = strings.Replace(filename, "/", "-", -1)
filename = strings.Replace(filename, "\\", "-", -1)
filename = filepath.Join(outDir, filename+objExtensions[dynamism])
return filename
}
+105
View File
@@ -0,0 +1,105 @@
// After editing this file, run "go generate" in this directory.
#ifndef PUFFS_BASE_HEADER_H
#define PUFFS_BASE_HEADER_H
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
// Puffs 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.
//
// 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"
#elif __WORDSIZE > 64
#error "Puffs requires a word size of at most 64 bits"
#endif
// PUFFS_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
// backwards incompatible change.
//
// 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)
// 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__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.
typedef struct {
uint8_t* ptr; // Pointer.
size_t len; // Length.
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__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 {
// 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;
// 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;
uint8_t* mark;
} private_impl;
} puffs_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;
// 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;
uint8_t* mark;
} private_impl;
} puffs_base__writer1;
#endif // PUFFS_BASE_HEADER_H
+346
View File
@@ -0,0 +1,346 @@
// After editing this file, run "go generate" in this directory.
#ifndef PUFFS_BASE_IMPL_H
#define PUFFS_BASE_IMPL_H
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 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
// 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()".
typedef struct {
} puffs_base__empty_struct;
#define PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
// 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_BASE__MAGIC (0xCB3699CCU)
// 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_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) \
coro_susp_point = n; \
case n:;
#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_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))
#define PUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
#else
#define PUFFS_BASE__LIKELY(expr) (expr)
#define PUFFS_BASE__UNLIKELY(expr) (expr)
#endif
// Uncomment this #include for printf-debugging.
// #include <stdio.h>
// ---------------- Static Inline Functions
//
// The helpers below are functions, instead of macros, because their arguments
// can be an expression that we shouldn't evaluate more than once.
//
// They are in base-impl.h and hence copy/pasted into every generated C file,
// instead of being in some "base.c" file, since a design goal is that users of
// the generated C code can often just #include a single .c file, such as
// "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
// 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) {
return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);
}
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) {
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) {
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,
uint64_t i) {
if ((i <= SIZE_MAX) && (i <= s.len)) {
return ((puffs_base__slice_u8){
.ptr = s.ptr + i,
.len = s.len - i,
});
}
return ((puffs_base__slice_u8){});
}
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){});
}
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){
.ptr = s.ptr + i,
.len = j - i,
});
}
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,
uint64_t up_to) {
if ((uint64_t)(s.len) > up_to) {
s.len = up_to;
}
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,
uint64_t up_to) {
if ((uint64_t)(s.len) > up_to) {
s.ptr += (uint64_t)(s.len) - up_to;
s.len = up_to;
}
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.
//
// 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) {
size_t length = dst.len < src.len ? dst.len : src.len;
if (length > 0) {
memmove(dst.ptr, src.ptr, length);
}
return length;
}
static inline uint32_t puffs_base__writer1__copy_from_history32(
uint8_t** ptr_ptr,
uint8_t* start, // May be NULL, meaning an unmarked writer1.
uint8_t* end,
uint32_t distance,
uint32_t length) {
if (!start || !distance) {
return 0;
}
uint8_t* ptr = *ptr_ptr;
if ((size_t)(ptr - start) < (size_t)(distance)) {
return 0;
}
start = ptr - distance;
size_t n = end - ptr;
if ((size_t)(length) > n) {
length = n;
} else {
n = length;
}
// 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,
// and the cgen can look if that argument is the constant expression '3'.
//
// See also puffs_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?
for (; n >= 3; n -= 3) {
*ptr++ = *start++;
*ptr++ = *start++;
*ptr++ = *start++;
}
for (; n; n--) {
*ptr++ = *start++;
}
*ptr_ptr = ptr;
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
// 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(
uint8_t** ptr_ptr,
uint8_t* start,
uint8_t* end,
uint32_t distance,
uint32_t length) {
uint8_t* ptr = *ptr_ptr;
start = ptr - distance;
uint32_t n = length;
for (; n >= 3; n -= 3) {
*ptr++ = *start++;
*ptr++ = *start++;
*ptr++ = *start++;
}
for (; n; n--) {
*ptr++ = *start++;
}
*ptr_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) {
uint8_t* wptr = *ptr_wptr;
size_t n = length;
if (n > wend - wptr) {
n = wend - wptr;
}
uint8_t* rptr = *ptr_rptr;
if (n > rend - rptr) {
n = rend - rptr;
}
if (n > 0) {
memmove(wptr, rptr, n);
*ptr_wptr += n;
*ptr_rptr += n;
}
return n;
}
static inline uint64_t puffs_base__writer1__copy_from_slice(
uint8_t** ptr_wptr,
uint8_t* wend,
puffs_base__slice_u8 src) {
uint8_t* wptr = *ptr_wptr;
size_t n = src.len;
if (n > wend - wptr) {
n = wend - wptr;
}
if (n > 0) {
memmove(wptr, src.ptr, n);
*ptr_wptr += n;
}
return n;
}
static inline uint32_t puffs_base__writer1__copy_from_slice32(
uint8_t** ptr_wptr,
uint8_t* wend,
puffs_base__slice_u8 src,
uint32_t length) {
uint8_t* wptr = *ptr_wptr;
size_t n = src.len;
if (n > length) {
n = length;
}
if (n > wend - wptr) {
n = wend - wptr;
}
if (n > 0) {
memmove(wptr, src.ptr, n);
*ptr_wptr += n;
}
return n;
}
// 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
// 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
// private_impl fields directly.
static inline puffs_base__reader1 puffs_base__reader1__limit(
puffs_base__reader1* o,
uint64_t* ptr_to_len) {
puffs_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,
uint8_t* mark) {
o->private_impl.mark = mark;
return ((puffs_base__empty_struct){});
}
// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit()
static inline puffs_base__empty_struct puffs_base__writer1__mark(
puffs_base__writer1* o,
uint8_t* mark) {
o->private_impl.mark = mark;
return ((puffs_base__empty_struct){});
}
#endif // PUFFS_BASE_IMPL_H
+630
View File
@@ -0,0 +1,630 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate go run gen.go
package cgen
import (
"bytes"
"fmt"
"math/big"
"os"
"os/exec"
"strings"
"github.com/google/wuffs/lang/base38"
"github.com/google/wuffs/lang/builtin"
"github.com/google/wuffs/lang/check"
"github.com/google/wuffs/lang/generate"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
var (
zero = big.NewInt(0)
one = big.NewInt(1)
)
// 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.
const (
aPrefix = "a_" // Function argument.
bPrefix = "b_" // Derived from a function argument.
cPrefix = "c_" // Coroutine state.
fPrefix = "f_" // Struct field.
iPrefix = "i_" // Iterate variable.
tPrefix = "t_" // Temporary local variable.
vPrefix = "v_" // Local variable.
)
// Do transpiles a Puffs program to a C program.
//
// The arguments list the source Puffs 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 + "__",
pkgName: pkgName,
tm: tm,
checker: c,
files: files,
}
unformatted, err := g.generate()
if err != nil {
return nil, err
}
stdout := &bytes.Buffer{}
cmd := exec.Command("clang-format", "-style=Chromium")
cmd.Stdin = bytes.NewReader(unformatted)
cmd.Stdout = stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
return stdout.Bytes(), nil
})
}
const (
maxNamespacedStatusCode = 255
statusCodeNamespaceMask = 1<<base38.MaxBits - 1
statusCodeNamespaceShift = 10
statusCodeCodeBits = 8
statusCodeDescription = "" +
"// Status codes are int32_t values:\n" +
"// - the sign bit indicates a non-recoverable status code: an error\n" +
"// - bits 10-30 hold the packageid: a namespace\n" +
"// - bits 8-9 are reserved\n" +
"// - bits 0-7 are a package-namespaced numeric code\n"
)
func init() {
// The +1 is for the error bit (the sign bit).
if statusCodeNamespaceShift+base38.MaxBits+1 != 32 {
panic("inconsistent status code namespace shift")
}
if len(builtin.StatusList) > maxNamespacedStatusCode {
panic("too many built-in statuses")
}
}
type replacementPolicy bool
const (
replaceNothing = replacementPolicy(false)
replaceCallSuspendibles = replacementPolicy(true)
)
// parenthesesPolicy controls whether to print the outer parentheses in an
// expression like "(x + y)". An "if" or "while" will print their own
// parentheses for "if (expr)" because they need to be able to say "if (x)".
// But a double-parenthesized expression like "if ((x == y))" is a clang
// warning (-Wparentheses-equality) and we like to compile with -Wall -Werror.
type parenthesesPolicy bool
const (
parenthesesMandatory = parenthesesPolicy(false)
parenthesesOptional = parenthesesPolicy(true)
)
type visibility uint32
const (
bothPubPri = visibility(iota)
pubOnly
priOnly
)
const maxTemp = 10000
type status struct {
name string
msg string
keyword t.ID
}
type buffer []byte
func (b *buffer) Write(p []byte) (int, error) {
*b = append(*b, p...)
return len(p), nil
}
func (b *buffer) printf(format string, args ...interface{}) { fmt.Fprintf(b, format, args...) }
func (b *buffer) writeb(x byte) { *b = append(*b, x) }
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__"
pkgName string // e.g. "jpeg"
tm *t.Map
checker *check.Checker
files []*a.File
statusList []status
statusMap map[t.ID]status
structList []*a.Struct
structMap map[t.ID]*a.Struct
currFunk funk
funks map[t.QID]funk
}
func (g *gen) generate() ([]byte, error) {
b := new(buffer)
g.statusMap = map[t.ID]status{}
if err := g.forEachStatus(b, bothPubPri, (*gen).gatherStatuses); err != nil {
return nil, err
}
// Make a topologically sorted list of structs.
unsortedStructs := []*a.Struct(nil)
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if tld.Kind() == a.KStruct {
unsortedStructs = append(unsortedStructs, tld.Struct())
}
}
}
var ok bool
g.structList, ok = a.TopologicalSortStructs(unsortedStructs)
if !ok {
return nil, fmt.Errorf("cyclical struct definitions")
}
g.structMap = map[t.ID]*a.Struct{}
for _, n := range g.structList {
g.structMap[n.Name()] = n
}
g.funks = map[t.QID]funk{}
if err := g.forEachFunc(nil, bothPubPri, (*gen).gatherFuncImpl); err != nil {
return nil, err
}
if err := g.genHeader(b); err != nil {
return nil, err
}
b.writes("// C HEADER ENDS HERE.\n\n")
if err := g.genImpl(b); err != nil {
return nil, err
}
return *b, nil
}
func (g *gen) genHeader(b *buffer) error {
includeGuard := "PUFFS_" + 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")
b.writes(baseHeader)
b.writes("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
b.writes("// ---------------- Status Codes\n\n")
b.writes(statusCodeDescription)
b.printf("//\n// Do not manipulate these bits directly. Use the API functions such as\n"+
"// %sstatus__is_error instead.\n", g.pkgPrefix)
b.printf("typedef int32_t %sstatus;\n\n", g.pkgPrefix)
pkgID := g.checker.PackageID()
b.printf("#define %spackageid %d // %#08x\n\n", g.pkgPrefix, pkgID, pkgID)
for i, z := range builtin.StatusList {
code := uint32(0)
if z.Keyword == t.IDError {
code |= 1 << 31
}
code |= uint32(i)
b.printf("#define %s %d // %#08x\n", strings.ToUpper(g.cName(z.String())), int32(code), code)
}
b.writes("\n")
if len(g.statusList) > maxNamespacedStatusCode {
return fmt.Errorf("too many status codes")
}
for i, s := range g.statusList {
code := pkgID << statusCodeNamespaceShift
if s.keyword.Key() == t.KeyError {
code |= 1 << 31
}
code |= uint32(i)
b.printf("#define %s %d // %#08x\n", s.name, int32(code), code)
}
b.writes("\n")
b.printf("bool %sstatus__is_error(%sstatus s);\n\n", g.pkgPrefix, g.pkgPrefix)
b.printf("const char* %sstatus__string(%sstatus s);\n\n", g.pkgPrefix, g.pkgPrefix)
b.writes("// ---------------- Public Consts\n\n")
if err := g.forEachConst(b, pubOnly, (*gen).writeConst); err != nil {
return err
}
b.writes("// ---------------- Structs\n\n")
for _, n := range g.structList {
if err := g.writeStruct(b, n); err != nil {
return err
}
}
b.writes("// ---------------- Public Initializer Prototypes\n\n")
for _, n := range g.structList {
if n.Public() {
if err := g.writeInitializerPrototype(b, n); err != nil {
return err
}
}
}
b.writes("// ---------------- Public Function Prototypes\n\n")
if err := g.forEachFunc(b, pubOnly, (*gen).writeFuncPrototype); err != nil {
return err
}
b.writes("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n")
b.printf("#endif // %s\n\n", includeGuard)
return nil
}
func (g *gen) genImpl(b *buffer) error {
b.writes(baseImpl)
b.writes("\n")
b.writes("// ---------------- Status Codes Implementations\n\n")
b.printf("bool %sstatus__is_error(%sstatus s) { return s < 0; }\n\n", g.pkgPrefix, g.pkgPrefix)
b.printf("const char* %sstatus__strings0[%d] = {\n", g.pkgPrefix, len(builtin.StatusList))
for _, z := range builtin.StatusList {
b.printf("%q,", g.pkgName+": "+z.Message)
}
b.writes("};\n\n")
b.printf("const char* %sstatus__strings1[%d] = {\n", g.pkgPrefix, len(g.statusList))
for _, s := range g.statusList {
b.printf("%q,", g.pkgName+": "+s.msg)
}
b.writes("};\n\n")
b.printf("const char* %sstatus__string(%sstatus s) {\n", g.pkgPrefix, g.pkgPrefix)
b.printf("const char** a = NULL;\n")
b.printf("uint32_t n = 0;\n")
b.printf("switch ((s >> %d) & %#x) {\n", statusCodeNamespaceShift, statusCodeNamespaceMask)
b.printf("case 0: a = %sstatus__strings0; n = %d; break;\n", g.pkgPrefix, len(builtin.StatusList))
b.printf("case %spackageid: a = %sstatus__strings1; n = %d; break;\n",
g.pkgPrefix, g.pkgPrefix, len(g.statusList))
// TODO: add cases for other packages used by this one.
b.printf("}\n")
b.printf("uint32_t i = s & %#0x;\n", 1<<statusCodeCodeBits-1)
b.printf("return i < n ? a[i] : \"%s: unknown status\";\n", g.pkgName)
b.writes("}\n\n")
b.writes("// ---------------- Private Consts\n\n")
if err := g.forEachConst(b, priOnly, (*gen).writeConst); err != nil {
return err
}
b.writes("// ---------------- Private Initializer Prototypes\n\n")
for _, n := range g.structList {
if !n.Public() {
if err := g.writeInitializerPrototype(b, n); err != nil {
return err
}
}
}
b.writes("// ---------------- Private Function Prototypes\n\n")
if err := g.forEachFunc(b, priOnly, (*gen).writeFuncPrototype); err != nil {
return err
}
b.writes("// ---------------- Initializer Implementations\n\n")
for _, n := range g.structList {
if err := g.writeInitializerImpl(b, n); err != nil {
return err
}
}
b.writes("// ---------------- Function Implementations\n\n")
if err := g.forEachFunc(b, bothPubPri, (*gen).writeFuncImpl); err != nil {
return err
}
return nil
}
func (g *gen) forEachConst(b *buffer, v visibility, f func(*gen, *buffer, *a.Const) error) error {
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if tld.Kind() != a.KConst ||
(v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) ||
(v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) {
continue
}
if err := f(g, b, tld.Const()); err != nil {
return err
}
}
}
return nil
}
func (g *gen) forEachFunc(b *buffer, v visibility, f func(*gen, *buffer, *a.Func) error) error {
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if tld.Kind() != a.KFunc ||
(v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) ||
(v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) {
continue
}
if err := f(g, b, tld.Func()); err != nil {
return err
}
}
}
return nil
}
func (g *gen) forEachStatus(b *buffer, v visibility, f func(*gen, *buffer, *a.Status) error) error {
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if tld.Kind() != a.KStatus ||
(v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) ||
(v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) {
continue
}
if err := f(g, b, tld.Status()); err != nil {
return err
}
}
}
return nil
}
func (g *gen) cName(name string) string {
s := []byte(g.pkgPrefix)
underscore := true
for _, r := range name {
if 'A' <= r && r <= 'Z' {
s = append(s, byte(r+'a'-'A'))
underscore = false
} else if ('a' <= r && r <= 'z') || ('0' <= r && r <= '9') {
s = append(s, byte(r))
underscore = false
} else if !underscore {
s = append(s, '_')
underscore = true
}
}
if underscore {
s = s[:len(s)-1]
}
return string(s)
}
func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) {
if typ.Decorator() == 0 {
switch typ.Name().Key() {
case t.KeyU8:
return 1, nil
case t.KeyU16:
return 2, nil
case t.KeyU32:
return 4, nil
case t.KeyU64:
return 8, nil
}
}
return 0, fmt.Errorf("unknown sizeof for %q", typ.String(g.tm))
}
func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
raw := n.Message().String(g.tm)
msg, ok := t.Unescape(raw)
if !ok {
return fmt.Errorf("bad status message %q", raw)
}
prefix := "SUSPENSION_"
if n.Keyword().Key() == t.KeyError {
prefix = "ERROR_"
}
s := status{
name: strings.ToUpper(g.cName(prefix + msg)),
msg: msg,
keyword: n.Keyword(),
}
g.statusList = append(g.statusList, s)
g.statusMap[n.Message()] = s
return nil
}
func (g *gen) writeConst(b *buffer, n *a.Const) error {
if !n.Public() {
b.writes("static ")
}
b.writes("const ")
if err := g.writeCTypeName(b, n.XType(), g.pkgPrefix, n.Name().String(g.tm)); err != nil {
return err
}
b.writes(" = ")
if err := g.writeConstList(b, n.Value()); err != nil {
return err
}
b.writes(";\n\n")
return nil
}
func (g *gen) writeConstList(b *buffer, n *a.Expr) error {
switch n.ID0().Key() {
case 0:
b.writes(n.ConstValue().String())
case t.KeyDollar:
b.writeb('{')
for _, o := range n.Args() {
if err := g.writeConstList(b, o.Expr()); err != nil {
return err
}
b.writeb(',')
}
b.writeb('}')
default:
return fmt.Errorf("invalid const value %q", n.String(g.tm))
}
return nil
}
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;"
// 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)
b.writes("typedef struct {\n")
b.writes("// Do not access the private_impl's fields directly. There is no API/ABI\n")
b.writes("// compatibility or safety guarantee if you do so. Instead, use the\n")
b.printf("// %s%s__etc functions.\n", g.pkgPrefix, structName)
b.writes("//\n")
b.writes("// In C++, these fields would be \"private\", but C does not support that.\n")
b.writes("//\n")
b.writes("// It is a struct, not a struct*, so that it can be stack allocated.\n")
b.writes("struct {\n")
if n.Suspendible() {
b.printf("%sstatus status;\n", g.pkgPrefix)
b.writes("uint32_t magic;\n")
b.writes("\n")
}
for _, o := range n.Fields() {
o := o.Field()
if err := g.writeCTypeName(b, o.XType(), fPrefix, o.Name().String(g.tm)); err != nil {
return err
}
b.writes(";\n")
}
if n.Suspendible() {
b.writeb('\n')
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if tld.Kind() != a.KFunc {
continue
}
o := tld.Func()
if o.Receiver() != n.Name() || !o.Suspendible() {
continue
}
k := g.funks[o.QID()]
if k.coroSuspPoint == 0 && !k.usesScratch {
continue
}
// TODO: allow max depth > 1 for recursive coroutines.
const maxDepth = 1
b.writes("struct {\n")
if k.coroSuspPoint != 0 {
b.writes("uint32_t coro_susp_point;\n")
if err := g.writeVars(b, o.Body(), true, true); err != nil {
return err
}
}
if k.usesScratch {
b.writes("uint64_t scratch;\n")
}
b.printf("} %s%s[%d];\n", cPrefix, o.Name().String(g.tm), maxDepth)
}
}
}
b.printf("} private_impl;\n } %s%s;\n\n", g.pkgPrefix, structName)
return nil
}
func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) error {
structName := n.Name().String(g.tm)
if public {
b.printf("// %s%s__initialize is an initializer function.\n", g.pkgPrefix, structName)
b.printf("//\n")
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("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(")")
return nil
}
func (g *gen) writeInitializerPrototype(b *buffer, n *a.Struct) error {
if !n.Suspendible() {
return nil
}
if err := g.writeInitializerSignature(b, n, n.Public()); err != nil {
return err
}
b.writes(";\n\n")
return nil
}
func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
if !n.Suspendible() {
return nil
}
if err := g.writeInitializerSignature(b, n, false); err != nil {
return err
}
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("return;\n")
b.printf("}\n")
b.writes("if (for_internal_use_only != PUFFS_BASE__ALREADY_ZEROED) {" +
"memset(self, 0, sizeof(*self)); }\n")
b.writes("self->private_impl.magic = PUFFS_BASE__MAGIC;\n")
for _, f := range n.Fields() {
f := f.Field()
if dv := f.DefaultValue(); dv != nil {
// TODO: set default values for array types.
b.printf("self->private_impl.%s%s = %d;\n", fPrefix, f.Name().String(g.tm), dv.ConstValue())
}
}
// Call any ctors on sub-structs.
for _, f := range n.Fields() {
f := f.Field()
x := f.XType()
if x != x.Innermost() {
// TODO: arrays of sub-structs.
continue
}
if g.structMap[x.Name()] == nil {
continue
}
b.printf("%s%s__initialize(&self->private_impl.%s%s,"+
"PUFFS_VERSION, PUFFS_BASE__ALREADY_ZEROED);\n",
g.pkgPrefix, x.Name().String(g.tm), fPrefix, f.Name().String(g.tm))
}
b.writes("}\n\n")
return nil
}
+55
View File
@@ -0,0 +1,55 @@
// Code generated by running "go generate". DO NOT EDIT.
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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, 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" +
""
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// 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" +
""
type template_args_short_read struct {
PKGPREFIX string
name string
}
func template_short_read(b *buffer, args template_args_short_read) error {
b.printf("short_read_%s:\nif (a_%s.buf && a_%s.buf->closed &&\n!a_%s.private_impl.limit.ptr_to_len) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n}\nstatus = %sSUSPENSION_SHORT_READ;\ngoto suspend;\n",
args.name,
args.name,
args.name,
args.name,
args.PKGPREFIX,
args.PKGPREFIX,
)
return nil
}
+653
View File
@@ -0,0 +1,653 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"fmt"
"strings"
"github.com/google/wuffs/lang/builtin"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
depth++
if rp == replaceCallSuspendibles && n.CallSuspendible() {
if g.currFunk.tempR >= g.currFunk.tempW {
return fmt.Errorf("internal error: temporary variable count out of sync")
}
// TODO: check that this works with nested call-suspendibles:
// "foo?().bar().qux?()(p?(), q?())".
//
// Also be aware of evaluation order in the presence of side effects:
// in "foo(a?(), b!(), c?())", b should be called between a and c.
b.printf("%s%d", tPrefix, g.currFunk.tempR)
g.currFunk.tempR++
return nil
}
if cv := n.ConstValue(); cv != nil {
if !n.MType().IsBool() {
b.writes(cv.String())
} else if cv.Cmp(zero) == 0 {
b.writes("false")
} else if cv.Cmp(one) == 0 {
b.writes("true")
} else {
return fmt.Errorf("%v has type bool but constant value %v is neither 0 or 1", n.String(g.tm), cv)
}
return nil
}
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
if err := g.writeExprOther(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsUnaryOp:
if err := g.writeExprUnaryOp(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsBinaryOp:
if err := g.writeExprBinaryOp(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsAssociativeOp:
if err := g.writeExprAssociativeOp(b, n, rp, pp, depth); err != nil {
return err
}
default:
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExpr", n.ID0().Key())
}
return nil
}
func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
switch n.ID0().Key() {
case 0:
if id1 := n.ID1(); id1.Key() == t.KeyThis {
b.writes("self")
} else {
if n.GlobalIdent() {
b.writes(g.pkgPrefix)
} else {
b.writes(vPrefix)
}
b.writes(id1.String(g.tm))
}
return nil
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
if isThatMethod(g.tm, n, t.KeyLowBits, 1) {
// "x.low_bits(n:etc)" in C is "((x) & ((1 << (n)) - 1))".
x := n.LHS().Expr().LHS().Expr()
b.writes("((")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(") & ((1 << (")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")) - 1))")
return nil
}
if isThatMethod(g.tm, n, t.KeyHighBits, 1) {
// "x.high_bits(n:etc)" in C is "((x) >> (8*sizeof(x) - (n)))".
x := n.LHS().Expr().LHS().Expr()
b.writes("((")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(") >> (")
if sz, err := g.sizeof(x.MType()); err != nil {
return err
} else {
b.printf("%d", 8*sz)
}
b.writes(" - (")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")))")
return nil
}
if isThatMethod(g.tm, n, t.KeyIsError, 0) || isThatMethod(g.tm, n, t.KeyIsOK, 0) ||
isThatMethod(g.tm, n, t.KeyIsSuspension, 0) {
if pp == parenthesesMandatory {
b.writeb('(')
}
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil {
return err
}
switch key := n.LHS().Expr().ID1().Key(); key {
case t.KeyIsError:
b.writes(" < 0")
case t.KeyIsOK:
b.writes(" == 0")
case t.KeyIsSuspension:
b.writes(" > 0")
default:
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther's IsXxx", key)
}
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
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(")
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writeb(',')
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")")
return nil
}
if isInSrc(g.tm, n, t.KeyLimit, 1) {
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)
return nil
}
if isInSrc(g.tm, n, t.KeySinceMark, 0) {
b.printf("((puffs_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)
return nil
}
// TODO: reader1.is_marked, not just writer1.is_marked?
if isInDst(g.tm, n, t.KeyLimit, 1) {
return fmt.Errorf(`TODO: cgen an "in.dst.limit" expression`)
}
if isInDst(g.tm, n, t.KeyMark, 0) {
// TODO: is a private_impl.mark the right representation? What if
// 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)
return nil
}
if isInDst(g.tm, n, t.KeySinceMark, 0) {
// Write .len as either "foo ? bar : baz" or "bar".
//
// TODO: drop the "true" in the "if true", provided that the
// benchmark numbers improve.
len0, len1 := "", ""
if true || !n.BoundsCheckOptimized() {
len0 = aPrefix + "dst.private_impl.mark ?"
len1 = ": 0"
}
b.printf("((puffs_base__slice_u8){ "+
".ptr = %sdst.private_impl.mark, "+
".len = %s (size_t)(%swptr_dst - %sdst.private_impl.mark) %s, })",
aPrefix, len0, bPrefix, aPrefix, len1)
return nil
}
if isInDst(g.tm, n, t.KeyIsMarked, 0) {
if pp == parenthesesMandatory {
b.writeb('(')
}
b.printf("%sdst.private_impl.mark != NULL", aPrefix)
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromReader32, 2) {
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()
if err := g.writeExpr(b, a, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writeb(')')
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromHistory32, 2) {
bco := ""
if n.BoundsCheckOptimized() {
bco = "__bco"
}
b.printf("puffs_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(',')
if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
}
b.writeb(')')
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromSlice32, 2) {
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 {
return err
}
}
b.writeb(')')
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromSlice, 1) {
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
}
b.writeb(')')
return nil
}
if isThatMethod(g.tm, n, t.KeyCopyFromSlice, 1) {
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
}
b.writeb(',')
a := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, a, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")\n")
return nil
}
if isThatMethod(g.tm, n, t.KeyLength, 0) {
if pp == parenthesesMandatory {
b.writeb('(')
}
b.writes("(uint64_t)(")
if err := g.writeExpr(b, n.LHS().Expr().LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(".len)")
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
if isThatMethod(g.tm, n, t.KeyAvailable, 0) {
if pp == parenthesesMandatory {
b.writeb('(')
}
p0, p1 := "", ""
if o := n.LHS().Expr().LHS().Expr(); o != nil {
// TODO: don't hard-code these.
switch o.String(g.tm) {
case "in.dst":
p0 = bPrefix + "wend_dst"
p1 = bPrefix + "wptr_dst"
case "in.src":
p0 = bPrefix + "rend_src"
p1 = bPrefix + "rptr_src"
}
}
if p0 == "" {
return fmt.Errorf(`TODO: cgen a "foo.available" expression`)
}
b.printf("(uint64_t)(%s - %s)", p0, p1)
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
if isThatMethod(g.tm, n, g.tm.ByName("update").Key(), 1) {
// TODO: don't hard-code this.adler.
b.printf("%sadler32__update(&self->private_impl.f_adler, ", g.pkgPrefix)
a := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, a, rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(")\n")
return nil
}
if isThatMethod(g.tm, n, g.tm.ByName("set_literal_width").Key(), 1) {
// TODO: don't hard-code lzw.
b.printf("%slzw_decoder__set_literal_width(&self->private_impl.f_lzw, ", g.pkgPrefix)
a := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, a, rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(")\n")
return nil
}
if isThatMethod(g.tm, n, t.KeyLimit, 1) {
return fmt.Errorf(`TODO: cgen a "foo.limit" expression`)
}
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)")
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){ " +
".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
}
// TODO.
case t.KeyOpenBracket:
// n is an index.
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
if lTyp := n.LHS().Expr().MType(); lTyp.Decorator().Key() == t.KeyColon {
// TODO: don't assume that the slice is a slice of u8.
b.writes(".ptr")
}
b.writeb('[')
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writeb(']')
return nil
case t.KeyColon:
// n is a slice.
lhs := n.LHS().Expr()
mhs := n.MHS().Expr()
rhs := n.RHS().Expr()
switch {
case mhs != nil && rhs == nil:
b.writes("puffs_base__slice_u8__subslice_i(")
case mhs == nil && rhs != nil:
b.writes("puffs_base__slice_u8__subslice_j(")
case mhs != nil && rhs != nil:
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=")
}
if err := g.writeExpr(b, lhs, rp, parenthesesOptional, depth); err != nil {
return err
}
if lhsIsArray {
b.printf(",.len=%v})", lhs.MType().ArrayLength().ConstValue())
}
if mhs != nil {
b.writeb(',')
if err := g.writeExpr(b, mhs, rp, parenthesesOptional, depth); err != nil {
return err
}
}
if rhs != nil {
b.writeb(',')
if err := g.writeExpr(b, rhs, rp, parenthesesOptional, depth); err != nil {
return err
}
}
if mhs != nil || rhs != nil {
b.writeb(')')
}
return nil
case t.KeyDot:
lhs := n.LHS().Expr()
if lhs.ID1().Key() == t.KeyIn {
b.writes(aPrefix)
b.writes(n.ID1().String(g.tm))
return nil
}
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
return err
}
if key := lhs.MType().Decorator().Key(); key == t.KeyPtr || key == t.KeyNptr {
b.writes("->")
} else {
b.writes(".")
}
b.writes("private_impl." + fPrefix)
b.writes(n.ID1().String(g.tm))
return nil
case t.KeyError, t.KeyStatus, t.KeySuspension:
status := g.statusMap[n.ID1()]
if status.name == "" {
msg := builtin.TrimQuotes(n.ID1().String(g.tm))
z := builtin.StatusMap[msg]
if z.Message == "" {
return fmt.Errorf("no status code for %q", msg)
}
status.name = strings.ToUpper(g.cName(z.String()))
}
b.writes(status.name)
return nil
}
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
}
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
b.writes(cOpNames[0xFF&n.ID0().Key()])
return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth)
}
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
op := n.ID0()
if op.Key() == t.KeyXBinaryAs {
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
}
if pp == parenthesesMandatory {
b.writeb('(')
}
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyXBinaryAmpHat need special consideration?
b.writes(cOpNames[0xFF&op.Key()])
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, depth uint32) error {
b.writes("((")
// TODO: watch for passing an array type to writeCTypeName? In C, an array
// type can decay into a pointer.
if err := g.writeCTypeName(b, rhs, "", ""); err != nil {
return err
}
b.writes(")(")
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writes("))")
return nil
}
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
if pp == parenthesesMandatory {
b.writeb('(')
}
opName := cOpNames[0xFF&n.ID0().Key()]
for i, o := range n.Args() {
if i != 0 {
b.writes(opName)
}
if err := g.writeExpr(b, o.Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
}
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
// TODO: fix this, allow slices of all types, not just of u8's. Also allow
// arrays of slices, slices of pointers, etc.
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.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
return nil
}
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
}
// maxNumPointers is an arbitrary implementation restriction.
const maxNumPointers = 16
x := n
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
}
numPointers, innermost := 0, x
for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() {
// 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))
}
numPointers++
continue
}
// TODO: fix this.
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
}
fallback := true
if key := innermost.Name().Key(); key < t.Key(len(cTypeNames)) {
if s := cTypeNames[key]; s != "" {
b.writes(s)
fallback = false
}
}
if fallback {
b.printf("%s%s", g.pkgPrefix, innermost.Name().String(g.tm))
}
for i := 0; i < numPointers; i++ {
b.writeb('*')
}
b.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
x = n
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
b.writeb('[')
b.writes(x.ArrayLength().ConstValue().String())
b.writeb(']')
}
return nil
}
var cTypeNames = [...]string{
t.KeyI8: "int8_t",
t.KeyI16: "int16_t",
t.KeyI32: "int32_t",
t.KeyI64: "int64_t",
t.KeyU8: "uint8_t",
t.KeyU16: "uint16_t",
t.KeyU32: "uint32_t",
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",
}
var cOpNames = [256]string{
t.KeyEq: " = ",
t.KeyPlusEq: " += ",
t.KeyMinusEq: " -= ",
t.KeyStarEq: " *= ",
t.KeySlashEq: " /= ",
t.KeyShiftLEq: " <<= ",
t.KeyShiftREq: " >>= ",
t.KeyAmpEq: " &= ",
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
t.KeyPipeEq: " |= ",
t.KeyHatEq: " ^= ",
t.KeyPercentEq: " %= ",
t.KeyTildePlusEq: " += ",
t.KeyXUnaryPlus: " + ",
t.KeyXUnaryMinus: " - ",
t.KeyXUnaryNot: " ! ",
t.KeyXUnaryRef: " & ",
t.KeyXUnaryDeref: " * ",
t.KeyXBinaryPlus: " + ",
t.KeyXBinaryMinus: " - ",
t.KeyXBinaryStar: " * ",
t.KeyXBinarySlash: " / ",
t.KeyXBinaryShiftL: " << ",
t.KeyXBinaryShiftR: " >> ",
t.KeyXBinaryAmp: " & ",
t.KeyXBinaryAmpHat: " no_such_amp_hat_C_operator ",
t.KeyXBinaryPipe: " | ",
t.KeyXBinaryHat: " ^ ",
t.KeyXBinaryPercent: " % ",
t.KeyXBinaryNotEq: " != ",
t.KeyXBinaryLessThan: " < ",
t.KeyXBinaryLessEq: " <= ",
t.KeyXBinaryEqEq: " == ",
t.KeyXBinaryGreaterEq: " >= ",
t.KeyXBinaryGreaterThan: " > ",
t.KeyXBinaryAnd: " && ",
t.KeyXBinaryOr: " || ",
t.KeyXBinaryAs: " no_such_as_C_operator ",
t.KeyXBinaryTildePlus: " + ",
t.KeyXAssociativePlus: " + ",
t.KeyXAssociativeStar: " * ",
t.KeyXAssociativeAmp: " & ",
t.KeyXAssociativePipe: " | ",
t.KeyXAssociativeHat: " ^ ",
t.KeyXAssociativeAnd: " && ",
t.KeyXAssociativeOr: " || ",
}
+388
View File
@@ -0,0 +1,388 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"fmt"
"math/big"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
type funk struct {
bHeader buffer
bBodyResume buffer
bBody buffer
bBodySuspend buffer
bFooter buffer
astFunc *a.Func
cName string
derivedVars map[t.ID]struct{}
jumpTargets map[a.Loop]uint32
coroSuspPoint uint32
tempW uint32
tempR uint32
public bool
suspendible bool
usesScratch bool
shortReads []string
}
func (k *funk) jumpTarget(n a.Loop) (uint32, error) {
if k.jumpTargets == nil {
k.jumpTargets = map[a.Loop]uint32{}
}
if jt, ok := k.jumpTargets[n]; ok {
return jt, nil
}
jt := uint32(len(k.jumpTargets))
if jt == 1000000 {
return 0, fmt.Errorf("too many jump targets")
}
k.jumpTargets[n] = jt
return jt, nil
}
func (g *gen) funcCName(n *a.Func) string {
if r := n.Receiver(); r != 0 {
return g.pkgPrefix + r.String(g.tm) + "__" + n.Name().String(g.tm)
}
return g.pkgPrefix + n.Name().String(g.tm)
}
func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error {
if !n.Public() {
b.writes("static ")
}
// TODO: write n's return values.
if n.Suspendible() {
b.printf("%sstatus ", g.pkgPrefix)
} else if outFields := n.Out().Fields(); len(outFields) == 0 {
b.writes("void ")
} else if len(outFields) == 1 {
// TODO: does this generate the right C if the XType is an array?
if err := g.writeCTypeName(b, outFields[0].Field().XType(), "", ""); err != nil {
return err
}
} else {
return fmt.Errorf("TODO: multiple return values")
}
b.writes(g.funcCName(n))
b.writeb('(')
comma := false
if r := n.Receiver(); r != 0 {
b.printf("%s%s *self", g.pkgPrefix, r.String(g.tm))
comma = true
}
for _, o := range n.In().Fields() {
if comma {
b.writeb(',')
}
comma = true
o := o.Field()
if err := g.writeCTypeName(b, o.XType(), aPrefix, o.Name().String(g.tm)); err != nil {
return err
}
}
b.printf(")")
return nil
}
func (g *gen) writeFuncPrototype(b *buffer, n *a.Func) error {
if err := g.writeFuncSignature(b, n); err != nil {
return err
}
b.writes(";\n\n")
return nil
}
func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error {
k := g.funks[n.QID()]
if err := g.writeFuncSignature(b, n); err != nil {
return err
}
b.writes("{\n")
b.writex(k.bHeader)
if k.suspendible && k.coroSuspPoint > 0 {
b.writex(k.bBodyResume)
}
b.writex(k.bBody)
if k.suspendible && k.coroSuspPoint > 0 {
b.writex(k.bBodySuspend)
}
b.writex(k.bFooter)
b.writes("}\n\n")
return nil
}
func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error {
g.currFunk = funk{
astFunc: n,
cName: g.funcCName(n),
public: n.Public(),
suspendible: n.Suspendible(),
}
if err := g.writeFuncImplHeader(&g.currFunk.bHeader); err != nil {
return err
}
if err := g.writeFuncImplBodyResume(&g.currFunk.bBodyResume); err != nil {
return err
}
if err := g.writeFuncImplBody(&g.currFunk.bBody); err != nil {
return err
}
if err := g.writeFuncImplBodySuspend(&g.currFunk.bBodySuspend); err != nil {
return err
}
if err := g.writeFuncImplFooter(&g.currFunk.bFooter); err != nil {
return err
}
if g.currFunk.tempW != g.currFunk.tempR {
return fmt.Errorf("internal error: temporary variable count out of sync")
}
g.funks[n.QID()] = g.currFunk
return nil
}
func (g *gen) writeFuncImplHeader(b *buffer) error {
// Check the previous status and the "self" arg.
if g.currFunk.public && g.currFunk.astFunc.Receiver() != 0 {
b.writes("if (!self) {")
if g.currFunk.suspendible {
b.printf("return %sERROR_BAD_RECEIVER;", g.PKGPREFIX)
} else {
b.printf("return;")
}
b.writes("}")
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) {")
if g.currFunk.suspendible {
b.writes("return self->private_impl.status;")
} else {
b.writes("return;")
}
b.writes("}\n")
}
if g.currFunk.suspendible {
b.printf("%sstatus status = %sSTATUS_OK;\n", g.pkgPrefix, g.PKGPREFIX)
}
// For public functions, check (at runtime) the other args for bounds and
// null-ness. For private functions, those checks are done at compile time.
if g.currFunk.public {
if err := g.writeFuncImplArgChecks(b, g.currFunk.astFunc); err != nil {
return err
}
}
b.writes("\n")
// Generate the local variables.
if err := g.writeVars(b, g.currFunk.astFunc.Body(), false, true); err != nil {
return err
}
b.writes("\n")
if g.currFunk.suspendible {
g.findDerivedVars()
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.Field()
if err := g.writeLoadDerivedVar(b, o.Name(), o.XType(), true); err != nil {
return err
}
}
b.writes("\n")
}
return nil
}
func (g *gen) writeFuncImplBodyResume(b *buffer) error {
if g.currFunk.suspendible {
// TODO: don't hard-code [0], and allow recursive coroutines.
b.printf("uint32_t coro_susp_point = self->private_impl.%s%s[0].coro_susp_point;\n",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
b.printf("if (coro_susp_point) {\n")
if err := g.writeResumeSuspend(b, g.currFunk.astFunc.Body(), false); err != nil {
return err
}
b.writes("}\n")
// Generate a coroutine switch similiar to the technique in
// 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")
}
return nil
}
func (g *gen) writeFuncImplBody(b *buffer) error {
for _, o := range g.currFunk.astFunc.Body() {
if err := g.writeStatement(b, o, 0); err != nil {
return err
}
}
return nil
}
func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
if g.currFunk.suspendible {
// We've reached the end of the function body. Reset the coroutine
// suspension point so that the next call to this function starts at
// the top.
b.writes("\ngoto ok;\n") // Avoid the "unused label" warning.
b.writes("ok:\n")
b.printf("self->private_impl.%s%s[0].coro_susp_point = 0;\n",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
b.writes("goto exit; }\n\n") // Close the coroutine switch.
b.writes("goto suspend;\n") // Avoid the "unused label" warning.
b.writes("suspend:\n")
b.printf("self->private_impl.%s%s[0].coro_susp_point = coro_susp_point;\n",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
if err := g.writeResumeSuspend(b, g.currFunk.astFunc.Body(), true); err != nil {
return err
}
b.writes("\n")
}
return nil
}
func (g *gen) writeFuncImplFooter(b *buffer) error {
if g.currFunk.suspendible {
b.writes("exit:")
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.Field()
if err := g.writeSaveDerivedVar(b, o.Name(), o.XType(), true); err != nil {
return err
}
}
b.writes("\n")
if g.currFunk.public {
b.writes("self->private_impl.status = status;\n")
}
b.writes("return status;\n\n")
shortReadsSeen := map[string]struct{}{}
for _, sr := range g.currFunk.shortReads {
if _, ok := shortReadsSeen[sr]; ok {
continue
}
shortReadsSeen[sr] = struct{}{}
if err := template_short_read(b, template_args_short_read{
PKGPREFIX: g.PKGPREFIX,
name: sr,
}); err != nil {
return err
}
}
}
return nil
}
func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error {
checks := []string(nil)
for _, o := range n.In().Fields() {
o := o.Field()
oTyp := o.XType()
if oTyp.Decorator().Key() != t.KeyPtr && !oTyp.IsRefined() {
// TODO: Also check elements, for array-typed arguments.
continue
}
switch {
case oTyp.Decorator().Key() == t.KeyPtr:
checks = append(checks, fmt.Sprintf("!%s%s", aPrefix, o.Name().String(g.tm)))
case oTyp.IsRefined():
bounds := [2]*big.Int{}
for i, bound := range oTyp.Bounds() {
if bound != nil {
if cv := bound.ConstValue(); cv != nil {
bounds[i] = cv
}
}
}
if key := oTyp.Name().Key(); key < t.Key(len(numTypeBounds)) {
ntb := numTypeBounds[key]
for i := 0; i < 2; i++ {
if bounds[i] != nil && ntb[i] != nil && bounds[i].Cmp(ntb[i]) == 0 {
bounds[i] = nil
continue
}
}
}
for i, bound := range bounds {
if bound != nil {
op := '<'
if i != 0 {
op = '>'
}
checks = append(checks, fmt.Sprintf("%s%s %c %s", aPrefix, o.Name().String(g.tm), op, bound))
}
}
}
}
if len(checks) == 0 {
return nil
}
b.writes("if (")
for i, c := range checks {
if i != 0 {
b.writes(" || ")
}
b.writes(c)
}
b.writes(") {")
if g.currFunk.suspendible {
b.printf("status = %sERROR_BAD_ARGUMENT; goto exit;", g.PKGPREFIX)
} else if n.Receiver() != 0 {
b.printf("self->private_impl.status = %sERROR_BAD_ARGUMENT; return;", g.PKGPREFIX)
} else {
b.printf("return;")
}
b.writes("}\n")
return nil
}
var numTypeBounds = [256][2]*big.Int{
t.KeyI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.KeyI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.KeyI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
t.KeyI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)},
t.KeyU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)},
t.KeyU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)},
t.KeyU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)},
t.KeyU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)},
t.KeyUsize: {zero, zero},
t.KeyBool: {zero, one},
}
+254
View File
@@ -0,0 +1,254 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build ignore
package main
// gen.go converts base.* to data.go.
//
// Invoke it via "go generate".
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"sort"
"strings"
)
const columns = 1024
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
out := &bytes.Buffer{}
out.WriteString("// Code generated by running \"go generate\". DO NOT EDIT.\n")
out.WriteString("\n")
out.WriteString("// Copyright 2017 The Puffs Authors.\n")
out.WriteString("//\n")
out.WriteString("// Licensed under the Apache License, Version 2.0 (the \"License\");\n")
out.WriteString("// you may not use this file except in compliance with the License.\n")
out.WriteString("// You may obtain a copy of the License at\n")
out.WriteString("//\n")
out.WriteString("// https://www.apache.org/licenses/LICENSE-2.0\n")
out.WriteString("//\n")
out.WriteString("// Unless required by applicable law or agreed to in writing, software\n")
out.WriteString("// distributed under the License is distributed on an \"AS IS\" BASIS,\n")
out.WriteString("// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n")
out.WriteString("// See the License for the specific language governing permissions and\n")
out.WriteString("// limitations under the License.\n")
out.WriteString("\n")
out.WriteString("package cgen\n")
out.WriteString("\n")
if err := genBase(out); err != nil {
return err
}
if err := genTemplates(out); err != nil {
return err
}
formatted, err := format.Source(out.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile("data.go", formatted, 0644)
}
func genBase(out *bytes.Buffer) error {
files := []struct {
filename, varname string
}{
{"base-header.h", "baseHeader"},
{"base-impl.h", "baseImpl"},
}
for _, f := range files {
in, err := ioutil.ReadFile(f.filename)
if err != nil {
return err
}
const afterEditing = "// After editing this file,"
if !bytes.HasPrefix(in, []byte(afterEditing)) {
return fmt.Errorf("%s's contents do not start with %q", f.filename, afterEditing)
}
if i := bytes.Index(in, []byte("\n\n")); i >= 0 {
in = in[i+2:]
}
fmt.Fprintf(out, "const %s = \"\" +\n", f.varname)
for len(in) > 0 {
s := in
if len(s) > columns {
s = s[:columns]
}
in = in[len(s):]
fmt.Fprintf(out, "%q +\n", s)
}
out.WriteString("\"\"\n\n")
}
return nil
}
type template struct {
name string
args [][2]string
format string
formatArgs []string
}
func genTemplates(out *bytes.Buffer) error {
templates, err := readTemplates()
if err != nil {
return err
}
sort.Slice(templates, func(i, j int) bool {
return templates[i].name < templates[j].name
})
for _, t := range templates {
out.WriteString("\n")
fmt.Fprintf(out, "type template_args_%s struct{\n", t.name)
for _, a := range t.args {
key, val := stripq(a[0]), a[1]
switch val {
case "%s":
fmt.Fprintf(out, "%s string\n", key)
}
}
fmt.Fprintf(out, "}\n\n")
fmt.Fprintf(out, "func template_%s(b *buffer, args template_args_%s) error {\n", t.name, t.name)
fmt.Fprintf(out, "b.printf(%q,\n", t.format)
for _, a := range t.formatArgs {
fmt.Fprintf(out, "args.%s,\n", stripq(a))
}
fmt.Fprintf(out, ")\n")
fmt.Fprintf(out, "return nil\n")
fmt.Fprintf(out, "}\n")
}
return nil
}
func readTemplates() ([]template, error) {
f, err := os.Open("templates.h")
if err != nil {
return nil, err
}
defer f.Close()
templates := []template(nil)
name := ""
args := [][2]string(nil)
argsMap := map[string]string{}
format := []byte(nil)
formatArgs := []string(nil)
r := bufio.NewScanner(f)
for r.Scan() {
s := r.Text()
if len(s) == 0 || strings.HasPrefix(s, "//") {
continue
}
if name == "" {
line := s
const prefix = "template "
if !strings.HasPrefix(s, prefix) {
return nil, fmt.Errorf("bad template line %q", line)
}
s = s[len(prefix):]
if i := strings.IndexByte(s, '('); i < 0 {
return nil, fmt.Errorf("bad template line %q", line)
} else {
name = s[:i]
s = s[i+1:]
}
if i := strings.IndexByte(s, ')'); i < 0 {
return nil, fmt.Errorf("bad template line %q", line)
} else {
s = s[:i]
}
for _, x := range strings.Split(s, ",") {
x = strings.TrimSpace(x)
key, val := "", ""
switch {
case strings.HasPrefix(x, "string "):
key = x[len("string "):]
val = "%s"
}
if key == "" || argsMap[key] != "" {
return nil, fmt.Errorf("bad template line %q", line)
}
args = append(args, [2]string{key, val})
argsMap[key] = val
}
continue
}
if s == "}" {
templates = append(templates, template{
name: name,
args: args,
format: string(format),
formatArgs: formatArgs,
})
name = ""
args = nil
argsMap = map[string]string{}
format = nil
formatArgs = nil
continue
}
s = strings.TrimSpace(s)
if len(s) == 0 || strings.HasPrefix(s, "//") {
continue
}
s = strings.Replace(s, "%", "%%", -1)
for _, a := range args {
key, val := a[0], a[1]
for {
i := strings.Index(s, key)
if i < 0 {
break
}
s = s[:i] + val + s[i+len(key):]
formatArgs = append(formatArgs, key)
}
}
format = append(format, s...)
format = append(format, '\n')
}
if err := r.Err(); err != nil {
return nil, err
}
return templates, nil
}
func stripq(s string) string {
if len(s) >= 2 && s[0] == 'q' && s[len(s)-1] == 'q' {
return s[1 : len(s)-1]
}
return s
}
+833
View File
@@ -0,0 +1,833 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"errors"
"fmt"
"strings"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
// genFilenameLineComments is whether to print "// foo.puffs: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.
const genFilenameLineComments = false
func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
if depth > a.MaxBodyDepth {
return fmt.Errorf("body recursion depth too large")
}
depth++
if n.Kind() == a.KAssert {
// Assertions only apply at compile-time.
return nil
}
mightIntroduceTemporaries := false
switch n.Kind() {
case a.KAssign:
n := n.Assign()
mightIntroduceTemporaries = n.LHS().Suspendible() || n.RHS().Suspendible()
case a.KVar:
v := n.Var().Value()
mightIntroduceTemporaries = v != nil && v.Suspendible()
}
if mightIntroduceTemporaries {
// Put n's code into its own block, to restrict the scope of the
// tPrefix temporary variables. This helps avoid "jump bypasses
// variable initialization" compiler warnings with the coroutine
// suspension points.
b.writes("{\n")
defer b.writes("}\n")
}
if genFilenameLineComments {
filename, line := n.Raw().FilenameLine()
if i := strings.LastIndexByte(filename, '/'); i >= 0 {
filename = filename[i+1:]
}
if i := strings.LastIndexByte(filename, '\\'); i >= 0 {
filename = filename[i+1:]
}
b.printf("// %s:%d\n", filename, line)
}
switch n.Kind() {
case a.KAssign:
n := n.Assign()
if err := g.writeSuspendibles(b, n.LHS(), depth); err != nil {
return err
}
if err := g.writeSuspendibles(b, n.RHS(), depth); err != nil {
return err
}
if err := g.writeExpr(b, n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyAmpHatEq need special consideration?
b.writes(cOpNames[0xFF&n.Operator().Key()])
if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(";\n")
return nil
case a.KExpr:
n := n.Expr()
if err := g.writeSuspendibles(b, n, depth); err != nil {
return err
}
if n.CallSuspendible() {
return nil
}
if err := g.writeExpr(b, n, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(";\n")
return nil
case a.KIf:
// TODO: for writeSuspendibles, make sure that we get order of
// sub-expression evaluation correct.
n, nCloseCurly := n.If(), 1
for first := true; ; first = false {
if n.Condition().Suspendible() {
if !first {
b.writeb('{')
const maxCloseCurly = 1000
if nCloseCurly == maxCloseCurly {
return fmt.Errorf("too many nested if's")
}
nCloseCurly++
}
if err := g.writeSuspendibles(b, n.Condition(), depth); err != nil {
return err
}
}
b.writes("if (")
if err := g.writeExpr(b, n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
b.writes(") {\n")
for _, o := range n.BodyIfTrue() {
if err := g.writeStatement(b, o, depth); err != nil {
return err
}
}
if bif := n.BodyIfFalse(); len(bif) > 0 {
b.writes("} else {\n")
for _, o := range bif {
if err := g.writeStatement(b, o, depth); err != nil {
return err
}
}
break
}
n = n.ElseIf()
if n == nil {
break
}
b.writes("} else ")
}
for ; nCloseCurly > 0; nCloseCurly-- {
b.writes("}\n")
}
return nil
case a.KIterate:
n := n.Iterate()
vars := n.Variables()
if len(vars) == 0 {
return nil
}
if len(vars) != 1 {
return fmt.Errorf("TODO: iterate over more than one variable")
}
v := vars[0].Var()
name := v.Name().String(g.tm)
b.writes("{\n")
// 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)
if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
b.writes(";\n")
b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name)
// TODO: look at n.HasContinue() and n.HasBreak().
unrollCount := int(n.UnrollCount().ConstValue().Int64())
if unrollCount != 1 {
b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n",
iPrefix, name, iPrefix, name, iPrefix, name, unrollCount, unrollCount)
b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name)
for i := 0; i < unrollCount; i++ {
for _, o := range n.Body() {
if err := g.writeStatement(b, o, depth); err != nil {
return err
}
}
b.printf("%s%s++;\n", vPrefix, name)
}
b.writes("}\n")
}
b.printf("uint8_t* %send1_%s = %sslice_%s.ptr + %sslice_%s.len;\n",
iPrefix, name, iPrefix, name, iPrefix, name)
b.printf("while (%s%s < %send1_%s) {\n", vPrefix, name, iPrefix, name)
for _, o := range n.Body() {
if err := g.writeStatement(b, o, depth); err != nil {
return err
}
}
b.printf("%s%s++;\n", vPrefix, name)
b.writes("}\n")
b.writes("}\n")
return nil
case a.KJump:
n := n.Jump()
jt, err := g.currFunk.jumpTarget(n.JumpTarget())
if err != nil {
return err
}
keyword := "continue"
if n.Keyword().Key() == t.KeyBreak {
keyword = "break"
}
b.printf("goto label_%d_%s;\n", jt, keyword)
return nil
case a.KReturn:
n := n.Return()
retExpr := n.Value()
if g.currFunk.suspendible {
b.writes("status = ")
retKeyword := t.KeyStatus
if retExpr == nil {
b.printf("%s%s", g.PKGPREFIX, "STATUS_OK")
} else {
retKeyword = retExpr.ID0().Key()
// TODO: check that retExpr has no call-suspendibles.
if err := g.writeExpr(
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
}
b.writes(";")
switch retKeyword {
case t.KeyError:
b.writes("goto exit;")
return nil
case t.KeyStatus:
b.writes("goto ok;")
return nil
}
return g.writeCoroSuspPoint(b, true)
}
b.writes("return ")
if len(g.currFunk.astFunc.Out().Fields()) == 0 {
if retExpr != nil {
return fmt.Errorf("return expression %q incompatible with empty return type", retExpr.String(g.tm))
}
} else if retExpr == nil {
// TODO: should a bare "return" imply "return out"?
return fmt.Errorf("empty return expression incompatible with non-empty return type")
} else if err := g.writeExpr(b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writeb(';')
return nil
case a.KVar:
n := n.Var()
if v := n.Value(); v != nil {
if err := g.writeSuspendibles(b, v, depth); err != nil {
return err
}
}
switch n.XType().Decorator().Key() {
case t.KeyOpenBracket:
if n.Value() != nil {
// TODO: something like:
// cv := n.XType().ArrayLength().ConstValue()
// // TODO: check that cv is within size_t's range.
// g.printf("{ size_t i; for (i = 0; i < %d; i++) { %s%s[i] = $DEFAULT_VALUE; }}\n",
// cv, vPrefix, n.Name().String(g.tm))
return fmt.Errorf("TODO: array initializers for non-zero default values")
}
// TODO: arrays of arrays.
name := n.Name().String(g.tm)
b.printf("memset(%s%s, 0, sizeof(%s%s));\n", vPrefix, name, vPrefix, name)
default:
b.printf("%s%s = ", vPrefix, n.Name().String(g.tm))
if v := n.Value(); v != nil {
if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil {
return err
}
} 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){})")
} else {
b.writeb('0')
}
b.writes(";\n")
}
return nil
case a.KWhile:
n := n.While()
// TODO: consider suspendible calls.
if n.HasContinue() {
jt, err := g.currFunk.jumpTarget(n)
if err != nil {
return err
}
b.printf("label_%d_continue:;\n", jt)
}
b.writes("while (")
if err := g.writeExpr(b, n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
b.writes(") {\n")
for _, o := range n.Body() {
if err := g.writeStatement(b, o, depth); err != nil {
return err
}
}
b.writes("}\n")
if n.HasBreak() {
jt, err := g.currFunk.jumpTarget(n)
if err != nil {
return err
}
b.printf("label_%d_break:;\n", jt)
}
return nil
}
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
}
func (g *gen) writeCoroSuspPoint(b *buffer, maybeSuspend bool) error {
const maxCoroSuspPoint = 0xFFFFFFFF
g.currFunk.coroSuspPoint++
if g.currFunk.coroSuspPoint == maxCoroSuspPoint {
return fmt.Errorf("too many coroutine suspension points required")
}
macro := ""
if maybeSuspend {
macro = "_MAYBE_SUSPEND"
}
b.printf("PUFFS_BASE__COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
return nil
}
func (g *gen) writeSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if !n.Suspendible() {
return nil
}
err := g.mightActuallySuspend(n, depth)
if err != nil && err != errMightActuallySuspend {
return err
}
mightActuallySuspend := err != nil
if mightActuallySuspend {
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
}
return g.writeCallSuspendibles(b, n, depth)
}
// errMightActuallySuspend is the absence of ProvenNotToSuspend.
//
// TODO: find better, less clumsy names for this concept.
var errMightActuallySuspend = errors.New("internal: might actually suspend")
// TODO: this would be simpler with a call keyword and an explicit "foo = call
// bar?()" syntax.
func (g *gen) mightActuallySuspend(n *a.Expr, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
depth++
// The evaluation order for suspendible calls (which can have side effects)
// is important here: LHS, MHS, RHS, Args and finally the node itself.
if !n.CallSuspendible() {
for _, o := range n.Node().Raw().SubNodes() {
if o != nil && o.Kind() == a.KExpr {
if err := g.mightActuallySuspend(o.Expr(), depth); err != nil {
return err
}
}
}
for _, o := range n.Args() {
if o != nil && o.Kind() == a.KExpr {
if err := g.mightActuallySuspend(o.Expr(), depth); err != nil {
return err
}
}
}
return nil
}
if n.ProvenNotToSuspend() {
return nil
}
return errMightActuallySuspend
}
func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
depth++
// The evaluation order for suspendible calls (which can have side effects)
// is important here: LHS, MHS, RHS, Args and finally the node itself.
if !n.CallSuspendible() {
for _, o := range n.Node().Raw().SubNodes() {
if o != nil && o.Kind() == a.KExpr {
if err := g.writeCallSuspendibles(b, o.Expr(), depth); err != nil {
return err
}
}
}
for _, o := range n.Args() {
if o != nil && o.Kind() == a.KExpr {
if err := g.writeCallSuspendibles(b, o.Expr(), depth); err != nil {
return err
}
}
}
return nil
}
if err := g.writeSaveExprDerivedVars(b, n); err != nil {
return err
}
// TODO: delete these hacks that only matches "in.src.read_u8?()" etc.
//
// TODO: check reader1.buf and writer1.buf is non-NULL.
if isInSrc(g.tm, n, t.KeyReadU8, 0) {
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
if !n.ProvenNotToSuspend() {
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.
if err := g.writeCTypeName(b, n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
return err
}
b.printf(" = *%srptr_src++;\n", bPrefix)
} else if isInSrc(g.tm, n, t.KeyUnreadU8, 0) {
b.printf("if (%srptr_src == %srstart_src) { status = %sERROR_INVALID_I_O_OPERATION;",
bPrefix, bPrefix, g.PKGPREFIX)
b.writes("goto exit;")
b.writes("}\n")
b.printf("%srptr_src--;\n", bPrefix)
} else if isInSrc(g.tm, n, t.KeyReadU16BE, 0) {
return g.writeReadUXX(b, n, "src", 16, "be")
} else if isInSrc(g.tm, n, t.KeyReadU16LE, 0) {
return g.writeReadUXX(b, n, "src", 16, "le")
} else if isInSrc(g.tm, n, t.KeyReadU32BE, 0) {
return g.writeReadUXX(b, n, "src", 32, "be")
} else if isInSrc(g.tm, n, t.KeyReadU32LE, 0) {
return g.writeReadUXX(b, n, "src", 32, "le")
} else if isInSrc(g.tm, n, t.KeySkip32, 1) {
g.currFunk.usesScratch = true
// TODO: don't hard-code [0], and allow recursive coroutines.
scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch",
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
b.printf("%s = ", scratchName)
x := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(";\n")
// TODO: the CSP prior to this is probably unnecessary.
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
b.printf("if (%s > %srend_src - %srptr_src) {\n", scratchName, bPrefix, bPrefix)
b.printf("%s -= %srend_src - %srptr_src;\n", scratchName, bPrefix, bPrefix)
b.printf("%srptr_src = %srend_src;\n", bPrefix, bPrefix)
b.writes("goto short_read_src; }\n")
g.currFunk.shortReads = append(g.currFunk.shortReads, "src")
b.printf("%srptr_src += %s;\n", bPrefix, scratchName)
} else if isInDst(g.tm, n, t.KeyWriteU8, 1) {
if !n.ProvenNotToSuspend() {
b.printf("if (%swptr_dst == %swend_dst) { status = %sSUSPENSION_SHORT_WRITE;",
bPrefix, bPrefix, g.PKGPREFIX)
b.writes("goto suspend;")
b.writes("}\n")
}
b.printf("*%swptr_dst++ = ", bPrefix)
x := n.Args()[0].Arg().Value()
if err := g.writeExpr(b, x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(";\n")
} else if isThisMethod(g.tm, n, "decode_header", 1) {
b.printf("status = %s%s__decode_header(self, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
// TODO: should we goto exit/suspend depending on if status < or > 0?
// Ditto below.
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_lsd", 1) {
b.printf("status = %s%s__decode_lsd(self, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_extension", 1) {
b.printf("status = %s%s__decode_extension(self, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_id", 2) {
b.printf("status = %s%s__decode_id(self, %sdst, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_blocks", 2) {
// TODO: don't hard code being inside a try call.
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("%sstatus %s%d = %s%s__decode_blocks(self, %sdst, %ssrc);\n",
g.pkgPrefix, tPrefix, temp,
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
// TODO: check if tPrefix_temp is an error, and return?
} else if isThisMethod(g.tm, n, "decode_uncompressed", 2) {
b.printf("status = %s%s__decode_uncompressed(self, %sdst, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_huffman_fast", 2) {
b.printf("status = %s%s__decode_huffman_fast(self, %sdst, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_huffman_slow", 2) {
b.printf("status = %s%s__decode_huffman_slow(self, %sdst, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "init_fixed_huffman", 0) {
b.printf("status = %s%s__init_fixed_huffman(self);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm))
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "init_dynamic_huffman", 1) {
b.printf("status = %s%s__init_dynamic_huffman(self, %ssrc);\n",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "init_huff", 4) {
b.printf("status = %s%s__init_huff(self,",
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm))
for i, o := range n.Args() {
if i != 0 {
b.writes(",")
}
if err := g.writeExpr(b, o.Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
return err
}
}
b.writes(");\n")
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThatMethod(g.tm, n, g.tm.ByName("decode").Key(), 2) ||
isThatMethod(g.tm, n, g.tm.ByName("decode").Key(), 3) {
switch g.pkgName {
case "flate":
// TODO: don't hard code being inside a try call.
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
// TODO: don't hard-code a_dst or a_src.
b.printf("%sstatus %s%d = %sflate_decoder__decode(&self->private_impl.f_flate, %sdst, %ssrc);\n",
g.pkgPrefix, tPrefix, temp,
g.pkgPrefix, aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
// TODO: check if tPrefix_temp is an error, and return?
case "gif":
// TODO: don't hard code being inside a try call.
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
// 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",
g.pkgPrefix, tPrefix, temp,
g.pkgPrefix, aPrefix, vPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
// 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))
}
} 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 nil
}
func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endianness string) error {
if size != 16 && size != 32 {
return fmt.Errorf("internal error: bad writeReadUXX size %d", size)
}
if endianness != "be" && endianness != "le" {
return fmt.Errorf("internal error: bad writeReadUXX endianness %q", endianness)
}
// TODO: look at n.ProvenNotToSuspend().
if g.currFunk.tempW > maxTemp-1 {
return fmt.Errorf("too many temporary variables required")
}
// temp0 is read by code generated in this function. temp1 is read elsewhere.
temp0 := g.currFunk.tempW + 0
temp1 := g.currFunk.tempW + 1
g.currFunk.tempW += 2
g.currFunk.tempR += 1
if err := g.writeCTypeName(b, n.MType(), tPrefix, fmt.Sprint(temp1)); err != nil {
return err
}
b.writes(";")
g.currFunk.usesScratch = true
// TODO: don't hard-code [0], and allow recursive coroutines.
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("%srptr_src += %d;\n", bPrefix, size/8)
b.printf("} else {")
b.printf("%s = 0;\n", scratchName)
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
b.printf("while (true) {")
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)
b.printf("uint32_t %s%d = %s", tPrefix, temp0, scratchName)
switch endianness {
case "be":
b.printf("& 0xFF;")
b.printf("%s >>= 8;", scratchName)
b.printf("%s <<= 8;", scratchName)
b.printf("%s |= ((uint64_t)(*%srptr_%s++)) << (64 - %s%d);",
scratchName, bPrefix, name, tPrefix, temp0)
case "le":
b.printf(">> 56;")
b.printf("%s <<= 8;", scratchName)
b.printf("%s >>= 8;", scratchName)
b.printf("%s |= ((uint64_t)(*%srptr_%s++)) << %s%d;",
scratchName, bPrefix, name, tPrefix, temp0)
}
b.printf("if (%s%d == %d) {", tPrefix, temp0, size-8)
switch endianness {
case "be":
b.printf("%s%d = %s >> (64 - %d);", tPrefix, temp1, scratchName, size)
case "le":
b.printf("%s%d = %s;", tPrefix, temp1, scratchName)
}
b.printf("break;")
b.printf("}")
b.printf("%s%d += 8;", tPrefix, temp0)
switch endianness {
case "be":
b.printf("%s |= ((uint64_t)(%s%d));", scratchName, tPrefix, temp0)
case "le":
b.printf("%s |= ((uint64_t)(%s%d)) << 56;", scratchName, tPrefix, temp0)
}
b.writes("}}\n")
return nil
}
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
callSuspendible := methodName != t.KeySinceMark &&
methodName != t.KeyMark &&
methodName != t.KeyLimit
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("src") {
return false
}
n = n.LHS().Expr()
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
}
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
callSuspendible := methodName != t.KeyCopyFromReader32 &&
methodName != t.KeyCopyFromHistory32 &&
methodName != t.KeyCopyFromSlice32 &&
methodName != t.KeyCopyFromSlice &&
methodName != t.KeySinceMark &&
methodName != t.KeyIsMarked &&
methodName != t.KeyMark &&
methodName != t.KeyLimit
// TODO: check that n.Args() is "(x:bar)".
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("dst") {
return false
}
n = n.LHS().Expr()
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
}
func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
// TODO: check that n.Args() is "(src:in.src)".
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return false
}
if len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName(methodName) {
return false
}
n = n.LHS().Expr()
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
}
// isThatMethod is like isThisMethod but for foo.bar(etc), not this.bar(etc).
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return false
}
if len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
}
+33
View File
@@ -0,0 +1,33 @@
// After editing this file, run "go generate" in this directory.
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file contains C-like code. It is close enough to C so that clang-format
// can format it well. But it is not C code per se. It contains templates that
// the code generator expands via a custom mechanism.
//
// This file has a .h extension, not a .c extension, so that Go tools such as
// "go generate" don't try to build it as part of the Go package, via cgo.
template short_read(string qPKGPREFIXq, string qnameq) {
short_read_qnameq:
if (a_qnameq.buf && a_qnameq.buf->closed &&
!a_qnameq.private_impl.limit.ptr_to_len) {
status = qPKGPREFIXqERROR_UNEXPECTED_EOF;
goto exit;
}
status = qPKGPREFIXqSUSPENSION_SHORT_READ;
goto suspend;
}
+368
View File
@@ -0,0 +1,368 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"errors"
"fmt"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
var errNeedDerivedVar = errors.New("internal: need derived var")
func (g *gen) needDerivedVar(name t.ID) bool {
for _, o := range g.currFunk.astFunc.Body() {
err := o.Walk(func(p *a.Node) error {
// Look for p matching "in.name.etc(etc)".
if p.Kind() != a.KExpr {
return nil
}
q := p.Expr()
if q.ID0().Key() != t.KeyOpenParen {
return nil
}
q = q.LHS().Expr()
if q.ID0().Key() != t.KeyDot {
return nil
}
q = q.LHS().Expr()
if q.ID0().Key() != t.KeyDot || q.ID1() != name {
return nil
}
q = q.LHS().Expr()
if q.ID0() != 0 || q.ID1().Key() != t.KeyIn {
return nil
}
return errNeedDerivedVar
})
if err == errNeedDerivedVar {
return true
}
}
return false
}
func (g *gen) findDerivedVars() {
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.Field()
oTyp := o.XType()
if oTyp.Decorator() != 0 {
continue
}
if key := oTyp.Name().Key(); key != t.KeyReader1 && key != t.KeyWriter1 {
continue
}
if !g.needDerivedVar(o.Name()) {
continue
}
if g.currFunk.derivedVars == nil {
g.currFunk.derivedVars = map[t.ID]struct{}{}
}
g.currFunk.derivedVars[o.Name()] = struct{}{}
}
}
func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:in.src".
if name.String(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.String(g.tm)
switch typ.Name().Key() {
case t.KeyReader1:
if header {
b.printf("uint8_t* %srptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("%srptr_%s = %s%s.buf->ptr + %s%s.buf->ri;",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
if header {
b.printf("%srstart_%s = %srptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
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("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")
b.printf("}\n")
b.printf("}\n")
b.printf("%srend_%s = %srptr_%s + len;", bPrefix, nameStr, bPrefix, nameStr)
}
b.printf("}\n")
case t.KeyWriter1:
if header {
b.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("%swptr_%s = %s%s.buf->ptr + %s%s.buf->wi;",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
if header {
b.printf("%swstart_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
b.printf("%swend_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
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("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")
b.printf("}\n")
b.printf("}\n")
b.printf("%swend_%s += len;", bPrefix, nameStr)
b.printf("}\n")
}
b.printf("}\n")
}
return nil
}
func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:in.src".
if name.String(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.String(g.tm)
switch typ.Name().Key() {
case t.KeyReader1:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
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;\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")
b.printf("}\n")
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("}\n")
case t.KeyWriter1:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
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;\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")
b.printf("}\n")
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("}\n")
}
return nil
}
func (g *gen) writeLoadExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return nil
}
for _, o := range n.Args() {
o := o.Arg()
// TODO: don't hard-code these.
if s := o.Value().String(g.tm); s != "in.dst" && s != "in.src" {
continue
}
if err := g.writeLoadDerivedVar(b, o.Name(), o.Value().MType(), false); err != nil {
return err
}
}
return nil
}
func (g *gen) writeSaveExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return nil
}
for _, o := range n.Args() {
o := o.Arg()
// TODO: don't hard-code these.
if s := o.Value().String(g.tm); s != "in.dst" && s != "in.src" {
continue
}
if err := g.writeSaveDerivedVar(b, o.Name(), o.Value().MType(), false); err != nil {
return err
}
}
return nil
}
func (g *gen) visitVars(b *buffer, block []*a.Node, depth uint32, f func(*gen, *buffer, *a.Var) error) error {
if depth > a.MaxBodyDepth {
return fmt.Errorf("body recursion depth too large")
}
depth++
for _, o := range block {
switch o.Kind() {
case a.KIf:
for o := o.If(); o != nil; o = o.ElseIf() {
if err := g.visitVars(b, o.BodyIfTrue(), depth, f); err != nil {
return err
}
if err := g.visitVars(b, o.BodyIfFalse(), depth, f); err != nil {
return err
}
}
case a.KVar:
if err := f(g, b, o.Var()); err != nil {
return err
}
case a.KIterate:
if err := g.visitVars(b, o.Iterate().Variables(), depth, f); err != nil {
return err
}
if err := g.visitVars(b, o.Iterate().Body(), depth, f); err != nil {
return err
}
case a.KWhile:
if err := g.visitVars(b, o.While().Body(), depth, f); err != nil {
return err
}
}
}
return nil
}
func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool) error {
local := fmt.Sprintf("%s%s", vPrefix, n.Name().String(g.tm))
if typ := n.XType(); typ.HasPointers() {
if suspend {
return nil
}
rhs := ""
switch typ.Decorator().Key() {
case 0:
if key := typ.Name().Key(); key < t.Key(len(cTypeNames)) {
rhs = cTypeNames[key]
}
case t.KeyColon:
// TODO: don't assume that the slice is a slice of u8.
rhs = "puffs_base__slice_u8"
}
if rhs != "" {
b.printf("%s = ((%s){});\n", local, rhs)
return nil
}
} else {
lhs := local
// TODO: don't hard-code [0], and allow recursive coroutines.
rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.Name().String(g.tm), lhs)
if suspend {
lhs, rhs = rhs, lhs
}
switch typ.Decorator().Key() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.KeyOpenBracket:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
switch inner.Name().Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64:
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
return nil
}
}
}
return fmt.Errorf("cannot resume or suspend a local variable %q of type %q",
n.Name().String(g.tm), n.XType().String(g.tm))
}
func (g *gen) writeResumeSuspend(b *buffer, block []*a.Node, suspend bool) error {
return g.visitVars(b, block, 0, func(g *gen, b *buffer, n *a.Var) error {
return g.writeResumeSuspend1(b, n, suspend)
})
}
func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool, skipIterateVariables bool) error {
return g.visitVars(b, block, 0, func(g *gen, b *buffer, n *a.Var) error {
if skipPointerTypes && n.XType().HasPointers() {
return nil
}
if skipIterateVariables && n.IterateVariable() {
return nil
}
if err := g.writeCTypeName(b, n.XType(), vPrefix, n.Name().String(g.tm)); err != nil {
return err
}
b.writes(";\n")
return nil
})
}
+48
View File
@@ -0,0 +1,48 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// wuffs-c handles the C language specific parts of the wuffs tool.
package main
import (
"fmt"
"os"
"github.com/google/wuffs/cmd/wuffs-c/internal/cgen"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
if len(os.Args) < 2 {
return fmt.Errorf("no sub-command given")
}
args := os.Args[2:]
switch os.Args[1] {
case "bench":
return doBench(args)
case "gen":
return cgen.Do(args)
case "genlib":
return doGenlib(args)
case "test":
return doTest(args)
}
return fmt.Errorf("bad sub-command %q", os.Args[1])
}
+152
View File
@@ -0,0 +1,152 @@
// Copyright 2017 The Puffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
cf "github.com/google/wuffs/cmd/commonflags"
)
func doBench(args []string) error { return doBenchTest(args, true) }
func doTest(args []string) error { return doBenchTest(args, false) }
func doBenchTest(args []string, bench bool) error {
flags := flag.FlagSet{}
ccompilersFlag := flags.String("ccompilers", cf.CcompilersDefault, cf.CcompilersUsage)
focusFlag := flags.String("focus", cf.FocusDefault, cf.FocusUsage)
mimicFlag := flags.Bool("mimic", cf.MimicDefault, cf.MimicUsage)
repsFlag := flags.Int("reps", cf.RepsDefault, cf.RepsUsage)
if err := flags.Parse(args); err != nil {
return err
}
if !cf.IsAlphaNumericIsh(*ccompilersFlag) {
return fmt.Errorf("bad -ccompilers flag value %q", *ccompilersFlag)
}
if !cf.IsAlphaNumericIsh(*focusFlag) {
return fmt.Errorf("bad -focus flag value %q", *focusFlag)
}
if *repsFlag < cf.RepsMin || cf.RepsMax < *repsFlag {
return fmt.Errorf("bad -reps flag value %d, outside the range [%d..%d]", *repsFlag, cf.RepsMin, cf.RepsMax)
}
args = flags.Args()
failed := false
for _, arg := range args {
f, err := doBenchTest1(arg, bench, *ccompilersFlag, *focusFlag, *mimicFlag, *repsFlag)
if err != nil {
return err
}
failed = failed || f
}
if failed {
s := "tests"
if bench {
s = "benchmarks"
}
return fmt.Errorf("%s: some %s failed", os.Args[0], s)
}
return nil
}
func doBenchTest1(filename string, bench bool, ccompilers string, focus string, mimic bool, reps int) (failed bool, err error) {
workDir, err := ioutil.TempDir("", "wuffs-c")
if err != nil {
return false, err
}
defer os.RemoveAll(workDir)
in := filename + ".c"
out := filepath.Join(workDir, "a.out")
ccArgs := []string(nil)
if bench {
ccArgs = append(ccArgs, "-O3")
} else {
// TODO: set these flags even if we pass -O3.
ccArgs = append(ccArgs, "-Wall", "-Werror")
}
ccArgs = append(ccArgs, "-std=c99", "-o", out, in)
if mimic {
extra, err := findPuffsMimicCflags(in)
if err != nil {
return false, err
}
ccArgs = append(ccArgs, extra...)
}
for _, cc := range strings.Split(ccompilers, ",") {
cc = strings.TrimSpace(cc)
if cc == "" {
continue
}
ccCmd := exec.Command(cc, ccArgs...)
ccCmd.Stdout = os.Stdout
ccCmd.Stderr = os.Stderr
if err := ccCmd.Run(); err != nil {
return false, err
}
outArgs := []string(nil)
if bench {
outArgs = append(outArgs, "-bench", fmt.Sprintf("-reps=%d", reps))
}
if focus != "" {
outArgs = append(outArgs, fmt.Sprintf("-focus=%s", focus))
}
outCmd := exec.Command(out, outArgs...)
outCmd.Stdout = os.Stdout
outCmd.Stderr = os.Stderr
outCmd.Dir = filepath.Dir(filename)
if err := outCmd.Run(); err == nil {
// No-op.
} else if _, ok := err.(*exec.ExitError); ok {
failed = true
} else {
return false, err
}
}
return failed, nil
}
func findPuffsMimicCflags(filename string) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
t := s.Text()
const prefix = "// !! puffs mimic cflags:"
if strings.HasPrefix(t, prefix) {
t = strings.TrimSpace(t[len(prefix):])
return strings.Split(t, " "), nil
}
}
return nil, s.Err()
}