Commit 1851e507 authored by 李晓奇's avatar 李晓奇

20 points, cry to death...

parent d7dae728
...@@ -96,7 +96,7 @@ class CminusfBuilder : public ASTVisitor { ...@@ -96,7 +96,7 @@ class CminusfBuilder : public ASTVisitor {
virtual void visit(ASTTerm &) override final; virtual void visit(ASTTerm &) override final;
virtual void visit(ASTCall &) override final; virtual void visit(ASTCall &) override final;
bool type_cast(Value *&, Value *&, std::string = "computation"); void type_cast(Value *&, Value *&, std::string = "computation");
std::unique_ptr<IRBuilder> builder; std::unique_ptr<IRBuilder> builder;
Scope scope; Scope scope;
......
...@@ -44,18 +44,17 @@ void error_exit(std::string s) { ...@@ -44,18 +44,17 @@ void error_exit(std::string s) {
// This function makes sure that // This function makes sure that
// 1. 2 values have same type // 1. 2 values have same type
// 2. type is either int or float // 2. type is either int or float
// 3. return true if there is float void CminusfBuilder::type_cast(Value *&lvalue, Value *&rvalue, std::string util) {
bool CminusfBuilder::type_cast(Value *&lvalue, Value *&rvalue, std::string util) { assert(not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()));
if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type())) {
if (Type::is_eq_type(lvalue->get_type(), INT32_T) and Type::is_eq_type(rvalue->get_type(), FLOAT_T)) // only support cast between int and float
lvalue = builder->create_sitofp(lvalue, FLOAT_T); if (Type::is_eq_type(lvalue->get_type(), INT32_T) and Type::is_eq_type(rvalue->get_type(), FLOAT_T))
else if (Type::is_eq_type(rvalue->get_type(), INT32_T) and Type::is_eq_type(lvalue->get_type(), FLOAT_T)) lvalue = builder->create_sitofp(lvalue, FLOAT_T);
rvalue = builder->create_sitofp(rvalue, FLOAT_T); else if (Type::is_eq_type(lvalue->get_type(), FLOAT_T) and Type::is_eq_type(rvalue->get_type(), INT32_T))
rvalue = builder->create_sitofp(rvalue, FLOAT_T);
else { // but we only support computing between int and float
error_exit("not supported type cast for " + util);
} }
// now 2 value is the same type, but we only support computing between int and float
if (not Type::is_eq_type(lvalue->get_type(), INT32_T) or not Type::is_eq_type(lvalue->get_type(), FLOAT_T))
error_exit("not supported cross-type " + util);
return Type::is_eq_type(lvalue->get_type(), FLOAT_T);
} }
void CminusfBuilder::visit(ASTProgram &node) { void CminusfBuilder::visit(ASTProgram &node) {
...@@ -219,11 +218,16 @@ void CminusfBuilder::visit(ASTParam &node) { ...@@ -219,11 +218,16 @@ void CminusfBuilder::visit(ASTParam &node) {
} }
} }
// Done?
void CminusfBuilder::visit(ASTCompoundStmt &node) { void CminusfBuilder::visit(ASTCompoundStmt &node) {
//!TODO: This function is not complete. //!TODO: This function is not complete.
// You may need to add some code here // You may need to add some code here
// to deal with complex statements. // to deal with complex statements.
/* auto bb = BasicBlock::create(builder->get_module(), "", cur_fun);
* builder->create_br(bb);
* builder->set_insert_point(bb); */
scope.enter();
for (auto &decl : node.local_declarations) { for (auto &decl : node.local_declarations) {
decl->accept(*this); decl->accept(*this);
} }
...@@ -233,26 +237,60 @@ void CminusfBuilder::visit(ASTCompoundStmt &node) { ...@@ -233,26 +237,60 @@ void CminusfBuilder::visit(ASTCompoundStmt &node) {
if (builder->get_insert_block()->get_terminator() != nullptr) if (builder->get_insert_block()->get_terminator() != nullptr)
break; break;
} }
scope.exit();
} }
// Done
void CminusfBuilder::visit(ASTExpressionStmt &node) { void CminusfBuilder::visit(ASTExpressionStmt &node) {
//!TODO: This function is empty now. //!TODO: This function is empty now.
// Add some code here. // Add some code here.
if (node.expression)
node.expression->accept(*this);
} }
// Done
void CminusfBuilder::visit(ASTSelectionStmt &node) { void CminusfBuilder::visit(ASTSelectionStmt &node) {
//!TODO: This function is empty now. //!TODO: This function is empty now.
// Add some code here. // Add some code here.
scope.enter();
node.expression->accept(*this);
auto cond = cur_value;
auto TBB = BasicBlock::create(builder->get_module(), "trueCase", cur_fun);
auto FBB = BasicBlock::create(builder->get_module(), "falseCase", cur_fun);
builder->create_cond_br(cond, TBB, FBB);
builder->set_insert_point(TBB);
node.if_statement->accept(*this);
builder->set_insert_point(FBB);
if (node.else_statement)
node.else_statement->accept(*this);
scope.exit();
} }
// Done
void CminusfBuilder::visit(ASTIterationStmt &node) { void CminusfBuilder::visit(ASTIterationStmt &node) {
//!TODO: This function is empty now. //!TODO: This function is empty now.
// Add some code here. // Add some code here.
scope.enter();
auto HEAD = BasicBlock::create(builder->get_module(), "loop_head", cur_fun);
auto BODY = BasicBlock::create(builder->get_module(), "loop_body", cur_fun);
auto END = BasicBlock::create(builder->get_module(), "end", cur_fun);
builder->create_br(HEAD);
builder->set_insert_point(HEAD);
node.expression->accept(*this);
auto cond = cur_value;
builder->create_cond_br(cond, BODY, END);
builder->set_insert_point(BODY);
node.statement->accept(*this);
builder->create_br(HEAD);
builder->set_insert_point(END);
scope.exit();
} }
// Jobs: // Done
// - call accept()
// - gen return code
void CminusfBuilder::visit(ASTReturnStmt &node) { void CminusfBuilder::visit(ASTReturnStmt &node) {
if (node.expression == nullptr) { if (node.expression == nullptr) {
builder->create_void_ret(); builder->create_void_ret();
...@@ -261,10 +299,22 @@ void CminusfBuilder::visit(ASTReturnStmt &node) { ...@@ -261,10 +299,22 @@ void CminusfBuilder::visit(ASTReturnStmt &node) {
// You need to solve other return cases (e.g. return an integer). // You need to solve other return cases (e.g. return an integer).
// //
node.expression->accept(*this); node.expression->accept(*this);
// type cast
// return type can only be int, float or void
if (not Type::is_eq_type(cur_fun->get_return_type(), cur_value->get_type())) {
if (not cur_value->get_type()->is_integer_type() or not cur_value->get_type()->is_float_type())
error_exit("unsupported return type");
if (cur_value->get_type()->is_integer_type())
cur_value = builder->create_sitofp(cur_value, FLOAT_T);
else
cur_value = builder->create_fptosi(cur_value, INT32_T);
}
builder->create_ret(cur_value); builder->create_ret(cur_value);
} }
} }
// Done
void CminusfBuilder::visit(ASTVar &node) { void CminusfBuilder::visit(ASTVar &node) {
//!TODO: This function is empty now. //!TODO: This function is empty now.
// Add some code here. // Add some code here.
...@@ -275,55 +325,44 @@ void CminusfBuilder::visit(ASTVar &node) { ...@@ -275,55 +325,44 @@ void CminusfBuilder::visit(ASTVar &node) {
auto memory = scope.find(node.id); auto memory = scope.find(node.id);
Value *addr; Value *addr;
if (memory == nullptr) { if (memory == nullptr)
LOG_ERROR << node.id << " not declared!"; error_exit("variable " + node.id + " not declared");
std::abort(); if (node.expression) { // e.g. int a[10]; // mem is [i32 x 10]*
}
if (node.expression) { // a[1] = 1;
node.expression->accept(*this); node.expression->accept(*this);
if (not Type::is_eq_type(cur_value->get_type(), INT32_T)) { if (not Type::is_eq_type(cur_value->get_type(), INT32_T)) {
if (Type::is_eq_type(cur_value->get_type(), FLOAT_T)) if (Type::is_eq_type(cur_value->get_type(), FLOAT_T))
cur_value = builder->create_fptosi(cur_value, INT32_T); cur_value = builder->create_fptosi(cur_value, INT32_T);
else { else
LOG_ERROR << "unexpected type!"; error_exit("bad type for subscription");
std::abort();
}
} }
addr = builder->create_gep(memory, {cur_value}); // now it's
addr = builder->create_gep(memory, {0, cur_value});
} else { // a = 1; } else { // e.g. int a; // a is i32*
addr = memory; addr = memory;
} }
cur_value = addr; cur_value = addr;
} }
// Done
void CminusfBuilder::visit(ASTAssignExpression &node) { void CminusfBuilder::visit(ASTAssignExpression &node) {
//!TODO: This function is empty now. //!TODO: This function is empty now.
// Add some code here. // Add some code here.
// //
// 1. For assignment, both side should have a vaule,
// the left side has an addr, right side have the correspond value
// 2. Type cast!!!
//
//
// This accept() should update `cur_value` to the left value
node.var->accept(*this); node.var->accept(*this);
auto left = cur_value; auto left = cur_value;
// This accept() should update `cur_value` to the right value
node.expression->accept(*this); node.expression->accept(*this);
assert(cur_value->get_type()->get_pointer_element_type() != nullptr);
// type cast // type cast
auto type = scope.find(node.var->id)->get_type(); if (not Type::is_eq_type(cur_value->get_type()->get_pointer_element_type(), cur_value->get_type())) {
if (not Type::is_eq_type(type, cur_value->get_type())) { if (cur_value->get_type()->is_integer_type())
if (type->is_pointer_type())
cur_value = builder->create_fptosi(cur_value, INT32_T);
else if (type->is_float_type())
cur_value = builder->create_sitofp(cur_value, FLOAT_T); cur_value = builder->create_sitofp(cur_value, FLOAT_T);
else { else if (cur_value->get_type()->is_float_type())
LOG(ERROR) << "Bad assignment!"; cur_value = builder->create_fptosi(cur_value, INT32_T);
std::abort(); else
} error_exit("bad type for assignment");
} }
// gen code // gen code
builder->create_store(left, cur_value); builder->create_store(left, cur_value);
...@@ -340,7 +379,9 @@ void CminusfBuilder::visit(ASTSimpleExpression &node) { ...@@ -340,7 +379,9 @@ void CminusfBuilder::visit(ASTSimpleExpression &node) {
node.additive_expression_r->accept(*this); node.additive_expression_r->accept(*this);
auto rvalue = cur_value; auto rvalue = cur_value;
// type cast // type cast
bool float_cmp = type_cast(lvalue, rvalue, "cmp"); if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "addop");
bool float_cmp = lvalue->get_type()->is_float_type();
switch (node.op) { switch (node.op) {
case OP_LE: { case OP_LE: {
if (float_cmp) if (float_cmp)
...@@ -399,7 +440,9 @@ void CminusfBuilder::visit(ASTAdditiveExpression &node) { ...@@ -399,7 +440,9 @@ void CminusfBuilder::visit(ASTAdditiveExpression &node) {
node.additive_expression->accept(*this); node.additive_expression->accept(*this);
auto rvalue = cur_value; auto rvalue = cur_value;
// type cast // type cast
bool float_type = type_cast(lvalue, rvalue, "addop"); if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "addop");
bool float_type = lvalue->get_type()->is_float_type();
// now left and right is the same type // now left and right is the same type
switch (node.op) { switch (node.op) {
case OP_PLUS: { case OP_PLUS: {
...@@ -430,7 +473,9 @@ void CminusfBuilder::visit(ASTTerm &node) { ...@@ -430,7 +473,9 @@ void CminusfBuilder::visit(ASTTerm &node) {
node.term->accept(*this); node.term->accept(*this);
auto rvalue = cur_value; auto rvalue = cur_value;
// type cast // type cast
bool float_type = type_cast(lvalue, rvalue, "mulop"); if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "addop");
bool float_type = lvalue->get_type()->is_float_type();
// now left and right is the same type // now left and right is the same type
switch (node.op) { switch (node.op) {
case OP_MUL: { case OP_MUL: {
......
...@@ -28,11 +28,14 @@ const Instruction *BasicBlock::get_terminator() const { ...@@ -28,11 +28,14 @@ const Instruction *BasicBlock::get_terminator() const {
return nullptr; return nullptr;
} }
switch (instr_list_.back().get_instr_type()) { switch (instr_list_.back().get_instr_type()) {
case Instruction::ret: return &instr_list_.back(); case Instruction::ret:
return &instr_list_.back();
case Instruction::br: return &instr_list_.back(); case Instruction::br:
return &instr_list_.back();
default: return nullptr; default:
return nullptr;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment