oppc/code: support simple constant unary exprs
authorDmitry Selyutin <ghostmansd@gmail.com>
Wed, 10 Jan 2024 21:09:37 +0000 (00:09 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/__main__.py
src/openpower/oppc/pc_code.py

index 91f5d4ba8c065778664aa20dac78c9beddefbed3..fcc0e8f1854543fa1943bb9181a4488b2699be83 100644 (file)
@@ -53,7 +53,7 @@ for path in []: # glob.glob(f"{find_wiki_dir()}/../isa/*.mdwn"):
             raise exc
 
 code = """
-a <- 0b0
+a <- ¬(RA)
 """
 tree = parser.parse(code=code)
 print(tree)
index 143a005550e94a5836c7becb08e138d42bcd83a1..74604f45f9eedc6d22ac79ec0a36ddd86a62ab27 100644 (file)
@@ -68,6 +68,27 @@ class CodeVisitor(pc_util.Visitor):
         yield node
         if isinstance(node.value, (pc_ast.GPR, pc_ast.FPR)):
             self.__regfetch[str(node.value)].append(node.value)
+            stmt = "".join([
+                str(self[node.op]),
+                f"oppc_reg_fetch({str(self[node.value])})",
+            ])
+        else:
+            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)
+    def Op(self, node):
+        yield node
+        mapping = {
+            pc_ast.Not: "~",
+            pc_ast.Add: "+",
+            pc_ast.Sub: "-",
+        }
+        stmt = mapping[node.__class__]
+        self[node].emit(stmt=stmt)
 
     @pc_util.Hook(pc_ast.BinLiteral, pc_ast.DecLiteral, pc_ast.HexLiteral)
     def Integer(self, node):
@@ -99,6 +120,10 @@ class CodeVisitor(pc_util.Visitor):
         self.__decls[str(node)].append(node)
         self[node].emit(stmt=str(node))
 
+    @pc_util.Hook(pc_ast.Node)
+    def Node(self, node):
+        raise NotImplementedError(type(node))
+
 
 def code(name, root):
     yield from CodeVisitor(name=name, root=root)