mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 00:52:18 +01:00
Add cpu_arch
This commit is contained in:
1 parent
6124fda77f
commit
020b8e6903
15 files changed
+353
-77
No files matched your search
+38
-15
@@ -106,6 +106,7 @@ const (
|
||||
FlagsRetsError = Flags(0x00004000)
|
||||
FlagsPrivateData = Flags(0x00008000)
|
||||
FlagsChoosy = Flags(0x00010000)
|
||||
FlagsHasChooseCPUArch = Flags(0x00020000)
|
||||
)
|
||||
|
||||
func (f Flags) AsEffect() Effect { return Effect(f) }
|
||||
@@ -424,8 +425,9 @@ func NewExpr(flags Flags, operator t.ID, ident t.ID, lhs *Node, mhs *Node, rhs *
|
||||
}
|
||||
}
|
||||
|
||||
// Assert is "assert RHS via ID2(args)", "pre etc", "inv etc" or "post etc":
|
||||
// - ID0: <IDAssert|IDPre|IDInv|IDPost>
|
||||
// Assert is "assert RHS via ID2(args)", "choose etc", "pre etc", "inv etc" or
|
||||
// "post etc":
|
||||
// - ID0: <IDAssert|IDChoose|IDPre|IDInv|IDPost>
|
||||
// - ID2: <"-string literal> reason
|
||||
// - RHS: <Expr>
|
||||
// - List0: <Arg> reason arguments
|
||||
@@ -439,6 +441,26 @@ func (n *Assert) Args() []*Node { return n.list0 }
|
||||
|
||||
func (n *Assert) DropExprCachedMBounds() error { return n.AsNode().Walk(dropExprCachedMBounds) }
|
||||
|
||||
func (n *Assert) IsChooseCPUArch() bool {
|
||||
if n.id0 != t.IDChoose {
|
||||
return false
|
||||
}
|
||||
cond := n.Condition()
|
||||
if cond.Operator() != t.IDXBinaryGreaterEq {
|
||||
return false
|
||||
}
|
||||
lhs := cond.LHS().AsExpr()
|
||||
rhs := cond.RHS().AsExpr()
|
||||
if (lhs.Operator() != 0) || (lhs.Ident() != t.IDCPUArch) || (rhs.Operator() != 0) {
|
||||
return false
|
||||
}
|
||||
switch rhs.Ident() {
|
||||
case t.IDSSE128:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func NewAssert(keyword t.ID, condition *Expr, reason t.ID, args []*Node) *Assert {
|
||||
return &Assert{
|
||||
kind: KAssert,
|
||||
@@ -894,19 +916,20 @@ const MaxBodyDepth = 255
|
||||
// - List2: <Statement> body
|
||||
type Func Node
|
||||
|
||||
func (n *Func) AsNode() *Node { return (*Node)(n) }
|
||||
func (n *Func) Choosy() bool { return n.flags&FlagsChoosy != 0 }
|
||||
func (n *Func) Effect() Effect { return Effect(n.flags) }
|
||||
func (n *Func) Public() bool { return n.flags&FlagsPublic != 0 }
|
||||
func (n *Func) Filename() string { return n.filename }
|
||||
func (n *Func) Line() uint32 { return n.line }
|
||||
func (n *Func) QQID() t.QQID { return t.QQID{n.id1, n.id2, n.id0} }
|
||||
func (n *Func) Receiver() t.QID { return t.QID{n.id1, n.id2} }
|
||||
func (n *Func) FuncName() t.ID { return n.id0 }
|
||||
func (n *Func) In() *Struct { return n.lhs.AsStruct() }
|
||||
func (n *Func) Out() *TypeExpr { return n.rhs.AsTypeExpr() }
|
||||
func (n *Func) Asserts() []*Node { return n.list1 }
|
||||
func (n *Func) Body() []*Node { return n.list2 }
|
||||
func (n *Func) AsNode() *Node { return (*Node)(n) }
|
||||
func (n *Func) Choosy() bool { return n.flags&FlagsChoosy != 0 }
|
||||
func (n *Func) Effect() Effect { return Effect(n.flags) }
|
||||
func (n *Func) HasChooseCPUArch() bool { return n.flags&FlagsHasChooseCPUArch != 0 }
|
||||
func (n *Func) Public() bool { return n.flags&FlagsPublic != 0 }
|
||||
func (n *Func) Filename() string { return n.filename }
|
||||
func (n *Func) Line() uint32 { return n.line }
|
||||
func (n *Func) QQID() t.QQID { return t.QQID{n.id1, n.id2, n.id0} }
|
||||
func (n *Func) Receiver() t.QID { return t.QID{n.id1, n.id2} }
|
||||
func (n *Func) FuncName() t.ID { return n.id0 }
|
||||
func (n *Func) In() *Struct { return n.lhs.AsStruct() }
|
||||
func (n *Func) Out() *TypeExpr { return n.rhs.AsTypeExpr() }
|
||||
func (n *Func) Asserts() []*Node { return n.list1 }
|
||||
func (n *Func) Body() []*Node { return n.list2 }
|
||||
|
||||
func (n *Func) BodyEndsWithReturn() bool {
|
||||
if len(n.list2) == 0 {
|
||||
|
||||
@@ -370,6 +370,18 @@ func (q *checker) hasIsErrorFact(id t.ID) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (q *checker) bcheckFuncAssert(n *a.Assert) error {
|
||||
if n.IsChooseCPUArch() {
|
||||
b := bounds{zero, one}
|
||||
cond := n.Condition()
|
||||
cond.SetMBounds(b)
|
||||
cond.LHS().AsExpr().SetMBounds(b)
|
||||
cond.RHS().AsExpr().SetMBounds(b)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: function assertions are not supported yet")
|
||||
}
|
||||
|
||||
func (q *checker) bcheckAssert(n *a.Assert) error {
|
||||
if err := n.DropExprCachedMBounds(); err != nil {
|
||||
return err
|
||||
|
||||
+5
-1
@@ -624,7 +624,11 @@ func (c *Checker) checkFuncContract(node *a.Node) error {
|
||||
tm: c.tm,
|
||||
}
|
||||
for _, o := range n.Asserts() {
|
||||
if err := q.tcheckAssert(o.AsAssert()); err != nil {
|
||||
setPlaceholderMBoundsMType(o)
|
||||
if err := q.tcheckFuncAssert(o.AsAssert()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.bcheckFuncAssert(o.AsAssert()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,6 +206,17 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *checker) tcheckFuncAssert(n *a.Assert) error {
|
||||
if n.IsChooseCPUArch() {
|
||||
cond := n.Condition()
|
||||
cond.SetMType(typeExprBool)
|
||||
cond.LHS().AsExpr().SetMType(typeExprU32)
|
||||
cond.RHS().AsExpr().SetMType(typeExprU32)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: function assertions are not supported yet")
|
||||
}
|
||||
|
||||
func (q *checker) tcheckAssert(n *a.Assert) error {
|
||||
cond := n.Condition()
|
||||
if err := q.tcheckExpr(cond, 0); err != nil {
|
||||
@@ -531,6 +542,10 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
return fmt.Errorf("check: %q has effect %q but %q has effect %q",
|
||||
n.Str(q.tm), ne, f.QQID().Str(q.tm), fe)
|
||||
}
|
||||
if f.HasChooseCPUArch() {
|
||||
return fmt.Errorf(`check: cannot call cpu_arch function %q directly, only via "choose"`,
|
||||
f.QQID().Str(q.tm))
|
||||
}
|
||||
|
||||
genericType1 := (*a.TypeExpr)(nil)
|
||||
genericType2 := (*a.TypeExpr)(nil)
|
||||
|
||||
+40
-9
@@ -249,9 +249,20 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.assertsSorted(asserts); err != nil {
|
||||
if err := p.assertsSorted(asserts, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, o := range asserts {
|
||||
o := o.AsAssert()
|
||||
if o.Keyword() != t.IDChoose {
|
||||
continue
|
||||
} else if o.IsChooseCPUArch() {
|
||||
flags |= a.FlagsHasChooseCPUArch
|
||||
} else {
|
||||
return nil, fmt.Errorf(`parse: invalid "choose" condition at %s:%d`,
|
||||
p.filename, p.line())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p.allowVar = true
|
||||
@@ -266,6 +277,17 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
||||
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
|
||||
if (flags & a.FlagsHasChooseCPUArch) != 0 {
|
||||
if (flags & a.FlagsPublic) != 0 {
|
||||
return nil, fmt.Errorf(`parse: cpu_arch function cannot be public at %s:%d`,
|
||||
p.filename, p.line())
|
||||
}
|
||||
if (flags & a.FlagsChoosy) != 0 {
|
||||
return nil, fmt.Errorf(`parse: cpu_arch function cannot be choosy at %s:%d`,
|
||||
p.filename, p.line())
|
||||
}
|
||||
}
|
||||
p.funcEffect = 0
|
||||
in := a.NewStruct(0, p.filename, line, t.IDArgs, nil, argFields)
|
||||
return a.NewFunc(flags, p.filename, line, id0, id1, in, out, asserts, body).AsNode(), nil
|
||||
@@ -638,17 +660,26 @@ func (p *parser) parseBlock(doubleCurly bool) ([]*a.Node, error) {
|
||||
return block, nil
|
||||
}
|
||||
|
||||
func (p *parser) assertsSorted(asserts []*a.Node) error {
|
||||
seenInv, seenPost := false, false
|
||||
for _, a := range asserts {
|
||||
switch a.AsAssert().Keyword() {
|
||||
func (p *parser) assertsSorted(asserts []*a.Node, allowChoose bool) error {
|
||||
seenPre, seenInv, seenPost := false, false, false
|
||||
for _, o := range asserts {
|
||||
switch o.AsAssert().Keyword() {
|
||||
case t.IDAssert:
|
||||
return fmt.Errorf(`parse: assertion chain cannot contain "assert", `+
|
||||
`only "pre", "inv" and "post" at %s:%d`, p.filename, p.line())
|
||||
case t.IDChoose:
|
||||
if !allowChoose {
|
||||
return fmt.Errorf(`parse: invalid "choose" at %s:%d`, p.filename, p.line())
|
||||
}
|
||||
if seenPre || seenPost || seenInv {
|
||||
break
|
||||
}
|
||||
continue
|
||||
case t.IDPre:
|
||||
if seenPost || seenInv {
|
||||
break
|
||||
}
|
||||
seenPre = true
|
||||
continue
|
||||
case t.IDInv:
|
||||
if seenPost {
|
||||
@@ -660,7 +691,7 @@ func (p *parser) assertsSorted(asserts []*a.Node) error {
|
||||
seenPost = true
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf(`parse: assertion chain not in "pre", "inv", "post" order at %s:%d`,
|
||||
return fmt.Errorf(`parse: assertion chain not in "choose", "pre", "inv", "post" order at %s:%d`,
|
||||
p.filename, p.line())
|
||||
}
|
||||
return nil
|
||||
@@ -668,7 +699,7 @@ func (p *parser) assertsSorted(asserts []*a.Node) error {
|
||||
|
||||
func (p *parser) parseAssertNode() (*a.Node, error) {
|
||||
switch x := p.peek1(); x {
|
||||
case t.IDAssert, t.IDPre, t.IDInv, t.IDPost:
|
||||
case t.IDAssert, t.IDChoose, t.IDPre, t.IDInv, t.IDPost:
|
||||
p.src = p.src[1:]
|
||||
condition, err := p.parseExpr()
|
||||
if err != nil {
|
||||
@@ -753,7 +784,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
p.allowVar = false
|
||||
|
||||
switch x {
|
||||
case t.IDAssert, t.IDPre, t.IDPost:
|
||||
case t.IDAssert:
|
||||
return p.parseAssertNode()
|
||||
|
||||
case t.IDBreak, t.IDContinue:
|
||||
@@ -1006,7 +1037,7 @@ func (p *parser) parseAsserts() ([]*a.Node, error) {
|
||||
if asserts, err = p.parseList(t.IDOpenDoubleCurly, (*parser).parseAssertNode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.assertsSorted(asserts); err != nil {
|
||||
if err := p.assertsSorted(asserts, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
+22
-6
@@ -605,12 +605,13 @@ const (
|
||||
// -------- 0x200 block.
|
||||
|
||||
IDAdvance = ID(0x200)
|
||||
IDInitialize = ID(0x201)
|
||||
IDLength = ID(0x202)
|
||||
IDReset = ID(0x203)
|
||||
IDSet = ID(0x204)
|
||||
IDUnroll = ID(0x205)
|
||||
IDUpdate = ID(0x206)
|
||||
IDCPUArch = ID(0x201)
|
||||
IDInitialize = ID(0x202)
|
||||
IDLength = ID(0x203)
|
||||
IDReset = ID(0x204)
|
||||
IDSet = ID(0x205)
|
||||
IDUnroll = ID(0x206)
|
||||
IDUpdate = ID(0x207)
|
||||
|
||||
// TODO: range/rect methods like intersection and contains?
|
||||
|
||||
@@ -636,6 +637,15 @@ const (
|
||||
|
||||
IDLimitedSwizzleU32InterleavedFromReader = ID(0x280)
|
||||
IDSwizzleInterleavedFromReader = ID(0x281)
|
||||
|
||||
// -------- 0x300 block.
|
||||
|
||||
// 0x30? are reserved for NEON.
|
||||
|
||||
IDSSE128 = ID(0x310)
|
||||
IDSSE128I = ID(0x311)
|
||||
|
||||
// 0x32? are reserved for AVX256.
|
||||
)
|
||||
|
||||
var builtInsByID = [nBuiltInIDs]string{
|
||||
@@ -969,6 +979,7 @@ var builtInsByID = [nBuiltInIDs]string{
|
||||
// -------- 0x200 block.
|
||||
|
||||
IDAdvance: "advance",
|
||||
IDCPUArch: "cpu_arch",
|
||||
IDInitialize: "initialize",
|
||||
IDLength: "length",
|
||||
IDReset: "reset",
|
||||
@@ -998,6 +1009,11 @@ var builtInsByID = [nBuiltInIDs]string{
|
||||
|
||||
IDLimitedSwizzleU32InterleavedFromReader: "limited_swizzle_u32_interleaved_from_reader",
|
||||
IDSwizzleInterleavedFromReader: "swizzle_interleaved_from_reader",
|
||||
|
||||
// -------- 0x300 block.
|
||||
|
||||
IDSSE128: "sse128",
|
||||
IDSSE128I: "sse128_i",
|
||||
}
|
||||
|
||||
var builtInsByName = map[string]ID{}
|
||||
|
||||
Reference in new issue
Block a user