|
|
@ -115,11 +115,9 @@ void IRBuilder::BinaryExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::BinOp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (dynamic_pointer_cast<Constant>(lhs) == nullptr && 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 (dynamic_pointer_cast<Constant>(rhs) == nullptr && 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
|
|
|
|
auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
|
|
|
@ -134,9 +132,7 @@ void IRBuilder::BinaryExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::BinOp
|
|
|
|
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);
|
|
|
|
std::cout << 2 <<std::endl;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
tmpInst = builder->create_iadd(lhs, rhs);
|
|
|
|
tmpInst = builder->create_iadd(lhs, rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -241,12 +237,9 @@ void IRBuilder::BinaryCondExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::Bi
|
|
|
|
if (dynamic_pointer_cast<Constant>(lhs) == nullptr &&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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()) {
|
|
|
|
if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) {
|
|
|
|
rhs = builder->create_load(rhs);
|
|
|
|
rhs = builder->create_load(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << 2<<std::endl;
|
|
|
|
|
|
|
|
auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
|
|
|
|
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);
|
|
|
|
auto rhsFloat = (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(rhs);
|
|
|
|
if (lhsFloat && !rhsFloat) {
|
|
|
|
if (lhsFloat && !rhsFloat) {
|
|
|
@ -442,7 +435,7 @@ void IRBuilder::visit(SyntaxTree::FuncFParamList &node) {
|
|
|
|
|
|
|
|
|
|
|
|
// FINISH
|
|
|
|
// FINISH
|
|
|
|
void IRBuilder::visit(SyntaxTree::FuncParam &node) {
|
|
|
|
void IRBuilder::visit(SyntaxTree::FuncParam &node) {
|
|
|
|
auto tmpType = GetParamType(node.param_type, node.array_index.empty());
|
|
|
|
auto tmpType = GetParamType(node.param_type, !node.array_index.empty());
|
|
|
|
funcFParam.push_back(tmpType);
|
|
|
|
funcFParam.push_back(tmpType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -475,19 +468,17 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) {
|
|
|
|
TypeConvert(tmpInst, varType);
|
|
|
|
TypeConvert(tmpInst, varType);
|
|
|
|
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
|
|
|
|
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto otherLen = arrayType->get_num_of_elements() - node.initializers->elementList.size();
|
|
|
|
auto arrayLen = arrayType->get_num_of_elements();
|
|
|
|
|
|
|
|
auto otherLen = arrayLen - node.initializers->elementList.size();
|
|
|
|
auto tmpZero = CONST_INT(0);
|
|
|
|
auto tmpZero = CONST_INT(0);
|
|
|
|
TypeConvert(tmpZero, varType);
|
|
|
|
TypeConvert(tmpZero, varType);
|
|
|
|
for (int i = 0; i < otherLen; i++) {
|
|
|
|
for (int i = 0; i < otherLen; i++) {
|
|
|
|
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
|
|
|
|
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto zeroInit = ConstantZero::create(varType, module);
|
|
|
|
auto arrayInit = ConstantArray::create(varType->get_array_type(varType, arrayLen), varInit);
|
|
|
|
auto tmpInit = ConstantArray::create(static_pointer_cast<ArrayType>(varType), varInit);
|
|
|
|
identAlloca = GlobalVariable::create(node.name, module, arrayType, node.is_constant, arrayInit);
|
|
|
|
identAlloca = GlobalVariable::create(node.name, module, varType, node.is_constant, tmpInit);
|
|
|
|
|
|
|
|
//std::cout <<"VarDef1"<<std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} 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);
|
|
|
@ -551,7 +542,6 @@ void IRBuilder::visit(SyntaxTree::LVal &node) {
|
|
|
|
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 {
|
|
|
@ -601,9 +591,7 @@ 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);
|
|
|
|
std::cout << "RET" <<std::endl;
|
|
|
|
|
|
|
|
builder->create_store(tmpInst, 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);
|
|
|
@ -614,7 +602,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;
|
|
|
|
//std::cout << "Block" <<std::endl;
|
|
|
|
stmt->accept(*this);
|
|
|
|
stmt->accept(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scope.exit();
|
|
|
|
scope.exit();
|
|
|
|