Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance bytecode reader #469

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(bytecode reader): update the symbols, values and code method of t…
…he BytecodeReader to avoid multiple reads of the same data
  • Loading branch information
SuperFola committed Jun 5, 2024
commit 5b40490eaa4f7d689aae595e2c0f08914316767b
12 changes: 8 additions & 4 deletions include/Ark/Compiler/BytecodeReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace Ark
{
class State;

enum class BytecodeSegment
{
All,
Expand Down Expand Up @@ -127,16 +129,16 @@ namespace Ark
[[nodiscard]] Symbols symbols() const;

/**
*
* @param symbols
* @return Values
*/
[[nodiscard]] Values values() const;
[[nodiscard]] Values values(const Symbols& symbols) const;

/**
*
* @param values
* @return Code
*/
[[nodiscard]] Code code() const;
[[nodiscard]] Code code(const Values& values) const;

/**
* @brief Display the bytecode opcode in a human friendly way.
Expand All @@ -151,6 +153,8 @@ namespace Ark
std::optional<uint16_t> sEnd = std::nullopt,
std::optional<uint16_t> cPage = std::nullopt);

friend class Ark::State;

private:
bytecode_t m_bytecode;

Expand Down
17 changes: 7 additions & 10 deletions src/arkreactor/Compiler/BytecodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,20 @@ namespace Ark
return block;
}

Values BytecodeReader::values() const
Values BytecodeReader::values(const Symbols& symbols) const
{
if (!checkMagic())
return {};

const auto data = symbols();
std::size_t i = data.end;
std::size_t i = symbols.end;
if (m_bytecode[i] != VAL_TABLE_START)
return {};
i++;


const uint16_t size = readNumber(i);
i++;
Values block;
block.start = data.end;
block.start = symbols.end;
block.values.reserve(size);

for (uint16_t j = 0; j < size; ++j)
Expand Down Expand Up @@ -170,13 +168,12 @@ namespace Ark
return block;
}

Code BytecodeReader::code() const
Code BytecodeReader::code(const Values& values) const
{
if (!checkMagic())
return {};

const auto data = values();
std::size_t i = data.end;
std::size_t i = values.end;

Code block;
block.start = i;
Expand Down Expand Up @@ -236,8 +233,8 @@ namespace Ark
}

const auto syms = symbols();
const auto vals = values();
const auto code_block = code();
const auto vals = values(syms);
const auto code_block = code(vals);

// symbols table
{
Expand Down
12 changes: 7 additions & 5 deletions src/arkreactor/VM/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ namespace Ark
#endif
}

// FIXME: we're going to read the symbols 3 times and the values twice
// because code calls values which calls symbols
m_symbols = bcr.symbols().symbols;
m_constants = bcr.values().values;
m_pages = bcr.code().pages;
const auto syms = bcr.symbols();
const auto vals = bcr.values(syms);
const auto code = bcr.code(vals);

m_symbols = syms.symbols;
m_constants = vals.values;
m_pages = code.pages;
}

void State::reset() noexcept
Expand Down
5 changes: 3 additions & 2 deletions tests/unittests/BytecodeReaderSuite.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <boost/ut.hpp>

#include <Ark/Compiler/BytecodeReader.hpp>

#include <string>

using namespace boost;
Expand Down Expand Up @@ -35,9 +36,9 @@ ut::suite<"BytecodeReader"> bcr_suite = [] {
expect(that % sha256 == expected_sha);
};

const auto [pages, start_code] = bcr.code();
const auto values_block = bcr.values();
const auto symbols_block = bcr.symbols();
const auto values_block = bcr.values(symbols_block);
const auto [pages, start_code] = bcr.code(values_block);

should("list all symbols") = [symbols_block] {
using namespace std::literals::string_literals;
Expand Down
Loading