Commit b8f14d6d authored by 李晓奇's avatar 李晓奇

73, very good

parent 6d308da4
...@@ -91,7 +91,7 @@ PB20111654 李晓奇 ...@@ -91,7 +91,7 @@ PB20111654 李晓奇
- 全局变量要特殊对待 - 全局变量要特殊对待
- 数组下标为负时要调用`neg_idx_except()` - 数组下标为负时要调用`neg_idx_except()`,这个函数的call后边也要加上br
- 神奇,bool变量不能比较,所以在比较、计算的时候要做类型转换 - 神奇,bool变量不能比较,所以在比较、计算的时候要做类型转换
...@@ -107,6 +107,10 @@ PB20111654 李晓奇 ...@@ -107,6 +107,10 @@ PB20111654 李晓奇
- 不要乱给lable命名,因为lable的命名不会自动(针对自定义)去重。 - 不要乱给lable命名,因为lable的命名不会自动(针对自定义)去重。
- 发现ifelse语句忘了无条件跳转了……
- 返现布尔表达式必须使用i1类型,类型要求真的太严格了!!
## 实验设计 ## 实验设计
请写明为了顺利完成本次实验,加入了哪些亮点设计,并对这些设计进行解释。 请写明为了顺利完成本次实验,加入了哪些亮点设计,并对这些设计进行解释。
...@@ -121,6 +125,8 @@ PB20111654 李晓奇 ...@@ -121,6 +125,8 @@ PB20111654 李晓奇
此次实验有什么收获 此次实验有什么收获
不管多困难,先开始。~~因为更多的困难在动手前是永远想不到的~~。
### 实验反馈 (可选 不计入评分) ### 实验反馈 (可选 不计入评分)
对本次实验的建议 对本次实验的建议
...@@ -97,6 +97,7 @@ class CminusfBuilder : public ASTVisitor { ...@@ -97,6 +97,7 @@ class CminusfBuilder : public ASTVisitor {
virtual void visit(ASTCall &) override final; virtual void visit(ASTCall &) override final;
void biop_type_check(Value *&, Value *&, std::string = "computation"); void biop_type_check(Value *&, Value *&, std::string = "computation");
void cast_to_i1(Value *&);
std::unique_ptr<IRBuilder> builder; std::unique_ptr<IRBuilder> builder;
Scope scope; Scope scope;
......
...@@ -81,6 +81,16 @@ void CminusfBuilder::biop_type_check(Value *&lvalue, Value *&rvalue, std::string ...@@ -81,6 +81,16 @@ void CminusfBuilder::biop_type_check(Value *&lvalue, Value *&rvalue, std::string
} }
} }
// this function makes sure value is a bool type
void CminusfBuilder::cast_to_i1(Value *&value) {
assert(value->get_type()->is_integer_type() or value->get_type()->is_float_type());
if (value->get_type()->is_float_type())
// value = builder->create_fptosi(value, INT1_T);
value = builder->create_fcmp_ne(value, CONST_FP(0));
else if (Type::is_eq_type(value->get_type(), INT32_T))
value = builder->create_icmp_ne(value, CONST_INT(0));
}
void CminusfBuilder::visit(ASTProgram &node) { void CminusfBuilder::visit(ASTProgram &node) {
VOID_T = Type::get_void_type(module.get()); VOID_T = Type::get_void_type(module.get());
INT1_T = Type::get_int1_type(module.get()); INT1_T = Type::get_int1_type(module.get());
...@@ -303,16 +313,19 @@ void CminusfBuilder::visit(ASTSelectionStmt &node) { ...@@ -303,16 +313,19 @@ void CminusfBuilder::visit(ASTSelectionStmt &node) {
scope.enter(); scope.enter();
node.expression->accept(*this); node.expression->accept(*this);
auto cond = cur_value; auto cond = cur_value;
cast_to_i1(cond);
auto TBB = BasicBlock::create(builder->get_module(), "", cur_fun); auto TBB = BasicBlock::create(builder->get_module(), "", cur_fun);
auto passBB = BasicBlock::create(builder->get_module(), "", cur_fun); auto FBB = BasicBlock::create(builder->get_module(), "", cur_fun);
builder->create_cond_br(cond, TBB, passBB); builder->create_cond_br(cond, TBB, FBB);
builder->set_insert_point(TBB); builder->set_insert_point(TBB);
node.if_statement->accept(*this); node.if_statement->accept(*this);
builder->create_br(FBB);
builder->set_insert_point(passBB); builder->set_insert_point(FBB);
if (node.else_statement) if (node.else_statement) {
node.else_statement->accept(*this); node.else_statement->accept(*this);
}
scope.exit(); scope.exit();
} }
...@@ -330,6 +343,7 @@ void CminusfBuilder::visit(ASTIterationStmt &node) { ...@@ -330,6 +343,7 @@ void CminusfBuilder::visit(ASTIterationStmt &node) {
builder->set_insert_point(HEAD); builder->set_insert_point(HEAD);
node.expression->accept(*this); node.expression->accept(*this);
auto cond = cur_value; auto cond = cur_value;
cast_to_i1(cond);
builder->create_cond_br(cond, BODY, END); builder->create_cond_br(cond, BODY, END);
builder->set_insert_point(BODY); builder->set_insert_point(BODY);
...@@ -397,14 +411,14 @@ void CminusfBuilder::visit(ASTVar &node) { ...@@ -397,14 +411,14 @@ void CminusfBuilder::visit(ASTVar &node) {
auto cond = builder->create_icmp_lt(cur_value, CONST_INT(0)); auto cond = builder->create_icmp_lt(cur_value, CONST_INT(0));
auto except_func = scope.find("neg_idx_except"); auto except_func = scope.find("neg_idx_except");
auto TBB = BasicBlock::create(builder->get_module(), "", cur_fun); auto TBB = BasicBlock::create(builder->get_module(), "", cur_fun);
auto FBB = BasicBlock::create(builder->get_module(), "", cur_fun); auto passBB = BasicBlock::create(builder->get_module(), "", cur_fun);
builder->create_cond_br(cond, TBB, FBB); builder->create_cond_br(cond, TBB, passBB);
builder->set_insert_point(TBB); builder->set_insert_point(TBB);
builder->create_call(except_func, {}); builder->create_call(except_func, {});
builder->create_br(FBB); builder->create_br(passBB);
builder->set_insert_point(FBB); builder->set_insert_point(passBB);
addr = builder->create_gep(memory, {CONST_INT(0), cur_value}); addr = builder->create_gep(memory, {CONST_INT(0), cur_value});
......
void main(void) { void main(void) {
int a[10]; int a[10];
/* a[3] = 1234; */ a[3] = 1234;
output(a[3]); output(a[3]);
return; return;
} }
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