This commit is contained in:
2026-05-02 08:19:20 -05:00
parent de961d5d2c
commit a1dbc40961
7 changed files with 44 additions and 70 deletions

View File

@@ -68,6 +68,7 @@ Logical/arithmetic:
2e pow Power
2f shl Shift left
30 shr Shift right
31 mod Modulo
Other value operations:
40 len Get table, array or string size

View File

@@ -53,6 +53,8 @@ static int init_ = []() {
BIN_OP(ShiftRight, Integer, Integer) { return Value::CreateInteger(a.as_integer() >> b.as_integer()); };
BIN_OP(Modulo, Integer, Integer) { return Value::CreateInteger(a.as_integer() % b.as_integer()); };
#undef BIN_OP
return 0;

View File

@@ -7,7 +7,7 @@ namespace tyche::vm {
enum class BinaryOperationType
{
Sum, Subtraction, Multiplication, Division, IntegerDivision, Equality, Inequality, LessThan, LessThanOrEquals,
GreaterThan, GreaterThanOrEquals, BitwiseAnd, BitwiseOr, BitwiseXor, Power, ShiftLeft, ShiftRight,
GreaterThan, GreaterThanOrEquals, BitwiseAnd, BitwiseOr, BitwiseXor, Power, ShiftLeft, ShiftRight, Modulo,
COUNT
};

View File

@@ -46,6 +46,7 @@ const std::unordered_map<std::string, Instruction> instruction_names = {
{ "pow", Instruction::Power },
{ "shl", Instruction::ShiftLeft },
{ "shr", Instruction::ShiftRight },
{ "mod", Instruction::Modulo },
{ "len", Instruction::Len },
{ "type", Instruction::Type },
{ "cast", Instruction::Cast },
@@ -133,6 +134,7 @@ std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper)
case Instruction::Power: out = "pow"; break;
case Instruction::ShiftLeft: out = "shl"; break;
case Instruction::ShiftRight: out = "shr"; break;
case Instruction::Modulo: out = "mod"; break;
case Instruction::Len: out = "len"; break;
case Instruction::Type: out = "type"; break;
case Instruction::Cast: out = "cast"; break;

View File

@@ -77,6 +77,7 @@ enum class Instruction : uint8_t {
Power = 0x2e,
ShiftLeft = 0x2f,
ShiftRight = 0x30,
Modulo = 0x31,
// other value operations
Len = 0x40,

View File

@@ -125,6 +125,7 @@ TEST(VM, IntegerIntegerOperations)
ASSERT_EQ(test_op(2, 3, "pow"), 8);
ASSERT_EQ(test_op(2, 3, "shl"), 16);
ASSERT_EQ(test_op(30, 2, "shr"), 7);
ASSERT_EQ(test_op(8, 3, "mod"), 2);
// TODO - div
}

View File

@@ -52,82 +52,49 @@ void VM::run_until_return()
void VM::step()
{
Operation op = code_.operation(loc_.top());
Value a, b;
switch (op.instruction) {
//
// stack management
//
case Instruction::PushInt8:
case Instruction::PushInt16:
case Instruction::PushInt32:
push_integer(op.operator_);
break;
case Instruction::Sum:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Sum));
break;
case Instruction::Subtract:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Subtraction));
break;
case Instruction::Multiply:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Multiplication));
break;
case Instruction::Divide:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Division));
break;
case Instruction::DivideInt:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::IntegerDivision));
break;
case Instruction::Equals:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Equality));
break;
case Instruction::NotEquals:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Inequality));
break;
case Instruction::LessThan:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::LessThan));
break;
case Instruction::LessThanEq:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::LessThanOrEquals));
break;
case Instruction::GreaterThan:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::GreaterThan));
break;
case Instruction::GreaterThanEq:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::GreaterThanOrEquals));
break;
case Instruction::And:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseAnd));
break;
case Instruction::Or:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseOr));
break;
case Instruction::Xor:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseXor));
break;
case Instruction::Power:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Power));
break;
case Instruction::ShiftLeft:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::ShiftLeft));
break;
case Instruction::ShiftRight:
a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::ShiftRight));
break;
//
// logical/arithmetic
//
#define BIN_OP(op) { Value a = stack_.pop(); Value b = stack_.pop(); stack_.push(binary_operation(a, b, BinaryOperationType::op)); }
case Instruction::Sum: BIN_OP(Sum) break;
case Instruction::Subtract: BIN_OP(Subtraction) break;
case Instruction::Multiply: BIN_OP(Multiplication) break;
case Instruction::Divide: BIN_OP(Division) break;
case Instruction::DivideInt: BIN_OP(IntegerDivision) break;
case Instruction::Equals: BIN_OP(Equality) break;
case Instruction::NotEquals: BIN_OP(Inequality) break;
case Instruction::LessThan: BIN_OP(LessThan) break;
case Instruction::LessThanEq: BIN_OP(LessThanOrEquals) break;
case Instruction::GreaterThan: BIN_OP(GreaterThan) break;
case Instruction::GreaterThanEq: BIN_OP(GreaterThanOrEquals) break;
case Instruction::And: BIN_OP(BitwiseAnd) break;
case Instruction::Or: BIN_OP(BitwiseOr) break;
case Instruction::Xor: BIN_OP(BitwiseXor) break;
case Instruction::Power: BIN_OP(Power) break;
case Instruction::ShiftLeft: BIN_OP(ShiftLeft) break;
case Instruction::ShiftRight: BIN_OP(ShiftRight) break;
case Instruction::Modulo: BIN_OP(Modulo) break;
#undef BIN_OP
//
// function operations
//
case Instruction::Return: {
Value v = stack_.pop();
stack_.pop_fp();