Skip to content

Commit bd2f8b8

Browse files
committed
Add cmd line options
1 parent aaeb641 commit bd2f8b8

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ RUN(NAME structs_13 LABELS llvm c
589589
RUN(NAME structs_14 LABELS cpython llvm c)
590590
RUN(NAME structs_15 LABELS cpython llvm c)
591591
RUN(NAME structs_16 LABELS cpython llvm c)
592-
#RUN(NAME structs_17 LABELS cpython llvm c)
592+
RUN(NAME structs_17 LABELS cpython llvm c)
593593
RUN(NAME structs_18 LABELS cpython llvm c
594594
EXTRAFILES structs_18b.c)
595595
RUN(NAME structs_19 LABELS cpython llvm c

src/bin/lpython.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ int emit_asr(const std::string &infile,
243243
pass_options.always_run = true;
244244
pass_options.verbose = compiler_options.verbose;
245245
pass_options.pass_cumulative = compiler_options.pass_cumulative;
246+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
247+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
248+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
249+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
250+
246251

247252
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
248253

@@ -345,6 +350,10 @@ int emit_c(const std::string &infile,
345350
pass_options.run_fun = "f";
346351
pass_options.always_run = true;
347352
pass_options.verbose = compiler_options.verbose;
353+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
354+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
355+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
356+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
348357

349358
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
350359

@@ -398,6 +407,10 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
398407
pass_options.run_fun = "f";
399408
pass_options.always_run = true;
400409
pass_options.verbose = compiler_options.verbose;
410+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
411+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
412+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
413+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
401414

402415
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
403416

@@ -1501,6 +1514,7 @@ int main(int argc, char *argv[])
15011514
bool print_targets = false;
15021515
bool print_rtl_header_dir = false;
15031516
bool print_rtl_dir = false;
1517+
bool separate_compilation = false;
15041518

15051519
std::string arg_fmt_file;
15061520
// int arg_fmt_indent = 4;
@@ -1579,6 +1593,11 @@ int main(int argc, char *argv[])
15791593
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
15801594
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
15811595
app.add_flag("--link-numpy", compiler_options.link_numpy, "Enable NumPy runtime (implies --enable-cpython)");
1596+
app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols");
1597+
app.add_flag("--module-mangling", compiler_options.module_name_mangling, "Mangles the module name");
1598+
app.add_flag("--global-mangling", compiler_options.global_symbols_mangling, "Mangles all the global symbols");
1599+
app.add_flag("--intrinsic-mangling", compiler_options.intrinsic_symbols_mangling, "Mangles all the intrinsic symbols");
1600+
app.add_flag("--all-mangling", compiler_options.all_symbols_mangling, "Mangles all possible symbols");
15821601

15831602
// LSP specific options
15841603
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
@@ -1616,7 +1635,7 @@ int main(int argc, char *argv[])
16161635
app.require_subcommand(0, 1);
16171636
CLI11_PARSE(app, argc, argv);
16181637

1619-
lcompilers_unique_ID = LCompilers::get_unique_ID();
1638+
lcompilers_unique_ID = separate_compilation ? LCompilers::get_unique_ID(): "";
16201639

16211640

16221641
if( compiler_options.fast && compiler_options.enable_bounds_checking ) {

src/libasr/asr_scopes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name,
8585

8686
std::string SymbolTable::get_unique_name(const std::string &name, bool use_unique_id) {
8787
std::string unique_name = name;
88-
if( use_unique_id ) {
88+
if( use_unique_id && !lcompilers_unique_ID.empty()) {
8989
unique_name += "_" + lcompilers_unique_ID;
9090
}
9191
int counter = 1;

src/libasr/pass/unique_symbols.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
232232

233233
public:
234234
std::unordered_map<ASR::symbol_t*, std::string>& sym_to_new_name;
235-
std::unordered_map<std::string, ASR::symbol_t*> current_scope;
235+
std::map<std::string, ASR::symbol_t*> current_scope;
236236

237237
UniqueSymbolVisitor(Allocator& al_,
238238
std::unordered_map<ASR::symbol_t*, std::string> &sn) : al(al_), sym_to_new_name(sn){}
239239

240240

241241
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
242242
ASR::TranslationUnit_t& xx = const_cast<ASR::TranslationUnit_t&>(x);
243-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
243+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
244244
current_scope = x.m_global_scope->get_scope();
245245
for (auto &a : xx.m_global_scope->get_scope()) {
246246
visit_symbol(*a.second);
@@ -256,7 +256,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
256256

257257
void visit_Program(const ASR::Program_t &x) {
258258
ASR::Program_t& xx = const_cast<ASR::Program_t&>(x);
259-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
259+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
260260
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
261261
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
262262
xx.m_name = s2c(al, sym_to_new_name[sym]);
@@ -284,7 +284,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
284284

285285
void visit_Module(const ASR::Module_t &x) {
286286
ASR::Module_t& xx = const_cast<ASR::Module_t&>(x);
287-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
287+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
288288
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
289289
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
290290
xx.m_name = s2c(al, sym_to_new_name[sym]);
@@ -312,7 +312,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
312312

313313
void visit_Function(const ASR::Function_t &x) {
314314
ASR::Function_t& xx = const_cast<ASR::Function_t&>(x);
315-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
315+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
316316
ASR::symbol_t *sym = ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&x);
317317
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
318318
xx.m_name = s2c(al, sym_to_new_name[sym]);
@@ -376,7 +376,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
376376
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
377377
xx.m_name = s2c(al, sym_to_new_name[sym]);
378378
}
379-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
379+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
380380
for (size_t i=0; i<xx.n_dependencies; i++) {
381381
if (current_scope.find(xx.m_dependencies[i]) != current_scope.end()) {
382382
sym = current_scope[xx.m_dependencies[i]];
@@ -412,7 +412,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
412412
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
413413
xx.m_name = s2c(al, sym_to_new_name[sym]);
414414
}
415-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
415+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
416416
for (size_t i=0; i<xx.n_dependencies; i++) {
417417
if (current_scope.find(xx.m_dependencies[i]) != current_scope.end()) {
418418
sym = current_scope[xx.m_dependencies[i]];
@@ -448,7 +448,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
448448
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
449449
xx.m_name = s2c(al, sym_to_new_name[sym]);
450450
}
451-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
451+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
452452
for (size_t i=0; i<xx.n_dependencies; i++) {
453453
if (current_scope.find(xx.m_dependencies[i]) != current_scope.end()) {
454454
sym = current_scope[xx.m_dependencies[i]];
@@ -500,7 +500,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
500500
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
501501
xx.m_name = s2c(al, sym_to_new_name[sym]);
502502
}
503-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
503+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
504504
current_scope = x.m_symtab->get_scope();
505505
for (auto &a : x.m_symtab->get_scope()) {
506506
visit_symbol(*a.second);
@@ -528,7 +528,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
528528
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
529529
xx.m_name = s2c(al, sym_to_new_name[sym]);
530530
}
531-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
531+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
532532
current_scope = x.m_symtab->get_scope();
533533
for (auto &a : x.m_symtab->get_scope()) {
534534
visit_symbol(*a.second);
@@ -548,7 +548,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
548548
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
549549
xx.m_name = s2c(al, sym_to_new_name[sym]);
550550
}
551-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
551+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
552552
current_scope = x.m_symtab->get_scope();
553553
for (auto &a : x.m_symtab->get_scope()) {
554554
visit_symbol(*a.second);
@@ -568,7 +568,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
568568
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
569569
xx.m_name = s2c(al, sym_to_new_name[sym]);
570570
}
571-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
571+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
572572
current_scope = x.m_symtab->get_scope();
573573
for (auto &a : x.m_symtab->get_scope()) {
574574
visit_symbol(*a.second);
@@ -588,7 +588,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
588588
if (sym_to_new_name.find(sym) != sym_to_new_name.end()) {
589589
xx.m_name = s2c(al, sym_to_new_name[sym]);
590590
}
591-
std::unordered_map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
591+
std::map<std::string, ASR::symbol_t*> current_scope_copy = current_scope;
592592
current_scope = x.m_symtab->get_scope();
593593
for (auto &a : x.m_symtab->get_scope()) {
594594
visit_symbol(*a.second);
@@ -606,8 +606,16 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor<UniqueSymbolVisitor> {
606606

607607

608608
void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit,
609-
const LCompilers::PassOptions& /*pass_options*/) {
610-
SymbolRenameVisitor v(true, true, true, true);
609+
const LCompilers::PassOptions& pass_options) {
610+
bool any_present = (pass_options.module_name_mangling || pass_options.global_symbols_mangling ||
611+
pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling);
612+
if (!any_present || lcompilers_unique_ID.empty()) {
613+
return;
614+
}
615+
SymbolRenameVisitor v(pass_options.module_name_mangling,
616+
pass_options.global_symbols_mangling,
617+
pass_options.intrinsic_symbols_mangling,
618+
pass_options.all_symbols_mangling);
611619
v.visit_TranslationUnit(unit);
612620
UniqueSymbolVisitor u(al, v.sym_to_renamed);
613621
u.visit_TranslationUnit(unit);

src/libasr/utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ struct CompilerOptions {
6464
bool enable_cpython = false;
6565
bool enable_symengine = false;
6666
bool link_numpy = false;
67+
bool module_name_mangling = false;
68+
bool global_symbols_mangling = false;
69+
bool intrinsic_symbols_mangling = false;
70+
bool all_symbols_mangling = false;
6771
std::vector<std::string> import_paths;
6872
Platform platform;
6973

@@ -93,6 +97,10 @@ namespace LCompilers {
9397
bool verbose = false; // For developer debugging
9498
bool pass_cumulative = false; // Apply passes cumulatively
9599
bool disable_main = false;
100+
bool module_name_mangling = false;
101+
bool global_symbols_mangling = false;
102+
bool intrinsic_symbols_mangling = false;
103+
bool all_symbols_mangling = false;
96104
};
97105

98106
}

0 commit comments

Comments
 (0)