import os import subprocess from analyzer import Analyzer from compiler import Compiler from runner import Runner from pretty_print import Print_C from presenter import Presenter def get_sy_testcases(sub_dir=""): filelist = [] #not_file_list = ["063_sort_test3", "10_break", "55_sort_test3", "62_long_code"] not_file_list = [] for file in os.listdir(os.path.join("testcases", sub_dir)): if os.path.splitext(file)[1] == ".sy" and not(os.path.splitext(file)[0] in not_file_list): filelist.append(os.path.splitext(file)[0]) return sorted(filelist) class Tester: def __init__(self, a_scheme, is_trivial, testcases): self.scheme = a_scheme["scheme"] self.frontend_instr = a_scheme["frontend_instr"] self.emit_llvm_ir = a_scheme["emit_llvm_ir"] self.is_trivial = is_trivial self.compiler = Compiler(scheme=self.scheme, testcases=testcases) self.runner = Runner(scheme=self.scheme, testcases=testcases) self.analyzer = Analyzer(scheme=self.scheme, emit_llvm_ir=self.emit_llvm_ir, testcases=testcases) def generate_ir(self): for testcase in testcases: self.compiler.sy_to_ir(frontend_instr=self.frontend_instr, testcase=testcase) def compile(self): self.compiler.compile_all_tests(frontend_instr=self.frontend_instr, emit_llvm_ir=self.emit_llvm_ir) def compile_rubbish(self, category): self.compiler.compile_rubbish(frontend_instr=self.frontend_instr, category=category) def compile_reference(self, category): self.compiler.compile_reference(frontend_instr=self.frontend_instr, category=category) def run(self): self.runner.run_all_tests() def analyze(self): self.analyzer.analyze() def test(self): Print_C.print_header(f"[TESTING {self.scheme}]") self.compile() self.run() self.analyze() print() print() def test_ir(self): Print_C.print_header(f"[TESTING {self.scheme}] (IR only)") for testcase in testcases: Print_C.print_subheader(f"[Compiling {testcase} with {self.scheme}]") self.compiler.sy_to_ir(frontend_instr=self.frontend_instr, testcase=testcase) print() print() thu_compiler = "build/bin/thu_compiler " ustc_compiler = "build/bin/ustc_compiler " ustc_compiler_no_vec = "build/bin/ustc_compiler_no_vec " clang_llvm_scheme = {"scheme": "clang_llvm", "frontend_instr": "clang -x c -c -Ofast -mcpu=cortex-a72 -mfpu=neon -mfloat-abi=hard -S -emit-llvm -include {header} {sy} -o {ir}", "emit_llvm_ir": True} thu_llvm_scheme = {"scheme": "thu_llvm", "frontend_instr": thu_compiler + "-l {ir} {sy}", "emit_llvm_ir": True} thu_thu_scheme = {"scheme": "thu_thu", "frontend_instr": thu_compiler + "-o {asm} {sy}", "emit_llvm_ir": False} my_compiler = "build/sysy" my_scheme = {"scheme": "my", "frontend_instr": my_compiler + " -S {sy} -o {ir}", "emit_llvm_ir": False} llir_scheme = {"scheme": "llir", "frontend_instr": "clang -x c -c -fPIE -m32 -S -emit-llvm -include {header} {sy} -o {ir}", "emit_llvm_ir": True} # gcc -c -fPIC -o sylib.o -include sylib.h sylib.c && ar rcs libsysy_x86.a sylib.o # ustc_ustc_scheme = {"scheme": "ustc_ustc", # "frontend_instr": ustc_compiler + "-o {asm} {sy}", # "emit_llvm_ir": False} # ustc_ustc_no_vec_scheme = { # "scheme": "ustc_ustc_no_vec", # "frontend_instr": ustc_compiler_no_vec + "-o {asm} {sy}", # "emit_llvm_ir": False} # gcc_gcc_scheme = {"scheme": "gcc_gcc", # "frontend_instr": "gcc -x c -c -Ofast -mcpu=cortex-a72 -mfpu=neon -mfloat-abi=hard -S -include {header} {sy} -o {asm}", # "emit_llvm_ir": False} # ustc_llvm_scheme = {"scheme": "ustc_llvm", # generate ir only # "frontend_instr": ustc_compiler + "-emit -o {ir} {sy}", # "emit_llvm_ir": True} if __name__ == '__main__': category = "functional" testcases = get_sy_testcases(sub_dir=category) all_schemes = [clang_llvm_scheme, thu_llvm_scheme, thu_thu_scheme] # gcc_gcc_scheme, ustc_ustc_scheme, ustc_ustc_no_vec_scheme] testers = [] # Print_C.print_header("[Removing old data...]\n\n") # subprocess.run("rm -rf build/test_results/".split()) # subprocess.run("rm -rf build/output/".split()) # subprocess.run("rm -rf build/log/compile_log".split()) # subprocess.run("rm -rf build/log/run_log".split()) # subprocess.run("rm -rf build/log/test_result.log".split()) # mytester = Tester(my_scheme, True, testcases) # mytester.compile_rubbish(category=category) # mytester.run() clangtester = Tester(llir_scheme, True, testcases) clangtester.compile_reference(category=category) clangtester.run() # for scheme in all_schemes: # tester = Tester(scheme, is_trivial=True) # testers.append(tester) # tester.test() # # Tester(ustc_llvm_scheme).test_ir() # presenter = Presenter(schemes=[scheme["scheme"] for scheme in all_schemes], testcases=get_sy_testcases()) # presenter.present_all_testcases()