Skip to content

ASR: Split BinOp based on different types #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,11 @@ stmt


expr
= BoolOp(expr left, boolop op, expr right, ttype type, expr? value)
| BinOp(expr left, binop op, expr right, ttype type, expr? value, expr? overloaded)
= IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
-- Such as: (x, y+z), (3.0, 2.0) generally not known at compile time
| ComplexConstructor(expr re, expr im, ttype type, expr? value)
| NamedExpr(expr target, expr value, ttype type)
| Compare(expr left, cmpop op, expr right, ttype type, expr? value, expr? overloaded)
| IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
| FunctionCall(symbol name, symbol? original_name,
call_arg* args, ttype type, expr? value, expr? dt)
| DerivedTypeConstructor(symbol dt_sym, expr* args, ttype type, expr? value)
Expand All @@ -228,12 +226,16 @@ expr
| IntegerBOZ(int v, integerboz intboz_type, ttype? type)
| IntegerBitNot(expr arg, ttype type, expr? value)
| IntegerUnaryMinus(expr arg, ttype type, expr? value)
| IntegerBinOp(expr left, binop op, expr right, ttype type, expr? value)
| RealConstant(float r, ttype type)
| RealUnaryMinus(expr arg, ttype type, expr? value)
| RealBinOp(expr left, binop op, expr right, ttype type, expr? value)
| ComplexConstant(float re, float im, ttype type)
| ComplexUnaryMinus(expr arg, ttype type, expr? value)
| ComplexBinOp(expr left, binop op, expr right, ttype type, expr? value)
| LogicalConstant(bool value, ttype type)
| LogicalNot(expr arg, ttype type, expr? value)
| LogicalBinOp(expr left, logicalbinop op, expr right, ttype type, expr? value)

| ListConstant(expr* args, ttype type)
| ListLen(expr arg, ttype type, expr? value)
Expand Down Expand Up @@ -266,6 +268,7 @@ expr
| ArrayPack(expr array, expr mask, expr? vector, ttype type, expr? value)
| Transfer(expr source, expr mold, expr? size, ttype type, expr? value)
| DerivedRef(expr v, symbol m, ttype type, expr? value)
| OverloadedBinOp(expr left, binop op, expr right, ttype type, expr? value, expr overloaded)
| Cast(expr arg, cast_kind kind, ttype type, expr? value)
| ComplexRe(expr arg, ttype type, expr? value)
| ComplexIm(expr arg, ttype type, expr? value)
Expand Down Expand Up @@ -315,10 +318,10 @@ ttype
| Pointer(ttype type)
| CPtr()

boolop = And | Or | Xor | NEqv | Eqv

binop = Add | Sub | Mul | Div | Pow

logicalbinop = And | Or | Xor | NEqv | Eqv

cmpop = Eq | NotEq | Lt | LtE | Gt | GtE

integerboz = Binary | Hex | Octal
Expand Down
12 changes: 6 additions & 6 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ static inline std::string cmpop_to_str(const ASR::cmpopType t) {
}
}

static inline std::string boolop_to_str(const ASR::boolopType t) {
static inline std::string logicalbinop_to_str(const ASR::logicalbinopType t) {
switch (t) {
case (ASR::boolopType::And): { return " && "; }
case (ASR::boolopType::Or): { return " || "; }
case (ASR::boolopType::Eqv): { return " == "; }
case (ASR::boolopType::NEqv): { return " != "; }
case (ASR::logicalbinopType::And): { return " && "; }
case (ASR::logicalbinopType::Or): { return " || "; }
case (ASR::logicalbinopType::Eqv): { return " == "; }
case (ASR::logicalbinopType::NEqv): { return " != "; }
default : throw LFortranException("Cannot represent the boolean operator as a string");
}
}
Expand Down Expand Up @@ -1074,7 +1074,7 @@ inline bool is_same_type_pointer(ASR::ttype_t* source, ASR::ttype_t* dest) {
a_len = -3;
break;
}
case ASR::exprType::BinOp: {
case ASR::exprType::IntegerBinOp: {
a_len = -3;
break;
}
Expand Down
27 changes: 20 additions & 7 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,20 @@ R"(#include <stdio.h>
src = indent + dest_src + " = (" + type_src + ") " + source_src + ";\n";
}

void visit_BinOp(const ASR::BinOp_t &x) {
void visit_IntegerBinOp(const ASR::IntegerBinOp_t &x) {
handle_BinOp(x);
}

void visit_RealBinOp(const ASR::RealBinOp_t &x) {
handle_BinOp(x);
}

void visit_ComplexBinOp(const ASR::ComplexBinOp_t &x) {
handle_BinOp(x);
}

template <typename T>
void handle_BinOp(const T &x) {
self().visit_expr(*x.m_left);
std::string left = std::move(src);
int left_precedence = last_expr_precedence;
Expand Down Expand Up @@ -816,27 +829,27 @@ R"(#include <stdio.h>
}
}

void visit_BoolOp(const ASR::BoolOp_t &x) {
void visit_LogicalBinOp(const ASR::LogicalBinOp_t &x) {
self().visit_expr(*x.m_left);
std::string left = std::move(src);
int left_precedence = last_expr_precedence;
self().visit_expr(*x.m_right);
std::string right = std::move(src);
int right_precedence = last_expr_precedence;
switch (x.m_op) {
case (ASR::boolopType::And): {
case (ASR::logicalbinopType::And): {
last_expr_precedence = 14;
break;
}
case (ASR::boolopType::Or): {
case (ASR::logicalbinopType::Or): {
last_expr_precedence = 15;
break;
}
case (ASR::boolopType::NEqv): {
case (ASR::logicalbinopType::NEqv): {
last_expr_precedence = 10;
break;
}
case (ASR::boolopType::Eqv): {
case (ASR::logicalbinopType::Eqv): {
last_expr_precedence = 10;
break;
}
Expand All @@ -848,7 +861,7 @@ R"(#include <stdio.h>
} else {
src += "(" + left + ")";
}
src += ASRUtils::boolop_to_str(x.m_op);
src += ASRUtils::logicalbinop_to_str(x.m_op);
if (right_precedence <= last_expr_precedence) {
src += right;
} else {
Expand Down
Loading