hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
codegenObjectCache.hpp
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2026/2/7.
3//
4
5#ifndef HOSHI_LANG_CODEGENOBJECTCACHE_HPP
6#define HOSHI_LANG_CODEGENOBJECTCACHE_HPP
7
9#include "compiler/ir/IR.h"
10#include <memory>
11#include <mutex>
12#include <share/def.hpp>
13
14namespace yoi {
15 namespace serialization {
16 template <typename T> void write(FILE *fp, const T &value) {
17 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable, otherwise explicit serialization method must be provided.");
18 // general serialization helper for POD types
19 fwrite(&value, sizeof(T), 1, fp);
20 }
21
22 template <typename T> void read(FILE *fp, T &value) {
23 static_assert(std::is_trivially_copyable_v<T>,
24 "T must be trivially copyable, otherwise explicit deserialization method must be provided.");
25 // general deserialization helper for POD types
26 fread(&value, sizeof(T), 1, fp);
27 }
28
29 template <> void write(FILE *fp, const yoi::wstr &value);
30
31 template <> void read(FILE *fp, yoi::wstr &value);
32 } // namespace serialization
33
35 // the absolute path of the source file on the disk
37 // the output object file name
39 // the hash of the source file, if this file got removed in the next compilation, we can save this hash to free-list for reuse.
41 // to further accelerate the compilation, we save the last modification time of the source file, if the last modification time is the same as
42 // the last compilation, we can skip the codegen as they exposed the same implementations.
44 std::set<yoi::indexT> depend_by; // the modules which depend on this module
45
47
52 CodegenObjectCacheEntry setDependBy(const std::set<yoi::indexT> &depend_by);
53
54 const yoi::wstr &getAbsPathOnDisk() const;
55 const yoi::wstr &getObjectFilename() const;
56 yoi::indexT getHash() const;
58 const std::set<yoi::indexT> &getDependBy() const;
59 };
60
62 public:
63 std::shared_ptr<compilerContext> compilerCtx;
64 std::map<yoi::wstr, CodegenObjectCacheEntry> cache;
65 std::set<yoi::indexT> free_list;
67 mutable std::mutex cacheMutex;
68
74 CodegenObjectCache &setCompilerCtx(const std::shared_ptr<compilerContext> &compilerCtx);
75
80 void purge_and_update(const yoi::vec<yoi::wstr> &source_files);
81
88 yoi::indexT register_entry(const yoi::wstr &abs_path_on_disk);
89
95 void update_last_modification(const yoi::wstr &abs_path_on_disk, yoi::indexT last_modification);
96
102 yoi::indexT get_entry_index(const yoi::wstr &abs_path_on_disk);
103
109 CodegenObjectCacheEntry get_entry(const yoi::wstr &abs_path_on_disk);
110
115 void remove_entry(const yoi::wstr &abs_path_on_disk);
116
117 private:
118 yoi::indexT register_entry_unlocked(const yoi::wstr &abs_path_on_disk);
119 };
120
121 namespace serialization {
122 template <> void write(FILE *fp, const CodegenObjectCache &value);
123
124 template <> void read(FILE *fp, CodegenObjectCache &value);
125
126 template <> void write(FILE *fp, const CodegenObjectCacheEntry &value);
127
128 template <> void read(FILE *fp, CodegenObjectCacheEntry &value);
129
130 template <> void write(FILE *fp, const std::set<yoi::indexT> &value);
131
132 template <> void read(FILE *fp, std::set<yoi::indexT> &value);
133 } // namespace serialization
134
135} // namespace yoi
136#endif
void purge_and_update(const yoi::vec< yoi::wstr > &source_files)
purge the cache, add the entries that previously not in the cache, remove the entries that are not in...
CodegenObjectCacheEntry get_entry(const yoi::wstr &abs_path_on_disk)
get the entry from the cache
yoi::indexT register_entry(const yoi::wstr &abs_path_on_disk)
register a new entry to the cache
yoi::indexT register_entry_unlocked(const yoi::wstr &abs_path_on_disk)
CodegenObjectCache & setCompilerCtx(const std::shared_ptr< compilerContext > &compilerCtx)
set the build config
void remove_entry(const yoi::wstr &abs_path_on_disk)
remove the entry from the cache
std::set< yoi::indexT > free_list
std::shared_ptr< compilerContext > compilerCtx
yoi::indexT get_entry_index(const yoi::wstr &abs_path_on_disk)
get the entry from the cache
void update_last_modification(const yoi::wstr &abs_path_on_disk, yoi::indexT last_modification)
update the last modification time of an entry
std::map< yoi::wstr, CodegenObjectCacheEntry > cache
void write(FILE *fp, const yoi::wstr &value)
void read(FILE *fp, yoi::wstr &value)
std::vector< t > vec
Definition def.hpp:53
std::wstring wstr
Definition def.hpp:48
uint64_t indexT
Definition def.hpp:51
CodegenObjectCacheEntry setAbsPathOnDisk(const yoi::wstr &abs_path_on_disk)
const yoi::wstr & getObjectFilename() const
CodegenObjectCacheEntry setHash(yoi::indexT hash)
const std::set< yoi::indexT > & getDependBy() const
CodegenObjectCacheEntry setLastModification(yoi::indexT last_modification)
const yoi::wstr & getAbsPathOnDisk() const
CodegenObjectCacheEntry setObjectFilename(const yoi::wstr &object_filename)
CodegenObjectCacheEntry setDependBy(const std::set< yoi::indexT > &depend_by)
yoi::indexT getLastModification() const
std::set< yoi::indexT > depend_by