Remove unnecessary code from context

This commit is contained in:
Sameer Rahmani 2022-06-13 22:24:19 +01:00
parent 3cc5da9789
commit 592d7c4fb0
4 changed files with 3 additions and 250 deletions

View File

@ -10,6 +10,8 @@
#define I32_SIZE 32
#define I64_SIZE 64
#define MAX_PATH_SLOTS 256
// Should we build the support for MLIR CL OPTIONS?
#cmakedefine SERENE_WITH_MLIR_CL_OPTION

View File

@ -19,8 +19,7 @@
#ifndef SERENE_CONTEXT_H
#define SERENE_CONTEXT_H
#include "serene/export.h" // for SERENE_EXPORT
#include "serene/source_mgr.h" // for SourceMgr
#include "serene/export.h" // for SERENE_EXPORT
#include <llvm/ADT/Triple.h> // for Triple
#include <llvm/ADT/Twine.h> // for Twine
@ -81,10 +80,6 @@ public:
template <typename T>
using CurrentNSFn = std::function<T()>;
/// The source manager is responsible for loading files and practically
/// managing the source code in form of memory buffers.
SourceMgr sourceManager;
/// The set of options to change the compilers behaivoirs
Options opts;

View File

@ -1,182 +0,0 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef SERENE_SOURCE_MGR_H
#define SERENE_SOURCE_MGR_H
#include "serene/export.h"
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/Support/ErrorOr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <mlir/IR/Diagnostics.h>
#include <mlir/Support/Timing.h>
#include <memory>
#define SMGR_LOG(...) \
DEBUG_WITH_TYPE("sourcemgr", llvm::dbgs() \
<< "[SMGR]: " << __VA_ARGS__ << "\n");
namespace serene {
namespace reader {
class LocationRange;
} // namespace reader
class SereneContext;
/// This class is quite similar to the `llvm::SourceMgr` in functionality. We
/// even borrowed some of the code from the original implementation but removed
/// a lot of code that were irrelevant to us.
///
/// SouceMgr is responsible for finding a namespace in the `loadPaths` and read
/// the content of the `.srn` (or any of the `DEFAULT_SUFFIX`) into a
/// `llvm::MemoryBuffer` embedded in a `SrcBuffer` object as the owner of the
/// source files and then it will call the `reader` on the buffer to parse it
/// and create the actual `Namespace` object from the parsed AST.
///
/// Later on, whenever we need to refer to the source file of a namespace for
/// diagnosis purposes or any other purpose we can use the functions in this
/// class to get hold of a pointer to a specific `reader::Location` of the
/// buffer.
///
/// Note: Unlike the original version, SourceMgr does not handle the diagnostics
/// and it uses the Serene's `DiagnosticEngine` for that matter.
class SERENE_EXPORT SourceMgr {
public:
// TODO: Make it a vector of supported suffixes
std::string DEFAULT_SUFFIX = "srn";
private:
struct SrcBuffer {
/// The memory buffer for the file.
std::unique_ptr<llvm::MemoryBuffer> buffer;
/// Vector of offsets into Buffer at which there are line-endings
/// (lazily populated). Once populated, the '\n' that marks the end of
/// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since
/// these offsets are in sorted (ascending) order, they can be
/// binary-searched for the first one after any given offset (eg. an
/// offset corresponding to a particular SMLoc).
///
/// Since we're storing offsets into relatively small files (often smaller
/// than 2^8 or 2^16 bytes), we select the offset vector element type
/// dynamically based on the size of Buffer.
mutable void *offsetCache = nullptr;
/// Look up a given \p ptr in in the buffer, determining which line it came
/// from.
unsigned getLineNumber(const char *ptr) const;
template <typename T>
unsigned getLineNumberSpecialized(const char *ptr) const;
/// Return a pointer to the first character of the specified line number or
/// null if the line number is invalid.
const char *getPointerForLineNumber(unsigned lineNo) const;
template <typename T>
const char *getPointerForLineNumberSpecialized(unsigned lineNo) const;
/// This is the location of the parent import or unknown location if it is
/// the main namespace
// reader::LocationRange importLoc;
SrcBuffer() = default;
SrcBuffer(SrcBuffer &&) noexcept;
SrcBuffer(const SrcBuffer &) = delete;
SrcBuffer &operator=(const SrcBuffer &) = delete;
~SrcBuffer();
};
using MemBufPtr = std::unique_ptr<llvm::MemoryBuffer>;
/// This is all of the buffers that we are reading from.
std::vector<SrcBuffer> buffers;
/// A hashtable that works as an index from namespace names to the buffer
/// position it the `buffer`
llvm::StringMap<unsigned> nsTable;
// This is the list of directories we should search for include files in.
std::vector<std::string> loadPaths;
// Find a namespace file with the given \p name in the load path and \r retuns
// a unique pointer to the memory buffer containing the content or an error.
// In the success case it will put the path of the file into the \p
// importedFile.
MemBufPtr findFileInLoadPath(const std::string &name,
std::string &importedFile);
bool isValidBufferID(unsigned i) const;
/// Converts the ns name to a partial path by replacing the dots with slashes
static std::string convertNamespaceToPath(std::string ns_name);
public:
SourceMgr() = default;
SourceMgr(const SourceMgr &) = delete;
SourceMgr &operator=(const SourceMgr &) = delete;
SourceMgr(SourceMgr &&) = default;
SourceMgr &operator=(SourceMgr &&) = default;
~SourceMgr() = default;
/// Set the `loadPaths` to the given \p dirs. `loadPaths` is a vector of
/// directories that Serene will look in order to find a file that constains a
/// namespace which it is looking for.
void setLoadPaths(std::vector<std::string> &dirs) { loadPaths.swap(dirs); }
/// Return a reference to a `SrcBuffer` with the given ID \p i.
const SrcBuffer &getBufferInfo(unsigned i) const {
assert(isValidBufferID(i));
return buffers[i - 1];
}
/// Return a reference to a `SrcBuffer` with the given namspace name \p ns.
const SrcBuffer &getBufferInfo(llvm::StringRef ns) const {
auto bufferId = nsTable.lookup(ns);
if (bufferId == 0) {
// No such namespace
llvm_unreachable("couldn't find the src buffer for a namespace. It "
"should never happen.");
}
return buffers[bufferId - 1];
}
/// Return a pointer to the internal `llvm::MemoryBuffer` of the `SrcBuffer`
/// with the given ID \p i.
const llvm::MemoryBuffer *getMemoryBuffer(unsigned i) const {
assert(isValidBufferID(i));
return buffers[i - 1].buffer.get();
}
unsigned getNumBuffers() const { return buffers.size(); }
/// Add a new source buffer to this source manager. This takes ownership of
/// the memory buffer.
unsigned AddNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> f,
reader::LocationRange includeLoc);
};
}; // namespace serene
#endif

