Skip to content

Commit

Permalink
Implement ArrayItem in specific classes due to increased C/C++ specif…
Browse files Browse the repository at this point in the history
…ic code
  • Loading branch information
czgdp1807 committed Jul 15, 2022
1 parent a84fdf7 commit 2396484
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
33 changes: 33 additions & 0 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,39 @@ R"(
src = out;
}

void visit_ArrayItem(const ASR::ArrayItem_t &x) {
this->visit_expr(*x.m_v);
std::string array = src;
std::string out = array;
ASR::dimension_t* m_dims;
ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(x.m_v), m_dims);
out += "->data[";
std::string index = "";
for (size_t i=0; i<x.n_args; i++) {
std::string current_index = "";
if (x.m_args[i].m_right) {
this->visit_expr(*x.m_args[i].m_right);
} else {
src = "/* FIXME right index */";
}

current_index += "(" + src + " - " + array + "->dims["
+ std::to_string(i) + "].lower_bound)";
for( size_t j = 0; j < i; j++ ) {
std::string lb = array + "->dims[" + std::to_string(j) + "].lower_bound";
std::string ub = array + "->dims[" + std::to_string(j) + "].upper_bound";
current_index += " * (" + ub + " - " + lb + " + 1)";
}
index += current_index;
if (i < x.n_args - 1) {
index += " + ";
}
}
out += index + "]";
last_expr_precedence = 2;
src = out;
}

};

Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
Expand Down
54 changes: 1 addition & 53 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ R"(#include <stdio.h>
target += "->data";
}
} else if (ASR::is_a<ASR::ArrayItem_t>(*x.m_target)) {
visit_ArrayItem(*ASR::down_cast<ASR::ArrayItem_t>(x.m_target));
self().visit_ArrayItem(*ASR::down_cast<ASR::ArrayItem_t>(x.m_target));
target = src;
} else if (ASR::is_a<ASR::DerivedRef_t>(*x.m_target)) {
visit_DerivedRef(*ASR::down_cast<ASR::DerivedRef_t>(x.m_target));
Expand Down Expand Up @@ -638,58 +638,6 @@ R"(#include <stdio.h>
}
}

// TODO: C and non-C code have diverged too much
// so, split and define the methods in individual
// C and non-C (CPP) visitors.
void visit_ArrayItem(const ASR::ArrayItem_t &x) {
this->visit_expr(*x.m_v);
std::string array = src;
std::string out = array;
ASR::dimension_t* m_dims;
ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(x.m_v), m_dims);
if( is_c ) {
out += "->data[";
} else {
out += "->data->operator[](";
}
std::string index = "";
for (size_t i=0; i<x.n_args; i++) {
std::string current_index = "";
if (x.m_args[i].m_right) {
self().visit_expr(*x.m_args[i].m_right);
} else {
src = "/* FIXME right index */";
}
if( is_c ) {
current_index += "(" + src + " - " + array + "->dims["
+ std::to_string(i) + "].lower_bound)";
for( size_t j = 0; j < i; j++ ) {
std::string lb = array + "->dims[" + std::to_string(j) + "].lower_bound";
std::string ub = array + "->dims[" + std::to_string(j) + "].upper_bound";
current_index += " * (" + ub + " - " + lb + " + 1)";
}
index += current_index;
} else {
out += src;
out += " - " + array + "->dims[" + std::to_string(i) + "].lower_bound";
}
if (i < x.n_args - 1) {
if( is_c ) {
index += " + ";
} else {
out += ", ";
}
}
}
if( is_c ) {
out += index + "]";
} else {
out += ")";
}
last_expr_precedence = 2;
src = out;
}

void visit_Cast(const ASR::Cast_t &x) {
self().visit_expr(*x.m_arg);
switch (x.m_kind) {
Expand Down
26 changes: 26 additions & 0 deletions src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,32 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
src = out;
}

void visit_ArrayItem(const ASR::ArrayItem_t &x) {
this->visit_expr(*x.m_v);
std::string array = src;
std::string out = array;
ASR::dimension_t* m_dims;
ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(x.m_v), m_dims);
out += "->data->operator[](";
std::string index = "";
for (size_t i=0; i<x.n_args; i++) {
std::string current_index = "";
if (x.m_args[i].m_right) {
this->visit_expr(*x.m_args[i].m_right);
} else {
src = "/* FIXME right index */";
}
out += src;
out += " - " + array + "->dims[" + std::to_string(i) + "].lower_bound";
if (i < x.n_args - 1) {
out += ", ";
}
}
out += ")";
last_expr_precedence = 2;
src = out;
}

};

Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
Expand Down

0 comments on commit 2396484

Please sign in to comment.