mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 00:47:28 +01:00
Add a "limit" keyword.
This commit is contained in:
1 parent
1d39b1e173
commit
7a3ddbc025
14 files changed
+204
-29
No files matched your search
@@ -46,14 +46,19 @@ typedef struct {
|
||||
bool closed; // No further writes are expected.
|
||||
} puffs_base_buf1;
|
||||
|
||||
typedef struct puffs_base_limit {
|
||||
uint64_t* len;
|
||||
struct puffs_base_limit* next;
|
||||
} puffs_base_limit;
|
||||
|
||||
typedef struct {
|
||||
puffs_base_buf1* buf;
|
||||
// TODO: limits.
|
||||
puffs_base_limit limit;
|
||||
} puffs_base_reader1;
|
||||
|
||||
typedef struct {
|
||||
puffs_base_buf1* buf;
|
||||
// TODO: limits.
|
||||
puffs_base_limit limit;
|
||||
} puffs_base_writer1;
|
||||
|
||||
#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//\n// Similarly, 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_buf1 is a 1-dimensional buffer (a pointer and length) 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 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\ntypedef struct {\n puffs_base_buf1* buf;\n // TODO: limits.\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n // TODO: limits.\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
||||
"/ 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_buf1 is a 1-dimensional buffer (a pointer and length) 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 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\ntypedef struct puffs_base_limit {\n uint64_t* len;\n struct puffs_base_limit* next;\n} puffs_base_limit;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
||||
""
|
||||
|
||||
const baseImpl = "" +
|
||||
|
||||
+46
-6
@@ -35,6 +35,7 @@ var (
|
||||
const (
|
||||
aPrefix = "a_" // Function argument.
|
||||
fPrefix = "f_" // Struct field.
|
||||
lPrefix = "l_" // Limit length.
|
||||
tPrefix = "t_" // Temporary local variable.
|
||||
vPrefix = "v_" // Local variable.
|
||||
)
|
||||
@@ -125,12 +126,13 @@ type status struct {
|
||||
}
|
||||
|
||||
type perFunc struct {
|
||||
funk *a.Func
|
||||
jumpTargets map[*a.While]uint32
|
||||
tempW uint32
|
||||
tempR uint32
|
||||
public bool
|
||||
suspendible bool
|
||||
funk *a.Func
|
||||
jumpTargets map[*a.While]uint32
|
||||
tempW uint32
|
||||
tempR uint32
|
||||
public bool
|
||||
suspendible bool
|
||||
limitVarName string
|
||||
}
|
||||
|
||||
type gen struct {
|
||||
@@ -721,6 +723,9 @@ func (g *gen) writeVars(block []*a.Node, depth uint32) error {
|
||||
|
||||
case a.KVar:
|
||||
o := o.Var()
|
||||
if v := o.Value(); v != nil && v.ID0().Key() == t.KeyLimit {
|
||||
g.printf("uint64_t %s%v;\n", lPrefix, o.Name().String(g.tm))
|
||||
}
|
||||
if err := g.writeCTypeName(o.XType(), vPrefix, o.Name().String(g.tm)); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -873,6 +878,15 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
||||
if err := g.writeSuspendibles(v, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if v.ID0().Key() == t.KeyLimit {
|
||||
g.perFunc.limitVarName = n.Name().String(g.tm)
|
||||
g.printf("%s%s =", lPrefix, g.perFunc.limitVarName)
|
||||
if err := g.writeExpr(
|
||||
v.LHS().Expr(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(";\n")
|
||||
}
|
||||
}
|
||||
if n.XType().Decorator().Key() == t.KeyOpenBracket {
|
||||
if n.Value() != nil {
|
||||
@@ -1084,6 +1098,12 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
||||
g.pkgName, g.perFunc.funk.Receiver().String(g.tm), aPrefix, aPrefix)
|
||||
g.writes("if (status) { goto cleanup0; }\n")
|
||||
|
||||
} else if isDecode(g.tm, n) {
|
||||
g.printf("status = puffs_%s_lzw_decoder_decode(&self->private_impl.f_lzw, %sdst, %s%s);\n",
|
||||
g.pkgName, aPrefix, vPrefix, n.Args()[1].Arg().Value().String(g.tm))
|
||||
g.writes("if (status) { return status; }\n")
|
||||
return nil
|
||||
|
||||
} else {
|
||||
// TODO: fix this.
|
||||
//
|
||||
@@ -1212,6 +1232,17 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
||||
g.writes(fPrefix)
|
||||
g.writes(n.ID1().String(g.tm))
|
||||
return nil
|
||||
|
||||
case t.KeyLimit:
|
||||
// TODO: don't hard code so much detail.
|
||||
g.writes("(puffs_base_reader1) {")
|
||||
g.writes(".buf = a_src.buf,")
|
||||
g.writes(".limit = (puffs_base_limit) {")
|
||||
g.printf(".len = &%s%s,", lPrefix, g.perFunc.limitVarName)
|
||||
g.writes(".next = &a_src.limit,")
|
||||
g.writes("}}")
|
||||
g.perFunc.limitVarName = ""
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
|
||||
}
|
||||
@@ -1280,6 +1311,15 @@ func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width")
|
||||
}
|
||||
|
||||
func isDecode(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(dst:bar, src:baz)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 2 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
|
||||
}
|
||||
|
||||
func (g *gen) writeExprUnaryOp(n *a.Expr, rp replacementPolicy, depth uint32) error {
|
||||
// TODO.
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user