oppc/code: support if exprs
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 12 Jan 2024 20:16:17 +0000 (23:16 +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 fcc0e8f1854543fa1943bb9181a4488b2699be83..63a03eee5e7f328315839137c0988d66a28eed78 100644 (file)
@@ -53,7 +53,17 @@ for path in []: # glob.glob(f"{find_wiki_dir()}/../isa/*.mdwn"):
             raise exc
 
 code = """
-a <- ¬(RA)
+src <- [0]*64
+src[64-XLEN:63] <- (RS)
+result <- [0]*64
+do i = 0 to 1
+    n <- i * 32
+    result[n+0:n+7] <- 0
+    result[n+8:n+19] <- DPD_TO_BCD(src[n+12:n+21])
+    result[n+20:n+31] <- DPD_TO_BCD(src[n+22:n+31])
+RA <- result[64-XLEN:63]
+if a < b then RT <- (RA)
+else          RT <- (RB)
 """
 tree = parser.parse(code=code)
 print(tree)
index a1cd43146a113200a0bc2ad69a495c79e3e2174b..f014eaf1e02ecdd8dbf80058dc85850ff1816507 100644 (file)
@@ -200,6 +200,23 @@ class CodeVisitor(pc_util.Visitor):
             self[node].emit(stmt=stmt, level=level)
         self[node].emit(f"}}")
 
+    @pc_util.Hook(pc_ast.IfExpr)
+    def IfExpr(self, node):
+        yield node
+        stmt = " ".join([
+            "if",
+            str(self[node.test]),
+            "{",
+        ])
+        self[node].emit(stmt=stmt)
+        for (level, stmt) in self[node.body]:
+            self[node].emit(stmt=stmt, level=level)
+        if node.orelse:
+            self[node].emit("} else {")
+            for (level, stmt) in self[node.orelse]:
+                self[node].emit(stmt=stmt, level=level)
+        self[node].emit("}")
+
     @pc_util.Hook(pc_ast.Call.Name)
     def CallName(self, node):
         yield node