From c503759319c4f41fe4fd826d9ce4f1fa08789072 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Wed, 22 Mar 2023 12:59:07 +0530 Subject: [PATCH 1/3] Add backend=c to directly run with C. --- src/bin/lpython.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 3d09f40c14..913eadcccb 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -304,6 +304,53 @@ int emit_c(const std::string &infile, return 0; } +int emit_c_to_file(const std::string &infile, const std::string &outfile, + const std::string &runtime_library_dir, + CompilerOptions &compiler_options) +{ + Allocator al(4*1024); + LCompilers::diag::Diagnostics diagnostics; + LCompilers::LocationManager lm; + { + LCompilers::LocationManager::FileLocations fl; + fl.in_filename = infile; + lm.files.push_back(fl); + std::string input = LCompilers::read_file(infile); + lm.init_simple(input); + lm.file_ends.push_back(input.size()); + } + LCompilers::Result r = parse_python_file( + al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser); + std::cerr << diagnostics.render(lm, compiler_options); + if (!r.ok) { + return 1; + } + LCompilers::LPython::AST::ast_t* ast = r.result; + + diagnostics.diagnostics.clear(); + LCompilers::Result + r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile); + std::cerr << diagnostics.render(lm, compiler_options); + if (!r1.ok) { + LCOMPILERS_ASSERT(diagnostics.has_error()) + return 2; + } + LCompilers::ASR::TranslationUnit_t* asr = r1.result; + + diagnostics.diagnostics.clear(); + auto res = LCompilers::asr_to_c(al, *asr, diagnostics, compiler_options, 0); + std::cerr << diagnostics.render(lm, compiler_options); + if (!res.ok) { + LCOMPILERS_ASSERT(diagnostics.has_error()) + return 3; + } + FILE *fp; + fp = fopen(outfile.c_str(), "w"); + fputs(res.result.c_str(), fp); + fclose(fp); + return 0; +} + int emit_wat(const std::string &infile, const std::string &runtime_library_dir, CompilerOptions &compiler_options) @@ -996,7 +1043,7 @@ int link_executable(const std::vector &infiles, const std::string &outfile, const std::string &runtime_library_dir, Backend backend, bool static_executable, bool kokkos, - CompilerOptions &compiler_options) + CompilerOptions &compiler_options, const std::string &rtlib_header_dir) { /* The `gcc` line for dynamic linking that is constructed below: @@ -1116,7 +1163,7 @@ int link_executable(const std::vector &infiles, for (auto &s : infiles) { cmd += s + " "; } - cmd += + " -L"; + cmd += " -L"; cmd += " " + post_options + " -lm"; int err = system(cmd.c_str()); if (err) { @@ -1124,6 +1171,19 @@ int link_executable(const std::vector &infiles, return 10; } return 0; + } else if (backend == Backend::c) { + std::string CXX = "gcc"; + std::string cmd = CXX + " -o " + outfile + " "; + for (auto &s : infiles) { + cmd += s + " "; + } + cmd += " -I " + rtlib_header_dir; + int err = system(cmd.c_str()); + if (err) { + std::cout << "The command '" + cmd + "' failed." << std::endl; + return 10; + } + return 0; } else if (backend == Backend::x86) { std::string cmd = "cp " + infiles[0] + " " + outfile; int err = system(cmd.c_str()); @@ -1679,6 +1739,11 @@ int main(int argc, char *argv[]) } else if (backend == Backend::wasm_x86 || backend == Backend::wasm_x64) { err = compile_to_binary_wasm_to_x86(arg_file, outfile, runtime_library_dir, compiler_options, time_report, backend); + } else if (backend == Backend::c) { + std::string emit_file_name = basename + "__tmp__generated__.c"; + err = emit_c_to_file(arg_file, emit_file_name, runtime_library_dir, compiler_options); + err = link_executable({emit_file_name}, outfile, runtime_library_dir, + backend, static_link, true, compiler_options, rtlib_header_dir); } else if (backend == Backend::llvm) { #ifdef HAVE_LFORTRAN_LLVM std::string tmp_o = outfile + ".tmp.o"; @@ -1686,7 +1751,7 @@ int main(int argc, char *argv[]) lpython_pass_manager, compiler_options, time_report); if (err != 0) return err; err = link_executable({tmp_o}, outfile, runtime_library_dir, - backend, static_link, true, compiler_options); + backend, static_link, true, compiler_options, rtlib_header_dir); if (err != 0) return err; #ifdef HAVE_RUNTIME_STACKTRACE @@ -1742,7 +1807,7 @@ int main(int argc, char *argv[]) return 0; } else { return link_executable(arg_files, outfile, runtime_library_dir, - backend, static_link, true, compiler_options); + backend, static_link, true, compiler_options, rtlib_header_dir); } } catch(const LCompilers::LCompilersException &e) { std::cerr << "Internal Compiler Error: Unhandled exception" << std::endl; From 1da9ea78bd60437a62bec4bb09a401deb192a1e9 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Wed, 22 Mar 2023 12:59:24 +0530 Subject: [PATCH 2/3] Add generated files to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 468bb9c6ed..991469178c 100644 --- a/.gitignore +++ b/.gitignore @@ -87,6 +87,7 @@ inst/bin/* *_lines.txt *_ldd.txt *_lines.dat.txt +*__tmp__generated__.c ### https://raw.github.com/github/gitignore/218a941be92679ce67d0484547e3e142b2f5f6f0/Global/macOS.gitignore From eb0bd418592577a292c8a1cd9d83e781b74f9085 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 23 Mar 2023 10:45:19 +0530 Subject: [PATCH 3/3] Add a test in CI --- ci/test.xsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ci/test.xsh b/ci/test.xsh index 50622fd55f..f2db432453 100644 --- a/ci/test.xsh +++ b/ci/test.xsh @@ -19,6 +19,8 @@ if $WIN == "1": python run_tests.py --skip-run-with-dbg --no-color else: python run_tests.py + src/bin/lpython examples/expr2.py + src/bin/lpython --backend=c examples/expr2.py cd integration_tests python run_tests.py -j16 -b llvm cpython c wasm