qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: thuth@redhat.com, philmd@redhat.com
Subject: [Qemu-devel] [PATCH 03/51] minikconfig: add semantic analysis
Date: Thu,  7 Feb 2019 18:56:46 +0100	[thread overview]
Message-ID: <1549562254-41157-4-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1549562254-41157-1-git-send-email-pbonzini@redhat.com>

There are three parts in the semantic analysis:

1) evaluating expressions.  This is done as a simple visit
of the Expr nodes.

2) ordering clauses.  This is done by constructing a graph of variables.
There is an edge from X to Y if Y depends on X, if X selects Y, or if
X appears in a conditional selection of Y; in other words, if the value
of X can affect the value of Y.  Each clause has a "destination" variable
whose value can be affected by the clause, and clauses will be processed
according to a topological sorting of their destination variables.
Defaults are processed after all other clauses with the same destination.

3) deriving the value of the variables.  This is done by processing
the clauses in the topological order provided by the previous step.
A "depends on" clause will force a variable to False, a "select" clause
will force a variable to True, an assignment will force a variable
to its RHS.  A default will set a variable to its RHS if it has not
been set before.  Because all variables have a default, after visiting
all clauses all variables will have been set.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20190123065618.3520-25-yang.zhong@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 scripts/minikconf.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 130 insertions(+), 8 deletions(-)

diff --git a/scripts/minikconf.py b/scripts/minikconf.py
index d74b9df..2df14ca 100644
--- a/scripts/minikconf.py
+++ b/scripts/minikconf.py
@@ -20,6 +20,10 @@ def debug_print(*args):
     print('#' + (' '.join(str(x) for x in args)))
     pass
 
+def debug_print(*args):
+    #print ' '.join(str(x) for x in args)
+    pass
+
 # -------------------------------------------
 # KconfigData implements the Kconfig semantics.  For now it can only
 # detect undefined symbols, i.e. symbols that were referenced in
@@ -39,6 +43,12 @@ class KconfigData:
         def __invert__(self):
             return KconfigData.NOT(self)
 
+        # Abstract methods
+        def add_edges_to(self, var):
+            pass
+        def evaluate(self):
+            assert False
+
     class AND(Expr):
         def __init__(self, lhs, rhs):
             self.lhs = lhs
@@ -46,6 +56,12 @@ class KconfigData:
         def __str__(self):
             return "(%s && %s)" % (self.lhs, self.rhs)
 
+        def add_edges_to(self, var):
+            self.lhs.add_edges_to(var)
+            self.rhs.add_edges_to(var)
+        def evaluate(self):
+            return self.lhs.evaluate() and self.rhs.evaluate()
+
     class OR(Expr):
         def __init__(self, lhs, rhs):
             self.lhs = lhs
@@ -53,35 +69,85 @@ class KconfigData:
         def __str__(self):
             return "(%s || %s)" % (self.lhs, self.rhs)
 
+        def add_edges_to(self, var):
+            self.lhs.add_edges_to(var)
+            self.rhs.add_edges_to(var)
+        def evaluate(self):
+            return self.lhs.evaluate() or self.rhs.evaluate()
+
     class NOT(Expr):
         def __init__(self, lhs):
             self.lhs = lhs
         def __str__(self):
             return "!%s" % (self.lhs)
 
+        def add_edges_to(self, var):
+            self.lhs.add_edges_to(var)
+        def evaluate(self):
+            return not self.lhs.evaluate()
+
     class Var(Expr):
         def __init__(self, name):
             self.name = name
             self.value = None
+            self.outgoing = set()
+            self.clauses_for_var = list()
         def __str__(self):
             return self.name
 
+        def has_value(self):
+            return not (self.value is None)
+        def set_value(self, val, clause):
+            self.clauses_for_var.append(clause)
+            if self.has_value() and self.value != val:
+                print("The following clauses were found for " + self.name)
+                for i in self.clauses_for_var:
+                    print("    " + str(i), file=sys.stderr)
+                raise KconfigParserError('contradiction between clauses when setting %s' % self)
+            debug_print("=> %s is now %s" % (self.name, val))
+            self.value = val
+
+        # depth first search of the dependency graph
+        def dfs(self, visited, f):
+            if self in visited:
+                return
+            visited.add(self)
+            for v in self.outgoing:
+                v.dfs(visited, f)
+            f(self)
+
+        def add_edges_to(self, var):
+            self.outgoing.add(var)
+        def evaluate(self):
+            if not self.has_value():
+                raise KconfigParserError('cycle found including %s' % self)
+            return self.value
+
     class Clause:
         def __init__(self, dest):
             self.dest = dest
+        def priority(self):
+            return 0
+        def process(self):
+            pass
 
     class AssignmentClause(Clause):
         def __init__(self, dest, value):
             KconfigData.Clause.__init__(self, dest)
             self.value = value
         def __str__(self):
