mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 08:47:28 +01:00
Add choose
wuffs_png_decode_filter_1_sub/clang9 905MB/s ± 0% 1339MB/s ± 0% +48.05% (p=0.008 n=5+5) wuffs_png_decode_filter_1_sub/gcc10 1.12GB/s ± 0% 1.85GB/s ± 0% +64.80% (p=0.016 n=5+4)
This commit is contained in:
1 parent
364c396ae8
commit
beaf3ef11f
11 files changed
+235
-22
No files matched your search
@@ -40,6 +40,7 @@ const (
|
||||
KArg
|
||||
KAssert
|
||||
KAssign
|
||||
KChoose
|
||||
KConst
|
||||
KExpr
|
||||
KField
|
||||
@@ -71,6 +72,7 @@ var kindStrings = [...]string{
|
||||
KArg: "KArg",
|
||||
KAssert: "KAssert",
|
||||
KAssign: "KAssign",
|
||||
KChoose: "KChoose",
|
||||
KConst: "KConst",
|
||||
KExpr: "KExpr",
|
||||
KField: "KField",
|
||||
@@ -160,6 +162,7 @@ type Node struct {
|
||||
// Arg . . name Arg
|
||||
// Assert keyword . lit(reason) Assert
|
||||
// Assign operator . . Assign
|
||||
// Choose . . name Choose
|
||||
// Const . pkg name Const
|
||||
// Expr operator . literal/ident Expr
|
||||
// Field . . name Field
|
||||
@@ -198,6 +201,7 @@ func (n *Node) SetMType(x *TypeExpr) { n.mType = x }
|
||||
func (n *Node) AsArg() *Arg { return (*Arg)(n) }
|
||||
func (n *Node) AsAssert() *Assert { return (*Assert)(n) }
|
||||
func (n *Node) AsAssign() *Assign { return (*Assign)(n) }
|
||||
func (n *Node) AsChoose() *Choose { return (*Choose)(n) }
|
||||
func (n *Node) AsConst() *Const { return (*Const)(n) }
|
||||
func (n *Node) AsExpr() *Expr { return (*Expr)(n) }
|
||||
func (n *Node) AsField() *Field { return (*Field)(n) }
|
||||
@@ -647,6 +651,23 @@ func NewIf(condition *Expr, bodyIfTrue []*Node, bodyIfFalse []*Node, elseIf *If)
|
||||
}
|
||||
}
|
||||
|
||||
// Choose is "choose ID2: List0":
|
||||
// - ID2: name
|
||||
// - List0: <Expr> method names.
|
||||
type Choose Node
|
||||
|
||||
func (n *Choose) AsNode() *Node { return (*Node)(n) }
|
||||
func (n *Choose) Name() t.ID { return n.id2 }
|
||||
func (n *Choose) Args() []*Node { return n.list0 }
|
||||
|
||||
func NewChoose(name t.ID, args []*Node) *Choose {
|
||||
return &Choose{
|
||||
kind: KChoose,
|
||||
id2: name,
|
||||
list0: args,
|
||||
}
|
||||
}
|
||||
|
||||
// Ret is "return LHS" or "yield LHS":
|
||||
// - FlagsReturnsError LHS is an error status
|
||||
// - ID0: <IDReturn|IDYield>
|
||||
@@ -891,6 +912,32 @@ func (n *Func) BodyEndsWithReturn() bool {
|
||||
return (end.kind == KRet) && (end.AsRet().Keyword() == t.IDReturn)
|
||||
}
|
||||
|
||||
func fieldsEq(xs []*Node, ys []*Node) bool {
|
||||
if len(xs) != len(ys) {
|
||||
return false
|
||||
}
|
||||
for i := range xs {
|
||||
x, y := xs[i].AsField(), ys[i].AsField()
|
||||
if (x.Name() != y.Name()) || !x.XType().Eq(y.XType()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *Func) CheckChooseCompatible(o *Func) error {
|
||||
if n.Effect() != o.Effect() {
|
||||
return fmt.Errorf("different effects")
|
||||
}
|
||||
if !fieldsEq(n.In().Fields(), o.In().Fields()) {
|
||||
return fmt.Errorf("different args type")
|
||||
}
|
||||
if !n.Out().Eq(o.Out()) {
|
||||
return fmt.Errorf("different return type")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcName t.ID, in *Struct, out *TypeExpr, asserts []*Node, body []*Node) *Func {
|
||||
return &Func{
|
||||
kind: KFunc,
|
||||
@@ -1030,6 +1077,7 @@ func NewFile(filename string, topLevelDecls []*Node) *File {
|
||||
// Statement means one of:
|
||||
// - Assert
|
||||
// - Assign
|
||||
// - Choose
|
||||
// - IOBind
|
||||
// - If
|
||||
// - Iterate
|
||||
|
||||
@@ -245,6 +245,9 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KChoose:
|
||||
// No-op.
|
||||
|
||||
case a.KIOBind:
|
||||
n := n.AsIOBind()
|
||||
if _, err := q.bcheckExpr(n.IO(), 0); err != nil {
|
||||
|
||||
@@ -34,6 +34,7 @@ var (
|
||||
typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil)
|
||||
typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDQIdeal, nil, nil, nil)
|
||||
typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDComma, nil, nil, nil)
|
||||
typeExprNonNullptr = a.NewTypeExpr(0, t.IDBase, t.IDQNonNullptr, nil, nil, nil)
|
||||
typeExprNullptr = a.NewTypeExpr(0, t.IDBase, t.IDQNullptr, nil, nil, nil)
|
||||
typeExprPackage = a.NewTypeExpr(0, t.IDBase, t.IDQPackage, nil, nil, nil)
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDQPlaceholder, nil, nil, nil)
|
||||
|
||||
@@ -63,6 +63,11 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KChoose:
|
||||
if err := q.tcheckChoose(n.AsChoose()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KIf:
|
||||
for n := n.AsIf(); n != nil; n = n.ElseIf() {
|
||||
cond := n.Condition()
|
||||
@@ -1061,6 +1066,29 @@ swtch:
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *checker) tcheckChoose(n *a.Choose) error {
|
||||
qqid := q.astFunc.QQID()
|
||||
fQQID := t.QQID{qqid[0], qqid[1], n.Name()}
|
||||
f := q.c.funcs[fQQID]
|
||||
if f == nil {
|
||||
return fmt.Errorf("check: no function named %q", fQQID.Str(q.tm))
|
||||
}
|
||||
for _, o := range n.Args() {
|
||||
o := o.AsExpr()
|
||||
gQQID := t.QQID{qqid[0], qqid[1], o.Ident()}
|
||||
g := q.c.funcs[gQQID]
|
||||
if g == nil {
|
||||
return fmt.Errorf("check: no function named %q", gQQID.Str(q.tm))
|
||||
} else if err := f.CheckChooseCompatible(g); err != nil {
|
||||
return fmt.Errorf("check: incompatible choose functions %q and %q: %v",
|
||||
fQQID.Str(q.tm), gQQID.Str(q.tm), err)
|
||||
}
|
||||
o.SetMBounds(bounds{one, one})
|
||||
o.SetMType(typeExprNonNullptr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var comparisonOps = [...]bool{
|
||||
t.IDXBinaryNotEq: true,
|
||||
t.IDXBinaryLessThan: true,
|
||||
|
||||
+35
-2
@@ -309,7 +309,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
||||
implements := []*a.Node(nil)
|
||||
if p.peek1() == t.IDImplements {
|
||||
p.src = p.src[1:]
|
||||
implements, err = p.parseList(t.IDOpenParen, (*parser).parseQualifiedIdentNode)
|
||||
implements, err = p.parseList(t.IDOpenParen, (*parser).parseQualifiedIdentAsTypeExprNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -340,7 +340,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
||||
return nil, fmt.Errorf(`parse: unrecognized top level declaration at %s:%d`, p.filename, line)
|
||||
}
|
||||
|
||||
func (p *parser) parseQualifiedIdentNode() (*a.Node, error) {
|
||||
func (p *parser) parseQualifiedIdentAsTypeExprNode() (*a.Node, error) {
|
||||
pkg, name, err := p.parseQualifiedIdent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -367,6 +367,14 @@ func (p *parser) parseQualifiedIdent() (t.ID, t.ID, error) {
|
||||
return x, y, nil
|
||||
}
|
||||
|
||||
func (p *parser) parseIdentAsExprNode() (*a.Node, error) {
|
||||
id, err := p.parseIdent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewExpr(0, 0, id, nil, nil, nil, nil).AsNode(), nil
|
||||
}
|
||||
|
||||
func (p *parser) parseIdent() (t.ID, error) {
|
||||
if len(p.src) == 0 {
|
||||
return 0, fmt.Errorf(`parse: expected identifier at %s:%d`, p.filename, p.line())
|
||||
@@ -790,6 +798,31 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
n.SetJumpTarget(loop)
|
||||
return n.AsNode(), nil
|
||||
|
||||
case t.IDChoose:
|
||||
p.src = p.src[1:]
|
||||
if p.funcEffect.Pure() {
|
||||
return nil, fmt.Errorf(`parse: choose within pure function at %s:%d`, p.filename, p.line())
|
||||
}
|
||||
name, err := p.parseIdent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if x := p.peek1(); x != t.IDEq {
|
||||
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.IDOpenBracket {
|
||||
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:]
|
||||
args, err := p.parseList(t.IDCloseBracket, (*parser).parseIdentAsExprNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewChoose(name, args).AsNode(), nil
|
||||
|
||||
case t.IDIOBind, t.IDIOLimit:
|
||||
return p.parseIOBindNode()
|
||||
|
||||
|
||||
@@ -457,6 +457,7 @@ const (
|
||||
IDDagger1 = ID(0x106)
|
||||
IDDagger2 = ID(0x107)
|
||||
|
||||
IDQNonNullptr = ID(0x10A)
|
||||
IDQNullptr = ID(0x10B)
|
||||
IDQPackage = ID(0x10C)
|
||||
IDQPlaceholder = ID(0x10D)
|
||||
@@ -811,6 +812,10 @@ var builtInsByID = [nBuiltInIDs]string{
|
||||
IDDagger1: "†", // U+2020 DAGGER
|
||||
IDDagger2: "‡", // U+2021 DOUBLE DAGGER
|
||||
|
||||
// IDQNonNullptr is used by the type checker to build an artificial MType
|
||||
// for function pointers.
|
||||
IDQNonNullptr: "«NonNullptr»",
|
||||
|
||||
// IDQNullptr is used by the type checker to build an artificial MType for
|
||||
// the nullptr literal.
|
||||
IDQNullptr: "«Nullptr»",
|
||||
|
||||
Reference in new issue
Block a user