blob: 47f39fdb4c1a9ad8d818f3f6787c9f617bef1148 [file] [log] [blame]
#!/usr/bin/env python3
"""
Generate tests for Vector Instructions
"""
import os
import logging
import argparse
from collections import namedtuple
from pathlib import Path
from mako.lookup import TemplateLookup
import mako.exceptions
parser = argparse.ArgumentParser(
description='Generate tests for vector instructions.')
parser.add_argument('--template-path', dest='template_path',
help='path to templates', required=True)
parser.add_argument('-v', '--verbose', help='increase output verbosity',
action='store_true')
parser.add_argument('--op-code', dest='op_code',
help='Op-code', required=True)
parser.add_argument('--out-path', dest='out_path',
help='Path to output files', default='.')
template_name_lookup = {
'OPIVV':'opivv_test.tpl.cpp',
'OPIVI':'opivi_test.tpl.cpp',
'OPIVX':'opivx_test.tpl.cpp',
'VXUNARY0':'vxunary0_test.tpl.cpp'
}
parser.add_argument('--instruction-format',
action='append', choices=template_name_lookup.keys(), required=True)
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
def main():
""" Main routine for generating tests from templates."""
mylookup = TemplateLookup(directories=[args.template_path, "."])
template_names = [template_name_lookup.get(fmt, None) for fmt in args.instruction_format]
template_names = [template for template in template_names if template is not None]
template_jobs = []
TemplateJob = namedtuple("TemplateJob", "template outfile")
Path(args.out_path).mkdir(parents=True, exist_ok=True)
for opfmt, template_name in template_name_lookup.items():
if not opfmt in args.instruction_format:
continue
try:
template = mylookup.get_template(template_name)
except mako.exceptions.TopLevelLookupException:
parser.error("Template does not exist %s" % template_name)
outfile_name = "%s_%s" % (args.op_code, template_name.replace(".tpl", ""))
template_jobs.append(TemplateJob(template, outfile_name))
for template_job in template_jobs:
full_outfile_path = os.path.join(args.out_path, template_job.outfile)
with open(full_outfile_path, "w+") as outfile:
outfile.write(template_job.template.render(op_code=args.op_code))
if __name__ == "__main__":
main()