Skip to content

Add --backend=c to directly run with C #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions ci/test.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 69 additions & 4 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LCompilers::LPython::AST::ast_t*> 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<LCompilers::ASR::TranslationUnit_t*>
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)
Expand Down Expand Up @@ -996,7 +1043,7 @@ int link_executable(const std::vector<std::string> &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:
Expand Down Expand Up @@ -1116,14 +1163,27 @@ int link_executable(const std::vector<std::string> &infiles,
for (auto &s : infiles) {
cmd += s + " ";
}
cmd += + " -L";
cmd += " -L";
cmd += " " + post_options + " -lm";
int err = system(cmd.c_str());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
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());
Expand Down Expand Up @@ -1679,14 +1739,19 @@ 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";
err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
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
Expand Down Expand Up @@ -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;
Expand Down