Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Nov 10, 2023
1 parent cdb47e8 commit 283be95
Show file tree
Hide file tree
Showing 71 changed files with 84 additions and 450 deletions.
120 changes: 49 additions & 71 deletions src/libasr/pass/print_arr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
ASR::stmt_t* doloop = nullptr;
ASR::stmt_t* empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, loc,
nullptr, 0, nullptr, nullptr));
nullptr, 0, nullptr, nullptr));
ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t(
al, loc, 1, 1, nullptr));
ASR::ttype_t *str_type_len_2 = ASRUtils::TYPE(ASR::make_Character_t(
Expand Down Expand Up @@ -106,28 +106,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
return doloop;
}

void print_fixed_sized_array(ASR::expr_t *arr_expr,std::vector<ASR::expr_t*>& print_body, const Location &loc) {
ASR::dimension_t* m_dims;
int n_dims = ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(arr_expr), m_dims);
int m_dim_length = ASR::down_cast<ASR::IntegerConstant_t>(m_dims->m_length)->m_n;
Vec<ASR::expr_t*> idx_vars;
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
ASR::ttype_t *int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4));
ASR::expr_t* one = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, 1, int32_type));
for (int n = 0; n < n_dims; n++) {
ASR::expr_t* idx_var = idx_vars[n];
idx_var = one;
for (int m = 0; m < m_dim_length; m++) {
ASR::expr_t* ref = PassUtils::create_array_ref(arr_expr, idx_var, al, current_scope);
print_body.push_back(ref);
idx_var = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc, idx_var,
ASR::binopType::Add, one, int32_type, nullptr));
}
}
}

ASR::stmt_t* create_formatstmt(std::vector<ASR::expr_t*> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type,
ASR::expr_t* unit = nullptr, ASR::expr_t* separator = nullptr, ASR::expr_t* end = nullptr) {
ASR::stmt_t* create_formatstmt(std::vector<ASR::expr_t*> &print_body, ASR::StringFormat_t* format, const Location &loc, ASR::stmtType _type) {
Vec<ASR::expr_t*> body;
body.reserve(al, print_body.size());
for (size_t j=0; j<print_body.size(); j++) {
Expand All @@ -142,10 +121,10 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
ASR::stmt_t* statement = nullptr;
if (_type == ASR::stmtType::Print) {
statement = ASRUtils::STMT(ASR::make_Print_t(al, loc,
print_args.p, print_args.size(), nullptr, nullptr));
print_args.p, print_args.size(), nullptr, nullptr));
} else if (_type == ASR::stmtType::FileWrite) {
statement = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc, 0, unit,
nullptr, nullptr, nullptr, print_args.p, print_args.size(), separator, end));
statement = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc, 0, nullptr,
nullptr, nullptr, nullptr, print_args.p, print_args.size(), nullptr, nullptr));
}
print_body.clear();
return statement;
Expand All @@ -157,21 +136,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
ASR::stmt_t* print_stmt;
if (x.m_values[0] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values[0])) {
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, nullptr));
nullptr, 0, nullptr, nullptr));
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values[0]);
for (size_t i=0; i<format->n_args; i++) {
if (PassUtils::is_array(format->m_args[i])) {
if (ASRUtils::is_fixed_size_array(ASRUtils::expr_type(format->m_args[i]))) {
print_fixed_sized_array(format->m_args[i], print_body, x.base.base.loc);
} else {
if (print_body.size() > 0) {
print_stmt = create_formatstmt(print_body, format, x.base.base.loc, ASR::stmtType::Print);
pass_result.push_back(al, print_stmt);
}
print_stmt = print_array_using_doloop(format->m_args[i],format, x.base.base.loc);
if (print_body.size() > 0) {
print_stmt = create_formatstmt(print_body, format, x.base.base.loc, ASR::stmtType::Print);
pass_result.push_back(al, print_stmt);
pass_result.push_back(al, empty_print_endl);
}
print_stmt = print_array_using_doloop(format->m_args[i],format, x.base.base.loc);
pass_result.push_back(al, print_stmt);
pass_result.push_back(al, empty_print_endl);
} else {
print_body.push_back(format->m_args[i]);
}
Expand All @@ -189,12 +164,17 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
ASR::expr_t *backspace = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, x.base.base.loc, s2c(al, "\b"), str_type_len_1));
ASR::stmt_t* back = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, backspace));
nullptr, 0, nullptr, backspace));
for (size_t i=0; i<x.n_values; i++) {
// DIVERGENCE between LFortran and LPython
// If a pointer array variable is provided
// then it will be printed as a normal array.
if (PassUtils::is_array(x.m_values[i])) {
// TODO: This will disallow printing array pointer in Fortran
// Pointers are treated the same as normal variables in Fortran
// However, LPython prints the address of pointers when you do
// print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
// So we need to figure out a way to de-couple printing support from libasr
// or add nodes according to the frontends because each frontend will have a different
// way of handling printing of pointers and non-pointers
if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_values[i])) &&
PassUtils::is_array(x.m_values[i])) {
if (print_body.size() > 0) {
Vec<ASR::expr_t*> body;
body.reserve(al, print_body.size());
Expand All @@ -219,18 +199,18 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
if (x.m_separator) {
if (i == x.n_values - 1) {
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, x.m_end));
nullptr, 0, nullptr, x.m_end));
} else {
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, x.m_separator));
nullptr, 0, nullptr, x.m_separator));
}
} else {
if (i == x.n_values - 1) {
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, x.m_end));
nullptr, 0, nullptr, x.m_end));
} else {
empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc,
nullptr, 0, nullptr, nullptr));
nullptr, 0, nullptr, nullptr));
}
}
pass_result.push_back(al, empty_print_endl);
Expand All @@ -252,7 +232,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
}
}

