Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 283be95

Browse files
committed
done
1 parent cdb47e8 commit 283be95

File tree

71 files changed

+84
-450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+84
-450
lines changed

src/libasr/pass/print_arr.cpp

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
5959
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
6060
ASR::stmt_t* doloop = nullptr;
6161
ASR::stmt_t* empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, loc,
62-
nullptr, 0, nullptr, nullptr));
62+
nullptr, 0, nullptr, nullptr));
6363
ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t(
6464
al, loc, 1, 1, nullptr));
6565
ASR::ttype_t *str_type_len_2 = ASRUtils::TYPE(ASR::make_Character_t(
@@ -106,28 +106,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
106106
return doloop;
107107
}
108108

109-
void print_fixed_sized_array(ASR::expr_t *arr_expr,std::vector<ASR::expr_t*>& print_body, const Location &loc) {
110-
ASR::dimension_t* m_dims;
111-
int n_dims = ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(arr_expr), m_dims);
112-
int m_dim_length = ASR::down_cast<ASR::IntegerConstant_t>(m_dims->m_length)->m_n;
113-
Vec<ASR::expr_t*> idx_vars;
114-
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
115-
ASR::ttype_t *int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4));
116-
ASR::expr_t* one = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, 1, int32_type));
117-
for (int n = 0; n < n_dims; n++) {
118-
ASR::expr_t* idx_var = idx_vars[n];
119-
idx_var = one;
120-
for (int m = 0; m < m_dim_length; m++) {
121-
ASR::expr_t* ref = PassUtils::create_array_ref(arr_expr, idx_var, al, current_scope);
122-
print_body.push_back(ref);
123-
idx_var = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc, idx_var,
124-
ASR::binopType::Add, one, int32_type, nullptr));
125-
}
126-
}
127-
}
128-
129-
ASR::stmt_t* create_formatstmt(std::vector<ASR::expr_t*> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type,
130-
ASR::expr_t* unit = nullptr, ASR::expr_t* separator = nullptr, ASR::expr_t* end = nullptr) {
109+
ASR::stmt_t* create_formatstmt(std::vector<ASR::expr_t*> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type) {
131110
Vec<ASR::expr_t*> body;
132111
body.reserve(al, print_body.size());
133112
for (size_t j=0; j<print_body.size(); j++) {
@@ -142,10 +121,10 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
142121
ASR::stmt_t* statement = nullptr;
143122
if (_type == ASR::stmtType::Print) {
144123
statement = ASRUtils::STMT(ASR::make_Print_t(al, loc,
145-
print_args.p, print_args.size(), nullptr, nullptr));
124+
print_args.p, print_args.size(), nullptr, nullptr));
146125
} else if (_type == ASR::stmtType::FileWrite) {
147-
statement = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc, 0, unit,
148-
nullptr, nullptr, nullptr, print_args.p, print_args.size(), separator, end));
126+
statement = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc, 0, nullptr,
127+
nullptr, nullptr, nullptr, print_args.p, print_args.size(), nullptr, nullptr));
149128
}
150129
print_body.clear();
151130
return statement;
@@ -157,21 +136,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
157136
ASR::stmt_t* print_stmt;
158137
if (x.m_values[0] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values[0])) {
159138
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
160-
nullptr, 0, nullptr, nullptr));
139+
nullptr, 0, nullptr, nullptr));
161140
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values[0]);
162141
for (size_t i=0; i<format->n_args; i++) {
163142
if (PassUtils::is_array(format->m_args[i])) {
164-
if (ASRUtils::is_fixed_size_array(ASRUtils::expr_type(format->m_args[i]))) {
165-
print_fixed_sized_array(format->m_args[i], print_body, x.base.base.loc);
166-
} else {
167-
if (print_body.size() > 0) {
168-
print_stmt = create_formatstmt(print_body, format, x.base.base.loc, ASR::stmtType::Print);
169-
pass_result.push_back(al, print_stmt);
170-
}
171-
print_stmt = print_array_using_doloop(format->m_args[i],format, x.base.base.loc);
143+
if (print_body.size() > 0) {
144+
print_stmt = create_formatstmt(print_body, format, x.base.base.loc, ASR::stmtType::Print);
172145
pass_result.push_back(al, print_stmt);
173-
pass_result.push_back(al, empty_print_endl);
174146
}
147+
print_stmt = print_array_using_doloop(format->m_args[i],format, x.base.base.loc);
148+
pass_result.push_back(al, print_stmt);
149+
pass_result.push_back(al, empty_print_endl);
175150
} else {
176151
print_body.push_back(format->m_args[i]);
177152
}
@@ -189,12 +164,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
189164
ASR::expr_t *backspace = ASRUtils::EXPR(ASR::make_StringConstant_t(
190165
al, x.base.base.loc, s2c(al, "\b"), str_type_len_1));
191166
ASR::stmt_t* back = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
192-
nullptr, 0, nullptr, backspace));
167+
nullptr, 0, nullptr, backspace));
193168
for (size_t i=0; i<x.n_values; i++) {
194-
// DIVERGENCE between LFortran and LPython
195-
// If a pointer array variable is provided
196-
// then it will be printed as a normal array.
197-
if (PassUtils::is_array(x.m_values[i])) {
169+
// TODO: This will disallow printing array pointer in Fortran
170+
// Pointers are treated the same as normal variables in Fortran
171+
// However, LPython prints the address of pointers when you do
172+
// print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
173+
// So we need to figure out a way to de-couple printing support from libasr
174+
// or add nodes according to the frontends because each frontend will have a different
175+
// way of handling printing of pointers and non-pointers
176+
if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_values[i])) &&
177+
PassUtils::is_array(x.m_values[i])) {
198178
if (print_body.size() > 0) {
199179
Vec<ASR::expr_t*> body;
200180
body.reserve(al, print_body.size());
@@ -219,18 +199,18 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
219199
if (x.m_separator) {
220200
if (i == x.n_values - 1) {
221201
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
222-
nullptr, 0, nullptr, x.m_end));
202+
nullptr, 0, nullptr, x.m_end));
223203
} else {
224204
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
225-
nullptr, 0, nullptr, x.m_separator));
205+
nullptr, 0, nullptr, x.m_separator));
226206
}
227207
} else {
228208
if (i == x.n_values - 1) {
229209
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
230-
nullptr, 0, nullptr, x.m_end));
210+
nullptr, 0, nullptr, x.m_end));
231211
} else {
232212
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
233-
nullptr, 0, nullptr, nullptr));
213+
nullptr, 0, nullptr, nullptr));
234214
}
235215
}
236216
pass_result.push_back(al, empty_print_endl);
@@ -252,7 +232,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
252232
}
253233
}
254234

