From: Dmitry Selyutin Date: Wed, 10 Jan 2024 20:49:22 +0000 (+0300) Subject: oppc/code: support simple constant assignments X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=115e1c0efe552dce44cbf48359102424264a1295;p=openpower-isa.git oppc/code: support simple constant assignments --- diff --git a/src/openpower/oppc/__main__.py b/src/openpower/oppc/__main__.py index 2d415f2e..91f5d4ba 100644 --- a/src/openpower/oppc/__main__.py +++ b/src/openpower/oppc/__main__.py @@ -53,15 +53,7 @@ for path in []: # glob.glob(f"{find_wiki_dir()}/../isa/*.mdwn"): raise exc code = """ -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] +a <- 0b0 """ tree = parser.parse(code=code) print(tree) diff --git a/src/openpower/oppc/pc_code.py b/src/openpower/oppc/pc_code.py index 3781d889..143a0055 100644 --- a/src/openpower/oppc/pc_code.py +++ b/src/openpower/oppc/pc_code.py @@ -28,6 +28,17 @@ class CodeVisitor(pc_util.Visitor): yield from self.__code[self.__root] yield from self.__code[self.__footer] + def __getitem__(self, node): + return self.__code[node] + + @pc_util.Hook(pc_ast.Scope) + def Scope(self, node): + yield node + with self[node]: + for subnode in node: + for (level, stmt) in self[subnode]: + self[node].emit(stmt=stmt, level=level) + @pc_util.Hook(pc_ast.AssignExpr, pc_ast.AssignIEAExpr) def AssignExpr(self, node): yield node @@ -36,6 +47,14 @@ 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};") + @pc_util.Hook(pc_ast.BinaryExpr) def BinaryExpr(self, node): yield node @@ -50,6 +69,19 @@ class CodeVisitor(pc_util.Visitor): if isinstance(node.value, (pc_ast.GPR, pc_ast.FPR)): self.__regfetch[str(node.value)].append(node.value) + @pc_util.Hook(pc_ast.BinLiteral, pc_ast.DecLiteral, pc_ast.HexLiteral) + def Integer(self, node): + yield node + if isinstance(node, pc_ast.BinLiteral): + base = 2 + elif isinstance(node, pc_ast.DecLiteral): + base = 10 + elif isinstance(node, pc_ast.HexLiteral): + base = 16 + else: + raise ValueError(node) + self[node].emit(stmt=f"UINT64_C({hex(int(node, base))})") + @pc_util.Hook(pc_ast.Call.Name) def CallName(self, node): yield node @@ -65,6 +97,7 @@ class CodeVisitor(pc_util.Visitor): def Symbol(self, node): yield node self.__decls[str(node)].append(node) + self[node].emit(stmt=str(node)) def code(name, root):