From 96175111c4f88760dbc995d5c79071af98143a20 Mon Sep 17 00:00:00 2001 From: Odeinjul Date: Fri, 15 Dec 2023 14:48:25 +0800 Subject: [PATCH] Pass 1-6 --- Student/task1/ll/assign_hand.ll | 30 ++++++++++++ Student/task3/test.py | 1 + src/SysYFIR/GlobalVariable.cpp | 2 + src/SysYFIR/Module.cpp | 3 ++ src/SysYFIRBuilder/IRBuilder.cpp | 79 ++++++++++++++++++-------------- src/main.cpp | 1 + 6 files changed, 81 insertions(+), 35 deletions(-) diff --git a/Student/task1/ll/assign_hand.ll b/Student/task1/ll/assign_hand.ll index e69de29..0c40498 100644 --- a/Student/task1/ll/assign_hand.ll +++ b/Student/task1/ll/assign_hand.ll @@ -0,0 +1,30 @@ +define i32 @main() #0 { + ; alloc b and store 1.8 to b + %1 = alloca float, align 4 ;b + store float 0x3FFCCCCCC0000000, float* %1, align 4 + + ; alloc a[2] and store 2 to a[0] + %2 = alloca [2 x i32], align 4 ;a + %3 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0 ;a[0] + store i32 2, i32* %3 + + ; load b + %4 = load float, float* %1, align 4 + + ; load a[0] and convert to float + %5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0 ;a[0] + %6 = load i32, i32* %5, align 4 + %7 = sitofp i32 %6 to float ;a[0] to float + + ; calculate b * a[0] + %8 = fmul float %4, %7 + %9 = fptosi float %8 to i32 + + ; store to a[1] + %10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1 ;a[1] + store i32 %9, i32* %10, align 4; + %11 = load i32, i32* %10, align 4 + + ; return a[1] + ret i32 %11 +} diff --git a/Student/task3/test.py b/Student/task3/test.py index cede8f7..79e5bf0 100644 --- a/Student/task3/test.py +++ b/Student/task3/test.py @@ -30,6 +30,7 @@ def eval(EXE_PATH, TEST_BASE_PATH, optimization): subprocess.run(ExeGen_ptn.format(optimization, TEST_PATH, LL_PATH), shell=True, stderr=subprocess.PIPE) result = subprocess.run(Exe_ptn.format(TEST_PATH), shell=True, input=input_option, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out = result.stdout.split(b'\n') + print(str(result.returncode).encode()) if result.returncode != b'': out.append(str(result.returncode).encode()) for i in range(len(out)-1, -1, -1): diff --git a/src/SysYFIR/GlobalVariable.cpp b/src/SysYFIR/GlobalVariable.cpp index 7b5a74f..3838146 100644 --- a/src/SysYFIR/GlobalVariable.cpp +++ b/src/SysYFIR/GlobalVariable.cpp @@ -27,6 +27,7 @@ Ptr GlobalVariable::create(std::string name, Ptr m, Ptr< std::string GlobalVariable::print() { + std::cout << "globalval0" <get_init()->print(); return global_val_ir; + std::cout << "globalval0" <global_list_) { + std::cout << "global0" <print(); module_ir += "\n"; + std::cout << "global1" <function_list_) { + std::cout << "func" <print(); module_ir += "\n"; } diff --git a/src/SysYFIRBuilder/IRBuilder.cpp b/src/SysYFIRBuilder/IRBuilder.cpp index 64ac4eb..2397048 100644 --- a/src/SysYFIRBuilder/IRBuilder.cpp +++ b/src/SysYFIRBuilder/IRBuilder.cpp @@ -114,27 +114,32 @@ void IRBuilder::BinaryExprGen(Ptr lhs, Ptr rhs, SyntaxTree::BinOp break; } } else { - if (lhs->get_type()->is_pointer_type()) { + if (dynamic_pointer_cast(lhs) == nullptr && lhs->get_type()->is_pointer_type()) { + std::cout << 2<create_load(lhs); } - if (rhs->get_type()->is_pointer_type()) { + if (dynamic_pointer_cast(rhs) == nullptr && rhs->get_type()->is_pointer_type()) { + std::cout << 3<create_load(rhs); } - if (lhs->get_type()->is_float_type() && - rhs->get_type()->is_integer_type()) { + auto lhsFloat = (dynamic_pointer_cast(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast(lhs); + auto rhsFloat = (dynamic_pointer_cast(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast(rhs); + if (lhsFloat && !rhsFloat) { rhs = builder->create_sitofp(rhs, FLOAT_T); } - if (lhs->get_type()->is_integer_type() && - rhs->get_type()->is_float_type()) { + if (!lhsFloat && rhsFloat) { lhs = builder->create_sitofp(lhs, FLOAT_T); } - isFloat = lhs->get_type()->is_float_type(); + isFloat = lhsFloat || rhsFloat; switch (op) { case SyntaxTree::BinOp::PLUS: if (isFloat) { + std::cout << 1<create_fadd(lhs, rhs); - } else + std::cout << 2 <create_iadd(lhs, rhs); + } break; case SyntaxTree::BinOp::MINUS: if (isFloat) { @@ -233,21 +238,24 @@ void IRBuilder::BinaryCondExprGen(Ptr lhs, Ptr rhs, SyntaxTree::Bi break; } } else { - if (lhs->get_type()->is_pointer_type()) { + if (dynamic_pointer_cast(lhs) == nullptr &&lhs->get_type()->is_pointer_type()) { lhs = builder->create_load(lhs); } - if (rhs->get_type()->is_pointer_type()) { + std::cout << (dynamic_pointer_cast(rhs)) <(rhs) == nullptr && rhs->get_type()->is_pointer_type()) { rhs = builder->create_load(rhs); } - if (lhs->get_type()->is_float_type() && - rhs->get_type()->is_integer_type()) { + std::cout << 2<(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast(lhs); + auto rhsFloat = (dynamic_pointer_cast(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast(rhs); + if (lhsFloat && !rhsFloat) { rhs = builder->create_sitofp(rhs, FLOAT_T); } - if (lhs->get_type()->is_integer_type() && - rhs->get_type()->is_float_type()) { + if (!lhsFloat && rhsFloat) { lhs = builder->create_sitofp(lhs, FLOAT_T); } - isFloat = lhs->get_type()->is_float_type(); + isFloat = lhsFloat && rhsFloat; switch (op) { case SyntaxTree::BinaryCondOp::LT: if (isFloat) { @@ -312,6 +320,7 @@ void IRBuilder::TypeConvert(Ptr origin, Ptr expected) { if (type->is_pointer_type()) { type = type->get_pointer_element_type(); origin = builder->create_load(origin); + tmpInst = origin; } if (type == INT32_T && expected == INT1_T) { @@ -322,7 +331,6 @@ void IRBuilder::TypeConvert(Ptr origin, Ptr expected) { tmpInst = builder->create_fcmp_ne(origin, CONST_FLOAT(0)); return; } - if (type == INT32_T && expected == FLOAT_T) { tmpInst = builder->create_sitofp(origin, expected); return; @@ -331,7 +339,6 @@ void IRBuilder::TypeConvert(Ptr origin, Ptr expected) { tmpInst = builder->create_fptosi(origin, expected); return; } - } else { auto tmpInt = dynamic_pointer_cast(origin); auto tmpFloat = dynamic_pointer_cast(origin); @@ -375,6 +382,7 @@ void IRBuilder::visit(SyntaxTree::InitVal &node) { // FINISH void IRBuilder::visit(SyntaxTree::FuncDef &node) { + auto funcRetType = GetParamType(node.ret_type); node.param_list->accept(*this); auto funcType = FunctionType::create(funcRetType, funcFParam); @@ -393,7 +401,6 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) { for (const auto &arg : funcArgs) { if(para == para_end) break; - auto name = (*para)->name; auto argAlloca = builder->create_alloca(arg->get_type()); builder->create_store(arg, argAlloca); @@ -406,6 +413,7 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) { retAlloca = builder->create_alloca(funcRetType); } node.body->accept(*this); + if (builder->get_insert_block()->get_terminator() == nullptr) { if (funcRetType == INT32_T) { builder->create_store(CONST_INT(0), retAlloca); @@ -414,7 +422,6 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) { } builder->create_br(retBB); } - builder->set_insert_point(retBB); if (funcRetType == VOID_T) { builder->create_void_ret(); @@ -455,8 +462,11 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) { if (scope.in_global()) { if (!node.is_inited || (!node.array_length.empty() && node.initializers->elementList.empty())) { auto zeroInit = ConstantZero::create(varType, module); - identAlloca = GlobalVariable::create(node.name, module, varType, false, zeroInit); - + if (node.array_length.empty()) { + identAlloca = GlobalVariable::create(node.name, module, varType, false, zeroInit); + } else { + identAlloca = GlobalVariable::create(node.name, module, arrayType, false, zeroInit); + } } else { if (!node.array_length.empty()) { std::vector> varInit; @@ -472,20 +482,17 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) { varInit.push_back(dynamic_pointer_cast(tmpInst)); } auto zeroInit = ConstantZero::create(varType, module); - identAlloca = GlobalVariable::create(node.name, module, arrayType, false, zeroInit); - for (int i = 0; i < varInit.size(); i++) { - auto index = CONST_INT(i); - auto ptr = builder->create_gep(identAlloca, {CONST_INT(0), index}); - builder->create_store(varInit[i], ptr); - } - //alter - //auto tmpInit = ConstantArray::create(static_pointer_cast(varType), varInit); - //identAlloca = GlobalVariable::create(node.name, module, varType, false, tmpInit); + auto tmpInit = ConstantArray::create(static_pointer_cast(varType), varInit); + identAlloca = GlobalVariable::create(node.name, module, varType, node.is_constant, tmpInit); + //std::cout <<"VarDef1"<expr->accept(*this); TypeConvert(tmpInst, varType); - identAlloca = tmpInst; + auto tmpInit = dynamic_pointer_cast(tmpInst); + identAlloca = tmpInit; } else { node.initializers->expr->accept(*this); TypeConvert(tmpInst, varType); @@ -539,12 +546,12 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) { // FINISH void IRBuilder::visit(SyntaxTree::LVal &node) { auto ident = scope.find(node.name, false); - if (!node.array_index.empty()) { node.array_index[0]->accept(*this); auto constIndex = dynamic_pointer_cast(tmpInst); auto globalIdent = dynamic_pointer_cast(ident); - if(globalIdent != nullptr && globalIdent->is_const() && constIndex == nullptr) { + if(globalIdent != nullptr && globalIdent->is_const() && constIndex != nullptr) { + std::cout << "Const LVal "<(globalIdent->get_init()); tmpInst = arrayInit->get_element_value(constIndex->get_value()); } else { @@ -594,8 +601,9 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) { auto expectRetType = builder->get_insert_block()->get_parent()->get_return_type(); TypeConvert(tmpInst, expectRetType); - auto retValue = tmpInst; - builder->create_store(retValue, retAlloca); + std::cout << "RET" <create_store(tmpInst, retAlloca); + std::cout << "RET" <create_br(retBB); @@ -606,6 +614,7 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) { void IRBuilder::visit(SyntaxTree::BlockStmt &node) { scope.enter(); for (const auto &stmt : node.body) { + std::cout << "Block" <accept(*this); } scope.exit(); diff --git a/src/main.cpp b/src/main.cpp index a4232bd..5d5a62d 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,7 @@ int main(int argc, char *argv[]) m->set_file_name(filename); m->set_print_name(); auto IR = m->print(); + std::cout << "END OUT" <