oppc/code: eliminate redundant pseudocode
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 14 Jan 2024 12:56:43 +0000 (15:56 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/pc_code.py

index 77bdd429a38011128ea79530680a0713c8702a65..ac68b48bd0d1d20994e7b5b6aad5cb39bcf54a0d 100644 (file)
@@ -51,10 +51,11 @@ class CodeVisitor(pc_util.Visitor):
     def __getitem__(self, node):
         return self.__code[node]
 
-    def transient(self, node, value="UINT64_C(0)", bits="(uint8_t)OPPC_XLEN"):
+    def transient(self, node,
+            value="UINT64_C(0)",
+            bits="(uint8_t)OPPC_XLEN"):
         transient = Transient(value=value, bits=bits)
-        with self.pseudocode(node=node):
-            self.traverse(root=transient)
+        self.traverse(root=transient)
         return transient
 
     def ccall(self, node, name, code, stmt=False):
@@ -71,8 +72,7 @@ class CodeVisitor(pc_util.Visitor):
 
         code = tuple(map(validate, code))
         ccall = CCall(name=name, code=code, stmt=stmt)
-        with self.pseudocode(node=node):
-            self.traverse(root=ccall)
+        self.traverse(root=ccall)
         return ccall
 
     @contextlib.contextmanager
@@ -125,8 +125,9 @@ class CodeVisitor(pc_util.Visitor):
                 self[node.lvalue],
                 rvalue,
             ])
-        for (level, stmt) in self[ccall]:
-            self[node].emit(stmt=stmt, level=level)
+        with self.pseudocode(node=node):
+            for (level, stmt) in self[ccall]:
+                self[node].emit(stmt=stmt, level=level)
 
     @pc_util.Hook(pc_ast.BinaryExpr)
     def BinaryExpr(self, node):
@@ -153,8 +154,9 @@ class CodeVisitor(pc_util.Visitor):
                 self[node.left],
                 self[node.right],
             ])
-        for (level, stmt) in self[ccall]:
-            self[node].emit(stmt=stmt, level=level)
+        with self.pseudocode(node=node):
+            for (level, stmt) in self[ccall]:
+                self[node].emit(stmt=stmt, level=level)
 
     @pc_util.Hook(pc_ast.UnaryExpr)
     def UnaryExpr(self, node):
@@ -162,8 +164,9 @@ class CodeVisitor(pc_util.Visitor):
         ccall = self.ccall(name=str(self[node.op]), node=node, code=[
             self[node.value],
         ])
-        for (level, stmt) in self[ccall]:
-            self[node].emit(stmt=stmt, level=level)
+        with self.pseudocode(node=node):
+            for (level, stmt) in self[ccall]:
+                self[node].emit(stmt=stmt, level=level)
 
     @pc_util.Hook(
             pc_ast.Not, pc_ast.Add, pc_ast.Sub,
@@ -216,8 +219,9 @@ class CodeVisitor(pc_util.Visitor):
             raise NotImplementedError()
         value = f"UINT64_C({fmt(value)})"
         transient = self.transient(node=node, value=value, bits=bits)
-        for (level, stmt) in self[transient]:
-            self[node].emit(stmt=stmt, level=level)
+        with self.pseudocode(node=node):
+            for (level, stmt) in self[transient]:
+                self[node].emit(stmt=stmt, level=level)
 
     @pc_util.Hook(Transient)
     def Transient(self, node):
@@ -276,8 +280,9 @@ class CodeVisitor(pc_util.Visitor):
         yield node
         (value, bits) = ("OPPC_XLEN", "(uint8_t)OPPC_XLEN")
         transient = self.transient(node=node, value=value, bits=bits)
-        for (level, stmt) in self[transient]:
-            self[node].emit(stmt=stmt, level=level)
+        with self.pseudocode(node=node):
+            for (level, stmt) in self[transient]:
+                self[node].emit(stmt=stmt, level=level)
 
     @pc_util.Hook(pc_ast.SubscriptExpr)
     def SubscriptExpr(self, node):