From 037abe809bd7d841bbaa46130143ba31d436f47b Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Tue, 9 Jan 2024 21:29:45 +0300 Subject: [PATCH] oppc: simplify ternary conditionals --- src/openpower/oppc/pc_pseudocode.py | 30 ++++++++++------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/openpower/oppc/pc_pseudocode.py b/src/openpower/oppc/pc_pseudocode.py index 76058ecb..f6b5a56c 100644 --- a/src/openpower/oppc/pc_pseudocode.py +++ b/src/openpower/oppc/pc_pseudocode.py @@ -103,32 +103,22 @@ class PseudocodeVisitor(mdis.visitor.ContextVisitor): rvalue = str(self[node.rvalue]) if isinstance(node.rvalue, pc_ast.IfExpr): - # All right, this deserves an explanation. - # We basically convert T <- C ? A : B into this code: - # - # if C then - # T <- A - # else - # T <- B - # - # To make things work, we must ensure that objects are unique. - # Otherwise we'll reuse the bogus code already produced before. - (body, orelse) = map(lambda node: node.clone(), - (node.rvalue.body[0], node.rvalue.orelse[0])) - body = pc_ast.Scope([node.__class__(lvalue=node.lvalue.clone(), rvalue=body)]) - orelse = pc_ast.Scope([node.__class__(lvalue=node.lvalue.clone(), rvalue=orelse)]) - tmpnode = node.rvalue.clone(body=body, orelse=orelse) - walker = mdis.walker.Walker() - traverse(root=tmpnode, visitor=self, walker=walker) - for (level, stmt) in self[tmpnode]: - self[node].emit(stmt=stmt, level=level) + stmt = " ".join([ + lvalue, + mapping[node.__class__], + str(self[node.rvalue.test]), + "?", + str(self[node.rvalue.body[0]]), + ":", + str(self[node.rvalue.orelse[0]]), + ]) else: stmt = " ".join([ lvalue, mapping[node.__class__], rvalue, ]) - self[node].emit(stmt=stmt) + self[node].emit(stmt=stmt) @Hook(pc_ast.BinaryExpr) def BinaryExpr(self, node): -- 2.30.2