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

Skip to content

Commit cdb47e8

Browse files
committed
wip
1 parent d58d006 commit cdb47e8

File tree

11 files changed

+198
-126
lines changed

11 files changed

+198
-126
lines changed

src/libasr/ASR.asdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ stmt
198198
| GoToTarget(int id, identifier name)
199199
| If(expr test, stmt* body, stmt* orelse)
200200
| IfArithmetic(expr test, int lt_label, int eq_label, int gt_label)
201-
| Print(expr? fmt, expr* values, expr? separator, expr? end)
201+
| Print(expr* values, expr? separator, expr? end)
202202
| FileOpen(int label, expr? newunit, expr? filename, expr? status, expr? form)
203203
| FileClose(int label, expr? unit, expr? iostat, expr? iomsg, expr? err, expr? status)
204204
| FileRead(int label, expr? unit, expr? fmt, expr? iomsg, expr? iostat, expr? id, expr* values)
@@ -212,7 +212,7 @@ stmt
212212
expr? read, expr? write, expr? readwrite, expr? delim,
213213
expr? pad, expr? flen, expr? blocksize, expr? convert,
214214
expr? carriagecontrol, expr? iolength)
215-
| FileWrite(int label, expr? unit, expr? fmt, expr? iomsg, expr? iostat, expr? id, expr* values, expr? separator, expr? end)
215+
| FileWrite(int label, expr? unit, expr? iomsg, expr? iostat, expr? id, expr* values, expr? separator, expr? end)
216216
| Return()
217217
| Select(expr test, case_stmt* body, stmt* default)
218218
| Stop(expr? code)

src/libasr/asdl_cpp.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,6 @@ def make_visitor(self, name, fields, cons):
18121812
self.emit('s.append(",\\n" + indtd);', 2)
18131813
self.emit('dec_indent(); s.append("\\n" + indtd);', 2)
18141814
self.emit( 's.append("}");', 2)
1815-
self.emit( 's.append(",\\n" + indtd);', 2)
18161815
if name in products:
18171816
self.emit( 'append_location(s, x.loc.first, x.loc.last);', 2)
18181817
else:

src/libasr/pass/intrinsic_function_registry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ class ASRBuilder {
865865
// Used for debugging
866866
Vec<ASR::expr_t *> x_exprs;
867867
x_exprs.from_pointer_n_copy(al, &items[0], items.size());
868-
return STMT(ASR::make_Print_t(al, loc, nullptr, x_exprs.p, x_exprs.n,
868+
return STMT(ASR::make_Print_t(al, loc, x_exprs.p, x_exprs.n,
869869
nullptr, nullptr));
870870
}
871871

src/libasr/pass/pass_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ namespace LCompilers {
392392
apply_default_passes = false;
393393
}
394394

395+
void use_fortran_passes() {
396+
_user_defined_passes.push_back("unique_symbols");
397+
}
395398
};
396399

397400
}

src/libasr/pass/print_arr.cpp

Lines changed: 80 additions & 54 deletions
Large diffs are not rendered by default.

src/libasr/pass/print_list_tuple.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ class PrintListTupleVisitor
149149
}
150150

