diff --git a/.gitignore b/.gitignore index 3c73ba5..50c5e73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build/ .antlr/ *.log -__pycache__/ \ No newline at end of file +__pycache__/ +*.o \ No newline at end of file diff --git a/scripts/compiler.py b/scripts/compiler.py index 5398107..7cd0b36 100644 --- a/scripts/compiler.py +++ b/scripts/compiler.py @@ -2,8 +2,8 @@ import os import subprocess from pretty_print import Print_C -lib = "build/lib/libsysy.a" -header = "build/include/sylib.h" +lib = "tools/sylib/libsysy_x86.a" +header = "tools/sylib/sylib.h" class Compiler: def __init__(self, scheme, testcases): @@ -78,7 +78,11 @@ class Compiler: log_file = open(log, "a+") Print_C.print_procedure(f"Generating {self.scheme}.s") - subprocess.run(f"llc -O3 -march=arm -mcpu=cortex-a72 -float-abi=hard -filetype=asm {ir} -o {asm}".split(), stdout=log_file, stderr=log_file, bufsize=1) + # subprocess.run(f"llc -O3 -march=arm -mcpu=cortex-a72 -float-abi=hard -filetype=asm {ir} -o {asm}".split(), stdout=log_file, stderr=log_file, bufsize=1) + completed = subprocess.run(f"llc -O3 --relocation-model=pic {ir} -o {asm}".split(), stdout=log_file, stderr=log_file, bufsize=1) + if completed.returncode != 0: + Print_C.print_error(f"Generating {self.scheme}.s failed! See {log} | {ir}") + self.count_error += 1 log_file.close() @@ -91,8 +95,11 @@ class Compiler: log_file = open(log, "a+") Print_C.print_procedure(f"Generating {self.scheme}.o") - subprocess.run(f"as -march=armv7-a -mfloat-abi=hard {asm} -o {obj}".split(), stdout=log_file, stderr=log_file, bufsize=1) - + # subprocess.run(f"as -march=armv7-a -mfloat-abi=hard {asm} -o {obj}".split(), stdout=log_file, stderr=log_file, bufsize=1) + completed = subprocess.run(f"as {asm} -o {obj}".split(), stdout=log_file, stderr=log_file, bufsize=1) + if completed.returncode != 0: + Print_C.print_error(f"Generating {self.scheme}.o failed! See {log}") + self.count_error += 1 log_file.close() def obj_to_bin(self, testcase): @@ -103,8 +110,11 @@ class Compiler: log_file = open(log, "a+") Print_C.print_procedure(f"Generating {self.scheme}") - subprocess.run(f"clang -Ofast -marm -march=armv7-a -mfpu=neon -mfloat-abi=hard {obj} {lib} -o {bin}".split(), stdout=log_file, stderr=log_file, bufsize=1) - + # subprocess.run(f"clang -Ofast -marm -march=armv7-a -mfpu=neon -mfloat-abi=hard {obj} {lib} -o {bin}".split(), stdout=log_file, stderr=log_file, bufsize=1) + completed = subprocess.run(f"clang -Ofast -fPIE {obj} {lib} -o {bin}".split(), stdout=log_file, stderr=log_file, bufsize=1) + if completed.returncode != 0: + Print_C.print_error(f"Linking {self.scheme} failed! See {log}") + self.count_error += 1 log_file.close() @@ -148,5 +158,16 @@ class Compiler: if self.count_error >= 1: Print_C.print_error(f"Test script stopped due to too many errors") return + + def compile_reference(self, frontend_instr, category): + for testcase in self.testcases: + Print_C.print_subheader(f"[Compiling {self.scheme} | {testcase}]") + self.sy_to_ir(frontend_instr=frontend_instr, testcase=testcase, category=category) + self.ir_to_asm(testcase=testcase) + self.asm_to_obj(testcase=testcase) + self.obj_to_bin(testcase=testcase) + if self.count_error >= 1: + Print_C.print_error(f"Test script stopped due to too many errors") + return \ No newline at end of file diff --git a/scripts/tester.py b/scripts/tester.py index 8fed9f7..d1aabcb 100644 --- a/scripts/tester.py +++ b/scripts/tester.py @@ -36,6 +36,9 @@ class Tester: 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() @@ -84,6 +87,12 @@ 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 -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} @@ -103,7 +112,8 @@ my_scheme = {"scheme": "my", if __name__ == '__main__': - testcases = get_sy_testcases(sub_dir="functional_test") + 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") @@ -113,7 +123,8 @@ if __name__ == '__main__': subprocess.run("rm -rf build/log/run_log".split()) subprocess.run("rm -rf build/log/test_result.log".split()) - Tester(my_scheme, True, testcases).compile_rubbish(category="functional_test") + # Tester(my_scheme, True, testcases).compile_rubbish(category=category) + Tester(llir_scheme, True, testcases).compile_reference(category=category) # for scheme in all_schemes: # tester = Tester(scheme, is_trivial=True) diff --git a/tools/sylib/libsysy.a b/tools/sylib/libsysy.a new file mode 100644 index 0000000..85780e5 Binary files /dev/null and b/tools/sylib/libsysy.a differ diff --git a/tools/sylib/libsysy_x86.a b/tools/sylib/libsysy_x86.a new file mode 100644 index 0000000..8a5af1d Binary files /dev/null and b/tools/sylib/libsysy_x86.a differ diff --git a/tools/sylib/sylib.c b/tools/sylib/sylib.c new file mode 100644 index 0000000..717e8fe --- /dev/null +++ b/tools/sylib/sylib.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include"sylib.h" +struct timeval _sysy_start,_sysy_end; + +int _sysy_l1[_SYSY_N],_sysy_l2[_SYSY_N]; +int _sysy_h[_SYSY_N], _sysy_m[_SYSY_N],_sysy_s[_SYSY_N],_sysy_us[_SYSY_N]; +int _sysy_idx; + +/* Input & output functions */ +int getint(){int t; scanf("%d",&t); return t; } +int getch(){char c; scanf("%c",&c); return (int)c; } +float getfloat(){ + float n; + scanf("%a", &n); + return n; +} + +int getarray(int a[]){ + int n; + scanf("%d",&n); + for(int i=0;i +#include +#include +#ifdef __cplusplus +extern "C" { +#endif +/* Input & output functions */ +int getint(),getch(),getarray(int a[]); +void putint(int a),putch(int a),putarray(int n,int a[]); +// #define putf(fmt, ...) printf(fmt, __VA_ARGS__) // TODO? +/* Timing function implementation */ +#define starttime() _sysy_starttime(__LINE__) +#define stoptime() _sysy_stoptime(__LINE__) +#define _SYSY_N 1024 + +__attribute((constructor)) void before_main(); +__attribute((destructor)) void after_main(); +void _sysy_starttime(int lineno); +void _sysy_stoptime(int lineno); +#ifdef __cplusplus +} +#endif +#endif