main
Odeinjul 11 months ago
parent 1c825df14e
commit 96175111c4
No known key found for this signature in database
GPG Key ID: E384228B2B38FFBB

@ -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
}

@ -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) 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) 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') out = result.stdout.split(b'\n')
print(str(result.returncode).encode())
if result.returncode != b'': if result.returncode != b'':
out.append(str(result.returncode).encode()) out.append(str(result.returncode).encode())
for i in range(len(out)-1, -1, -1): for i in range(len(out)-1, -1, -1):

@ -27,6 +27,7 @@ Ptr<GlobalVariable> GlobalVariable::create(std::string name, Ptr<Module> m, Ptr<
std::string GlobalVariable::print() std::string GlobalVariable::print()
{ {
std::cout << "globalval0" <<std::endl;
std::string global_val_ir; std::string global_val_ir;
global_val_ir += print_as_op(shared_from_this(), false); global_val_ir += print_as_op(shared_from_this(), false);
global_val_ir += " = "; global_val_ir += " = ";
@ -35,6 +36,7 @@ std::string GlobalVariable::print()
global_val_ir += " "; global_val_ir += " ";
global_val_ir += this->get_init()->print(); global_val_ir += this->get_init()->print();
return global_val_ir; return global_val_ir;
std::cout << "globalval0" <<std::endl;
} }
} }

@ -129,11 +129,14 @@ std::string Module::print()
std::string module_ir; std::string module_ir;
for ( auto global_val : this->global_list_) for ( auto global_val : this->global_list_)
{ {
std::cout << "global0" <<std::endl;
module_ir += global_val->print(); module_ir += global_val->print();
module_ir += "\n"; module_ir += "\n";
std::cout << "global1" <<std::endl;
} }
for ( auto func : this->function_list_) for ( auto func : this->function_list_)
{ {
std::cout << "func" <<std::endl;
module_ir += func->print(); module_ir += func->print();
module_ir += "\n"; module_ir += "\n";
} }

