diff --git a/src/main/java/org/jcnc/snow/vm/commands/ref/control/RLoadCommand.java b/src/main/java/org/jcnc/snow/vm/commands/ref/control/RLoadCommand.java new file mode 100644 index 0000000..d7e314d --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/ref/control/RLoadCommand.java @@ -0,0 +1,54 @@ +package org.jcnc.snow.vm.commands.ref.control; + +import org.jcnc.snow.vm.interfaces.Command; +import org.jcnc.snow.vm.module.CallStack; +import org.jcnc.snow.vm.module.LocalVariableStore; +import org.jcnc.snow.vm.module.OperandStack; + +/** + * The {@code RLoadCommand} class implements the {@link Command} interface and represents the + * reference load instruction ({@code R_LOAD}) in the virtual machine. + * + *

+ * This instruction loads a reference object from the current stack frame’s local variable store + * at the specified slot and pushes it onto the operand stack. + *

+ * + *

Instruction format: {@code R_LOAD }

+ * + * + *

Behavior:

+ * + */ +public final class RLoadCommand implements Command { + + /** + * Executes the {@code R_LOAD} instruction, loading a reference from the local variable table and pushing it onto the operand stack. + * + * @param parts The instruction parameters. {@code parts[0]} is the operator ("R_LOAD"), {@code parts[1]} is the slot index. + * @param pc The current program counter value, indicating the instruction address being executed. + * @param stack The operand stack manager. The loaded reference will be pushed onto this stack. + * @param lvs The local variable store. (Not used directly, as this command uses the store from the current stack frame.) + * @param cs The call stack manager. The reference will be loaded from the local variable store of the top stack frame. + * @return The next program counter value ({@code pc + 1}), pointing to the next instruction. + * @throws NumberFormatException if the slot parameter cannot be parsed as an integer. + */ + @Override + public int execute(String[] parts, int pc, + OperandStack stack, + LocalVariableStore lvs, + CallStack cs) { + + int slot = Integer.parseInt(parts[1]); + Object v = cs.peekFrame().getLocalVariableStore().getVariable(slot); + stack.push(v); + return pc + 1; + } +}