From patchwork Thu Feb 7 17:56:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1038215 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NmJINKAu"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43wR2w5btMz9s3q for ; Fri, 8 Feb 2019 05:01:44 +1100 (AEDT) Received: from localhost ([127.0.0.1]:44500 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grnza-0004fr-Mo for incoming@patchwork.ozlabs.org; Thu, 07 Feb 2019 13:01:42 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grnvv-0001wE-Ej for qemu-devel@nongnu.org; Thu, 07 Feb 2019 12:57:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1grnvr-0002Dc-W8 for qemu-devel@nongnu.org; Thu, 07 Feb 2019 12:57:55 -0500 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:46150) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1grnvr-000254-JE for qemu-devel@nongnu.org; Thu, 07 Feb 2019 12:57:51 -0500 Received: by mail-wr1-x443.google.com with SMTP id l9so762498wrt.13 for ; Thu, 07 Feb 2019 09:57:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=HuMqdnrT+QqbUjlywYFuxOfs2R7PfYyfuisg3d4Eiv8=; b=NmJINKAuCHPu5hU0KVC08cqZ4R3OEKPsW0RwSMFJldZR0hHC2HbClWAqvc0QLhopb3 ZWSonNosZZhUmpQ/tMgkY1nH7N8ZHZY9RENFtPeMw6137PMgYPQZJmiLErxoyxeQGYXT FezKe6SROhAoRDlMdF8ZhA4wm+wn5u1p2j1XD4mKiFSRmwYffIblHbS1CAgICB/ovNtm o2LhtlcrKQ2v9QT+dX7rXMFJJor/AJlL/dKNeMd9WiikwgNTbxT0VmTPtlP72NEliBOr qBnIom6PjZqPdzQ20k6NNXP8ZNN73vwuD1KWXz5pHMwNovwtAWcTF7SWMp4CI2pTbCH1 mT5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=HuMqdnrT+QqbUjlywYFuxOfs2R7PfYyfuisg3d4Eiv8=; b=lhMinl+AGc+5a2R94/f4uniDFk97xctHrXGZ+cmppolwXkvw6uyoyGjCUTNADrx5EN etzNln/1jLMurYq5FFWz67mJmfiTuxsSGQypULfuD509GZQ0Vs92A+CpbHgb/i3jIKLw I1sa7iJVM7mr7r1hNf5g9OJwXFeIAGVkesiRFMTEC2DPr//Uus1XV0dEZXAVWGYDWo/Z ECQ5fGj0fSyTIZyaLG9lqr3RLNigknJHDghjUzrE08dHJ+oC5CPU757PEpUomfiBvU6Y I21gUe11qMOC3IPAWYxgOzXPt8vm/yf1vpQ62eBIKnQHwsVi+bQDZAB2f9nQ9sfnL/Kh L/xw== X-Gm-Message-State: AHQUAuajhunDXGHLDB7/Ja16Iav96qQ3CH//0j/vBMFrPCH+39VmdU81 8C/Ewvh3JMDBDKpzrj1r56EqQNMW X-Google-Smtp-Source: AHgI3IZWNdWPNW0yPgjY1LIK19YwBPD6OtAF//ehVpDjIHtrPLbMvYwxZ2j+4wxWmLyNQ6TDP4Azaw== X-Received: by 2002:adf:dccd:: with SMTP id x13mr10060455wrm.162.1549562260631; Thu, 07 Feb 2019 09:57:40 -0800 (PST) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id q12sm12708982wmf.2.2019.02.07.09.57.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Feb 2019 09:57:40 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 7 Feb 2019 18:56:46 +0100 Message-Id: <1549562254-41157-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1549562254-41157-1-git-send-email-pbonzini@redhat.com> References: <1549562254-41157-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::443 Subject: [Qemu-devel] [PATCH 03/51] minikconfig: add semantic analysis X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: thuth@redhat.com, philmd@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 Message-Id: <20190123065618.3520-25-yang.zhong@intel.com> Signed-off-by: Paolo Bonzini --- 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()