Finish all visit except VarDef

main
Odeinjul 12 months ago
parent 12e012dd6b
commit 7653fecd16
No known key found for this signature in database
GPG Key ID: E384228B2B38FFBB

@ -18,6 +18,9 @@ int exprRes = 0;
std::string curFuncName = ""; std::string curFuncName = "";
Ptr<SysYF::IR::Instruction> tmpInst; Ptr<SysYF::IR::Instruction> tmpInst;
bool isFloat = false; bool isFloat = false;
bool isAddr = false;
Ptr<BasicBlock> curCondBB;
Ptr<BasicBlock> curAfterBB;
// types // types
Ptr<Type> VOID_T; Ptr<Type> VOID_T;
@ -35,10 +38,6 @@ Ptr<Type> GetType(SyntaxTree::Type type){
return INT1_T; return INT1_T;
case SyntaxTree::Type::VOID: case SyntaxTree::Type::VOID:
return VOID_T; return VOID_T;
case SyntaxTree::Type::BOOL:
return INT1_T;
case SyntaxTree::Type::STRING:
return INT32PTR_T;
default: default:
return nullptr; return nullptr;
} }
@ -88,21 +87,12 @@ void IRBuilder::visit(SyntaxTree::FuncParam &node) {
break; break;
case SyntaxTree::Type::FLOAT: case SyntaxTree::Type::FLOAT:
if (!node.array_index.empty()) { if (!node.array_index.empty()) {
// TODO high dim array // TODO high dim array (not now)
funcFParam.push_back(FLOATPTR_T); funcFParam.push_back(FLOATPTR_T);
} }
else else
funcFParam.push_back(FLOAT_T); funcFParam.push_back(FLOAT_T);
break; break;
case SyntaxTree::Type::VOID:
funcFParam.push_back(VOID_T);
break;
case SyntaxTree::Type::STRING:
funcFParam.push_back(INT32PTR_T);
break;
case SyntaxTree::Type::BOOL:
funcFParam.push_back(INT1_T);
break;
default: default:
funcFParam.push_back(INT32_T); funcFParam.push_back(INT32_T);
break; break;
@ -110,13 +100,13 @@ void IRBuilder::visit(SyntaxTree::FuncParam &node) {
} }
void IRBuilder::visit(SyntaxTree::VarDef &node) { void IRBuilder::visit(SyntaxTree::VarDef &node) {
;
} }
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()) {
//TODO: high dim array //TODO: high dim array (not now)
node.array_index[0]->accept(*this); node.array_index[0]->accept(*this);
auto index = tmpInst; auto index = tmpInst;
tmpInst = builder->create_gep(ident, {CONST_INT(0), index}); tmpInst = builder->create_gep(ident, {CONST_INT(0), index});
@ -124,48 +114,50 @@ void IRBuilder::visit(SyntaxTree::LVal &node) {
else { else {
tmpInst = dynamic_pointer_cast<Instruction>(ident); tmpInst = dynamic_pointer_cast<Instruction>(ident);
} }
isAddr = true;
} }
void IRBuilder::visit(SyntaxTree::AssignStmt &node) { void IRBuilder::visit(SyntaxTree::AssignStmt &node) {
node.target->accept(*this); node.target->accept(*this);
auto target = tmpInst; auto target = tmpInst;
node.value->accept(*this); node.value->accept(*this);
auto value = tmpInst; Ptr<Value> value;
if (isAddr) {
value = builder->create_load(tmpInst);
isAddr = false;
}
else
value = dynamic_pointer_cast<Value>(tmpInst);
builder->create_store(value, target); builder->create_store(value, target);
} }
void IRBuilder::visit(SyntaxTree::Literal &node) { void IRBuilder::visit(SyntaxTree::Literal &node) {
/*
switch(node.literal_type) { switch(node.literal_type) {
case SyntaxTree::Type::INT: case SyntaxTree::Type::INT:
tmp_val = CONST_INT(node.int_val); tmp_val = CONST_INT(node.int_const);
isFloat = false;
break; break;
case SyntaxTree::Type::FLOAT: case SyntaxTree::Type::FLOAT:
tmp_val = CONST_FLOAT(node.float_val); tmp_val = CONST_FLOAT(node.float_const);
break; isFloat = true;
case SyntaxTree::Type::BOOL:
tmp_val = CONST_INT(node.bool_val);
break;
case SyntaxTree::Type::STRING:
tmp_val = CONST_INT(node.str_val);
break; break;
default: default:
tmp_val = CONST_INT(0); tmp_val = CONST_INT(0);
break; break;
} }
*/
} }
void IRBuilder::visit(SyntaxTree::ReturnStmt &node) { void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {
node.ret->accept(*this); node.ret->accept(*this);
builder->create_ret(tmpInst); builder->create_ret(tmp_val);
return ;
} }
void IRBuilder::visit(SyntaxTree::BlockStmt &node) { void IRBuilder::visit(SyntaxTree::BlockStmt &node) {
scope.enter();
for (const auto &stmt : node.body) { for (const auto &stmt : node.body) {
stmt->accept(*this); stmt->accept(*this);
} }
scope.exit();
return ; return ;
} }
@ -182,17 +174,55 @@ void IRBuilder::visit(SyntaxTree::UnaryCondExpr &node) {
node.rhs->accept(*this); node.rhs->accept(*this);
switch (node.op) { switch (node.op) {
case SyntaxTree::UnaryCondOp::NOT: case SyntaxTree::UnaryCondOp::NOT:
if(isFloat){ if (isFloat) {
tmpInst = builder->create_fcmp_eq(tmpInst, CONST_FLOAT(0.0)); tmpInst = builder->create_fcmp_eq(tmpInst, CONST_FLOAT(0.0));
} }
else else {
tmpInst = builder->create_icmp_eq(tmpInst, CONST_INT(0)); tmpInst = builder->create_icmp_eq(tmpInst, CONST_INT(0));
}
default: default:
break; break;
} }
} }
void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) { void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) {
auto curFunc = builder->get_insert_block()->get_parent();
auto trueBB = BasicBlock::create(module, "trueBB_and", curFunc);
auto falseBB = BasicBlock::create(module, "falseBB_and", curFunc);
Ptr<Instruction> cond;
switch (node.op) {
case SyntaxTree::BinaryCondOp::LAND:
node.lhs->accept(*this);
cond = tmpInst;
builder->create_cond_br(cond, trueBB, falseBB);
// True - continue
builder->set_insert_point(trueBB);
node.rhs->accept(*this);
cond = tmpInst;
// False
builder->set_insert_point(falseBB);
tmpInst = cond;
break;
case SyntaxTree::BinaryCondOp::LOR:
node.lhs->accept(*this);
cond = tmpInst;
builder->create_cond_br(cond, trueBB, falseBB);
// False - continue
builder->set_insert_point(falseBB);
node.rhs->accept(*this);
cond = tmpInst;
// True
builder->set_insert_point(trueBB);
tmpInst = cond;
break;
default:
node.lhs->accept(*this); node.lhs->accept(*this);
auto lhs = tmpInst; auto lhs = tmpInst;
node.rhs->accept(*this); node.rhs->accept(*this);
@ -246,20 +276,25 @@ void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) {
tmpInst = builder->create_icmp_ne(lhs, rhs); tmpInst = builder->create_icmp_ne(lhs, rhs);
} }
break; break;
case SyntaxTree::BinaryCondOp::LAND: default:
//TODO: and
break;
case SyntaxTree::BinaryCondOp::LOR:
//TODO: or
break; break;
} }
}
} }
void IRBuilder::visit(SyntaxTree::BinaryExpr &node) { void IRBuilder::visit(SyntaxTree::BinaryExpr &node) {
node.lhs->accept(*this); node.lhs->accept(*this);
auto lhs = tmpInst; auto lhs = tmpInst;
if (isAddr) {
lhs = builder->create_load(lhs);
isAddr = false;
}
node.rhs->accept(*this); node.rhs->accept(*this);
auto rhs = tmpInst; auto rhs = tmpInst;
if(isAddr){
rhs = builder->create_load(rhs);
isAddr = false;
}
switch (node.op) { switch (node.op) {
case SyntaxTree::BinOp::PLUS: case SyntaxTree::BinOp::PLUS:
if(isFloat){ if(isFloat){
@ -291,7 +326,7 @@ void IRBuilder::visit(SyntaxTree::BinaryExpr &node) {
break; break;
case SyntaxTree::BinOp::MODULO: case SyntaxTree::BinOp::MODULO:
if(isFloat){ if(isFloat){
// not support ;// not support
} else { } else {
tmpInst = builder->create_isrem(lhs, rhs); tmpInst = builder->create_isrem(lhs, rhs);
} }
@ -318,11 +353,17 @@ void IRBuilder::visit(SyntaxTree::UnaryExpr &node) {
} }
void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) { void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) {
auto curFunc = builder->get_insert_block()->get_parent();
std::vector<Ptr<Value>> funcRParam;
for (const auto &param : node.params) {
param->accept(*this);
funcRParam.push_back(tmp_val);
}
builder->create_call(curFunc, funcRParam);
} }
void IRBuilder::visit(SyntaxTree::IfStmt &node) { void IRBuilder::visit(SyntaxTree::IfStmt &node) {
auto curFunc = dynamic_pointer_cast<Function>(scope.find(curFuncName, true)); auto curFunc = builder->get_insert_block()->get_parent();
node.cond_exp->accept(*this); node.cond_exp->accept(*this);
auto cond = tmp_val; auto cond = tmp_val;
auto trueBB = BasicBlock::create(module, "trueBB_if", curFunc); auto trueBB = BasicBlock::create(module, "trueBB_if", curFunc);
@ -335,10 +376,12 @@ void IRBuilder::visit(SyntaxTree::IfStmt &node) {
} }
void IRBuilder::visit(SyntaxTree::WhileStmt &node) { void IRBuilder::visit(SyntaxTree::WhileStmt &node) {
auto curFunc = dynamic_pointer_cast<Function>(scope.find(curFuncName, true)); 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;
curAfterBB = afterBB;
builder->create_br(condBB); builder->create_br(condBB);
@ -353,15 +396,18 @@ void IRBuilder::visit(SyntaxTree::WhileStmt &node) {
builder->create_br(condBB); builder->create_br(condBB);
builder->set_insert_point(afterBB); builder->set_insert_point(afterBB);
return ;
} }
void IRBuilder::visit(SyntaxTree::BreakStmt &node) { void IRBuilder::visit(SyntaxTree::BreakStmt &node) {
builder->create_br(curAfterBB);
return ;
} }
void IRBuilder::visit(SyntaxTree::ContinueStmt &node) { void IRBuilder::visit(SyntaxTree::ContinueStmt &node) {
builder->create_br(curCondBB);
return ;
} }
} }
} }
Loading…
Cancel
Save