@@ -12,25 +12,8 @@ namespace LFortran {
12
12
13
13
const std::string lfortran_modfile_type_string = " LFortran Modfile" ;
14
14
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
34
17
BinaryWriter b;
35
18
#else
36
19
TextWriter b;
@@ -54,11 +37,40 @@ std::string save_modfile(const ASR::TranslationUnit_t &m) {
54
37
// Full ASR:
55
38
b.write_string (serialize (m));
56
39
57
- return b.get_str ();
40
+ asr_string = b.get_str ();
58
41
}
59
42
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) {
62
74
#ifdef WITH_LFORTRAN_BINARY_MODFILES
63
75
BinaryReader b (s);
64
76
#else
@@ -72,11 +84,27 @@ ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
72
84
if (version != LFORTRAN_VERSION) {
73
85
throw LCompilersException (" Incompatible format: LFortran Modfile was generated using version '" + version + " ', but current LFortran version is '" + LFORTRAN_VERSION + " '" );
74
86
}
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);
76
94
ASR::asr_t *asr = deserialize_asr (al, asr_binary, load_symtab_id, symtab);
77
95
78
96
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
79
97
return tu;
80
98
}
81
99
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
+
82
110
} // namespace LFortran
0 commit comments