@ -114,27 +114,32 @@ void IRBuilder::BinaryExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::BinOp
break; break;
} }
} else { } else {
if (lhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_pointer_type()) {
std::cout << 2<<std::endl;
lhs = builder->create_load(lhs); lhs = builder->create_load(lhs);
} }
if (rhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) {
std::cout << 3<<std::endl;
rhs = builder->create_load(rhs); rhs = builder->create_load(rhs);
} }
if (lhs->get_type()->is_float_type() && auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
rhs->get_type()->is_integer_type()) { auto rhsFloat = (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(rhs);
if (lhsFloat && !rhsFloat) {
rhs = builder->create_sitofp(rhs, FLOAT_T); rhs = builder->create_sitofp(rhs, FLOAT_T);
} }
if (lhs->get_type()->is_integer_type() && if (!lhsFloat && rhsFloat) {
rhs->get_type()->is_float_type()) {
lhs = builder->create_sitofp(lhs, FLOAT_T); lhs = builder->create_sitofp(lhs, FLOAT_T);
} }
isFloat = lhs->get_type()->is_float_type(); isFloat = lhsFloat || rhsFloat;
switch (op) { switch (op) {
case SyntaxTree::BinOp::PLUS: case SyntaxTree::BinOp::PLUS:
if (isFloat) { if (isFloat) {
std::cout << 1<<std::endl;
tmpInst = builder->create_fadd(lhs, rhs); tmpInst = builder->create_fadd(lhs, rhs);
} else std::cout << 2 <<std::endl;
} else {
tmpInst = builder->create_iadd(lhs, rhs); tmpInst = builder->create_iadd(lhs, rhs);
}
break; break;
case SyntaxTree::BinOp::MINUS: case SyntaxTree::BinOp::MINUS:
if (isFloat) { if (isFloat) {
@ -233,21 +238,24 @@ void IRBuilder::BinaryCondExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::Bi
break; break;
} }
} else { } else {
if (lhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(lhs) == nullptr &&lhs->get_type()->is_pointer_type()) {
lhs = builder->create_load(lhs); lhs = builder->create_load(lhs);
} }
if (rhs->get_type()->is_pointer_type()) { std::cout << (dynamic_pointer_cast<Constant>(rhs)) <<std::endl;
std::cout << 2<<std::endl;
if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) {
rhs = builder->create_load(rhs); rhs = builder->create_load(rhs);
} }
if (lhs->get_type()->is_float_type() && std::cout << 2<<std::endl;
rhs->get_type()->is_integer_type()) { auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
auto rhsFloat = (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(rhs);
if (lhsFloat && !rhsFloat) {
rhs = builder->create_sitofp(rhs, FLOAT_T); rhs = builder->create_sitofp(rhs, FLOAT_T);
} }
if (lhs->get_type()->is_integer_type() && if (!lhsFloat && rhsFloat) {
rhs->get_type()->is_float_type()) {
lhs = builder->create_sitofp(lhs, FLOAT_T); lhs = builder->create_sitofp(lhs, FLOAT_T);
} }
isFloat = lhs->get_type()->is_float_type(); isFloat = lhsFloat && rhsFloat;
switch (op) { switch (op) {
case SyntaxTree::BinaryCondOp::LT: case SyntaxTree::BinaryCondOp::LT:
if (isFloat) { if (isFloat) {
@ -312,6 +320,7 @@ void IRBuilder::TypeConvert(Ptr<Value> origin, Ptr<Type> expected) {
if (type->is_pointer_type()) { if (type->is_pointer_type()) {
type = type->get_pointer_element_type(); type = type->get_pointer_element_type();
origin = builder->create_load(origin); origin = builder->create_load(origin);
tmpInst = origin;
} }
if (type == INT32_T && expected == INT1_T) { if (type == INT32_T && expected == INT1_T) {
@ -322,7 +331,6 @@ void IRBuilder::TypeConvert(Ptr<Value> origin, Ptr<Type> expected) {
tmpInst = builder->create_fcmp_ne(origin, CONST_FLOAT(0)); tmpInst = builder->create_fcmp_ne(origin, CONST_FLOAT(0));
return; return;
} }
if (type == INT32_T && expected == FLOAT_T) { if (type == INT32_T && expected == FLOAT_T) {
tmpInst = builder->create_sitofp(origin, expected); tmpInst = builder->create_sitofp(origin, expected);
return; return;
@ -331,7 +339,6 @@ void IRBuilder::TypeConvert(Ptr<Value> origin, Ptr<Type> expected) {
tmpInst = builder->create_fptosi(origin, expected); tmpInst = builder->create_fptosi(origin, expected);
return; return;
} }
} else { } else {
auto tmpInt = dynamic_pointer_cast<ConstantInt>(origin); auto tmpInt = dynamic_pointer_cast<ConstantInt>(origin);
auto tmpFloat = dynamic_pointer_cast<ConstantFloat>(origin); auto tmpFloat = dynamic_pointer_cast<ConstantFloat>(origin);
@ -375,6 +382,7 @@ void IRBuilder::visit(SyntaxTree::InitVal &node) {
// FINISH // FINISH
void IRBuilder::visit(SyntaxTree::FuncDef &node) { void IRBuilder::visit(SyntaxTree::FuncDef &node) {
auto funcRetType = GetParamType(node.ret_type); auto funcRetType = GetParamType(node.ret_type);
node.param_list->accept(*this); node.param_list->accept(*this);
auto funcType = FunctionType::create(funcRetType, funcFParam); auto funcType = FunctionType::create(funcRetType, funcFParam);
@ -393,7 +401,6 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) {
for (const auto &arg : funcArgs) { for (const auto &arg : funcArgs) {
if(para == para_end) if(para == para_end)
break; break;
auto name = (*para)->name; auto name = (*para)->name;
auto argAlloca = builder->create_alloca(arg->get_type()); auto argAlloca = builder->create_alloca(arg->get_type());
builder->create_store(arg, argAlloca); builder->create_store(arg, argAlloca);
@ -406,6 +413,7 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) {
retAlloca = builder->create_alloca(funcRetType); retAlloca = builder->create_alloca(funcRetType);
} }
node.body->accept(*this); node.body->accept(*this);
if (builder->get_insert_block()->get_terminator() == nullptr) { if (builder->get_insert_block()->get_terminator() == nullptr) {
if (funcRetType == INT32_T) { if (funcRetType == INT32_T) {
builder->create_store(CONST_INT(0), retAlloca); builder->create_store(CONST_INT(0), retAlloca);
@ -414,7 +422,6 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) {
} }
builder->create_br(retBB); builder->create_br(retBB);
} }
builder->set_insert_point(retBB); builder->set_insert_point(retBB);
if (funcRetType == VOID_T) { if (funcRetType == VOID_T) {
builder->create_void_ret(); builder->create_void_ret();
@ -455,8 +462,11 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) {
if (scope.in_global()) { if (scope.in_global()) {
if (!node.is_inited || (!node.array_length.empty() && node.initializers->elementList.empty())) { if (!node.is_inited || (!node.array_length.empty() && node.initializers->elementList.empty())) {
auto zeroInit = ConstantZero::create(varType, module); 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 { } else {
if (!node.array_length.empty()) { if (!node.array_length.empty()) {
std::vector<Ptr<Constant>> varInit; std::vector<Ptr<Constant>> varInit;
@ -472,20 +482,17 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) {
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst)); varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
} }
auto zeroInit = ConstantZero::create(varType, module); auto zeroInit = ConstantZero::create(varType, module);
identAlloca = GlobalVariable::create(node.name, module, arrayType, false, zeroInit); auto tmpInit = ConstantArray::create(static_pointer_cast<ArrayType>(varType), varInit);
for (int i = 0; i < varInit.size(); i++) { identAlloca = GlobalVariable::create(node.name, module, varType, node.is_constant, tmpInit);
auto index = CONST_INT(i); //std::cout <<"VarDef1"<<std::endl;
auto ptr = builder->create_gep(identAlloca, {CONST_INT(0), index});
builder->create_store(varInit[i], ptr);
}
//alter
//auto tmpInit = ConstantArray::create(static_pointer_cast<ArrayType>(varType), varInit);
//identAlloca = GlobalVariable::create(node.name, module, varType, false, tmpInit);
} else { } else {
//std::cout <<"VarDef2"<<std::endl;
if (node.is_constant) { if (node.is_constant) {
node.initializers->expr->accept(*this); node.initializers->expr->accept(*this);
TypeConvert(tmpInst, varType); TypeConvert(tmpInst, varType);
identAlloca = tmpInst; auto tmpInit = dynamic_pointer_cast<Constant>(tmpInst);
identAlloca = tmpInit;
} else { } else {
node.initializers->expr->accept(*this); node.initializers->expr->accept(*this);
TypeConvert(tmpInst, varType); TypeConvert(tmpInst, varType);
@ -539,12 +546,12 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) {
// FINISH // FINISH
void IRBuilder::visit(SyntaxTree::LVal &node) { void IRBuilder::visit(SyntaxTree::LVal &node) {
auto ident = scope.find(node.name, false); auto ident = scope.find(node.name, false);
if (!node.array_index.empty()) { if (!node.array_index.empty()) {
node.array_index[0]->accept(*this); node.array_index[0]->accept(*this);
auto constIndex = dynamic_pointer_cast<ConstantInt>(tmpInst); auto constIndex = dynamic_pointer_cast<ConstantInt>(tmpInst);
auto globalIdent = dynamic_pointer_cast<GlobalVariable>(ident); auto globalIdent = dynamic_pointer_cast<GlobalVariable>(ident);
if(globalIdent != nullptr && globalIdent->is_const() && constIndex == nullptr) { if(globalIdent != nullptr && globalIdent->is_const() && constIndex != nullptr) {
std::cout << "Const LVal "<<std::endl;
auto arrayInit = dynamic_pointer_cast<ConstantArray>(globalIdent->get_init()); auto arrayInit = dynamic_pointer_cast<ConstantArray>(globalIdent->get_init());
tmpInst = arrayInit->get_element_value(constIndex->get_value()); tmpInst = arrayInit->get_element_value(constIndex->get_value());
} else { } else {
@ -594,8 +601,9 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {
auto expectRetType = auto expectRetType =
builder->get_insert_block()->get_parent()->get_return_type(); builder->get_insert_block()->get_parent()->get_return_type();
TypeConvert(tmpInst, expectRetType); TypeConvert(tmpInst, expectRetType);
auto retValue = tmpInst; std::cout << "RET" <<std::endl;
builder->create_store(retValue, retAlloca); builder->create_store(tmpInst, retAlloca);
std::cout << "RET" <<std::endl;
} }
// every ret stmt only store the value and jump to the retBB // every ret stmt only store the value and jump to the retBB
builder->create_br(retBB); builder->create_br(retBB);
@ -606,6 +614,7 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {
void IRBuilder::visit(SyntaxTree::BlockStmt &node) { void IRBuilder::visit(SyntaxTree::BlockStmt &node) {
scope.enter(); scope.enter();
for (const auto &stmt : node.body) { for (const auto &stmt : node.body) {
std::cout << "Block" <<std::endl;
stmt->accept(*this); stmt->accept(*this);
} }
scope.exit(); scope.exit();

@ -54,6 +54,7 @@ int main(int argc, char *argv[])
m->set_file_name(filename); m->set_file_name(filename);
m->set_print_name(); m->set_print_name();
auto IR = m->print(); auto IR = m->print();
std::cout << "END OUT" <<std::endl;
std::ofstream output_stream; std::ofstream output_stream;
output_stream.open(output_llvm_file, std::ios::out); output_stream.open(output_llvm_file, std::ios::out);
output_stream << IR; output_stream << IR;

Loading…
Cancel
Save