Skip to content

Commit 8395ad4

Browse files
authored
Merge pull request #51 from czgdp1807/lc_31
Ported ``integration_tests/arrays_inputs_15.f90`` from LFortran and improve LC to compile it
2 parents 125b287 + d89e76f commit 8395ad4

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,5 @@ RUN(NAME array_18.cpp LABELS gcc llvm NOFAST
216216
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
217217
RUN(NAME array_19.cpp LABELS gcc llvm NOFAST
218218
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
219+
RUN(NAME array_20.cpp LABELS gcc llvm NOFAST
220+
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)

integration_tests/array_20.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <xtensor/xtensor.hpp>
4+
#include <xtensor/xfixed.hpp>
5+
#include "xtensor/xio.hpp"
6+
7+
void copy_from_to(const xt::xtensor_fixed<int, xt::xshape<10>>& xa,
8+
xt::xtensor_fixed<int, xt::xshape<10>>&xb) {
9+
int i;
10+
for( i = 0; i < xa.size(); i++ ) {
11+
xb(i) = xa(i);
12+
}
13+
}
14+
15+
bool verify(const xt::xtensor_fixed<int, xt::xshape<10>>& a,
16+
const xt::xtensor_fixed<int, xt::xshape<10>>& b) {
17+
int i;
18+
bool r = true;
19+
for( i = 0; i < a.size(); i++ ) {
20+
r = r && (a(i) == b(i));
21+
}
22+
23+
return r;
24+
}
25+
26+
int main() {
27+
28+
xt::xtensor_fixed<int, xt::xshape<10>> x, y;
29+
int i;
30+
bool r;
31+
32+
for( i = 0; i < x.size(); i++ ) {
33+
x(i) = i;
34+
}
35+
36+
copy_from_to(x, y);
37+
std::cout<< x << std::endl;
38+
std::cout << y << std::endl;
39+
r = verify(x, y);
40+
std::cout << r << std::endl;
41+
if (!r) {
42+
exit(2);
43+
}
44+
45+
return 0;
46+
47+
}

src/lc/clang_ast_to_asr.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,21 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
13451345
}
13461346
}
13471347

1348+
void CreateLogicalOp(ASR::expr_t* lhs, ASR::expr_t* rhs,
1349+
ASR::logicalbinopType binop_type, const Location& loc) {
1350+
cast_helper(lhs, rhs, false);
1351+
ASRUtils::make_ArrayBroadcast_t_util(al, loc, lhs, rhs);
1352+
if( ASRUtils::is_integer(*ASRUtils::expr_type(lhs)) &&
1353+
ASRUtils::is_integer(*ASRUtils::expr_type(rhs)) ) {
1354+
tmp = ASR::make_LogicalBinOp_t(al, loc, lhs,
1355+
binop_type, rhs, ASRUtils::expr_type(lhs), nullptr);
1356+
} else {
1357+
throw std::runtime_error("Only integer types are supported so "
1358+
"far for logical binary operator, found: " + ASRUtils::type_to_str(ASRUtils::expr_type(lhs))
1359+
+ " and " + ASRUtils::type_to_str(ASRUtils::expr_type(rhs)));
1360+
}
1361+
}
1362+
13481363
void CreateBinOp(ASR::expr_t* lhs, ASR::expr_t* rhs,
13491364
ASR::binopType binop_type, const Location& loc) {
13501365
cast_helper(lhs, rhs, false);
@@ -1368,6 +1383,17 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
13681383
}
13691384
}
13701385

1386+
void CreateLogicalNot(ASR::expr_t* op, const Location& loc) {
1387+
if( ASRUtils::is_logical(*ASRUtils::expr_type(op)) ) {
1388+
tmp = ASR::make_LogicalNot_t(al, loc, op,
1389+
ASRUtils::expr_type(op), nullptr);
1390+
} else {
1391+
throw std::runtime_error("Only logical types are supported so "
1392+
"far for logical not operator, found: " +
1393+
ASRUtils::type_to_str(ASRUtils::expr_type(op)));
1394+
}
1395+
}
1396+
13711397
void CreateUnaryMinus(ASR::expr_t* op, const Location& loc) {
13721398
if( ASRUtils::is_integer(*ASRUtils::expr_type(op)) ) {
13731399
tmp = ASR::make_IntegerUnaryMinus_t(al, loc, op,
@@ -1393,8 +1419,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
13931419
is_stmt_created = true;
13941420
} else {
13951421
bool is_binop = false, is_cmpop = false;
1422+
bool is_logicalbinop = false;
13961423
ASR::binopType binop_type;
13971424
ASR::cmpopType cmpop_type;
1425+
ASR::logicalbinopType logicalbinop_type;
13981426
switch (op) {
13991427
case clang::BO_Add: {
14001428
binop_type = ASR::binopType::Add;
@@ -1446,6 +1474,11 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
14461474
is_cmpop = true;
14471475
break;
14481476
}
1477+
case clang::BO_LAnd: {
1478+
logicalbinop_type = ASR::logicalbinopType::And;
1479+
is_logicalbinop = true;
1480+
break;
1481+
}
14491482
default: {
14501483
throw std::runtime_error("BinaryOperator not supported " + std::to_string(op));
14511484
break;
@@ -1455,6 +1488,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
14551488
CreateBinOp(x_lhs, x_rhs, binop_type, Lloc(x));
14561489
} else if( is_cmpop ) {
14571490
CreateCompareOp(x_lhs, x_rhs, cmpop_type, Lloc(x));
1491+
} else if( is_logicalbinop ) {
1492+
CreateLogicalOp(x_lhs, x_rhs, logicalbinop_type, Lloc(x));
14581493
} else {
14591494
throw std::runtime_error("Only binary operators supported so far");
14601495
}
@@ -1604,6 +1639,14 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
16041639
return true;
16051640
}
16061641

1642+
bool TraverseCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr* x) {
1643+
bool b = x->getValue();
1644+
tmp = ASR::make_LogicalConstant_t(al, Lloc(x), b,
1645+
ASRUtils::TYPE(ASR::make_Logical_t(al, Lloc(x), 4)));
1646+
is_stmt_created = false;
1647+
return true;
1648+
}
1649+
16071650
bool TraverseFloatingLiteral(clang::FloatingLiteral* x) {
16081651
double d = x->getValue().convertToDouble();
16091652
tmp = ASR::make_RealConstant_t(al, Lloc(x), d,
@@ -1715,6 +1758,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
17151758
CreateUnaryMinus(var, Lloc(x));
17161759
break;
17171760
}
1761+
case clang::UnaryOperatorKind::UO_LNot: {
1762+
CreateLogicalNot(var, Lloc(x));
1763+
break;
1764+
}
17181765
default: {
17191766
throw std::runtime_error("Only postfix increment and minus are supported so far.");
17201767
}

0 commit comments

Comments
 (0)