From 4706c79a6d2abfe2707c9f93ffa257f132093e62 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Mon, 8 May 2023 21:16:16 +0800 Subject: [PATCH] at least everything compiles --- include/llir_value.h | 3 +-- scripts/compiler.py | 4 ++-- scripts/tester.py | 8 ++++---- src/visitor.cpp | 26 +++++++++++++++++++------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/include/llir_value.h b/include/llir_value.h index a87abec..bdbe9a4 100644 --- a/include/llir_value.h +++ b/include/llir_value.h @@ -303,8 +303,7 @@ public: } else { LOG(WARNING) << "Unexpected pointer type" << pointer->to_string(); - aim_to = pointer; - panic("message"); + aim_to = nullptr; } element_type = extract_type(pointer, indices); operand_list.push_back(pointer); diff --git a/scripts/compiler.py b/scripts/compiler.py index 20dcfaa..5398107 100644 --- a/scripts/compiler.py +++ b/scripts/compiler.py @@ -65,7 +65,7 @@ class Compiler: Print_C.print_procedure(f"Generating {self.scheme}.ll") completed = subprocess.run(frontend_instr.format(header=header, sy=sy, ir=ir).split(), stdout=log_file, stderr=log_file, bufsize=1) if completed.returncode != 0: - Print_C.print_error(f"Generating {self.scheme}.ll failed! See {log}") + Print_C.print_error(f"Generating {self.scheme}.ll failed! See {log} | {sy}") self.count_error += 1 log_file.close() @@ -145,7 +145,7 @@ class Compiler: 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) - if self.count_error >= 5: + if self.count_error >= 1: Print_C.print_error(f"Test script stopped due to too many errors") return diff --git a/scripts/tester.py b/scripts/tester.py index b0684fe..8fed9f7 100644 --- a/scripts/tester.py +++ b/scripts/tester.py @@ -34,8 +34,8 @@ class Tester: def compile(self): self.compiler.compile_all_tests(frontend_instr=self.frontend_instr, emit_llvm_ir=self.emit_llvm_ir) - def compile_rubbish(self): - self.compiler.compile_rubbish(frontend_instr=self.frontend_instr, category="functional") + def compile_rubbish(self, category): + self.compiler.compile_rubbish(frontend_instr=self.frontend_instr, category=category) def run(self): self.runner.run_all_tests() @@ -103,7 +103,7 @@ my_scheme = {"scheme": "my", if __name__ == '__main__': - testcases = get_sy_testcases(sub_dir="functional") + testcases = get_sy_testcases(sub_dir="functional_test") 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 +113,7 @@ 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() + Tester(my_scheme, True, testcases).compile_rubbish(category="functional_test") # for scheme in all_schemes: # tester = Tester(scheme, is_trivial=True) diff --git a/src/visitor.cpp b/src/visitor.cpp index c10d38b..5a1f395 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -683,13 +683,13 @@ std::any Visitor::visitNumber(SysyParser::NumberContext *ctx) { std::any Visitor::visitIntConst(SysyParser::IntConstContext *ctx) { int const_int = 0; if (ctx->DECIMAL_CONST()) { - const_int = std::stoi(ctx->DECIMAL_CONST()->getText(), nullptr, 10); + const_int = std::stol(ctx->DECIMAL_CONST()->getText(), nullptr, 10); } else if (ctx->HEXADECIMAL_CONST()) { - const_int = std::stoi(ctx->HEXADECIMAL_CONST()->getText(), nullptr, 16); + const_int = std::stol(ctx->HEXADECIMAL_CONST()->getText(), nullptr, 16); } else if (ctx->OCTAL_CONST()) { - const_int = std::stoi(ctx->OCTAL_CONST()->getText(), nullptr, 8); + const_int = std::stol(ctx->OCTAL_CONST()->getText(), nullptr, 8); } return build_ConstantInt("", const_int); } @@ -730,6 +730,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { } }; case Type::TypeTag::PointerType: { + sysy_assert(0); if (ctx->exp().empty()) { // ??? Pointer // @retval: InstLoad @@ -743,6 +744,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { auto pointed_type = std::dynamic_pointer_cast(lval->type)->pointed_type; auto inst_load = build_InstLoad(lval, pointed_type, _state.current_bb); + LOG(INFO) << lval->to_string() << " " << lval->type->to_string(); auto ptr = build_InstGEP(inst_load, {CONST0}, _state.current_bb); pointed_type = std::dynamic_pointer_cast(ptr->type)->pointed_type; LOG(INFO) << pointed_type->to_string(); @@ -778,7 +780,11 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { } } case Type::TypeTag::ArrayType: { - sysy_assert(!ctx->exp().empty()); + if (ctx->exp().empty()) { + // passing an array to function + auto ptr = build_InstGEP(lval, {CONST0, CONST0}, _state.current_bb); + return ptr; + } // get &array[0] auto ptr = build_InstGEP(lval, {CONST0, CONST0}, _state.current_bb); auto pointed_type = std::dynamic_pointer_cast(ptr->type)->pointed_type; @@ -806,7 +812,12 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { return arr_elem_ptr; } else { - panic("Should be int"); + // return the address of an sub array + auto array_type = std::dynamic_pointer_cast(pointed_type); + auto dim_size = ConstantInt::make_shared(array_type->element_count); + auto inst_mul = build_InstBinary(InstTag::Mul, offset, dim_size, _state.current_bb); + auto arr_elem_ptr = build_InstGEP(ptr, {CONST0, offset}, _state.current_bb); + return arr_elem_ptr; } } default: @@ -879,8 +890,9 @@ std::any Visitor::visitFuncFParam(SysyParser::FuncFParamContext *ctx) { dim_list.push_back(exp_val->value); } auto array_type = ArrayType::build_from_list(dim_list); - auto true_array_type = std::make_shared(array_type); - return {true_array_type}; + // auto true_array_type = std::make_shared(array_type); + // return {true_array_type}; + return {array_type}; } }