oppc/code: introduce header and footer
authorDmitry Selyutin <ghostmansd@gmail.com>
Wed, 10 Jan 2024 20:43:59 +0000 (23:43 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/pc_code.py

index 24f78d17e91640d27308f5c477e296b644756457..3781d8890515e511390d11eadab9be8aaaff94d1 100644 (file)
@@ -6,23 +6,27 @@ import openpower.oppc.pc_util as pc_util
 
 class CodeVisitor(pc_util.Visitor):
     def __init__(self, name, root):
-        self.__name = name
-        self.__code = pc_util.Code()
+        self.__root = root
+        self.__header = object()
+        self.__footer = object()
+        self.__code = collections.defaultdict(lambda: pc_util.Code())
         self.__decls = collections.defaultdict(list)
         self.__regfetch = collections.defaultdict(list)
         self.__regstore = collections.defaultdict(list)
 
         super().__init__(root=root)
 
-        self.__code.emit("void")
-        self.__code.emit(f"oppc_{name}(void) {{")
-        with self.__code:
+        self.__code[self.__header].emit("void")
+        self.__code[self.__header].emit(f"oppc_{name}(void) {{")
+        with self.__code[self.__header]:
             for decl in self.__decls:
-                self.__code.emit(f"uint64_t {decl};")
-        self.__code.emit(f"}}")
+                self.__code[self.__header].emit(f"uint64_t {decl};")
+        self.__code[self.__footer].emit(f"}}")
 
     def __iter__(self):
-        yield from self.__code
+        yield from self.__code[self.__header]
+        yield from self.__code[self.__root]
+        yield from self.__code[self.__footer]
 
     @pc_util.Hook(pc_ast.AssignExpr, pc_ast.AssignIEAExpr)
     def AssignExpr(self, node):