diff --git a/include/visitor.h b/include/visitor.h index fc56aea..39716c9 100644 --- a/include/visitor.h +++ b/include/visitor.h @@ -41,7 +41,7 @@ std::shared_ptr build_InstStore( std::shared_ptr parent_bb ); -std::shared_ptr build_InstAlloca(TypePtr_t type, std::shared_ptr parent_bb); +std::shared_ptr build_InstAlloca(const std::string &str, TypePtr_t type, std::shared_ptr parent_bb); std::shared_ptr build_InstBinary( InstTag inst_tag, diff --git a/scripts/manual-compile.py b/scripts/manual-compile.py index 951b43a..8c9ff32 100644 --- a/scripts/manual-compile.py +++ b/scripts/manual-compile.py @@ -1,17 +1,19 @@ import sys import os -assert len(sys.argv) > 2 -print("compile {}".format(sys.argv[1])) -fullpath = sys.argv[1] -libpath = sys.argv[2] -(filename, extname) = os.path.splitext(sys.argv[1]) +assert len(sys.argv) > 3 +print("compile {}".format(sys.argv[2])) +selector = sys.argv[1] +fullpath = sys.argv[2] +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") -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") +if selector != "my": + 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") os.system(f"llc --march=x86 --relocation-model=pic {filename}.ll -o {filename}.s") print(f"as --32 {filename}.s -o {filename}.o") diff --git a/scripts/simple-diff.sh b/scripts/simple-diff.sh new file mode 100755 index 0000000..90e08a7 --- /dev/null +++ b/scripts/simple-diff.sh @@ -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 \ No newline at end of file diff --git a/scripts/tester.py b/scripts/tester.py index 407a800..9a064ae 100644 --- a/scripts/tester.py +++ b/scripts/tester.py @@ -116,16 +116,19 @@ if __name__ == '__main__': 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()) - - Tester(my_scheme, True, testcases).compile_rubbish(category=category) - # Tester(llir_scheme, True, testcases).compile_reference(category=category) + # 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) diff --git a/src/visitor.cpp b/src/visitor.cpp index 8478756..bcf6b81 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -152,7 +152,7 @@ std::any Visitor::visitConstDef(SysyParser::ConstDefContext *ctx) { if (_scope_tab.get_level()) { // local const array // 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_); // first dim base, ptr = *alloca_ = &array[0] auto base_ptr = build_InstGEP(alloca_, {CONST0, CONST0}, _state.current_bb); @@ -205,7 +205,7 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) { } // local variable 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_); if (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); // local array 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_); if (ctx->initVal()) { _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_name = fparam_ctx->IDENT()->getText(); auto fparam = std::make_shared(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); _scope_tab.push_name(fparam_name, alloca_); _state.current_func->fparam_list.push_back(fparam); diff --git a/src/visitor_factory.cpp b/src/visitor_factory.cpp index f1e4a66..2b0a356 100644 --- a/src/visitor_factory.cpp +++ b/src/visitor_factory.cpp @@ -43,8 +43,9 @@ std::shared_ptr build_InstStore( } // a little bit different, we put all alloca in the head of each basic block -std::shared_ptr build_InstAlloca(TypePtr_t type, std::shared_ptr parent_bb) { +std::shared_ptr build_InstAlloca(const std::string & name, TypePtr_t type, std::shared_ptr parent_bb) { auto inst_alloca = std::make_shared(type, parent_bb); + inst_alloca->name = name; parent_bb->inst_list.insert(parent_bb->inst_list.begin(), inst_alloca); // parent_bb->inst_list.push_back(inst_alloca); return inst_alloca; diff --git a/src/visitor_llir_gen.cpp b/src/visitor_llir_gen.cpp index 5e66b9c..e8ac4f4 100644 --- a/src/visitor_llir_gen.cpp +++ b/src/visitor_llir_gen.cpp @@ -179,7 +179,7 @@ static void _gen_blocks(std::ostream &ostr, const std::list &bl assert(cond->ir_seqno >= 0); assert(bb_true->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; } @@ -401,6 +401,7 @@ static void _gen_blocks(std::ostream &ostr, const std::list &bl assert(inst->ir_seqno != -1); auto pointer_type = Type::asType(inst->type); ostr << "%" << inst->ir_seqno << " = alloca " << pointer_type->pointed_type->to_IR_string() << ", align 4"; + ostr << " ;" << inst->name; break; } case InstTag::Zext: {