255-
ASR::stmt_t* write_array_using_doloop(ASR::expr_t *arr_expr, ASR::StringFormat_t* format, ASR::expr_t* unit, const Location &loc) {
235+
ASR::stmt_t* write_array_using_doloop(ASR::expr_t *arr_expr, ASR::StringFormat_t* format, const Location &loc) {
256236
int n_dims = PassUtils::get_rank(arr_expr);
257237
Vec<ASR::expr_t*> idx_vars;
258238
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
@@ -262,7 +242,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
262242
ASR::expr_t *empty_space = ASRUtils::EXPR(ASR::make_StringConstant_t(
263243
al, loc, s2c(al, ""), str_type_len));
264244
ASR::stmt_t* empty_file_write_endl = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc,
265-
0, unit, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
245+
0, nullptr, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
266246
for( int i = n_dims - 1; i >= 0; i-- ) {
267247
ASR::do_loop_head_t head;
268248
head.m_v = idx_vars[i];
@@ -286,11 +266,11 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
286266
format_args.reserve(al, 1);
287267
format_args.push_back(al, string_format);
288268
write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
289-
al, loc, i, unit, nullptr, nullptr, nullptr,
269+
al, loc, i, nullptr, nullptr, nullptr, nullptr,
290270
format_args.p, format_args.size(), nullptr, empty_space));
291271
} else {
292272
write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
293-
al, loc, i, unit, nullptr, nullptr, nullptr,
273+
al, loc, i, nullptr, nullptr, nullptr, nullptr,
294274
print_args.p, print_args.size(), nullptr, nullptr));
295275
}
296276
doloop_body.push_back(al, write_stmt);
@@ -307,56 +287,54 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
307287
Vec<ASR::expr_t*> body;
308288
body.from_pointer_n_copy(al, write_body.data(), write_body.size());
309289
ASR::stmt_t* write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
310-
al, x.base.base.loc, x.m_label, x.m_unit, x.m_iomsg,
311-
x.m_iostat, x.m_id, body.p, body.size(), x.m_separator, x.m_end));
290+
al, x.base.base.loc, x.m_label, x.m_unit, x.m_iomsg, x.m_iostat,
291+
x.m_id, body.p, body.size(), x.m_separator, x.m_end));
312292
pass_result.push_back(al, write_stmt);
313293
write_body.clear();
314294
}
315295

