Question: [In Java] Please finish the VirtualMachine.java, with comments indicating hints what should go there Program.java also needs to be implemented, but do the VM first.

[In Java] Please finish the VirtualMachine.java, with comments indicating hints what should go there Program.java also needs to be implemented, but do the VM first.

//VirtualMachine.java

package interpreter;

import java.util.Stack; import interpreter.ByteCode.*;

public class VirtualMachine {

private RunTimeStack runStack; private int pc; private Stack returnAddrs; private boolean isRunning; private Program program;

protected VirtualMachine(Program program) { this.program = program; }

void executeProgram() { pc = 0; runStack = new RunTimeStack(); returnAddrs = new Stack(); isRunning = true; while(isRunning){ ByteCode code = program.getCode(pc); pc++; } } /*Note that we can easily add new bytecodes without affecting the operation of the Virtual Machine We are using dynamic binding to achieve code flexibility, extendability and readability (note how easy it is to read and understand this code).*/

} /*The returnAddrs stack stores the bytecode index (PC) that the virtual machine should execute when the current function exits. Each time a function is entered, the PC should be pushed on to the returnAddrs stack. When a function exits the PC should be restored to the value that is popped from the top of the returnAddrs stack.*/

//Program.java(

package interpreter;

import interpreter.ByteCode.*; import java.util.ArrayList;

public class Program {

private ArrayList program;

public Program() { program = new ArrayList<>(); }

protected ByteCode getCode(int pc) { return this.program.get(pc); }

public int getSize() { return this.program.size(); }

/** * This function should go through the program and resolve all addresses. * Currently all labels look like LABEL <>>, these need to be converted into * correct addresses so the VirtualMachine knows what to set the Program Counter(PC) * HINT: make note what type of data-stucture bytecodes are stored in. * * @param program Program object that holds a list of ByteCodes */ public void resolveAddrs(Program program) {

}

//RunTimeStack.java

package interpreter;

import java.util.ArrayList; import java.util.Stack;

public class RunTimeStack {

private ArrayList runTimeStack; private Stack framePointer;

public RunTimeStack() { runTimeStack = new ArrayList<>(); framePointer = new Stack<>(); //Add initial Frame Pointer, main is the entry // point of our language, so its frame pointer is 0. framePointer.add(0); } public void dump(){ //Dump the RunTimeStack information for debugging ArrayList stack = (ArrayList) runTimeStack.clone(); ArrayList framePtrs = (ArrayList) framePointer.clone(); ArrayList frameArray[] = new ArrayList[framePtrs.size()];

for (int i = framePtrs.size(); i > 0; i--) { frameArray[i - 1] = new ArrayList(); int startIndex = framePtrs.get(i - 1); int currentSize = stack.size(); for (int k = startIndex; k < currentSize; k++) { frameArray[i - 1].add(stack.remove(startIndex)); } }

for (int i = 0; i < framePtrs.size(); i++) {

System.out.print(frameArray[i].toString());

}

System.out.println(); } public int peek(){ //Returns the top item on the runtime stack return (int)runTimeStack.get(runTimeStack.size() - 1); } public int pop(){ //Pop the top item from the runtime stack //Returns that item int top = (int)runTimeStack.get(runTimeStack.size() - 1); runTimeStack.remove(runTimeStack.size() - 1); return top; }

public int push(int i){ //i - push this item on the runtime stack //Returns the item just pushed //runTimeStack.add(framePointer.push(i)); runTimeStack.add(i); return i; }

public int newFrameAt(int offset){ //Start new frame /* offset - indicates the number of slots down from the top of RunTimeStack for starting the new frame */ int size = runTimeStack.size(); framePointer.push(size - offset); return 0; } public void popframe(){ /*We pop the top frame when we return from a function; before popping, the functions return value is at the top of the stack so well save the value, pop the top frame and then push the return value*/ int top = this.pop(); int currentSize = runTimeStack.size(); int startIndex = framePointer.pop(); for (int i = startIndex; i < currentSize; i++) { runTimeStack.remove(startIndex); } this.push(top); } public int store(int offset){ /*Used to store into variables*/ int top = this.pop(); runTimeStack.add(framePointer.peek() + offset, top); runTimeStack.remove(framePointer.peek() + offset + 1);

return top; } public int load(int offset){ /*Used to load variables onto the stack*/ return (int)runTimeStack.get(framePointer.peek() + offset); }

}

//ByteCode.java

package interpreter.ByteCode;

public abstract class ByteCode { public abstract void init(String param); abstract void execute(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!