-            return "%s=%s" % (self.dest, 'y' if self.value else 'n')
+            return "CONFIG_%s=%s" % (self.dest, 'y' if self.value else 'n')
+
+        def process(self):
+            self.dest.set_value(self.value, self)
 
     class DefaultClause(Clause):
         def __init__(self, dest, value, cond=None):
             KconfigData.Clause.__init__(self, dest)
             self.value = value
             self.cond = cond
+            if not (self.cond is None):
+                self.cond.add_edges_to(self.dest)
         def __str__(self):
             value = 'y' if self.value else 'n'
             if self.cond is None:
@@ -89,20 +155,38 @@ class KconfigData:
             else:
                 return "config %s default %s if %s" % (self.dest, value, self.cond)
 
+        def priority(self):
+            # Defaults are processed just before leaving the variable
+            return -1
+        def process(self):
+            if not self.dest.has_value() and \
+                    (self.cond is None or self.cond.evaluate()):
+                self.dest.set_value(self.value, self)
+
     class DependsOnClause(Clause):
         def __init__(self, dest, expr):
             KconfigData.Clause.__init__(self, dest)
             self.expr = expr
+            self.expr.add_edges_to(self.dest)
         def __str__(self):
             return "config %s depends on %s" % (self.dest, self.expr)
 
+        def process(self):
+            if not self.expr.evaluate():
+                self.dest.set_value(False, self)
+
     class SelectClause(Clause):
         def __init__(self, dest, cond):
             KconfigData.Clause.__init__(self, dest)
             self.cond = cond
+            self.cond.add_edges_to(self.dest)
         def __str__(self):
             return "select %s if %s" % (self.dest, self.cond)
 
+        def process(self):
+            if self.cond.evaluate():
+                self.dest.set_value(True, self)
+
     def __init__(self):
         self.previously_included = []
         self.incl_info = None
@@ -120,11 +204,54 @@ class KconfigData:
                 undef = True
         return undef
 
+    def compute_config(self):
+        if self.check_undefined():
+            raise KconfigParserError(parser, "there were undefined symbols")
+            return None
+
+        debug_print("Input:")
+        for clause in self.clauses:
+            debug_print(clause)
+
+        debug_print("\nDependency graph:")
+        for i in self.referenced_vars:
+            debug_print(i, "->", [str(x) for x in self.referenced_vars[i].outgoing])
+
+        # The reverse of the depth-first order is the topological sort
+        dfo = dict()
+        visited = set()
+        debug_print("\n")
+        def visit_fn(var):
+            debug_print(var, "has DFS number", len(dfo))
+            dfo[var] = len(dfo)
+
+        for name, v in self.referenced_vars.items():
+            self.do_default(v, False)
+            v.dfs(visited, visit_fn)
+
+        # Put higher DFS numbers and higher priorities first.  This
+        # places the clauses in topological order and places defaults
+        # after assignments and dependencies.
+        self.clauses.sort(key=lambda x: (-dfo[x.dest], -x.priority()))
+
+        debug_print("\nSorted clauses:")
+        for clause in self.clauses:
+            debug_print(clause)
+            clause.process()
+
+        debug_print("")
+        values = dict()
+        for name, v in self.referenced_vars.items():
+            debug_print("Evaluating", name)
+            values[name] = v.evaluate()
+
+        return values
+
     # semantic actions -------------
 
     def do_declaration(self, var):
         if (var in self.defined_vars):
-            raise Exception('variable "' + var + '" defined twice')
+            raise KconfigParserError('variable "' + var + '" defined twice')
 
         self.defined_vars.add(var.name)
 
@@ -201,9 +328,6 @@ class KconfigParser:
         data = KconfigData()
         parser = KconfigParser(data)
         parser.parse_file(fp)
-        if data.check_undefined():
-            raise KconfigParserError(parser, "there were undefined symbols")
-
         return data
 
     def __init__(self, data):
@@ -392,7 +516,6 @@ class KconfigParser:
               self.tok == TOK_SELECT or self.tok == TOK_BOOL or \
               self.tok == TOK_IMPLY:
             self.parse_property(var)
-        self.data.do_default(var, False)
 
         # for nicer error message
         if self.tok != TOK_SOURCE and self.tok != TOK_CONFIG and \
@@ -520,5 +643,4 @@ class KconfigParser:
 if __name__ == '__main__':
     fname = len(sys.argv) > 1 and sys.argv[1] or 'Kconfig.test'
     data = KconfigParser.parse(open(fname, 'r'))
-    for i in data.clauses:
-        print i
+    print data.compute_config()
-- 
1.8.3.1

  parent reply	other threads:[~2019-02-07 17:57 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-07 17:56 [Qemu-devel] [PATCH v6 00/51] Support Kconfig in QEMU Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 01/51] minikconfig: add parser skeleton Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 02/51] minikconfig: add AST Paolo Bonzini
