oppc/code: emit only function body
authorDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:09:45 +0000 (22:09 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:08 +0000 (22:10 +0300)
src/openpower/oppc/pc_code.py

index a178a65c133551b1a55d73b5af7626f645a99448..d490a85dbc3019d921c7b6307e5b7a1f33e69c92 100644 (file)
@@ -32,6 +32,9 @@ class Instruction(pc_ast.Node):
 
 class CodeVisitor(pc_util.Visitor):
     def __init__(self, insn, root):
+        if not isinstance(root, pc_ast.Scope):
+            raise ValueError(root)
+
         self.__root = root
         self.__insn = insn
         self.__decls = set()
@@ -44,11 +47,8 @@ class CodeVisitor(pc_util.Visitor):
 
         super().__init__(root=root)
 
-        self.__code[self.__header].emit(stmt="void")
-        self.__code[self.__header].emit(stmt=f"oppc_{insn.name}(struct oppc_value const *insn) {{")
-        with self.__code[self.__header]:
-            for decl in self.__decls:
-                self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};")
+        for decl in self.__decls:
+            self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};")
         decls = sorted(filter(lambda decl: decl in insn.fields, self.__decls))
         if decls:
             self.__code[self.__header].emit()
@@ -58,9 +58,8 @@ class CodeVisitor(pc_util.Visitor):
             symbol = pc_ast.Symbol(decl)
             assign = pc_ast.AssignExpr(lvalue=symbol, rvalue=transient)
             self.traverse(root=assign)
-            with self[self.__header]:
-                for (level, stmt) in self[assign]:
-                    self[self.__header].emit(stmt=stmt, level=level)
+            for (level, stmt) in self[assign]:
+                self[self.__header].emit(stmt=stmt, level=level)
             for (lbit, rbit) in enumerate(insn.fields[decl]):
                 lsymbol = pc_ast.Symbol(decl)
                 rsymbol = Instruction()
@@ -70,19 +69,17 @@ class CodeVisitor(pc_util.Visitor):
                 rvalue = pc_ast.SubscriptExpr(index=rindex, subject=rsymbol)
                 assign = pc_ast.AssignExpr(lvalue=lvalue, rvalue=rvalue)
                 self.traverse(root=assign)
-                with self[self.__header]:
-                    for (level, stmt) in self[assign]:
-                        self[self.__header].emit(stmt=stmt, level=level)
+                for (level, stmt) in self[assign]:
+                    self[self.__header].emit(stmt=stmt, level=level)
             self.__code[self.__header].emit()
         if decls:
             self.__code[self.__header].emit()
-        self.__code[self.__footer].emit(stmt=f"}}")
-        self.__code[self.__footer].emit()
 
     def __iter__(self):
-        yield from self.__code[self.__header]
-        yield from self.__code[self.__root]
-        yield from self.__code[self.__footer]
+        yield from self[self.__header]
+        for (level, stmt) in self[self.__root]:
+            yield ((level - 1), stmt)
+        yield from self[self.__footer]
 
     def __getitem__(self, node):
         return self.__code[node]