diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 3310df35..ab153230 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -198,10 +198,19 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInDst(g.tm, n, t.KeySinceMark, 0) { + // Write .len as either "foo ? bar : baz" or "bar". + // + // TODO: drop the "true" in the "if true", provided that the + // benchmark numbers improve. + len0, len1 := "", "" + if true || !n.BoundsCheckOptimized() { + len0 = aPrefix + "dst.private_impl.mark ?" + len1 = ": 0" + } b.printf("((puffs_base__slice_u8){ "+ ".ptr = %sdst.private_impl.mark, "+ - ".len = %sdst.private_impl.mark ? (size_t)(%swptr_dst - %sdst.private_impl.mark) : 0, })", - aPrefix, aPrefix, bPrefix, aPrefix) + ".len = %s (size_t)(%swptr_dst - %sdst.private_impl.mark) %s, })", + aPrefix, len0, bPrefix, aPrefix, len1) return nil } if isInDst(g.tm, n, t.KeyIsMarked, 0) { diff --git a/lang/ast/ast.go b/lang/ast/ast.go index bd6ff418..5b2b9b5e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -87,16 +87,33 @@ var kindStrings = [...]string{ type Flags uint32 const ( - FlagsImpure = Flags(0x00000001) - FlagsSuspendible = Flags(0x00000002) - FlagsCallImpure = Flags(0x00000004) - FlagsCallSuspendible = Flags(0x00000008) - FlagsPublic = Flags(0x00000010) - FlagsTypeChecked = Flags(0x00000020) - FlagsHasBreak = Flags(0x00000040) - FlagsHasContinue = Flags(0x00000080) - FlagsGlobalIdent = Flags(0x00000100) - FlagsProvenNotToSuspend = Flags(0x00000200) + FlagsImpure = Flags(0x00000001) + FlagsSuspendible = Flags(0x00000002) + FlagsCallImpure = Flags(0x00000004) + FlagsCallSuspendible = Flags(0x00000008) + FlagsPublic = Flags(0x00000010) + FlagsTypeChecked = Flags(0x00000020) + FlagsHasBreak = Flags(0x00000040) + FlagsHasContinue = Flags(0x00000080) + FlagsGlobalIdent = Flags(0x00000100) +) + +// These flags are set by the bounds checker to generate optimized code. +const ( + // FlagsProvenNotToSuspend notes that a method such as read_u8 or + // write_u16le is proven not to suspend. For example, if it's guaranteed + // that there enough buffer space for an I/O method to succeed. + // + // Some methods, such as unread_u8, are called with the question mark, as + // "r.unread_u8?()", so nominally can suspend, but their semantics also + // guarantee to either succeed or fail, never suspend and retry later. + FlagsProvenNotToSuspend = Flags(0x00010000) + // FlagsBoundsCheckOptimized similarly means that some code gen's run time + // bounds checks or null pointer checks can be skipped, although such + // methods are not called with the question mark. Specifically, those + // methods are: + // - since_mark + FlagsBoundsCheckOptimized = Flags(0x00020000) ) type Node struct { @@ -245,23 +262,25 @@ const MaxExprDepth = 255 // and ID1 is the message. type Expr Node -func (n *Expr) Node() *Node { return (*Node)(n) } -func (n *Expr) Pure() bool { return n.flags&FlagsImpure == 0 } -func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 } -func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 } -func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 } -func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 } -func (n *Expr) GlobalIdent() bool { return n.flags&FlagsGlobalIdent != 0 } -func (n *Expr) ProvenNotToSuspend() bool { return n.flags&FlagsProvenNotToSuspend != 0 } -func (n *Expr) ConstValue() *big.Int { return n.constValue } -func (n *Expr) MType() *TypeExpr { return n.mType } -func (n *Expr) ID0() t.ID { return n.id0 } -func (n *Expr) ID1() t.ID { return n.id1 } -func (n *Expr) LHS() *Node { return n.lhs } -func (n *Expr) MHS() *Node { return n.mhs } -func (n *Expr) RHS() *Node { return n.rhs } -func (n *Expr) Args() []*Node { return n.list0 } +func (n *Expr) Node() *Node { return (*Node)(n) } +func (n *Expr) Pure() bool { return n.flags&FlagsImpure == 0 } +func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 } +func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 } +func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 } +func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 } +func (n *Expr) GlobalIdent() bool { return n.flags&FlagsGlobalIdent != 0 } +func (n *Expr) ProvenNotToSuspend() bool { return n.flags&FlagsProvenNotToSuspend != 0 } +func (n *Expr) BoundsCheckOptimized() bool { return n.flags&FlagsBoundsCheckOptimized != 0 } +func (n *Expr) ConstValue() *big.Int { return n.constValue } +func (n *Expr) MType() *TypeExpr { return n.mType } +func (n *Expr) ID0() t.ID { return n.id0 } +func (n *Expr) ID1() t.ID { return n.id1 } +func (n *Expr) LHS() *Node { return n.lhs } +func (n *Expr) MHS() *Node { return n.mhs } +func (n *Expr) RHS() *Node { return n.rhs } +func (n *Expr) Args() []*Node { return n.list0 } +func (n *Expr) SetBoundsCheckOptimized() { n.flags |= FlagsBoundsCheckOptimized } func (n *Expr) SetConstValue(x *big.Int) { n.constValue = x } func (n *Expr) SetGlobalIdent() { n.flags |= FlagsGlobalIdent } func (n *Expr) SetMType(x *TypeExpr) { n.mType = x } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index d67a5d99..1dd16963 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -233,8 +233,8 @@ func (q *checker) bcheckStatement(n *a.Node) error { q.errFilename, q.errLine = n.Raw().FilenameLine() // TODO: be principled about checking for provenNotToSuspend. Should we - // call bcheckIOMethods only for assignments, for var statements too, or - // for all statements generally? + // call bcheckOptimizeSuspendible only for assignments, for var statements + // too, or for all statements generally? switch n.Kind() { case a.KAssert: @@ -250,7 +250,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { return err } if n.Suspendible() { - if err := q.bcheckIOMethods(n, 0); err != nil { + if err := q.bcheckOptimizeSuspendible(n, 0); err != nil { return err } } @@ -407,7 +407,7 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { // What's here is somewhat ad hoc. Perhaps we need a call keyword and an // explicit "foo = call bar?()" syntax. if rhs.Suspendible() { - if err := q.bcheckIOMethods(rhs, 0); err != nil { + if err := q.bcheckOptimizeSuspendible(rhs, 0); err != nil { return err } } @@ -715,6 +715,9 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error return nil, nil, fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]", n.String(q.tm), nMin, nMax, tMin, tMax) } + if err := q.bcheckOptimizeNonSuspendible(n); err != nil { + return nil, nil, err + } return nMin, nMax, nil } @@ -1162,7 +1165,50 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { return b[0], b[1], nil } -func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error { +// splitReceiverMethod returns the "receiver" and "method" in the expression +// "receiver.method(etc)". +func splitReceiverMethod(n *a.Expr) (receiver *a.Expr, method t.Key) { + if n.ID0().Key() != t.KeyOpenParen { + return nil, 0 + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot { + return nil, 0 + } + return n.LHS().Expr(), n.ID1().Key() +} + +// TODO: should bcheckOptimizeNonSuspendible be bcheckOptimizeExpr and check +// both suspendible and non-suspendible expressions? +// +// See also "be principled about checking for provenNotToSuspend". + +func (q *checker) bcheckOptimizeNonSuspendible(n *a.Expr) error { + // TODO: check n.CallSuspendible()? + // + // Should we have an explicit "foo = call bar?()" syntax? + + nReceiver, nMethod := splitReceiverMethod(n) + if nReceiver == nil { + return nil + } + + // TODO: check that nReceiver's type is actually a reader1 or writer1. + + if nMethod == t.KeySinceMark { + for _, x := range q.facts { + xReceiver, xMethod := splitReceiverMethod(x) + if xMethod == t.KeyIsMarked && xReceiver.Eq(nReceiver) { + n.SetBoundsCheckOptimized() + return nil + } + } + } + + return nil +} + +func (q *checker) bcheckOptimizeSuspendible(n *a.Expr, depth uint32) error { if depth > a.MaxExprDepth { return fmt.Errorf("check: expression recursion depth too large") } @@ -1171,14 +1217,14 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error { if !n.CallSuspendible() { for _, o := range n.Node().Raw().SubNodes() { if o != nil && o.Kind() == a.KExpr { - if err := q.bcheckIOMethods(o.Expr(), depth); err != nil { + if err := q.bcheckOptimizeSuspendible(o.Expr(), depth); err != nil { return err } } } for _, o := range n.Args() { if o != nil && o.Kind() == a.KExpr { - if err := q.bcheckIOMethods(o.Expr(), depth); err != nil { + if err := q.bcheckOptimizeSuspendible(o.Expr(), depth); err != nil { return err } } @@ -1186,31 +1232,29 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error { return nil } - // Figure out the "receiver" in "receiver.read_xxx?()", for some I/O method - // "read_xxx". - if n.ID0().Key() != t.KeyOpenParen { + nReceiver, nMethod := splitReceiverMethod(n) + if nReceiver == nil { return nil } - receiver := n.LHS().Expr() - if receiver.ID0().Key() != t.KeyDot { - return nil - } - receiver, method := receiver.LHS().Expr(), receiver.ID1().Key() - // TODO: check that receiver's type is actually a reader1 or writer1. + // TODO: check that nReceiver's type is actually a reader1 or writer1. - advance := (*big.Int)(nil) - if method == t.KeyUnreadU8 { + if nMethod == t.KeyUnreadU8 { // unread_u8 can never suspend, only succeed or fail. n.SetProvenNotToSuspend() return nil - } else if method < t.Key(len(ioMethodAdvances)) { - advance = ioMethodAdvances[method] - } - if advance == nil { - return nil } + if nMethod < t.Key(len(ioMethodAdvances)) { + if advance := ioMethodAdvances[nMethod]; advance != nil { + return q.bcheckOptimizeIOMethodAdvance(n, nReceiver, advance) + } + } + + return nil +} + +func (q *checker) bcheckOptimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *big.Int) error { return q.facts.update(func(x *a.Expr) (*a.Expr, error) { op := x.ID0().Key() if op != t.KeyXBinaryGreaterEq && op != t.KeyXBinaryGreaterThan {