Skip to content

Commit

Permalink
feat(bytecode reader): factorising code inside the bytecode reader to…
Browse files Browse the repository at this point in the history
… promote code reuse
  • Loading branch information
SuperFola committed Jun 4, 2024
1 parent 7323dfe commit e3bdb18
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 305 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __arkscript__/
*.arkc
*.arkm
/*.ark
!tests/unittests/resources/BytecodeReaderSuite/*.arkc

# Generated files
include/Ark/Constants.hpp
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- fixed a bug in the compiler where one could "use" operators without calling them: `(print nil?)`
- fixed a bug in the compiler allowing the use of operators without any argument: `(+)`
- fixed a bug in the vm during error reporting when a non-function was used as a function
- refactored code inside the bytecode reader to promote code reuse

### Removed
- removed unused `NodeType::Closure`
Expand Down
68 changes: 66 additions & 2 deletions include/Ark/Compiler/BytecodeReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file BytecodeReader.hpp
* @author Alexandre Plateau (lexplt.dev@gmail.com)
* @brief A bytecode disassembler for ArkScript
* @version 0.4
* @version 0.5
* @date 2020-10-27
*
* @copyright Copyright (c) 2020-2024
Expand All @@ -19,6 +19,7 @@

#include <Ark/Platform.hpp>
#include <Ark/Compiler/Common.hpp>
#include <Ark/VM/Value.hpp>

namespace Ark
{
Expand All @@ -31,6 +32,33 @@ namespace Ark
HeadersOnly
};

struct Version
{
uint16_t major;
uint16_t minor;
uint16_t patch;
};

struct Symbols
{
std::vector<std::string> symbols;
std::size_t start; ///< Point to the SYM_TABLE_START byte in the bytecode
std::size_t end; ///< Point to the byte following the last byte of the table in the bytecode
};

struct Values
{
std::vector<Value> values;
std::size_t start; ///< Point to the VAL_TABLE_START byte in the bytecode
std::size_t end; ///< Point to the byte following the last byte of the table in the bytecode
};

struct Code
{
std::vector<bytecode_t> pages;
std::size_t start; ///< Point to the CODE_SEGMENT_START byte in the bytecode
};

/**
* @brief This class is just a helper to
* - check if a bytecode is valid
Expand All @@ -53,19 +81,55 @@ namespace Ark
*/
void feed(const std::string& file);

/**
* Check for the presence of the magic header
* @return true if the magic 'ark\0' was found
*/
[[nodiscard]] bool checkMagic() const;

/**
* @brief Return the bytecode object constructed
*
* @return const bytecode_t&
*/
[[nodiscard]] const bytecode_t& bytecode() noexcept;

/**
*
* @return Version compiler version used to create the given bytecode file
*/
[[nodiscard]] Version version() const;

/**
* @brief Return the read timestamp from the bytecode file
*
* @return unsigned long long
*/
[[nodiscard]] unsigned long long timestamp();
[[nodiscard]] unsigned long long timestamp() const;

/**
*
* @return std::vector<unsigned char> bytecode sha
*/
[[nodiscard]] std::vector<unsigned char> sha256() const;

/**
*
* @return Symbols
*/
[[nodiscard]] Symbols symbols() const;

/**
*
* @return Values
*/
[[nodiscard]] Values values() const;

/**
*
* @return Code
*/
[[nodiscard]] Code code() const;

/**
* @brief Display the bytecode opcode in a human friendly way.
Expand Down
4 changes: 3 additions & 1 deletion include/Ark/VM/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace Ark
{
class VM;
class BytecodeReader;

// Note: we can have at most 0x7f (127) different types
// because type index is stored on the 7 right most bits of a uint8_t in the class Value.
Expand Down Expand Up @@ -153,12 +154,13 @@ namespace Ark
friend ARK_API_INLINE bool operator!(const Value& A) noexcept;

friend class Ark::VM;
friend class Ark::BytecodeReader;

private:
uint8_t m_const_type; ///< First bit if for constness, right most bits are for type
Value_t m_value;

[[nodiscard]] constexpr uint8_t type_num() const noexcept { return m_const_type & 0x7f; }
[[nodiscard]] constexpr uint8_t type_num() const noexcept { return m_const_type & 0x7f; } // TODO: rename typeNum

[[nodiscard]] internal::PageAddr_t pageAddr() const { return std::get<internal::PageAddr_t>(m_value); }
[[nodiscard]] const ProcType& proc() const { return std::get<ProcType>(m_value); }
Expand Down
Loading

0 comments on commit e3bdb18

Please sign in to comment.