feat: 支持 byte8 转 short16、long64、float32、double64

This commit is contained in:
zhangxun 2025-06-28 20:28:12 +08:00 committed by Luke
parent ae0baf3e50
commit 2289cf3ee4
4 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,47 @@
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;
/**
* B2DCommand Opcode: Represents the type conversion operation from byte8 to double64 in the virtual machine.
* <p>This opcode is implemented by the {@link B2DCommand} class, which defines its specific execution logic.</p>
*
* <p>Execution Steps:</p>
* <ol>
* <li>Pop the top byte8 value from the operand stack.</li>
* <li>Convert the byte8 value to a double64 value.</li>
* <li>Push the converted double64 value back onto the operand stack for subsequent operations.</li>
* </ol>
*
* <p>This opcode is used to widen a byte8 value to a double64 type to ensure compatibility with integer-based operations.</p>
*/
public class B2DCommand implements Command {
/**
* Default constructor for creating an instance of B2DCommand.
*/
public B2DCommand() {
// Empty constructor
}
/**
* Executes the byte8 to double64 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) {
double convertedValue = (byte) operandStack.pop();
operandStack.push(convertedValue);
return currentPC + 1;
}
}

View File

@ -0,0 +1,47 @@
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;
/**
* B2FCommand Opcode: Represents the type conversion operation from byte8 to float32 in the virtual machine.
* <p>This opcode is implemented by the {@link B2FCommand} class, which defines its specific execution logic.</p>
*
* <p>Execution Steps:</p>
* <ol>
* <li>Pop the top byte8 value from the operand stack.</li>
* <li>Convert the byte8 value to a float32 value.</li>
* <li>Push the converted float32 value back onto the operand stack for subsequent operations.</li>
* </ol>
*
* <p>This opcode is used to widen a byte8 value to a float32 type to ensure compatibility with integer-based operations.</p>
*/
public class B2FCommand implements Command {
/**
* Default constructor for creating an instance of B2FCommand.
*/
public B2FCommand() {
// Empty constructor
}
/**
* Executes the byte8 to float32 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) {
float convertedValue = (byte) operandStack.pop();
operandStack.push(convertedValue);
return currentPC + 1;
}
}

View File

@ -0,0 +1,47 @@
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;
/**
* B2LCommand Opcode: Represents the type conversion operation from byte8 to long64 in the virtual machine.
* <p>This opcode is implemented by the {@link B2LCommand} class, which defines its specific execution logic.</p>
*
* <p>Execution Steps:</p>
* <ol>
* <li>Pop the top byte8 value from the operand stack.</li>
* <li>Convert the byte8 value to a long64 value.</li>
* <li>Push the converted long64 value back onto the operand stack for subsequent operations.</li>
* </ol>
*
* <p>This opcode is used to widen a byte8 value to a long64 type to ensure compatibility with integer-based operations.</p>
*/
public class B2LCommand implements Command {
/**
* Default constructor for creating an instance of B2LCommand.
*/
public B2LCommand() {
// Empty constructor
}
/**
* Executes the byte8 to long64 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 convertedValue = (byte) operandStack.pop();
operandStack.push(convertedValue);
return currentPC + 1;
}
}

View File

@ -0,0 +1,47 @@
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;
/**
* B2SCommand Opcode: Represents the type conversion operation from byte8 to short16 in the virtual machine.
* <p>This opcode is implemented by the {@link B2SCommand} class, which defines its specific execution logic.</p>
*
* <p>Execution Steps:</p>
* <ol>
* <li>Pop the top byte8 value from the operand stack.</li>
* <li>Convert the byte8 value to a short16 value.</li>
* <li>Push the converted short16 value back onto the operand stack for subsequent operations.</li>
* </ol>
*
* <p>This opcode is used to widen a byte8 value to a short16 type to ensure compatibility with integer-based operations.</p>
*/
public class B2SCommand implements Command {
/**
* Default constructor for creating an instance of B2SCommand.
*/
public B2SCommand() {
// Empty constructor
}
/**
* Executes the byte8 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) {
short convertedValue = (byte) operandStack.pop();
operandStack.push(convertedValue);
return currentPC + 1;
}
}