151151
ASR::stmt_t *print_open_bracket = ASRUtils::STMT(
152-
ASR::make_Print_t(al, loc, nullptr, v1.p, v1.size(),
152+
ASR::make_Print_t(al, loc, v1.p, v1.size(),
153153
nullptr, empty_str));
154154
ASR::stmt_t *print_comma_space = ASRUtils::STMT(
155-
ASR::make_Print_t(al, loc, nullptr, v4.p, v4.size(),
155+
ASR::make_Print_t(al, loc, v4.p, v4.size(),
156156
empty_str, empty_str));
157157
ASR::stmt_t *print_item = ASRUtils::STMT(
158-
ASR::make_Print_t(al, loc, nullptr, v2.p, v2.size(),
158+
ASR::make_Print_t(al, loc, v2.p, v2.size(),
159159
empty_str, empty_str));
160160
ASR::stmt_t *print_close_bracket = ASRUtils::STMT(
161-
ASR::make_Print_t(al, loc, nullptr, v3.p, v3.size(),
161+
ASR::make_Print_t(al, loc, v3.p, v3.size(),
162162
sep_expr, end_expr));
163163

164164
Vec<ASR::stmt_t *> if_body;
@@ -245,13 +245,13 @@ class PrintListTupleVisitor
245245
Vec<ASR::stmt_t*> tmp_vec;
246246
tmp_vec.reserve(al, 3);
247247
ASR::stmt_t *print_open_bracket = ASRUtils::STMT(
248-
ASR::make_Print_t(al, loc, nullptr, v1.p, v1.size(),
248+
ASR::make_Print_t(al, loc, v1.p, v1.size(),
249249
nullptr, empty_str));
250250
ASR::stmt_t *print_comma_space = ASRUtils::STMT(
251-
ASR::make_Print_t(al, loc, nullptr, v4.p, v4.size(),
251+
ASR::make_Print_t(al, loc, v4.p, v4.size(),
252252
empty_str, empty_str));
253253
ASR::stmt_t *print_close_bracket = ASRUtils::STMT(
254-
ASR::make_Print_t(al, loc, nullptr, v3.p, v3.size(),
254+
ASR::make_Print_t(al, loc, v3.p, v3.size(),
255255
sep_expr, end_expr));
256256

257257
tmp_vec.push_back(al, print_open_bracket);
@@ -286,7 +286,7 @@ class PrintListTupleVisitor
286286
v2.push_back(al, tup_item);
287287
}
288288
ASR::stmt_t *print_item = ASRUtils::STMT(
289-
ASR::make_Print_t(al, loc, nullptr, v2.p, v2.size(),
289+
ASR::make_Print_t(al, loc, v2.p, v2.size(),
290290
empty_str, empty_str));
291291
tmp_vec.push_back(al, print_item);
292292
}
@@ -316,11 +316,11 @@ class PrintListTupleVisitor
316316
}
317317
if (x.m_separator) {
318318
print_stmt = ASRUtils::STMT(ASR::make_Print_t(al,
319-
x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(),
319+
x.base.base.loc, tmp_vec.p, tmp_vec.size(),
320320
x.m_separator, x.m_separator));
321321
} else {
322322
print_stmt = ASRUtils::STMT(ASR::make_Print_t(al,
323-
x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(),
323+
x.base.base.loc, tmp_vec.p, tmp_vec.size(),
324324
x.m_separator, space));
325325
}
326326
print_tmp.clear();
@@ -353,7 +353,7 @@ class PrintListTupleVisitor
353353
tmp_vec.push_back(al, e);
354354
}
355355
ASR::stmt_t *print_stmt = ASRUtils::STMT(
356-
ASR::make_Print_t(al, x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(),
356+
ASR::make_Print_t(al, x.base.base.loc, tmp_vec.p, tmp_vec.size(),
357357
x.m_separator, x.m_end));
358358
print_tmp.clear();
359359
pass_result.push_back(al, print_stmt);

src/libasr/pass/replace_symbolic.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
13981398

13991399
for (size_t i=0; i<x.n_args; i++) {
14001400
ASR::expr_t* val = x.m_args[i].m_value;
1401-
if (ASR::is_a<ASR::IntrinsicScalarFunction_t>(*val) && ASR::is_a<ASR::SymbolicExpression_t>(*ASRUtils::expr_type(val))) {
1401+
if (val && ASR::is_a<ASR::IntrinsicScalarFunction_t>(*val) && ASR::is_a<ASR::SymbolicExpression_t>(*ASRUtils::expr_type(val))) {
14021402
ASR::IntrinsicScalarFunction_t* intrinsic_func = ASR::down_cast<ASR::IntrinsicScalarFunction_t>(val);
14031403
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_SymbolicExpression_t(al, x.base.base.loc));
14041404
std::string symengine_var = symengine_stack.push();
@@ -1421,7 +1421,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
14211421
call_arg.loc = x.base.base.loc;
14221422
call_arg.m_value = target;
14231423
call_args.push_back(al, call_arg);
1424-
} else if (ASR::is_a<ASR::Cast_t>(*val)) {
1424+
} else if (val && ASR::is_a<ASR::Cast_t>(*val)) {
14251425
ASR::Cast_t* cast_t = ASR::down_cast<ASR::Cast_t>(val);
14261426
if(cast_t->m_kind != ASR::cast_kindType::IntegerToSymbolicExpression) return;
14271427
this->visit_Cast(*cast_t);
@@ -1437,7 +1437,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
14371437
}
14381438
}
14391439
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, x.base.base.loc, x.m_name,
1440-
x.m_name, call_args.p, call_args.n, nullptr));
1440+
x.m_name, call_args.p, call_args.n, x.m_dt));
14411441
pass_result.push_back(al, stmt);
14421442
}
14431443

