mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
lang: add if.likely and if.unlikely
This commit is contained in:
1 parent
1acd74482a
commit
8916f1b6ea
5 files changed
+32
-8
No files matched your search
@@ -4,6 +4,7 @@
|
|||||||
## Work In Progress
|
## Work In Progress
|
||||||
|
|
||||||
- Added `example/toy-aux-image`.
|
- Added `example/toy-aux-image`.
|
||||||
|
- Added `if.likely` and `if.unlikely`.
|
||||||
- Added `slice_var as nptr array[etc] etc` conversion.
|
- Added `slice_var as nptr array[etc] etc` conversion.
|
||||||
- Added `std/jpeg`.
|
- Added `std/jpeg`.
|
||||||
- Added `std/netpbm`.
|
- Added `std/netpbm`.
|
||||||
|
|||||||
@@ -430,12 +430,19 @@ func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
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)
|
condition := buffer(nil)
|
||||||
if err := g.writeExpr(&condition, n.Condition(), false, 0); err != nil {
|
if err := g.writeExpr(&condition, n.Condition(), false, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Calling trimParens avoids clang's -Wparentheses-equality warning.
|
// 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() {
|
for _, o := range n.BodyIfTrue() {
|
||||||
if err := g.writeStatement(b, o, depth); err != nil {
|
if err := g.writeStatement(b, o, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+4
-2
@@ -170,7 +170,7 @@ type Node struct {
|
|||||||
// File . . . File
|
// File . . . File
|
||||||
// Func funcName receiverPkg receiverName Func
|
// Func funcName receiverPkg receiverName Func
|
||||||
// IOManip keyword . . IOManip
|
// IOManip keyword . . IOManip
|
||||||
// If . . . If
|
// If . likelihood . If
|
||||||
// Iterate advance label length Iterate
|
// Iterate advance label length Iterate
|
||||||
// Jump keyword label . Jump
|
// Jump keyword label . Jump
|
||||||
// Ret keyword . . Ret
|
// Ret keyword . . Ret
|
||||||
@@ -706,14 +706,16 @@ func NewWhile(label t.ID, condition *Expr, asserts []*Node) *While {
|
|||||||
type If Node
|
type If Node
|
||||||
|
|
||||||
func (n *If) AsNode() *Node { return (*Node)(n) }
|
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) Condition() *Expr { return n.mhs.AsExpr() }
|
||||||
func (n *If) ElseIf() *If { return n.rhs.AsIf() }
|
func (n *If) ElseIf() *If { return n.rhs.AsIf() }
|
||||||
func (n *If) BodyIfTrue() []*Node { return n.list2 }
|
func (n *If) BodyIfTrue() []*Node { return n.list2 }
|
||||||
func (n *If) BodyIfFalse() []*Node { return n.list1 }
|
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{
|
return &If{
|
||||||
kind: KIf,
|
kind: KIf,
|
||||||
|
id1: likelihood,
|
||||||
mhs: condition.AsNode(),
|
mhs: condition.AsNode(),
|
||||||
rhs: elseIf.AsNode(),
|
rhs: elseIf.AsNode(),
|
||||||
list1: bodyIfFalse,
|
list1: bodyIfFalse,
|
||||||
|
|||||||
+11
-1
@@ -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())
|
return nil, fmt.Errorf(`parse: expected "if", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
}
|
}
|
||||||
p.src = p.src[1:]
|
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()
|
condition, err := p.parseExpr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (p *parser) parseIterateNode() (*a.Node, error) {
|
||||||
|
|||||||
+8
-4
@@ -652,10 +652,12 @@ const (
|
|||||||
IDCPUArchIs32Bit = ID(0x202)
|
IDCPUArchIs32Bit = ID(0x202)
|
||||||
IDInitialize = ID(0x203)
|
IDInitialize = ID(0x203)
|
||||||
IDLength = ID(0x204)
|
IDLength = ID(0x204)
|
||||||
IDReset = ID(0x205)
|
IDLikely = ID(0x205)
|
||||||
IDSet = ID(0x206)
|
IDReset = ID(0x206)
|
||||||
IDUnroll = ID(0x207)
|
IDSet = ID(0x207)
|
||||||
IDUpdate = ID(0x208)
|
IDUnlikely = ID(0x208)
|
||||||
|
IDUnroll = ID(0x209)
|
||||||
|
IDUpdate = ID(0x20A)
|
||||||
|
|
||||||
// TODO: range/rect methods like intersection and contains?
|
// TODO: range/rect methods like intersection and contains?
|
||||||
|
|
||||||
@@ -1089,8 +1091,10 @@ var builtInsByID = [nBuiltInIDs]string{
|
|||||||
IDCPUArchIs32Bit: "cpu_arch_is_32_bit",
|
IDCPUArchIs32Bit: "cpu_arch_is_32_bit",
|
||||||
IDInitialize: "initialize",
|
IDInitialize: "initialize",
|
||||||
IDLength: "length",
|
IDLength: "length",
|
||||||
|
IDLikely: "likely",
|
||||||
IDReset: "reset",
|
IDReset: "reset",
|
||||||
IDSet: "set",
|
IDSet: "set",
|
||||||
|
IDUnlikely: "unlikely",
|
||||||
IDUnroll: "unroll",
|
IDUnroll: "unroll",
|
||||||
IDUpdate: "update",
|
IDUpdate: "update",
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user