Skip to content

ASR: Split BinOp #428

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

Closed
wants to merge 23 commits into from
Closed
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
12 changes: 8 additions & 4 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ 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)
| UnaryOp(unaryop op, expr operand, ttype type, expr? value)
= UnaryOp(unaryop op, expr operand, 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)
Expand All @@ -224,10 +222,14 @@ expr
| DerivedTypeConstructor(symbol dt_sym, expr* args, ttype type)
| ImpliedDoLoop(expr* values, expr var, expr start, expr end,
expr? increment, ttype type, expr? value)
| IntegerBinOp(expr left, binop op, expr right, ttype type, expr? value)
| IntegerConstant(int n, ttype type)
| IntegerBOZ(int v, integerboz intboz_type, ttype? type)
| RealBinOp(expr left, binop op, expr right, ttype type, expr? value)
| RealConstant(float r, ttype type)
| ComplexBinOp(expr left, binop op, expr right, ttype type, expr? value)
| ComplexConstant(float re, float im, ttype type)
| LogicalBinOp(expr left, logicalbinop op, expr right, ttype type, expr? value)
| LogicalConstant(bool value, ttype type)

| ListConstant(expr* args, ttype type)
Expand Down Expand Up @@ -256,6 +258,7 @@ expr
| ArraySize(expr v, expr? dim, ttype type, expr? value)
| ArrayBound(expr v, expr? dim ttype type, arraybound bound,
expr? value)
| OverloadedBinOp(expr left, binop op, expr right, ttype type, expr? value, expr overloaded)
| DerivedRef(expr v, symbol m, ttype type, expr? value)
| Cast(expr arg, cast_kind kind, ttype type, expr? value)
| ComplexRe(expr arg, ttype type, expr? value)
Expand Down Expand Up @@ -301,10 +304,11 @@ ttype
| Dict(ttype key_type, ttype value_type)
| Pointer(ttype type)

boolop = And | Or | Xor | NEqv | Eqv

binop = Add | Sub | Mul | Div | Pow

logicalbinop = And | Or | Xor | NEqv | Eqv

unaryop = Invert | Not | UAdd | USub

cmpop = Eq | NotEq | Lt | LtE | Gt | GtE
Expand Down
24 changes: 14 additions & 10 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ static inline ASR::ttype_t* expr_type(const ASR::expr_t *f)
{
LFORTRAN_ASSERT(f != nullptr);
switch (f->type) {
case ASR::exprType::BoolOp: { return ((ASR::BoolOp_t*)f)->m_type; }
case ASR::exprType::BinOp: { return ((ASR::BinOp_t*)f)->m_type; }
case ASR::exprType::LogicalBinOp: { return ((ASR::LogicalBinOp_t*)f)->m_type; }
case ASR::exprType::IntegerBinOp: { return ((ASR::IntegerBinOp_t*)f)->m_type; }
case ASR::exprType::RealBinOp: { return ((ASR::RealBinOp_t*)f)->m_type; }
case ASR::exprType::ComplexBinOp: { return ((ASR::ComplexBinOp_t*)f)->m_type; }
case ASR::exprType::UnaryOp: { return ((ASR::UnaryOp_t*)f)->m_type; }
case ASR::exprType::ComplexConstructor: { return ((ASR::ComplexConstructor_t*)f)->m_type; }
case ASR::exprType::NamedExpr: { return ((ASR::NamedExpr_t*)f)->m_type; }
Expand Down Expand Up @@ -272,21 +274,23 @@ 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");
}
}

static inline ASR::expr_t* expr_value(ASR::expr_t *f)
{
switch (f->type) {
case ASR::exprType::BoolOp: { return ASR::down_cast<ASR::BoolOp_t>(f)->m_value; }
case ASR::exprType::BinOp: { return ASR::down_cast<ASR::BinOp_t>(f)->m_value; }
case ASR::exprType::LogicalBinOp: { return ASR::down_cast<ASR::LogicalBinOp_t>(f)->m_value; }
case ASR::exprType::IntegerBinOp: { return ASR::down_cast<ASR::IntegerBinOp_t>(f)->m_value; }
case ASR::exprType::RealBinOp: { return ASR::down_cast<ASR::RealBinOp_t>(f)->m_value; }
case ASR::exprType::ComplexBinOp: { return ASR::down_cast<ASR::ComplexBinOp_t>(f)->m_value; }
case ASR::exprType::UnaryOp: { return ASR::down_cast<ASR::UnaryOp_t>(f)->m_value; }
case ASR::exprType::ComplexConstructor: { return ASR::down_cast<ASR::ComplexConstructor_t>(f)->m_value; }
case ASR::exprType::Compare: { return ASR::down_cast<ASR::Compare_t>(f)->m_value; }
Expand Down Expand Up @@ -1070,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 @@ -663,7 +663,20 @@ R"(#include <stdio.h>
}
}

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 @@ -710,27 +723,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 @@ -742,7 +755,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