oppc/code: support simple constant assignments
authorDmitry Selyutin <ghostmansd@gmail.com>
Wed, 10 Jan 2024 20:49:22 +0000 (23:49 +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 2d415f2efcb82d4c296b18bcbc32bd991251b33a..91f5d4ba8c065778664aa20dac78c9beddefbed3 100644 (file)
@@ -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)
index 3781d8890515e511390d11eadab9be8aaaff94d1..143a005550e94a5836c7becb08e138d42bcd83a1 100644 (file)
@@ -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):