hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
compilerContext.cpp
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2024/9/6.
3//
4
5#include "compilerContext.h"
6#include "moduleContext.h"
7#include "builtinModule.hpp"
8#include "frontend/ast.hpp"
9#include "frontend/lexer.hpp"
10#include "frontend/parser.hpp"
11#include "ir/IR.h"
12#include "ir/IROptimizer.hpp"
13#include "share/def.hpp"
14#include "visitor/visitor.h"
15#include <filesystem>
16#include <stdexcept>
17
18namespace yoi {
19 std::shared_ptr<IRModule> compilerContext::getImportedModule(yoi::indexT index) {
20 return moduleImported[index];
21 }
22
23 std::shared_ptr<IRModule> compilerContext::getImportedModule(const yoi::wstr &modRealPath) {
24 try {
25 auto it = moduleImported.find(getModuleIndexByRealPath( modRealPath ));
26 if (it == moduleImported.end()) {
27 return nullptr;
28 }
29 return it->second;
30 } catch (const std::runtime_error &e) {
31 return nullptr;
32 }
33 }
34
35 const std::map<indexT, std::shared_ptr<IRModule>> &compilerContext::getCompiledModules() const {
36 return moduleImported;
37 }
38
39
41 return modules.getIndex(modRealPath);
42 }
43
44 std::shared_ptr<DiagnosticEngine> compilerContext::getDiagnosticEngine() const {
45 return diagnosticEngine;
46 }
47
48 void compilerContext::setDiagnosticEngine(const std::shared_ptr<DiagnosticEngine> &engine) {
49 diagnosticEngine = engine;
50 }
51
53 yoi::wstr rFilepath;
54 if (filepath != L"builtin") {
55 for (auto &prep : buildConfig->searchPaths) {
56 std::filesystem::path final = prep / std::filesystem::path(filepath);
57 // printf("searching for %s\n", wstring2string(prep).c_str());
58 rFilepath = realpath(final.wstring());
59 if (std::filesystem::exists(rFilepath) && std::filesystem::is_regular_file(rFilepath)) {
60 break;
61 } else if (std::filesystem::exists(rFilepath + L".hoshi") && std::filesystem::is_regular_file(rFilepath + L".hoshi")) {
62 rFilepath += L".hoshi";
63 break;
64 } else if (std::filesystem::exists(rFilepath) && std::filesystem::is_directory(rFilepath) && std::filesystem::exists(std::filesystem::path(rFilepath) / "index.hoshi")) {
65 rFilepath = (std::filesystem::path(rFilepath) / "index.hoshi").wstring();
66 break;
67 } else {
68 rFilepath.clear();
69 continue;
70 }
71 }
72 } else {
74 }
75
76 if (rFilepath.empty()) {
77 throw std::runtime_error("file not resolved in all search paths: " + wstring2string(filepath));
78 }
79
80 try {
81 return modules.getIndex(rFilepath);
82 } catch (const std::out_of_range &e) {
83 auto fp = fopen(wstring2string(rFilepath).c_str(), "rb");
84 if (!fp)
85 throw std::runtime_error("invalid filename: " + wstring2string(rFilepath));
86
87 // temporarily add current directory to search path
88 buildConfig->searchPaths.push_back(std::filesystem::path(rFilepath).parent_path().wstring());
89 buildConfig->searchPaths.push_back(std::filesystem::path(rFilepath).parent_path().append(".tsuki_modules").wstring());
90
91 fseek(fp, 0, SEEK_END);
92 auto size = ftell(fp);
93 fseek(fp, 0, SEEK_SET);
94 auto *a = new std::string(size, 0);
95 fread(a->data(), size, 1, fp);
96 auto *b = new yoi::wstr{yoi::string2wstring(*a)};
97 delete a;
98 fclose(fp);
99
100 yoi::lexer l{std::wstringstream(*b)};
101 l.scan();
102 delete b;
103 auto current_file = __current_file_path;
104 set_current_file_path(rFilepath);
105
106 // Wire up diagnostic engine for this compilation unit
107 if (diagnosticEngine) {
108 diagnosticEngine->setCurrentFilePath(rFilepath);
110 }
111
112 hoshiModule *mod;
113 yoi::parse(mod, l);
114 std::shared_ptr<moduleContext> modCtx = std::make_shared<moduleContext>(shared_from_this(), rFilepath, mod);
115 std::shared_ptr<IRModule> irMod = std::make_shared<IRModule>();
116 irMod->modulePath = rFilepath;
117 auto idx = modules.put(rFilepath, modCtx);
118 irMod->identifier = idx;
119 moduleImported[idx] = irMod;
120 std::shared_ptr<visitor> vis = std::make_shared<visitor>(modCtx, irMod, idx);
121 vis->visit();
122
123 // Clear diagnostic engine for this compilation unit
124 set_diagnostic_engine(nullptr);
125
126 astToFinalize.insert(mod);
127 set_current_file_path(current_file);
128
129 // pop current directory from search path
130 buildConfig->searchPaths.pop_back();
131 buildConfig->searchPaths.pop_back();
132
133 return idx;
134 }
135 }
136
137 const std::shared_ptr<IRObjectFile> &compilerContext::getIRObjectFile() const {
138 return irObjectFile;
139 }
140
142 auto builtinModule = std::make_shared<IRModule>();
143
145
146 builtinModuleBuilder = std::make_shared<BuiltinModuleBuilder>(builtinModule);
147 builtinModuleBuilder->build();
148 irFFITable = std::make_shared<IRFFITable>();
149
150 // Wire up diagnostic engine for builtin module compilation
151 if (diagnosticEngine) {
152 diagnosticEngine->setCurrentFilePath(L"builtin");
154 }
155
157 l.scan();
158 hoshiModule *mod;
159 yoi::parse(mod, l);
160 builtinModuleContext = std::make_shared<moduleContext>(shared_from_this(), L"builtin", mod);
161 std::shared_ptr<visitor> vis = std::make_shared<visitor>(builtinModuleContext, builtinModule, HOSHI_COMPILER_CTX_GLOB_ID_CONST);
162 vis->visit();
163 astToFinalize.insert(mod);
164
165 // Clear diagnostic engine
166 set_diagnostic_engine(nullptr);
167 }
168
169 std::shared_ptr<yoi::IRValueType> compilerContext::getIntObjectType(bool forceRawAttr) {
170 return forceRawAttr ? managedPtr(builtinModuleBuilder->getIntObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"int"]);
171 }
172
173 std::shared_ptr<yoi::IRValueType> compilerContext::getBoolObjectType(bool forceRawAttr) {
174 return forceRawAttr ? managedPtr(builtinModuleBuilder->getBoolObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"bool"]);
175 }
176
177 std::shared_ptr<yoi::IRValueType> compilerContext::getDeciObjectType(bool forceRawAttr) {
178 return forceRawAttr ? managedPtr(builtinModuleBuilder->getDeciObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"deci"]);
179 }
180
181 std::shared_ptr<yoi::IRValueType> compilerContext::getStrObjectType(bool forceRawAttr) {
182 return forceRawAttr ? managedPtr(builtinModuleBuilder->getStrObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"string"]);
183 }
184
185 std::shared_ptr<yoi::IRValueType> compilerContext::getCharObjectType(bool forceRawAttr) {
186 return forceRawAttr ? managedPtr(builtinModuleBuilder->getCharObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"char"]);
187 }
188
189 std::shared_ptr<yoi::IRValueType> compilerContext::getNoneObjectType() {
190 return builtinModuleBuilder->sharedValueType[L"none"];
191 }
192
194 const std::shared_ptr<IRObjectFile> &irObjectFile) {
195 this->irObjectFile = irObjectFile;
196 }
197
198 std::shared_ptr<IRBuildConfig> compilerContext::getBuildConfig() const {
199 return buildConfig;
200 }
201
203 const std::shared_ptr<IRBuildConfig> &buildConfig) {
204 this->buildConfig = buildConfig;
205 }
206
207 std::shared_ptr<IRFFITable> compilerContext::getIRFFITable() {
208 return irFFITable;
209 }
210
211 std::shared_ptr<yoi::IRValueType> compilerContext::getForeignInt32ObjectType() {
212 return builtinModuleBuilder->sharedValueType[L"foreignInt32Type"];
213 }
214
215 std::shared_ptr<yoi::IRValueType> compilerContext::getForeignFloatObjectType() {
216 return builtinModuleBuilder->sharedValueType[L"foreignFloatType"];
217 }
218
219 std::shared_ptr<yoi::IRValueType> compilerContext::getNullInterfaceType() {
220 return builtinModuleBuilder->sharedValueType[L"NullInterface"];
221 }
222
223 std::shared_ptr<yoi::moduleContext> compilerContext::getModuleContext(yoi::indexT index) {
225 }
226
228 for (auto &i : astToFinalize) {
229 finalizeAST(i);
230 }
231 }
232
234 IROptimizer opt{shared_from_this(), 0};
235 opt.buildCallGraph();
236 opt.optimize();
237 }
238
239 std::shared_ptr<yoi::IRValueType> compilerContext::getPointerType() {
240 return builtinModuleBuilder->sharedValueType[L"ptr"];
241 }
242 yoi::IRValueType compilerContext::normalizeForeignBasicType(const std::shared_ptr<yoi::IRValueType> &type, bool handlePointer) {
243 if (type->isForeignBasicType()) {
244 switch (type->type) {
246 return *getIntObjectType();
248 return handlePointer ? *getUnsignedObjectType() : *type;
250 return *getDeciObjectType();
251 default:
252 throw std::runtime_error("unknown foreign basic type");
253 }
254 }
255 return *type;
256 }
257
258 std::shared_ptr<yoi::IRValueType> compilerContext::getShortObjectType(bool forceRawAttr) {
259 return forceRawAttr ? managedPtr(builtinModuleBuilder->getShortObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"short"]);
260 }
261
262 std::shared_ptr<yoi::IRValueType> compilerContext::getUnsignedObjectType(bool forceRawAttr) {
263 return forceRawAttr ? managedPtr(builtinModuleBuilder->getUnsignedObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"unsigned"]);
264 }
265
267 return builtinModuleBuilder != nullptr;
268 }
269
270 void compilerContext::registerModule(yoi::indexT idx, std::shared_ptr<IRModule> mod) {
271 moduleImported[idx] = std::move(mod);
272 }
273} // namespace yoi
#define HOSHI_COMPILER_CTX_GLOB_ID_CONST
std::shared_ptr< IRBuildConfig > buildConfig
std::shared_ptr< yoi::IRValueType > getForeignFloatObjectType()
std::map< yoi::indexT, std::shared_ptr< IRModule > > moduleImported
const std::map< yoi::indexT, std::shared_ptr< IRModule > > & getCompiledModules() const
std::shared_ptr< moduleContext > builtinModuleContext
void setIRObjectFile(const std::shared_ptr< IRObjectFile > &irObjectFile)
yoi::indexTable< yoi::wstr, std::shared_ptr< yoi::moduleContext > > modules
std::shared_ptr< yoi::moduleContext > getModuleContext(yoi::indexT index)
get module context by index
std::shared_ptr< yoi::IRValueType > getCharObjectType(bool forceRawAttr=false)
void registerModule(yoi::indexT idx, std::shared_ptr< IRModule > mod)
std::shared_ptr< yoi::IRValueType > getNullInterfaceType()
void setBuildConfig(const std::shared_ptr< IRBuildConfig > &buildConfig)
std::set< hoshiModule * > astToFinalize
std::shared_ptr< yoi::IRValueType > getPointerType()
const std::shared_ptr< IRObjectFile > & getIRObjectFile() const
std::shared_ptr< yoi::IRValueType > getBoolObjectType(bool forceRawAttr=false)
std::shared_ptr< DiagnosticEngine > getDiagnosticEngine() const
yoi::indexT getModuleIndexByRealPath(const yoi::wstr &modRealPath)
std::shared_ptr< yoi::IRValueType > getUnsignedObjectType(bool forceRawAttr=false)
std::shared_ptr< IRFFITable > getIRFFITable()
std::shared_ptr< yoi::IRValueType > getForeignInt32ObjectType()
std::shared_ptr< yoi::IRValueType > getShortObjectType(bool forceRawAttr=false)
std::shared_ptr< yoi::IRValueType > getNoneObjectType()
yoi::indexT compileModule(const yoi::wstr &filepath)
std::shared_ptr< yoi::IRValueType > getIntObjectType(bool forceRawAttr=false)
std::shared_ptr< IRObjectFile > irObjectFile
std::shared_ptr< IRFFITable > irFFITable
std::shared_ptr< IRModule > getImportedModule(yoi::indexT index)
std::shared_ptr< BuiltinModuleBuilder > builtinModuleBuilder
void setDiagnosticEngine(const std::shared_ptr< DiagnosticEngine > &engine)
std::shared_ptr< IRBuildConfig > getBuildConfig() const
std::shared_ptr< yoi::IRValueType > getDeciObjectType(bool forceRawAttr=false)
yoi::IRValueType normalizeForeignBasicType(const std::shared_ptr< yoi::IRValueType > &type, bool handlePointer=true)
std::shared_ptr< yoi::IRValueType > getStrObjectType(bool forceRawAttr=false)
std::shared_ptr< DiagnosticEngine > diagnosticEngine
yoi::indexT getIndex(const A &k)
Definition def.hpp:209
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:172
token scan()
Definition lexer.cpp:29
static const char * __yoi_builtin_module_hoshi
std::string wstring2string(const std::wstring &v)
Definition def.cpp:230
std::shared_ptr< T > managedPtr(const T &v)
Definition def.hpp:335
std::wstring string2wstring(const std::string &v)
Definition def.cpp:224
yoi::wstr realpath(const std::wstring &path)
Definition def.cpp:236
thread_local yoi::wstr __current_file_path
Definition def.cpp:15
void set_diagnostic_engine(DiagnosticEngine *engine)
Definition def.cpp:19
void set_current_file_path(const std::wstring &path)
Definition def.cpp:123
void finalizeAST(funcTypeSpec *ptr)
Definition ast.cpp:475
std::wstring wstr
Definition def.hpp:51
uint64_t indexT
Definition def.hpp:54
void parse(yoi::basicLiterals *&o, yoi::lexer &lex)
Definition parser.cpp:25