/* -*- C++ -*- * Serene Programming Language * * Copyright (c) 2019-2022 Sameer Rahmani * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef SERENE_CONTEXT_H #define SERENE_CONTEXT_H #include "serene/export.h" // for SERENE_EXPORT #include "serene/options.h" #include // for Triple #include // for Twine #include // for LLVMContext #include // for getDefaultTargetTriple #include // for function #include // for make_unique, unique_ptr #include // for string, basic_string #include // for vector #define DEFAULT_NS_NAME "serene.user" #define INTERNAL_NS "serene.internal" namespace serene { class SereneContext; /// This enum describes the different operational phases for the compiler /// in order. Anything below `NoOptimization` is considered only for debugging enum class CompilationPhase { Parse, Analysis, SLIR, MLIR, // Lowered slir to other dialects LIR, // Lowered to the llvm ir dialect IR, // Lowered to the LLVMIR itself NoOptimization, O1, O2, O3, }; /// Terminates the serene compiler process in a thread safe manner /// This function is only meant to be used in the compiler context /// if you want to terminate the process in context of a serene program /// via the JIT use an appropriate function in the `serene.core` ns. SERENE_EXPORT void terminate(SereneContext &ctx, int exitCode); class SERENE_EXPORT SereneContext { public: template using CurrentNSFn = std::function; /// The set of options to change the compilers behaivoirs Options opts; const llvm::Triple triple; explicit SereneContext(Options &options) : opts(options), triple(llvm::sys::getDefaultTargetTriple()), targetPhase(CompilationPhase::NoOptimization){}; /// Set the target compilation phase of the compiler. The compilation /// phase dictates the behavior and the output type of the compiler. void setOperationPhase(CompilationPhase phase); CompilationPhase getTargetPhase() { return targetPhase; }; int getOptimizatioLevel(); static std::unique_ptr genLLVMContext() { return std::make_unique(); }; /// Setup the load path for namespace lookups void setLoadPaths(std::vector &dirs) { loadPaths.swap(dirs); }; /// Return the load paths for namespaces std::vector &getLoadPaths() { return loadPaths; }; // JIT JITDylib related functions --- // TODO: For Dylib related functions, make sure that the namespace in questoin // is aleady registered in the context /// Return a pointer to the most registered JITDylib of the given \p ns ////name // llvm::orc::JITDylib *getLatestJITDylib(Namespace &ns); // /// Register the given pointer to a `JITDylib` \p l, with the give \p ns. // void pushJITDylib(Namespace &ns, llvm::orc::JITDylib *l); // /// Returns the number of registered `JITDylib` for the given \p ns. // size_t getNumberOfJITDylibs(Namespace &ns); private: CompilationPhase targetPhase; std::vector loadPaths; /// A vector of pointers to all the jitDylibs for namespaces. Usually /// There will be only one pre NS but in case of forceful reloads of a /// namespace there will be more. // llvm::StringMap> jitDylibs; }; /// Creates a new context object. Contexts are used through out the compilation /// process to store the state. /// /// \p opts is an instance of \c Options that can be used to set options of /// of the compiler. SERENE_EXPORT std::unique_ptr makeSereneContext(Options opts = Options()); } // namespace serene #endif