oppc/code: simplify exprs and regs
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 12 Jan 2024 19:24:22 +0000 (22:24 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/pc_code.py

index b0bfaede9ba1a7946d1046d3ac1cb99eeaa463f1..a4bbe66563fd0d6ca06d0d31574ad75e960c2752 100644 (file)
@@ -47,15 +47,12 @@ class CodeVisitor(pc_util.Visitor):
         if isinstance(node.rvalue, (pc_ast.GPR, pc_ast.FPR)):
             self.__regfetch[str(node.rvalue)].append(node.rvalue)
 
-        if str(node.lvalue) in self.__decls:
-            stmt = " ".join([
-                str(self[node.lvalue]),
-                "=",
-                str(self[node.rvalue]),
-            ])
-            self[node].emit(stmt=f"{stmt};")
-        else:
-            raise ValueError(node)
+        stmt = " ".join([
+            str(self[node.lvalue]),
+            "=",
+            str(self[node.rvalue]),
+        ])
+        self[node].emit(stmt=f"{stmt};")
 
     @pc_util.Hook(pc_ast.BinaryExpr)
     def BinaryExpr(self, node):
@@ -64,62 +61,30 @@ class CodeVisitor(pc_util.Visitor):
             self.__regfetch[str(node.left)].append(node.left)
         if isinstance(node.right, (pc_ast.GPR, pc_ast.FPR)):
             self.__regfetch[str(node.right)].append(node.left)
-        if isinstance(node.left, (pc_ast.GPR, pc_ast.FPR)):
-            left = f"oppc_reg_fetch({str(self[node.left])})"
-        else:
-            left = str(self[node.left])
-        if isinstance(node.right, (pc_ast.GPR, pc_ast.FPR)):
-            right = f"oppc_reg_fetch({str(self[node.right])})"
-        else:
-            right = str(self[node.right])
-        if isinstance(node.op, (pc_ast.Add, pc_ast.Sub,
-                    pc_ast.Mul, pc_ast.Div, pc_ast.Mod,
-                    pc_ast.Lt, pc_ast.Le,
-                    pc_ast.Eq, pc_ast.NotEq,
-                    pc_ast.Ge, pc_ast.Gt,
-                    pc_ast.LShift, pc_ast.RShift,
-                    pc_ast.BitAnd, pc_ast.BitOr, pc_ast.BitXor,
-                )):
-            op = {
-                pc_ast.Not: "~",
-                pc_ast.Add: "+",
-                pc_ast.Sub: "-",
-                pc_ast.Mul: "*",
-                pc_ast.Div: "/",
-                pc_ast.Mod: "%",
-                pc_ast.Lt: "<",
-                pc_ast.Le: "<=",
-                pc_ast.Eq: "==",
-                pc_ast.NotEq: "!=",
-                pc_ast.Ge: ">=",
-                pc_ast.Gt: ">",
-                pc_ast.LShift: "<<",
-                pc_ast.RShift: "<<",
-                pc_ast.BitAnd: "&",
-                pc_ast.BitOr: "|",
-                pc_ast.BitXor: "^",
-            }[node.op.__class__]
-            stmt = " ".join([left, op, right])
-            self[node].emit(stmt=f"({stmt})")
-        else:
-            raise ValueError(node)
+        special = (
+            pc_ast.MulS,
+            pc_ast.MulU,
+            pc_ast.DivT,
+            pc_ast.Sqrt,
+            pc_ast.BitConcat
+        )
+        if isinstance(node.op, special):
+            raise NotImplementedError(node)
+        stmt = " ".join([
+            str(self[node.left]),
+            str(self[node.op]),
+            str(self[node.right]),
+        ])
+        self[node].emit(stmt=f"({stmt})")
 
     @pc_util.Hook(pc_ast.UnaryExpr)
     def UnaryExpr(self, node):
         yield node
-        if isinstance(node.value, (pc_ast.GPR, pc_ast.FPR)):
-            value = f"oppc_reg_fetch({str(self[node.value])})"
-        else:
-            value = f"({str(self[node.value])})"
-        if isinstance(node.op, (pc_ast.Not, pc_ast.Add, pc_ast.Sub)):
-            op = {
-                pc_ast.Not: "~",
-                pc_ast.Add: "+",
-                pc_ast.Sub: "-",
-            }[node.op.__class__]
-            self[node].emit(stmt="".join([op, value]))
-        else:
-            raise ValueError(node)
+        stmt = "".join([
+            str(self[node.op]),
+            f"({str(self[node.value])})",
+        ])
+        self[node].emit(stmt=stmt)
 
     @pc_util.Hook(
             pc_ast.Not, pc_ast.Add, pc_ast.Sub,
@@ -132,6 +97,26 @@ class CodeVisitor(pc_util.Visitor):
         )
     def Op(self, node):
         yield node
+        op = {
+            pc_ast.Not: "~",
+            pc_ast.Add: "+",
+            pc_ast.Sub: "-",
+            pc_ast.Mul: "*",
+            pc_ast.Div: "/",
+            pc_ast.Mod: "%",
+            pc_ast.Lt: "<",
+            pc_ast.Le: "<=",
+            pc_ast.Eq: "==",
+            pc_ast.NotEq: "!=",
+            pc_ast.Ge: ">=",
+            pc_ast.Gt: ">",
+            pc_ast.LShift: "<<",
+            pc_ast.RShift: "<<",
+            pc_ast.BitAnd: "&",
+            pc_ast.BitOr: "|",
+            pc_ast.BitXor: "^",
+        }[node.__class__]
+        self[node].emit(stmt=op)
 
     @pc_util.Hook(pc_ast.BinLiteral, pc_ast.DecLiteral, pc_ast.HexLiteral)
     def Integer(self, node):
@@ -146,6 +131,16 @@ class CodeVisitor(pc_util.Visitor):
             raise ValueError(node)
         self[node].emit(stmt=f"UINT64_C({hex(int(node, base))})")
 
+    @pc_util.Hook(pc_ast.GPR)
+    def GPR(self, node):
+        yield node
+        self[node].emit(stmt=f"ctx->gpr[OPPC_GPR_{str(node)}]")
+
+    @pc_util.Hook(pc_ast.FPR)
+    def FPR(self, node):
+        yield node
+        self[node].emit(stmt=f"ctx->fpr[OPPC_FPR_{str(node)}]")
+
     @pc_util.Hook(pc_ast.Call.Name)
     def CallName(self, node):
         yield node