oppc/code: support ternary conditionals
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 12 Jan 2024 20:23:36 +0000 (23:23 +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 63a03eee5e7f328315839137c0988d66a28eed78..126125d22ba9939367e6727fded56b261e7be3de 100644 (file)
@@ -62,6 +62,7 @@ do i = 0 to 1
     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]
+RA <- (C ? A : B)
 if a < b then RT <- (RA)
 else          RT <- (RB)
 """
index f014eaf1e02ecdd8dbf80058dc85850ff1816507..bf16efaa184311ad73851e1d50b08511dc972117 100644 (file)
@@ -47,13 +47,23 @@ class CodeVisitor(pc_util.Visitor):
         if isinstance(node.rvalue, (pc_ast.GPR, pc_ast.FPR)):
             self.__regfetch[str(node.rvalue)].append(node.rvalue)
 
+        rvalue = str(self[node.rvalue])
+        if isinstance(node.rvalue, pc_ast.IfExpr):
+            rvalue = " ".join([
+                str(self[node.rvalue.test]),
+                "?",
+                str(self[node.rvalue.body[0]]),
+                ":",
+                str(self[node.rvalue.orelse[0]]),
+            ])
+
         if isinstance(node.lvalue, pc_ast.SubscriptExpr):
             index = str(self[node.lvalue.index])
             subject = str(self[node.lvalue.subject])
             args = ", ".join([
                 f"&{subject}",
                 index,
-                str(self[node.rvalue]),
+                rvalue,
             ])
             self[node].emit(f"oppc_subscript_assign({args});")
         elif isinstance(node.lvalue, pc_ast.RangeSubscriptExpr):
@@ -64,14 +74,14 @@ class CodeVisitor(pc_util.Visitor):
                 f"&{subject}",
                 start,
                 end,
-                str(self[node.rvalue]),
+                rvalue,
             ])
             self[node].emit(f"oppc_range_subscript_assign({args});")
         else:
             stmt = " ".join([
                 str(self[node.lvalue]),
                 "=",
-                str(self[node.rvalue]),
+                rvalue,
             ])
             self[node].emit(stmt=f"{stmt};")