diff --git a/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2BCommand.java b/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2BCommand.java new file mode 100644 index 0000000..cde429c --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2BCommand.java @@ -0,0 +1,48 @@ +package org.jcnc.snow.vm.commands.type.conversion; + +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; + +/** + * L2BCommand Opcode: Represents the type conversion operation from long64 to byte8 in the virtual machine. + *

This opcode is implemented by the {@link L2BCommand} class, which defines its specific execution logic.

+ * + *

Execution Steps:

+ *
    + *
  1. Pop the top long64 value from the operand stack.
  2. + *
  3. Convert the long64 value to a byte8 value.
  4. + *
  5. Push the converted byte8 value back onto the operand stack for subsequent operations.
  6. + *
+ * + *

This opcode is used to widen a long64 value to a byte8 type for high-precision floating-point computations.

+ */ +public class L2BCommand implements Command { + + /** + * Default constructor for creating an instance of L2BCommand. + */ + public L2BCommand() { + // Empty constructor + } + + /** + * Executes the long64 to byte8 conversion operation. + * + * @param parts The array of instruction parameters, which is not used in this operation. + * @param currentPC The current program counter, representing the instruction address. + * @param operandStack The operand stack of the virtual machine. + * @param localVariableStore The local variable store for managing method-local variables. + * @param callStack The call stack of the virtual machine. + * @return The updated program counter after execution. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, + LocalVariableStore localVariableStore, CallStack callStack) { + long value = (long) operandStack.pop(); + byte convertedValue = (byte) value; + operandStack.push(convertedValue); + return currentPC + 1; + } +} diff --git a/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2SCommand.java b/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2SCommand.java new file mode 100644 index 0000000..54652d6 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/type/conversion/L2SCommand.java @@ -0,0 +1,48 @@ +package org.jcnc.snow.vm.commands.type.conversion; + +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; + +/** + * L2SCommand Opcode: Represents the type conversion operation from long64 to short16 in the virtual machine. + *

This opcode is implemented by the {@link L2SCommand} class, which defines its specific execution logic.

+ * + *

Execution Steps:

+ *
    + *
  1. Pop the top long64 value from the operand stack.
  2. + *
  3. Convert the long64 value to a short16 value.
  4. + *
  5. Push the converted short16 value back onto the operand stack for subsequent operations.
  6. + *
+ * + *

This opcode is used to widen a long64 value to a short16 type for high-precision floating-point computations.

+ */ +public class L2SCommand implements Command { + + /** + * Default constructor for creating an instance of L2SCommand. + */ + public L2SCommand() { + // Empty constructor + } + + /** + * Executes the long64 to short16 conversion operation. + * + * @param parts The array of instruction parameters, which is not used in this operation. + * @param currentPC The current program counter, representing the instruction address. + * @param operandStack The operand stack of the virtual machine. + * @param localVariableStore The local variable store for managing method-local variables. + * @param callStack The call stack of the virtual machine. + * @return The updated program counter after execution. + */ + @Override + public int execute(String[] parts, int currentPC, OperandStack operandStack, + LocalVariableStore localVariableStore, CallStack callStack) { + long value = (long) operandStack.pop(); + short convertedValue = (short) value; + operandStack.push(convertedValue); + return currentPC + 1; + } +}