-
Notifications
You must be signed in to change notification settings - Fork 0
added virtual machine #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.jengine.virtualmachine; | ||
|
|
||
| import ru.jengine.virtualmachine.commands.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| List<Object> firstStorage = new ArrayList<>(Arrays.asList(5, 7, 2)); | ||
| byte[] firstCode = { | ||
| 0b00, | ||
| 0, | ||
| 0b00, | ||
| 1, | ||
| 0b100, | ||
| 0b00, | ||
| 2, | ||
| 0b01 | ||
| }; | ||
| VirtualMachine virtualMachine = new VirtualMachine(List.of(new Add(), new Const(), new Sub(), new Div(), new Mul(), new Jump())); | ||
| virtualMachine.registerScript(firstStorage, firstCode); | ||
| virtualMachine.executeRegisteredContexts(); | ||
|
|
||
| // TODO: Handle errors? | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package ru.jengine.virtualmachine; | ||
|
|
||
| import java.util.ArrayDeque; | ||
|
|
||
| public class VMStack { | ||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final int stackSize; | ||
| private final ArrayDeque<Object> stack; | ||
|
|
||
| public VMStack(int capacity) { | ||
| stackSize = capacity; | ||
| stack = new ArrayDeque<>(capacity); | ||
| } | ||
|
|
||
| public Object pop() { | ||
| return stack.pop(); | ||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void push(Object element) { | ||
| if (stack.size() == stackSize) { | ||
| throw new StackOverflowError("VM's stack has overflowed."); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут давай кидать тоже |
||
| } | ||
| stack.push(element); | ||
| } | ||
|
|
||
| public Object peek() { | ||
| return stack.peek(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ru.jengine.virtualmachine; | ||
|
|
||
| import ru.jengine.virtualmachine.commands.*; | ||
| import ru.jengine.virtualmachine.context.ScriptContext; | ||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class VirtualMachine { | ||
|
|
||
| private final Map<Byte, Command> commandsMap; | ||
|
|
||
| private final List<ScriptContext> contexts = new ArrayList<>(); | ||
|
|
||
| public VirtualMachine(List<Command> commands) { | ||
| commandsMap = commands.stream().collect(Collectors.toMap(Command::getOpCode, Function.identity())); | ||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void executeRegisteredContexts() { | ||
| for (ScriptContext context : this.contexts) { | ||
| execute(context); | ||
| } | ||
| } | ||
|
|
||
| private Command extractNextCommand(ScriptContext context, byte[] code) { | ||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| byte opCode = code[context.getInstructionPointer()]; | ||
| return commandsMap.get(opCode); | ||
| } | ||
|
|
||
| public void registerScript(List<Object> initialStorage, byte[] bytecode) { | ||
| ScriptContext scriptContext = new ScriptContext(initialStorage, bytecode); | ||
| contexts.add(scriptContext); | ||
| } | ||
|
|
||
| private void execute(ScriptContext context) { | ||
| byte[] code = context.getCode(); | ||
| while (context.getInstructionPointer() < code.length) { | ||
| Command command = extractNextCommand(context, code); | ||
| try { | ||
| int offset = command.execute(context); | ||
| context.moveInstructionPointer(offset); | ||
| } catch (VirtualMachineException e) { | ||
| throw e; // TODO: ??? | ||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Предлагаю начать писать тесты :) По сути весь данный пакет должен перекочевать в тестовую структуру, за исключением интерфейса
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| public class Add implements Command { | ||
| private static final byte[] opCode = {0b0000_0001}; | ||
| private static final int offset = 1; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
| Object firstOp = context.popFromStack(); | ||
| Object secondOp = context.popFromStack(); | ||
|
|
||
| if (firstOp instanceof Number && secondOp instanceof Number) { | ||
| Number result = BinaryHandler.sumTwoNumbers((Number) firstOp, (Number) secondOp); | ||
| context.pushToStack(result); | ||
| } else throw new VirtualMachineException("Add operation uses two instances of Number."); | ||
|
|
||
| return offset; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
|
|
||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| public class BinaryHandler { | ||
| public static Number sumTwoNumbers(Number firstOp, Number secondOp) { | ||
| if (firstOp instanceof Double || secondOp instanceof Double) { | ||
| return firstOp.doubleValue() + secondOp.doubleValue(); | ||
| } else if (firstOp instanceof Float || secondOp instanceof Float) { | ||
| return firstOp.floatValue() + secondOp.floatValue(); | ||
| } else if (firstOp instanceof Long || secondOp instanceof Long) { | ||
| return firstOp.longValue() + secondOp.longValue(); | ||
| } else { | ||
| return firstOp.intValue() + secondOp.intValue(); | ||
| } | ||
| } | ||
|
|
||
| public static Number mulTwoNumbers(Number firstOp, Number secondOp) { | ||
| if (firstOp instanceof Double || secondOp instanceof Double) { | ||
| return firstOp.doubleValue() * secondOp.doubleValue(); | ||
| } else if (firstOp instanceof Float || secondOp instanceof Float) { | ||
| return firstOp.floatValue() * secondOp.floatValue(); | ||
| } else if (firstOp instanceof Long || secondOp instanceof Long) { | ||
| return firstOp.longValue() * secondOp.longValue(); | ||
| } else { | ||
| return firstOp.intValue() * secondOp.intValue(); | ||
| } | ||
| } | ||
|
|
||
| public static Number subTwoNumbers(Number firstOp, Number secondOp) { | ||
| if (firstOp instanceof Double || secondOp instanceof Double) { | ||
| return firstOp.doubleValue() - secondOp.doubleValue(); | ||
| } else if (firstOp instanceof Float || secondOp instanceof Float) { | ||
| return firstOp.floatValue() - secondOp.floatValue(); | ||
| } else if (firstOp instanceof Long || secondOp instanceof Long) { | ||
| return firstOp.longValue() - secondOp.longValue(); | ||
| } else { | ||
| return firstOp.intValue() - secondOp.intValue(); | ||
| } | ||
| } | ||
|
|
||
| public static Number divTwoNumbers(Number firstOp, Number secondOp) { | ||
| if (secondOp.doubleValue() < 1.0E-10) { | ||
| throw new VirtualMachineException("Division by zero is restricted."); | ||
| } | ||
|
|
||
| if (firstOp instanceof Double || secondOp instanceof Double) { | ||
| return firstOp.doubleValue() / secondOp.doubleValue(); | ||
| } else if (firstOp instanceof Float || secondOp instanceof Float) { | ||
| return firstOp.floatValue() / secondOp.floatValue(); | ||
| } else if (firstOp instanceof Long || secondOp instanceof Long) { | ||
| return firstOp.doubleValue() / secondOp.longValue(); | ||
| } else { | ||
| return firstOp.floatValue() / secondOp.intValue(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
|
|
||
| public interface Command { | ||
| Byte getOpCode(); | ||
|
|
||
| int execute(CommandContext context); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
|
|
||
| public class Const implements Command { | ||
| private static final byte[] opCode = {0b0000_0000}; | ||
| private static final int offset = 2; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; // TODO: Byte[] opcode | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Давай, наверное, унаследуем |
||
| byte storageIndex = context.readBytes(1)[0]; | ||
| Object constValue = context.getElementByIndex(storageIndex); | ||
| context.pushToStack(constValue); | ||
| return offset; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| public class Div implements Command { | ||
| private static final byte[] opCode = {0b0000_0100}; | ||
| private static final int offset = 1; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
| Object firstOp = context.popFromStack(); | ||
| Object secondOp = context.popFromStack(); | ||
| if (firstOp instanceof Number && secondOp instanceof Number) { | ||
| try { | ||
| Number result = BinaryHandler.divTwoNumbers((Number) firstOp, (Number) secondOp); | ||
| context.pushToStack(result); | ||
| } catch (VirtualMachineException e) { | ||
| throw e; // TODO: ??? | ||
| } | ||
| } else throw new VirtualMachineException("Divide operation uses two instances of Number."); | ||
| return offset; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
|
|
||
| public class Jump implements Command { | ||
|
|
||
| private static final byte[] opCode = {0b0111_1111}; | ||
| private static final int offset = 1; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
| byte storageIndex = context.readBytes(1)[0]; | ||
| Integer offsetValue = (Integer) context.getElementByIndex(storageIndex); | ||
| return offsetValue; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| public class Mul implements Command { | ||
| private static final byte[] opCode = {0b0000_0011}; | ||
| private static final int offset = 1; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
| Object firstOp = context.popFromStack(); | ||
| Object secondOp = context.popFromStack(); | ||
| if (firstOp instanceof Number && secondOp instanceof Number) { | ||
| Number result = BinaryHandler.mulTwoNumbers((Number) firstOp, (Number) secondOp); | ||
| context.pushToStack(result); | ||
| } else throw new VirtualMachineException("Multiply operation uses two instances of Number."); | ||
| return offset; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.jengine.virtualmachine.commands; | ||
|
|
||
| import ru.jengine.virtualmachine.context.CommandContext; | ||
| import ru.jengine.virtualmachine.exceptions.VirtualMachineException; | ||
|
|
||
| public class Sub implements Command { | ||
| private static final byte[] opCode = {0b0000_0010}; | ||
| private static final int offset = 1; | ||
|
|
||
| @Override | ||
| public Byte getOpCode() { | ||
| return opCode[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(CommandContext context) { | ||
| Object firstOp = context.popFromStack(); | ||
| Object secondOp = context.popFromStack(); | ||
| if (firstOp instanceof Number && secondOp instanceof Number) { | ||
| Number result = BinaryHandler.subTwoNumbers((Number) firstOp, (Number) secondOp); | ||
| context.pushToStack(result); | ||
| } else throw new VirtualMachineException("Sub operation uses two instances of Number."); | ||
| return offset; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ru.jengine.virtualmachine.context; | ||
|
|
||
| public interface CommandContext { | ||
|
|
||
Kimislazuli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Object popFromStack(); | ||
|
|
||
| Object peekFromStack(); | ||
|
|
||
| void pushToStack(Object element); | ||
|
|
||
| Object getElementByIndex(Byte index); | ||
|
|
||
| byte[] readBytes(int n); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.