316296
void visit_FileWrite(const ASR::FileWrite_t& x) {
317-
if (x.m_unit && ASRUtils::is_character(*ASRUtils::expr_type(x.m_unit))) {
318-
// Skip for character write
319-
return;
320-
}
321297
std::vector<ASR::expr_t*> write_body;
322298
ASR::stmt_t* write_stmt;
323299
ASR::stmt_t* empty_file_write_endl = ASRUtils::STMT(ASR::make_FileWrite_t(al, x.base.base.loc,
324-
x.m_label, x.m_unit, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
300+
x.m_label, nullptr, nullptr, nullptr, nullptr,
301+
nullptr, 0, nullptr, nullptr));
325302
if(x.m_values && x.m_values[0] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values[0])){
326303
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values[0]);
327304
for (size_t i=0; i<format->n_args; i++) {
328305
if (PassUtils::is_array(format->m_args[i])) {
329-
if (ASRUtils::is_fixed_size_array(ASRUtils::expr_type(format->m_args[i]))) {
330-
print_fixed_sized_array(format->m_args[i], write_body, x.base.base.loc);
331-
} else {
332-
if (write_body.size() > 0) {
333-
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite, x.m_unit, x.m_separator, x.m_end);
334-
pass_result.push_back(al, write_stmt);
335-
}
336-
write_stmt = write_array_using_doloop(format->m_args[i], format, x.m_unit, x.base.base.loc);
306+
if (write_body.size() > 0) {
307+
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite);
337308
pass_result.push_back(al, write_stmt);
338-
pass_result.push_back(al, empty_file_write_endl);
339309
}
310+
write_stmt = write_array_using_doloop(format->m_args[i],format, x.base.base.loc);
311+
pass_result.push_back(al, write_stmt);
312+
pass_result.push_back(al, empty_file_write_endl);
340313
} else {
341314
write_body.push_back(format->m_args[i]);
342315
}
343316
}
344317
if (write_body.size() > 0) {
345-
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite, x.m_unit, x.m_separator, x.m_end);
318+
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite);
346319
pass_result.push_back(al, write_stmt);
347320
}
348321
return;
349322
}
350323
for (size_t i=0; i<x.n_values; i++) {
351-
// DIVERGENCE between LFortran and LPython
352-
// If a pointer array variable is provided
353-
// then it will be printed as a normal array.
354-
if (PassUtils::is_array(x.m_values[i])) {
324+
// TODO: This will disallow printing array pointer in Fortran
325+
// Pointers are treated the same as normal variables in Fortran
326+
// However, LPython prints the address of pointers when you do
327+
// print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
328+
// So we need to figure out a way to de-couple printing support from libasr
329+
// or add nodes according to the frontends because each frontend will have a different
330+
// way of handling printing of pointers and non-pointers
331+
if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_values[i])) &&
332+
PassUtils::is_array(x.m_values[i])) {
355333
if (write_body.size() > 0) {
356334
print_args_apart_from_arrays(write_body, x);
357335
pass_result.push_back(al, empty_file_write_endl);
358336
}
359-
write_stmt = write_array_using_doloop(x.m_values[i], nullptr, x.m_unit, x.base.base.loc);
337+
write_stmt = write_array_using_doloop(x.m_values[i], nullptr, x.base.base.loc);
360338
pass_result.push_back(al, write_stmt);
361339
pass_result.push_back(al, empty_file_write_endl);
362340
} else {

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "1faebd012adf52a67e912023e64747cd86287bca16bcd3551011da5a",
9+
"stdout_hash": "489d2e6a364cc6020f2942b94738849349928901f1269b975a6e2464",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_01_decl-39cf894.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@
878878
()
879879
)
880880
(Print
881-
()
882881
[(FunctionCall
883882
2 accept_i16_array
884883
()
@@ -902,7 +901,6 @@
902901
()
903902
)
904903
(Print
905-
()
906904
[(FunctionCall
907905
2 accept_i32_array
908906
()
@@ -926,7 +924,6 @@
926924
()
927925
)
928926
(Print
929-
()
930927
[(FunctionCall
931928
2 accept_i64_array
932929
()
@@ -950,7 +947,6 @@
950947
()
951948
)
952949
(Print
953-
()
954950
[(FunctionCall
955951
2 accept_f32_array
956952
()
@@ -974,7 +970,6 @@
974970
()
975971
)
976972
(Print
977-
()
978973
[(FunctionCall
979974
2 accept_f64_array
980975
()

tests/reference/asr-array_02_decl-e8f6874.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_02_decl-e8f6874.stdout",
9-
"stdout_hash": "87598ba9c49806e4e943c164621a63720d3ff12ad599f3dd0eabe85a",
9+
"stdout_hash": "51a219ffb9a2c93f90e7a7c2928ef6f229796a81c72b03208be94602",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_02_decl-e8f6874.stdout

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@
684684
()
685685
)
686686
(Print
687-
()
688687
[(FunctionCall
689688
2 accept_multidim_i32_array
690689
()
@@ -710,7 +709,6 @@
710709
()
711710
)
712711
(Print
713-
()
714712
[(FunctionCall
715713
2 accept_multidim_i64_array
716714
()
@@ -738,7 +736,6 @@
738736
()
739737
)
740738
(Print
741-
()
742739
[(FunctionCall
743740
2 accept_multidim_f32_array
744741
()
@@ -762,7 +759,6 @@
762759
()
763760
)
764761
(Print
765-
()
766762
[(FunctionCall
767763
2 accept_multidim_f64_array
768764
()

tests/reference/asr-bindc_01-6d521a9.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_01-6d521a9.stdout",
9-
"stdout_hash": "ec7543eae1d1bbaeba72993e35e98165ba0248ab59dc6597f9c50e7b",
9+
"stdout_hash": "2a346b7a00bd685bde6e5c686bda4012fa442eac78bf03bad9d846aa",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_01-6d521a9.stdout

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
()
3939
)
4040
(Print
41-
()
4241
[(Var 2 queries)
4342
(Var 2 x)]
4443
()

tests/reference/asr-bindc_02-bc1a7ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "7e3c2190d73d9f8614974002f89340fbd517b3e44d0a5ee3941e08de",
9+
"stdout_hash": "96d620a26be932ce9f2bcddc31e7f6bebefc49f82923c3605b9b828c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)