| chigraph master Systems programming language written for beginners in LLVM

Context.hpp
Go to the documentation of this file.
1
4 #ifndef CHI_CONTEXT_HPP
5 #define CHI_CONTEXT_HPP
6
7 #pragma once
8
9 #include <memory>
10 #include <unordered_map>
11
12 #include "chi/Fwd.hpp"
13 #include "chi/ModuleCache.hpp"
14 #include "chi/Support/Flags.hpp"
15 #include "chi/Support/json.hpp"
16
17 #include <llvm/IR/LLVMContext.h>
18 #include <llvm/Support/CodeGen.h> // for CodeGenOpt
19
20 #include <boost/filesystem/path.hpp>
21 #include <boost/utility/string_view.hpp>
22
23 namespace chi {
24
26 enum class CompileSettings {
27
29 UseCache = 1u,
30
35 LinkDependencies = 1u << 1,
36
38 Default = UseCache | LinkDependencies
39 };
40
55 struct Context {
58 Context(const boost::filesystem::path& workPath = {});
59
61 ~Context();
62
63 // no move or copy, doesn't make sense
64 Context(const Context& context) = delete;
65 Context(Context&&) = delete;
66
71 ChiModule* moduleByFullName(const boost::filesystem::path& fullModuleName) const noexcept;
72
76 GraphModule* newGraphModule(const boost::filesystem::path& fullName);
77
80 // init it (pretty sure it inits windows networking stuff)
81 std::vector<std::string> listModulesInWorkspace() const noexcept;
82
89 Result loadModule(const boost::filesystem::path& name, ChiModule** toFill = nullptr);
90
96 Result addModuleFromJson(const boost::filesystem::path& fullName, const nlohmann::json& json,
97 GraphModule** toFill = nullptr);
98
102 bool addModule(std::unique_ptr<ChiModule> modToAdd) noexcept;
103
107 bool unloadModule(const boost::filesystem::path& fullName);
108
115 Result typeFromModule(const boost::filesystem::path& module, boost::string_view name,
116 DataType* toFill) noexcept;
117
126 Result nodeTypeFromModule(const boost::filesystem::path& moduleName,
127 boost::string_view typeName, const nlohmann::json& data,
128 std::unique_ptr<NodeType>* toFill) noexcept;
129
134 std::unique_ptr<NodeType> createConverterNodeType(const DataType& fromType, const DataType& toType);
135
138 boost::filesystem::path workspacePath() const { return mWorkspacePath; }
141 bool hasWorkspace() const noexcept { return !workspacePath().empty(); }
142
151 Result compileModule(const boost::filesystem::path& fullName, Flags<CompileSettings> settings,
152 std::unique_ptr<llvm::Module>* toFill);
153
161 Result compileModule(ChiModule& mod, Flags<CompileSettings> settings,
162 std::unique_ptr<llvm::Module>* toFill);
163
168 std::vector<NodeInstance*> findInstancesOfType(const boost::filesystem::path& moduleName,
169 boost::string_view typeName) const;
170
173 llvm::LLVMContext& llvmContext() { return mLLVMContext; }
174
177 LangModule* langModule() const { return mLangModule; }
178
181 std::vector<ChiModule*> modules() const {
182 std::vector<ChiModule*> ret;
183
184 for (const auto& mod : mModules) { ret.push_back(mod.get()); }
185
186 return ret;
187 }
188
191 const ModuleCache& moduleCache() const { return *mModuleCache; }
192
194 ModuleCache& moduleCache() { return *mModuleCache; }
195
199 void setModuleCache(std::unique_ptr<ModuleCache> newCache);
200
201 private:
202 boost::filesystem::path mWorkspacePath;
203
204 llvm::LLVMContext mLLVMContext;
205
206 std::vector<std::unique_ptr<ChiModule>> mModules;
207
208 // This cache is only for use during compilation to not duplicate modules
209 std::unordered_map<std::string /*full name*/, llvm::Module* /*the compiled module*/>
210 mCompileCache;
211
212 LangModule* mLangModule = nullptr;
213
214 std::unique_ptr<ModuleCache> mModuleCache;
215
216 std::unordered_map<std::string /*from Type*/, std::unordered_map<std::string /*to type*/, std::unique_ptr<NodeType>>> mTypeConverters;
217 };
218
224 boost::filesystem::path workspaceFromChildPath(const boost::filesystem::path& path);
225
229 std::string stringifyLLVMType(llvm::Type* ty);
230
239 Result interpretLLVMIR(std::unique_ptr<llvm::Module> mod,
240 llvm::CodeGenOpt::Level optLevel = llvm::CodeGenOpt::Default,
241 const std::vector<llvm::GenericValue>& args = {},
242 llvm::Function* funcToRun = nullptr, llvm::GenericValue* ret = nullptr);
243
251 Result interpretLLVMIRAsMain(std::unique_ptr<llvm::Module> mod,
252 llvm::CodeGenOpt::Level optLevel = llvm::CodeGenOpt::Default,
253 const std::vector<std::string>& args = {},
254 llvm::Function* funcToRun = nullptr, int* ret = nullptr);
255 } // namespace chi
256
257 #endif // CHI_CONTEXT_HPP
chi::Context::langModule
LangModule * langModule() const
Get the LangModule, if it has been loaded.
Definition: Context.hpp:177
Fwd.hpp
Forward declares all the chigraph data types.
chi::LangModule
The module that provides built-in operations like literals, math operations, etc. ...
Definition: LangModule.hpp:16
chi::CompileSettings::Default
Default, which is both.
chi::Context::workspacePath
boost::filesystem::path workspacePath() const
Get the workspace path of the Context.
Definition: Context.hpp:138
ModuleCache.hpp
chi::Context::modules
std::vector< ChiModule * > modules() const
Get the modules in the Context.
Definition: Context.hpp:181
chi::CompileSettings
CompileSettings
Settings for compiling modules.
Definition: Context.hpp:26
chi::workspaceFromChildPath
boost::filesystem::path workspaceFromChildPath(const boost::filesystem::path &path)
Get the workspace directory from a child of the workspace directory Example: say you have a workspace...
Definition: Context.cpp:462
chi::Context
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
chi::ChiModule
An abstract class that represents a module of code in Chigraph Can be compiled to a llvm::Module...
Definition: ChiModule.hpp:24
chi::interpretLLVMIRAsMain
Result interpretLLVMIRAsMain(std::unique_ptr< llvm::Module > mod, llvm::CodeGenOpt::Level optLevel=llvm::CodeGenOpt::Default, const std::vector< std::string > &args={}, llvm::Function *funcToRun=nullptr, int *ret=nullptr)
Interpret LLVM IR as if it were the main function.
Definition: Context.cpp:564
chi::Context::moduleCache
ModuleCache & moduleCache()
Get the module cache.
Definition: Context.hpp:194
chi::interpretLLVMIR
Result interpretLLVMIR(std::unique_ptr< llvm::Module > mod, llvm::CodeGenOpt::Level optLevel=llvm::CodeGenOpt::Default, const std::vector< llvm::GenericValue > &args={}, llvm::Function *funcToRun=nullptr, llvm::GenericValue *ret=nullptr)
Interpret LLVM IR, just a convenience function.
Definition: Context.cpp:528
chi::Flags
A template class for type-safe flags.
Definition: Flags.hpp:26
chi::Context::llvmContext
llvm::LLVMContext & llvmContext()
Get the LLVMContext
Definition: Context.hpp:173
chi::Context::moduleCache
const ModuleCache & moduleCache() const
Get the module cache.
Definition: Context.hpp:191
chi::ModuleCache
This class provides an interface for creating module caches.
Definition: ModuleCache.hpp:16
chi::GraphModule
Module that holds graph functions.
Definition: GraphModule.hpp:16
chi::Context::hasWorkspace
bool hasWorkspace() const noexcept
Check if this context has a workspace bound to it – same as !workspacePath().empty() ...
Definition: Context.hpp:141
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12
chi::CompileSettings::UseCache
Use the cache in lib.
chi::stringifyLLVMType
std::string stringifyLLVMType(llvm::Type *ty)
Turns a type into a string.
Definition: Context.cpp:474
chi::DataType
A type of data Loose wrapper around llvm::Type*, except it knows which ChiModule it&#39;s in and it embed...
Definition: DataType.hpp:17
chi::CompileSettings::LinkDependencies
Link in dependencies If this is set, it will be a ready to run module If not, it&#39;ll contain forward d...
chi::Result
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
Flags.hpp