From 930ff53ff63da3fd928bb04dcfc2fcb34d9f0824 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 1 Jun 2017 11:02:43 +1000 Subject: [PATCH] Add cmd/puffs-gen-c/base-impl.h. Also rename preamble.h to base-header.h. --- cmd/puffs-gen-c/{preamble.h => base-header.h} | 8 +-- cmd/puffs-gen-c/base-impl.h | 8 +++ cmd/puffs-gen-c/data.go | 10 ++-- cmd/puffs-gen-c/gen.go | 51 +++++++++++-------- cmd/puffs-gen-c/main.go | 5 +- gen/c/gif.c | 15 ++++-- gen/h/gif.h | 8 +-- 7 files changed, 67 insertions(+), 38 deletions(-) rename cmd/puffs-gen-c/{preamble.h => base-header.h} (87%) create mode 100644 cmd/puffs-gen-c/base-impl.h diff --git a/cmd/puffs-gen-c/preamble.h b/cmd/puffs-gen-c/base-header.h similarity index 87% rename from cmd/puffs-gen-c/preamble.h rename to cmd/puffs-gen-c/base-header.h index 72e23b4b..8ca9e936 100644 --- a/cmd/puffs-gen-c/preamble.h +++ b/cmd/puffs-gen-c/base-header.h @@ -1,7 +1,7 @@ // After editing this file, run "go generate" in this directory. -#ifndef PUFFS_BASE_H -#define PUFFS_BASE_H +#ifndef PUFFS_BASE_HEADER_H +#define PUFFS_BASE_HEADER_H // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. @@ -26,7 +26,7 @@ // // For now, the API and ABI are simply unstable and can change at any time. // -// TODO: don't hard code this in preamble.h. +// TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -#endif // PUFFS_BASE_H +#endif // PUFFS_BASE_HEADER_H diff --git a/cmd/puffs-gen-c/base-impl.h b/cmd/puffs-gen-c/base-impl.h new file mode 100644 index 00000000..dee0b8c3 --- /dev/null +++ b/cmd/puffs-gen-c/base-impl.h @@ -0,0 +1,8 @@ +// After editing this file, run "go generate" in this directory. + +#ifndef PUFFS_BASE_IMPL_H +#define PUFFS_BASE_IMPL_H + +#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) + +#endif // PUFFS_BASE_IMPL_H diff --git a/cmd/puffs-gen-c/data.go b/cmd/puffs-gen-c/data.go index 8f0bc529..6d10e97f 100644 --- a/cmd/puffs-gen-c/data.go +++ b/cmd/puffs-gen-c/data.go @@ -5,7 +5,11 @@ package main -const preamble = "" + - "#ifndef PUFFS_BASE_H\n#define PUFFS_BASE_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#include \n#include \n#include \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#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 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 preamble.h.\n#define PUFFS_VERSION (0x00001)\n\n#endif // PUFF" + - "S_BASE_H\n" + +const baseHeader = "" + + "#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_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#include \n#include \n#include \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#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 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#endif // PUFFS_BASE_HEADER_H\n" + + "" + +const baseImpl = "" + + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" diff --git a/cmd/puffs-gen-c/gen.go b/cmd/puffs-gen-c/gen.go index f4490f8a..1898cc2a 100644 --- a/cmd/puffs-gen-c/gen.go +++ b/cmd/puffs-gen-c/gen.go @@ -5,7 +5,7 @@ package main -// gen.go converts preamble.h to data.go. +// gen.go converts base.* to data.go. // // Invoke it via "go generate". @@ -27,18 +27,11 @@ func main() { } func main1() error { - const inFilename = "preamble.h" - in, err := ioutil.ReadFile(inFilename) - 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", inFilename, afterEditing) - } - if i := bytes.Index(in, []byte("\n\n")); i >= 0 { - in = in[i+2:] + files := []struct { + filename, varname string + }{ + {"base-header.h", "baseHeader"}, + {"base-impl.h", "baseImpl"}, } out := &bytes.Buffer{} @@ -49,16 +42,32 @@ func main1() error { out.WriteString("\n") out.WriteString("package main\n") out.WriteString("\n") - out.WriteString("const preamble = \"\" +\n") - for len(in) > 0 { - s := in - if len(s) > columns { - s = s[:columns] + + for _, f := range files { + in, err := ioutil.ReadFile(f.filename) + if err != nil { + return err } - in = in[len(s):] - fmt.Fprintf(out, "%q +\n", s) + + 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") } - out.WriteString("\"\"\n") formatted, err := format.Source(out.Bytes()) if err != nil { diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 4e53f674..c923ec16 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -45,9 +45,8 @@ func (gen) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) includeGuard := "PUFFS_" + strings.ToUpper(pkgName) + "_H" fmt.Fprintf(b, "#ifndef %s\n#define %s\n\n", includeGuard, includeGuard) - // Write preamble. fmt.Fprintf(b, "// Code generated by puffs-gen-c. DO NOT EDIT.\n\n") - b.WriteString(preamble) + b.WriteString(baseHeader) b.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n") b.WriteString("// ---------------- Status Codes\n\n") @@ -82,6 +81,8 @@ func (gen) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n") fmt.Fprintf(b, "#endif // %s\n\n", includeGuard) b.WriteString("// C HEADER ENDS HERE.\n\n") + b.WriteString(baseImpl) + b.WriteString("\n") b.WriteString("// ---------------- Private Structs\n\n") if err := forEachStruct(b, pkgName, m, files, priOnly, writeStruct); err != nil { diff --git a/gen/c/gif.c b/gen/c/gif.c index 2aa94372..2360933f 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -3,8 +3,8 @@ // Code generated by puffs-gen-c. DO NOT EDIT. -#ifndef PUFFS_BASE_H -#define PUFFS_BASE_H +#ifndef PUFFS_BASE_HEADER_H +#define PUFFS_BASE_HEADER_H // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. @@ -29,10 +29,10 @@ // // For now, the API and ABI are simply unstable and can change at any time. // -// TODO: don't hard code this in preamble.h. +// TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -#endif // PUFFS_BASE_H +#endif // PUFFS_BASE_HEADER_H #ifdef __cplusplus extern "C" { @@ -88,6 +88,13 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self); // C HEADER ENDS HERE. +#ifndef PUFFS_BASE_IMPL_H +#define PUFFS_BASE_IMPL_H + +#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) + +#endif // PUFFS_BASE_IMPL_H + // ---------------- Private Structs // ---------------- Private Constructor and Destructor Prototypes diff --git a/gen/h/gif.h b/gen/h/gif.h index f1953fb3..e5e0e777 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -3,8 +3,8 @@ // Code generated by puffs-gen-c. DO NOT EDIT. -#ifndef PUFFS_BASE_H -#define PUFFS_BASE_H +#ifndef PUFFS_BASE_HEADER_H +#define PUFFS_BASE_HEADER_H // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. @@ -29,10 +29,10 @@ // // For now, the API and ABI are simply unstable and can change at any time. // -// TODO: don't hard code this in preamble.h. +// TODO: don't hard code this in base-header.h. #define PUFFS_VERSION (0x00001) -#endif // PUFFS_BASE_H +#endif // PUFFS_BASE_HEADER_H #ifdef __cplusplus extern "C" {