From e7c7451004d8b5d99ee68212771332bf02bfe385 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 21 Jul 2025 17:16:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20CallCommand=20?= =?UTF-8?q?=E7=B1=BB=E5=B9=B6=E4=BC=98=E5=8C=96=E6=96=87=E6=A1=A3=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=20-=20=E9=87=8D=E6=96=B0=E7=BB=84=E7=BB=87=E7=B1=BB?= =?UTF-8?q?=E6=96=87=E6=A1=A3=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=AF=B9=20CallCo?= =?UTF-8?q?mmand=20=E5=8A=9F=E8=83=BD=E5=92=8C=E8=A1=8C=E4=B8=BA=E7=9A=84?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E6=8F=8F=E8=BF=B0=20-=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20execute=20=E6=96=B9=E6=B3=95=E7=9A=84=E8=AF=A6=E7=BB=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E6=98=8E=E7=A1=AE=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=92=8C=E8=BF=94=E5=9B=9E=E5=80=BC=E7=9A=84=E7=94=A8=E9=80=94?= =?UTF-8?q?=20-=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=AB=98=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C?= =?UTF-8?q?=E5=8F=AF=E7=BB=B4=E6=8A=A4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vm/commands/flow/control/CallCommand.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jcnc/snow/vm/commands/flow/control/CallCommand.java b/src/main/java/org/jcnc/snow/vm/commands/flow/control/CallCommand.java index befbfea..1171885 100644 --- a/src/main/java/org/jcnc/snow/vm/commands/flow/control/CallCommand.java +++ b/src/main/java/org/jcnc/snow/vm/commands/flow/control/CallCommand.java @@ -5,12 +5,48 @@ import org.jcnc.snow.vm.interfaces.Command; import org.jcnc.snow.vm.module.*; /** - * CALL addr nArgs — pushes a new stack-frame, transfers the specified - * argument count from the operand stack into the callee’s local slots - * 0‥n-1 (left-to-right), then jumps to {@code addr}. + * The CallCommand class implements the {@link Command} interface and represents a subroutine/function call + * instruction in the virtual machine. + *

+ * This command facilitates method invocation by creating a new stack frame, transferring arguments + * from the operand stack to the callee's local variable store, and jumping to the specified target address. + *

+ * + *

Specific behavior:

+ * */ public class CallCommand implements Command { + /** + * Executes the CALL instruction, initiating a subroutine/function call within the virtual machine. + *

+ * This method handles the creation of a new stack frame for the callee, argument passing, + * and control transfer to the target function address. + *

+ * + * @param parts The instruction parameters. Must include: + * + * @param currentPC The current program counter, used to record the return address for after the call. + * @param operandStack The operand stack manager. Arguments are popped from this stack. + * @param callerLVS The local variable store of the caller function (not directly modified here). + * @param callStack The virtual machine's call stack manager, used to push the new stack frame. + * @return The new program counter value, which is the address of the callee function (i.e., jump target). + * The VM should transfer control to this address after setting up the call frame. + * @throws IllegalArgumentException If the instruction parameters are malformed or missing. + * @throws IllegalStateException If the operand stack does not contain enough arguments. + */ @Override public int execute(String[] parts, int currentPC,