Add an io_limit keyword

This commit is contained in:
Nigel Tao committed 2018-12-09 09:03:21 +11:00
1 parent 3aff5e6f42
commit e992921dce
10 files changed
+159 -32

No files matched your search

+10 -2
View File
@@ -344,8 +344,16 @@ func (h *resumabilityHelper) doExpr1(r resumabilities, n *a.Expr, sef subExprFil
}
func (h *resumabilityHelper) doIOBind(r resumabilities, n *a.IOBind, depth uint32) error {
// Ignore the IOBind fields, as they should be I/O types and therefore not
// resumable.
if n.IO() != nil {
if err := h.doExpr(r, n.IO()); err != nil {
return err
}
}
if n.Limit() != nil {
if err := h.doExpr(r, n.Limit()); err != nil {
return err
}
}
return h.doBlock(r, n.Body(), depth)
}
+10
View File
@@ -226,6 +226,9 @@ func (g *gen) writeStatementAssign(b *buffer,
func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
inFields := n.InFields()
if n.Keyword() == t.IDIOLimit {
inFields = []*a.Node{n.IO().AsNode()}
}
if g.currFunk.ioBinds > maxIOBinds || len(inFields) > maxIOBindInFields {
return fmt.Errorf("too many temporary variables required")
@@ -251,6 +254,13 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
b.printf("wuffs_base__io_%s %s%d_%s%s = %s%s;\n",
cTyp, oPrefix, ioBindNum, prefix, name, prefix, name)
if n.Keyword() == t.IDIOLimit {
b.printf("wuffs_base__io_%s__set_limit(&%s%s, iop_%s%s,\n", cTyp, prefix, name, prefix, name)
if err := g.writeExpr(b, n.Limit(), 0); err != nil {
return err
}
b.printf(");\n")
}
// TODO: save / restore all iop vars, not just for local IO vars? How
// does this work if the io_bind body advances these pointers, either
// directly or by calling other funcs?
+1 -1
View File
@@ -7,7 +7,7 @@
- Ship as a "single file C library"
- Added a `skipgendeps` flag.
- Added a `nullptr` literal and `nptr T` type.
- Added an `io_bind` keyword.
- Added `io_bind` and `io_limit` keywords.
- Added a `use` keyword.
- Added a `yield` keyword.
- Made the `return` value mandatory; added `ok` literal.
+11 -2
View File
@@ -482,7 +482,10 @@ func NewField(name t.ID, xType *TypeExpr) *Field {
}
}
// IOBind is "io_bind (in_fields) { List2 }":
// IOBind is "io_bind (in_fields) { List2 }" or "io_limit (io:LHS, limit:MHS)":
// - ID0: <IDIOBind|IDIOLimit>
// - LHS: <nil|Expr>
// - MHS: <nil|Expr>
// - List0: <Expr> in.something fields
// - List2: <Statement> body
//
@@ -490,12 +493,18 @@ func NewField(name t.ID, xType *TypeExpr) *Field {
type IOBind Node
func (n *IOBind) AsNode() *Node { return (*Node)(n) }
func (n *IOBind) Keyword() t.ID { return n.id0 }
func (n *IOBind) IO() *Expr { return n.lhs.AsExpr() }
func (n *IOBind) Limit() *Expr { return n.mhs.AsExpr() }
func (n *IOBind) InFields() []*Node { return n.list0 }
func (n *IOBind) Body() []*Node { return n.list2 }
func NewIOBind(in_fields []*Node, body []*Node) *IOBind {
func NewIOBind(keyword t.ID, io *Expr, limit *Expr, in_fields []*Node, body []*Node) *IOBind {
return &IOBind{
kind: KIOBind,
id0: keyword,
lhs: io.AsNode(),
mhs: limit.AsNode(),
list0: in_fields,
list2: body,
}
+1 -1
View File
@@ -205,7 +205,7 @@ var Funcs = []string{
"io_reader.available() u64",
"io_reader.position() u64",
"io_reader.set!(s slice u8, closed bool)",
"io_reader.set_limit!(l u64)",
"io_reader.set_limit!(l u64)", // TODO: remove, as it's no longer used?
"io_reader.set_mark!()",
"io_reader.since_mark() slice u8",
+11 -2
View File
@@ -234,8 +234,17 @@ func (q *checker) bcheckStatement(n *a.Node) error {
case a.KIOBind:
n := n.AsIOBind()
for _, o := range n.InFields() {
if _, err := q.bcheckExpr(o.AsExpr(), 0); err != nil {
if n.Keyword() == t.IDIOBind {
for _, o := range n.InFields() {
if _, err := q.bcheckExpr(o.AsExpr(), 0); err != nil {
return err
}
}
} else {
if _, err := q.bcheckExpr(n.IO(), 0); err != nil {
return err
}
if _, err := q.bcheckExpr(n.Limit(), 0); err != nil {
return err
}
}
+23 -5
View File
@@ -120,13 +120,31 @@ func (q *checker) tcheckStatement(n *a.Node) error {
case a.KIOBind:
n := n.AsIOBind()
for _, o := range n.InFields() {
if err := q.tcheckExpr(o.AsExpr(), 0); err != nil {
if n.Keyword() == t.IDIOBind {
for _, o := range n.InFields() {
if err := q.tcheckExpr(o.AsExpr(), 0); err != nil {
return err
}
if typ := o.AsExpr().MType(); !typ.IsIOType() {
return fmt.Errorf("check: io_bind expression %q, of type %q, does not have an I/O type",
o.AsExpr().Str(q.tm), typ.Str(q.tm))
}
}
} else {
if err := q.tcheckExpr(n.IO(), 0); err != nil {
return err
}
if typ := o.AsExpr().MType(); !typ.IsIOType() {
return fmt.Errorf("check: io_bind expression %q, of type %q, does not have an I/O type",
o.AsExpr().Str(q.tm), typ.Str(q.tm))
if typ := n.IO().MType(); !typ.IsIOType() {
return fmt.Errorf("check: %s expression %q, of type %q, does not have an I/O type",
n.Keyword().Str(q.tm), n.IO().Str(q.tm), typ.Str(q.tm))
}
if err := q.tcheckExpr(n.Limit(), 0); err != nil {
return err
}
if typ := n.Limit().MType(); !typ.Eq(typeExprU64) {
return fmt.Errorf("check: %s expression %q, of type %q, does not have base.u64 type",
n.Keyword().Str(q.tm), n.Limit().Str(q.tm), typ.Str(q.tm))
}
}
for _, o := range n.Body() {
+85 -11
View File
@@ -582,17 +582,8 @@ func (p *parser) parseStatement1() (*a.Node, error) {
}
return a.NewJump(x, label).AsNode(), nil
case t.IDIOBind:
p.src = p.src[1:]
in_fields, err := p.parseList(t.IDCloseParen, (*parser).parseIOBindExprNode)
if err != nil {
return nil, err
}
body, err := p.parseBlock()
if err != nil {
return nil, err
}
return a.NewIOBind(in_fields, body).AsNode(), nil
case t.IDIOBind, t.IDIOLimit:
return p.parseIOBindNode()
case t.IDIf:
o, err := p.parseIf()
@@ -677,6 +668,89 @@ func (p *parser) parseAsserts() ([]*a.Node, error) {
return asserts, nil
}
func (p *parser) parseIOBindNode() (*a.Node, error) {
if p.peek1() == t.IDIOBind {
p.src = p.src[1:]
in_fields, err := p.parseList(t.IDCloseParen, (*parser).parseIOBindExprNode)
if err != nil {
return nil, err
}
body, err := p.parseBlock()
if err != nil {
return nil, err
}
return a.NewIOBind(t.IDIOBind, nil, nil, in_fields, body).AsNode(), nil
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDOpenParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDIO {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "io", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDColon {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ":", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
io, err := p.parseExpr()
if err != nil {
return nil, err
}
if io.Effect() != 0 {
return nil, fmt.Errorf(`parse: argument %q is not effect-free at %s:%d`,
io.Str(p.tm), p.filename, p.line())
}
if x := p.peek1(); x != t.IDComma {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ",", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDLimit {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "limit", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1(); x != t.IDColon {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ":", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
limit, err := p.parseExpr()
if err != nil {
return nil, err
}
if limit.Effect() != 0 {
return nil, fmt.Errorf(`parse: argument %q is not effect-free at %s:%d`,
io.Str(p.tm), p.filename, p.line())
}
if x := p.peek1(); x != t.IDCloseParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
body, err := p.parseBlock()
if err != nil {
return nil, err
}
return a.NewIOBind(t.IDIOLimit, io, limit, nil, body).AsNode(), nil
}
func (p *parser) parseIf() (*a.If, error) {
if x := p.peek1(); x != t.IDIf {
got := p.tm.ByID(x)
+6
View File
@@ -353,6 +353,7 @@ const (
IDIterate = ID(0x92)
IDYield = ID(0x93)
IDIOBind = ID(0x94)
IDIOLimit = ID(0x95)
)
const (
@@ -588,6 +589,8 @@ const (
IDStride = ID(0x245)
IDSuffix = ID(0x246)
IDWidth = ID(0x247)
IDIO = ID(0x248)
IDLimit = ID(0x249)
)
var builtInsByID = [nBuiltInIDs]string{
@@ -678,6 +681,7 @@ var builtInsByID = [nBuiltInIDs]string{
IDIterate: "iterate",
IDYield: "yield",
IDIOBind: "io_bind",
IDIOLimit: "io_limit",
IDArray: "array",
IDNptr: "nptr",
@@ -908,6 +912,8 @@ var builtInsByID = [nBuiltInIDs]string{
IDStride: "stride",
IDSuffix: "suffix",
IDWidth: "width",
IDIO: "io",
IDLimit: "limit",
}
var builtInsByName = map[string]ID{}
+1 -8
View File
@@ -640,14 +640,7 @@ pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reade
var w base.io_writer
io_bind (w) {
w.set!(s:this.uncompressed[this.uncompressed_wi:])
// TODO: enforce that calling r.set_limit has a precondition
// that r.is_bound(), and that you can't suspend inside an
// io_bind? Otherwise, the cgen implementation becomes more
// complicated if we must save and re-apply limits.
//
// Should calling r.set_mark also require r.is_bound()?
io_bind (args.src) {
args.src.set_limit!(l:block_size)
io_limit (io:args.src, limit:block_size) {
args.src.set_mark!()
var z base.status =? this.lzw.decode!??(dst:w, src:args.src)
this.uncompressed_wi = 4096 - (w.available().min(x:4096) as base.u32)