@@ -12,25 +12,8 @@ namespace LFortran {
1212
1313const std::string lfortran_modfile_type_string = " LFortran Modfile" ;
1414
15- // The save_modfile() and load_modfile() must stay consistent. What is saved
16- // must be loaded in exactly the same order.
17-
18- /*
19- Saves the module into a binary stream.
20-
21- That stream can be saved to a mod file by the caller.
22- The sections in the file/stream are saved using write_string(), so they
23- can be efficiently read by the loader and ignored if needed.
24-
25- Comments below show some possible future improvements to the mod format.
26- */
27- std::string save_modfile (const ASR::TranslationUnit_t &m) {
28- LFORTRAN_ASSERT (m.m_global_scope ->get_scope ().size ()== 1 );
29- for (auto &a : m.m_global_scope ->get_scope ()) {
30- LFORTRAN_ASSERT (ASR::is_a<ASR::Module_t>(*a.second ));
31- if ((bool &)a) { } // Suppress unused warning in Release mode
32- }
33- #ifdef WITH_LFORTRAN_BINARY_MODFILES
15+ inline void save_asr (const ASR::TranslationUnit_t &m, std::string& asr_string) {
16+ #ifdef WITH_LFORTRAN_BINARY_MODFILES
3417 BinaryWriter b;
3518#else
3619 TextWriter b;
@@ -54,11 +37,40 @@ std::string save_modfile(const ASR::TranslationUnit_t &m) {
5437 // Full ASR:
5538 b.write_string (serialize (m));
5639
57- return b.get_str ();
40+ asr_string = b.get_str ();
5841}
5942
60- ASR::TranslationUnit_t* load_modfile (Allocator &al, const std::string &s,
61- bool load_symtab_id, SymbolTable &symtab) {
43+ // The save_modfile() and load_modfile() must stay consistent. What is saved
44+ // must be loaded in exactly the same order.
45+
46+ /*
47+ Saves the module into a binary stream.
48+
49+ That stream can be saved to a mod file by the caller.
50+ The sections in the file/stream are saved using write_string(), so they
51+ can be efficiently read by the loader and ignored if needed.
52+
53+ Comments below show some possible future improvements to the mod format.
54+ */
55+ std::string save_modfile (const ASR::TranslationUnit_t &m) {
56+ LFORTRAN_ASSERT (m.m_global_scope ->get_scope ().size ()== 1 );
57+ for (auto &a : m.m_global_scope ->get_scope ()) {
58+ LFORTRAN_ASSERT (ASR::is_a<ASR::Module_t>(*a.second ));
59+ if ((bool &)a) { } // Suppress unused warning in Release mode
60+ }
61+
62+ std::string asr_string;
63+ save_asr (m, asr_string);
64+ return asr_string;
65+ }
66+
67+ std::string save_pycfile (const ASR::TranslationUnit_t &m) {
68+ std::string asr_string;
69+ save_asr (m, asr_string);
70+ return asr_string;
71+ }
72+
73+ inline void load_serialised_asr (const std::string &s, std::string& asr_binary) {
6274#ifdef WITH_LFORTRAN_BINARY_MODFILES
6375 BinaryReader b (s);
6476#else
@@ -72,11 +84,27 @@ ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
7284 if (version != LFORTRAN_VERSION) {
7385 throw LCompilersException (" Incompatible format: LFortran Modfile was generated using version '" + version + " ', but current LFortran version is '" + LFORTRAN_VERSION + " '" );
7486 }
75- std::string asr_binary = b.read_string ();
87+ asr_binary = b.read_string ();
88+ }
89+
90+ ASR::TranslationUnit_t* load_modfile (Allocator &al, const std::string &s,
91+ bool load_symtab_id, SymbolTable &symtab) {
92+ std::string asr_binary;
93+ load_serialised_asr (s, asr_binary);
7694 ASR::asr_t *asr = deserialize_asr (al, asr_binary, load_symtab_id, symtab);
7795
7896 ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
7997 return tu;
8098}
8199
100+ ASR::TranslationUnit_t* load_pycfile (Allocator &al, const std::string &s,
101+ bool load_symtab_id) {
102+ std::string asr_binary;
103+ load_serialised_asr (s, asr_binary);
104+ ASR::asr_t *asr = deserialize_asr (al, asr_binary, load_symtab_id);
105+
106+ ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
107+ return tu;
108+ }
109+
82110} // namespace LFortran
0 commit comments