ASR::stmt_t* write_array_using_doloop(ASR::expr_t *arr_expr, ASR::StringFormat_t* format, ASR::expr_t* unit, const Location &loc) {
ASR::stmt_t* write_array_using_doloop(ASR::expr_t *arr_expr, ASR::StringFormat_t* format, const Location &loc) {
int n_dims = PassUtils::get_rank(arr_expr);
Vec<ASR::expr_t*> idx_vars;
PassUtils::create_idx_vars(idx_vars, n_dims, loc, al, current_scope);
Expand All @@ -262,7 +242,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
ASR::expr_t *empty_space = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, loc, s2c(al, ""), str_type_len));
ASR::stmt_t* empty_file_write_endl = ASRUtils::STMT(ASR::make_FileWrite_t(al, loc,
0, unit, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
0, nullptr, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
for( int i = n_dims - 1; i >= 0; i-- ) {
ASR::do_loop_head_t head;
head.m_v = idx_vars[i];
Expand All @@ -286,11 +266,11 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
format_args.reserve(al, 1);
format_args.push_back(al, string_format);
write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
al, loc, i, unit, nullptr, nullptr, nullptr,
al, loc, i, nullptr, nullptr, nullptr, nullptr,
format_args.p, format_args.size(), nullptr, empty_space));
} else {
write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
al, loc, i, unit, nullptr, nullptr, nullptr,
al, loc, i, nullptr, nullptr, nullptr, nullptr,
print_args.p, print_args.size(), nullptr, nullptr));
}
doloop_body.push_back(al, write_stmt);
Expand All @@ -307,56 +287,54 @@ class PrintArrVisitor : public PassUtils::PassVisitor<PrintArrVisitor>
Vec<ASR::expr_t*> body;
body.from_pointer_n_copy(al, write_body.data(), write_body.size());
ASR::stmt_t* write_stmt = ASRUtils::STMT(ASR::make_FileWrite_t(
al, x.base.base.loc, x.m_label, x.m_unit, x.m_iomsg,
x.m_iostat, x.m_id, body.p, body.size(), x.m_separator, x.m_end));
al, x.base.base.loc, x.m_label, x.m_unit, x.m_iomsg, x.m_iostat,
x.m_id, body.p, body.size(), x.m_separator, x.m_end));
pass_result.push_back(al, write_stmt);
write_body.clear();
}

