diff --git a/doc/changelog.md b/doc/changelog.md index efdea195..d7073df9 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -4,6 +4,7 @@ ## Work In Progress - Added `example/toy-aux-image`. +- Added `if.likely` and `if.unlikely`. - Added `slice_var as nptr array[etc] etc` conversion. - Added `std/jpeg`. - Added `std/netpbm`. diff --git a/internal/cgen/statement.go b/internal/cgen/statement.go index 52d8d9af..41074a1d 100644 --- a/internal/cgen/statement.go +++ b/internal/cgen/statement.go @@ -430,12 +430,19 @@ func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error { } for { + prefix, suffix := "", "" + switch n.Likelihood() { + case t.IDLikely: + prefix, suffix = "WUFFS_BASE__LIKELY(", ")" + case t.IDUnlikely: + prefix, suffix = "WUFFS_BASE__UNLIKELY(", ")" + } condition := buffer(nil) if err := g.writeExpr(&condition, n.Condition(), false, 0); err != nil { return err } // Calling trimParens avoids clang's -Wparentheses-equality warning. - b.printf("if (%s) {\n", trimParens(condition)) + b.printf("if (%s%s%s) {\n", prefix, trimParens(condition), suffix) for _, o := range n.BodyIfTrue() { if err := g.writeStatement(b, o, depth); err != nil { return err diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 98b39732..85b73ebb 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -170,7 +170,7 @@ type Node struct { // File . . . File // Func funcName receiverPkg receiverName Func // IOManip keyword . . IOManip - // If . . . If + // If . likelihood . If // Iterate advance label length Iterate // Jump keyword label . Jump // Ret keyword . . Ret @@ -706,14 +706,16 @@ func NewWhile(label t.ID, condition *Expr, asserts []*Node) *While { type If Node func (n *If) AsNode() *Node { return (*Node)(n) } +func (n *If) Likelihood() t.ID { return n.id1 } func (n *If) Condition() *Expr { return n.mhs.AsExpr() } func (n *If) ElseIf() *If { return n.rhs.AsIf() } func (n *If) BodyIfTrue() []*Node { return n.list2 } func (n *If) BodyIfFalse() []*Node { return n.list1 } -func NewIf(condition *Expr, bodyIfTrue []*Node, bodyIfFalse []*Node, elseIf *If) *If { +func NewIf(likelihood t.ID, condition *Expr, bodyIfTrue []*Node, bodyIfFalse []*Node, elseIf *If) *If { return &If{ kind: KIf, + id1: likelihood, mhs: condition.AsNode(), rhs: elseIf.AsNode(), list1: bodyIfFalse, diff --git a/lang/parse/parse.go b/lang/parse/parse.go index c6667051..0d9bb813 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -1174,6 +1174,16 @@ func (p *parser) parseIf() (*a.If, error) { return nil, fmt.Errorf(`parse: expected "if", got %q at %s:%d`, got, p.filename, p.line()) } p.src = p.src[1:] + likelihood, err := p.parseLabel() + if err != nil { + return nil, err + } + switch likelihood { + case 0, t.IDLikely, t.IDUnlikely: + default: + got := p.tm.ByID(likelihood) + return nil, fmt.Errorf(`parse: expected "if.likely" or "if.unlikely", got %q at %s:%d`, "if."+got, p.filename, p.line()) + } condition, err := p.parseExpr() if err != nil { return nil, err @@ -1201,7 +1211,7 @@ func (p *parser) parseIf() (*a.If, error) { } } } - return a.NewIf(condition, bodyIfTrue, bodyIfFalse, elseIf), nil + return a.NewIf(likelihood, condition, bodyIfTrue, bodyIfFalse, elseIf), nil } func (p *parser) parseIterateNode() (*a.Node, error) { diff --git a/lang/token/list.go b/lang/token/list.go index ab4709cb..eab589cb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -652,10 +652,12 @@ const ( IDCPUArchIs32Bit = ID(0x202) IDInitialize = ID(0x203) IDLength = ID(0x204) - IDReset = ID(0x205) - IDSet = ID(0x206) - IDUnroll = ID(0x207) - IDUpdate = ID(0x208) + IDLikely = ID(0x205) + IDReset = ID(0x206) + IDSet = ID(0x207) + IDUnlikely = ID(0x208) + IDUnroll = ID(0x209) + IDUpdate = ID(0x20A) // TODO: range/rect methods like intersection and contains? @@ -1089,8 +1091,10 @@ var builtInsByID = [nBuiltInIDs]string{ IDCPUArchIs32Bit: "cpu_arch_is_32_bit", IDInitialize: "initialize", IDLength: "length", + IDLikely: "likely", IDReset: "reset", IDSet: "set", + IDUnlikely: "unlikely", IDUnroll: "unroll", IDUpdate: "update",