Skip to content

Commit a81154b

Browse files
committed
runtime: Move sections to main runtime class so they're global per-runtime
1 parent 25a7cb9 commit a81154b

File tree

5 files changed

+79
-80
lines changed

5 files changed

+79
-80
lines changed

lib/include/pl/core/evaluator.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,6 @@ namespace pl::core {
160160
void popSectionId();
161161
[[nodiscard]] u64 getSectionId() const;
162162
[[nodiscard]] u64 getUserSectionId() const;
163-
[[nodiscard]] u64 createSection(const std::string &name);
164-
void removeSection(u64 id);
165-
[[nodiscard]] std::vector<u8>& getSection(u64 id);
166-
[[nodiscard]] u64 getSectionSize(u64 id);
167-
[[nodiscard]] const std::map<u64, api::Section>& getSections() const;
168-
169-
[[nodiscard]] u64 getSectionCount() const;
170163

171164
void setInVariables(const std::map<std::string, Token::Literal> &inVariables) {
172165
this->m_inVariables = inVariables;
@@ -500,8 +493,6 @@ namespace pl::core {
500493
std::atomic<bool> m_aborted;
501494

502495
std::vector<u64> m_sectionIdStack;
503-
std::map<u64, api::Section> m_sections;
504-
u64 m_sectionId = 0;
505496

506497
std::vector<std::vector<u8>> m_heap;
507498
std::map<u32, PatternLocalData> m_patternLocalStorage;

lib/include/pl/pattern_language.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ namespace pl {
257257
* @param id ID of the section
258258
* @return Memory of the section
259259
*/
260-
[[nodiscard]] const std::vector<u8>& getSection(u64 id) const;
260+
[[nodiscard]] std::vector<u8>& getSection(u64 id);
261261

262262
/**
263263
* @brief Gets all custom sections that were created
@@ -411,9 +411,21 @@ namespace pl {
411411

412412
[[nodiscard]] const std::set<pl::ptrn::Pattern*>& getPatternsWithAttribute(const std::string &attribute) const;
413413

414+
[[nodiscard]] u64 getSectionSize(u64 id);
415+
[[nodiscard]] u64 getSectionCount() const;
416+
[[nodiscard]] u64 createSection(const std::string &name);
417+
void removeSection(u64 id);
418+
414419
private:
415420
void flattenPatterns();
416421

422+
friend class core::Evaluator;
423+
424+
struct SectionData {
425+
std::map<u64, api::Section> sections;
426+
u64 nextSectionId = 0;
427+
};
428+
417429
private:
418430
Internals m_internals;
419431
std::vector<core::err::CompileError> m_compileErrors;
@@ -450,6 +462,8 @@ namespace pl {
450462
std::function<bool()> m_dangerousFunctionCallCallback;
451463
core::LogConsole::Callback m_logCallback;
452464

465+
std::shared_ptr<SectionData> m_sectionData;
466+
453467
struct Function {
454468
api::Namespace nameSpace;
455469
std::string name;

lib/source/pl/core/evaluator.cpp

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ namespace pl::core {
699699
else
700700
err::E0011.throwError(fmt::format("Tried accessing out of bounds pattern local cell {}. This is a bug.", pattern->getHeapAddress()));
701701
} else {
702-
return this->getSection(pattern->getSection());
702+
return this->m_patternLanguage->getSection(pattern->getSection());
703703
}
704704
};
705705

@@ -912,20 +912,16 @@ namespace pl::core {
912912
} else if (sectionId == ptrn::Pattern::InstantiationSectionId) {
913913
err::E0012.throwError("Cannot access data of type that hasn't been placed in memory.");
914914
} else {
915-
if (this->m_sections.contains(sectionId)) {
916-
auto &section = this->m_sections[sectionId];
917-
918-
if (!write) {
919-
if ((address + size) <= section.data.size())
920-
std::memmove(buffer, section.data.data() + address, size);
921-
else
922-
std::memset(buffer, 0x00, size);
923-
} else {
924-
if ((address + size) <= section.data.size())
925-
std::memmove(section.data.data() + address, buffer, size);
926-
}
927-
} else
928-
err::E0012.throwError(fmt::format("Tried accessing a non-existing section with id {}.", sectionId));
915+
auto &section = this->getRuntime().getSection(sectionId);
916+
if (!write) {
917+
if ((address + size) <= section.size())
918+
std::memmove(buffer, section.data() + address, size);
919+
else
920+
std::memset(buffer, 0x00, size);
921+
} else {
922+
if ((address + size) <= section.size())
923+
std::memmove(section.data() + address, buffer, size);
924+
}
929925
}
930926

931927
if (this->isDebugModeEnabled()) [[unlikely]]
@@ -959,54 +955,11 @@ namespace pl::core {
959955
return 0;
960956
}
961957

962-
963-
u64 Evaluator::createSection(const std::string &name) {
964-
auto id = this->m_sectionId;
965-
this->m_sectionId++;
966-
967-
this->m_sections.insert({ id, { name, { } } });
968-
return id;
969-
}
970-
971-
void Evaluator::removeSection(u64 id) {
972-
this->m_sections.erase(id);
973-
}
974-
975-
std::vector<u8>& Evaluator::getSection(u64 id) {
976-
if (id == ptrn::Pattern::MainSectionId)
977-
err::E0011.throwError("Cannot access main section.");
978-
else if (id == ptrn::Pattern::HeapSectionId)
979-
return this->m_heap.back();
980-
else if (this->m_sections.contains(id))
981-
return this->m_sections[id].data;
982-
else if (id == ptrn::Pattern::InstantiationSectionId)
983-
err::E0012.throwError("Cannot access data of type that hasn't been placed in memory.");
984-
else
985-
err::E0011.throwError(fmt::format("Tried accessing a non-existing section with id {}.", id));
986-
}
987-
988-
u64 Evaluator::getSectionSize(u64 id) {
989-
if (id == ptrn::Pattern::MainSectionId)
990-
return this->getDataSize();
991-
else
992-
return this->getSection(id).size();
993-
}
994-
995-
const std::map<u64, api::Section> &Evaluator::getSections() const {
996-
return this->m_sections;
997-
}
998-
999-
u64 Evaluator::getSectionCount() const {
1000-
return this->m_sections.size();
1001-
}
1002-
1003958
bool Evaluator::evaluate(const std::vector<std::shared_ptr<ast::ASTNode>> &ast) {
1004959
this->m_readOrderReversed = false;
1005960
this->m_currBitOffset = 0;
1006961

1007-
this->m_sections.clear();
1008962
this->m_sectionIdStack.clear();
1009-
this->m_sectionId = 1;
1010963
this->m_outVariables.clear();
1011964
this->m_outVariableValues.clear();
1012965

lib/source/pl/lib/std/mem.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace pl::lib::libstd::mem {
7676
if (section == 0xFFFF'FFFF'FFFF'FFFF)
7777
section = ctx->getUserSectionId();
7878

79-
return u128(ctx->getSectionSize(section));
79+
return u128(ctx->getRuntime().getSectionSize(section));
8080
});
8181

8282
/* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
@@ -181,14 +181,14 @@ namespace pl::lib::libstd::mem {
181181
runtime.addFunction(nsStdMem, "create_section", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
182182
auto name = params[0].toString(false);
183183

184-
return u128(ctx->createSection(name));
184+
return u128(ctx->getRuntime().createSection(name));
185185
});
186186

187187
/* delete_section(id) */
188188
runtime.addFunction(nsStdMem, "delete_section", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
189189
auto id = u64(params[0].toUnsigned());
190190

191-
ctx->removeSection(id);
191+
ctx->getRuntime().removeSection(id);
192192

193193
return std::nullopt;
194194
});
@@ -197,15 +197,15 @@ namespace pl::lib::libstd::mem {
197197
runtime.addFunction(nsStdMem, "get_section_size", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
198198
auto id = u64(params[0].toUnsigned());
199199

200-
return u128(ctx->getSection(id).size());
200+
return u128(ctx->getRuntime().getSection(id).size());
201201
});
202202

203203
/* set_section_size(id, size) */
204204
runtime.addFunction(nsStdMem, "set_section_size", FunctionParameterCount::exactly(2), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
205205
auto id = u64(params[0].toUnsigned());
206206
auto size = size_t(params[1].toUnsigned());
207207

208-
ctx->getSection(id).resize(size);
208+
ctx->getRuntime().getSection(id).resize(size);
209209

210210
return std::nullopt;
211211
});
@@ -225,7 +225,7 @@ namespace pl::lib::libstd::mem {
225225
else if (toId == ptrn::Pattern::HeapSectionId)
226226
err::E0012.throwError("Invalid section id.");
227227

228-
auto& section = ctx->getSection(toId);
228+
auto& section = ctx->getRuntime().getSection(toId);
229229
if (section.size() < toAddr + size)
230230
section.resize(toAddr + size);
231231
std::memcpy(section.data() + toAddr, data.data(), size);
@@ -243,7 +243,7 @@ namespace pl::lib::libstd::mem {
243243
else if (toId == ptrn::Pattern::HeapSectionId)
244244
err::E0012.throwError("Invalid section id.");
245245

246-
auto& section = ctx->getSection(toId);
246+
auto& section = ctx->getRuntime().getSection(toId);
247247

248248
switch (params[0].getType()) {
249249
using enum Token::ValueType;

lib/source/pl/pattern_language.cpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <wolv/io/file.hpp>
1919
#include <wolv/utils/string.hpp>
2020

21+
#include <pl/core/errors/runtime_errors.hpp>
22+
2123
namespace pl {
2224

2325
static std::string getFunctionName(const api::Namespace &ns, const std::string &name) {
@@ -43,6 +45,8 @@ namespace pl {
4345

4446
this->m_internals.evaluator->setRuntime(this);
4547

48+
this->m_sectionData = std::make_shared<SectionData>();
49+
4650
if (addLibStd)
4751
lib::libstd::registerFunctions(*this);
4852

@@ -97,6 +101,8 @@ namespace pl {
97101
this->m_runId.exchange(other.m_runId.load());
98102
this->m_subRuntime = other.m_subRuntime;
99103

104+
this->m_sectionData = std::move(other.m_sectionData);
105+
100106
m_startAddress = std::move(other.m_startAddress);
101107
m_defaultEndian = other.m_defaultEndian;
102108
m_runningTime = other.m_runningTime;
@@ -124,6 +130,7 @@ namespace pl {
124130

125131
runtime.m_functions = this->m_functions;
126132
runtime.m_subRuntime = true;
133+
runtime.m_sectionData = this->m_sectionData;
127134

128135
return runtime;
129136
}
@@ -470,16 +477,45 @@ namespace pl {
470477
return this->m_internals.evaluator->getPatternLimit();
471478
}
472479

473-
const std::vector<u8>& PatternLanguage::getSection(u64 id) const {
480+
std::vector<u8>& PatternLanguage::getSection(u64 id) {
474481
static std::vector<u8> empty;
475-
if (id > this->m_internals.evaluator->getSectionCount() || id == ptrn::Pattern::MainSectionId || id == ptrn::Pattern::HeapSectionId)
476-
return empty;
482+
if (id == ptrn::Pattern::MainSectionId)
483+
core::err::E0012.throwError("Cannot access main section.");
484+
else if (id == ptrn::Pattern::HeapSectionId)
485+
return this->getInternals().evaluator->m_heap.back();
486+
else if (id == ptrn::Pattern::InstantiationSectionId)
487+
core::err::E0012.throwError("Cannot access data of type that hasn't been placed in memory.");
488+
else if (this->m_sectionData->sections.contains(id))
489+
return this->m_sectionData->sections.at(id).data;
477490
else
478-
return this->m_internals.evaluator->getSection(id);
491+
core::err::E0012.throwError(fmt::format("Tried accessing a non-existing section with id {}.", id));
479492
}
480493

481494
[[nodiscard]] const std::map<u64, api::Section>& PatternLanguage::getSections() const {
482-
return this->m_internals.evaluator->getSections();
495+
return this->m_sectionData->sections;
496+
}
497+
498+
u64 PatternLanguage::createSection(const std::string &name) {
499+
auto id = this->m_sectionData->nextSectionId;
500+
this->m_sectionData->nextSectionId++;
501+
502+
this->m_sectionData->sections.insert({ id, { name, { } } });
503+
return id;
504+
}
505+
506+
void PatternLanguage::removeSection(u64 id) {
507+
this->m_sectionData->sections.erase(id);
508+
}
509+
510+
u64 PatternLanguage::getSectionSize(u64 id) {
511+
if (id == ptrn::Pattern::MainSectionId)
512+
return this->m_dataSize;
513+
else
514+
return this->getSection(id).size();
515+
}
516+
517+
u64 PatternLanguage::getSectionCount() const {
518+
return this->m_sectionData->sections.size();
483519
}
484520

485521
[[nodiscard]] const std::vector<std::shared_ptr<ptrn::Pattern>> &PatternLanguage::getPatterns(u64 section) const {
@@ -516,6 +552,11 @@ namespace pl {
516552
this->m_internals.parser->setParserManager(&m_parserManager);
517553
this->m_patternsValid = false;
518554

555+
if (!this->m_subRuntime) {
556+
this->m_sectionData->nextSectionId = 1;
557+
this->m_sectionData->sections.clear();
558+
}
559+
519560
this->m_resolvers.setDefaultResolver([this](const std::string& path) {
520561
return this->m_fileResolver.resolve(path);
521562
});

0 commit comments

Comments
 (0)