From a45494d861260696dc6454ec75d64f99bd8f3db4 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Tue, 26 Dec 2023 12:04:16 +0100 Subject: [PATCH] stratom numatom --- src/vm/includes/Cell.h | 18 ++++++++++++++---- src/vm/includes/ConsUtils.h | 2 +- src/vm/includes/MemoryContext.h | 24 ++++++++++++++++++++++++ src/vm/src/ConsUtils.cpp | 2 +- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/vm/includes/Cell.h b/src/vm/includes/Cell.h index f7d2427..62decaf 100644 --- a/src/vm/includes/Cell.h +++ b/src/vm/includes/Cell.h @@ -7,9 +7,12 @@ #include #include +#include +#include enum class CellType { - INT, + NUMATOM, + STRATOM, CONS }; @@ -23,13 +26,20 @@ struct Cell { bool live = false; }; -struct ValueCell : public Cell { - ValueCell() = delete; - explicit ValueCell(CellValType val) : Cell(CellType::INT), _val(val) {} +struct NumAtomCell : public Cell { + NumAtomCell() = delete; + explicit NumAtomCell(CellValType val) : Cell(CellType::NUMATOM), _val(val) {} CellValType _val; }; +struct StrAtomCell : public Cell { + StrAtomCell() = delete; + explicit StrAtomCell(std::string val) : Cell(CellType::STRATOM), _val(std::move(val)) {} + + std::string _val; +}; + struct ConsCell : public Cell { ConsCell() : Cell(CellType::CONS) {} explicit ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {} diff --git a/src/vm/includes/ConsUtils.h b/src/vm/includes/ConsUtils.h index 7f767ba..c7d5cc7 100644 --- a/src/vm/includes/ConsUtils.h +++ b/src/vm/includes/ConsUtils.h @@ -11,7 +11,7 @@ namespace ConsUtils { static inline MCHandle car(const MCHandle &cell) { return dynamic_cast(*cell)._car; } static inline MCHandle cdr(const MCHandle &cell) { return dynamic_cast(*cell)._cdr; } - static inline CellValType val(const MCHandle &cell) { return dynamic_cast(*cell)._val; } + static inline CellValType val(const MCHandle &cell) { return dynamic_cast(*cell)._val; } MCHandle cons(const MCHandle &car, const MCHandle &cdr); MCHandle pop(MCHandle &from); MCHandle push(MCHandle &to, const MCHandle &what); diff --git a/src/vm/includes/MemoryContext.h b/src/vm/includes/MemoryContext.h index 296560b..c1c0809 100644 --- a/src/vm/includes/MemoryContext.h +++ b/src/vm/includes/MemoryContext.h @@ -24,6 +24,28 @@ public: return cell; } + template<> + Handle create_cell(CellValType val) { + if (numatom_index.contains(val)) + return numatom_index.at(val); + + NumAtomCell *cell = new NumAtomCell(val); + _cells.emplace_back(cell); + numatom_index.emplace(val, cell); + return cell; + } + + template<> + Handle create_cell(std::string val) { + if (stratom_index.contains(val)) + return stratom_index.at(val); + + StrAtomCell *cell = new StrAtomCell(std::move(val)); + _cells.emplace_back(cell); + stratom_index.emplace(cell->_val, cell); + return cell; + } + class Handle { public: Handle(Cell *target) : _target(target) {} @@ -38,6 +60,8 @@ public: private: std::list _cells; + std::unordered_map numatom_index; + std::unordered_map stratom_index; }; using MCHandle = MemoryContext::Handle; diff --git a/src/vm/src/ConsUtils.cpp b/src/vm/src/ConsUtils.cpp index f37749e..fa482c8 100644 --- a/src/vm/src/ConsUtils.cpp +++ b/src/vm/src/ConsUtils.cpp @@ -33,7 +33,7 @@ void ConsUtils::append(MCHandle to, const MCHandle &what) { } MCHandle ConsUtils::makeIntCell(int64_t val) { - return CURRENT_MC.load()->create_cell(val); + return CURRENT_MC.load()->create_cell(val); } void ConsUtils::setcar(const MCHandle &to, const MCHandle &car) {