bytecode-improvements #3
@@ -58,7 +58,7 @@ FetchContent_MakeAvailable(googletest)
|
||||
# library
|
||||
#
|
||||
|
||||
add_library(lib${PROJECT_NAME} STATIC
|
||||
add_library(lib${PROJECT_NAME} SHARED
|
||||
src/bytecode/bytearray.hh
|
||||
src/bytecode/bytearray.cc
|
||||
src/bytecode/bytecode.cc
|
||||
|
||||
2
TODO.md
2
TODO.md
@@ -10,7 +10,7 @@
|
||||
- [x] Refactor bytecode code
|
||||
|
||||
Improvements:
|
||||
- [ ] Fixed int type (based on opcode)
|
||||
- [x] Fixed int type (based on opcode)
|
||||
- [ ] Constant type (only floats and strings for now)
|
||||
- [ ] Debug output bytecode format
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "bytearray.hh"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
namespace tyche {
|
||||
|
||||
@@ -132,4 +133,23 @@ void ByteArray::append_bytearray(ByteArray const& bytearray)
|
||||
data_.insert(data_.end(), bytearray.data().begin(), bytearray.data().end());
|
||||
}
|
||||
|
||||
std::string ByteArray::hexdump() const
|
||||
{
|
||||
auto to_hex = [](uint32_t value, size_t n_chars) -> std::string {
|
||||
char buf[15];
|
||||
snprintf(buf, sizeof buf, (std::string("%0") + std::to_string(n_chars) + "X").c_str(), value);
|
||||
return { buf };
|
||||
};
|
||||
|
||||
std::string out;
|
||||
for (size_t i = 0; i < data_.size(); ++i) {
|
||||
if (i % 16 == 0)
|
||||
out += to_hex(i, 4) + " | ";
|
||||
out += to_hex(data_.at(i), 2) + " ";
|
||||
if (i % 16 == 15)
|
||||
out += "\n";
|
||||
}
|
||||
return out + "\n";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,6 +45,8 @@ public:
|
||||
[[nodiscard]] std::vector<uint8_t> const& data() const { return data_; }
|
||||
[[nodiscard]] size_t size() const { return data_.size(); }
|
||||
|
||||
[[nodiscard]] std::string hexdump() const;
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> data_ {};
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace tyche {
|
||||
|
||||
struct BytecodePrototype {
|
||||
using ConstantValue = std::variant<int32_t, float, std::string>;
|
||||
using ConstantValue = std::variant<float, std::string>;
|
||||
|
||||
struct Function {
|
||||
uint16_t n_pars;
|
||||
|
||||
@@ -44,7 +44,7 @@ TEST(ByteArray, ByteArray)
|
||||
TEST(Bytecode, Constants)
|
||||
{
|
||||
BytecodePrototype bp;
|
||||
bp.constants.emplace_back(42);
|
||||
bp.constants.emplace_back(42.3f);
|
||||
bp.constants.emplace_back("HELLO");
|
||||
|
||||
std::vector<uint8_t> expected = {
|
||||
@@ -57,7 +57,7 @@ TEST(Bytecode, Constants)
|
||||
// index
|
||||
0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // constant index
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // function undex
|
||||
0x58, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // raw constants
|
||||
0x58, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, // raw constants
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // raw code
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
@@ -66,14 +66,14 @@ TEST(Bytecode, Constants)
|
||||
|
||||
// constant indexes
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x00, 0x00,
|
||||
|
||||
// constant values
|
||||
0x54, 'H', 'E', 'L', 'L', 'O', 0x00
|
||||
0x33, 0x33, 0x29, 0x42, // float: 42.3f
|
||||
'H', 'E', 'L', 'L', 'O', 0x00
|
||||
};
|
||||
|
||||
ByteArray ba = Bytecode::generate(bp);
|
||||
// print(ba.data()); print(expected);
|
||||
ASSERT_EQ(ba.data(), expected);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ TEST(Bytecode, Code)
|
||||
BytecodePrototype bp;
|
||||
auto& f = bp.functions.emplace_back(0, 0);
|
||||
f.code.append_byte(0x68);
|
||||
f.code.append_int32(42);
|
||||
f.code.append_int8(42);
|
||||
|
||||
auto& f2 = bp.functions.emplace_back(2, 1);
|
||||
f2.code.append_byte(0x42);
|
||||
@@ -109,7 +109,7 @@ TEST(Bytecode, Code)
|
||||
0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
|
||||
|
||||
// code
|
||||
0x68, 0x54, 0x42,
|
||||
0x68, 42, 0x42,
|
||||
};
|
||||
|
||||
ByteArray ba = Bytecode::generate(bp);
|
||||
@@ -122,7 +122,6 @@ TEST(Bytecode, Parsing)
|
||||
|
||||
BytecodePrototype bp;
|
||||
|
||||
bp.constants.emplace_back(42);
|
||||
bp.constants.emplace_back(3.14f);
|
||||
bp.constants.emplace_back("HELLO");
|
||||
|
||||
@@ -143,8 +142,8 @@ TEST(Bytecode, Parsing)
|
||||
ASSERT_EQ(bc.n_constants(), 2);
|
||||
ASSERT_EQ(bc.n_functions(), 2);
|
||||
|
||||
ASSERT_FLOAT_EQ(bc.get_constant_float(1), 3.14f);
|
||||
ASSERT_EQ(bc.get_constant_string(2), "HELLO");
|
||||
ASSERT_FLOAT_EQ(bc.get_constant_float(0), 3.14f);
|
||||
ASSERT_EQ(bc.get_constant_string(1), "HELLO");
|
||||
|
||||
Bytecode::FunctionDef f1 = bc.get_function_def(0);
|
||||
ASSERT_EQ(f1.n_params, 0);
|
||||
|
||||
Reference in New Issue
Block a user