1
0
mirror of https://github.com/taigrr/gopher-os synced 2026-04-02 02:48:44 -07:00

acpi: refactor scope lookup code and move it into the entity pkg

This commit is contained in:
Achilleas Anagnostopoulos
2017-12-29 09:10:16 +00:00
parent 17842763e9
commit 38143ab510
3 changed files with 230 additions and 368 deletions

View File

@@ -0,0 +1,138 @@
package entity
import "strings"
// ResolveScopedPath examines a path expression and attempts to break it down
// into a parent and child segment. The parent segment is looked up via the
// regular scope rules specified in page 252 of the ACPI 6.2 spec. If the
// parent scope is found then the function returns back the parent entity and
// the name of the child that should be appended to it. If the expression
// lookup fails then the function returns nil, "".
func ResolveScopedPath(curScope, rootScope Container, expr string) (parent Container, name string) {
if len(expr) <= 1 {
return nil, ""
}
// Pattern looks like \FOO or ^+BAR or BAZ (relative to curScope)
lastDotIndex := strings.LastIndexByte(expr, '.')
if lastDotIndex == -1 {
switch expr[0] {
case '\\':
return rootScope, expr[1:]
case '^':
lastHatIndex := strings.LastIndexByte(expr, '^')
if target := FindInScope(curScope, rootScope, expr[:lastHatIndex+1]); target != nil {
return target.(Container), expr[lastHatIndex+1:]
}
return nil, ""
default:
return curScope, expr
}
}
// Pattern looks like: \FOO.BAR.BAZ or ^+FOO.BAR.BAZ or FOO.BAR.BAZ
if target := FindInScope(curScope, rootScope, expr[:lastDotIndex]); target != nil {
return target.(Container), expr[lastDotIndex+1:]
}
return nil, ""
}
// FindInScope attempts to find an object with the given name using the rules
// specified in page 252 of the ACPI 6.2 spec:
//
// There are two types of namespace paths: an absolute namespace path (that is,
// one that starts with a \ prefix), and a relative namespace path (that is,
// one that is relative to the current namespace). The namespace search rules
// discussed above, only apply to single NameSeg paths, which is a relative
// namespace path. For those relative name paths that contain multiple NameSegs
// or Parent Prefixes, ^, the search rules do not apply. If the search rules
// do not apply to a relative namespace path, the namespace object is looked up
// relative to the current namespace
func FindInScope(curScope, rootScope Container, name string) Entity {
nameLen := len(name)
if nameLen == 0 {
return nil
}
switch {
case name[0] == '\\': // relative to the root scope
if nameLen > 1 {
return findRelativeToScope(rootScope, name[1:])
}
// Name was just `\`; this matches the root namespace
return rootScope
case name[0] == '^': // relative to the parent scope(s)
for startIndex := 0; startIndex < nameLen; startIndex++ {
switch name[startIndex] {
case '^':
curScope = curScope.Parent()
// No parent to visit
if curScope == nil {
return nil
}
default:
// Found the start of the name. Look it up relative to curNs
return findRelativeToScope(curScope, name[startIndex:])
}
}
// Name was just a sequence of '^'; this matches the last curScope value
return curScope
case strings.ContainsRune(name, '.'):
// If the name contains any '.' then we still need to look it
// up relative to the current scope
return findRelativeToScope(curScope, name)
default:
// We can apply the search rules described by the spec
for s := curScope; s != nil; s = s.Parent() {
for _, child := range s.Children() {
if child.Name() == name {
return child
}
}
}
}
// Not found
return nil
}
// findRelativeToScope returns the Entity referenced by path relative
// to the provided Namespace. If the name contains dots, each segment
// is used to access a nested namespace. If the path does not point
// to a NamedObject then lookupRelativeTo returns back nil.
func findRelativeToScope(ns Container, path string) Entity {
var matchName string
matchNextPathSegment:
for {
dotSepIndex := strings.IndexRune(path, '.')
if dotSepIndex != -1 {
matchName = path[:dotSepIndex]
path = path[dotSepIndex+1:]
// Search for a scoped child named "matchName"
for _, child := range ns.Children() {
if childNs, ok := child.(Container); ok && childNs.Name() == matchName {
ns = childNs
continue matchNextPathSegment
}
}
} else {
// Search for a child named "name"
for _, child := range ns.Children() {
if child.Name() == path {
return child
}
}
}
// Next segment in the path was not found or last segment not found
break
}
return nil
}

View File

