diff --git a/src/bytecode/bytearray.cc b/src/bytecode/bytearray.cc index 9968642..e8fbbd9 100644 --- a/src/bytecode/bytearray.cc +++ b/src/bytecode/bytearray.cc @@ -24,6 +24,14 @@ void ByteArray::add_int(uint32_t addr, int32_t value) add_byte(addr, zz & 0x7F); } +void ByteArray::add_uint32(uint32_t addr, uint32_t value) +{ + add_byte(addr, (uint8_t)(value)); + add_byte(addr+1, (uint8_t)(value >> 8)); + add_byte(addr+2, (uint8_t)(value >> 16)); + add_byte(addr+3, (uint8_t)(value >> 24)); +} + void ByteArray::add_float(uint32_t addr, float value) { uint32_t bits; @@ -46,6 +54,14 @@ uint8_t ByteArray::get_byte(uint32_t addr) const return data_.at(addr); } +uint32_t ByteArray::get_uint32(uint32_t addr) const +{ + return (uint32_t) get_byte(addr) + | (uint32_t) get_byte(addr+1) << 8 + | (uint32_t) get_byte(addr+2) << 16 + | (uint32_t) get_byte(addr+3) << 24; +} + std::pair ByteArray::get_int(uint32_t addr) const { uint32_t zz = 0; diff --git a/src/bytecode/bytearray.hh b/src/bytecode/bytearray.hh index 4b25e2f..c189ba6 100644 --- a/src/bytecode/bytearray.hh +++ b/src/bytecode/bytearray.hh @@ -10,17 +10,23 @@ namespace tyche { class ByteArray { public: + ByteArray() = default; + explicit ByteArray(std::vector data) : data_(std::move(data)) {} + void add_byte(uint32_t addr, uint8_t byte); + void add_uint32(uint32_t addr, uint32_t value); void add_int(uint32_t addr, int32_t value); void add_float(uint32_t addr, float value); void add_string(uint32_t addr, std::string const& str); void append_byte(uint32_t addr, uint8_t byte) { add_byte(data_.size(), byte); } + void append_uint32(uint32_t addr, uint32_t value) { add_uint32(data_.size(), value); } void append_int(uint32_t addr, int32_t value) { add_int(data_.size(), value); } void append_float(uint32_t addr, float value) { add_float(data_.size(), value); } void append_string(uint32_t addr, std::string const& str) { add_string(data_.size(), str); } [[nodiscard]] uint8_t get_byte(uint32_t addr) const; + [[nodiscard]] uint32_t get_uint32(uint32_t addr) const; [[nodiscard]] std::pair get_int(uint32_t addr) const; [[nodiscard]] std::pair get_float(uint32_t addr) const; [[nodiscard]] std::pair get_string(uint32_t addr) const; diff --git a/src/bytecode/bytecode.cc b/src/bytecode/bytecode.cc index d9171fc..5069221 100644 --- a/src/bytecode/bytecode.cc +++ b/src/bytecode/bytecode.cc @@ -2,5 +2,67 @@ namespace tyche { +Bytecode::Bytecode(BytecodePrototype const& bp) +{ + // header + byte_array_.add_uint32(0, MAGIC); + byte_array_.add_byte(4, VERSION); + + // constants + std::vector constant_indexes; + std::vector constant_array; + for (auto const& constant: bp.constants) { + + } + + // constants table + + // function table +} + +uint32_t Bytecode::n_constants() const +{ + return 0; +} + +uint32_t Bytecode::n_functions() const +{ + return 0; +} + +int32_t Bytecode::get_constant_int(uint32_t addr) const +{ + return 0; +} + +float Bytecode::get_constant_float(uint32_t addr) const +{ + return 0; +} + +std::string Bytecode::get_constant_string(uint32_t addr) const +{ + return std::string(); +} + +Bytecode::FunctionDef Bytecode::get_function_def(uint32_t function_id) const +{ + return Bytecode::FunctionDef(); +} + +uint8_t Bytecode::get_code_byte(uint32_t function_id, uint32_t idx) const +{ + return 0; +} + +int32_t Bytecode::get_code_int(uint32_t function_id, uint32_t idx) const +{ + return 0; +} + +float Bytecode::get_code_float(uint32_t function_id, uint32_t idx) const +{ + return 0; +} } \ No newline at end of file diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index 4b2e69a..2095bba 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -8,8 +8,8 @@ namespace tyche { class Bytecode { public: - Bytecode(std::vector data); - Bytecode(BytecodePrototype const& bp); + explicit Bytecode(std::vector data) : byte_array_(std::move(data)) {} + explicit Bytecode(BytecodePrototype const& bp); [[nodiscard]] uint32_t n_constants() const; [[nodiscard]] uint32_t n_functions() const; @@ -29,6 +29,8 @@ public: private: ByteArray byte_array_; + static constexpr uint8_t VERSION = 1; + static constexpr uint32_t MAGIC = 0x74b3c138; }; }