forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modfile.cpp
82 lines (66 loc) · 2.52 KB
/
modfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <string>
#include <libasr/config.h>
#include <libasr/asr_utils.h>
#include <libasr/asr_verify.h>
#include <libasr/modfile.h>
#include <libasr/serialization.h>
#include <libasr/bwriter.h>
namespace LFortran {
const std::string lfortran_modfile_type_string = "LFortran Modfile";
// The save_modfile() and load_modfile() must stay consistent. What is saved
// must be loaded in exactly the same order.
/*
Saves the module into a binary stream.
That stream can be saved to a mod file by the caller.
The sections in the file/stream are saved using write_string(), so they
can be efficiently read by the loader and ignored if needed.
Comments below show some possible future improvements to the mod format.
*/
std::string save_modfile(const ASR::TranslationUnit_t &m) {
LFORTRAN_ASSERT(m.m_global_scope->get_scope().size()== 1);
for (auto &a : m.m_global_scope->get_scope()) {
LFORTRAN_ASSERT(ASR::is_a<ASR::Module_t>(*a.second));
if ((bool&)a) { } // Suppress unused warning in Release mode
}
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryWriter b;
#else
TextWriter b;
#endif
// Header
b.write_string(lfortran_modfile_type_string);
b.write_string(LFORTRAN_VERSION);
// AST section: Original module source code:
// Currently empty.
// Note: in the future we can save here:
// * A path to the original source code
// * Hash of the orig source code
// * AST binary export of it (this AST only changes if the hash changes)
// ASR section:
// Export ASR:
// Currently empty.
// Full ASR:
b.write_string(serialize(m));
return b.get_str();
}
ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
bool load_symtab_id, SymbolTable &symtab) {
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryReader b(s);
#else
TextReader b(s);
#endif
std::string file_type = b.read_string();
if (file_type != lfortran_modfile_type_string) {
throw LCompilersException("LFortran Modfile format not recognized");
}
std::string version = b.read_string();
if (version != LFORTRAN_VERSION) {
throw LCompilersException("Incompatible format: LFortran Modfile was generated using version '" + version + "', but current LFortran version is '" + LFORTRAN_VERSION + "'");
}
std::string asr_binary = b.read_string();
ASR::asr_t *asr = deserialize_asr(al, asr_binary, load_symtab_id, symtab);
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
return tu;
}
} // namespace LFortran