Skip to content

Commit 97c0a7e

Browse files
committed
C: Support passes
1 parent 6ebd4b8 commit 97c0a7e

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/bin/lpython.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ int emit_cpp(const std::string &infile,
262262

263263
int emit_c(const std::string &infile,
264264
const std::string &runtime_library_dir,
265+
LCompilers::PassManager& pass_manager,
265266
CompilerOptions &compiler_options)
266267
{
267268
Allocator al(4*1024);
@@ -293,6 +294,13 @@ int emit_c(const std::string &infile,
293294
}
294295
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
295296

297+
// Apply ASR passes
298+
LCompilers::PassOptions pass_options;
299+
pass_manager.use_default_passes();
300+
pass_options.run_fun = "f";
301+
pass_options.always_run = true;
302+
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
303+
296304
diagnostics.diagnostics.clear();
297305
auto res = LCompilers::asr_to_c(al, *asr, diagnostics, compiler_options, 0);
298306
std::cerr << diagnostics.render(lm, compiler_options);
@@ -305,7 +313,7 @@ int emit_c(const std::string &infile,
305313
}
306314

307315
int emit_c_to_file(const std::string &infile, const std::string &outfile,
308-
const std::string &runtime_library_dir,
316+
const std::string &runtime_library_dir, LCompilers::PassManager& pass_manager,
309317
CompilerOptions &compiler_options)
310318
{
311319
Allocator al(4*1024);
@@ -337,6 +345,13 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
337345
}
338346
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
339347

348+
// Apply ASR passes
349+
LCompilers::PassOptions pass_options;
350+
pass_manager.use_default_passes();
351+
pass_options.run_fun = "f";
352+
pass_options.always_run = true;
353+
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
354+
340355
diagnostics.diagnostics.clear();
341356
auto res = LCompilers::asr_to_c(al, *asr, diagnostics, compiler_options, 0);
342357
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1666,7 +1681,8 @@ int main(int argc, char *argv[])
16661681
return emit_cpp(arg_file, runtime_library_dir, compiler_options);
16671682
}
16681683
if (show_c) {
1669-
return emit_c(arg_file, runtime_library_dir, compiler_options);
1684+
return emit_c(arg_file, runtime_library_dir, lpython_pass_manager,
1685+
compiler_options);
16701686
}
16711687
if (show_wat) {
16721688
return emit_wat(arg_file, runtime_library_dir, compiler_options);
@@ -1741,7 +1757,8 @@ int main(int argc, char *argv[])
17411757
runtime_library_dir, compiler_options, time_report, backend);
17421758
} else if (backend == Backend::c) {
17431759
std::string emit_file_name = basename + "__tmp__generated__.c";
1744-
err = emit_c_to_file(arg_file, emit_file_name, runtime_library_dir, compiler_options);
1760+
err = emit_c_to_file(arg_file, emit_file_name, runtime_library_dir,
1761+
lpython_pass_manager, compiler_options);
17451762
err = link_executable({emit_file_name}, outfile, runtime_library_dir,
17461763
backend, static_link, true, compiler_options, rtlib_header_dir);
17471764
} else if (backend == Backend::llvm) {

src/libasr/codegen/asr_to_c.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,17 +1289,10 @@ R"(
12891289
}
12901290
};
12911291

1292-
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
1292+
Result<std::string> asr_to_c(Allocator & /*al*/, ASR::TranslationUnit_t &asr,
12931293
diag::Diagnostics &diagnostics, CompilerOptions &co,
12941294
int64_t default_lower_bound)
12951295
{
1296-
1297-
LCompilers::PassOptions pass_options;
1298-
pass_options.always_run = true;
1299-
pass_create_subroutine_from_function(al, asr, pass_options);
1300-
pass_replace_array_op(al, asr, pass_options);
1301-
pass_unused_functions(al, asr, pass_options);
1302-
pass_replace_class_constructor(al, asr, pass_options);
13031296
ASRToCVisitor v(diagnostics, co, default_lower_bound);
13041297
try {
13051298
v.visit_asr((ASR::asr_t &)asr);

0 commit comments

Comments
 (0)