@@ -0,0 +1,215 @@
package entity
import (
"reflect"
"testing"
)
func TestResolveScopedPath(t *testing.T) {
scopeMap := genTestScopes()
specs := []struct {
curScope Container
pathExpr string
wantParent Entity
wantName string
}{
{
scopeMap["IDE0"].(Container),
`\_SB_`,
scopeMap[`\`],
"_SB_",
},
{
scopeMap["IDE0"].(Container),
`^FOO`,
scopeMap[`PCI0`],
"FOO",
},
{
scopeMap["IDE0"].(Container),
`^^FOO`,
scopeMap[`_SB_`],
"FOO",
},
{
scopeMap["IDE0"].(Container),
`_ADR`,
scopeMap[`IDE0`],
"_ADR",
},
// Paths with dots
{
scopeMap["IDE0"].(Container),
`\_SB_.PCI0.IDE0._ADR`,
scopeMap[`IDE0`],
"_ADR",
},
{
scopeMap["PCI0"].(Container),
`IDE0._ADR`,
scopeMap[`IDE0`],
"_ADR",
},
{
scopeMap["PCI0"].(Container),
`_CRS`,
scopeMap[`PCI0`],
"_CRS",
},
// Bad queries
{
scopeMap["PCI0"].(Container),
`FOO.BAR.BAZ`,
nil,
"",
},
{
scopeMap["PCI0"].(Container),
``,
nil,
"",
},
{
scopeMap["PCI0"].(Container),
`\`,
nil,
"",
},
{
scopeMap["PCI0"].(Container),
`^^^^^^^^^BADPATH`,
nil,
"",
},
}
root := scopeMap[`\`].(Container)
for specIndex, spec := range specs {
gotParent, gotName := ResolveScopedPath(spec.curScope, root, spec.pathExpr)
if !reflect.DeepEqual(gotParent, spec.wantParent) {
t.Errorf("[spec %d] expected lookup to return %#v; got %#v", specIndex, spec.wantParent, gotParent)
continue
}
if gotName != spec.wantName {
t.Errorf("[spec %d] expected lookup to return node name %q; got %q", specIndex, spec.wantName, gotName)
}
}
}
func TestFindInScope(t *testing.T) {
scopeMap := genTestScopes()
specs := []struct {
curScope Container
lookup string
want Entity
}{
// Search rules do not apply for these cases
{
scopeMap["PCI0"].(Container),
`\`,
scopeMap[`\`],
},
{
scopeMap["PCI0"].(Container),
"IDE0._ADR",
scopeMap["_ADR"],
},
{
scopeMap["IDE0"].(Container),
"^^PCI0.IDE0._ADR",
scopeMap["_ADR"],
},
{
scopeMap["IDE0"].(Container),
`\_SB_.PCI0.IDE0._ADR`,
scopeMap["_ADR"],
},
{
scopeMap["IDE0"].(Container),
`\_SB_.PCI0`,
scopeMap["PCI0"],
},
{
scopeMap["IDE0"].(Container),
`^`,
scopeMap["PCI0"],
},
// Bad queries
{
scopeMap["_SB_"].(Container),
"PCI0.USB._CRS",
nil,
},
{
scopeMap["IDE0"].(Container),
"^^^^^^^^^^^^^^^^^^^",
nil,
},
{
scopeMap["IDE0"].(Container),
`^^^^^^^^^^^FOO`,
nil,
},
{
scopeMap["IDE0"].(Container),
"FOO",
nil,
},
{
scopeMap["IDE0"].(Container),
"",
nil,
},
// Search rules apply for these cases
{
scopeMap["IDE0"].(Container),
"_CRS",
scopeMap["_CRS"],
},
}
root := scopeMap[`\`].(Container)
for specIndex, spec := range specs {
if got := FindInScope(spec.curScope, root, spec.lookup); !reflect.DeepEqual(got, spec.want) {
t.Errorf("[spec %d] expected lookup to return %#v; got %#v", specIndex, spec.want, got)
}
}
}
func genTestScopes() map[string]Entity {
// Setup the example tree from page 252 of the acpi 6.2 spec
// \
// SB
// \
// PCI0
// | _CRS
// \
// IDE0
// | _ADR
ideScope := NewScope(OpScope, 42, `IDE0`)
pciScope := NewScope(OpScope, 42, `PCI0`)
sbScope := NewScope(OpScope, 42, `_SB_`)
rootScope := NewScope(OpScope, 42, `\`)
adr := NewMethod(42, `_ADR`)
crs := NewMethod(42, `_CRS`)
// Setup tree
ideScope.Append(adr)
pciScope.Append(crs)
pciScope.Append(ideScope)
sbScope.Append(pciScope)
rootScope.Append(sbScope)
return map[string]Entity{
"IDE0": ideScope,
"PCI0": pciScope,
"_SB_": sbScope,
"\\": rootScope,
"_ADR": adr,
"_CRS": crs,
}
}