2019-02-07 17:56 ` Paolo Bonzini [this message]
2019-02-07 17:56 ` [Qemu-devel] [PATCH 04/51] hw/display: make edid configurable Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 05/51] kconfig: introduce kconfig files Paolo Bonzini
2019-02-07 18:35   ` Thomas Huth
2019-02-20 15:42   ` Thomas Huth
2019-02-20 15:46     ` Paolo Bonzini
2019-02-20 16:55   ` Max Filippov
2019-02-20 17:44     ` Paolo Bonzini
2019-02-21 15:58   ` Stefano Garzarella
2019-02-07 17:56 ` [Qemu-devel] [PATCH 06/51] build: switch to Kconfig Paolo Bonzini
2019-02-21 15:44   ` Stefano Garzarella
2019-02-21 17:59     ` Paolo Bonzini
2019-02-21 20:15       ` Stefano Garzarella
2019-02-07 17:56 ` [Qemu-devel] [PATCH 07/51] minikconfig: implement allnoconfig and defconfig modes Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 08/51] kconfig: introduce CONFIG_TEST_DEVICES Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 09/51] ide: express dependencies with Kconfig Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 10/51] hw/pci/Makefile.objs: make pcie configurable Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 11/51] build: convert pci.mak to Kconfig Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 12/51] build: convert sound.mak " Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 13/51] build: convert usb.mak " Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 14/51] block: fix recursion in hw/block/dataplane Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 15/51] scsi: express dependencies with Kconfig Paolo Bonzini
2019-02-07 17:56 ` [Qemu-devel] [PATCH 16/51] isa: express dependencies with kconfig Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 17/51] i386: express dependencies with Kconfig Paolo Bonzini
2019-02-25  8:44   ` Stefano Garzarella
2019-02-26 13:23     ` Paolo Bonzini
2019-02-26 13:44       ` Markus Armbruster
2019-02-26 15:25         ` Paolo Bonzini
2019-02-26 17:24           ` Markus Armbruster
2019-02-26 15:42   ` Thomas Huth
2019-02-07 17:57 ` [Qemu-devel] [PATCH 18/51] i2c: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 19/51] ptimer: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 20/51] display: express dependencies with kconfig Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 21/51] hyperv: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 22/51] vfio: express vfio dependencies with Kconfig Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 23/51] virtio: express virtio " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 24/51] tpm: express " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 25/51] isa: express SuperIO " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 26/51] ssi: express dependencies with kconfig Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 27/51] sd: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 28/51] ipmi: " Paolo Bonzini
2019-02-20 14:06   ` Corey Minyard
2019-02-07 17:57 ` [Qemu-devel] [PATCH 29/51] i386-softmmu.mak: remove all CONFIG_* except boards definitions Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 30/51] ppc64: Express dependencies of 'pseries' and 'powernv' machines with kconfig Paolo Bonzini
2019-02-28 10:44   ` Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 31/51] ppc: Express dependencies of the 'prep' and '40p' " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 32/51] ppc: Express dependencies of the Mac " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 33/51] ppc: Express dependencies of the Sam460EX " Paolo Bonzini
2019-02-07 23:16   ` BALATON Zoltan
2019-02-08  4:30     ` Thomas Huth
2019-02-07 17:57 ` [Qemu-devel] [PATCH 34/51] ppc: Express dependencies of the embedded " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 35/51] alpha-softmmu.mak: express dependencies with Kconfig Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 36/51] cris-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 37/51] hppa-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 38/51] lm32-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 39/51] m68k-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 40/51] microblaze-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 41/51] moxie-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 42/51] nios2-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 43/51] or1k-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 44/51] s390x: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 45/51] sh4-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 46/51] sparc-softmmu.mak: " Paolo Bonzini
2019-02-08 17:33   ` Mark Cave-Ayland
2019-02-08 17:40     ` Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 47/51] sparc64-softmmu.mak: " Paolo Bonzini
2019-02-08 17:35   ` Mark Cave-Ayland
2019-02-07 17:57 ` [Qemu-devel] [PATCH 48/51] unicore32-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 49/51] xtensa-softmmu.mak: " Paolo Bonzini
2019-02-07 17:57 ` [Qemu-devel] [PATCH 50/51] .travis.yml: test that no-default-device builds do not regress Paolo Bonzini
2019-02-07 18:26   ` Thomas Huth
2019-02-07 17:57 ` [Qemu-devel] [PATCH 51/51] FIXME vhost: add more stubs Paolo Bonzini
2019-02-07 18:52 ` [Qemu-devel] [PATCH v6 00/51] Support Kconfig in QEMU no-reply
2019-02-07 18:56 ` no-reply
2019-02-07 19:37 ` Michael S. Tsirkin
2019-02-07 19:47 ` no-reply
2019-02-07 19:50 ` no-reply
2019-02-08  0:14 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1549562254-41157-4-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).