lang: add if.likely and if.unlikely

This commit is contained in:
Nigel Tao committed 2023-06-20 18:00:48 +10:00
1 parent 1acd74482a
commit 8916f1b6ea
5 files changed
+32 -8

No files matched your search

+1
View File
@@ -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`.
+8 -1
View File
@@ -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
+4 -2
View File
@@ -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,
+11 -1
View File
@@ -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) {
+8 -4
View File
@@ -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",