fix cond gen

This commit is contained in:
ridethepig 2023-05-15 10:20:40 +08:00
parent 391b529960
commit 436d22b452
7 changed files with 39 additions and 24 deletions

View File

@ -41,7 +41,7 @@ std::shared_ptr<InstStore> build_InstStore(
std::shared_ptr<BasicBlock> parent_bb std::shared_ptr<BasicBlock> parent_bb
); );
std::shared_ptr<InstAlloca> build_InstAlloca(TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb); std::shared_ptr<InstAlloca> build_InstAlloca(const std::string &str, TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstBinary> build_InstBinary( std::shared_ptr<InstBinary> build_InstBinary(
InstTag inst_tag, InstTag inst_tag,

View File

@ -1,17 +1,19 @@
import sys import sys
import os import os
assert len(sys.argv) > 2 assert len(sys.argv) > 3
print("compile {}".format(sys.argv[1])) print("compile {}".format(sys.argv[2]))
fullpath = sys.argv[1] selector = sys.argv[1]
libpath = sys.argv[2] fullpath = sys.argv[2]
(filename, extname) = os.path.splitext(sys.argv[1]) libpath = sys.argv[3]
(filename, extname) = os.path.splitext(fullpath)
''' '''
python3 scripts/manual-compile.py build/manual-test/55_sort_test1.sy tools/sylib/libsysy_x86.a python3 my|clang scripts/manual-compile.py build/manual-test/55_sort_test1.sy tools/sylib/libsysy_x86.a
''' '''
# os.system(f"clang -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin") # os.system(f"clang -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin")
print(f"clang -x c -c -m32 -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin") if selector != "my":
os.system(f"clang -x c -c -m32 -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin") print(f"clang -x c -c -m32 -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin")
os.system(f"clang -x c -c -m32 -emit-llvm -S {fullpath} -o {filename}.ll -O0 -fno-builtin")
print(f"llc --march=x86 --relocation-model=pic {filename}.ll -o {filename}.s") print(f"llc --march=x86 --relocation-model=pic {filename}.ll -o {filename}.s")
os.system(f"llc --march=x86 --relocation-model=pic {filename}.ll -o {filename}.s") os.system(f"llc --march=x86 --relocation-model=pic {filename}.ll -o {filename}.s")
print(f"as --32 {filename}.s -o {filename}.o") print(f"as --32 {filename}.s -o {filename}.o")

8
scripts/simple-diff.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
set -e
basedir="build/output"
for kase in `ls $basedir`
do
echo "$basedir/$kase"
diff $basedir/$kase/my.out $basedir/$kase/llir.out
done

View File

@ -116,16 +116,19 @@ if __name__ == '__main__':
testcases = get_sy_testcases(sub_dir=category) 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")
subprocess.run("rm -rf build/test_results/".split()) # subprocess.run("rm -rf build/test_results/".split())
subprocess.run("rm -rf build/output/".split()) # subprocess.run("rm -rf build/output/".split())
subprocess.run("rm -rf build/log/compile_log".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/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=category)
# Tester(llir_scheme, True, testcases).compile_reference(category=category)
# 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: # for scheme in all_schemes:
# tester = Tester(scheme, is_trivial=True) # tester = Tester(scheme, is_trivial=True)
# testers.append(tester) # testers.append(tester)

View File

@ -152,7 +152,7 @@ std::any Visitor::visitConstDef(SysyParser::ConstDefContext *ctx) {
if (_scope_tab.get_level()) { if (_scope_tab.get_level()) {
// local const array // local const array
// Keep a pointer to the base address, alloca_ = &array, *alloca_ = &array[0] // Keep a pointer to the base address, alloca_ = &array, *alloca_ = &array[0]
auto alloca_ = build_InstAlloca(array_type, _state.current_bb); auto alloca_ = build_InstAlloca(const_name , array_type, _state.current_bb);
_scope_tab.push_name(const_name, alloca_); _scope_tab.push_name(const_name, alloca_);
// first dim base, ptr = *alloca_ = &array[0] // first dim base, ptr = *alloca_ = &array[0]
auto base_ptr = build_InstGEP(alloca_, {CONST0, CONST0}, _state.current_bb); auto base_ptr = build_InstGEP(alloca_, {CONST0, CONST0}, _state.current_bb);
@ -205,7 +205,7 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) {
} }
// local variable // local variable
else { else {
auto alloca_ = build_InstAlloca(TypeHelper::TYPE_I32, _state.current_bb); auto alloca_ = build_InstAlloca(var_name, TypeHelper::TYPE_I32, _state.current_bb);
_scope_tab.push_name(var_name, alloca_); _scope_tab.push_name(var_name, alloca_);
if (ctx->initVal()) { if (ctx->initVal()) {
auto init_val = any_to_Value(visitInitVal(ctx->initVal())); auto init_val = any_to_Value(visitInitVal(ctx->initVal()));
@ -224,7 +224,7 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) {
auto array_type = ArrayType::build_from_list(dim_list); auto array_type = ArrayType::build_from_list(dim_list);
// local array // local array
if (_scope_tab.get_level()) { if (_scope_tab.get_level()) {
auto alloca_ = build_InstAlloca(array_type, _state.current_bb); auto alloca_ = build_InstAlloca(var_name, array_type, _state.current_bb);
_scope_tab.push_name(var_name, alloca_); _scope_tab.push_name(var_name, alloca_);
if (ctx->initVal()) { if (ctx->initVal()) {
_state.arr_dim_index = 0; _state.arr_dim_index = 0;
@ -891,7 +891,7 @@ std::any Visitor::visitFuncFParams(SysyParser::FuncFParamsContext *ctx) {
auto fparam_type = any_to_Type(visitFuncFParam(fparam_ctx)); auto fparam_type = any_to_Type(visitFuncFParam(fparam_ctx));
auto fparam_name = fparam_ctx->IDENT()->getText(); auto fparam_name = fparam_ctx->IDENT()->getText();
auto fparam = std::make_shared<FParam>(fparam_name, fparam_type); auto fparam = std::make_shared<FParam>(fparam_name, fparam_type);
auto alloca_ = build_InstAlloca(fparam_type, _state.current_bb); auto alloca_ = build_InstAlloca(fparam_name, fparam_type, _state.current_bb);
build_InstStore(fparam, alloca_, _state.current_bb); build_InstStore(fparam, alloca_, _state.current_bb);
_scope_tab.push_name(fparam_name, alloca_); _scope_tab.push_name(fparam_name, alloca_);
_state.current_func->fparam_list.push_back(fparam); _state.current_func->fparam_list.push_back(fparam);

View File

@ -43,8 +43,9 @@ std::shared_ptr<InstStore> build_InstStore(
} }
// a little bit different, we put all alloca in the head of each basic block // a little bit different, we put all alloca in the head of each basic block
std::shared_ptr<InstAlloca> build_InstAlloca(TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb) { std::shared_ptr<InstAlloca> build_InstAlloca(const std::string & name, TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb) {
auto inst_alloca = std::make_shared<InstAlloca>(type, parent_bb); auto inst_alloca = std::make_shared<InstAlloca>(type, parent_bb);
inst_alloca->name = name;
parent_bb->inst_list.insert(parent_bb->inst_list.begin(), inst_alloca); parent_bb->inst_list.insert(parent_bb->inst_list.begin(), inst_alloca);
// parent_bb->inst_list.push_back(inst_alloca); // parent_bb->inst_list.push_back(inst_alloca);
return inst_alloca; return inst_alloca;

View File

@ -179,7 +179,7 @@ static void _gen_blocks(std::ostream &ostr, const std::list<BasicBlockPtr_t> &bl
assert(cond->ir_seqno >= 0); assert(cond->ir_seqno >= 0);
assert(bb_true->ir_seqno >= 0); assert(bb_true->ir_seqno >= 0);
assert(bb_false->ir_seqno >= 0); assert(bb_false->ir_seqno >= 0);
ostr << "i1 " << cond->ir_seqno << ", label %" << bb_true->ir_seqno << ", label %" << bb_false->ir_seqno; ostr << "i1 %" << cond->ir_seqno << ", label %" << bb_true->ir_seqno << ", label %" << bb_false->ir_seqno;
} }
break; break;
} }
@ -401,6 +401,7 @@ static void _gen_blocks(std::ostream &ostr, const std::list<BasicBlockPtr_t> &bl
assert(inst->ir_seqno != -1); assert(inst->ir_seqno != -1);
auto pointer_type = Type::asType<PointerType>(inst->type); auto pointer_type = Type::asType<PointerType>(inst->type);
ostr << "%" << inst->ir_seqno << " = alloca " << pointer_type->pointed_type->to_IR_string() << ", align 4"; ostr << "%" << inst->ir_seqno << " = alloca " << pointer_type->pointed_type->to_IR_string() << ", align 4";
ostr << " ;" << inst->name;
break; break;
} }
case InstTag::Zext: { case InstTag::Zext: {