Skip to content

Commit 53d93de

Browse files
committed
wip
1 parent 0afa199 commit 53d93de

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

src/bin/lpython.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ int compile_python_to_object_file(
512512
const std::string &runtime_library_dir,
513513
LCompilers::PassManager& pass_manager,
514514
CompilerOptions &compiler_options,
515-
bool time_report)
515+
bool time_report, bool arg_c=false)
516516
{
517517
Allocator al(4*1024);
518518
LFortran::diag::Diagnostics diagnostics;
@@ -540,7 +540,7 @@ int compile_python_to_object_file(
540540
diagnostics.diagnostics.clear();
541541
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
542542
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
543-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
543+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, !arg_c,
544544
compiler_options.disable_main, compiler_options.symtab_only, infile);
545545
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
546546
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
@@ -552,7 +552,7 @@ int compile_python_to_object_file(
552552
}
553553
LFortran::ASR::TranslationUnit_t* asr = r1.result;
554554
{
555-
int err = save_pyc_files(*asr, infile);
555+
int err = LFortran::LPython::save_pyc_files(*asr, infile);
556556
if( err ) {
557557
return err;
558558
}
@@ -1071,7 +1071,8 @@ int main(int argc, char *argv[])
10711071
if (arg_c) {
10721072
if (backend == Backend::llvm) {
10731073
#ifdef HAVE_LFORTRAN_LLVM
1074-
return compile_python_to_object_file(arg_file, outfile, runtime_library_dir, lpython_pass_manager, compiler_options, time_report);
1074+
return compile_python_to_object_file(arg_file, outfile, runtime_library_dir, lpython_pass_manager, compiler_options, time_report,
1075+
arg_c);
10751076
#else
10761077
std::cerr << "The -c option requires the LLVM backend to be enabled. Recompile with `WITH_LLVM=yes`." << std::endl;
10771078
return 1;

src/lpython/semantics/python_ast_to_asr.cpp

+51-31
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
172172
// * First the current directory (this is incorrect, we need to do it relative to the current file)
173173
// * Then the LPython runtime directory
174174
LFortran::Result<std::string> get_full_path(const std::string &filename,
175-
const std::string &runtime_library_dir, bool &ltypes) {
175+
const std::string &runtime_library_dir, std::string& input,
176+
bool &ltypes) {
176177
ltypes = false;
177-
std::string input;
178178
bool status = read_file(filename, input);
179179
if (status) {
180180
return filename;
@@ -194,8 +194,8 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
194194
} else {
195195
return LFortran::Error();
196196
}
197-
} else if (filename == "numpy.py") {
198-
filename_intrinsic = runtime_library_dir + "/lpython_intrinsic_numpy.py";
197+
} else if (startswith(filename, "numpy.py")) {
198+
filename_intrinsic = runtime_library_dir + "/lpython_intrinsic_" + filename;
199199
status = read_file(filename_intrinsic, input);
200200
if (status) {
201201
return filename_intrinsic;
@@ -210,9 +210,10 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
210210
}
211211

212212
bool set_module_path(std::string infile0, std::vector<std::string> &rl_path,
213-
std::string& infile, std::string& path_used, bool& ltypes) {
213+
std::string& infile, std::string& path_used, std::string& input,
214+
bool& ltypes) {
214215
for (auto path: rl_path) {
215-
Result<std::string> rinfile = get_full_path(infile0, path, ltypes);
216+
Result<std::string> rinfile = get_full_path(infile0, path, input, ltypes);
216217
if (rinfile.ok) {
217218
infile = rinfile.result;
218219
path_used = path;
@@ -222,6 +223,37 @@ bool set_module_path(std::string infile0, std::vector<std::string> &rl_path,
222223
return false;
223224
}
224225

226+
ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al,
227+
std::vector<std::string> &rl_path, std::string infile,
228+
const Location &loc,
229+
const std::function<void (const std::string &, const Location &)> err) {
230+
// TODO: diagnostic should be an argument to this function
231+
diag::Diagnostics diagnostics;
232+
Result<AST::ast_t*> r = parse_python_file(al, rl_path[0], infile,
233+
diagnostics, false);
234+
if (!r.ok) {
235+
err("The file '" + infile + "' failed to parse", loc);
236+
}
237+
LFortran::LPython::AST::ast_t* ast = r.result;
238+
239+
// Convert the module from AST to ASR
240+
LFortran::LocationManager lm;
241+
lm.in_filename = infile;
242+
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
243+
diagnostics, false, true, false, infile);
244+
save_pyc_files(*r2.result, infile + "c");
245+
std::string input;
246+
read_file(infile, input);
247+
CompilerOptions compiler_options;
248+
std::cerr << diagnostics.render(input, lm, compiler_options);
249+
if (!r2.ok) {
250+
LFORTRAN_ASSERT(diagnostics.has_error())
251+
return nullptr; // Error
252+
}
253+
254+
return r2.result;
255+
}
256+
225257
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
226258
const std::string &module_name,
227259
const Location &loc, bool intrinsic,
@@ -247,43 +279,31 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
247279
std::string infile0 = module_name + ".py";
248280
std::string infile0c = infile0 + "c";
249281
std::string path_used = "", infile;
282+
bool compile_module = true;
283+
ASR::TranslationUnit_t* mod1 = nullptr;
284+
std::string input;
250285
bool found = set_module_path(infile0c, rl_path, infile,
251-
path_used, ltypes);
286+
path_used, input, ltypes);
287+
std::cout<<"infile0c: "<<infile0c<<" "<<found<<std::endl;
252288
if( !found ) {
289+
input.clear();
253290
found = set_module_path(infile0, rl_path, infile,
254-
path_used, ltypes);
291+
path_used, input, ltypes);
255292
} else {
256-
load_pycfile();
293+
std::cout<<"loading_pyc_file: "<<std::endl;
294+
mod1 = load_pycfile(al, input, false);
295+
std::cout<<"loaded_pyc_file: "<<std::endl;
296+
compile_module = false;
257297
}
258298

259299
if (!found) {
260300
err("Could not find the module '" + infile0 + "'", loc);
261301
}
262302
if (ltypes) return nullptr;
263303

264-
// TODO: diagnostic should be an argument to this function
265-
diag::Diagnostics diagnostics;
266-
Result<AST::ast_t*> r = parse_python_file(al, rl_path[0], infile,
267-
diagnostics, false);
268-
if (!r.ok) {
269-
err("The file '" + infile + "' failed to parse", loc);
270-
}
271-
LFortran::LPython::AST::ast_t* ast = r.result;
272-
273-
// Convert the module from AST to ASR
274-
LFortran::LocationManager lm;
275-
lm.in_filename = infile;
276-
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
277-
diagnostics, false, true, false, infile);
278-
std::string input;
279-
read_file(infile, input);
280-
CompilerOptions compiler_options;
281-
std::cerr << diagnostics.render(input, lm, compiler_options);
282-
if (!r2.ok) {
283-
LFORTRAN_ASSERT(diagnostics.has_error())
284-
return nullptr; // Error
304+
if( compile_module ) {
305+
mod1 = compile_module_till_asr(al, rl_path, infile, loc, err);
285306
}
286-
ASR::TranslationUnit_t* mod1 = r2.result;
287307

288308
// insert into `symtab`
289309
std::vector<std::pair<std::string, ASR::Module_t*>> children_modules;

0 commit comments

Comments
 (0)