First cut at a "[] u8" slice type.

This commit is contained in:
Nigel Tao committed 2017-09-08 16:53:59 +10:00
1 parent ceedd8c3b9
commit 519ec373f2
15 files changed
+123 -32

No files matched your search

+10 -2
View File
@@ -34,8 +34,16 @@
// 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 length) plus
// additional indexes into that buffer.
// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length).
//
// A value with all fields NULL or zero is a valid, empty slice.
typedef struct {
uint8_t* ptr;
size_t len;
} puffs_base_slice_u8;
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus
// additional indexes into that buffer, plus an opened / closed flag.
//
// A value with all fields NULL or zero is a valid, empty buffer.
typedef struct {
+2 -2
View File
@@ -7,8 +7,8 @@ package cgen
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// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len" +
"; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\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_slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base_slice_u8;\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\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// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's curr" +
"ent read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
+14
View File
@@ -250,6 +250,20 @@ func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy,
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
// TODO: fix this, allow slices of all types, not just of u8's. Also allow
// arrays of slices, slices of pointers, etc.
if n.Decorator().Key() == t.KeyColon {
o := n.Inner()
if o.Decorator() == 0 && o.Name().Key() == t.KeyU8 && !o.IsRefined() {
b.writes("puffs_base_slice_u8")
b.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
return nil
}
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
}
// maxNumPointers is an arbitrary implementation restriction.
const maxNumPointers = 16
+15 -2
View File
@@ -169,7 +169,8 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
b.writes(";\n")
}
}
if n.XType().Decorator().Key() == t.KeyOpenBracket {
switch n.XType().Decorator().Key() {
case t.KeyOpenBracket:
if n.Value() != nil {
// TODO: something like:
// cv := n.XType().ArrayLength().ConstValue()
@@ -181,7 +182,19 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
// TODO: arrays of arrays.
name := n.Name().String(g.tm)
b.printf("memset(%s%s, 0, sizeof(%s%s));\n", vPrefix, name, vPrefix, name)
} else {
case t.KeyColon:
if n.Value() != nil {
return fmt.Errorf("TODO: slice initializers for non-zero default values")
}
// TODO: don't assume that the slice is a slice of u8.
b.printf("%s%s = ((puffs_base_slice_u8){});", vPrefix, n.Name().String(g.tm))
// TODO: remove this.
b.printf("\n/* Avoid the \"unused variable\" warning. */\n")
b.printf("if (%s%s.ptr) {}\n", vPrefix, n.Name().String(g.tm))
default:
b.printf("%s%s = ", vPrefix, n.Name().String(g.tm))
if v := n.Value(); v != nil {
if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil {
+5
View File
@@ -242,6 +242,11 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, prefix string, suspend bo
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
return nil
}
case t.KeyColon:
// TODO: should we be able to resume and suspend slices?
// TODO: don't assume that the slice is a slice of u8.
b.printf("%s = ((puffs_base_slice_u8){});\n", lhs)
return nil
}
return fmt.Errorf("cannot resume or suspend a local variable %q of type %q",
n.Name().String(g.tm), n.XType().String(g.tm))