Skip to content

Commit 0afa199

Browse files
committed
wip
1 parent 9e3f76e commit 0afa199

File tree

7 files changed

+78
-32
lines changed

7 files changed

+78
-32
lines changed

src/bin/lpython.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,6 @@ int emit_c(const std::string &infile,
262262
return 0;
263263
}
264264

265-
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
266-
std::string infile)
267-
{
268-
LFORTRAN_ASSERT(LFortran::asr_verify(u));
269-
std::string modfile_binary = LFortran::save_pycfile(u);
270-
271-
while( infile.back() != '.' ) {
272-
infile.pop_back();
273-
}
274-
std::string modfile = infile + "pyc";
275-
{
276-
std::ofstream out;
277-
out.open(modfile, std::ofstream::out | std::ofstream::binary);
278-
out << modfile_binary;
279-
}
280-
return 0;
281-
}
282-
283265
#ifdef HAVE_LFORTRAN_RAPIDJSON
284266

285267
int get_symbols (const std::string &infile,

src/libasr/modfile.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,27 @@ ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
107107
return tu;
108108
}
109109

110+
ASR::TranslationUnit_t* load_pycfile(Allocator &al, const std::string &s,
111+
bool load_symtab_id) {
112+
#ifdef WITH_LFORTRAN_BINARY_MODFILES
113+
BinaryReader b(s);
114+
#else
115+
TextReader b(s);
116+
#endif
117+
118+
std::string file_type = b.read_string();
119+
if (file_type != lfortran_modfile_type_string) {
120+
throw LCompilersException("LFortran Modfile format not recognized");
121+
}
122+
std::string version = b.read_string();
123+
if (version != LFORTRAN_VERSION) {
124+
throw LCompilersException("Incompatible format: LFortran Modfile was generated using version '" + version + "', but current LFortran version is '" + LFORTRAN_VERSION + "'");
125+
}
126+
std::string asr_binary = b.read_string();
127+
ASR::asr_t *asr = deserialize_asr(al, asr_binary, load_symtab_id);
128+
129+
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
130+
return tu;
131+
}
132+
110133
} // namespace LFortran

src/libasr/modfile.h

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace LFortran {
1414
ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
1515
bool load_symtab_id, SymbolTable &symtab);
1616

17+
ASR::TranslationUnit_t* load_pycfile(Allocator &al, const std::string &s,
18+
bool load_symtab_id);
19+
1720
}
1821

1922
#endif // LFORTRAN_MODFILE_H

src/libasr/serialization.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,12 @@ void fix_external_symbols(ASR::TranslationUnit_t &unit,
307307
}
308308

309309
ASR::asr_t* deserialize_asr(Allocator &al, const std::string &s,
310-
bool load_symtab_id, SymbolTable &external_symtab) {
310+
bool load_symtab_id, SymbolTable & /*external_symtab*/) {
311+
return deserialize_asr(al, s, load_symtab_id);
312+
}
313+
314+
ASR::asr_t* deserialize_asr(Allocator &al, const std::string &s,
315+
bool load_symtab_id) {
311316
ASRDeserializationVisitor v(al, s, load_symtab_id);
312317
ASR::asr_t *node = v.deserialize_node();
313318
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(node);
@@ -319,9 +324,6 @@ ASR::asr_t* deserialize_asr(Allocator &al, const std::string &s,
319324

320325
LFORTRAN_ASSERT(asr_verify(*tu, false));
321326

322-
// Suppress a warning for now
323-
if ((bool&)external_symtab) {}
324-
325327
return node;
326328
}
327329

src/libasr/serialization.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace LFortran {
99
std::string serialize(const ASR::TranslationUnit_t &unit);
1010
ASR::asr_t* deserialize_asr(Allocator &al, const std::string &s,
1111
bool load_symtab_id, SymbolTable &symtab);
12+
ASR::asr_t* deserialize_asr(Allocator &al, const std::string &s,
13+
bool load_symtab_id);
1214

1315
void fix_external_symbols(ASR::TranslationUnit_t &unit,
1416
SymbolTable &external_symtab);

src/lpython/semantics/python_ast_to_asr.cpp

+40-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <libasr/utils.h>
1919
#include <libasr/pass/global_stmts_program.h>
2020
#include <libasr/pass/instantiate_template.h>
21+
#include <libasr/modfile.h>
2122

2223
#include <lpython/python_ast.h>
2324
#include <lpython/semantics/python_ast_to_asr.h>
@@ -150,6 +151,23 @@ namespace CastingUtil {
150151
}
151152
}
152153

154+
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
155+
std::string infile) {
156+
LFORTRAN_ASSERT(LFortran::asr_verify(u));
157+
std::string modfile_binary = LFortran::save_pycfile(u);
158+
159+
while( infile.back() != '.' ) {
160+
infile.pop_back();
161+
}
162+
std::string modfile = infile + "pyc";
163+
{
164+
std::ofstream out;
165+
out.open(modfile, std::ofstream::out | std::ofstream::binary);
166+
out << modfile_binary;
167+
}
168+
return 0;
169+
}
170+
153171
// Does a CPython style lookup for a module:
154172
// * First the current directory (this is incorrect, we need to do it relative to the current file)
155173
// * Then the LPython runtime directory
@@ -191,6 +209,19 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
191209
}
192210
}
193211

212+
bool set_module_path(std::string infile0, std::vector<std::string> &rl_path,
213+
std::string& infile, std::string& path_used, bool& ltypes) {
214+
for (auto path: rl_path) {
215+
Result<std::string> rinfile = get_full_path(infile0, path, ltypes);
216+
if (rinfile.ok) {
217+
infile = rinfile.result;
218+
path_used = path;
219+
return true;
220+
}
221+
}
222+
return false;
223+
}
224+
194225
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
195226
const std::string &module_name,
196227
const Location &loc, bool intrinsic,
@@ -214,17 +245,17 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
214245

215246
// Parse the module `module_name`.py to AST
216247
std::string infile0 = module_name + ".py";
217-
bool found = false;
248+
std::string infile0c = infile0 + "c";
218249
std::string path_used = "", infile;
219-
for (auto path: rl_path) {
220-
Result<std::string> rinfile = get_full_path(infile0, path, ltypes);
221-
if (rinfile.ok) {
222-
found = true;
223-
infile = rinfile.result;
224-
path_used = path;
225-
break;
226-
}
250+
bool found = set_module_path(infile0c, rl_path, infile,
251+
path_used, ltypes);
252+
if( !found ) {
253+
found = set_module_path(infile0, rl_path, infile,
254+
path_used, ltypes);
255+
} else {
256+
load_pycfile();
227257
}
258+
228259
if (!found) {
229260
err("Could not find the module '" + infile0 + "'", loc);
230261
}

src/lpython/semantics/python_ast_to_asr.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#include <libasr/asr.h>
66

77
namespace LFortran::LPython {
8-
8+
99
std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
1010
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
1111
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
1212
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
1313
bool main_module, bool disable_main, bool symtab_only, std::string file_path);
1414

15+
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
16+
std::string infile);
17+
1518
} // namespace LFortran
1619

1720
#endif // LFORTRAN_PYTHON_AST_TO_ASR_H

0 commit comments

Comments
 (0)