From 38e1ad82557e2f7be50b6e19b8e706ec9ac828c4 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Fri, 12 Jan 2024 23:16:17 +0300 Subject: [PATCH] oppc/code: support if exprs --- src/openpower/oppc/__main__.py | 12 +++++++++++- src/openpower/oppc/pc_code.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/openpower/oppc/__main__.py b/src/openpower/oppc/__main__.py index fcc0e8f1..63a03eee 100644 --- a/src/openpower/oppc/__main__.py +++ b/src/openpower/oppc/__main__.py @@ -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) diff --git a/src/openpower/oppc/pc_code.py b/src/openpower/oppc/pc_code.py index a1cd4314..f014eaf1 100644 --- a/src/openpower/oppc/pc_code.py +++ b/src/openpower/oppc/pc_code.py @@ -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 -- 2.30.2