void visit_FileWrite(const ASR::FileWrite_t& x) {
if (x.m_unit && ASRUtils::is_character(*ASRUtils::expr_type(x.m_unit))) {
// Skip for character write
return;
}
std::vector<ASR::expr_t*> write_body;
ASR::stmt_t* write_stmt;
ASR::stmt_t* empty_file_write_endl = ASRUtils::STMT(ASR::make_FileWrite_t(al, x.base.base.loc,
x.m_label, x.m_unit, nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr));
x.m_label, nullptr, nullptr, nullptr, nullptr,
nullptr, 0, nullptr, nullptr));
if(x.m_values && x.m_values[0] != nullptr && ASR::is_a<ASR::StringFormat_t>(*x.m_values[0])){
ASR::StringFormat_t* format = ASR::down_cast<ASR::StringFormat_t>(x.m_values[0]);
for (size_t i=0; i<format->n_args; i++) {
if (PassUtils::is_array(format->m_args[i])) {
if (ASRUtils::is_fixed_size_array(ASRUtils::expr_type(format->m_args[i]))) {
print_fixed_sized_array(format->m_args[i], write_body, x.base.base.loc);
} else {
if (write_body.size() > 0) {
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite, x.m_unit, x.m_separator, x.m_end);
pass_result.push_back(al, write_stmt);
}
write_stmt = write_array_using_doloop(format->m_args[i], format, x.m_unit, x.base.base.loc);
if (write_body.size() > 0) {
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite);
pass_result.push_back(al, write_stmt);
pass_result.push_back(al, empty_file_write_endl);
}
write_stmt = write_array_using_doloop(format->m_args[i],format, x.base.base.loc);
pass_result.push_back(al, write_stmt);
pass_result.push_back(al, empty_file_write_endl);
} else {
write_body.push_back(format->m_args[i]);
}
}
if (write_body.size() > 0) {
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite, x.m_unit, x.m_separator, x.m_end);
write_stmt = create_formatstmt(write_body, format, x.base.base.loc, ASR::stmtType::FileWrite);
pass_result.push_back(al, write_stmt);
}
return;
}
for (size_t i=0; i<x.n_values; i++) {
// DIVERGENCE between LFortran and LPython
// If a pointer array variable is provided
// then it will be printed as a normal array.
if (PassUtils::is_array(x.m_values[i])) {
// TODO: This will disallow printing array pointer in Fortran
// Pointers are treated the same as normal variables in Fortran
// However, LPython prints the address of pointers when you do
// print(some_pointer). Same goes for C/C++ (if we add their frontends in future).
// So we need to figure out a way to de-couple printing support from libasr
// or add nodes according to the frontends because each frontend will have a different
// way of handling printing of pointers and non-pointers
if (!ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_values[i])) &&
PassUtils::is_array(x.m_values[i])) {
if (write_body.size() > 0) {
print_args_apart_from_arrays(write_body, x);
pass_result.push_back(al, empty_file_write_endl);
}
write_stmt = write_array_using_doloop(x.m_values[i], nullptr, x.m_unit, x.base.base.loc);
write_stmt = write_array_using_doloop(x.m_values[i], nullptr, x.base.base.loc);
pass_result.push_back(al, write_stmt);
pass_result.push_back(al, empty_file_write_endl);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "1faebd012adf52a67e912023e64747cd86287bca16bcd3551011da5a",
"stdout_hash": "489d2e6a364cc6020f2942b94738849349928901f1269b975a6e2464",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/asr-array_01_decl-39cf894.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_i16_array
()
Expand All @@ -902,7 +901,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_i32_array
()
Expand All @@ -926,7 +924,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_i64_array
()
Expand All @@ -950,7 +947,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_f32_array
()
Expand All @@ -974,7 +970,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_f64_array
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "87598ba9c49806e4e943c164621a63720d3ff12ad599f3dd0eabe85a",
"stdout_hash": "51a219ffb9a2c93f90e7a7c2928ef6f229796a81c72b03208be94602",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
4 changes: 0 additions & 4 deletions tests/reference/asr-array_02_decl-e8f6874.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_multidim_i32_array
()
Expand All @@ -710,7 +709,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_multidim_i64_array
()
Expand Down Expand Up @@ -738,7 +736,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_multidim_f32_array
()
Expand All @@ -762,7 +759,6 @@
()
)
(Print
()
[(FunctionCall
2 accept_multidim_f64_array
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-bindc_01-6d521a9.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-bindc_01-6d521a9.stdout",
"stdout_hash": "ec7543eae1d1bbaeba72993e35e98165ba0248ab59dc6597f9c50e7b",
"stdout_hash": "2a346b7a00bd685bde6e5c686bda4012fa442eac78bf03bad9d846aa",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
1 change: 0 additions & 1 deletion tests/reference/asr-bindc_01-6d521a9.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
()
)
(Print
()
[(Var 2 queries)
(Var 2 x)]
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-bindc_02-bc1a7ea.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-bindc_02-bc1a7ea.stdout",
"stdout_hash": "7e3c2190d73d9f8614974002f89340fbd517b3e44d0a5ee3941e08de",
"stdout_hash": "96d620a26be932ce9f2bcddc31e7f6bebefc49f82923c3605b9b828c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 283be95

Please sign in to comment.