Skip to content

Commit 0a7865e

Browse files
authored
Merge pull request #616 from namannimmo10/split_binop
ASR: Split `BinOp` based on different types
2 parents 49a65ea + 4e01d8c commit 0a7865e

File tree

60 files changed

+561
-399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+561
-399
lines changed

src/libasr/ASR.asdl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,11 @@ stmt
212212

213213

214214
expr
215-
= BoolOp(expr left, boolop op, expr right, ttype type, expr? value)
216-
| BinOp(expr left, binop op, expr right, ttype type, expr? value, expr? overloaded)
215+
= IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
217216
-- Such as: (x, y+z), (3.0, 2.0) generally not known at compile time
218217
| ComplexConstructor(expr re, expr im, ttype type, expr? value)
219218
| NamedExpr(expr target, expr value, ttype type)
220219
| Compare(expr left, cmpop op, expr right, ttype type, expr? value, expr? overloaded)
221-
| IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
222220
| FunctionCall(symbol name, symbol? original_name,
223221
call_arg* args, ttype type, expr? value, expr? dt)
224222
| DerivedTypeConstructor(symbol dt_sym, expr* args, ttype type, expr? value)
@@ -228,12 +226,16 @@ expr
228226
| IntegerBOZ(int v, integerboz intboz_type, ttype? type)
229227
| IntegerBitNot(expr arg, ttype type, expr? value)
230228
| IntegerUnaryMinus(expr arg, ttype type, expr? value)
229+
| IntegerBinOp(expr left, binop op, expr right, ttype type, expr? value)
231230
| RealConstant(float r, ttype type)
232231
| RealUnaryMinus(expr arg, ttype type, expr? value)
232+
| RealBinOp(expr left, binop op, expr right, ttype type, expr? value)
233233
| ComplexConstant(float re, float im, ttype type)
234234
| ComplexUnaryMinus(expr arg, ttype type, expr? value)
235+
| ComplexBinOp(expr left, binop op, expr right, ttype type, expr? value)
235236
| LogicalConstant(bool value, ttype type)
236237
| LogicalNot(expr arg, ttype type, expr? value)
238+
| LogicalBinOp(expr left, logicalbinop op, expr right, ttype type, expr? value)
237239

238240
| ListConstant(expr* args, ttype type)
239241
| ListLen(expr arg, ttype type, expr? value)
@@ -266,6 +268,7 @@ expr
266268
| ArrayPack(expr array, expr mask, expr? vector, ttype type, expr? value)
267269
| Transfer(expr source, expr mold, expr? size, ttype type, expr? value)
268270
| DerivedRef(expr v, symbol m, ttype type, expr? value)
271+
| OverloadedBinOp(expr left, binop op, expr right, ttype type, expr? value, expr overloaded)
269272
| Cast(expr arg, cast_kind kind, ttype type, expr? value)
270273
| ComplexRe(expr arg, ttype type, expr? value)
271274
| ComplexIm(expr arg, ttype type, expr? value)
@@ -315,10 +318,10 @@ ttype
315318
| Pointer(ttype type)
316319
| CPtr()
317320

318-
boolop = And | Or | Xor | NEqv | Eqv
319-
320321
binop = Add | Sub | Mul | Div | Pow
321322

323+
logicalbinop = And | Or | Xor | NEqv | Eqv
324+
322325
cmpop = Eq | NotEq | Lt | LtE | Gt | GtE
323326

324327
integerboz = Binary | Hex | Octal

src/libasr/asr_utils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ static inline std::string cmpop_to_str(const ASR::cmpopType t) {
226226
}
227227
}
228228

229-
static inline std::string boolop_to_str(const ASR::boolopType t) {
229+
static inline std::string logicalbinop_to_str(const ASR::logicalbinopType t) {
230230
switch (t) {
231-
case (ASR::boolopType::And): { return " && "; }
232-
case (ASR::boolopType::Or): { return " || "; }
233-
case (ASR::boolopType::Eqv): { return " == "; }
234-
case (ASR::boolopType::NEqv): { return " != "; }
231+
case (ASR::logicalbinopType::And): { return " && "; }
232+
case (ASR::logicalbinopType::Or): { return " || "; }
233+
case (ASR::logicalbinopType::Eqv): { return " == "; }
234+
case (ASR::logicalbinopType::NEqv): { return " != "; }
235235
default : throw LFortranException("Cannot represent the boolean operator as a string");
236236
}
237237
}
@@ -1074,7 +1074,7 @@ inline bool is_same_type_pointer(ASR::ttype_t* source, ASR::ttype_t* dest) {
10741074
a_len = -3;
10751075
break;
10761076
}
1077-
case ASR::exprType::BinOp: {
1077+
case ASR::exprType::IntegerBinOp: {
10781078
a_len = -3;
10791079
break;
10801080
}

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,20 @@ R"(#include <stdio.h>
765765
src = indent + dest_src + " = (" + type_src + ") " + source_src + ";\n";
766766
}
767767

768-
void visit_BinOp(const ASR::BinOp_t &x) {
768+
void visit_IntegerBinOp(const ASR::IntegerBinOp_t &x) {
769+
handle_BinOp(x);
770+
}
771+
772+
void visit_RealBinOp(const ASR::RealBinOp_t &x) {
773+
handle_BinOp(x);
774+
}
775+
776+
void visit_ComplexBinOp(const ASR::ComplexBinOp_t &x) {
777+
handle_BinOp(x);
778+
}
779+
780+
template <typename T>
781+
void handle_BinOp(const T &x) {
769782
self().visit_expr(*x.m_left);
770783
std::string left = std::move(src);
771784
int left_precedence = last_expr_precedence;
@@ -816,27 +829,27 @@ R"(#include <stdio.h>
816829
}
817830
}
818831

819-
void visit_BoolOp(const ASR::BoolOp_t &x) {
832+
void visit_LogicalBinOp(const ASR::LogicalBinOp_t &x) {
820833
self().visit_expr(*x.m_left);
821834
std::string left = std::move(src);
822835
int left_precedence = last_expr_precedence;
823836
self().visit_expr(*x.m_right);
824837
std::string right = std::move(src);
825838
int right_precedence = last_expr_precedence;
826839
switch (x.m_op) {
827-
case (ASR::boolopType::And): {
840+
case (ASR::logicalbinopType::And): {
828841
last_expr_precedence = 14;
829842
break;
830843
}
831-
case (ASR::boolopType::Or): {
844+
case (ASR::logicalbinopType::Or): {
832845
last_expr_precedence = 15;
833846
break;
834847
}
835-
case (ASR::boolopType::NEqv): {
848+
case (ASR::logicalbinopType::NEqv): {
836849
last_expr_precedence = 10;
837850
break;
838851
}
839-
case (ASR::boolopType::Eqv): {
852+
case (ASR::logicalbinopType::Eqv): {
840853
last_expr_precedence = 10;
841854
break;
842855
}
@@ -848,7 +861,7 @@ R"(#include <stdio.h>
848861
} else {
849862
src += "(" + left + ")";
850863
}
851-
src += ASRUtils::boolop_to_str(x.m_op);
864+
src += ASRUtils::logicalbinop_to_str(x.m_op);
852865
if (right_precedence <= last_expr_precedence) {
853866
src += right;
854867
} else {

0 commit comments

Comments
 (0)