View File

@ -21,68 +21,6 @@
#include <cstdlib> // for exit
namespace serene {
// void SereneContext::setOperationPhase(CompilationPhase phase) {
// this->targetPhase = phase;
// if (phase == CompilationPhase::SLIR) {
// return;
// }
// if (phase >= CompilationPhase::MLIR) {
// pm.addPass(serene::passes::createSLIRLowerToMLIRPass());
// }
// if (phase >= CompilationPhase::LIR) {
// pm.addPass(serene::passes::createSLIRLowerToLLVMDialectPass());
// }
// };
// int SereneContext::getOptimizatioLevel() {
// if (targetPhase <= CompilationPhase::NoOptimization) {
// return 0;
// }
// if (targetPhase == CompilationPhase::O1) {
// return 1;
// }
// if (targetPhase == CompilationPhase::O2) {
// return 2;
// }
// return 3;
// }
// llvm::orc::JITDylib *SereneContext::getLatestJITDylib(Namespace &ns) {
// if (jitDylibs.count(ns.name) == 0) {
// return nullptr;
// }
// auto vec = jitDylibs[ns.name];
// // TODO: Make sure that the returning Dylib still exists in the JIT
// // by calling jit->engine->getJITDylibByName(dylib_name);
// return vec.empty() ? nullptr : vec.back();
// };
// void SereneContext::pushJITDylib(Namespace &ns, llvm::orc::JITDylib *l) {
// if (jitDylibs.count(ns.name) == 0) {
// llvm::SmallVector<llvm::orc::JITDylib *, 1> vec{l};
// jitDylibs[ns.name] = vec;
// return;
// }
// auto vec = jitDylibs[ns.name];
// vec.push_back(l);
// jitDylibs[ns.name] = vec;
// }
// size_t SereneContext::getNumberOfJITDylibs(Namespace &ns) {
// if (jitDylibs.count(ns.name) == 0) {
// return 0;
// }
// auto vec = jitDylibs[ns.name];
// return vec.size();
// };
void terminate(SereneContext &ctx, int exitCode) {
(void)ctx;
// TODO: Since we are running in a single thread for now using exit is fine