53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#ifndef TYCHE_BYTECODE_HH
|
|
#define TYCHE_BYTECODE_HH
|
|
|
|
#include "bytearray.hh"
|
|
#include "bytecodeprototype.hh"
|
|
|
|
namespace tyche {
|
|
|
|
class Bytecode {
|
|
public:
|
|
explicit Bytecode(std::vector<uint8_t> data);
|
|
explicit Bytecode(BytecodePrototype const& bp);
|
|
|
|
[[nodiscard]] uint32_t n_constants() const;
|
|
[[nodiscard]] uint32_t n_functions() const;
|
|
|
|
[[nodiscard]] int32_t get_constant_int(uint32_t idx) const;
|
|
[[nodiscard]] float get_constant_float(uint32_t idx) const;
|
|
[[nodiscard]] std::string get_constant_string(uint32_t idx) const;
|
|
|
|
struct FunctionDef { uint16_t n_params, locals; };
|
|
[[nodiscard]] FunctionDef get_function_def(uint32_t function_id) const;
|
|
|
|
[[nodiscard]] uint8_t get_code_byte(uint32_t function_id, uint32_t idx) const;
|
|
[[nodiscard]] int32_t get_code_int(uint32_t function_id, uint32_t idx) const;
|
|
[[nodiscard]] float get_code_float(uint32_t function_id, uint32_t idx) const;
|
|
|
|
// TODO - debugging info
|
|
|
|
[[nodiscard]] std::vector<uint8_t> const& data() const { return byte_array_.data(); }
|
|
|
|
private:
|
|
ByteArray byte_array_; // the actual data
|
|
|
|
static constexpr uint8_t VERSION = 1;
|
|
static constexpr uint32_t MAGIC = 0x74b3c138;
|
|
static constexpr uint32_t HEADER_SZ = 16,
|
|
INDEX_SZ = 8 * 6;
|
|
|
|
// caching for faster reading of data
|
|
struct Cache {
|
|
uint32_t constants_addr;
|
|
uint16_t n_constants;
|
|
uint32_t functions_addr;
|
|
uint32_t n_functions;
|
|
};
|
|
Cache cache_ {};
|
|
};
|
|
|
|
}
|
|
|
|
#endif //TYCHE_BYTECODE_HH
|