bytecode2 #2
@@ -24,6 +24,14 @@ void ByteArray::add_int(uint32_t addr, int32_t value)
|
|||||||
add_byte(addr, zz & 0x7F);
|
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)
|
void ByteArray::add_float(uint32_t addr, float value)
|
||||||
{
|
{
|
||||||
uint32_t bits;
|
uint32_t bits;
|
||||||
@@ -46,6 +54,14 @@ uint8_t ByteArray::get_byte(uint32_t addr) const
|
|||||||
return data_.at(addr);
|
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<int32_t, size_t> ByteArray::get_int(uint32_t addr) const
|
std::pair<int32_t, size_t> ByteArray::get_int(uint32_t addr) const
|
||||||
{
|
{
|
||||||
uint32_t zz = 0;
|
uint32_t zz = 0;
|
||||||
|
|||||||
@@ -10,17 +10,23 @@ namespace tyche {
|
|||||||
|
|
||||||
class ByteArray {
|
class ByteArray {
|
||||||
public:
|
public:
|
||||||
|
ByteArray() = default;
|
||||||
|
explicit ByteArray(std::vector<uint8_t> data) : data_(std::move(data)) {}
|
||||||
|
|
||||||
void add_byte(uint32_t addr, uint8_t byte);
|
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_int(uint32_t addr, int32_t value);
|
||||||
void add_float(uint32_t addr, float value);
|
void add_float(uint32_t addr, float value);
|
||||||
void add_string(uint32_t addr, std::string const& str);
|
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_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_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_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); }
|
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]] uint8_t get_byte(uint32_t addr) const;
|
||||||
|
[[nodiscard]] uint32_t get_uint32(uint32_t addr) const;
|
||||||
[[nodiscard]] std::pair<int32_t, size_t> get_int(uint32_t addr) const;
|
[[nodiscard]] std::pair<int32_t, size_t> get_int(uint32_t addr) const;
|
||||||
[[nodiscard]] std::pair<float, size_t> get_float(uint32_t addr) const;
|
[[nodiscard]] std::pair<float, size_t> get_float(uint32_t addr) const;
|
||||||
[[nodiscard]] std::pair<std::string, size_t> get_string(uint32_t addr) const;
|
[[nodiscard]] std::pair<std::string, size_t> get_string(uint32_t addr) const;
|
||||||
|
|||||||
@@ -2,5 +2,67 @@
|
|||||||
|
|
||||||
namespace tyche {
|
namespace tyche {
|
||||||
|
|
||||||
|
Bytecode::Bytecode(BytecodePrototype const& bp)
|
||||||
|
{
|
||||||
|
// header
|
||||||
|
byte_array_.add_uint32(0, MAGIC);
|
||||||
|
byte_array_.add_byte(4, VERSION);
|
||||||
|
|
||||||
|
// constants
|
||||||
|
std::vector<uint32_t> constant_indexes;
|
||||||
|
std::vector<uint8_t> 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,8 @@ namespace tyche {
|
|||||||
|
|
||||||
class Bytecode {
|
class Bytecode {
|
||||||
public:
|
public:
|
||||||
Bytecode(std::vector<uint8_t> data);
|
explicit Bytecode(std::vector<uint8_t> data) : byte_array_(std::move(data)) {}
|
||||||
Bytecode(BytecodePrototype const& bp);
|
explicit Bytecode(BytecodePrototype const& bp);
|
||||||
|
|
||||||
[[nodiscard]] uint32_t n_constants() const;
|
[[nodiscard]] uint32_t n_constants() const;
|
||||||
[[nodiscard]] uint32_t n_functions() const;
|
[[nodiscard]] uint32_t n_functions() const;
|
||||||
@@ -29,6 +29,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
ByteArray byte_array_;
|
ByteArray byte_array_;
|
||||||
|
static constexpr uint8_t VERSION = 1;
|
||||||
|
static constexpr uint32_t MAGIC = 0x74b3c138;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user