Skip to content

Commit

Permalink
Merge pull request #51 from czgdp1807/lc_31
Browse files Browse the repository at this point in the history
Ported ``integration_tests/arrays_inputs_15.f90`` from LFortran and improve LC to compile it
  • Loading branch information
czgdp1807 authored Jan 5, 2024
2 parents 125b287 + d89e76f commit 8395ad4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,5 @@ RUN(NAME array_18.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_19.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_20.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
47 changes: 47 additions & 0 deletions integration_tests/array_20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <cmath>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"

void copy_from_to(const xt::xtensor_fixed<int, xt::xshape<10>>& xa,
xt::xtensor_fixed<int, xt::xshape<10>>&xb) {
int i;
for( i = 0; i < xa.size(); i++ ) {
xb(i) = xa(i);
}
}

bool verify(const xt::xtensor_fixed<int, xt::xshape<10>>& a,
const xt::xtensor_fixed<int, xt::xshape<10>>& b) {
int i;
bool r = true;
for( i = 0; i < a.size(); i++ ) {
r = r && (a(i) == b(i));
}

return r;
}

int main() {

xt::xtensor_fixed<int, xt::xshape<10>> x, y;
int i;
bool r;

for( i = 0; i < x.size(); i++ ) {
x(i) = i;
}

copy_from_to(x, y);
std::cout<< x << std::endl;
std::cout << y << std::endl;
r = verify(x, y);
std::cout << r << std::endl;
if (!r) {
exit(2);
}

return 0;

}
47 changes: 47 additions & 0 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,21 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}
}

void CreateLogicalOp(ASR::expr_t* lhs, ASR::expr_t* rhs,
ASR::logicalbinopType binop_type, const Location& loc) {
cast_helper(lhs, rhs, false);
ASRUtils::make_ArrayBroadcast_t_util(al, loc, lhs, rhs);
if( ASRUtils::is_integer(*ASRUtils::expr_type(lhs)) &&
ASRUtils::is_integer(*ASRUtils::expr_type(rhs)) ) {
tmp = ASR::make_LogicalBinOp_t(al, loc, lhs,
binop_type, rhs, ASRUtils::expr_type(lhs), nullptr);
} else {
throw std::runtime_error("Only integer types are supported so "
"far for logical binary operator, found: " + ASRUtils::type_to_str(ASRUtils::expr_type(lhs))
+ " and " + ASRUtils::type_to_str(ASRUtils::expr_type(rhs)));
}
}

void CreateBinOp(ASR::expr_t* lhs, ASR::expr_t* rhs,
ASR::binopType binop_type, const Location& loc) {
cast_helper(lhs, rhs, false);
Expand All @@ -1368,6 +1383,17 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}
}

void CreateLogicalNot(ASR::expr_t* op, const Location& loc) {
if( ASRUtils::is_logical(*ASRUtils::expr_type(op)) ) {
tmp = ASR::make_LogicalNot_t(al, loc, op,
ASRUtils::expr_type(op), nullptr);
} else {
throw std::runtime_error("Only logical types are supported so "
"far for logical not operator, found: " +
ASRUtils::type_to_str(ASRUtils::expr_type(op)));
}
}

void CreateUnaryMinus(ASR::expr_t* op, const Location& loc) {
if( ASRUtils::is_integer(*ASRUtils::expr_type(op)) ) {
tmp = ASR::make_IntegerUnaryMinus_t(al, loc, op,
Expand All @@ -1393,8 +1419,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
is_stmt_created = true;
} else {
bool is_binop = false, is_cmpop = false;
bool is_logicalbinop = false;
ASR::binopType binop_type;
ASR::cmpopType cmpop_type;
ASR::logicalbinopType logicalbinop_type;
switch (op) {
case clang::BO_Add: {
binop_type = ASR::binopType::Add;
Expand Down Expand Up @@ -1446,6 +1474,11 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
is_cmpop = true;
break;
}
case clang::BO_LAnd: {
logicalbinop_type = ASR::logicalbinopType::And;
is_logicalbinop = true;
break;
}
default: {
throw std::runtime_error("BinaryOperator not supported " + std::to_string(op));
break;
Expand All @@ -1455,6 +1488,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
CreateBinOp(x_lhs, x_rhs, binop_type, Lloc(x));
} else if( is_cmpop ) {
CreateCompareOp(x_lhs, x_rhs, cmpop_type, Lloc(x));
} else if( is_logicalbinop ) {
CreateLogicalOp(x_lhs, x_rhs, logicalbinop_type, Lloc(x));
} else {
throw std::runtime_error("Only binary operators supported so far");
}
Expand Down Expand Up @@ -1604,6 +1639,14 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
return true;
}

bool TraverseCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr* x) {
bool b = x->getValue();
tmp = ASR::make_LogicalConstant_t(al, Lloc(x), b,
ASRUtils::TYPE(ASR::make_Logical_t(al, Lloc(x), 4)));
is_stmt_created = false;
return true;
}

bool TraverseFloatingLiteral(clang::FloatingLiteral* x) {
double d = x->getValue().convertToDouble();
tmp = ASR::make_RealConstant_t(al, Lloc(x), d,
Expand Down Expand Up @@ -1715,6 +1758,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
CreateUnaryMinus(var, Lloc(x));
break;
}
case clang::UnaryOperatorKind::UO_LNot: {
CreateLogicalNot(var, Lloc(x));
break;
}
default: {
throw std::runtime_error("Only postfix increment and minus are supported so far.");
}
Expand Down

0 comments on commit 8395ad4

Please sign in to comment.