Skip to content

Commit 873b056

Browse files
committed
C: Support array indexing using stride and offset
1 parent 3f9944e commit 873b056

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,51 +1313,82 @@ R"( // Initialise Numpy
13131313
CHECK_FAST_C(compiler_options, x)
13141314
this->visit_expr(*x.m_v);
13151315
std::string array = src;
1316-
std::string out = array;
13171316
ASR::ttype_t* x_mv_type = ASRUtils::expr_type(x.m_v);
13181317
ASR::dimension_t* m_dims;
13191318
int n_dims = ASRUtils::extract_dimensions_from_ttype(x_mv_type, m_dims);
13201319
bool is_data_only_array = ASRUtils::is_fixed_size_array(m_dims, n_dims) &&
13211320
ASR::is_a<ASR::StructType_t>(*ASRUtils::get_asr_owner(x.m_v));
13221321
if( is_data_only_array ) {
1322+
std::string index = "";
1323+
std::string out = array;
13231324
out += "[";
1324-
} else {
1325-
out += "->data[";
1326-
}
1327-
std::string index = "";
1328-
for (size_t i=0; i<x.n_args; i++) {
1329-
std::string current_index = "";
1330-
if (x.m_args[i].m_right) {
1331-
this->visit_expr(*x.m_args[i].m_right);
1332-
} else {
1333-
src = "/* FIXME right index */";
1334-
}
1325+
for (size_t i=0; i<x.n_args; i++) {
1326+
std::string current_index = "";
1327+
if (x.m_args[i].m_right) {
1328+
this->visit_expr(*x.m_args[i].m_right);
1329+
} else {
1330+
src = "/* FIXME right index */";
1331+
}
13351332

1336-
if( is_data_only_array ) {
13371333
current_index += src;
1338-
for( size_t j = i + 1; j < x.n_args; j++ ) {
1334+
for( size_t j = 0; j < i; j++ ) {
13391335
int64_t dim_size = 0;
13401336
ASRUtils::extract_value(m_dims[j].m_length, dim_size);
13411337
std::string length = std::to_string(dim_size);
13421338
current_index += " * " + length;
13431339
}
13441340
index += current_index;
1345-
} else {
1346-
current_index += "(" + src + " - " + array + "->dims["
1347-
+ std::to_string(i) + "].lower_bound)";
1348-
for( size_t j = i + 1; j < x.n_args; j++ ) {
1349-
std::string length = array + "->dims[" + std::to_string(j) + "].length";
1350-
current_index += " * " + length;
1341+
if (i < x.n_args - 1) {
1342+
index += " + ";
13511343
}
1352-
index += current_index;
13531344
}
1354-
if (i < x.n_args - 1) {
1355-
index += " + ";
1345+
out += index + "]";
1346+
last_expr_precedence = 2;
1347+
src = out;
1348+
return;
1349+
}
1350+
1351+
std::vector<std::string> indices;
1352+
for( size_t r = 0; r < x.n_args; r++ ) {
1353+
ASR::array_index_t curr_idx = x.m_args[r];
1354+
this->visit_expr(*curr_idx.m_right);
1355+
indices.push_back(src);
1356+
}
1357+
1358+
ASR::ttype_t* x_mv_type_ = ASRUtils::type_get_past_allocatable(
1359+
ASRUtils::type_get_past_pointer(ASRUtils::type_get_past_const(x_mv_type)));
1360+
LCOMPILERS_ASSERT(ASR::is_a<ASR::Array_t>(*x_mv_type_));
1361+
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(x_mv_type_);
1362+
std::vector<std::string> diminfo;
1363+
if( array_t->m_physical_type == ASR::array_physical_typeType::PointerToDataArray ||
1364+
array_t->m_physical_type == ASR::array_physical_typeType::FixedSizeArray ) {
1365+
for( size_t idim = 0; idim < x.n_args; idim++ ) {
1366+
this->visit_expr(*m_dims[idim].m_start);
1367+
diminfo.push_back(src);
1368+
this->visit_expr(*m_dims[idim].m_length);
1369+
diminfo.push_back(src);
13561370
}
1371+
} else if( array_t->m_physical_type == ASR::array_physical_typeType::UnboundedPointerToDataArray ) {
1372+
for( size_t idim = 0; idim < x.n_args; idim++ ) {
1373+
this->visit_expr(*m_dims[idim].m_start);
1374+
diminfo.push_back(src);
1375+
}
1376+
}
1377+
1378+
LCOMPILERS_ASSERT(ASRUtils::extract_n_dims_from_ttype(x_mv_type) > 0);
1379+
if (array_t->m_physical_type == ASR::array_physical_typeType::UnboundedPointerToDataArray) {
1380+
src = arr_get_single_element(array, indices, x.n_args,
1381+
true,
1382+
false,
1383+
diminfo,
1384+
true);
1385+
} else {
1386+
src = arr_get_single_element(array, indices, x.n_args,
1387+
array_t->m_physical_type == ASR::array_physical_typeType::PointerToDataArray,
1388+
array_t->m_physical_type == ASR::array_physical_typeType::FixedSizeArray,
1389+
diminfo, false);
13571390
}
1358-
out += index + "]";
13591391
last_expr_precedence = 2;
1360-
src = out;
13611392
}
13621393

13631394
void visit_StringItem(const ASR::StringItem_t& x) {

0 commit comments

Comments
 (0)