Skip to content

Commit

Permalink
Make Generator class becomes ABC
Browse files Browse the repository at this point in the history
- The following member functions will return NotImplemented if not
  implemented in derived classes to let user aware that the function
  call is not functioning
  - write()
  - write_title()
  - gen_prologue()
  - inst_group_prologue()
  - inst_group_epilogue()
  - post_gen()

- The func function is set as abstract method, all the derived classes
  shoule have their own implementation

- The original func implementation is copied to DocGenerator and
  APITestGenerator and replace the call of base func() implementation

Signed-off-by: Jerry Zhang Jian <[email protected]>
  • Loading branch information
jerryzj authored and kito-cheng committed Jun 5, 2024
1 parent c783839 commit 910b3c8
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions rvv-intrinsic-generator/rvv_intrinsic_gen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Generator classes that controls structures of the output.
"""
from abc import ABC, abstractmethod
import os
import collections
import re
Expand All @@ -25,7 +26,7 @@
from enums import ToolChainType


class Generator():
class Generator(ABC):
"""
Base class for all generators.
"""
Expand All @@ -36,29 +37,23 @@ def __init__(self):
pass

def write(self, text):
pass
return NotImplemented

def write_title(self, text, link):
pass
return NotImplemented

def gen_prologue(self):
pass
return NotImplemented

def inst_group_prologue(self):
return ""
return NotImplemented

def inst_group_epilogue(self):
return ""
return NotImplemented

@abstractmethod
def func(self, inst_info, name, return_type, **kwargs):
# pylint: disable=unused-argument
# FIXME: inst_info is currently only used by RIFGenerator.
self.generated_functions_set.add(name)
args = ", ".join(map(lambda a: f"{a[1]} {a[0]}", kwargs.items()))
# "T * name" to "T *name"
args = args.replace("* ", "*")
s = f"{return_type} {name} ({args});\n"
return s
return NotImplemented

def function_group(self, template, title, link, op_list, type_list, sew_list,
lmul_list, decorator_list):
Expand All @@ -74,7 +69,7 @@ def function_group(self, template, title, link, op_list, type_list, sew_list,
decorator_list=decorator_list)

def start_group(self, group_name):
pass
return NotImplemented

@staticmethod
def func_name(name):
Expand Down Expand Up @@ -296,7 +291,7 @@ def report_summary(self):
\x1b[0mfunctions")

def post_gen(self):
pass
return NotImplemented


class DocGenerator(Generator):
Expand Down Expand Up @@ -358,7 +353,13 @@ def function_group(self, template, title, link, op_list, type_list, sew_list,

def func(self, inst_info, name, return_type, **kwargs):
name = Generator.func_name(name)
s = super().func(inst_info, name, return_type, **kwargs)
# pylint: disable=unused-argument
# FIXME: inst_info is currently only used by RIFGenerator.
self.generated_functions_set.add(name)
args = ", ".join(map(lambda a: f"{a[1]} {a[0]}", kwargs.items()))
# "T * name" to "T *name"
args = args.replace("* ", "*")
s = f"{return_type} {name} ({args});\n"
self.write(s)

def start_group(self, group_name):
Expand Down Expand Up @@ -517,10 +518,12 @@ def func(self, inst_info, name, return_type, **kwargs):
os.path.join(self.folder, test_file_name), mode, encoding="utf-8")

stripped_prefix_non_overloaded_func_name = non_overloaded_func_name[8:]
func_decl = super().func(inst_info,
"test_" + stripped_prefix_non_overloaded_func_name,
return_type, **kwargs)
func_decl = func_decl.replace(" (", "(")
non_overloaded_func_name = "test_" + stripped_prefix_non_overloaded_func_name
self.generated_functions_set.add(non_overloaded_func_name)
args = ", ".join(map(lambda a: f"{a[1]} {a[0]}", kwargs.items()))
# "T * name" to "T *name"
args = args.replace("* ", "*")
func_decl = f"{return_type} {non_overloaded_func_name}({args});\n"

# Strip redundant parameters in function declaration because the intrinsic
# requires an immediate to be provided to the parameter.
Expand Down

0 comments on commit 910b3c8

Please sign in to comment.