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
45 yoi::wstr rFilepath;
46 if (filepath != L"builtin") {
47 for (auto &prep : buildConfig->searchPaths) {
48 std::filesystem::path final = prep / std::filesystem::path(filepath);
49 rFilepath = realpath(final.wstring());
50 if (std::filesystem::exists(rFilepath) && std::filesystem::is_regular_file(rFilepath)) {
51 break;
52 } else if (std::filesystem::exists(rFilepath + L".hoshi") && std::filesystem::is_regular_file(rFilepath + L".hoshi")) {
53 rFilepath += L".hoshi";
54 break;
55 } else if (std::filesystem::exists(rFilepath) && std::filesystem::is_directory(rFilepath) && std::filesystem::exists(std::filesystem::path(rFilepath) / "index.hoshi")) {
56 rFilepath = (std::filesystem::path(rFilepath) / "index.hoshi").wstring();
57 break;
58 } else {
59 rFilepath.clear();
60 continue;
61 }
62 }
63 } else {
65 }
66
67 if (rFilepath.empty()) {
68 throw std::runtime_error("file not resolved in all search paths: " + wstring2string(filepath));
69 }
70
71 try {
72 return modules.getIndex(rFilepath);
73 } catch (const std::out_of_range &e) {
74 auto fp = fopen(wstring2string(rFilepath).c_str(), "rb");
75 if (!fp)
76 throw std::runtime_error("invalid filename: " + wstring2string(rFilepath));
77
78 // temporarily add current directory to search path
79 buildConfig->searchPaths.push_back(std::filesystem::path(rFilepath).parent_path().wstring());
80 buildConfig->searchPaths.push_back(std::filesystem::path(rFilepath).parent_path().append(".pardo_modules").wstring());
81
82 fseek(fp, 0, SEEK_END);
83 auto size = ftell(fp);
84 fseek(fp, 0, SEEK_SET);
85 auto *a = new std::string(size, 0);
86 fread(a->data(), size, 1, fp);
87 auto *b = new yoi::wstr{yoi::string2wstring(*a)};
88 delete a;
89 fclose(fp);
90
91 yoi::lexer l{std::wstringstream(*b)};
92 l.scan();
93 delete b;
94 auto current_file = __current_file_path;
95 set_current_file_path(rFilepath);
96 hoshiModule *mod;
97 yoi::parse(mod, l);
98 std::shared_ptr<moduleContext> modCtx = std::make_shared<moduleContext>(shared_from_this(), rFilepath, mod);
99 std::shared_ptr<IRModule> irMod = std::make_shared<IRModule>();
100 irMod->modulePath = rFilepath;
101 auto idx = modules.put(rFilepath, modCtx);
102 irMod->identifier = idx;
103 moduleImported[idx] = irMod;
104 std::shared_ptr<visitor> vis = std::make_shared<visitor>(modCtx, irMod, idx);
105 vis->visit();
106
107 astToFinalize.insert(mod);
108 set_current_file_path(current_file);
109
110 // pop current directory from search path
111 buildConfig->searchPaths.pop_back();
112 buildConfig->searchPaths.pop_back();
113
114 return idx;
115 }
116 }
117
118 const std::shared_ptr<IRObjectFile> &compilerContext::getIRObjectFile() const {
119 return irObjectFile;
120 }
121
123 auto builtinModule = std::make_shared<IRModule>();
124
126
127 builtinModuleBuilder = std::make_shared<BuiltinModuleBuilder>(builtinModule);
128 builtinModuleBuilder->build();
129 irFFITable = std::make_shared<IRFFITable>();
130
132 l.scan();
133 hoshiModule *mod;
134 yoi::parse(mod, l);
135 builtinModuleContext = std::make_shared<moduleContext>(shared_from_this(), L"builtin", mod);
136 std::shared_ptr<visitor> vis = std::make_shared<visitor>(builtinModuleContext, builtinModule, HOSHI_COMPILER_CTX_GLOB_ID_CONST);
137 vis->visit();
138 astToFinalize.insert(mod);
139 }
140
141 std::shared_ptr<yoi::IRValueType> compilerContext::getIntObjectType(bool forceRawAttr) {
142 return forceRawAttr ? managedPtr(builtinModuleBuilder->getIntObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"int"]);
143 }
144
145 std::shared_ptr<yoi::IRValueType> compilerContext::getBoolObjectType(bool forceRawAttr) {
146 return forceRawAttr ? managedPtr(builtinModuleBuilder->getBoolObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"bool"]);
147 }
148
149 std::shared_ptr<yoi::IRValueType> compilerContext::getDeciObjectType(bool forceRawAttr) {
150 return forceRawAttr ? managedPtr(builtinModuleBuilder->getDeciObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"deci"]);
151 }
152
153 std::shared_ptr<yoi::IRValueType> compilerContext::getStrObjectType(bool forceRawAttr) {
154 return forceRawAttr ? managedPtr(builtinModuleBuilder->getStrObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"string"]);
155 }
156
157 std::shared_ptr<yoi::IRValueType> compilerContext::getCharObjectType(bool forceRawAttr) {
158 return forceRawAttr ? managedPtr(builtinModuleBuilder->getCharObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"char"]);
159 }
160
161 std::shared_ptr<yoi::IRValueType> compilerContext::getNoneObjectType() {
162 return builtinModuleBuilder->sharedValueType[L"none"];
163 }
164
166 const std::shared_ptr<IRObjectFile> &irObjectFile) {
167 this->irObjectFile = irObjectFile;
168 }
169
170 std::shared_ptr<IRBuildConfig> compilerContext::getBuildConfig() const {
171 return buildConfig;
172 }
173
175 const std::shared_ptr<IRBuildConfig> &buildConfig) {
176 this->buildConfig = buildConfig;
177 }
178
179 std::shared_ptr<IRFFITable> compilerContext::getIRFFITable() {
180 return irFFITable;
181 }
182
183 std::shared_ptr<yoi::IRValueType> compilerContext::getForeignInt32ObjectType() {
184 return builtinModuleBuilder->sharedValueType[L"foreignInt32Type"];
185 }
186
187 std::shared_ptr<yoi::IRValueType> compilerContext::getForeignFloatObjectType() {
188 return builtinModuleBuilder->sharedValueType[L"foreignFloatType"];
189 }
190
191 std::shared_ptr<yoi::IRValueType> compilerContext::getNullInterfaceType() {
192 return builtinModuleBuilder->sharedValueType[L"NullInterface"];
193 }
194
195 std::shared_ptr<yoi::moduleContext> compilerContext::getModuleContext(yoi::indexT index) {
197 }
198
200 for (auto &i : astToFinalize) {
201 finalizeAST(i);
202 }
203 }
204
206 IROptimizer opt{shared_from_this(), 0};
207 opt.buildCallGraph();
208 opt.optimize();
209 }
210
211 std::shared_ptr<yoi::IRValueType> compilerContext::getPointerType() {
212 return builtinModuleBuilder->sharedValueType[L"ptr"];
213 }
214 yoi::IRValueType compilerContext::normalizeForeignBasicType(const std::shared_ptr<yoi::IRValueType> &type, bool handlePointer) {
215 if (type->isForeignBasicType()) {
216 switch (type->type) {
218 return *getIntObjectType();
220 return handlePointer ? *getUnsignedObjectType() : *type;
222 return *getDeciObjectType();
223 default:
224 throw std::runtime_error("unknown foreign basic type");
225 }
226 }
227 return *type;
228 }
229
230 std::shared_ptr<yoi::IRValueType> compilerContext::getShortObjectType(bool forceRawAttr) {
231 return forceRawAttr ? managedPtr(builtinModuleBuilder->getShortObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"short"]);
232 }
233
234 std::shared_ptr<yoi::IRValueType> compilerContext::getUnsignedObjectType(bool forceRawAttr) {
235 return forceRawAttr ? managedPtr(builtinModuleBuilder->getUnsignedObject()) : managedPtr(*builtinModuleBuilder->sharedValueType[L"unsigned"]);
236 }
237} // 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)
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)
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
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)
yoi::indexT getIndex(const A &k)
Definition def.hpp:198
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:161
token scan()
Definition lexer.cpp:29
static const char * __yoi_builtin_module_hoshi
std::string wstring2string(const std::wstring &v)
Definition def.cpp:184
std::shared_ptr< T > managedPtr(const T &v)
Definition def.hpp:324
std::wstring string2wstring(const std::string &v)
Definition def.cpp:178
yoi::wstr realpath(const std::wstring &path)
Definition def.cpp:190
thread_local yoi::wstr __current_file_path
Definition def.cpp:14
void set_current_file_path(const std::wstring &path)
Definition def.cpp:113
void finalizeAST(funcTypeSpec *ptr)
Definition ast.cpp:475
std::wstring wstr
Definition def.hpp:48
uint64_t indexT
Definition def.hpp:51
void parse(yoi::basicLiterals *&o, yoi::lexer &lex)
Definition parser.cpp:25