mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Generate C function arguments.
Also rename "io.buf1" to just "buf1".
This commit is contained in:
1 parent
ee27dffa9e
commit
4c0a659a18
9 files changed
+106
-29
No files matched your search
@@ -29,4 +29,15 @@
|
||||
// TODO: don't hard code this in base-header.h.
|
||||
#define PUFFS_VERSION (0x00001)
|
||||
|
||||
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and capacity) plus
|
||||
// additional indexes into that buffer.
|
||||
//
|
||||
// A value with all fields NULL or zero is a valid, empty buffer.
|
||||
typedef struct {
|
||||
uint8_t* ptr; // Pointer.
|
||||
size_t cap; // Capacity.
|
||||
size_t wi; // Write index. Invariant: wi <= cap.
|
||||
size_t ri; // Read index. Invariant: ri <= wi.
|
||||
} puffs_base_buf1;
|
||||
|
||||
#endif // PUFFS_BASE_HEADER_H
|
||||
@@ -7,7 +7,7 @@ package main
|
||||
|
||||
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 <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#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" +
|
||||
"\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and capacity) plus\n// additional indexes into that buffer.\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 cap; // Capacity.\n size_t wi; // Write index. Invariant: wi <= cap.\n size_t ri; // Read index. Invariant: ri <= wi.\n} puffs_base_buf1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
||||
""
|
||||
|
||||
const baseImpl = "" +
|
||||
|
||||
+43
-13
@@ -204,9 +204,10 @@ func (g *gen) writeStruct(n *a.Struct) error {
|
||||
g.printf("uint32_t magic;\n")
|
||||
}
|
||||
for _, f := range n.Fields() {
|
||||
if err := g.writeField(f.Field()); err != nil {
|
||||
if err := g.writeField(f.Field(), "f_"); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(";\n")
|
||||
}
|
||||
g.printf("} puffs_%s_%s;\n\n", g.pkgName, structName)
|
||||
return nil
|
||||
@@ -286,7 +287,8 @@ func (g *gen) writeCtorImpls(n *a.Struct) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeFuncSignature(n *a.Func) {
|
||||
func (g *gen) writeFuncSignature(n *a.Func) error {
|
||||
// TODO: write n's return values.
|
||||
if n.Suspendible() {
|
||||
g.printf("puffs_%s_status", g.pkgName)
|
||||
} else {
|
||||
@@ -297,22 +299,39 @@ func (g *gen) writeFuncSignature(n *a.Func) {
|
||||
g.printf("_%s", r.String(g.idMap))
|
||||
}
|
||||
g.printf("_%s(", n.Name().String(g.idMap))
|
||||
|
||||
comma := false
|
||||
if r := n.Receiver(); r != 0 {
|
||||
g.printf("puffs_%s_%s *self", g.pkgName, r.String(g.idMap))
|
||||
comma = true
|
||||
}
|
||||
// TODO: write n's args.
|
||||
for _, o := range n.In().Fields() {
|
||||
if comma {
|
||||
g.writeb(',')
|
||||
}
|
||||
comma = true
|
||||
if err := g.writeField(o.Field(), "a_"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
g.printf(")")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeFuncPrototype(n *a.Func) error {
|
||||
g.writeFuncSignature(n)
|
||||
if err := g.writeFuncSignature(n); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(";\n\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||
g.jumpTargets = nil
|
||||
g.writeFuncSignature(n)
|
||||
if err := g.writeFuncSignature(n); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes("{\n")
|
||||
|
||||
cleanup0 := false
|
||||
@@ -371,16 +390,23 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeField(n *a.Field) error {
|
||||
convertible := true
|
||||
func (g *gen) writeField(n *a.Field, namePrefix string) error {
|
||||
const maxNPtr = 16
|
||||
|
||||
convertible, nPtr := true, 0
|
||||
for x := n.XType(); x != nil; x = x.Inner() {
|
||||
if p := x.PackageOrDecorator(); p != 0 && p != t.IDOpenBracket {
|
||||
if p := x.PackageOrDecorator().Key(); p == t.KeyPtr {
|
||||
if nPtr == maxNPtr {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.XType().String(g.idMap))
|
||||
}
|
||||
nPtr++
|
||||
continue
|
||||
} else if p == t.KeyOpenBracket {
|
||||
continue
|
||||
} else if p != 0 {
|
||||
convertible = false
|
||||
break
|
||||
}
|
||||
if x.Inner() != nil {
|
||||
continue
|
||||
}
|
||||
if k := x.Name().Key(); k < t.Key(len(cTypeNames)) {
|
||||
if s := cTypeNames[k]; s != "" {
|
||||
g.writes(s)
|
||||
@@ -396,7 +422,10 @@ func (g *gen) writeField(n *a.Field) error {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.XType().String(g.idMap))
|
||||
}
|
||||
|
||||
g.writes("f_")
|
||||
for i := 0; i < nPtr; i++ {
|
||||
g.writeb('*')
|
||||
}
|
||||
g.writes(namePrefix)
|
||||
g.writes(n.Name().String(g.idMap))
|
||||
|
||||
for x := n.XType(); x != nil; x = x.Inner() {
|
||||
@@ -407,7 +436,6 @@ func (g *gen) writeField(n *a.Field) error {
|
||||
}
|
||||
}
|
||||
|
||||
g.writes(";\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -640,6 +668,8 @@ var cTypeNames = [...]string{
|
||||
t.KeyU64: "uint64_t",
|
||||
t.KeyUsize: "size_t",
|
||||
t.KeyBool: "bool",
|
||||
t.KeyBuf1: "puffs_base_buf1",
|
||||
t.KeyBuf2: "puffs_base_buf2",
|
||||
}
|
||||
|
||||
var cOpNames = [256]string{
|
||||
|
||||
+19
-2
@@ -32,6 +32,17 @@
|
||||
// TODO: don't hard code this in base-header.h.
|
||||
#define PUFFS_VERSION (0x00001)
|
||||
|
||||
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and capacity) plus
|
||||
// additional indexes into that buffer.
|
||||
//
|
||||
// A value with all fields NULL or zero is a valid, empty buffer.
|
||||
typedef struct {
|
||||
uint8_t* ptr; // Pointer.
|
||||
size_t cap; // Capacity.
|
||||
size_t wi; // Write index. Invariant: wi <= cap.
|
||||
size_t ri; // Read index. Invariant: ri <= wi.
|
||||
} puffs_base_buf1;
|
||||
|
||||
#endif // PUFFS_BASE_HEADER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -78,7 +89,10 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
|
||||
|
||||
// ---------------- Public Function Prototypes
|
||||
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self);
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
puffs_base_buf1* a_dst,
|
||||
puffs_base_buf1* a_src,
|
||||
bool a_src_final);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
@@ -142,7 +156,10 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {
|
||||
|
||||
// ---------------- Function Implementations
|
||||
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
puffs_base_buf1* a_dst,
|
||||
puffs_base_buf1* a_src,
|
||||
bool a_src_final) {
|
||||
if (!self) {
|
||||
return puffs_gif_error_null_receiver;
|
||||
}
|
||||
|
||||
+15
-1
@@ -32,6 +32,17 @@
|
||||
// TODO: don't hard code this in base-header.h.
|
||||
#define PUFFS_VERSION (0x00001)
|
||||
|
||||
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and capacity) plus
|
||||
// additional indexes into that buffer.
|
||||
//
|
||||
// A value with all fields NULL or zero is a valid, empty buffer.
|
||||
typedef struct {
|
||||
uint8_t* ptr; // Pointer.
|
||||
size_t cap; // Capacity.
|
||||
size_t wi; // Write index. Invariant: wi <= cap.
|
||||
size_t ri; // Read index. Invariant: ri <= wi.
|
||||
} puffs_base_buf1;
|
||||
|
||||
#endif // PUFFS_BASE_HEADER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -78,7 +89,10 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
|
||||
|
||||
// ---------------- Public Function Prototypes
|
||||
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self);
|
||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
puffs_base_buf1* a_dst,
|
||||
puffs_base_buf1* a_src,
|
||||
bool a_src_final);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
||||
+4
-6
@@ -539,7 +539,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
|
||||
|
||||
switch n.PackageOrDecorator().Key() {
|
||||
case 0:
|
||||
if name := n.Name(); name.IsNumType() || name.Key() == t.KeyBool {
|
||||
if n.Name().IsNumType() {
|
||||
for _, b := range n.Bounds() {
|
||||
if b == nil {
|
||||
continue
|
||||
@@ -556,6 +556,9 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
|
||||
if n.Min() != nil || n.Max() != nil {
|
||||
// TODO: reject.
|
||||
}
|
||||
if name := n.Name().Key(); name == t.KeyBool || name == t.KeyBuf1 {
|
||||
break
|
||||
}
|
||||
// TODO: see if name refers to a struct type.
|
||||
return fmt.Errorf("check: %q is not a type", n.Name().String(q.idMap))
|
||||
|
||||
@@ -577,11 +580,6 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
|
||||
}
|
||||
|
||||
default:
|
||||
// TODO: delete this hack.
|
||||
if n.PackageOrDecorator() == q.idMap.ByName("io") && n.Name() == q.idMap.ByName("buf1") {
|
||||
break
|
||||
}
|
||||
|
||||
return fmt.Errorf("check: unrecognized node for checkTypeExpr")
|
||||
}
|
||||
n.Node().SetTypeChecked()
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use "std/io"
|
||||
|
||||
pub struct lzw_decoder?(
|
||||
literal_width u32[2..8] = 8,
|
||||
stack[4096] u8,
|
||||
@@ -7,7 +5,7 @@ pub struct lzw_decoder?(
|
||||
prefixes[4096] u16,
|
||||
)
|
||||
|
||||
pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() {
|
||||
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
||||
var bits u32
|
||||
var n_bits u32
|
||||
|
||||
|
||||
+8
-2
@@ -19,7 +19,10 @@ const char* test_filename = "gif/basic.c";
|
||||
|
||||
void test_constructor_not_called() {
|
||||
puffs_gif_lzw_decoder dec;
|
||||
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec);
|
||||
puffs_base_buf1 dst = {0};
|
||||
puffs_base_buf1 src = {0};
|
||||
puffs_gif_status status =
|
||||
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
|
||||
if (status != puffs_gif_error_constructor_not_called) {
|
||||
FAIL("test_null_receiver: status: got %d, want %d", status,
|
||||
puffs_gif_error_constructor_not_called);
|
||||
@@ -27,7 +30,10 @@ void test_constructor_not_called() {
|
||||
}
|
||||
|
||||
void test_null_receiver() {
|
||||
puffs_gif_status status = puffs_gif_lzw_decoder_decode(NULL);
|
||||
puffs_base_buf1 dst = {0};
|
||||
puffs_base_buf1 src = {0};
|
||||
puffs_gif_status status =
|
||||
puffs_gif_lzw_decoder_decode(NULL, &dst, &src, false);
|
||||
if (status != puffs_gif_error_null_receiver) {
|
||||
FAIL("test_null_receiver: status: got %d, want %d", status,
|
||||
puffs_gif_error_null_receiver);
|
||||
|
||||
+4
-1
@@ -20,7 +20,10 @@ const char* test_filename = "gif/lzw.c";
|
||||
void test_lzw_decode() {
|
||||
puffs_gif_lzw_decoder dec;
|
||||
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec);
|
||||
puffs_base_buf1 dst = {0};
|
||||
puffs_base_buf1 src = {0};
|
||||
puffs_gif_status status =
|
||||
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
|
||||
if (status != puffs_gif_status_ok) {
|
||||
FAIL("test_lzw_decode: status: got %d, want %d", status,
|
||||
puffs_gif_status_ok);
|
||||
|
||||
Reference in new issue
Block a user