Add reader1 and writer1 base types.

This commit is contained in:
Nigel Tao committed 2017-06-18 11:50:34 +10:00
1 parent d16a25806f
commit 17e47fbc1e
11 files changed
+169 -138

No files matched your search

+10
View File
@@ -46,4 +46,14 @@ typedef struct {
bool closed; // No further writes are expected.
} puffs_base_buf1;
typedef struct {
puffs_base_buf1* buf;
// TODO: limits.
} puffs_base_reader1;
typedef struct {
puffs_base_buf1* buf;
// TODO: limits.
} puffs_base_writer1;
#endif // PUFFS_BASE_HEADER_H
+1 -1
View File
@@ -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\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 {\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" +
""
const baseImpl = "" +
+26 -21
View File
@@ -954,6 +954,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
}
// TODO: delete these hacks that only matches "in.src.read_u8?()" etc.
//
// TODO: check reader1.buf and writer1.buf is non-NULL.
if isInSrcReadU8(g.tm, n) {
if g.perFunc.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
@@ -962,8 +964,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
g.perFunc.tempW++
// TODO: suspend coroutine state.
g.printf("if (%ssrc->ri >= %ssrc->wi) { status = "+
"%ssrc->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
g.printf("if (%ssrc.buf->ri >= %ssrc.buf->wi) { status = "+
"%ssrc.buf->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, aPrefix, aPrefix, g.pkgName, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
@@ -976,20 +978,21 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
if err := g.writeCTypeName(n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
return err
}
g.printf(" = %ssrc->ptr[%ssrc->ri++];\n", aPrefix, aPrefix)
g.printf(" = %ssrc.buf->ptr[%ssrc.buf->ri++];\n", aPrefix, aPrefix)
} else if isInDst(g.tm, n, t.KeyWrite) {
// TODO: suspend coroutine state.
//
// TODO: don't assume that the argument is "this.stack[s:]".
g.printf("if (%sdst->closed) { status = puffs_%s_error_closed_for_writes;", aPrefix, g.pkgName)
g.printf("if (%sdst.buf->closed) { status = puffs_%s_error_closed_for_writes;", aPrefix, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
} else {
g.writes("return status;")
}
g.writes("}\n")
g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {", aPrefix, aPrefix)
g.printf("if ((%sdst.buf->len - %sdst.buf->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {",
aPrefix, aPrefix)
g.printf("status = puffs_%s_status_short_write;", g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
@@ -998,14 +1001,14 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
}
g.writes("}\n")
g.printf("memmove(" +
"a_dst->ptr + a_dst->wi," +
"a_dst.buf->ptr + a_dst.buf->wi," +
"self->private_impl.f_stack + v_s," +
"sizeof(self->private_impl.f_stack) - v_s);\n")
g.printf("a_dst->wi += sizeof(self->private_impl.f_stack) - v_s;\n")
g.printf("a_dst.buf->wi += sizeof(self->private_impl.f_stack) - v_s;\n")
} else if isInDst(g.tm, n, t.KeyWriteU8) {
// TODO: suspend coroutine state.
g.printf("if (%sdst->wi >= %sdst->len) { status = puffs_%s_status_short_write;",
g.printf("if (%sdst.buf->wi >= %sdst.buf->len) { status = puffs_%s_status_short_write;",
aPrefix, aPrefix, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
@@ -1013,7 +1016,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
g.writes("return status;")
}
g.writes("}\n")
g.printf("%sdst->ptr[%sdst->wi++] = ", aPrefix, aPrefix)
g.printf("%sdst.buf->ptr[%sdst.buf->wi++] = ", aPrefix, aPrefix)
x := n.Args()[0].Arg().Value()
if err := g.writeExpr(x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
@@ -1339,18 +1342,20 @@ var numTypeBounds = [256][2]*big.Int{
}
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.KeyBuf2: "puffs_base_buf2",
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.KeyBuf2: "puffs_base_buf2",
t.KeyReader1: "puffs_base_reader1",
t.KeyWriter1: "puffs_base_writer1",
}
var cOpNames = [256]string{