mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
Add cgen templates.
This commit is contained in:
1 parent
38bfebf204
commit
f3970d6670
4 files changed
+207
-23
No files matched your search
@@ -15,3 +15,20 @@ const baseImpl = "" +
|
|||||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that c" +
|
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that c" +
|
||||||
"lang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
"lang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||||
""
|
""
|
||||||
|
|
||||||
|
type template_args_short_read struct {
|
||||||
|
PKGPREFIX string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func template_short_read(g *gen, args template_args_short_read) error {
|
||||||
|
g.printf("short_read_%s:\nif (a_%s.limit.ptr_to_len) {\nstatus = %sSUSPENSION_LIMITED_READ;\n} else if (a_%s.buf->closed) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n} else {\nstatus = %sSUSPENSION_SHORT_READ;\n}\ngoto suspend;\n",
|
||||||
|
args.name,
|
||||||
|
args.name,
|
||||||
|
args.PKGPREFIX,
|
||||||
|
args.name,
|
||||||
|
args.PKGPREFIX,
|
||||||
|
args.PKGPREFIX,
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -191,7 +191,10 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
shortReadsSeen[sr] = struct{}{}
|
shortReadsSeen[sr] = struct{}{}
|
||||||
if err := g.writeShortRead(sr); err != nil {
|
if err := template_short_read(g, template_args_short_read{
|
||||||
|
PKGPREFIX: g.PKGPREFIX,
|
||||||
|
name: sr,
|
||||||
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1094,19 +1097,6 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeShortRead(name string) error {
|
|
||||||
g.printf("\nshort_read_%s:\n", name)
|
|
||||||
// TODO: is ptr_to_len the right check?
|
|
||||||
g.printf("if (%s%s.limit.ptr_to_len) {", aPrefix, name)
|
|
||||||
g.printf("status = %sSUSPENSION_LIMITED_READ;", g.PKGPREFIX)
|
|
||||||
g.printf("} else if (%s%s.buf->closed) {", aPrefix, name)
|
|
||||||
g.printf("status = %sERROR_UNEXPECTED_EOF;", g.PKGPREFIX)
|
|
||||||
g.writes("goto exit;")
|
|
||||||
g.printf("} else {status = %sSUSPENSION_SHORT_READ; }", g.PKGPREFIX)
|
|
||||||
g.writes("goto suspend;\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *gen) writeReadUXX(n *a.Expr, name string, size uint32, endianness string) error {
|
func (g *gen) writeReadUXX(n *a.Expr, name string, size uint32, endianness string) error {
|
||||||
if size != 16 && size != 32 {
|
if size != 16 && size != 32 {
|
||||||
return fmt.Errorf("internal error: bad writeReadUXX size %d", size)
|
return fmt.Errorf("internal error: bad writeReadUXX size %d", size)
|
||||||
|
|||||||
@@ -10,11 +10,14 @@ package main
|
|||||||
// Invoke it via "go generate".
|
// Invoke it via "go generate".
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const columns = 1024
|
const columns = 1024
|
||||||
@@ -27,13 +30,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main1() error {
|
func main1() error {
|
||||||
files := []struct {
|
|
||||||
filename, varname string
|
|
||||||
}{
|
|
||||||
{"base-header.h", "baseHeader"},
|
|
||||||
{"base-impl.h", "baseImpl"},
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
out.WriteString("// Code generated by running \"go generate\". DO NOT EDIT.\n")
|
out.WriteString("// Code generated by running \"go generate\". DO NOT EDIT.\n")
|
||||||
out.WriteString("\n")
|
out.WriteString("\n")
|
||||||
@@ -43,6 +39,28 @@ func main1() error {
|
|||||||
out.WriteString("package cgen\n")
|
out.WriteString("package cgen\n")
|
||||||
out.WriteString("\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 {
|
for _, f := range files {
|
||||||
in, err := ioutil.ReadFile(f.filename)
|
in, err := ioutil.ReadFile(f.filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -68,10 +86,147 @@ func main1() error {
|
|||||||
}
|
}
|
||||||
out.WriteString("\"\"\n\n")
|
out.WriteString("\"\"\n\n")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
formatted, err := format.Source(out.Bytes())
|
type template struct {
|
||||||
|
name string
|
||||||
|
args [][2]string
|
||||||
|
format string
|
||||||
|
formatArgs []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func genTemplates(out *bytes.Buffer) error {
|
||||||
|
templates, err := readTemplates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return ioutil.WriteFile("data.go", formatted, 0644)
|
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(g *gen, args template_args_%s) error {\n", t.name, t.name)
|
||||||
|
fmt.Fprintf(out, "g.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
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// After editing this file, run "go generate" in this directory.
|
||||||
|
|
||||||
|
// 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:
|
||||||
|
// TODO: is ptr_to_len the right check?
|
||||||
|
if (a_qnameq.limit.ptr_to_len) {
|
||||||
|
status = qPKGPREFIXqSUSPENSION_LIMITED_READ;
|
||||||
|
} else if (a_qnameq.buf->closed) {
|
||||||
|
status = qPKGPREFIXqERROR_UNEXPECTED_EOF;
|
||||||
|
goto exit;
|
||||||
|
} else {
|
||||||
|
status = qPKGPREFIXqSUSPENSION_SHORT_READ;
|
||||||
|
}
|
||||||
|
goto suspend;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user