diff --git a/cmd/puffs-gen-c/base-header.h b/cmd/puffs-gen-c/base-header.h index 18b2c446..cb68bdf5 100644 --- a/cmd/puffs-gen-c/base-header.h +++ b/cmd/puffs-gen-c/base-header.h @@ -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 diff --git a/cmd/puffs-gen-c/data.go b/cmd/puffs-gen-c/data.go index 587b0672..e8b6b595 100644 --- a/cmd/puffs-gen-c/data.go +++ b/cmd/puffs-gen-c/data.go @@ -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 \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//\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 = "" + diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index ba6860b6..65a0001d 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -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{ diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 3bd531fb..250c6584 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -49,6 +49,16 @@ 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 #ifdef __cplusplus @@ -145,15 +155,15 @@ void puffs_gif_decoder_destructor(puffs_gif_decoder* self); // ---------------- Public Function Prototypes puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, - puffs_base_buf1* a_dst, - puffs_base_buf1* a_src); + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src); void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, uint32_t a_lw); puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, - puffs_base_buf1* a_dst, - puffs_base_buf1* a_src); + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src); #ifdef __cplusplus } // extern "C" @@ -210,10 +220,10 @@ const char* puffs_gif_status_string(puffs_gif_status s) { // ---------------- Private Function Prototypes puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, - puffs_base_buf1* a_src); + puffs_base_reader1 a_src); puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, - puffs_base_buf1* a_src); + puffs_base_reader1 a_src); // ---------------- Constructor and Destructor Implementations @@ -282,8 +292,8 @@ void puffs_gif_decoder_destructor(puffs_gif_decoder* self) { // ---------------- Function Implementations puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, - puffs_base_buf1* a_dst, - puffs_base_buf1* a_src) { + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src) { if (!self) { return puffs_gif_error_bad_receiver; } @@ -295,10 +305,6 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, status = puffs_gif_error_constructor_not_called; goto cleanup0; } - if (!a_dst || !a_src) { - status = puffs_gif_error_bad_argument; - goto cleanup0; - } status = puffs_gif_decoder_decode_header(self, a_src); if (status) { @@ -315,7 +321,7 @@ cleanup0: } puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, - puffs_base_buf1* a_src) { + puffs_base_reader1 a_src) { puffs_gif_status status = self->private_impl.status; uint8_t v_c[6]; @@ -326,12 +332,12 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, }; v_i = 0; while (v_i < 6) { - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; return status; } - uint8_t t_0 = a_src->ptr[a_src->ri++]; + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; v_c[v_i] = t_0; v_i += 1; } @@ -344,7 +350,7 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, } puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, - puffs_base_buf1* a_src) { + puffs_base_reader1 a_src) { puffs_gif_status status = self->private_impl.status; uint8_t v_c[7]; @@ -356,12 +362,12 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, }; v_i = 0; while (v_i < 7) { - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; return status; } - uint8_t t_0 = a_src->ptr[a_src->ri++]; + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; v_c[v_i] = t_0; v_i += 1; } @@ -374,26 +380,26 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7))); v_i = 0; while (v_i < v_gct_size) { - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; return status; } - uint8_t t_1 = a_src->ptr[a_src->ri++]; + uint8_t t_1 = a_src.buf->ptr[a_src.buf->ri++]; self->private_impl.f_gct[(3 * v_i) + 0] = t_1; - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; return status; } - uint8_t t_2 = a_src->ptr[a_src->ri++]; + uint8_t t_2 = a_src.buf->ptr[a_src.buf->ri++]; self->private_impl.f_gct[(3 * v_i) + 1] = t_2; - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; return status; } - uint8_t t_3 = a_src->ptr[a_src->ri++]; + uint8_t t_3 = a_src.buf->ptr[a_src.buf->ri++]; self->private_impl.f_gct[(3 * v_i) + 2] = t_3; v_i += 1; } @@ -423,8 +429,8 @@ void puffs_gif_lzw_decoder_set_literal_width(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) { + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src) { if (!self) { return puffs_gif_error_bad_receiver; } @@ -436,10 +442,6 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, status = puffs_gif_error_constructor_not_called; goto cleanup0; } - if (!a_dst || !a_src) { - status = puffs_gif_error_bad_argument; - goto cleanup0; - } uint32_t v_clear_code; uint32_t v_end_code; @@ -464,12 +466,12 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, label_0_continue:; while (true) { while (v_n_bits < v_width) { - if (a_src->ri >= a_src->wi) { - status = a_src->closed ? puffs_gif_error_unexpected_eof - : puffs_gif_status_short_read; + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; goto cleanup0; } - uint8_t t_0 = a_src->ptr[a_src->ri++]; + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; v_bits |= (((uint32_t)(t_0)) << v_n_bits); v_n_bits += 8; } @@ -477,11 +479,11 @@ label_0_continue:; v_bits >>= v_width; v_n_bits -= v_width; if (v_code < v_clear_code) { - if (a_dst->wi >= a_dst->len) { + if (a_dst.buf->wi >= a_dst.buf->len) { status = puffs_gif_status_short_write; goto cleanup0; } - a_dst->ptr[a_dst->wi++] = ((uint8_t)(v_code)); + a_dst.buf->ptr[a_dst.buf->wi++] = ((uint8_t)(v_code)); if (v_use_save_code) { self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_code)); self->private_impl.f_prefixes[v_save_code] = ((uint16_t)(v_prev_code)); @@ -515,18 +517,18 @@ label_0_continue:; if ((v_code == v_save_code) && v_use_save_code) { self->private_impl.f_stack[4095] = ((uint8_t)(v_c)); } - if (a_dst->closed) { + if (a_dst.buf->closed) { status = puffs_gif_error_closed_for_writes; goto cleanup0; } - if ((a_dst->len - a_dst->wi) < + if ((a_dst.buf->len - a_dst.buf->wi) < (sizeof(self->private_impl.f_stack) - v_s)) { status = puffs_gif_status_short_write; goto cleanup0; } - memmove(a_dst->ptr + a_dst->wi, self->private_impl.f_stack + v_s, + memmove(a_dst.buf->ptr + a_dst.buf->wi, self->private_impl.f_stack + v_s, sizeof(self->private_impl.f_stack) - v_s); - a_dst->wi += sizeof(self->private_impl.f_stack) - v_s; + a_dst.buf->wi += sizeof(self->private_impl.f_stack) - v_s; if (v_use_save_code) { self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_c)); self->private_impl.f_prefixes[v_save_code] = ((uint16_t)(v_prev_code)); diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 1518dc78..0eb2a3bb 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -49,6 +49,16 @@ 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 #ifdef __cplusplus @@ -145,15 +155,15 @@ void puffs_gif_decoder_destructor(puffs_gif_decoder* self); // ---------------- Public Function Prototypes puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, - puffs_base_buf1* a_dst, - puffs_base_buf1* a_src); + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src); void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, uint32_t a_lw); puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, - puffs_base_buf1* a_dst, - puffs_base_buf1* a_src); + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src); #ifdef __cplusplus } // extern "C" diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 4f5d046c..75900e29 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -852,6 +852,10 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { case t.KeyPtr, t.KeyOpenBracket: return nil, nil, nil } + switch n.Name().Key() { + case t.KeyReader1, t.KeyWriter1: + return nil, nil, nil + } b := [2]*big.Int{} if key := n.Name().Key(); key < t.Key(len(numTypeBounds)) { diff --git a/lang/check/type.go b/lang/check/type.go index 9d79539b..2efcf1b0 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -467,9 +467,9 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { s = q.f.Func.In() case t.KeyOut: s = q.f.Func.Out() - case t.KeyBuf1: + case t.KeyReader1, t.KeyWriter1: // TODO: remove this hack and be more principled about the built-in - // buf1 type. + // buf1, reader1, writer1 types. // // Another hack is using typeExprPlaceholder until a TypeExpr can // represent function types. @@ -730,7 +730,7 @@ swtch: if n.Min() != nil || n.Max() != nil { // TODO: reject. You can only refine numeric types. } - if name := n.Name().Key(); name == t.KeyBool || name == t.KeyBuf1 { + if name := n.Name().Key(); name == t.KeyBool || name == t.KeyReader1 || name == t.KeyWriter1 { break } for _, s := range q.c.structs { diff --git a/lang/token/list.go b/lang/token/list.go index be643a56..2fb2acbc 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -214,18 +214,20 @@ const ( KeyTrue = Key(IDTrue >> KeyShift) KeyZero = Key(IDZero >> KeyShift) - KeyI8 = Key(IDI8 >> KeyShift) - KeyI16 = Key(IDI16 >> KeyShift) - KeyI32 = Key(IDI32 >> KeyShift) - KeyI64 = Key(IDI64 >> KeyShift) - KeyU8 = Key(IDU8 >> KeyShift) - KeyU16 = Key(IDU16 >> KeyShift) - KeyU32 = Key(IDU32 >> KeyShift) - KeyU64 = Key(IDU64 >> KeyShift) - KeyUsize = Key(IDUsize >> KeyShift) - KeyBool = Key(IDBool >> KeyShift) - KeyBuf1 = Key(IDBuf1 >> KeyShift) - KeyBuf2 = Key(IDBuf2 >> KeyShift) + KeyI8 = Key(IDI8 >> KeyShift) + KeyI16 = Key(IDI16 >> KeyShift) + KeyI32 = Key(IDI32 >> KeyShift) + KeyI64 = Key(IDI64 >> KeyShift) + KeyU8 = Key(IDU8 >> KeyShift) + KeyU16 = Key(IDU16 >> KeyShift) + KeyU32 = Key(IDU32 >> KeyShift) + KeyU64 = Key(IDU64 >> KeyShift) + KeyUsize = Key(IDUsize >> KeyShift) + KeyBool = Key(IDBool >> KeyShift) + KeyBuf1 = Key(IDBuf1 >> KeyShift) + KeyBuf2 = Key(IDBuf2 >> KeyShift) + KeyReader1 = Key(IDReader1 >> KeyShift) + KeyWriter1 = Key(IDWriter1 >> KeyShift) KeyUnderscore = Key(IDUnderscore >> KeyShift) KeyThis = Key(IDThis >> KeyShift) @@ -366,18 +368,20 @@ const ( IDTrue = ID(0x81<