acrn-hypervisor/misc/config_tools/board_inspector/acpiparser/dsdt.py
Junjie Mao 3bcb3146ad board_inspector: improve readability and performance of AML parser
This patch refines the AML parser to improve its readability and
performance in the following ways.

  1. A Tree object now has the parts of the corresponding object being
     member fields. As an example, a Tree with label `DefMethod` now has
     members `NameString`, `MethodFlags` and `TermList`.

  2. It is now possible to assign names each part of an object. The grammar
     is updated to assign different names to the parts with the same type
     in the same object.

  3. Invocation to intermediate factories is now skipped. As an example,
     when parsing a ByteConst that acts as a ByteIndex, the original
     implementation invokes the following factories in sequence:

         ByteIndex -> TermArg -> DataObject -> ComputationalData -> ByteConst

     The factories TermArg, DataObject and ComputationalData does nothing
     but forward the parsing to the next-level factory according to the
     opcode of the next object. With this patch, the invocation sequence
     becomes:

         ByteIndex -> ByteConst

     i.e. ByteIndex directly passes to ByteConst which can parse the next
     opcode.

Tracked-On: #6298
Signed-off-by: Junjie Mao <junjie.mao@intel.com>
2021-08-09 09:05:01 +08:00

42 lines
1.3 KiB
Python

# Copyright (C) 2021 Intel Corporation. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
import os
import logging
from acpiparser.aml.stream import Stream
from acpiparser.aml.parser import AMLCode, DeferredExpansion
from acpiparser.aml.tree import Tree
from acpiparser.aml.context import Context
from acpiparser.aml.interpreter import ConcreteInterpreter
from acpiparser.aml.visitors import ConditionallyUnregisterSymbolVisitor
def DSDT(val):
table_dir = os.path.dirname(val)
if not table_dir:
table_dir = "."
ssdt = filter(lambda x: x.startswith("SSDT"), os.listdir(table_dir))
tables = [val] + list(map(lambda x: os.path.join(table_dir, x), ssdt))
context = Context()
trees = []
try:
for t in tables:
logging.info(f"Loading {t}")
context.switch_stream(t)
tree = Tree()
AMLCode.parse(context, tree)
tree = DeferredExpansion(context).transform_topdown(tree)
trees.append(tree)
except Exception as e:
context.current_stream.dump()
raise
context.skip_external_on_lookup()
visitor = ConditionallyUnregisterSymbolVisitor(ConcreteInterpreter(context))
for tree in trees:
visitor.visit_topdown(tree)
return context