From 6098a290b1d67686df16098ac4dc6f88ea111378 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 21 Jul 2025 17:18:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20R=5FLOAD=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 RLoadCommand 类,实现 Command接口 - 该指令用于从局部变量表中加载引用对象,并将其推入操作数栈- 指令格式:R_LOAD - 其中 是局部变量表中的索引 - 执行过程包括解析索引、获取引用、推入栈顶和更新程序计数器 --- .../vm/commands/ref/control/RLoadCommand.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/org/jcnc/snow/vm/commands/ref/control/RLoadCommand.java 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 }

+ *
    + *
  • {@code }: The index in the local variable table to load the reference from.
  • + *
+ * + *

Behavior:

+ *
    + *
  • Parses the slot index from the instruction parameters.
  • + *
  • Fetches the reference object stored at the specified slot in the current stack frame's local variable store.
  • + *
  • Pushes the fetched reference onto the operand stack.
  • + *
  • Increments the program counter to the next instruction.
  • + *
+ */ +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; + } +}