Skip to content
Merged
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
129 changes: 83 additions & 46 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,25 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
}
sub += indent + std::string(v_m_name) + "->data = " + std::string(v_m_name) + "_data;\n";
sub += indent + std::string(v_m_name) + "->n_dims = " + std::to_string(n_dims) + ";\n";
for (int i = 0; i < n_dims; i++) {
sub += indent + std::string(v_m_name) + "->offset = " + std::to_string(0) + ";\n";
std::string stride = "1";
for (int i = n_dims - 1; i >= 0; i--) {
std::string start = "1", length = "0";
if( m_dims[i].m_start ) {
this->visit_expr(*m_dims[i].m_start);
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].lower_bound = " + src + ";\n";
} else {
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].lower_bound = 0" + ";\n";
start = src;
}
if( m_dims[i].m_length ) {
this->visit_expr(*m_dims[i].m_length);
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].length = " + src + ";\n";
} else {
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].length = 0" + ";\n";
length = src;
}
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].lower_bound = " + start + ";\n";
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].length = " + length + ";\n";
sub += indent + std::string(v_m_name) +
"->dims[" + std::to_string(i) + "].stride = " + stride + ";\n";
stride = "(" + stride + "*" + length + ")";
}
sub.pop_back();
sub.pop_back();
Expand Down Expand Up @@ -1116,21 +1118,25 @@ R"( // Initialise Numpy
ASR::dimension_t* m_dims = nullptr;
int n_dims = ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(x.m_ptr), m_dims);
dim_set_code = indent + dest_src + "->n_dims = " + std::to_string(n_dims) + ";\n";
for( int i = 0; i < n_dims; i++ ) {
dim_set_code = indent + dest_src + "->offset = 0;\n";
std::string stride = "1";
for (int i = n_dims - 1; i >= 0; i--) {
std::string start = "0", length = "0";
if( lower_bounds ) {
visit_expr(*lower_bounds->m_args[i]);
} else {
src = "0";
start = src;
}
dim_set_code += indent + dest_src + "->dims[" +
std::to_string(i) + "].lower_bound = " + src + ";\n";
if( m_dims[i].m_length ) {
visit_expr(*m_dims[i].m_length);
} else {
src = "0";
this->visit_expr(*m_dims[i].m_length);
length = src;
}
dim_set_code += indent + dest_src + "->dims[" +
std::to_string(i) + "].length = " + src + ";\n";
dim_set_code += indent + dest_src +
"->dims[" + std::to_string(i) + "].lower_bound = " + start + ";\n";
dim_set_code += indent + dest_src +
"->dims[" + std::to_string(i) + "].length = " + length + ";\n";
dim_set_code += indent + dest_src +
"->dims[" + std::to_string(i) + "].stride = " + stride + ";\n";
stride = "(" + stride + "*" + length + ")";
}
src.clear();
src += dim_set_code;
Expand Down Expand Up @@ -1307,51 +1313,82 @@ R"( // Initialise Numpy
CHECK_FAST_C(compiler_options, x)
this->visit_expr(*x.m_v);
std::string array = src;
std::string out = array;
ASR::ttype_t* x_mv_type = ASRUtils::expr_type(x.m_v);
ASR::dimension_t* m_dims;
int n_dims = ASRUtils::extract_dimensions_from_ttype(x_mv_type, m_dims);
bool is_data_only_array = ASRUtils::is_fixed_size_array(m_dims, n_dims) &&
ASR::is_a<ASR::StructType_t>(*ASRUtils::get_asr_owner(x.m_v));
if( is_data_only_array ) {
std::string index = "";
std::string out = array;
out += "[";
} else {
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 */";
}
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 */";
}

if( is_data_only_array ) {
current_index += src;
for( size_t j = i + 1; j < x.n_args; j++ ) {
for( size_t j = 0; j < i; j++ ) {
int64_t dim_size = 0;
ASRUtils::extract_value(m_dims[j].m_length, dim_size);
std::string length = std::to_string(dim_size);
current_index += " * " + length;
}
index += current_index;
} else {
current_index += "(" + src + " - " + array + "->dims["
+ std::to_string(i) + "].lower_bound)";
for( size_t j = i + 1; j < x.n_args; j++ ) {
std::string length = array + "->dims[" + std::to_string(j) + "].length";
current_index += " * " + length;
if (i < x.n_args - 1) {
index += " + ";
}
index += current_index;
}
if (i < x.n_args - 1) {
index += " + ";
out += index + "]";
last_expr_precedence = 2;
src = out;
return;
}

std::vector<std::string> indices;
for( size_t r = 0; r < x.n_args; r++ ) {
ASR::array_index_t curr_idx = x.m_args[r];
this->visit_expr(*curr_idx.m_right);
indices.push_back(src);
}

ASR::ttype_t* x_mv_type_ = ASRUtils::type_get_past_allocatable(
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(x_mv_type)));
LCOMPILERS_ASSERT(ASR::is_a<ASR::Array_t>(*x_mv_type_));
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(x_mv_type_);
std::vector<std::string> diminfo;
if( array_t->m_physical_type == ASR::array_physical_typeType::PointerToDataArray ||
array_t->m_physical_type == ASR::array_physical_typeType::FixedSizeArray ) {
for( size_t idim = 0; idim < x.n_args; idim++ ) {
this->visit_expr(*m_dims[idim].m_start);
diminfo.push_back(src);
this->visit_expr(*m_dims[idim].m_length);
diminfo.push_back(src);
}
} else if( array_t->m_physical_type == ASR::array_physical_typeType::UnboundedPointerToDataArray ) {
for( size_t idim = 0; idim < x.n_args; idim++ ) {
this->visit_expr(*m_dims[idim].m_start);
diminfo.push_back(src);
}
}
out += index + "]";

LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(x_mv_type) > 0);
if (array_t->m_physical_type == ASR::array_physical_typeType::UnboundedPointerToDataArray) {
src = arr_get_single_element(array, indices, x.n_args,
true,
false,
diminfo,
true);
} else {
src = arr_get_single_element(array, indices, x.n_args,
array_t->m_physical_type == ASR::array_physical_typeType::PointerToDataArray,
array_t->m_physical_type == ASR::array_physical_typeType::FixedSizeArray,
diminfo, false);
}
last_expr_precedence = 2;
src = out;
}

void visit_StringItem(const ASR::StringItem_t& x) {
Expand Down
Loading