Skip to content

Commit

Permalink
VM: Store variables as 64-bit values
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jul 28, 2024
1 parent 8f8f342 commit 5ef20f0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 146 deletions.
1 change: 0 additions & 1 deletion include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ VariableType::Type deduceType(Program &program, Segment &segment, AbstractSyntax
Instruction getInstructionWithType(GenericInstruction instruction, VariableType::Type type);
Instruction emitLoad(VariableType::Type, const Token &token);
void typeCast(std::vector<Instruction> &instructions, VariableType::Type from, VariableType::Type to);
size_t sizeOfType(VariableType::Type type);
VariableType::Type biggestType(VariableType::Type first, VariableType::Type second);
VariableType::Type getInstructionType(const Program &program, const Instruction &instruction);

Expand Down
30 changes: 11 additions & 19 deletions include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ struct Variable {
std::string name;
VariableType *type{};
size_t index{};
size_t size{};
Variable() = default;
Variable(std::string name, VariableType *type, size_t index, size_t size)
: name(std::move(name)), type(type), index(index), size(size){};
Variable(std::string name, VariableType *type, size_t index)
: name(std::move(name)), type(type), index(index){};
};

struct Segment {
Expand All @@ -148,14 +147,14 @@ struct Program {
};

struct StackFrame {
uint32_t *locals{};
uint64_t *locals{};
size_t localsSize{};
size_t segmentIndex{};
size_t currentInstruction{};
};

class VM {
uint32_t *stack;
uint64_t *stack;
size_t stackCapacity;
std::vector<StackFrame> callStack;

Expand All @@ -164,20 +163,13 @@ class VM {
~VM();
void newStackFrame(const Segment &segment);
void popStackFrame();
[[nodiscard]] uint32_t getLocal(size_t index) const;
void setLocal(size_t index, uint32_t value);
[[nodiscard]] uint32_t getGlobal(size_t index) const;
void setGlobal(size_t index, uint32_t value);
[[nodiscard]] uint64_t getDoubleLocal(size_t index) const;
void setDoubleLocal(size_t index, uint64_t value);
[[nodiscard]] uint64_t getDoubleGlobal(size_t index) const;
void setDoubleGlobal(size_t index, uint64_t value);
void pushStack(uint32_t value);
void pushDoubleStack(uint64_t value);
uint32_t popStack();
uint64_t popDoubleStack();
[[nodiscard]] uint32_t topStack() const;
[[nodiscard]] uint64_t topDoubleStack() const;
[[nodiscard]] uint64_t getLocal(size_t index) const;
void setLocal(size_t index, uint64_t value);
[[nodiscard]] uint64_t getGlobal(size_t index) const;
void setGlobal(size_t index, uint64_t value);
void pushStack(uint64_t value);
uint64_t popStack();
[[nodiscard]] uint64_t topStack() const;

void run(const Program &program);
size_t stackSize{};
Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void printTopStack(const VM &vm, const Program &program) {
std::cout << (int32_t) vm.topStack() << std::endl;
break;
case VariableType::I64: {
std::cout << vm.topDoubleStack() << std::endl;
std::cout << vm.topStack() << std::endl;
} break;
case VariableType::Object: {
auto obj = std::bit_cast<Object *>(vm.topDoubleStack());
auto obj = std::bit_cast<Object *>(vm.topStack());
if (obj->objType == Object::Type::String) {
std::cout << static_cast<StringObject *>(obj)->chars << std::endl;
}
Expand Down
10 changes: 4 additions & 6 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,14 @@ void Declaration::compile(Program &program, Segment &segment) const {
segment.declare_function(identifier.token.value,
new FunctionType(returnType, arguments),
program.segments.size());
size_t index = 0;
for (auto argument: functionDeclaration->arguments) {
for (size_t index = 0; index < functionDeclaration->arguments.size(); index++) {
auto argument = functionDeclaration->arguments[index];
newSegment.locals[argument->identifier.token.value] = Variable(
argument->identifier.token.value,
new VariableType(deduceType(program, segment, argument)),
index,
sizeOfType(varTypeConvert(functionDeclaration->returnType)));
newSegment.locals_capacity += sizeOfType(deduceType(program, segment, argument));
index += sizeOfType(varTypeConvert(argument->type.value()));
index);
}
newSegment.locals_capacity = newSegment.locals.size();
value.value()->compile(program, newSegment);
program.segments.push_back(newSegment);
} break;
Expand Down
13 changes: 0 additions & 13 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,6 @@ void typeCast(std::vector<Instruction> &instructions, VariableType::Type from, V
}
}

size_t sizeOfType(VariableType::Type type) {
switch (type) {
case VariableType::Bool:
case VariableType::I32:
return 1;
case VariableType::Object:
case VariableType::I64:
return 2;
default:
throw std::runtime_error("Invalid type");
}
}

VariableType::Type getInstructionType(const Program &program, const Instruction &instruction) {
switch (instruction.type) {
case Instruction::InstructionType::EqualI32:
Expand Down
Loading

0 comments on commit 5ef20f0

Please sign in to comment.