@@ -117,23 +117,25 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
117
117
}
118
118
sub += indent + std::string (v_m_name) + " ->data = " + std::string (v_m_name) + " _data;\n " ;
119
119
sub += indent + std::string (v_m_name) + " ->n_dims = " + std::to_string (n_dims) + " ;\n " ;
120
- for (int i = 0 ; i < n_dims; i++) {
120
+ sub += indent + std::string (v_m_name) + " ->offset = " + std::to_string (0 ) + " ;\n " ;
121
+ std::string stride = " 1" ;
122
+ for (int i = n_dims - 1 ; i >= 0 ; i--) {
123
+ std::string start = " 1" , length = " 0" ;
121
124
if ( m_dims[i].m_start ) {
122
125
this ->visit_expr (*m_dims[i].m_start );
123
- sub += indent + std::string (v_m_name) +
124
- " ->dims[" + std::to_string (i) + " ].lower_bound = " + src + " ;\n " ;
125
- } else {
126
- sub += indent + std::string (v_m_name) +
127
- " ->dims[" + std::to_string (i) + " ].lower_bound = 0" + " ;\n " ;
126
+ start = src;
128
127
}
129
128
if ( m_dims[i].m_length ) {
130
129
this ->visit_expr (*m_dims[i].m_length );
131
- sub += indent + std::string (v_m_name) +
132
- " ->dims[" + std::to_string (i) + " ].length = " + src + " ;\n " ;
133
- } else {
134
- sub += indent + std::string (v_m_name) +
135
- " ->dims[" + std::to_string (i) + " ].length = 0" + " ;\n " ;
130
+ length = src;
136
131
}
132
+ sub += indent + std::string (v_m_name) +
133
+ " ->dims[" + std::to_string (i) + " ].lower_bound = " + start + " ;\n " ;
134
+ sub += indent + std::string (v_m_name) +
135
+ " ->dims[" + std::to_string (i) + " ].length = " + length + " ;\n " ;
136
+ sub += indent + std::string (v_m_name) +
137
+ " ->dims[" + std::to_string (i) + " ].stride = " + stride + " ;\n " ;
138
+ stride = " (" + stride + " *" + length + " )" ;
137
139
}
138
140
sub.pop_back ();
139
141
sub.pop_back ();
@@ -1116,21 +1118,25 @@ R"( // Initialise Numpy
1116
1118
ASR::dimension_t * m_dims = nullptr ;
1117
1119
int n_dims = ASRUtils::extract_dimensions_from_ttype (ASRUtils::expr_type (x.m_ptr ), m_dims);
1118
1120
dim_set_code = indent + dest_src + " ->n_dims = " + std::to_string (n_dims) + " ;\n " ;
1119
- for ( int i = 0 ; i < n_dims; i++ ) {
1121
+ dim_set_code = indent + dest_src + " ->offset = 0;\n " ;
1122
+ std::string stride = " 1" ;
1123
+ for (int i = n_dims - 1 ; i >= 0 ; i--) {
1124
+ std::string start = " 0" , length = " 0" ;
1120
1125
if ( lower_bounds ) {
1121
1126
visit_expr (*lower_bounds->m_args [i]);
1122
- } else {
1123
- src = " 0" ;
1127
+ start = src;
1124
1128
}
1125
- dim_set_code += indent + dest_src + " ->dims[" +
1126
- std::to_string (i) + " ].lower_bound = " + src + " ;\n " ;
1127
1129
if ( m_dims[i].m_length ) {
1128
- visit_expr (*m_dims[i].m_length );
1129
- } else {
1130
- src = " 0" ;
1130
+ this ->visit_expr (*m_dims[i].m_length );
1131
+ length = src;
1131
1132
}
1132
- dim_set_code += indent + dest_src + " ->dims[" +
1133
- std::to_string (i) + " ].length = " + src + " ;\n " ;
1133
+ dim_set_code += indent + dest_src +
1134
+ " ->dims[" + std::to_string (i) + " ].lower_bound = " + start + " ;\n " ;
1135
+ dim_set_code += indent + dest_src +
1136
+ " ->dims[" + std::to_string (i) + " ].length = " + length + " ;\n " ;
1137
+ dim_set_code += indent + dest_src +
1138
+ " ->dims[" + std::to_string (i) + " ].stride = " + stride + " ;\n " ;
1139
+ stride = " (" + stride + " *" + length + " )" ;
1134
1140
}
1135
1141
src.clear ();
1136
1142
src += dim_set_code;
@@ -1307,51 +1313,82 @@ R"( // Initialise Numpy
1307
1313
CHECK_FAST_C (compiler_options, x)
1308
1314
this ->visit_expr (*x.m_v );
1309
1315
std::string array = src;
1310
- std::string out = array;
1311
1316
ASR::ttype_t * x_mv_type = ASRUtils::expr_type (x.m_v );
1312
1317
ASR::dimension_t * m_dims;
1313
1318
int n_dims = ASRUtils::extract_dimensions_from_ttype (x_mv_type, m_dims);
1314
1319
bool is_data_only_array = ASRUtils::is_fixed_size_array (m_dims, n_dims) &&
1315
1320
ASR::is_a<ASR::StructType_t>(*ASRUtils::get_asr_owner (x.m_v ));
1316
1321
if ( is_data_only_array ) {
1322
+ std::string index = " " ;
1323
+ std::string out = array;
1317
1324
out += " [" ;
1318
- } else {
1319
- out += " ->data[" ;
1320
- }
1321
- std::string index = " " ;
1322
- for (size_t i=0 ; i<x.n_args ; i++) {
1323
- std::string current_index = " " ;
1324
- if (x.m_args [i].m_right ) {
1325
- this ->visit_expr (*x.m_args [i].m_right );
1326
- } else {
1327
- src = " /* FIXME right index */" ;
1328
- }
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
+ }
1329
1332
1330
- if ( is_data_only_array ) {
1331
1333
current_index += src;
1332
- for ( size_t j = i + 1 ; j < x. n_args ; j++ ) {
1334
+ for ( size_t j = 0 ; j < i ; j++ ) {
1333
1335
int64_t dim_size = 0 ;
1334
1336
ASRUtils::extract_value (m_dims[j].m_length , dim_size);
1335
1337
std::string length = std::to_string (dim_size);
1336
1338
current_index += " * " + length;
1337
1339
}
1338
1340
index += current_index;
1339
- } else {
1340
- current_index += " (" + src + " - " + array + " ->dims["
1341
- + std::to_string (i) + " ].lower_bound)" ;
1342
- for ( size_t j = i + 1 ; j < x.n_args ; j++ ) {
1343
- std::string length = array + " ->dims[" + std::to_string (j) + " ].length" ;
1344
- current_index += " * " + length;
1341
+ if (i < x.n_args - 1 ) {
1342
+ index += " + " ;
1345
1343
}
1346
- index += current_index;
1347
1344
}
1348
- if (i < x.n_args - 1 ) {
1349
- 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);
1370
+ }
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);
1350
1375
}
1351
1376
}
1352
- out += index + " ]" ;
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 );
1390
+ }
1353
1391
last_expr_precedence = 2 ;
1354
- src = out;
1355
1392
}
1356
1393
1357
1394
void visit_StringItem (const ASR::StringItem_t& x) {
0 commit comments