Modified something

main
Odeinjul 11 months ago
parent 6102b29494
commit ed25175a5f
No known key found for this signature in database
GPG Key ID: E384228B2B38FFBB

@ -17,18 +17,12 @@ namespace IR
std::vector<Ptr<Type>> funcFParam; std::vector<Ptr<Type>> funcFParam;
struct WhileBlock { struct WhileBlock {
BasicBlock *condBB; Ptr<BasicBlock> condBB;
BasicBlock *innerBB; Ptr<BasicBlock> bodyBB;
BasicBlock *exitBB; Ptr<BasicBlock> afterBB;
}; };
auto curWhileBlock = WhileBlock{nullptr, nullptr, nullptr}; auto curWhileBlock = WhileBlock{nullptr, nullptr, nullptr};
struct CondBlock {
BasicBlock *trueBB;
BasicBlock *falseBB;
};
auto curCondStruct = CondBlock{nullptr, nullptr};
Ptr<BasicBlock> retBB; Ptr<BasicBlock> retBB;
Ptr<Value> retAlloca; Ptr<Value> retAlloca;
@ -91,14 +85,24 @@ void IRBuilder::TypeConvert(Ptr<Value> origin, Ptr<Type> expected) {
origin = builder->create_load(origin); origin = builder->create_load(origin);
} }
if (type == INT32_T && expected == INT1_T) {
tmpInst = builder->create_icmp_ne(origin, CONST_INT(0));
return;
}
if (type == FLOAT_T && expected == INT1_T) {
tmpInst = builder->create_fcmp_ne(origin, CONST_FLOAT(0));
return;
}
if (type == INT32_T && expected == FLOAT_T) { if (type == INT32_T && expected == FLOAT_T) {
origin = builder->create_sitofp(origin, expected); tmpInst = builder->create_sitofp(origin, expected);
return; return;
} }
if (type == FLOAT_T && expected == INT32_T) { if (type == FLOAT_T && expected == INT32_T) {
origin = 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);
@ -157,7 +161,11 @@ void IRBuilder::visit(SyntaxTree::FuncDef &node) {
builder->set_insert_point(entryBlock); builder->set_insert_point(entryBlock);
auto para = node.param_list->params.begin(); auto para = node.param_list->params.begin();
auto para_end = node.param_list->params.end();
for (auto arg = func->arg_begin(); arg != func->arg_end() ; arg++) { for (auto arg = func->arg_begin(); arg != func->arg_end() ; arg++) {
if(para == para_end) {
break;
}
auto name = (*para)->name; auto name = (*para)->name;
auto val = (*arg); auto val = (*arg);
auto argAlloca = builder->create_alloca(val->get_type()); auto argAlloca = builder->create_alloca(val->get_type());
@ -590,84 +598,107 @@ void IRBuilder::visit(SyntaxTree::BinaryExpr &node) {
} }
// TODO // FINISH
void IRBuilder::visit(SyntaxTree::UnaryExpr &node) { void IRBuilder::visit(SyntaxTree::UnaryExpr &node) {
node.rhs->accept(*this); node.rhs->accept(*this);
switch (node.op) { switch (node.op) {
case SyntaxTree::UnaryOp::PLUS: case SyntaxTree::UnaryOp::PLUS:
break; break;
case SyntaxTree::UnaryOp::MINUS: case SyntaxTree::UnaryOp::MINUS:
if(isFloat){ auto constInt = dynamic_pointer_cast<ConstantInt>(tmpInst);
tmpInst = builder->create_fsub(CONST_FLOAT(0.0), tmpInst); auto constFloat = dynamic_pointer_cast<ConstantFloat>(tmpInst);
if (constInt != nullptr) {
tmpInst = CONST_INT(-constInt->get_value());
} else if (constFloat != nullptr) {
tmpInst = CONST_FLOAT(-constFloat->get_value());
} else {
//TODO;
} }
else
tmpInst = builder->create_isub(CONST_INT(0), tmpInst);
break; break;
default: default:
break; break;
} }
} }
// TODO // Finish
void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) { void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) {
auto curFunc = builder->get_insert_block()->get_parent(); auto name = node.name;
auto funcIdent = dynamic_pointer_cast<Function>(scope.find(name, true));
std::vector<Ptr<Value>> funcRParam; std::vector<Ptr<Value>> funcRParam;
auto arg = funcIdent->arg_begin();
auto arg_end = funcIdent->arg_end();
for (const auto &param : node.params) { for (const auto &param : node.params) {
if(arg == arg_end) {
break;
}
param->accept(*this); param->accept(*this);
funcRParam.push_back(tmpVal); TypeConvert(tmpInst, (*arg)->get_type());
funcRParam.push_back(tmpInst);
arg++;
} }
builder->create_call(curFunc, funcRParam); tmpInst = builder->create_call(funcIdent, funcRParam);
} }
// TODO // FINISH
void IRBuilder::visit(SyntaxTree::IfStmt &node) { void IRBuilder::visit(SyntaxTree::IfStmt &node) {
auto curFunc = builder->get_insert_block()->get_parent(); auto curFunc = builder->get_insert_block()->get_parent();
node.cond_exp->accept(*this);
auto cond = tmpVal;
auto trueBB = BasicBlock::create(module, "trueBB_if", curFunc); auto trueBB = BasicBlock::create(module, "trueBB_if", curFunc);
auto falseBB = BasicBlock::create(module, "falseBB_if", curFunc); auto falseBB = BasicBlock::create(module, "falseBB_if", curFunc);
builder->create_cond_br(cond, trueBB, falseBB); auto exitBB = node.else_statement == nullptr ? falseBB : BasicBlock::create(module, "exitBB_if", curFunc);
node.cond_exp->accept(*this);
TypeConvert(tmpInst, INT1_T);
builder->create_cond_br(tmpInst, trueBB, falseBB);
builder->set_insert_point(trueBB); builder->set_insert_point(trueBB);
node.if_statement->accept(*this); node.if_statement->accept(*this);
builder->set_insert_point(falseBB); builder->create_br(exitBB);
node.else_statement->accept(*this);
if (node.else_statement) {
builder->set_insert_point(falseBB);
node.else_statement->accept(*this);
builder->create_br(exitBB);
}
if (!exitBB->get_pre_basic_blocks().empty()) {
builder->set_insert_point(exitBB);
}
} }
// TODO //FINISH
void IRBuilder::visit(SyntaxTree::WhileStmt &node) { void IRBuilder::visit(SyntaxTree::WhileStmt &node) {
auto curFunc = builder->get_insert_block()->get_parent(); auto curFunc = builder->get_insert_block()->get_parent();
auto condBB = BasicBlock::create(module, "condBB_while", curFunc); auto condBB = BasicBlock::create(module, "condBB_while", curFunc);
auto bodyBB = BasicBlock::create(module, "bodyBB_while", curFunc); auto bodyBB = BasicBlock::create(module, "bodyBB_while", curFunc);
auto afterBB = BasicBlock::create(module, "afterBB_while", curFunc); auto afterBB = BasicBlock::create(module, "afterBB_while", curFunc);
curCondBB = condBB; auto tmpWhileBlock = curWhileBlock;
curAfterBB = afterBB; curWhileBlock = WhileBlock{condBB, bodyBB, afterBB};
builder->create_br(condBB); builder->create_br(condBB);
builder->set_insert_point(condBB); builder->set_insert_point(condBB);
node.cond_exp->accept(*this); node.cond_exp->accept(*this);
auto cond = tmpVal; TypeConvert(tmpInst, INT1_T);
builder->create_cond_br(tmpInst, bodyBB, afterBB);
builder->create_cond_br(cond, bodyBB, afterBB);
builder->set_insert_point(bodyBB); builder->set_insert_point(bodyBB);
node.statement->accept(*this); node.statement->accept(*this);
builder->create_br(condBB); builder->create_br(condBB);
builder->set_insert_point(afterBB); builder->set_insert_point(afterBB);
curWhileBlock = tmpWhileBlock;
return ; return ;
} }
// TODO //FINISH
void IRBuilder::visit(SyntaxTree::BreakStmt &node) { void IRBuilder::visit(SyntaxTree::BreakStmt &node) {
builder->create_br(curAfterBB); builder->create_br(curWhileBlock.afterBB);
return ; return ;
} }
// TODO //FINISH
void IRBuilder::visit(SyntaxTree::ContinueStmt &node) { void IRBuilder::visit(SyntaxTree::ContinueStmt &node) {
builder->create_br(curCondBB); builder->create_br(curWhileBlock.condBB);
return ; return ;
} }
} }

Loading…
Cancel
Save