@@ -1576,7 +1576,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
15761576
tmp_vec.push_back(al, e);
15771577
}
15781578
ASR::stmt_t *print_stmt = ASRUtils::STMT(
1579-
ASR::make_Print_t(al, x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(),
1579+
ASR::make_Print_t(al, x.base.base.loc, tmp_vec.p, tmp_vec.size(),
15801580
x.m_separator, x.m_end));
15811581
print_tmp.clear();
15821582
pass_result.push_back(al, print_stmt);

src/libasr/pass/unique_symbols.cpp

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
4545
bool intrinsic_symbols_mangling;
4646
bool all_symbols_mangling;
4747
bool bindc_mangling = false;
48+
bool fortran_mangling;
4849
bool should_mangle = false;
4950
std::vector<std::string> parent_function_name;
5051
std::string module_name = "";
5152
SymbolTable* current_scope = nullptr;
5253

53-
SymbolRenameVisitor(
54-
bool mm, bool gm, bool im, bool am, bool bcm) : module_name_mangling(mm),
55-
global_symbols_mangling(gm), intrinsic_symbols_mangling(im),
56-
all_symbols_mangling(am), bindc_mangling(bcm){}
54+
SymbolRenameVisitor(bool mm, bool gm, bool im, bool am, bool bcm, bool fm) :
55+
module_name_mangling(mm), global_symbols_mangling(gm), intrinsic_symbols_mangling(im),
56+
all_symbols_mangling(am), bindc_mangling(bcm), fortran_mangling(fm) {}
5757

5858

5959
std::string update_name(std::string curr_name) {
@@ -84,12 +84,17 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
8484
}
8585

8686
void visit_Program(const ASR::Program_t &x) {
87+
SymbolTable *current_scope_copy = current_scope;
88+
current_scope = x.m_symtab;
8789
for (auto &a : x.m_symtab->get_scope()) {
8890
visit_symbol(*a.second);
8991
}
92+
current_scope = current_scope_copy;
9093
}
9194

9295
void visit_Module(const ASR::Module_t &x) {
96+
SymbolTable *current_scope_copy = current_scope;
97+
current_scope = x.m_symtab;
9398
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
9499
bool should_mangle_copy = should_mangle;
95100
std::string mod_name_copy = module_name;
@@ -106,6 +111,7 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
106111
}
107112
should_mangle = should_mangle_copy;
108113
module_name = mod_name_copy;
114+
current_scope = current_scope_copy;
109115
}
110116

111117
bool is_nested_function(ASR::symbol_t *sym) {
@@ -124,12 +130,24 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
124130
}
125131

