Skip to content

Commit

Permalink
VM: remove redundant instructions
Browse files Browse the repository at this point in the history
I added these instructions previously to improve performance, but I figured out these instructions were adding too much complexity and the performance benefits were not that huge
  • Loading branch information
mrunix00 committed Jul 5, 2024
1 parent b782f1c commit cf04da0
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 247 deletions.
28 changes: 0 additions & 28 deletions include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,19 @@ struct Instruction {
enum InstructionType : uint8_t {
Invalid = 0,
AddI32,
AddI32_RI,
AddI32_GI,
AddI64,
AddI64_RI,
AddI64_GI,
SubI32,
SubI32_RI,
SubI32_GI,
SubI64,
SubI64_RI,
SubI64_GI,
MulI32,
MulI32_RI,
MulI32_GI,
MulI64,
MulI64_RI,
MulI64_GI,
DivI32,
DivI32_RI,
DivI32_GI,
DivI64,
DivI64_RI,
DivI64_GI,
ModI32,
ModI32_RI,
ModI32_GI,
ModI64,
ModI64_RI,
ModI64_GI,
GreaterI32,
GreaterI32_RI,
GreaterI32_GI,
GreaterI64,
GreaterI64_RI,
GreaterI64_GI,
LessI32,
LessI32_RI,
LessI32_GI,
LessI64,
LessI64_RI,
LessI64_GI,
GreaterEqualI32,
GreaterEqualI64,
LessEqualI32,
Expand Down
79 changes: 0 additions & 79 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,6 @@ static inline void assert(bool condition, const char *message) {
GENERATE_EMIT_FUNCTION(Load)
GENERATE_EMIT_FUNCTION(Store)

#define GENERATE_EMIT_FUNCTION_RI(OPERATION) \
static inline void emit##OPERATION##RI(Program &program, Segment &segment, Node *left, Node *right) { \
Instruction instruction; \
size_t id; \
bool isLocal; \
Variable::Type type; \
if (segment.find_local(left->token.value) != -1) { \
isLocal = true; \
id = segment.find_local(left->token.value); \
type = segment.locals[left->token.value].type; \
} else if (program.find_global(left->token.value) != -1) { \
isLocal = false; \
id = program.find_global(left->token.value); \
type = program.segments[0].locals[left->token.value].type; \
} else { \
throw std::runtime_error("[BinaryExpression::compile] Identifier not found: " + left->token.value); \
} \
switch (type) { \
case Variable::Type::I32: \
instruction.type = isLocal ? Instruction::InstructionType::OPERATION##I32_RI \
: Instruction::InstructionType::OPERATION##I32_GI; \
instruction.params.ri32 = { \
.index = id, \
.i32 = std::stoi(right->token.value), \
}; \
break; \
case Variable::Type::I64: \
instruction.type = isLocal ? Instruction::InstructionType::OPERATION##I64_RI \
: Instruction::InstructionType::OPERATION##I64_GI; \
instruction.params.ri64 = { \
.index = id, \
.i64 = std::stol(right->token.value), \
}; \
break; \
default: \
throw std::runtime_error("[BinaryExpression::compile] Invalid variable type!"); \
} \
segment.instructions.push_back(instruction); \
}
GENERATE_EMIT_FUNCTION_RI(Add)
GENERATE_EMIT_FUNCTION_RI(Sub)
GENERATE_EMIT_FUNCTION_RI(Mul)
GENERATE_EMIT_FUNCTION_RI(Div)
GENERATE_EMIT_FUNCTION_RI(Mod)
GENERATE_EMIT_FUNCTION_RI(Greater)
GENERATE_EMIT_FUNCTION_RI(Less)

Node::Node(Token token) : token(std::move(token)) {
nodeType = Type::Node;
typeStr = "Node";
Expand Down Expand Up @@ -149,38 +102,6 @@ void BinaryExpression::compile(Program &program, Segment &segment) const {
return;
}

if (right->nodeType == AbstractSyntaxTree::Type::Node &&
left->nodeType == AbstractSyntaxTree::Type::Node &&
((Node *) left)->token.type == Identifier &&
((Node *) right)->token.type == Number) {
switch (op.type) {
case Plus:
emitAddRI(program, segment, (Node *) left, (Node *) right);
break;
case Minus:
emitSubRI(program, segment, (Node *) left, (Node *) right);
break;
case Multiply:
emitMulRI(program, segment, (Node *) left, (Node *) right);
break;
case Divide:
emitDivRI(program, segment, (Node *) left, (Node *) right);
break;
case Modulo:
emitModRI(program, segment, (Node *) left, (Node *) right);
break;
case Greater:
emitGreaterRI(program, segment, (Node *) left, (Node *) right);
break;
case Less:
emitLessRI(program, segment, (Node *) left, (Node *) right);
break;
default:
throw std::runtime_error("[BinaryExpression::compile] Invalid operator: " + op.value);
}
return;
}

left->compile(program, segment);
if (right->nodeType == AbstractSyntaxTree::Type::Node &&
((Node *) right)->token.value == "1" &&
Expand Down
Loading

0 comments on commit cf04da0

Please sign in to comment.