Commit 1de803c9 authored by 李晓奇's avatar 李晓奇

38 points

parent ab890b55
......@@ -96,6 +96,8 @@ PB20111654 李晓奇
- 全局变量要特殊对待
- 数组下标为负时要调用`neg_idx_except()`
- 神奇,bool变量不能比较,所以在比较、计算的时候要做类型转换
## 实验设计
......
......@@ -96,7 +96,7 @@ class CminusfBuilder : public ASTVisitor {
virtual void visit(ASTTerm &) override final;
virtual void visit(ASTCall &) override final;
void type_cast(Value *&, Value *&, std::string = "computation");
void biop_type_check(Value *&, Value *&, std::string = "computation");
std::unique_ptr<IRBuilder> builder;
Scope scope;
......
......@@ -49,8 +49,19 @@ void error_exit(std::string s) {
// This function makes sure that
// 1. 2 values have same type
// 2. type is either i32 or float
void CminusfBuilder::type_cast(Value *&lvalue, Value *&rvalue, std::string util) {
assert(not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()));
void CminusfBuilder::biop_type_check(Value *&lvalue, Value *&rvalue, std::string util) {
if (Type::is_eq_type(lvalue->get_type(), rvalue->get_type())) {
if (lvalue->get_type()->is_integer_type() or lvalue->get_type()->is_float_type()) {
// check for i1
if (Type::is_eq_type(lvalue->get_type(), INT1_T)) {
lvalue = builder->create_zext(lvalue, INT32_T);
rvalue = builder->create_zext(rvalue, INT32_T);
}
} else
error_exit("not supported type cast for " + util);
return;
}
// only support cast between int and float: i32, i1, float
//
......@@ -434,9 +445,8 @@ void CminusfBuilder::visit(ASTSimpleExpression &node) {
auto lvalue = cur_value;
node.additive_expression_r->accept(*this);
auto rvalue = cur_value;
// type cast
if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "cmp");
// check type
biop_type_check(lvalue, rvalue, "cmp");
bool float_cmp = lvalue->get_type()->is_float_type();
switch (node.op) {
case OP_LE: {
......@@ -496,9 +506,8 @@ void CminusfBuilder::visit(ASTAdditiveExpression &node) {
auto lvalue = cur_value;
node.term->accept(*this);
auto rvalue = cur_value;
// type cast
if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "addop");
// check type
biop_type_check(lvalue, rvalue, "addop");
bool float_type = lvalue->get_type()->is_float_type();
// now left and right is the same type
switch (node.op) {
......@@ -530,9 +539,8 @@ void CminusfBuilder::visit(ASTTerm &node) {
auto lvalue = cur_value;
node.factor->accept(*this);
auto rvalue = cur_value;
// type cast
if (not Type::is_eq_type(lvalue->get_type(), rvalue->get_type()))
type_cast(lvalue, rvalue, "mul");
// check type
biop_type_check(lvalue, rvalue, "mul");
bool float_type = lvalue->get_type()->is_float_type();
// now left and right is the same type
switch (node.op) {
......
......@@ -4,6 +4,8 @@
#include <iostream>
#include <memory>
#include "logging.hpp"
using namespace std::literals::string_literals;
void print_help(std::string exe_name) {
......
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