126132
void visit_Function(const ASR::Function_t &x) {
133+
SymbolTable *current_scope_copy = current_scope;
134+
current_scope = x.m_symtab;
127135
ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x);
128136
if (bindc_mangling || f_type->m_abi != ASR::abiType::BindC) {
129137
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
130138
if (all_symbols_mangling || should_mangle) {
131139
sym_to_renamed[sym] = update_name(x.m_name);
132140
}
141+
if ( fortran_mangling ) {
142+
if ( sym_to_renamed.find(sym) != sym_to_renamed.end()
143+
&& startswith(sym_to_renamed[sym], "_") ) {
144+
sym_to_renamed[sym] = current_scope->parent->get_unique_name(
145+
"f" + sym_to_renamed[sym]);
146+
} else if ( startswith(x.m_name, "_") ) {
147+
sym_to_renamed[sym] = current_scope->parent->get_unique_name(
148+
"f" + std::string(x.m_name));
149+
}
150+
}
133151
}
134152
for (auto &a : x.m_symtab->get_scope()) {
135153
bool nested_function = is_nested_function(a.second);
@@ -141,14 +159,25 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor<SymbolRenameVisitor> {
141159
parent_function_name.pop_back();
142160
}
143161
}
162+
current_scope = current_scope_copy;
144163
}
145164

146165
template <typename T>
147166
void visit_symbols_1(T &x) {
167+
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
148168
if (all_symbols_mangling || should_mangle) {
149-
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
150169
sym_to_renamed[sym] = update_name(x.m_name);
151170
}
171+
if ( fortran_mangling ) {
172+
if ( sym_to_renamed.find(sym) != sym_to_renamed.end()
173+
&& startswith(sym_to_renamed[sym], "_") ) {
174+
sym_to_renamed[sym] = current_scope->get_unique_name("v" +
175+
sym_to_renamed[sym]);
176+
} else if ( startswith(x.m_name, "_") ) {
177+
sym_to_renamed[sym] = current_scope->get_unique_name("v" +
178+
std::string(x.m_name));
179+
}
180+
}
152181
}
153182

154183
void visit_GenericProcedure(const ASR::GenericProcedure_t &x) {
@@ -472,19 +501,38 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
472501

473502
void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
474503
const LCompilers::PassOptions& pass_options) {
504+
/*
505+
* This pass is applied iff the following options are passed; otherwise, return
506+
* MANGLING_OPTION="--all-mangling"
507+
* MANGLING_OPTION="--module-mangling"
508+
* MANGLING_OPTION="--global-mangling"
509+
* MANGLING_OPTION="--intrinsic-mangling"
510+
* COMPILER_SPECIFIC_OPTION="--generate-object-code" // LFortran
511+
* COMPILER_SPECIFIC_OPTION="--separate-compilation" // LPython
512+
* Usage:
513+
* `$MANGLING_OPTION $COMPILER_SPECIFIC_OPTION`
514+
* The following are used by LFortran, Usage:
515+
* `$MANGLING_OPTIONS --mangle-underscore [$COMPILER_SPECIFIC_OPTION]`
516+
* * `--apply-fortran-mangling [$MANGLING_OPTION] [$COMPILER_SPECIFIC_OPTION]`
517+
*/
475518
bool any_present = (pass_options.module_name_mangling || pass_options.global_symbols_mangling ||
476-
pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling || pass_options.bindc_mangling);
519+
pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling ||
520+
pass_options.bindc_mangling || pass_options.fortran_mangling);
477521
if (pass_options.mangle_underscore) {
478522
lcompilers_unique_ID = "";
479523
}
480-
if (!any_present || ( !pass_options.mangle_underscore && lcompilers_unique_ID.empty() )) {
524+
if (!any_present || (!(pass_options.mangle_underscore ||
525+
pass_options.fortran_mangling) && lcompilers_unique_ID.empty())) {
526+
// `--mangle-underscore` doesn't require `lcompilers_unique_ID`
527+
// `lcompilers_unique_ID` is not mandatory for `--apply-fortran-mangling`
481528
return;
482529
}
483530
SymbolRenameVisitor v(pass_options.module_name_mangling,
484531
pass_options.global_symbols_mangling,
485532
pass_options.intrinsic_symbols_mangling,
486533
pass_options.all_symbols_mangling,
487-
pass_options.bindc_mangling);
534+
pass_options.bindc_mangling,
535+
pass_options.fortran_mangling);
488536
v.visit_TranslationUnit(unit);
489537
UniqueSymbolVisitor u(al, v.sym_to_renamed);
490538
u.visit_TranslationUnit(unit);

0 commit comments

Comments
 (0)