From a0fad7f642d0df9071068a6cb82c8870f85849f0 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 25 Jun 2025 16:48:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Avm=20=E5=A2=9E=E5=8A=A0=20byte8=20?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vm/commands/control/byte8/BCECommand.java | 76 ++++++++++++++++++ .../vm/commands/control/byte8/BCGCommand.java | 76 ++++++++++++++++++ .../commands/control/byte8/BCGECommand.java | 76 ++++++++++++++++++ .../vm/commands/control/byte8/BCLCommand.java | 76 ++++++++++++++++++ .../commands/control/byte8/BCLECommand.java | 76 ++++++++++++++++++ .../commands/control/byte8/BCNECommand.java | 77 +++++++++++++++++++ 6 files changed, 457 insertions(+) create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCECommand.java create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGCommand.java create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGECommand.java create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLCommand.java create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLECommand.java create mode 100644 src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCNECommand.java diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCECommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCECommand.java new file mode 100644 index 0000000..f7ccc5b --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCECommand.java @@ -0,0 +1,76 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCECommand class implements the {@link Command} interface and represents a conditional jump command in the virtual machine. + * This class compares two values from the stack, and if they are equal, it jumps to the specified target command. + * + *

Specific behavior:

+ * + */ +public class BCECommand implements Command { + /** + * Default constructor for creating an instance of BCECommand. + * This constructor is empty as no specific initialization is required. + */ + public BCECommand() { + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the operands are equal, jump to the target command + if (a == b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +} \ No newline at end of file diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGCommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGCommand.java new file mode 100644 index 0000000..92fb765 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGCommand.java @@ -0,0 +1,76 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCGCommand class implements the {@link Command} interface and represents a conditional jump command in the virtual machine. + * This class compares two values from the stack, and if the first value is greater than the second, it jumps to the specified target command. + * + *

Specific behavior:

+ * + */ +public class BCGCommand implements Command { + /** + * Default constructor for creating an instance of BCGCommand. + * This constructor is empty as no specific initialization is required. + */ + public BCGCommand() { + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the first operand is greater than the second, jump to the target command + if (a > b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +} diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGECommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGECommand.java new file mode 100644 index 0000000..16f468e --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCGECommand.java @@ -0,0 +1,76 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCGECommand class implements the {@link Command} interface and represents a conditional jump command in the virtual machine. + * This class compares two values from the stack, and if the first value is greater than or equal to the second, it jumps to the specified target command. + * + *

Specific behavior:

+ * + */ +public class BCGECommand implements Command { + /** + * Default constructor for creating an instance of BCGECommand. + * This constructor is empty as no specific initialization is required. + */ + public BCGECommand() { + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the first operand is greater than or equal to the second, jump to the target command + if (a >= b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +} diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLCommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLCommand.java new file mode 100644 index 0000000..1753865 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLCommand.java @@ -0,0 +1,76 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCLCommand class implements the {@link Command} interface and represents a conditional jump command in the virtual machine. + * This class compares two values from the stack, and if the first value is less than the second, it jumps to the specified target command. + * + *

Specific behavior:

+ * + */ +public class BCLCommand implements Command { + /** + * Default constructor for creating an instance of BCLCommand. + * This constructor is empty as no specific initialization is required. + */ + public BCLCommand() { + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the first operand is less than the second, jump to the target command + if (a < b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +} \ No newline at end of file diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLECommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLECommand.java new file mode 100644 index 0000000..bb90118 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCLECommand.java @@ -0,0 +1,76 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCLECommand class implements the {@link Command} interface and represents a conditional jump command in the virtual machine. + * This class compares two values from the stack, and if the first value is less than or equal to the second, it jumps to the specified target command. + * + *

Specific behavior:

+ * + */ +public class BCLECommand implements Command { + /** + * Default constructor for creating an instance of BCLECommand. + * This constructor is empty as no specific initialization is required. + */ + public BCLECommand() { + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the first operand is less than or equal to the second, jump to the target command + if (a <= b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +} diff --git a/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCNECommand.java b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCNECommand.java new file mode 100644 index 0000000..a474570 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/control/byte8/BCNECommand.java @@ -0,0 +1,77 @@ +package org.jcnc.snow.vm.commands.control.byte8; + +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; +import org.jcnc.snow.vm.utils.LoggingUtils; + +/** + * The BCNECommand class implements the {@link Command} interface and represents a conditional jump command + * in the virtual machine that triggers if two values are not equal. + * + *

Specific behavior:

+ * + */ +public class BCNECommand implements Command { + /** + * Default constructor for creating an instance of BCNECommand. + * This constructor is empty as no specific initialization is required. + */ + public BCNECommand() { + + // Empty constructor + } + + /** + * Executes the virtual machine instruction's operation. + * + *

This method retrieves the necessary data from the virtual machine stack and local variable store based on the instruction's + * specific implementation, performs the operation, and updates the program counter (PC) to reflect the next instruction + * to be executed.

+ * + *

The parameters provided allow the command to manipulate the operand stack, modify the local variables, and control the flow + * of execution by updating the program counter.

+ * + *

The exact behavior of this method will depend on the specific instruction being executed (e.g., arithmetic, branching, + * function calls, etc.). For example, a `CALL` instruction will modify the call stack by pushing a new frame, + * while a `POP` instruction will remove an item from the operand stack.

+ * + * @param parts The array of instruction parameters, which usually includes the operator and related arguments + * (such as target addresses, values, or function names). These parameters may vary based on + * the instruction being executed. + * @param currentPC The current program counter-value, indicating the address of the instruction being executed. + * This value is typically incremented after the execution of each instruction to point to the next one. + * @param operandStack The virtual machine's operand stack manager, responsible for performing operations on the operand stack, + * such as pushing, popping, and peeking values. + * @param localVariableStore The local variable store, typically used to manage method-local variables during instruction execution. + * The store may not be used in every command but can be leveraged by instructions that require access + * to local variables. + * @param callStack The virtual machine's call stack, which keeps track of the method invocation hierarchy. It is used by + * instructions that involve method calls or returns (such as `CALL` and `RETURN` instructions). + * @return The updated program counter-value, typically the current program counter-value incremented by 1, unless the + * instruction modifies control flow (such as a `JUMP` or `CALL`), in which case it may return a new address + * corresponding to the target of the jump or the subroutine to call. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, LocalVariableStore localVariableStore, CallStack callStack) { + // Parse the target command address + int target = Integer.parseInt(parts[1]); + + // Pop the two operands from the stack + byte b = (byte) operandStack.pop(); + byte a = (byte) operandStack.pop(); + + // If the operands are not equal, jump to the target command + if (a != b) { + LoggingUtils.logInfo("Jumping to command", String.valueOf(target)); + return target; + } + + return currentPC + 1; + } +}