compile and link on x86

This commit is contained in:
ridethepig 2023-05-08 22:58:12 +08:00
parent 4706c79a6d
commit ae32334ccc
7 changed files with 158 additions and 10 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ build/
.antlr/ .antlr/
*.log *.log
__pycache__/ __pycache__/
*.o

View File

@ -2,8 +2,8 @@ import os
import subprocess import subprocess
from pretty_print import Print_C from pretty_print import Print_C
lib = "build/lib/libsysy.a" lib = "tools/sylib/libsysy_x86.a"
header = "build/include/sylib.h" header = "tools/sylib/sylib.h"
class Compiler: class Compiler:
def __init__(self, scheme, testcases): def __init__(self, scheme, testcases):
@ -78,7 +78,11 @@ class Compiler:
log_file = open(log, "a+") log_file = open(log, "a+")
Print_C.print_procedure(f"Generating {self.scheme}.s") 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() log_file.close()
@ -91,8 +95,11 @@ class Compiler:
log_file = open(log, "a+") log_file = open(log, "a+")
Print_C.print_procedure(f"Generating {self.scheme}.o") 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() log_file.close()
def obj_to_bin(self, testcase): def obj_to_bin(self, testcase):
@ -103,8 +110,11 @@ class Compiler:
log_file = open(log, "a+") log_file = open(log, "a+")
Print_C.print_procedure(f"Generating {self.scheme}") 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() log_file.close()
@ -149,4 +159,15 @@ class Compiler:
Print_C.print_error(f"Test script stopped due to too many errors") Print_C.print_error(f"Test script stopped due to too many errors")
return 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

View File

@ -37,6 +37,9 @@ class Tester:
def compile_rubbish(self, category): def compile_rubbish(self, category):
self.compiler.compile_rubbish(frontend_instr=self.frontend_instr, category=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): def run(self):
self.runner.run_all_tests() self.runner.run_all_tests()
@ -84,6 +87,12 @@ my_scheme = {"scheme": "my",
"frontend_instr": my_compiler + " -S {sy} -o {ir}", "frontend_instr": my_compiler + " -S {sy} -o {ir}",
"emit_llvm_ir": False} "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", # ustc_ustc_scheme = {"scheme": "ustc_ustc",
# "frontend_instr": ustc_compiler + "-o {asm} {sy}", # "frontend_instr": ustc_compiler + "-o {asm} {sy}",
# "emit_llvm_ir": False} # "emit_llvm_ir": False}
@ -103,7 +112,8 @@ my_scheme = {"scheme": "my",
if __name__ == '__main__': 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] all_schemes = [clang_llvm_scheme, thu_llvm_scheme, thu_thu_scheme] # gcc_gcc_scheme, ustc_ustc_scheme, ustc_ustc_no_vec_scheme]
testers = [] testers = []
Print_C.print_header("[Removing old data...]\n\n") 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/run_log".split())
subprocess.run("rm -rf build/log/test_result.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: # for scheme in all_schemes:
# tester = Tester(scheme, is_trivial=True) # tester = Tester(scheme, is_trivial=True)

BIN
tools/sylib/libsysy.a Normal file

Binary file not shown.

BIN
tools/sylib/libsysy_x86.a Normal file

Binary file not shown.

89
tools/sylib/sylib.c Normal file
View File

@ -0,0 +1,89 @@
#include<stdio.h>
#include<stdarg.h>
#include<sys/time.h>
#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<n;i++)scanf("%d",&a[i]);
return n;
}
int getfarray(float a[]) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%a", &a[i]);
}
return n;
}
void putint(int a){ printf("%d",a);}
void putch(int a){ printf("%c",a); }
void putarray(int n,int a[]){
printf("%d:",n);
for(int i=0;i<n;i++)printf(" %d",a[i]);
printf("\n");
}
// void putfloat(float a) {
// printf("%a", a);
// }
void putfarray(int n, float a[]) {
printf("%d:", n);
for (int i = 0; i < n; i++) {
printf(" %a", a[i]);
}
printf("\n");
}
void putf(char a[], ...) {
va_list args;
va_start(args, a);
vfprintf(stdout, a, args);
va_end(args);
}
/* Timing function implementation */
__attribute((constructor)) void before_main(){
for(int i=0;i<_SYSY_N;i++)
_sysy_h[i] = _sysy_m[i]= _sysy_s[i] = _sysy_us[i] =0;
_sysy_idx=1;
}
__attribute((destructor)) void after_main(){
for(int i=1;i<_sysy_idx;i++){
fprintf(stderr,"Timer@%04d-%04d: %dH-%dM-%dS-%dus\n",\
_sysy_l1[i],_sysy_l2[i],_sysy_h[i],_sysy_m[i],_sysy_s[i],_sysy_us[i]);
_sysy_us[0]+= _sysy_us[i];
_sysy_s[0] += _sysy_s[i]; _sysy_us[0] %= 1000000;
_sysy_m[0] += _sysy_m[i]; _sysy_s[0] %= 60;
_sysy_h[0] += _sysy_h[i]; _sysy_m[0] %= 60;
}
fprintf(stderr,"TOTAL: %dH-%dM-%dS-%dus\n",_sysy_h[0],_sysy_m[0],_sysy_s[0],_sysy_us[0]);
}
void _sysy_starttime(int lineno){
_sysy_l1[_sysy_idx] = lineno;
gettimeofday(&_sysy_start,NULL);
}
void _sysy_stoptime(int lineno){
gettimeofday(&_sysy_end,NULL);
_sysy_l2[_sysy_idx] = lineno;
_sysy_us[_sysy_idx] += 1000000 * ( _sysy_end.tv_sec - _sysy_start.tv_sec ) + _sysy_end.tv_usec - _sysy_start.tv_usec;
_sysy_s[_sysy_idx] += _sysy_us[_sysy_idx] / 1000000 ; _sysy_us[_sysy_idx] %= 1000000;
_sysy_m[_sysy_idx] += _sysy_s[_sysy_idx] / 60 ; _sysy_s[_sysy_idx] %= 60;
_sysy_h[_sysy_idx] += _sysy_m[_sysy_idx] / 60 ; _sysy_m[_sysy_idx] %= 60;
_sysy_idx ++;
}

26
tools/sylib/sylib.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef __SYLIB_H_
#define __SYLIB_H_
#include<stdio.h>
#include<stdarg.h>
#include<sys/time.h>
#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