Generate code for "mark in.dst".

The generated code is ugly and the language design is not finalized, but
the flate_backref_crosses_blocks test (testing a correctness bug) now
passes. We can fix the ugliness in subsequent commits.
This commit is contained in:
Nigel Tao committed 2017-10-22 14:44:53 +11:00
1 parent 83c8faef4d
commit 21bbbd422f
11 files changed
+107 -24

No files matched your search

+10
View File
@@ -79,11 +79,21 @@ typedef struct puffs_base__limit1 {
typedef struct {
puffs_base__buf1* buf;
puffs_base__limit1 limit;
// Do not access the private_impl's fields directly. There is no API/ABI
// compatibility or safety guarantee if you do so.
struct {
uint8_t* mark;
} private_impl;
} puffs_base__reader1;
typedef struct {
puffs_base__buf1* buf;
puffs_base__limit1 limit;
// Do not access the private_impl's fields directly. There is no API/ABI
// compatibility or safety guarantee if you do so.
struct {
uint8_t* mark;
} private_impl;
} puffs_base__writer1;
#endif // PUFFS_BASE_HEADER_H
+2 -2
View File
@@ -19,8 +19,8 @@ package cgen
const baseHeader = "" +
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Puffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\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// Si" +
"milarly, 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__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, p" +
"lus 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 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} pu" +
"ffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
"lus 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 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 // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or s" +
"afety guarantee if you do so.\n struct {\n uint8_t* mark;\n } private_impl;\n} puffs_base__reader1;\n\ntypedef struct {\n puffs_base__buf1* buf;\n puffs_base__limit1 limit;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n uint8_t* mark;\n } private_impl;\n} puffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
+10 -5
View File
@@ -169,8 +169,10 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeySlice, 0) {
b.printf("((puffs_base__slice_u8){ .ptr = %swstart_dst, .len = %swptr_dst - %swstart_dst, })",
bPrefix, bPrefix, bPrefix)
b.printf("((puffs_base__slice_u8){ "+
".ptr = (%sdst.private_impl.mark ? %sdst.private_impl.mark : %swstart_dst), "+
".len = %swptr_dst - (%sdst.private_impl.mark ? %sdst.private_impl.mark : %swstart_dst), })",
aPrefix, aPrefix, bPrefix, bPrefix, aPrefix, aPrefix, bPrefix)
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromReader32, 2) {
@@ -185,8 +187,9 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
return nil
}
if isInDst(g.tm, n, t.KeyCopyFromHistory32, 2) {
b.printf("puffs_base__writer1__copy_from_history32(&%swptr_dst, %swstart_dst, %swend_dst",
bPrefix, bPrefix, bPrefix)
b.printf("puffs_base__writer1__copy_from_history32(&%swptr_dst, "+
"(%sdst.private_impl.mark ? %sdst.private_impl.mark : %swstart_dst), %swend_dst",
bPrefix, aPrefix, aPrefix, bPrefix, bPrefix)
for _, o := range n.Args() {
b.writeb(',')
if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil {
@@ -239,7 +242,9 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
if typ := x.MType(); typ.Decorator() == 0 && typ.Name().Key() == t.KeyWriter1 {
// TODO: don't hard-code dst.
const wName = "dst"
b.printf("(uint64_t)(%swptr_%s - %swstart_%s)", bPrefix, wName, bPrefix, wName)
b.printf("(uint64_t)(%swptr_%s - "+
"(%sdst.private_impl.mark ? %sdst.private_impl.mark : %swstart_%s))",
bPrefix, wName, aPrefix, aPrefix, bPrefix, wName)
} else {
b.writes("(uint64_t)(")
if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil {
+2 -1
View File
@@ -69,7 +69,8 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
return nil
case a.KIO:
b.writes("/* placeholder */\n")
// TODO: the mark needs to apply on every (coroutine) call to the
// function, not just when we execute this particular statement.
return nil
case a.KIf:
+14 -2
View File
@@ -88,7 +88,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
case t.KeyReader1:
if header {
b.printf("uint8_t* %srptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr) // TODO: delete.
b.printf("uint8_t* %srend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
@@ -110,13 +110,25 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header
case t.KeyWriter1:
if header {
b.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr) // TODO: delete.
b.printf("uint8_t* %swend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("%swptr_%s = %s%s.buf->ptr + %s%s.buf->wi;",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
// TODO: remove this hard-coded hack for "mark in.dst".
//
// TODO: is a private_impl.mark the right representation? What if the
// function is passed a (ptr writer1) instead of a (writer1)? Do we
// still want to have that mark live outside of the function scope?
if header &&
g.currFunk.astFunc.Receiver().String(g.tm) == "flate_decoder" &&
g.currFunk.astFunc.Name().String(g.tm) == "decode" {
b.printf("%s%s.private_impl.mark = %swptr_%s;", aPrefix, nameStr, bPrefix, nameStr)
}
if header {
b.printf("%swstart_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
}